You are here

function image_captcha_captcha in CAPTCHA 7

Same name and namespace in other branches
  1. 8 image_captcha/image_captcha.module \image_captcha_captcha()
  2. 5.3 image_captcha/image_captcha.module \image_captcha_captcha()
  3. 6.2 image_captcha/image_captcha.module \image_captcha_captcha()
  4. 6 image_captcha/image_captcha.module \image_captcha_captcha()

Implements hook_captcha().

File

image_captcha/image_captcha.module, line 205
Implements image CAPTCHA for use with the CAPTCHA module

Code

function image_captcha_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
  switch ($op) {
    case 'list':

      // Only offer the image CAPTCHA if it is possible to generate an image on this setup.
      if (!(_image_captcha_check_setup() & IMAGE_CAPTCHA_ERROR_NO_GDLIB)) {
        return array(
          'Image',
        );
      }
      else {
        return array();
      }
      break;
    case 'generate':
      if ($captcha_type == 'Image') {

        // In maintenance mode, the image CAPTCHA does not work because the request
        // for the image itself won't succeed (only ?q=user is permitted for
        // unauthenticated users). We fall back to the Math CAPTCHA in that case.
        global $user;
        if (variable_get('maintenance_mode', 0) && $user->uid == 0) {
          return captcha_captcha('generate', 'Math');
        }

        // Generate a CAPTCHA code.
        $allowed_chars = _image_captcha_utf8_split(variable_get('image_captcha_image_allowed_chars', IMAGE_CAPTCHA_ALLOWED_CHARACTERS));
        $code_length = (int) variable_get('image_captcha_code_length', 5);
        $code = '';
        for ($i = 0; $i < $code_length; $i++) {
          $code .= $allowed_chars[array_rand($allowed_chars)];
        }

        // Build the result to return.
        $result = array();
        $result['solution'] = $code;

        // Generate image source URL (add timestamp to avoid problems with
        // client side caching: subsequent images of the same CAPTCHA session
        // have the same URL, but should display a different code).
        $options = array(
          'query' => array(
            'sid' => $captcha_sid,
            'ts' => REQUEST_TIME,
          ),
        );
        $img_src = drupal_strip_dangerous_protocols(url("image_captcha", $options));
        list($width, $height) = _image_captcha_image_size($code);
        $result['form']['captcha_image'] = array(
          '#theme' => 'image',
          '#weight' => -2,
          '#path' => $img_src,
          '#width' => $width,
          '#height' => $height,
          '#title' => t('Image CAPTCHA'),
          '#alt' => t('Image CAPTCHA'),
        );
        $result['form']['captcha_response'] = array(
          '#type' => 'textfield',
          '#title' => t('What code is in the image?'),
          '#description' => t('Enter the characters shown in the image.'),
          '#weight' => 0,
          '#required' => TRUE,
          '#size' => 15,
        );

        // Handle the case insensitive validation option combined with ignoring spaces.
        switch (variable_get('captcha_default_validation', CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE)) {
          case CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE:
            $result['captcha_validate'] = 'captcha_validate_ignore_spaces';
            break;
          case CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE:
            $result['captcha_validate'] = 'captcha_validate_case_insensitive_ignore_spaces';
            break;
        }
        return $result;
      }
      break;
  }
}