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($path, $arg) {
switch ($path) {
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() {
$items = array();
$items['admin/user/captcha/lost_character_captcha'] = array(
'title' => 'Lost characters',
'file' => 'lost_character_captcha.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'lost_character_captcha_settings_form',
),
'access arguments' => array(
'administer CAPTCHA settings',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function lost_character_captcha_captcha($op, $captcha_type = '') {
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,
'#process' => array(
'lost_character_process',
),
);
return $captcha;
}
break;
}
}
function lost_character_process($element, $edit, &$form_state, $complete_form) {
$response = $element['#value'];
$parts = _text_captcha_whitespace_explode($response);
$response = implode('', $parts);
$characters = _text_captcha_utf8_split($response);
sort($characters);
$response = implode('', $characters);
$element['#value'] = $response;
return $element;
}