geekk/multi-captcha

支持在配置文件中切换的多种验证码机制

1.1.5 2021-06-09 08:34 UTC

This package is auto-updated.

Last update: 2024-09-09 15:36:41 UTC


README

验证码的常用php类接口。以及各种验证码的实现。您可以在项目中使用它来切换验证码的类型。

支持的验证码类型

当前包支持以下类型

  • Google ReCaptcha v2
  • HCaptcha
  • KCaptcha

您可以添加新的类型。您需要实现 CaptchaInterface 和 CaptchaRequestInterface。

框架支持

这个包没有绑定到任何框架。它不与特定框架的类或接口一起工作,例如 http 请求的 Illuminate\Http\Request

但是,您可以轻松创建所需的包装器和工厂,以便在框架和配置文件中工作。

对于 Laravel,您可以使用 geekk/multi-captcha-laravel 包。

安装

安装包

composer require geekk/multi-captcha

使用

配置数组

$config = [
        'default' => 'hcaptcha',

        'connections' => [

            'recaptcha2' => [
                'driver' => 'recaptcha2',
                'site_key' => '...',
                'secret_key' => '...'
            ],

            'hcaptcha' => [
                'driver' => 'hcaptcha',
                'site_key' => '...',
                'secret_key' => '...'
            ],

            'kcaptcha' => [
                'driver' => 'kcaptcha',
                'show_credits' => false
            ]
        ]
]

如果您打算使用 KCaptcha,您需要实现存储类

class CaptchaStore  implements CaptchaStoreInterface {

    protected $store;
    protected $prefix;
    protected $seconds;

    public function __construct(Repository $store, $prefix = 'kcaptcha:', int $seconds = 5*60)
    {
        $this->store = $store;
        $this->prefix = $prefix;
        $this->seconds = $seconds;
    }

    protected function getStoreKey($key)
    {
        return "$this->prefix:{$key}";
    }

    public function getValue(?string $key = null): ?string
    {
        $value = $this->store->get($this->getStoreKey($key));
        $this->store->forget($this->getStoreKey($key));
        return $value;
    }

    public function setValue(string $value, ?string $key = null)
    {
        $this->store->put($this->getStoreKey($key), $value);
    }
}

其中 Repository 是某个缓存存储库。或者您可以使用会话代替缓存。

实现 CaptchaManager - 工厂类

class CaptchaManager
{

    protected $config;

    protected $connectionName;

    protected $connectionConfig;

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    private function loadDriverConfig()
    {
        $this->connectionName = $this->config['default'];
        $this->connectionConfig = $this->config['connections'][$this->connectionName];
    }

    public function getCaptcha(): CaptchaInterface
    {
        $this->loadDriverConfig();
        $driverName = $this->connectionConfig['driver'];
        switch ($driverName) {
            case 'recaptcha2':
                return new ReCaptcha2($this->connectionConfig);
            case 'hcaptcha':
                return new HCaptcha($this->connectionConfig);
            case 'kcaptcha':
                $store = new CaptchaStore();
                return new KCaptcha($this->connectionConfig, $store);
        }
        throw new \Exception(sprintf('Unknown captcha driver: %s', $driverName));
    }

    public function getRequest(Request $request): CaptchaRequestInterface
    {
        $driverName = $this->connectionConfig['driver'];
        switch ($driverName) {
            case 'recaptcha2':
                return new ReCaptcha2Request(count($request->post()), $request->post(ReCaptcha2Request::RESPONSE_NAME), $request->ip());
            case 'hcaptcha':
                return new HCaptchaRequest(count($request->post()), $request->post(HCaptchaRequest::RESPONSE_NAME), $request->ip());
            case 'kcaptcha':
                return new KCaptchaRequest(count($request->post()),$request->post(KCaptchaRequest::RESPONSE_NAME), $request->post(KCaptchaRequest::KEY_NAME));
        }
        throw new \Exception(sprintf('Unknown captcha driver: %s', $driverName));
    }
}

然后您就可以使用它了

$captchaManager = new CaptchaManager($config);

$captcha = $captchaManager->getCaptcha();

// Render captcha in template
echo $captcha->render();

// Verify user's response
$result = $captcha->verify($captchaManager->getRequest($request));

自定义验证码视图

使用 CSS 进行自定义。

对于在前端生成的验证码模板,您可以从方法 CaptchaInterface::getViewData() 获取数据。