You are here

function image_captcha_captcha in CAPTCHA 6

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. 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 104

Code

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

      // only offer image CAPTCHA if possible to generate an image CAPTCHA
      list($font, $errmsg, $errvar) = _image_captcha_get_font();
      if (function_exists('imagejpeg') && $font) {
        return array(
          'Image',
        );
      }
      else {
        return array();
      }
    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();
        $result['solution'] = $code;
        $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' => t('Copy the characters (respecting upper/lower case) from the image.'),
          '#weight' => 0,
          '#required' => TRUE,
          '#size' => 15,
        );
        return $result;
      }
      break;
  }
}