You are here

function hcaptcha_captcha in hCaptcha 7

Same name and namespace in other branches
  1. 8 hcaptcha.module \hcaptcha_captcha()

Implements hook_captcha().

Parameters

$op:

string $captcha_type:

Return value

array

File

./hcaptcha.module, line 46

Code

function hcaptcha_captcha($op, $captcha_type = '') {
  global $language;
  switch ($op) {
    case 'list':
      return array(
        'hCaptcha',
      );
    case 'generate':
      $captcha = array();
      if ($captcha_type == 'hCaptcha') {
        $hcaptcha_site_key = variable_get('hcaptcha_site_key', '');
        $hcaptcha_secret_key = variable_get('hcaptcha_secret_key', '');
        if (!empty($hcaptcha_site_key) && !empty($hcaptcha_secret_key)) {
          $attributes = array(
            'class' => 'h-captcha',
            'data-sitekey' => $hcaptcha_site_key,
            'data-theme' => variable_get('hcaptcha_theme', ''),
            'data-size' => variable_get('hcaptcha_size', ''),
            'data-tabindex' => variable_get('hcaptcha_tabindex', 0),
          );
          $hcaptcha = new HCaptcha($hcaptcha_site_key, $hcaptcha_secret_key, $attributes);
          $captcha = $hcaptcha
            ->getWidget('hcaptcha_captcha_validation');

          // Load the library
          $hcaptcha_src = variable_get('hcaptcha_src', 'https://hcaptcha.com/1/api.js');
          $data = array(
            '#tag' => 'script',
            '#value' => '',
            '#attributes' => array(
              'src' => url($hcaptcha_src, array(
                'query' => array(
                  'hl' => $language->language,
                ),
                'absolute' => TRUE,
              )),
              'async' => 'async',
              'defer' => 'defer',
            ),
          );
          drupal_add_html_head($data, 'hcaptcha_api');
        }
        else {

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