定制 uestla/recaptcha-control 二次开发

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

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

uestla/recaptcha-control

Composer 安装命令:

composer require uestla/recaptcha-control

包简介

reCAPTCHA control for Nette Framework forms

README 文档

README

Adds the reCAPTCHA control to Nette Framework forms.

Documentation

  1. Installation
  2. Configuration
  3. Usage
  4. Requester
  5. AJAX
  6. Invisible reCAPTCHA
  7. Testing

Installation

For easy installation use Composer:

composer require uestla/recaptcha-control

Also don't forget to include the official JavaScript library:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

Are you using AJAX? Then you may want to use library asset instead - see more.

Configuration

To be able to use the reCAPTCHA control in your forms just register the DI extension in your config.neon:

extensions:
	recaptcha: ReCaptchaControl\DI\Extension

recaptcha:
	# required
	siteKey: '<your_site_key>'
	secretKey: '<your_secret_key>'

	# optional
	methodName: 'addReCaptcha'
	requester: ReCaptchaControl\Http\Requester\CurlRequester

Parameters:

Parameter Type Default value Required Meaning
siteKey string | Nette\DI\Statement ~ YES The site key you obtain in your Google Account
secretKey string | Nette\DI\Statement ~ YES The secret key you obtain in your Google Account
methodName string "addReCaptcha" NO Extension method name you'll be calling upon your forms to add the control, e.g. $form->addReCaptcha(...)
requester string "CurlRequester" NO Name of the class or service which sends requests to the Google validation API. The default CurlRequester needs PHP cURL extension to run properly. Read more about requesters here.

Usage

Form

To actually add reCAPTCHA to your form just call

$form->addReCaptcha(
	'captcha', // control name
	'reCAPTCHA for you', // label
	"Please prove you're not a robot." // error message
);

Please note that the validation rule is added automatically so you don't need to call any addRule() at all.

Template

You can then render the control in your Latte template using both macro and n:attr approach:

<form ...>
	{* n:attr *}
	<div n:name="captcha"></div>

	{* or macro *}
	{input captcha}

	{* don't forget to render potential errors *}
	{$form['captcha']->getError()}
</form>

And there she goes! :-)

reCAPTCHA

Requester

Requester is a layer for sending HTTP requests. It comes handy when your production environment does not meet the default requirements (cURL extension etc.).

You can change the default requester by setting the requester key in configuration. The value can be either a class name or a name of another service (see details below).

  1. CurlRequester

    This is the default one since requester value is optional in configuration.

    It uses PHP cURL extension. If you want to set any CURLOPT_* value for the requests you have to create the service aside and pass these options to the constructor:

    recaptcha:
    	...
    	requester: @curlRequester
    
    services:
    	curlRequester:
    		class: ReCaptchaControl\Http\Requester\CurlRequester
    		arguments:
    			-
    				CURLOPT_CAINFO: %appDir%/res/cacert.pem
    				CURLOPT_USERAGENT: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36'

    As you can see in the example it is possible to use all CURLOPT_* constants as string keys (they are converted internally).

  2. SimpleRequester

    Calls file_get_contents() with stream context.

    recaptcha:
    	...
    	requester: ReCaptchaControl\Http\Requester\SimpleRequester
  3. GuzzleRequester

    If you're already using the Guzzle HTTP Client in your application, this requester may come handy:

    recaptcha:
    	...
    	requester: ReCaptchaControl\Http\Requester\GuzzleRequester
    
    services:
    	- Guzzle\Http\Client # will be autowired to the GuzzleRequester constructor

    If you're using multiple clients it is better to define the requester as a service aside:

    recaptcha:
    	...
    	requester: @guzzleRequester
    
    services:
    	guzzleRequester: ReCaptchaControl\Http\Requester\GuzzleRequester(@primaryGuzzleHttpClient)
    
    	primaryGuzzleHttpClient: Guzzle\Http\Client
    	secondaryGuzzleHttpClient: Guzzle\Http\Client
  4. Custom requester

    You can also implement your own requester. Just make sure it implements the ReCaptchaControl\Http\Requester\IRequester interface.

    It basically requires a single public function post(string $url, array $values = []): string method which takes URL, performs a HTTP POST request with given $values and returns body of the response as a string. In case of a failure, ReCaptchaControl\Http\Requester\RequestException should be thrown.

    You can then use it the same way as above:

    recaptcha:
    	requester: MyRequesterClass

    or when you have some dependencies:

    recaptcha:
    	requester: @myRequester
    
    services:
    	myRequester:
    		factory: MyRequesterClass( ... )
    		...

AJAX

When a snippet containing reCAPTCHA control gets updated, the reCAPTCHA itself needs to be re-rendered.

If you're using the nette.ajax.js, you may want to use the assets/recaptcha.ajax.js script.

You can install it via bower:

bower install

IMPORTANT: The recaptcha.ajax.js script loads the official JavaScript library because it needs to render the reCATPCHAs explicitely. So please be careful not to loaded by yourself as well.

Invisible reCAPTCHA

You can also use this library for Invisible reCAPTCHA. The backend part stays the same so it only needs proper configuration in the frontend. To see it in action you can visit https://kesspess.cz/recaptcha/invisible and the code in tests.

Tests & CI

Automated tests

This library uses Nette Tester for automated testing and PHPStan for static analysis. For running them yourself simply run

# runs test suite & static analysis
composer ci

Manual testing

You may have noticed the tests/manual directory. Its content is actualy live at https://kesspess.cz/recaptcha.

To get it to work on your local machine, do following:

  1. copy config/local.neon.template to config/local.neon
  2. fill reCAPTCHA keys properly in config/local.neon
  3. run composer install
  4. run bower install

After that you should be able to run it via your local web server.

The form definitions are in TestPresenter.

Individual example templates are located in presenters/templates directory.

uestla/recaptcha-control 适用场景与选型建议

uestla/recaptcha-control 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 578.17k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2014 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 578.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 2
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-06-17