You are here

function captcha_captcha in CAPTCHA 8

Same name and namespace in other branches
  1. 5.3 captcha.module \captcha_captcha()
  2. 6.2 captcha.module \captcha_captcha()
  3. 6 captcha.module \captcha_captcha()
  4. 7 captcha.module \captcha_captcha()

Default implementation of hook_captcha().

1 call to captcha_captcha()
image_captcha_captcha in image_captcha/image_captcha.module
Implements hook_captcha().

File

./captcha.module, line 640
This module enables basic CAPTCHA functionality.

Code

function captcha_captcha($op, $captcha_type = '') {
  switch ($op) {
    case 'list':
      return [
        'Math',
      ];
    case 'generate':
      if ($captcha_type == 'Math') {
        $result = [];
        $answer = mt_rand(1, 20);
        $x = mt_rand(1, $answer);
        $y = $answer - $x;
        $result['solution'] = "{$answer}";

        // Build challenge widget.
        // Note that we also use t() for the math challenge itself. This makes
        // it possible to 'rephrase' the challenge a bit through localization
        // or string overrides.
        $result['form']['captcha_response'] = [
          '#type' => 'textfield',
          '#title' => t('Math question'),
          '#description' => t('Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.'),
          '#field_prefix' => t('@x + @y =', [
            '@x' => $x,
            '@y' => $y,
          ]),
          '#size' => 4,
          '#maxlength' => 2,
          '#required' => TRUE,
          '#attributes' => [
            'autocomplete' => 'off',
          ],
          '#cache' => [
            'max-age' => 0,
          ],
        ];
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
        return $result;
      }
      elseif ($captcha_type == 'Test') {

        // This challenge is not visible through the administrative interface
        // as it is not listed in captcha_captcha('list'),
        // but it is meant for debugging and testing purposes.
        // TODO for Drupal 7 version: This should be done with a mock module,
        // but Drupal 6 does not support this (mock modules can not be hidden).
        $result = [
          'solution' => 'Test 123',
          'form' => [],
        ];
        $result['form']['captcha_response'] = [
          '#type' => 'textfield',
          '#title' => t('Test one two three'),
          '#required' => TRUE,
          '#cache' => [
            'max-age' => 0,
          ],
        ];
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
        return $result;
      }
      break;
  }
}