You are here

function recaptcha_captcha in reCAPTCHA 8.2

Same name and namespace in other branches
  1. 8.3 recaptcha.module \recaptcha_captcha()
  2. 8 recaptcha.module \recaptcha_captcha()
  3. 5.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()

Implements hook_captcha().

File

./recaptcha.module, line 63
Verifies if user is a human without necessity to solve a CAPTCHA.

Code

function recaptcha_captcha($op, $captcha_type = '') {
  switch ($op) {
    case 'list':
      return [
        'reCAPTCHA',
      ];
    case 'generate':
      $captcha = [];
      if ($captcha_type == 'reCAPTCHA') {
        $config = \Drupal::config('recaptcha.settings');
        $renderer = \Drupal::service('renderer');
        $recaptcha_site_key = $config
          ->get('site_key');
        $recaptcha_secret_key = $config
          ->get('secret_key');
        $recaptcha_use_globally = $config
          ->get('use_globally');
        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'] = [
            '#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 ($config
            ->get('widget.noscript')) {
            $recaptcha_widget_noscript = [
              '#theme' => 'recaptcha_widget_noscript',
              '#widget' => [
                'sitekey' => $recaptcha_site_key,
                'recaptcha_src_fallback' => $recaptcha_src_fallback,
                'language' => \Drupal::service('language_manager')
                  ->getCurrentLanguage()
                  ->getId(),
              ],
            ];
            $noscript = $renderer
              ->render($recaptcha_widget_noscript);
          }
          $attributes = [
            'class' => 'g-recaptcha',
            'data-sitekey' => $recaptcha_site_key,
            'data-theme' => $config
              ->get('widget.theme'),
            'data-type' => $config
              ->get('widget.type'),
            'data-size' => $config
              ->get('widget.size'),
            'data-tabindex' => $config
              ->get('widget.tabindex'),
          ];

          // Filter out empty tabindex/size.
          $attributes = array_filter($attributes);
          $captcha['form']['recaptcha_widget'] = [
            '#markup' => '<div' . new Attribute($attributes) . '></div>',
            '#suffix' => $noscript,
            '#attached' => [
              'html_head' => [
                [
                  [
                    '#tag' => 'script',
                    '#attributes' => [
                      'src' => Url::fromUri($recaptcha_src, [
                        'query' => [
                          'hl' => \Drupal::service('language_manager')
                            ->getCurrentLanguage()
                            ->getId(),
                        ],
                        'absolute' => TRUE,
                      ])
                        ->toString(),
                      'async' => TRUE,
                      'defer' => TRUE,
                    ],
                  ],
                  'recaptcha_api',
                ],
              ],
            ],
          ];
        }
        else {

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

        // If module configuration changes the form cache need to be refreshed.
        $renderer
          ->addCacheableDependency($captcha['form'], $config);
      }
      return $captcha;
  }
}