You are here

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

Implements hook_captcha().

File

./recaptcha.module, line 71

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', '');
        $recaptcha_use_globally = variable_get('recaptcha_use_globally', FALSE);
        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',
          );

          // As the validate callback does not depend on sid or solution, this
          // captcha type can be displayed on cached pages.
          $captcha['cacheable'] = TRUE;

          // Check if reCAPTCHA use globally is enabled.
          $recaptcha_src = 'https://www.google.com/recaptcha/api.js';
          $recaptcha_src_fallback = 'https://www.google.com/recaptcha/api/fallback';
          if ($recaptcha_use_globally) {
            $recaptcha_src = 'https://www.recaptcha.net/recaptcha/api.js';
            $recaptcha_src_fallback = 'https://www.recaptcha.net/recaptcha/api/fallback';
          }
          $noscript = '';
          if (variable_get('recaptcha_noscript', 0)) {
            $variables = array(
              'sitekey' => $recaptcha_site_key,
              'language' => $language->language,
              'recaptcha_src_fallback' => $recaptcha_src_fallback,
            );
            $noscript = theme('recaptcha_widget_noscript', array(
              'widget' => $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(
            '#markup' => '<div' . drupal_attributes($attributes) . '></div>',
            '#suffix' => $noscript,
          );

          // @todo: #1664602: D7 does not yet support "async" in drupal_add_js().
          // drupal_add_js(url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), array('defer' => TRUE, 'async' => TRUE, 'type' => 'external'));
          $data = array(
            '#tag' => 'script',
            '#value' => '',
            '#attributes' => array(
              'src' => url($recaptcha_src, array(
                'query' => array(
                  'hl' => $language->language,
                ),
                'absolute' => TRUE,
              )),
              'async' => 'async',
              'defer' => 'defer',
            ),
          );
          drupal_add_html_head($data, 'recaptcha_api');
        }
        else {

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