You are here

function image_captcha_captcha in CAPTCHA 5.3

Same name and namespace in other branches
  1. 8 image_captcha/image_captcha.module \image_captcha_captcha()
  2. 6.2 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().

1 call to image_captcha_captcha()
image_captcha_help in image_captcha/image_captcha.module
Implementation of hook_help().

File

image_captcha/image_captcha.module, line 151
Implementation of image CAPTCHA for use with the CAPTCHA module

Code

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

      // only offer image CAPTCHA if possible to generate an image CAPTCHA
      list($font, $errmsg) = _image_captcha_get_font();
      if (function_exists('imagejpeg') && $font) {
        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)];
        }

        // store the answer in $_SESSION for the image generator function (which happens in another http request)
        $seed = mt_rand();
        $_SESSION['image_captcha'][$seed] = $code;

        // build the result to return
        $result = array();

        // Handle the case insesitivity option and change the code to lower case
        // before saving it as solution.
        if (variable_get('image_captcha_case_insensitive', FALSE)) {
          $code = strtolower($code);
          $result['preprocess'] = TRUE;
          $description = t('Enter the characters shown in the image without spaces.');
        }
        else {
          $description = t('Enter the characters shown in the image without spaces, also respect upper and lower case.');
        }
        $result['solution'] = $code;

        // Create the image CAPTCHA form elements
        // The img markup isn't done with theme('image', ...) because that
        // function needs a path to a real file (not applicable)
        // or a full absolute URL (which requires to add protocol and domain)
        $result['form']['captcha_image'] = array(
          '#type' => 'markup',
          '#value' => '<img src="' . check_url(url("image_captcha/{$seed}")) . '" 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' => $description,
          '#weight' => 0,
          '#required' => TRUE,
          '#size' => 15,
        );
        return $result;
      }
      break;
    case 'preprocess':

      // Preprocessing the response for case insesitive validation
      if ($captcha_type == 'Image') {
        return strtolower($response);
      }
      break;
  }
}