View source
<?php
require_once drupal_get_path('module', 'lost_character_captcha') . '/../text_captcha.inc';
define('LOST_CHARACTER_CAPTCHA_DEFAULT_WORD_POOL', 'information language interesting vocabulary communication computer security presentation infrastructure videotape yesterday xylophone workforce validation supervisor standalone multimedia grapefruit friendship aboriginal alphabetical agriculture atmosphere candidature catastrophe audiovisual fingerprint keyboard testimonial supervision supermarket temperature terminology telephonist ultraviolet scholarship spaceflight shoplifting punctuation screwdriver quarterback');
define('LOST_CHARACTER_CAPTCHA_HINTER', '_');
function lost_character_captcha_help($section) {
switch ($section) {
case 'admin/user/captcha/lost_character_captcha':
return '<p>' . t('The challenge in this CAPTCHA is to determine the lost character(s) of a given word.') . '</p>';
}
}
function lost_character_captcha_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/captcha/lost_character_captcha',
'title' => t('Lost characters'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'lost_character_captcha_settings_form',
),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
function lost_character_captcha_settings_form() {
$form = array();
$form['lost_character_captcha_quantity'] = array(
'#type' => 'select',
'#title' => t('Number of characters to lose'),
'#default_value' => variable_get('lost_character_captcha_quantity', 1),
'#description' => t('Select how many characters should be lost in the CAPTCHA.'),
'#options' => array(
1 => 1,
2 => 2,
3 => 3,
),
);
$form['lost_character_captcha_enable_hint'] = array(
'#type' => 'checkbox',
'#title' => t('Put "%hinter" where the characters are lost as a hint', array(
'%hinter' => LOST_CHARACTER_CAPTCHA_HINTER,
)),
'#default_value' => variable_get('lost_character_captcha_enable_hint', TRUE),
'#description' => t('Enable this option to make it easier to determine the lost characters.'),
);
_text_captcha_word_pool_form_items($form, 'lost_character_captcha_word_pool', 'Word pool', 'Enter the words to use, separated with spaces. Make sure every word is unambiguously recognizable when characters are lost. Avoid for example verbs, adverbs, plural forms, too short words, names. Also make sure the words are well known to your intended public.', LOST_CHARACTER_CAPTCHA_DEFAULT_WORD_POOL);
$form['#pre_render'] = (array) $form['#pre_render'] + array(
'lost_character_captcha_settings_form_pre_render',
);
return system_settings_form($form);
}
function lost_character_captcha_settings_form_pre_render() {
if (variable_get('lost_character_captcha_quantity', 1) > 2 && !variable_get('lost_character_captcha_enable_hint', TRUE)) {
drupal_set_message(t('Losing more than two characters without indication where they are lost could be too hard for a human. Check your settings.'), 'warning');
}
}
function lost_character_captcha_settings_form_validate($form_id, $form_values) {
if ($form_id == 'lost_character_captcha_settings_form') {
$lost_quantity = (int) $form_values['lost_character_captcha_quantity'];
$hinting = (int) $form_values['lost_character_captcha_enable_hint'];
$min_length = 3 + 2 * $lost_quantity + (1 - $hinting);
_text_captcha_word_pool_validate('lost_character_captcha_word_pool', $form_values, 3, $min_length, 'The following words are too short (at least @minimum_length characters needed for the current settings of characters to lose and hinting): <div>@words</div>');
}
}
function lost_character_captcha_captcha($op, $captcha_type = '', $response = '') {
switch ($op) {
case 'list':
return array(
"Lost characters",
);
case 'generate':
if ($captcha_type == "Lost characters") {
$words = _text_captcha_word_pool_get_content('lost_character_captcha_word_pool', NULL, LOST_CHARACTER_CAPTCHA_DEFAULT_WORD_POOL, TRUE);
$word = $words[array_rand($words)];
$characters = _text_captcha_utf8_split($word);
$lost = array();
$lose_quantity = variable_get('lost_character_captcha_quantity', 1);
for ($i = 0; $i < $lose_quantity; $i++) {
$n = array_rand($characters);
while ($characters[$n] == LOST_CHARACTER_CAPTCHA_HINTER) {
$n = array_rand($characters);
}
$lost[] = $characters[$n];
if (variable_get('lost_character_captcha_enable_hint', TRUE)) {
$characters[$n] = LOST_CHARACTER_CAPTCHA_HINTER;
}
else {
unset($characters[$n]);
}
}
sort($lost);
$given_word = implode('', $characters);
$solution = implode('', $lost);
if ($lose_quantity == 1) {
$title = t('Enter the missing character from the following word');
}
else {
$title = t('Enter the @num missing characters from the following word', array(
'@num' => $lose_quantity,
));
}
$captcha = array();
$captcha['solution'] = $solution;
$captcha['form']['captcha_response'] = array(
'#type' => 'textfield',
'#title' => $title,
'#field_prefix' => "{$given_word}: ",
'#size' => 3,
'#maxlength' => 3,
'#required' => TRUE,
);
$captcha['preprocess'] = TRUE;
return $captcha;
}
break;
case 'preprocess':
if ($captcha_type == "Lost characters") {
$parts = _text_captcha_whitespace_explode($response);
$response = implode('', $parts);
$characters = _text_captcha_utf8_split($response);
sort($characters);
$response = implode('', $characters);
return $response;
}
break;
}
}