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;
class NoCaptcha extends Facade {
namespace Anhskohbo\NoCaptcha\Facades;
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'captcha'; }
use Illuminate\Support\Facades\Facade;
class NoCaptcha extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'captcha';
}
}
<?php namespace Anhskohbo\NoCaptcha;
<?php
use Symfony\Component\HttpFoundation\Request;
namespace Anhskohbo\NoCaptcha;
class NoCaptcha {
const CLIENT_API = 'https://www.google.com/recaptcha/api.js';
const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
/**
* //
*
* @var string
*/
protected $secret;
/**
* //
*
* @var string
*/
protected $sitekey;
/**
* //
*
* @param string $secret
* @param string $sitekey
*/
public function __construct($secret, $sitekey)
{
$this->secret = $secret;
$this->sitekey = $sitekey;
}
/**
* //
*
* @return string
*/
public function display($attributes = [], $lang = null)
{
$attributes['data-sitekey'] = $this->sitekey;
$html = '<script src="'.$this->getJsLink($lang).'" async defer></script>'."\n";
$html .= '<div class="g-recaptcha"'.$this->buildAttributes($attributes).'></div>';
return $html;
}
/**
* //
*
* @param string $response
* @param string $clientIp
* @return bool
*/
public function verifyResponse($response, $clientIp = null)
{
if (empty($response)) return false;
$response = $this->sendRequestVerify([
'secret' => $this->secret,
'response' => $response,
'remoteip' => $clientIp
]);
return isset($response['success']) && $response['success'] === true;
}
/**
* //
*
* @param Request $request
* @return bool
*/
public function verifyRequest(Request $request)
{
return $this->verifyResponse(
$request->get('g-recaptcha-response'),
$request->getClientIp()
);
}
/**
* //
*
* @return string
*/
public function getJsLink($lang = null)
{
return $lang ? static::CLIENT_API.'?hl='.$lang : static::CLIENT_API;
}
/**
* //
*
* @param array $query
* @return array
*/
protected function sendRequestVerify(array $query = [])
{
$link = static::VERIFY_URL.'?'.http_build_query($query);
$response = file_get_contents($link);
return json_decode($response, true);
}
/**
* //
*
* @param array $attributes
* @return string
*/
protected function buildAttributes(array $attributes)
{
$html = [];
foreach ($attributes as $key => $value)
{
$html[] = $key.'="'.$value.'"';
}
return count($html) ? ' '.implode(' ', $html) : '';
}
use Symfony\Component\HttpFoundation\Request;
class NoCaptcha
{
const CLIENT_API = 'https://www.google.com/recaptcha/api.js';
const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
/**
* The recaptcha secret key.
*
* @var string
*/
protected $secret;
/**
* The recaptcha sitekey key.
*
* @var string
*/
protected $sitekey;
/**
* NoCaptcha.
*
* @param string $secret
* @param string $sitekey
*/
public function __construct($secret, $sitekey)
{
$this->secret = $secret;
$this->sitekey = $sitekey;
}
/**
* Render HTML captcha.
*
* @return string
*/
public function display($attributes = [], $lang = null)
{
$attributes['data-sitekey'] = $this->sitekey;
$html = '<script src="'.$this->getJsLink($lang).'" async defer></script>'."\n";
$html .= '<div class="g-recaptcha"'.$this->buildAttributes($attributes).'></div>';
return $html;
}
/**
* Verify no-captcha response.
*
* @param string $response
* @param string $clientIp
*
* @return bool
*/
public function verifyResponse($response, $clientIp = null)
{
if (empty($response)) {
return false;
}
$response = $this->sendRequestVerify([
'secret' => $this->secret,
'response' => $response,
'remoteip' => $clientIp,
]);
return isset($response['success']) && $response['success'] === true;
}
/**
* Verify no-captcha response by Symfony Request.
*
* @param Request $request
*
* @return bool
*/
public function verifyRequest(Request $request)
{
return $this->verifyResponse(
$request->get('g-recaptcha-response'),
$request->getClientIp()
);
}
/**
* Get recaptcha js link.
*
* @return string
*/
public function getJsLink($lang = null)
{
return $lang ? static::CLIENT_API.'?hl='.$lang : static::CLIENT_API;
}
/**
* Send verify request.
*
* @param array $query
*
* @return array
*/
protected function sendRequestVerify(array $query = [])
{
$link = static::VERIFY_URL.'?'.http_build_query($query);
$response = file_get_contents($link);
return json_decode($response, true);
}
/**
* Build HTML attributes.
*
* @param array $attributes
*
* @return string
*/
protected function buildAttributes(array $attributes)
{
$html = [];
foreach ($attributes as $key => $value) {
$html[] = $key.'="'.$value.'"';
}
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;
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
class NoCaptchaServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$app = $this->app;
/**
* Bootstrap the application events.
*/
public function boot()
{
$app = $this->app;
$this->bootConfig();
$this->bootConfig();
$app['validator']->extend('captcha', function($attribute, $value) use ($app)
{
return $app['captcha']->verifyResponse($value, $app['request']->getClientIp());
});
$app['validator']->extend('captcha', function ($attribute, $value) use ($app) {
return $app['captcha']->verifyResponse($value, $app['request']->getClientIp());
});
if ($app->bound('form'))
{
$app['form']->macro('captcha', function($attributes = []) use ($app)
{
return $app['captcha']->display($attributes, $app->getLocale());
});
}
}
/**
* //
*
* @return void
*/
protected function bootConfig()
{
$path = __DIR__.'/config/captcha.php';
if ($app->bound('form')) {
$app['form']->macro('captcha', function ($attributes = []) use ($app) {
return $app['captcha']->display($attributes, $app->getLocale());
});
}
}
$this->mergeConfigFrom($path, 'captcha');
/**
* Booting configure.
*/
protected function bootConfig()
{
$path = __DIR__.'/config/captcha.php';
$this->publishes([$path => config_path('captcha.php')]);
}
$this->mergeConfigFrom($path, 'captcha');
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('captcha', function($app)
{
return new NoCaptcha(
$app['config']['captcha.secret'],
$app['config']['captcha.sitekey']
);
});
}
$this->publishes([$path => config_path('captcha.php')]);
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['captcha'];
}
/**
* Register the service provider.
*/
public function register()
{
$this->app->bind('captcha', function ($app) {
return new NoCaptcha(
$app['config']['captcha.secret'],
$app['config']['captcha.sitekey']
);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['captcha'];
}
}
<?php
return [
'secret' => env('NOCAPTCHA_SECRET'),
'sitekey' => env('NOCAPTCHA_SITEKEY'),
'secret' => env('NOCAPTCHA_SECRET'),
'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