You are here

function recaptcha_captcha_validation in reCAPTCHA 6.2

Same name and namespace in other branches
  1. 8.3 recaptcha.module \recaptcha_captcha_validation()
  2. 8 recaptcha.module \recaptcha_captcha_validation()
  3. 8.2 recaptcha.module \recaptcha_captcha_validation()
  4. 6 recaptcha.module \recaptcha_captcha_validation()
  5. 7.2 recaptcha.module \recaptcha_captcha_validation()
  6. 7 recaptcha.module \recaptcha_captcha_validation()

CAPTCHA Callback; Validates the reCAPTCHA code.

1 string reference to 'recaptcha_captcha_validation'
recaptcha_captcha in ./recaptcha.module
Implementation of hook_captcha().

File

./recaptcha.module, line 129

Code

function recaptcha_captcha_validation($solution, $response, $element, $form_state) {
  $recaptcha_secret_key = variable_get('recaptcha_secret_key', '');
  if (empty($_POST['g-recaptcha-response']) || empty($recaptcha_secret_key)) {
    return FALSE;
  }

  // Use drupal_http_request() to circumvent all issues with the Google library.
  $recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_secret_key, new \ReCaptcha\RequestMethod\Drupal6Post());
  $resp = $recaptcha
    ->verify($_POST['g-recaptcha-response'], ip_address());
  if ($resp
    ->isSuccess()) {

    // Verified!
    return TRUE;
  }
  else {

    // Error code reference, https://developers.google.com/recaptcha/docs/verify
    $error_codes = array(
      'missing-input-secret' => t('The secret parameter is missing.'),
      'invalid-input-secret' => t('The secret parameter is invalid or malformed.'),
      'missing-input-response' => t('The response parameter is missing.'),
      'invalid-input-response' => t('The response parameter is invalid or malformed.'),
      'invalid-json' => t('The json response is invalid or malformed.'),
      'unknown' => t('Unknown error.'),
    );
    foreach ($resp
      ->getErrorCodes() as $code) {
      if (!isset($error_codes[$code])) {
        $code = 'unknown';
      }
      watchdog('reCAPTCHA', '@error', array(
        '@error' => $error_codes[$code],
      ), WATCHDOG_ERROR);
    }
  }
  return FALSE;
}