-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Blanquera
committed
Jan 15, 2019
1 parent
34cba32
commit 2612801
Showing
3 changed files
with
79 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,9 @@ | ||
<?php //--> | ||
use Cradle\Curl\CurlHandler; | ||
|
||
use Cradle\Http\Request; | ||
use Cradle\Http\Response; | ||
|
||
/** | ||
* Loads captcha token in stage | ||
* This file is part of a package designed for the CradlePHP Project. | ||
* | ||
* @param *Request $request | ||
* @param *Response $response | ||
* Copyright and license information can be found at LICENSE.txt | ||
* distributed with this package. | ||
*/ | ||
$cradle->on('captcha-load', function (Request $request, Response $response) { | ||
$config = $this->package('global')->service('captcha-main'); | ||
|
||
//if no config | ||
if (!$config | ||
|| !isset($config['token'], $config['secret']) | ||
|| $config['token'] === '<GOOGLE CAPTCHA TOKEN>' | ||
|| $config['secret'] === '<GOOGLE CAPTCHA SECRET>' | ||
) | ||
{ | ||
return; | ||
} | ||
|
||
//render the key | ||
$key = $config['token']; | ||
$response->setResults('captcha', $key); | ||
}); | ||
|
||
/** | ||
* Validates Captcha | ||
* | ||
* @param *Request $request | ||
* @param *Response $response | ||
*/ | ||
$cradle->on('captcha-validate', function (Request $request, Response $response) { | ||
$actual = $request->getStage('g-recaptcha-response'); | ||
$config = $this->package('global')->service('captcha-main'); | ||
|
||
//if no config | ||
if (!$config | ||
|| !isset($config['token'], $config['secret']) | ||
|| $config['token'] === '<GOOGLE CAPTCHA TOKEN>' | ||
|| $config['secret'] === '<GOOGLE CAPTCHA SECRET>' | ||
) | ||
{ | ||
//let it pass | ||
return; | ||
} | ||
|
||
$result = CurlHandler::i() | ||
->setUrl('https://www.google.com/recaptcha/api/siteverify') | ||
->verifyHost(false) | ||
->verifyPeer(false) | ||
->setPostFields(http_build_query(array( | ||
'secret' => $config['secret'], | ||
'response' => $actual | ||
))) | ||
->getJsonResponse(); | ||
|
||
if (!isset($result['success']) || !$result['success']) { | ||
//prepare to error | ||
$message = $this->package('global')->translate('Captcha Failed'); | ||
$response->setError(true, $message); | ||
} | ||
|
||
//it passed | ||
}); | ||
require_once __DIR__ . '/src/events.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php //--> | ||
/** | ||
* This file is part of a package designed for the CradlePHP Project. | ||
* | ||
* Copyright and license information can be found at LICENSE.txt | ||
* distributed with this package. | ||
*/ | ||
|
||
use Cradle\Curl\CurlHandler; | ||
use Cradle\Http\Request\RequestInterface; | ||
use Cradle\Http\Response\ResponseInterface; | ||
/** | ||
* Loads captcha token in stage | ||
* | ||
* @param *Request $request | ||
* @param *Response $response | ||
*/ | ||
$cradle->on('captcha-load', function (RequestInterface $request, ResponseInterface $response) { | ||
$config = $this->package('global')->service('captcha-main'); | ||
|
||
//if no config | ||
if (!$config | ||
|| !isset($config['token'], $config['secret']) | ||
|| $config['token'] === '<GOOGLE CAPTCHA TOKEN>' | ||
|| $config['secret'] === '<GOOGLE CAPTCHA SECRET>' | ||
) | ||
{ | ||
return; | ||
} | ||
|
||
//render the key | ||
$key = $config['token']; | ||
$response->setResults('captcha', $key); | ||
}); | ||
|
||
/** | ||
* Validates Captcha | ||
* | ||
* @param *Request $request | ||
* @param *Response $response | ||
*/ | ||
$cradle->on('captcha-validate', function (RequestInterface $request, ResponseInterface $response) { | ||
$actual = $request->getStage('g-recaptcha-response'); | ||
$config = $this->package('global')->service('captcha-main'); | ||
|
||
//if no config | ||
if (!$config | ||
|| !isset($config['token'], $config['secret']) | ||
|| $config['token'] === '<GOOGLE CAPTCHA TOKEN>' | ||
|| $config['secret'] === '<GOOGLE CAPTCHA SECRET>' | ||
) | ||
{ | ||
//let it pass | ||
return; | ||
} | ||
|
||
$result = CurlHandler::i() | ||
->setUrl('https://www.google.com/recaptcha/api/siteverify') | ||
->verifyHost(false) | ||
->verifyPeer(false) | ||
->setPostFields(http_build_query(array( | ||
'secret' => $config['secret'], | ||
'response' => $actual | ||
))) | ||
->getJsonResponse(); | ||
|
||
if (!isset($result['success']) || !$result['success']) { | ||
//prepare to error | ||
$message = $this->package('global')->translate('Captcha Failed'); | ||
$response->setError(true, $message); | ||
} | ||
|
||
//it passed | ||
}); |