定制 saeven/zf2-circlical-recaptcha 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

saeven/zf2-circlical-recaptcha

Composer 安装命令:

composer require saeven/zf2-circlical-recaptcha

包简介

ZF2 module that lets you easily incorporate Google's new, simpler recaptcha (I am not a robot)

README 文档

README

Total Downloads Codacy Badge Codacy Badge Build Status

Google just rolled out their great new CAPTCHA (fewer angry old people is always great!), and you want to get it into your ZF2/ZF3 project! Please users and management alike with this easy module.

Captcha Image

Requirements

Add this line to your composer.json

"saeven/zf2-circlical-recaptcha": "dev-master"

Then include 'CirclicalRecaptcha', in your application's module.config.php. The module should now be loaded.

Configuration

Copy circlical.recaptcha.local.php into your config/autoload folder. Open it up, and insert your ReCaptcha keys - you get these from Google's website.

<?php
return [
    'circlical' => [
        'recaptcha' => [
            'client' => 'yourclientkeygoeshere',
            'server' => 'yourserverkeygoeshere',
            'bypass' => false,
            'default_timeout' => 900,
        ],
    ],
];

A third parameter is there, to help you work through functional tests (e.g., behat). You could set 'bypass' to be true (don't validate the captcha) based on some fixed environment variable, example:

'bypass' => getenv('SOMEKEY') === 'development'

The fourth is the timeout (in seconds) that you permit between the time the captcha is served, and the time that it is solved.

Templates

You need to add the captcha to your form templates, E.g., using Twig it'd look like:

<div class="form-group" style="margin-bottom:25px;margin-top:25px;">
    {{ formLabel( form.get('g-recaptcha-response') ) }}
    {{ recaptcha( form.get('g-recaptcha-response') ) }}
    <div class="error_container alert alert-danger">{{ formElementErrors( form.get('g-recaptcha-response') ) }}</div>
</div>

For now, the g-recaptcha-response is not mutable. The reason being that Google renders it with this name, there's no option to change

Form & InputFilter

If you're unfamiliar with ZF2, here's sample form code that implements the Captcha. There's more than needed, but you can see how the Element is added, and similarly, how the counterpart InputFilter (validator) is added as well.

<?php

namespace CirclicalUser\Form;

use Laminas\Form\Element,
    Laminas\Captcha,
    Laminas\InputFilter,
    Laminas\Form\Element\Password,
    Laminas\Form\Element\Text,
    Laminas\Form\Form,
    CirclicalUser\Form\Element\Recaptcha,
    Laminas\Form\Element\Button;


class UserForm extends Form
{

    const       EMAIL = 'email';

    public function __construct( $name, $options = array() )
    {
        parent::__construct( $name, $options );
    }

    /**
     * Construct a registration form, with an AuthenticationFormInterface instance to establish minimum field count
     */
    public function init()
    {

          $this->add([
              'name'    => 'g-recaptcha-response',
              'type'    => Recaptcha::class,
              'options' => [
                  'label'     => _( "Please complete the challenge below" ),
                  'no_sitekey' => false,
                  'no_script' => false,
                  'language' => 'en', // see https://developers.google.com/recaptcha/docs/language
              ],
          ]);


        $this->add([
            'name'      => self::EMAIL,
            'type'      => self::EMAIL,
            'options' => [
                'label' => _( 'Email' ),
            ],
            'attributes' => [
                'maxlength' => 254,
            ],
        ]);

        $this->add([
            'name' => 'email_confirm',
            'type' => self::EMAIL,
            'options' => [
                'label' => _( "Confirm Email" ),
            ],
            'attributes' => [
                'maxlength' => 254,
            ],
        ]);


        $this->add([
            'name' => 'submit',
            'type' => Button::class,
            'options' => [
                'label' => _( "Submit" ),
            ],
            'attributes' => [
                'class' => 'btn btn-primary',
                'type'  => 'submit',
            ]
        ]);
    }
}

And here's a sample InputFilter

<?php

namespace CirclicalUser\InputFilter;

use CirclicalUser\Form\Validator\RecaptchaValidator;
use Doctrine\Common\Persistence\ObjectRepository;
use DoctrineModule\Validator\NoObjectExists;
use Laminas\Filter\StringToLower;
use Laminas\Filter\StringTrim;
use Laminas\InputFilter\InputFilter;
use Laminas\Form\Element;
use Laminas\Captcha;
use CirclicalUser\Form\Filter\ArrayBlock;
use HTMLPurifier;
use Laminas\Validator\EmailAddress;
use Laminas\Validator\StringLength;

class UserInputFilter extends InputFilter implements UserInputFilterInterface
{
    const EMAIL     = 'email';
    const RECAPTCHA = 'g-recaptcha-response';

    protected $userRepository;
    protected $has_captcha;


    public function __construct( ObjectRepository $userRepository, $has_captcha )
    {
        $this->userRepository = $userRepository;
        $this->has_captcha    = $has_captcha;
    }

    public function init()
    {

        if( $this->has_captcha )
        {
            $this->add([
                'name' => self::RECAPTCHA,
                'required' => true,
                'messages' => [_("Please complete the anti-robot check!")],
                'validators' => [
                    ['name' => \CirclicalRecaptcha\Form\Validator\RecaptchaValidator::class,],
                ],
            ]);

            $this->get( self::RECAPTCHA )->setBreakOnFailure( true );
        }

        $this->add([
            'name' => 'email',
            'required' => true,
            'filters' => [
                ['name' => ArrayBlock::class],
                ['name' => StringTrim::class],
                ['name' => HTMLPurifier::class],
                ['name' => StringToLower::class],
            ],
            'validators' => [
                [
                    'name' => EmailAddress::class,
                    'options' => [
                        'useMxCheck'        => true,
                        'useDeepMxCheck'    => true,
                        'useDomainCheck'    => true,
                        'message'           => _( "That email address has a typo in it, or its domain can't be checked" ),
                    ],
                ],

                [
                    'name' => NoObjectExists::class,
                    'options' => [
                        'fields'            => ['email'],
                        'messages'          => [
                            NoObjectExists::ERROR_OBJECT_FOUND => _( "That email is already taken, please log in instead" ),
                        ],
                        'object_repository' => $this->userRepository,
                    ],
                ],
            ],
        ]);

        $this->add([
            'name' => 'email_confirm',
            'required' => true,
            'filters' => [
                ['name' => ArrayBlock::class],
                ['name' => StringTrim::class],
                ['name' => HTMLPurifier::class],
                ['name' => StringToLower::class],
            ],
            'validators' => [
                [
                    'name' => 'identical',
                    'options' => [
                        'message' => _( "Your email and confirmation email are different" ),
                        'token' => self::EMAIL,
                    ],
                ],
            ],
        ]);
    }
}

That's all there is to it! Note the optional no_sitekey and no_script options on the form init. These are handy if you are sticking many recaptchas on the same view, but need to dynamically fade them in and out.

saeven/zf2-circlical-recaptcha 适用场景与选型建议

saeven/zf2-circlical-recaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.48k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 saeven/zf2-circlical-recaptcha 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 30.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-12