Commit f068a50c authored by soxofaan's avatar soxofaan

#198598: by Smartys, port of text CAPTCHA module to Drupal 6

parent 48008b4a
<?php
// $Id$
/**
* Administration form
*/
function text_captcha_settings_form() {
$form = array();
// radio buttons for selecting the kind of words to use
$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),
);
// textarea for user defined 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)),
);
// textfield for the number of words in the CAPTCHA phrase
$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,
);
$form['#validate'][] = 'text_captcha_settings_form_validate';
return system_settings_form($form);
}
/**
* Validate function of the administration form
*/
function text_captcha_settings_form_validate($form, &$form_state) {
if ($form_state['values']['text_captcha_words'] == TEXT_CAPTCHA_USER_DEFINED_WORDS) {
// check if there are at minimum TEXT_CAPTCHA_USER_DEFINED_WORD_MINIMUM user defined words
if (count(explode(' ', $form_state['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)
)
);
}
}
// chech text_captcha_word_quantity
$word_quantity = (int) $form_state['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.'));
}
}
......@@ -2,4 +2,6 @@
name = "Text CAPTCHA"
description = "Provides a simple text based CAPTCHA."
package = "Spam control"
dependencies = captcha
dependencies[] = captcha
core = 6.x
<?php
// $Id$
/**
* Implementation of a simple text based CAPTCHA, for use with the CAPTCHA module
*/
......@@ -13,146 +12,28 @@ define('TEXT_CAPTCHA_USER_DEFINED_WORD_MINIMUM', 20);
/**
* Implementation of hook_help().
*/
function text_captcha_help($section) {
switch ($section) {
function text_captcha_help($path, $arg) {
switch ($path) {
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;
}
/**
* Implementation of hook_menu().
*/
function text_captcha_menu($may_cache) {
$items = array();
if ($may_cache) {
// add an administration tab for text_captcha
$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,
function text_captcha_menu() {
$items['admin/user/captcha/text_captcha'] = array(
// add an administration tab for text_captcha
'title' => 'Text CAPTCHA',
'file' => 'text_captcha.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('text_captcha_settings_form'),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
/**
* Administration form
*/
function text_captcha_settings_form() {
$form = array();
// radio buttons for selecting the kind of words to use
$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),
);
// textarea for user defined 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)),
);
// textfield for the number of words in the CAPTCHA phrase
$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);
}
/**
* Validate function of the administration 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) {
// check if there are at minimum TEXT_CAPTCHA_USER_DEFINED_WORD_MINIMUM 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)
)
);
}
}
// chech text_captcha_word_quantity
$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 for generating a random nonsense word of a given number of characters
*/
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); // randomly start with vowel or consonants
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 for generating an array of words
*/
function _text_captcha_generate_words($num) {
$words = array();
if (variable_get('text_captcha_words', TEXT_CAPTCHA_GENERATE_NONSENSE_WORDS) == TEXT_CAPTCHA_USER_DEFINED_WORDS) {
// use user defined words
$uwords = explode(' ', variable_get('text_captcha_userdefined_words', ''));
$keys = array_rand($uwords, $num);
foreach ($keys as $key) {
$words[] = $uwords[$key];
}
}
else {
// generate nonsense words
for ($w=0; $w<$num; ++$w) {
$words[] = _text_captcha_generate_nonsense_word(mt_rand(3, 7));
}
}
return $words;
}
/**
* function that returns a textual represention of an ordinal
*/
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";
}
}
/**
* Implementation of hook_captcha
*/
......@@ -161,6 +42,8 @@ function text_captcha_captcha($op, $captcha_type='') {
case 'list':
return array('Text');
case 'generate':
// Add in the necessary functions
require_once drupal_get_path('module', 'text_captcha') .'/text_captcha.user.inc';
if ($captcha_type == 'Text') {
// generate words
$words = _text_captcha_generate_words((int) variable_get('text_captcha_word_quantity', 5));
......
<?php
// $Id$
/**
* function for generating a random nonsense word of a given number of characters
*/
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); // randomly start with vowel or consonants
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 for generating an array of words
*/
function _text_captcha_generate_words($num) {
$words = array();
if (variable_get('text_captcha_words', TEXT_CAPTCHA_GENERATE_NONSENSE_WORDS) == TEXT_CAPTCHA_USER_DEFINED_WORDS) {
// use user defined words
$uwords = explode(' ', variable_get('text_captcha_userdefined_words', ''));
$keys = array_rand($uwords, $num);
foreach ($keys as $key) {
$words[] = $uwords[$key];
}
}
else {
// generate nonsense words
for ($w=0; $w<$num; ++$w) {
$words[] = _text_captcha_generate_nonsense_word(mt_rand(3, 7));
}
}
return $words;
}
/**
* function that returns a textual represention of an ordinal
*/
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";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment