123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace Ppb\Validate;
- use Cube\Validate\AbstractValidate,
- Cube\Controller\Front;
- class ReCaptcha extends AbstractValidate
- {
-
- const VERIFY_SERVER = 'https://www.google.com/recaptcha/api/siteverify';
-
- protected $_message = "The reCAPTCHA code is incorrect.";
-
- public function isValid()
- {
- $settings = Front::getInstance()->getBootstrap()->getResource('settings');
- $request = Front::getInstance()->getRequest();
- $response = $request->getParam('g-recaptcha-response');
- if ($response == null || strlen($response) == 0) {
- return false;
- }
- $json = $this->_post(array(
- 'secret' => $settings['recaptcha_private_key'],
- 'remoteip' => $_SERVER['REMOTE_ADDR'],
- 'response' => $response
- ));
- $result = json_decode($json, true);
- if (isset($result['success']) && $result['success'] == true) {
- return true;
- }
- return false;
- }
-
- protected function _post(array $data)
- {
- $peerKey = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
- $options = array(
- 'http' => array(
- 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
- 'method' => 'POST',
- 'content' => http_build_query($data, '', '&'),
-
- 'verify_peer' => true,
-
- $peerKey => 'www.google.com',
- ),
- );
- $context = stream_context_create($options);
- return file_get_contents(self::VERIFY_SERVER, false, $context);
- }
- }
|