From 0d3893b414d98681e1d91499e9aa520f4d670f07 Mon Sep 17 00:00:00 2001 From: Amir Reza Mehrbakhsh Date: Sun, 18 Aug 2019 15:35:19 +0430 Subject: [PATCH] Recaptcha facade --- .gitignore | 1 + README.md | 2 +- composer.json | 27 +++++++++++++ src/Facade/Recaptcha.php | 23 +++++++++++ src/Recaptcha.php | 67 ++++++++++++++++++++++++++++++++ src/RecaptchaServiceProvider.php | 19 +++++++++ 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Facade/Recaptcha.php create mode 100644 src/Recaptcha.php create mode 100644 src/RecaptchaServiceProvider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eafae12 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +\.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 7ecc738..1b37589 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # recaptcha-package -the reCaptcha Public Packages repo. \ No newline at end of file +the Public repo of the reCaptcha project. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cf4411c --- /dev/null +++ b/composer.json @@ -0,0 +1,27 @@ +{ + "name": "dakyaco/recaptcha", + "description": "Package to verify recaptcha request", + "keywords": ["Laravel", "Recaptcha"], + "license": "MIT", + "authors": [ + { + "name": "Amir Reza Mehrbakhsh", + "email": "amirreza@dakyaco.com" + } + ], + "autoload" : { + "psr-4" : { + "Dakyaco\\Recaptcha\\": "src" + } + }, + "require": { + "Illuminate/Support" : "~5" + }, + "extra": { + "laravel": { + "providers": [ + "Dakyaco\\Recaptcha\\RecaptchaServiceProvider" + ] + } + } +} diff --git a/src/Facade/Recaptcha.php b/src/Facade/Recaptcha.php new file mode 100644 index 0000000..679037a --- /dev/null +++ b/src/Facade/Recaptcha.php @@ -0,0 +1,23 @@ +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; + } +} \ No newline at end of file diff --git a/src/RecaptchaServiceProvider.php b/src/RecaptchaServiceProvider.php new file mode 100644 index 0000000..ceae41d --- /dev/null +++ b/src/RecaptchaServiceProvider.php @@ -0,0 +1,19 @@ +app->singleton('Recaptcha', function () { + $recaptcha = new Recaptcha(); + + return $recaptcha; + }); + } + + public function boot() { + // + } +} \ No newline at end of file