You are here

function recaptcha_captcha in reCAPTCHA 6

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. 5.2 recaptcha.module \recaptcha_captcha()
  5. 6.2 recaptcha.module \recaptcha_captcha()
  6. 7.2 recaptcha.module \recaptcha_captcha()
  7. 7 recaptcha.module \recaptcha_captcha()

Implements hook_captcha().

File

./recaptcha.module, line 65
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);
      $captcha = array();
      if ($captcha_type == 'reCAPTCHA') {

        // Retrieve configuration variables.
        $recaptcha_theme = variable_get('recaptcha_theme', 'red');
        $recaptcha_tabindex = variable_get('recaptcha_tabindex', '');
        $recaptcha_public_key = variable_get('recaptcha_public_key', '');
        $recaptcha_ajax_api = variable_get('recaptcha_ajax_api', FALSE);

        // Test if reCAPTCHA can be used, falling back to Math if it is not
        // configured, the library won't load, or the server is down.
        if (empty($recaptcha_public_key) || !_recaptcha_load_library() || !_recaptcha_server_is_up()) {
          return captcha_captcha('generate', 'Math', $args);
        }
        if ($recaptcha_ajax_api) {

          // By default CAPTCHA turns off page caching on any page where a
          // CAPTCHA challenge appears. If recaptcha is using AJAX API, set
          // caching back to its old state as stored in DB.
          global $conf;
          $conf['cache'] = unserialize(db_result(db_query("SELECT value FROM {variable} WHERE name = 'cache'")));
        }
        $recaptcha_options = array(
          'theme' => $recaptcha_theme,
        );

        // Localization support.
        global $language;
        if (isset($language->language)) {

          // reCAPTCHA uses two-character language codes, so 'pt-br' must be
          // passed as 'pt'; cf. https://developers.google.com/recaptcha/docs/customization#i18n
          $recaptcha_options['lang'] = drupal_substr($language->language, 0, 2);
        }

        // Construct the Javascript, but only display it once.
        static $_recaptcha_jsadded = FALSE;
        if ($_recaptcha_jsadded == FALSE && $recaptcha_ajax_api == FALSE) {
          $_recaptcha_jsadded = TRUE;

          // Add support to display the custom theme.
          if ($recaptcha_theme == 'custom') {
            $recaptcha_options['custom_theme_widget'] = 'recaptcha_custom_theme_widget';
          }

          // Set the default tab index.
          if (!empty($recaptcha_tabindex)) {
            $recaptcha_options['tabindex'] = $recaptcha_tabindex;
          }
          drupal_add_js('var RecaptchaOptions = ' . drupal_to_js($recaptcha_options) . ';', 'inline');
        }

        // Create the form. Captcha requires TRUE to be returned in solution.
        $captcha['solution'] = TRUE;
        $captcha['captcha_validate'] = 'recaptcha_captcha_validation';

        // If 'Disable Client-Side Cookies' is set, then add query string to
        // end of the public key string before passing to recaptchalib.
        if (variable_get('recaptcha_nocookies', FALSE)) {
          $recaptcha_public_key .= '&nocookie=1';
        }
        $captcha['form']['captcha_response'] = array(
          '#type' => 'hidden',
          '#value' => 'reCAPTCHA',
        );

        // Expose the form, either straight HTML, or using the AJAX API.
        // Build the custom theme HTML if necessary.
        $recaptcha_custom_html = $recaptcha_theme == 'custom' ? theme('recaptcha_custom_widget') : '';
        if ($recaptcha_ajax_api == FALSE) {

          // Only generate recaptcha_get_html() if we're not using the AJAX API.
          $html = recaptcha_get_html($recaptcha_public_key, NULL, TRUE);
          $captcha['form']['captcha_form'] = array(
            '#type' => 'item',
            '#value' => (!empty($recaptcha_custom_html) ? '<div id="recaptcha_custom_theme_widget">' . $recaptcha_custom_html . '</div>' : '') . $html,
          );
        }
        else {
          $captcha['form']['captcha_form'] = array(
            '#type' => 'item',
            // Create the destination container, inserting any custom theme HTML
            // necessary ($recaptcha_custom_html will be empty if we are not
            // using custom theme).
            '#value' => '<div id="recaptcha_ajax_api_container">' . $recaptcha_custom_html . '</div>',
          );
          $js = '$(function() { Recaptcha.create(' . drupal_to_js($recaptcha_public_key) . ', "recaptcha_ajax_api_container", {theme: ' . drupal_to_js($recaptcha_theme) . '});});';
          drupal_add_js($js, 'inline', 'header');
          drupal_set_html_head('<script type="text/javascript" src="https://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>');
        }
      }
      return $captcha;
  }
}