Browse Source

Merge branch 'dev' into 'master'

Recaptcha facade

See merge request dakyaco-public/recaptcha-package!1
merge-requests/2/merge
parent
commit
2964cc02aa
6 changed files with 138 additions and 1 deletions
  1. +1
    -0
      .gitignore
  2. +1
    -1
      README.md
  3. +27
    -0
      composer.json
  4. +23
    -0
      src/Facade/Recaptcha.php
  5. +67
    -0
      src/Recaptcha.php
  6. +19
    -0
      src/RecaptchaServiceProvider.php

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
\.idea/

+ 1
- 1
README.md View File

@ -1,3 +1,3 @@
# recaptcha-package # recaptcha-package
the reCaptcha Public Packages repo.
the Public repo of the reCaptcha project.

+ 27
- 0
composer.json View File

@ -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"
]
}
}
}

+ 23
- 0
src/Facade/Recaptcha.php View File

@ -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';
}
}

+ 67
- 0
src/Recaptcha.php View File

@ -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;
}
}

+ 19
- 0
src/RecaptchaServiceProvider.php View File

@ -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() {
//
}
}

Loading…
Cancel
Save