You are here

function captcha_captcha in CAPTCHA 7

Same name and namespace in other branches
  1. 8 captcha.module \captcha_captcha()
  2. 5.3 captcha.module \captcha_captcha()
  3. 6.2 captcha.module \captcha_captcha()
  4. 6 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 845
This module enables basic CAPTCHA functionality: administrators can add a CAPTCHA to desired forms that users without the 'skip CAPTCHA' permission (typically anonymous visitors) have to solve.

Code

function captcha_captcha($op, $captcha_type = '') {
  switch ($op) {
    case 'list':
      return array(
        'Math',
      );
    case 'generate':
      if ($captcha_type == 'Math') {
        $result = array();
        $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'] = array(
          '#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 = ', array(
            '@x' => $x,
            '@y' => $y,
          )),
          '#size' => 4,
          '#maxlength' => 2,
          '#required' => TRUE,
        );
        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 = array(
          'solution' => 'Test 123',
          'form' => array(),
        );
        $result['form']['captcha_response'] = array(
          '#type' => 'textfield',
          '#title' => t('Test one two three'),
          '#required' => TRUE,
        );
        return $result;
      }
      break;
  }
}