You are here

function image_captcha_captcha in CAPTCHA 6.2

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 image_captcha/image_captcha.module \image_captcha_captcha()
  4. 7 image_captcha/image_captcha.module \image_captcha_captcha()

Implementation of hook_captcha().

File

image_captcha/image_captcha.module, line 177
Implementation of 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 offline 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('site_offline', FALSE) && $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();

        // Use javascript for some added usability (e.g. image reload).
        drupal_add_js(drupal_get_path('module', 'image_captcha') . '/image_captcha.js');
        $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).
        $img_src = check_url(url("image_captcha/{$captcha_sid}/" . time()));
        $result['form']['captcha_image'] = array(
          '#type' => 'markup',
          '#value' => '<img src="' . $img_src . '" class="captcha_image" id="captcha_image_' . $captcha_sid . '" alt="' . t('Image CAPTCHA') . '" title="' . t('Image CAPTCHA') . '" />',
          '#weight' => -2,
        );
        $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;
  }
}