Permalink
Browse files

first commit

  • Loading branch information...
albertcht committed May 4, 2017
0 parents commit 254966fd3e1f6c2a432f35af46f26ade286eab67
@@ -0,0 +1,3 @@
/vendor
composer.lock
.DS_Store
19 LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2017 Albert Chen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,22 @@
{
"name": "albertcht/invisible-recaptcha",
"description": "Invisible reCAPTCHA For Laravel.",
"keywords": ["recaptcha", "no-captcha", "captcha", "invisible", "invisible-recaptcha","laravel", "laravel5"],
"license": "MIT",
"authors": [
{
"name": "Albert Chen",
"email": "albert.cht@gmail.com"
}
],
"require": {
"php": "^5.6.4 || ^7.0",
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
"guzzlehttp/guzzle": "^6.2"
},
"autoload": {
"psr-4": {
"AlbertCht\\InvisibleReCaptcha\\": "src/"
}
}
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
@@ -0,0 +1,18 @@
<?php
namespace AlbertCht\InvisibleReCaptcha\Facades;
use Illuminate\Support\Facades\Facade;
class InvisibleReCaptcha extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'captcha';
}
}
@@ -0,0 +1,146 @@
<?php
namespace AlbertCht\InvisibleReCaptcha;
use Symfony\Component\HttpFoundation\Request;
use GuzzleHttp\Client;
class InvisibleReCaptcha
{
const API_URI = 'https://www.google.com/recaptcha/api.js';
const VERIFY_URI = 'https://www.google.com/recaptcha/api/siteverify';
/**
* The reCaptcha sitekey key.
*
* @var string
*/
protected $siteKey;
/**
* The reCaptcha secret key.
*
* @var string
*/
protected $secretKey;
/**
* The config to determine if hide the badge.
*
* @var boolean
*/
protected $hideBadge;
/**
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* InvisibleReCaptcha.
*
* @param string $secretKey
* @param string $siteKey
*/
public function __construct($siteKey, $secretKey, $hideBadge = false)
{
$this->siteKey = $siteKey;
$this->secretKey = $secretKey;
$this->hideBadge = $hideBadge;
$this->client = new Client(['timeout' => 5]);
}
/**
* Get reCaptcha js by optional language param.
*
* @param string $lang
*
* @return string
*/
public function getJs($lang = null)
{
return $lang ? self::API_URI . '?hl=' . $lang : self::API_URI;
}
/**
* Render HTML reCaptcha by formId and submitId.
*
* @param string $formId
* @param string $submitId
* @param string $lang
*
* @return string
*/
public function render($lang = null)
{
$html = '<div id="_g-recaptcha"></div>' . "\n";
if ($this->hideBadge) {
$html .= '<style>.grecaptcha-badge{display:none;!important}</style>' . "\n";
}
$html .= '<div class="g-recaptcha" data-sitekey="' . $this->siteKey .'" ';
$html .= 'data-bind="send-btn" data-callback="_submitForm"></div>';
$html .= '<script src="' . $this->getJs($lang) . '" async defer></script>' . "\n";
$html .= '<script>var _submitForm,_captchaForm, _captchaSubmit;</script>';
$html .= '<script>window.onload = function(){';
$html .= ' _captchaForm=document.querySelector("#_g-recaptcha").closest("form");';
$html .= " _captchaSubmit=_captchaForm.querySelector('[type=submit]');";
$html .= '_submitForm=function(){_captchaForm.submit();}}</script>' . "\n";
return $html;
}
/**
* Verify invisible reCaptcha response.
*
* @param string $response
* @param string $clientIp
*
* @return bool
*/
public function verifyResponse($response, $clientIp)
{
if (empty($response)) {
return false;
}
$response = $this->sendVerifyRequest([
'secret' => $this->secretKey,
'remoteip' => $clientIp,
'response' => $response
]);
return isset($response['success']) && $response['success'] === true;
}
/**
* Verify invisible reCaptcha response by Symfony Request.
*
* @param Request $request
*
* @return bool
*/
public function verifyRequest(Request $request)
{
return $this->verifyResponse(
$request->get('g-recaptcha-response'),
$request->getClientIp()
);
}
/**
* Send verify request.
*
* @param array $query
*
* @return array
*/
protected function sendVerifyRequest(array $query = [])
{
$response = $this->client->post(self::VERIFY_URI, [
'form_params' => $query,
]);
return json_decode($response->getBody(), true);
}
}
@@ -0,0 +1,66 @@
<?php
namespace AlbertCht\InvisibleReCaptcha;
use Illuminate\Support\ServiceProvider;
class InvisibleReCaptchaServiceProvider extends ServiceProvider
{
/**
* Boot the services for the application.
*
* @return void
*/
public function boot()
{
$app = $this->app;
$this->bootConfig();
$this->app['validator']->extend('captcha', function ($attribute, $value) use ($app) {
return $app['captcha']->verifyResponse($value, $app['request']->getClientIp());
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('captcha', function ($app) {
return new InvisibleReCaptcha(
$app['config']['captcha.siteKey'],
$app['config']['captcha.secretKey'],
$app['config']['captcha.hideBadge']
);
});
}
/**
* Boot configure.
*
* @return void
*/
protected function bootConfig()
{
$path = __DIR__.'/config/captcha.php';
$this->mergeConfigFrom($path, 'captcha');
if (function_exists('config_path')) {
$this->publishes([$path => config_path('captcha.php')]);
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['captcha'];
}
}
No changes.
@@ -0,0 +1,7 @@
<?php
return [
'siteKey' => env('INVISIBLE_RECAPTCHA_SITEKEY'),
'secretKey' => env('INVISIBLE_RECAPTCHA_SECRETKEY'),
'hideBadge' => env('INVISIBLE_RECAPTCHA_HIDE', false)
];

0 comments on commit 254966f

Please sign in to comment.