Commit 9001420d authored by anhskohbo's avatar anhskohbo

V2 with Laravel 5 support.

Signed-off-by: default avataranhskohbo <anhskohbo@gmail.com>
parent 346b704f
language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6
......
......@@ -10,8 +10,8 @@
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.*|5.0.*"
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
......
<?php namespace Anhskohbo\NoCaptcha;
use Symfony\Component\HttpFoundation\Request;
class NoCaptcha {
const CLIENT_API = 'https://www.google.com/recaptcha/api.js';
......@@ -19,22 +21,14 @@ class NoCaptcha {
*/
protected $sitekey;
/**
* //
*
* @var string
*/
protected $lang;
/**
* //
*
* @param string $secret
* @param string $sitekey
*/
public function __construct($secret, $sitekey, $lang = null)
public function __construct($secret, $sitekey)
{
$this->lang = $lang;
$this->secret = $secret;
$this->sitekey = $sitekey;
}
......@@ -44,11 +38,11 @@ class NoCaptcha {
*
* @return string
*/
public function display($attributes = array())
public function display($attributes = [], $lang = null)
{
$attributes['data-sitekey'] = $this->sitekey;
$html = '<script src="'.$this->getJsLink().'" async defer></script>'."\n";
$html = '<script src="'.$this->getJsLink($lang).'" async defer></script>'."\n";
$html .= '<div class="g-recaptcha"'.$this->buildAttributes($attributes).'></div>';
return $html;
......@@ -65,11 +59,11 @@ class NoCaptcha {
{
if (empty($response)) return false;
$response = $this->sendRequestVerify(array(
$response = $this->sendRequestVerify([
'secret' => $this->secret,
'response' => $response,
'remoteip' => $clientIp
));
]);
return isset($response['success']) && $response['success'] === true;
}
......@@ -77,16 +71,25 @@ class NoCaptcha {
/**
* //
*
* @return string
* @param Request $request
* @return bool
*/
public function getJsLink()
{
if ($this->lang)
public function verifyRequest(Request $request)
{
return static::CLIENT_API.'?hl='.$this->lang;
return $this->verifyResponse(
$request->get('g-recaptcha-response'),
$request->getClientIp()
);
}
return static::CLIENT_API;
/**
* //
*
* @return string
*/
public function getJsLink($lang = null)
{
return $lang ? static::CLIENT_API.'?hl='.$lang : static::CLIENT_API;
}
/**
......@@ -95,7 +98,7 @@ class NoCaptcha {
* @param array $query
* @return array
*/
protected function sendRequestVerify(array $query = array())
protected function sendRequestVerify(array $query = [])
{
$link = static::VERIFY_URL.'?'.http_build_query($query);
......@@ -112,7 +115,7 @@ class NoCaptcha {
*/
protected function buildAttributes(array $attributes)
{
$html = array();
$html = [];
foreach ($attributes as $key => $value)
{
......
......@@ -20,8 +20,6 @@ class NoCaptchaServiceProvider extends ServiceProvider {
{
$app = $this->app;
$app['config']->package('anhskohbo/no-captcha', __DIR__.'/config');
$app['validator']->extend('captcha', function($attribute, $value) use ($app)
{
return $app['captcha']->verifyResponse($value, $app['request']->getClientIp());
......@@ -29,9 +27,9 @@ class NoCaptchaServiceProvider extends ServiceProvider {
if ($app->bound('form'))
{
$app['form']->macro('captcha', function($attributes = array()) use ($app)
$app['form']->macro('captcha', function($attributes = []) use ($app)
{
return $app['captcha']->display($attributes);
return $app['captcha']->display($attributes, $app->getLocale());
});
}
}
......@@ -43,12 +41,15 @@ class NoCaptchaServiceProvider extends ServiceProvider {
*/
public function register()
{
$this->publishes([
__DIR__.'/config/captcha.php' => $this->app->configPath().'/captcha.php'
]);
$this->app->bind('captcha', function($app)
{
return new NoCaptcha(
$app['config']->get('no-captcha::secret'),
$app['config']->get('no-captcha::sitekey'),
$app['config']->get('no-captcha::lang')
$app['config']['captcha.secret'],
$app['config']['captcha.sitekey']
);
});
}
......
<?php
return [
'secret' => env('NOCAPTCHA_SECRET'),
'sitekey' => env('NOCAPTCHA_SITEKEY'),
];
<?php
return array(
'secret' => getenv('NOCAPTCHA_SECRET') ?: '',
'sitekey' => getenv('NOCAPTCHA_SITEKEY') ?: '',
'lang' => app()->getLocale(),
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment