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 language: php
php: php:
- 5.3
- 5.4 - 5.4
- 5.5 - 5.5
- 5.6 - 5.6
......
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
} }
], ],
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.4.0",
"illuminate/support": "4.*|5.0.*" "illuminate/support": "5.0.*"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
<?php namespace Anhskohbo\NoCaptcha; <?php namespace Anhskohbo\NoCaptcha;
use Symfony\Component\HttpFoundation\Request;
class NoCaptcha { class NoCaptcha {
const CLIENT_API = 'https://www.google.com/recaptcha/api.js'; const CLIENT_API = 'https://www.google.com/recaptcha/api.js';
...@@ -19,22 +21,14 @@ class NoCaptcha { ...@@ -19,22 +21,14 @@ class NoCaptcha {
*/ */
protected $sitekey; protected $sitekey;
/**
* //
*
* @var string
*/
protected $lang;
/** /**
* // * //
* *
* @param string $secret * @param string $secret
* @param string $sitekey * @param string $sitekey
*/ */
public function __construct($secret, $sitekey, $lang = null) public function __construct($secret, $sitekey)
{ {
$this->lang = $lang;
$this->secret = $secret; $this->secret = $secret;
$this->sitekey = $sitekey; $this->sitekey = $sitekey;
} }
...@@ -44,11 +38,11 @@ class NoCaptcha { ...@@ -44,11 +38,11 @@ class NoCaptcha {
* *
* @return string * @return string
*/ */
public function display($attributes = array()) public function display($attributes = [], $lang = null)
{ {
$attributes['data-sitekey'] = $this->sitekey; $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>'; $html .= '<div class="g-recaptcha"'.$this->buildAttributes($attributes).'></div>';
return $html; return $html;
...@@ -65,11 +59,11 @@ class NoCaptcha { ...@@ -65,11 +59,11 @@ class NoCaptcha {
{ {
if (empty($response)) return false; if (empty($response)) return false;
$response = $this->sendRequestVerify(array( $response = $this->sendRequestVerify([
'secret' => $this->secret, 'secret' => $this->secret,
'response' => $response, 'response' => $response,
'remoteip' => $clientIp 'remoteip' => $clientIp
)); ]);
return isset($response['success']) && $response['success'] === true; return isset($response['success']) && $response['success'] === true;
} }
...@@ -77,16 +71,25 @@ class NoCaptcha { ...@@ -77,16 +71,25 @@ class NoCaptcha {
/** /**
* // * //
* *
* @return string * @param Request $request
* @return bool
*/ */
public function getJsLink() public function verifyRequest(Request $request)
{ {
if ($this->lang) return $this->verifyResponse(
{ $request->get('g-recaptcha-response'),
return static::CLIENT_API.'?hl='.$this->lang; $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 { ...@@ -95,7 +98,7 @@ class NoCaptcha {
* @param array $query * @param array $query
* @return array * @return array
*/ */
protected function sendRequestVerify(array $query = array()) protected function sendRequestVerify(array $query = [])
{ {
$link = static::VERIFY_URL.'?'.http_build_query($query); $link = static::VERIFY_URL.'?'.http_build_query($query);
...@@ -112,7 +115,7 @@ class NoCaptcha { ...@@ -112,7 +115,7 @@ class NoCaptcha {
*/ */
protected function buildAttributes(array $attributes) protected function buildAttributes(array $attributes)
{ {
$html = array(); $html = [];
foreach ($attributes as $key => $value) foreach ($attributes as $key => $value)
{ {
......
...@@ -20,8 +20,6 @@ class NoCaptchaServiceProvider extends ServiceProvider { ...@@ -20,8 +20,6 @@ class NoCaptchaServiceProvider extends ServiceProvider {
{ {
$app = $this->app; $app = $this->app;
$app['config']->package('anhskohbo/no-captcha', __DIR__.'/config');
$app['validator']->extend('captcha', function($attribute, $value) use ($app) $app['validator']->extend('captcha', function($attribute, $value) use ($app)
{ {
return $app['captcha']->verifyResponse($value, $app['request']->getClientIp()); return $app['captcha']->verifyResponse($value, $app['request']->getClientIp());
...@@ -29,9 +27,9 @@ class NoCaptchaServiceProvider extends ServiceProvider { ...@@ -29,9 +27,9 @@ class NoCaptchaServiceProvider extends ServiceProvider {
if ($app->bound('form')) 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 { ...@@ -43,12 +41,15 @@ class NoCaptchaServiceProvider extends ServiceProvider {
*/ */
public function register() public function register()
{ {
$this->publishes([
__DIR__.'/config/captcha.php' => $this->app->configPath().'/captcha.php'
]);
$this->app->bind('captcha', function($app) $this->app->bind('captcha', function($app)
{ {
return new NoCaptcha( return new NoCaptcha(
$app['config']->get('no-captcha::secret'), $app['config']['captcha.secret'],
$app['config']->get('no-captcha::sitekey'), $app['config']['captcha.sitekey']
$app['config']->get('no-captcha::lang')
); );
}); });
} }
......
<?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