You are here

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

Implementation of hook_captcha().

File

./recaptcha.module, line 66

Code

function recaptcha_captcha($op, $captcha_type = '') {
  global $language;
  switch ($op) {
    case 'list':
      return array(
        'reCAPTCHA',
      );
    case 'generate':
      $captcha = array();
      if ($captcha_type == 'reCAPTCHA') {
        $recaptcha_site_key = variable_get('recaptcha_site_key', '');
        $recaptcha_secret_key = variable_get('recaptcha_secret_key', '');
        if (!empty($recaptcha_site_key) && !empty($recaptcha_secret_key)) {

          // Build the reCAPTCHA captcha form if site_key and secret_key are
          // configured. Captcha requires TRUE to be returned in solution.
          $captcha['solution'] = TRUE;
          $captcha['captcha_validate'] = 'recaptcha_captcha_validation';
          $captcha['form']['captcha_response'] = array(
            '#type' => 'hidden',
            '#value' => 'Google no captcha',
          );
          $noscript = '';
          if (variable_get('recaptcha_noscript', 0)) {
            $variables = array(
              'sitekey' => $recaptcha_site_key,
              'language' => $language->language,
            );
            $noscript = theme('recaptcha_widget_noscript', $variables);
          }
          $attributes = array(
            'class' => 'g-recaptcha',
            'data-sitekey' => $recaptcha_site_key,
            'data-theme' => variable_get('recaptcha_theme', 'light'),
            'data-type' => variable_get('recaptcha_type', 'image'),
            'data-size' => variable_get('recaptcha_size', ''),
            'data-tabindex' => variable_get('recaptcha_tabindex', 0),
          );

          // Filter out empty tabindex/size.
          $attributes = array_filter($attributes);
          $captcha['form']['recaptcha_widget'] = array(
            '#type' => 'item',
            '#value' => '<div' . drupal_attributes($attributes) . '></div>' . $noscript,
          );

          // D6 does not support "async" and "external" in drupal_add_js().
          drupal_set_html_head('<script type="text/javascript" src="' . url('https://www.google.com/recaptcha/api.js', array(
            'query' => array(
              'hl' => $language->language,
            ),
            'absolute' => TRUE,
          )) . '" async="async" defer="defer"></script>');
        }
        else {

          // Fallback to Math captcha as reCAPTCHA is not configured.
          $captcha = captcha_captcha('generate', 'Math');
        }
      }
      return $captcha;
  }
}