geekk/multi-captcha 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

geekk/multi-captcha

Composer 安装命令:

composer require geekk/multi-captcha

包简介

Various captchas that support the mechanism for switching between them in the configuration file

README 文档

README

Common php class interfaces for captchas. And various captchas implementation. You can use it for a switching of captcha's type in your project.

Supported types of captcha

Now packages supports these types:

  • Google ReCaptcha v2
  • HCaptcha
  • KCaptcha
  • Cloudflare Turnstile
  • Gregwar (image captcha, based on gregwar/captcha)

You can add new one type. You need implement CaptchaInterface and CaptchaRequestInterface.

Frameworks support

This package didn't tie with any framework. It doesn't work with specific framework's classes or interfaces, for example Illuminate\Http\Request for http requests.

But you can ease create needed wrappers and factories for a working your framework and configuration files.

For Laravel, you can use the geekk/multi-captcha-laravel package.

Installation

Install package:

composer require geekk/multi-captcha

Using

Configuration array:

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

        'connections' => [

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

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

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

            'gregwar' => [
                'driver' => 'gregwar',
                // optional: width, height, length, quality, allowed_symbols
            ],

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

If you plan to use KCaptcha, you need implement storage class:

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);
    }
}

where Repository is some cache repository. Or you can use session instead of cache.

Implement the CaptchaManager - factory class:

use SomeNamespace/Request; // Request which you use

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);
            case 'gregwar':
                $store = new CaptchaStore();
                return new Gregwar($this->connectionConfig, $store);
            case 'turnstile':
                return new Turnstile($this->connectionConfig);
        }
        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));
            case 'gregwar':
                return new GregwarRequest(count($request->post()), $request->post(GregwarRequest::RESPONSE_NAME), $request->post(GregwarRequest::KEY_NAME));
            case 'turnstile':
                return new TurnstileRequest(count($request->post()), $request->post(TurnstileRequest::RESPONSE_NAME), $request->ip());
        }
        throw new \Exception(sprintf('Unknown captcha driver: %s', $driverName));
    }
}

Alternative: single request via CaptchaRequest

If the frontend uses the same component for all captcha types (e.g. a Vue component with unified field names), you can avoid distinguishing request type and always create a CaptchaRequest. Form field names are defined once (e.g. captcha-response):

use Geekk\MultiCaptcha\CaptchaRequest;

public function getRequest(Request $request): CaptchaRequestInterface
{
    // For recaptcha2, hcaptcha, turnstile
    $submitted = (bool) count($request->post());
    $response = $request->post('captcha-response');
    $context = $request->ip();
    return new CaptchaRequest($submitted, $response, $context);
}

Note: for KCaptcha and Gregwar the context is the store key (sent from the frontend), not the IP, so with this approach either do not use KCaptcha/Gregwar or handle them in a separate branch (e.g. a separate if by driver).

Then you can use it

$captchaManager = new CaptchaManager($config);

$captcha = $captchaManager->getCaptcha();

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

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

Customising captcha's view

Use css for a customizing.

For captcha's templates generated on frontend side you can get data from method CaptchaInterface::getViewData().

geekk/multi-captcha 适用场景与选型建议

geekk/multi-captcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.44k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「recaptcha」 「captcha」 「kcaptcha」 「hcaptcha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 geekk/multi-captcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 geekk/multi-captcha 我们能提供哪些服务?
定制开发 / 二次开发

基于 geekk/multi-captcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-05