예제 #1
0
 /**
  * Checks and validates user's response
  *
  * @param bool|string $captcha_challenge Optional challenge string. If empty, value from $_POST will be used
  * @param bool|string $captcha_response Optional response string. If empty, value from $_POST will be used
  * @throws Exception
  * @return Response
  */
 public function check($captcha_challenge = false, $captcha_response = false)
 {
     if (!$this->getPrivateKey()) {
         throw new Exception('You must set private key provided by reCaptcha');
     }
     // Skip processing of empty data
     if (!$captcha_challenge && !$captcha_response) {
         if (isset($_POST['recaptcha_challenge_field']) && isset($_POST['recaptcha_response_field'])) {
             $captcha_challenge = $_POST['recaptcha_challenge_field'];
             $captcha_response = $_POST['recaptcha_response_field'];
         }
     }
     // Instance of response object
     $response = new Response();
     // Discard SPAM submissions
     if (strlen($captcha_challenge) == 0 || strlen($captcha_response) == 0) {
         $response->setValid(false);
         $response->setError('Incorrect-captcha-sol');
         return $response;
     }
     $process = $this->process(array('privatekey' => $this->getPrivateKey(), 'remoteip' => $this->getRemoteIp(), 'challenge' => $captcha_challenge, 'response' => $captcha_response));
     $answers = explode("\n", $process[1]);
     if (trim($answers[0]) == 'true') {
         $response->setValid(true);
     } else {
         $response->setValid(false);
         $response->setError($answers[1]);
     }
     return $response;
 }
예제 #2
0
 /**
  * Checks and validates user's response
  *
  * @param bool|string $captchaResponse Optional response string. If empty, value from $_POST will be used
  * @throws Exception
  * @return Response
  */
 public function check($captchaResponse = false)
 {
     if (!$this->getPrivateKey()) {
         throw new Exception('You must set private key provided by reCaptcha');
     }
     // Skip processing of empty data
     if (!$captchaResponse) {
         if (isset($_POST['g-recaptcha-response'])) {
             $captchaResponse = $_POST['g-recaptcha-response'];
         }
     }
     // Create a new response object
     $response = new Response();
     // Discard SPAM submissions
     if (strlen($captchaResponse) == 0) {
         $response->setValid(false);
         $response->setError('Incorrect-captcha-sol');
         return $response;
     }
     $process = $this->process(array('secret' => $this->getPrivateKey(), 'remoteip' => $this->getRemoteIp(), 'response' => $captchaResponse));
     $answer = @json_decode($process, true);
     if (is_array($answer) && isset($answer['success']) && $answer['success']) {
         $response->setValid(true);
     } else {
         $response->setValid(false);
         $response->setError(serialize($answer));
     }
     return $response;
 }