Recaptcha facade See merge request dakyaco-public/recaptcha-package!1merge-requests/1/merge
@ -0,0 +1 @@ | |||
\.idea/ |
@ -1,3 +1,3 @@ | |||
# recaptcha-package | |||
the reCaptcha Public Packages repo. | |||
the Public repo of the reCaptcha project. |
@ -0,0 +1,27 @@ | |||
{ | |||
"name": "dakyaco/recaptcha", | |||
"description": "Package to verify recaptcha request", | |||
"keywords": ["Laravel", "Recaptcha"], | |||
"license": "MIT", | |||
"authors": [ | |||
{ | |||
"name": "Amir Reza Mehrbakhsh", | |||
"email": "[email protected]" | |||
} | |||
], | |||
"autoload" : { | |||
"psr-4" : { | |||
"Dakyaco\\Recaptcha\\": "src" | |||
} | |||
}, | |||
"require": { | |||
"Illuminate/Support" : "~5" | |||
}, | |||
"extra": { | |||
"laravel": { | |||
"providers": [ | |||
"Dakyaco\\Recaptcha\\RecaptchaServiceProvider" | |||
] | |||
} | |||
} | |||
} |
@ -0,0 +1,23 @@ | |||
<?php | |||
namespace Dakyaco\Recaptcha\Facade; | |||
use Illuminate\Support\Facades\Facade; | |||
/** | |||
* @method static verify($request) | |||
*/ | |||
class Recaptcha extends Facade { | |||
/** | |||
* Get the registered name of the component. | |||
* | |||
* @return string | |||
*/ | |||
protected static function getFacadeAccessor() | |||
{ | |||
return 'Recaptcha'; | |||
} | |||
} |
@ -0,0 +1,67 @@ | |||
<?php | |||
namespace Dakyaco\Recaptcha; | |||
use Illuminate\Http\Request; | |||
class Recaptcha { | |||
protected $secretKey; | |||
protected $url; | |||
public function __construct() { | |||
$this->secretKey = config('services.recaptcha.secret'); | |||
$this->url = config('services.recaptcha.url'); | |||
} | |||
public function verify(Request $request) { | |||
$input = $request->get('p-captcha-input'); | |||
$session = $request->get('p-captcha-session'); | |||
if(! is_null($input)) { | |||
$data = [ | |||
'p-captcha-input' => $input, | |||
'p-captcha-session' => $session, | |||
'sitekey' => $this->secretKey | |||
]; | |||
// TODO: document this: | |||
// add keys to config/services.php | |||
$result = $this->call('POST', $this->url, $data); | |||
$result = json_decode($result, true); | |||
return $result; | |||
} | |||
} | |||
public function call($method, $url, $data = false) { | |||
$curl = curl_init(); | |||
switch ($method) | |||
{ | |||
case "POST": | |||
curl_setopt($curl, CURLOPT_POST, 1); | |||
if ($data) | |||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |||
break; | |||
case "PUT": | |||
curl_setopt($curl, CURLOPT_PUT, 1); | |||
break; | |||
default: | |||
if ($data) | |||
$url = sprintf("%s?%s", $url, http_build_query($data)); | |||
} | |||
// Optional Authentication: | |||
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |||
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); | |||
curl_setopt($curl, CURLOPT_URL, $url); | |||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |||
$result = curl_exec($curl); | |||
curl_close($curl); | |||
return $result; | |||
} | |||
} |
@ -0,0 +1,19 @@ | |||
<?php | |||
namespace Dakyaco\Recaptcha; | |||
use Illuminate\Support\ServiceProvider; | |||
class RecaptchaServiceProvider extends ServiceProvider { | |||
public function register() { | |||
$this->app->singleton('Recaptcha', function () { | |||
$recaptcha = new Recaptcha(); | |||
return $recaptcha; | |||
}); | |||
} | |||
public function boot() { | |||
// | |||
} | |||
} |