View source
<?php
define('TEXT_CAPTCHA_GENERATE_NONSENSE_WORDS', 0);
define('TEXT_CAPTCHA_USER_DEFINED_WORDS', 1);
define('TEXT_CAPTCHA_USER_DEFINED_WORD_MINIMUM', 20);
function text_captcha_help($section) {
switch ($section) {
case 'admin/user/captcha/text_captcha':
return '<p>' . t('In this challenge the visitor is asked for the n<sup>th</sup> word of a given phrase.') . '</p>';
}
return $output;
}
function text_captcha_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/captcha/text_captcha',
'title' => t('Text CAPTCHA'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'text_captcha_settings_form',
),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
function text_captcha_settings_form() {
$form = array();
$form['text_captcha_words'] = array(
'#type' => 'radios',
'#title' => t('Kind of words to use in the phrase'),
'#options' => array(
TEXT_CAPTCHA_GENERATE_NONSENSE_WORDS => t('Generate nonsense random words.'),
TEXT_CAPTCHA_USER_DEFINED_WORDS => t('Use user defined words.'),
),
'#default_value' => variable_get('text_captcha_words', TEXT_CAPTCHA_GENERATE_NONSENSE_WORDS),
);
$form['text_captcha_userdefined_words'] = array(
'#type' => 'textarea',
'#title' => t('User defined words'),
'#default_value' => variable_get('text_captcha_userdefined_words', ''),
'#description' => t('Enter a bunch of space separated words (at least @min).', array(
'@min' => TEXT_CAPTCHA_USER_DEFINED_WORD_MINIMUM,
)),
);
$form['text_captcha_word_quantity'] = array(
'#type' => 'textfield',
'#title' => t('Number of words in the phrase'),
'#default_value' => (int) variable_get('text_captcha_word_quantity', 5),
'#size' => 2,
'#maxlength' => 2,
);
return system_settings_form($form);
}
function text_captcha_settings_form_validate($form_id, $form_values) {
if ($form_id == 'text_captcha_settings_form') {
if ($form_values['text_captcha_words'] == TEXT_CAPTCHA_USER_DEFINED_WORDS) {
if (count(explode(' ', $form_values['text_captcha_userdefined_words'])) < TEXT_CAPTCHA_USER_DEFINED_WORD_MINIMUM) {
form_set_error('text_captcha_userdefined_words', t('You need to enter at least @min words if you want to use user defined words.', array(
'@min' => TEXT_CAPTCHA_USER_DEFINED_WORD_MINIMUM,
)));
}
}
$word_quantity = (int) $form_values['text_captcha_word_quantity'];
if ($word_quantity < 4 || $word_quantity > 10) {
form_set_error('text_captcha_word_quantity', t('Number of words in the phrase should be between 4 and 10.'));
}
}
}
function _text_captcha_generate_nonsense_word($characters) {
$vowels = "bcdfghjklmnpqrstvwxyz";
$consonants = "aeiou";
$vowel_max = strlen($vowels) - 1;
$consonant_max = strlen($consonants) - 1;
$word = '';
$o = mt_rand(0, 1);
for ($i = 0; $i < $characters; ++$i) {
if (($i + $o) % 2) {
$word .= $consonants[mt_rand(0, $consonant_max)];
}
else {
$word .= $vowels[mt_rand(0, $vowel_max)];
}
}
return $word;
}
function _text_captcha_generate_words($num) {
$words = array();
if (variable_get('text_captcha_words', TEXT_CAPTCHA_GENERATE_NONSENSE_WORDS) == TEXT_CAPTCHA_USER_DEFINED_WORDS) {
$uwords = explode(' ', variable_get('text_captcha_userdefined_words', ''));
$keys = array_rand($uwords, $num);
foreach ($keys as $key) {
$words[] = $uwords[$key];
}
}
else {
for ($w = 0; $w < $num; ++$w) {
$words[] = _text_captcha_generate_nonsense_word(mt_rand(3, 7));
}
}
return $words;
}
function _text_captcha_ordinal($n) {
$ordinalmap = array(
1 => t('first'),
2 => t('second'),
3 => t('third'),
4 => t('fourth'),
5 => t('fifth'),
6 => t('sixth'),
7 => t('seventh'),
8 => t('eighth'),
9 => t('ninth'),
10 => t('tenth'),
);
if (array_key_exists($n, $ordinalmap)) {
return $ordinalmap[$n];
}
else {
return "{$n}th";
}
}
function text_captcha_captcha($op, $captcha_type = '') {
switch ($op) {
case 'list':
return array(
'Text',
);
case 'generate':
if ($captcha_type == 'Text') {
$words = _text_captcha_generate_words((int) variable_get('text_captcha_word_quantity', 5));
$key = array_rand($words, 1);
$answer = $words[$key];
$result = array();
$result['solution'] = $answer;
$result['form']['captcha_response'] = array(
'#type' => 'textfield',
'#title' => t('What is the @nth word in the phrase "@words"?', array(
'@nth' => _text_captcha_ordinal($key + 1),
'@words' => implode(' ', $words),
)),
'#weight' => 0,
'#required' => TRUE,
'#size' => 15,
);
return $result;
}
}
}