View source
<?php
define('CSS_CAPTCHA_DEFAULT_ALLOWED_CHARACTERS', 'aAbBcCdDeEfFgGhHijkKLmMnNpPqQrRsStTuUvVwWxXyYzZ123456789');
require_once drupal_get_path('module', 'css_captcha') . '/../text_captcha/text_captcha.inc';
function css_captcha_help($section) {
switch ($section) {
case 'admin/user/captcha/css_captcha':
return '<p>' . t('The CSS CAPTCHA uses CSS tricks to obfuscate a random text code for spam bots. The characters of the code are scrambled in the HTML markup but are displayed in the correct order when rendered with a CSS capable browser.') . '</p>';
}
}
function css_captcha_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/captcha/css_captcha',
'title' => t('CSS CAPTCHA'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'css_captcha_settings_form',
),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
function css_captcha_settings_form() {
$form = array();
$form['css_captcha_allowed_characters'] = array(
'#type' => 'textfield',
'#title' => t('Characters to use in the code'),
'#default_value' => variable_get('css_captcha_allowed_characters', CSS_CAPTCHA_DEFAULT_ALLOWED_CHARACTERS),
);
$form['css_captcha_code_length'] = array(
'#type' => 'select',
'#title' => t('Code length'),
'#options' => array(
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10,
),
'#default_value' => (int) variable_get('css_captcha_code_length', 6),
);
return system_settings_form($form);
}
function css_captcha_settings_form_validate($form_id, $form_values) {
if ($form_id == 'css_captcha_settings_form') {
if (preg_match('/\\s/', $form_values['css_captcha_allowed_characters'])) {
form_set_error('css_captcha_allowed_characters', t('The list of characters to use should not contain spaces.'));
}
}
}
function css_captcha_captcha($op, $captcha_type = '', $response = '') {
switch ($op) {
case 'list':
return array(
"CSS CAPTCHA",
);
case 'generate':
if ($captcha_type == "CSS CAPTCHA") {
$allowed_chars = _text_captcha_utf8_split(variable_get('css_captcha_allowed_characters', CSS_CAPTCHA_DEFAULT_ALLOWED_CHARACTERS));
$code_length = (int) variable_get('css_captcha_code_length', 6);
$solution = '';
$code_struct = array();
for ($i = 0; $i < $code_length; $i++) {
$character = $allowed_chars[array_rand($allowed_chars)];
$code_struct[$i] = array(
'content' => $character,
'size' => 1.25,
);
$solution .= $character;
}
while (count($code_struct) > 1) {
$i = mt_rand(0, count($code_struct) - 2);
$item_left = $code_struct[$i];
$item_right = $code_struct[$i + 1];
$merged_size = $item_left['size'] + $item_right['size'] + 0.25;
if (mt_rand(0, 1)) {
$merged_content = "<div style=\"width:{$item_left['size']}em;float:left;\">{$item_left['content']}</div>" . "<div style=\"width:{$item_right['size']}em;float:left;\">{$item_right['content']}</div>";
}
else {
$merged_content = "<div style=\"width:{$item_right['size']}em;float:right;\">{$item_right['content']}</div>" . "<div style=\"width:{$item_left['size']}em;float:left;\">{$item_left['content']}</div>";
}
$item_merged = array(
'content' => "<div style=\"width:{$merged_size}em;height:1.5em;\">{$merged_content}</div>",
'size' => $merged_size,
);
array_splice($code_struct, $i, 2, array(
$item_merged,
));
$code_struct = array_values($code_struct);
}
$captcha = array();
$captcha['solution'] = $solution;
$captcha['form']['captcha_response'] = array(
'#type' => 'textfield',
'#title' => t('Enter the code above'),
'#size' => 10,
'#prefix' => $code_struct[0]['content'],
'#maxlength' => 10,
'#required' => TRUE,
'#description' => t('Enter the code without spaces and pay attention to upper/lower case.'),
);
$captcha['preprocess'] = FALSE;
return $captcha;
}
break;
}
}