View source
<?php
define('IMAGE_CAPTCHA_ALLOWED_CHARACTERS', 'aAbBCdEeFfGHhijKLMmNPQRrSTtWXYZ23456789%$#!@+?*');
function image_captcha_help($path, $arg) {
switch ($path) {
case 'admin/user/captcha/image_captcha':
$output = '<p>' . t('The image CAPTCHA is a popular challenge where a random textual code is obfuscated in an image. The image is generated on the fly for each request, which is rather CPU intensive for the server. Be careful with the size and computation related settings.') . '</p>';
if (in_array('Image', image_captcha_captcha('list'))) {
$result = image_captcha_captcha('generate', 'Image');
$img = $result['form']['captcha_image']['#value'];
$output .= t('<p>Example image, generated with the current settings:</p>!img', array(
'!img' => $img,
));
}
return $output;
}
}
function image_captcha_menu() {
$items = array();
$items['admin/user/captcha/image_captcha'] = array(
'title' => 'Image CAPTCHA',
'file' => 'image_captcha.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_captcha_settings_form',
),
'access arguments' => array(
'administer CAPTCHA settings',
),
'type' => MENU_LOCAL_TASK,
);
$items['image_captcha'] = array(
'file' => 'image_captcha.user.inc',
'page callback' => 'image_captcha_image',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function _image_captcha_get_font() {
$font = variable_get('image_captcha_font', 'BUILTIN');
$errmsg = FALSE;
$errvar = array();
if ($font != 'BUILTIN' && (!is_file($font) || !is_readable($font))) {
$errmsg = 'Could not find or read the configured font "%font" for the image captcha.';
$errvar = array(
'%font' => $font,
);
$font = FALSE;
}
return array(
$font,
$errmsg,
$errvar,
);
}
function _image_captcha_utf8_split($str) {
$characters = array();
$len = strlen($str);
for ($i = 0; $i < $len;) {
$chr = ord($str[$i]);
if (($chr & 0x80) == 0x0) {
$width = 1;
}
else {
if (($chr & 0xe0) == 0xc0) {
$width = 2;
}
elseif (($chr & 0xf0) == 0xe0) {
$width = 3;
}
elseif (($chr & 0xf8) == 0xf0) {
$width = 4;
}
else {
watchdog('CAPTCHA', 'Encountered an illegal byte while splitting an utf8 string in characters.', array(), WATCHDOG_ERROR);
return $characters;
}
}
$characters[] = substr($str, $i, $width);
$i += $width;
}
return $characters;
}
function image_captcha_captcha($op, $captcha_type = '') {
switch ($op) {
case 'list':
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') {
global $user;
if (variable_get('site_offline', FALSE) && $user->uid == 0) {
return captcha_captcha('generate', 'Math');
}
$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)];
}
$seed = mt_rand();
$_SESSION['image_captcha'][$seed] = $code;
$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;
}
}