You are here

function recaptcha_captcha in reCAPTCHA 5.2

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

reCAPTCHA implementation of hook_captcha

File

./recaptcha.module, line 136
Uses the reCAPTCHA web service to improve the CAPTCHA system.

Code

function recaptcha_captcha() {
  $args = func_get_args();
  $op = array_shift($args);
  switch ($op) {
    case 'list':
      return array(
        'reCAPTCHA',
      );
    case 'generate':
      $captcha_type = array_shift($args);
      $result = array();
      if ($captcha_type == 'reCAPTCHA') {
        require_once 'recaptcha.inc';
        global $_recaptcha_jsadded;
        @(include_once 'recaptcha/recaptchalib.php') or _recaptcha_library_not_found();

        // Check if reCAPTCHA is available and show Math if not
        $connect = @fsockopen(RECAPTCHA_VERIFY_SERVER, 80);
        if (!$connect) {
          return captcha_captcha('generate', 'Math', $args);
        }
        fclose($connect);

        // close connection
        // Retrieve configuration variables from database
        $recaptcha_secure_connection = variable_get('recaptcha_secure_connection', FALSE);
        $recaptcha_theme = variable_get('recaptcha_theme', 'red');
        $recaptcha_tabindex = variable_get('recaptcha_tabindex', NULL);
        $recaptcha_public_key = variable_get('recaptcha_public_key', '');
        $recaptcha_form_value = NULL;

        // Construct the Javascript
        if (!isset($_recaptcha_jsadded)) {

          // only display the javascript once.
          $_recaptcha_jsadded = TRUE;
          $js = "var RecaptchaOptions = {theme : '{$recaptcha_theme}'";

          // Add language support.
          if (module_exists('translation')) {
            $js .= ", lang:'" . i18n_get_lang() . "'";
          }

          // Add support to display the custom theme
          if ($recaptcha_theme == 'custom') {
            $js .= ", custom_theme_widget : 'recaptcha_custom_theme_widget'";
            $recaptcha_form_value = theme('recaptcha_custom_widget');
          }

          // Set the default tab index
          if (!empty($recaptcha_tabindex)) {
            $js .= ', tabindex : ' . $recaptcha_tabindex;
          }
          drupal_add_js($js . '};', 'inline');
        }

        // Create the form
        $result['preprocess'] = TRUE;

        // tell captcha to preprocess the form
        $result['solution'] = TRUE;

        // require TRUE to be returned
        $result['form']['captcha_challenge'] = array(
          '#type' => 'item',
          '#description' => recaptcha_get_html($recaptcha_public_key, NULL, $recaptcha_secure_connection),
          '#required' => TRUE,
          '#value' => $recaptcha_form_value ? '<div id="recaptcha_custom_theme_widget">' . $recaptcha_form_value . '</div>' : NULL,
        );
      }
      return $result;
    case 'preprocess':
      require_once 'recaptcha.inc';
      @(include_once 'recaptcha/recaptchalib.php') or _recaptcha_library_not_found();
      $resp = recaptcha_check_answer(variable_get('recaptcha_private_key', ''), $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
      if ($resp->is_valid) {
        return TRUE;
      }
      else {
        form_set_error('captcha_response', t('The reCAPTCHA code you entered was incorrect.'));
        return FALSE;
      }
      break;
  }
}