Commit 1c6c59ef authored by Nguyen Van Anh's avatar Nguyen Van Anh

PSR2

parent ae2c86af
<?php namespace Anhskohbo\NoCaptcha\Facades; <?php
use Illuminate\Support\Facades\Facade; namespace Anhskohbo\NoCaptcha\Facades;
class NoCaptcha extends Facade { use Illuminate\Support\Facades\Facade;
class NoCaptcha extends Facade
{
/** /**
* Get the registered name of the component. * Get the registered name of the component.
* *
* @return string * @return string
*/ */
protected static function getFacadeAccessor() { return 'captcha'; } protected static function getFacadeAccessor()
{
return 'captcha';
}
} }
<?php namespace Anhskohbo\NoCaptcha; <?php
use Symfony\Component\HttpFoundation\Request; namespace Anhskohbo\NoCaptcha;
class NoCaptcha { use Symfony\Component\HttpFoundation\Request;
class NoCaptcha
{
const CLIENT_API = 'https://www.google.com/recaptcha/api.js'; const CLIENT_API = 'https://www.google.com/recaptcha/api.js';
const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
/** /**
* // * The recaptcha secret key.
* *
* @var string * @var string
*/ */
protected $secret; protected $secret;
/** /**
* // * The recaptcha sitekey key.
* *
* @var string * @var string
*/ */
protected $sitekey; protected $sitekey;
/** /**
* // * NoCaptcha.
* *
* @param string $secret * @param string $secret
* @param string $sitekey * @param string $sitekey
...@@ -34,7 +36,7 @@ class NoCaptcha { ...@@ -34,7 +36,7 @@ class NoCaptcha {
} }
/** /**
* // * Render HTML captcha.
* *
* @return string * @return string
*/ */
...@@ -49,29 +51,33 @@ class NoCaptcha { ...@@ -49,29 +51,33 @@ class NoCaptcha {
} }
/** /**
* // * Verify no-captcha response.
* *
* @param string $response * @param string $response
* @param string $clientIp * @param string $clientIp
*
* @return bool * @return bool
*/ */
public function verifyResponse($response, $clientIp = null) public function verifyResponse($response, $clientIp = null)
{ {
if (empty($response)) return false; if (empty($response)) {
return false;
}
$response = $this->sendRequestVerify([ $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;
} }
/** /**
* // * Verify no-captcha response by Symfony Request.
* *
* @param Request $request * @param Request $request
*
* @return bool * @return bool
*/ */
public function verifyRequest(Request $request) public function verifyRequest(Request $request)
...@@ -83,7 +89,7 @@ class NoCaptcha { ...@@ -83,7 +89,7 @@ class NoCaptcha {
} }
/** /**
* // * Get recaptcha js link.
* *
* @return string * @return string
*/ */
...@@ -93,9 +99,10 @@ class NoCaptcha { ...@@ -93,9 +99,10 @@ class NoCaptcha {
} }
/** /**
* // * Send verify request.
* *
* @param array $query * @param array $query
*
* @return array * @return array
*/ */
protected function sendRequestVerify(array $query = []) protected function sendRequestVerify(array $query = [])
...@@ -108,21 +115,20 @@ class NoCaptcha { ...@@ -108,21 +115,20 @@ class NoCaptcha {
} }
/** /**
* // * Build HTML attributes.
* *
* @param array $attributes * @param array $attributes
*
* @return string * @return string
*/ */
protected function buildAttributes(array $attributes) protected function buildAttributes(array $attributes)
{ {
$html = []; $html = [];
foreach ($attributes as $key => $value) foreach ($attributes as $key => $value) {
{
$html[] = $key.'="'.$value.'"'; $html[] = $key.'="'.$value.'"';
} }
return count($html) ? ' '.implode(' ', $html) : ''; return count($html) ? ' '.implode(' ', $html) : '';
} }
} }
<?php namespace Anhskohbo\NoCaptcha; <?php
use Illuminate\Support\ServiceProvider; namespace Anhskohbo\NoCaptcha;
class NoCaptchaServiceProvider extends ServiceProvider { use Illuminate\Support\ServiceProvider;
class NoCaptchaServiceProvider extends ServiceProvider
{
/** /**
* Indicates if loading of the provider is deferred. * Indicates if loading of the provider is deferred.
* *
...@@ -13,8 +15,6 @@ class NoCaptchaServiceProvider extends ServiceProvider { ...@@ -13,8 +15,6 @@ class NoCaptchaServiceProvider extends ServiceProvider {
/** /**
* Bootstrap the application events. * Bootstrap the application events.
*
* @return void
*/ */
public function boot() public function boot()
{ {
...@@ -22,24 +22,19 @@ class NoCaptchaServiceProvider extends ServiceProvider { ...@@ -22,24 +22,19 @@ class NoCaptchaServiceProvider extends ServiceProvider {
$this->bootConfig(); $this->bootConfig();
$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());
}); });
if ($app->bound('form')) if ($app->bound('form')) {
{ $app['form']->macro('captcha', function ($attributes = []) use ($app) {
$app['form']->macro('captcha', function($attributes = []) use ($app)
{
return $app['captcha']->display($attributes, $app->getLocale()); return $app['captcha']->display($attributes, $app->getLocale());
}); });
} }
} }
/** /**
* // * Booting configure.
*
* @return void
*/ */
protected function bootConfig() protected function bootConfig()
{ {
...@@ -52,13 +47,10 @@ class NoCaptchaServiceProvider extends ServiceProvider { ...@@ -52,13 +47,10 @@ class NoCaptchaServiceProvider extends ServiceProvider {
/** /**
* Register the service provider. * Register the service provider.
*
* @return void
*/ */
public function register() public function register()
{ {
$this->app->bind('captcha', function($app) $this->app->bind('captcha', function ($app) {
{
return new NoCaptcha( return new NoCaptcha(
$app['config']['captcha.secret'], $app['config']['captcha.secret'],
$app['config']['captcha.sitekey'] $app['config']['captcha.sitekey']
...@@ -75,5 +67,4 @@ class NoCaptchaServiceProvider extends ServiceProvider { ...@@ -75,5 +67,4 @@ class NoCaptchaServiceProvider extends ServiceProvider {
{ {
return ['captcha']; return ['captcha'];
} }
} }
<?php <?php
return [ return [
'secret' => env('NOCAPTCHA_SECRET'), 'secret' => env('NOCAPTCHA_SECRET'),
'sitekey' => env('NOCAPTCHA_SITEKEY'), 'sitekey' => env('NOCAPTCHA_SITEKEY'),
]; ];
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