summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaslouski Yauheni2011-10-06 23:20:17 (GMT)
committer Maslouski Yauheni2011-10-06 23:20:17 (GMT)
commit9c9c027e1cf6c0ab881915d75bcfe6e8122761fc (patch)
tree3cdf34121d32c95d130735b3baa4891351b063ff
parente207d118f9415edb83196dac65f2dc32fb971631 (diff)
by Spleshka: lost character captcha was ported to 7.x
-rw-r--r--text_captcha/lost_character_captcha/lost_character_captcha.admin.inc28
-rw-r--r--text_captcha/lost_character_captcha/lost_character_captcha.info7
-rw-r--r--text_captcha/lost_character_captcha/lost_character_captcha.install8
-rw-r--r--text_captcha/lost_character_captcha/lost_character_captcha.module45
-rw-r--r--text_captcha/text_captcha.inc14
5 files changed, 57 insertions, 45 deletions
diff --git a/text_captcha/lost_character_captcha/lost_character_captcha.admin.inc b/text_captcha/lost_character_captcha/lost_character_captcha.admin.inc
index 6b783e7..c0fb9be 100644
--- a/text_captcha/lost_character_captcha/lost_character_captcha.admin.inc
+++ b/text_captcha/lost_character_captcha/lost_character_captcha.admin.inc
@@ -1,36 +1,42 @@
<?php
/**
+ * @file
+ * Functionality and helper functions for LOST CHARACTER CAPTCHA administration.
+ */
+
+/**
* Function for the settings form
*/
function lost_character_captcha_settings_form() {
$form = array();
- // form element for the number of characters to lose
+ // Form element for the number of characters to lose
$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),
+ '#options' => drupal_map_assoc(array(1, 2, 3)),
);
- // form element for hinting
+ // Form element for hinting
$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.'),
);
- // form elements for the word pool
+ // Form elements for the word pool
_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
);
- // add a pre_render callback
- $form['#pre_render'] = array('lost_character_captcha_settings_form_pre_render');
- $form['#validate'] = array('lost_character_captcha_settings_form_validate');
- // add buttons and return
+ // Add a pre_render callback
+ $form['#pre_render'][] = 'lost_character_captcha_settings_form_pre_render';
+ // Add validation function
+ $form['#validate'][] = 'lost_character_captcha_settings_form_validate';
+ // Add buttons and return
return system_settings_form($form);
}
@@ -38,7 +44,7 @@ function lost_character_captcha_settings_form() {
* Pre_render function
*/
function lost_character_captcha_settings_form_pre_render($form) {
- // set a warning if the numbers to lose is to big and if hinting is off
+ // Set a warning if the numbers to lose is to big and if hinting is off
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');
}
@@ -51,8 +57,8 @@ function lost_character_captcha_settings_form_pre_render($form) {
function lost_character_captcha_settings_form_validate($form, &$form_state) {
$lost_quantity = (int) $form_state['values']['lost_character_captcha_quantity'];
$hinting = (int)($form_state['values']['lost_character_captcha_enable_hint']);
- $min_length = 3 + 2 * $lost_quantity + (1-$hinting);
- // check the number of words in the pool
+ $min_length = 3 + 2 * $lost_quantity + (1 - $hinting);
+ // Check the number of words in the pool
_text_captcha_word_pool_validate(
'lost_character_captcha_word_pool',
$form_state['values'],
diff --git a/text_captcha/lost_character_captcha/lost_character_captcha.info b/text_captcha/lost_character_captcha/lost_character_captcha.info
index 323e271..a5fb3b4 100644
--- a/text_captcha/lost_character_captcha/lost_character_captcha.info
+++ b/text_captcha/lost_character_captcha/lost_character_captcha.info
@@ -1,5 +1,6 @@
-name = "Lost character CAPTCHA"
-description = "Provides CAPTCHA that asks for the lost character(s) in a given word."
+name = Lost character CAPTCHA
+description = Provides CAPTCHA that asks for the lost character(s) in a given word.
package = "Spam control"
dependencies[] = captcha
-core = 6.x
+core = 7.x
+configure = admin/config/people/captcha/lost_character_captcha
diff --git a/text_captcha/lost_character_captcha/lost_character_captcha.install b/text_captcha/lost_character_captcha/lost_character_captcha.install
index c1e68af..6cec0e9 100644
--- a/text_captcha/lost_character_captcha/lost_character_captcha.install
+++ b/text_captcha/lost_character_captcha/lost_character_captcha.install
@@ -1,7 +1,13 @@
<?php
/**
- * On uninstall: remove module variables and clear variable cache
+ * @file
+ * Install, update and uninstall functions for the LOST CHARACTER CAPTCHA module.
+ */
+
+/**
+ * Implements hook_uninstall().
+ * On uninstall: remove module variables and clear variables cache
*/
function lost_character_captcha_uninstall() {
db_query("DELETE FROM {variable} WHERE name LIKE 'lost_character_captcha_%'");
diff --git a/text_captcha/lost_character_captcha/lost_character_captcha.module b/text_captcha/lost_character_captcha/lost_character_captcha.module
index ce95054..f6652cf 100644
--- a/text_captcha/lost_character_captcha/lost_character_captcha.module
+++ b/text_captcha/lost_character_captcha/lost_character_captcha.module
@@ -1,27 +1,27 @@
<?php
-require_once(drupal_get_path('module', 'lost_character_captcha') .'/../text_captcha.inc');
+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', '_');
/**
- * Implementation of hook_help().
+ * Implements hook_help().
*/
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>';
+ case 'admin/config/people/captcha/lost_character_captcha':
+ return '<p>' . t('The challenge in this CAPTCHA is to determine the lost character(s) of a given word.') . '</p>';
}
}
/**
- * Implementation of hook_menu().
+ * Implements hook_menu().
*/
function lost_character_captcha_menu() {
$items = array();
- $items['admin/user/captcha/lost_character_captcha'] = array(
- 'title' => 'Lost characters',
+ $items['admin/config/people/captcha/lost_character_captcha'] = array(
+ 'title' => 'Lost characters CAPTCHA',
'file' => 'lost_character_captcha.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('lost_character_captcha_settings_form'),
@@ -32,32 +32,32 @@ function lost_character_captcha_menu() {
}
/**
- * Implementation of hook_captcha().
+ * Implements hook_captcha().
*/
-function lost_character_captcha_captcha($op, $captcha_type='') {
+function lost_character_captcha_captcha($op, $captcha_type = '') {
switch ($op) {
case 'list':
return array("Lost characters");
case 'generate':
if ($captcha_type == "Lost characters") {
- // get the word pool
+ // Get the word pool
$words = _text_captcha_word_pool_get_content('lost_character_captcha_word_pool', NULL, LOST_CHARACTER_CAPTCHA_DEFAULT_WORD_POOL, TRUE);
- // pick a random word
+ // Pick a random word
$word = $words[array_rand($words)];
- // split in characters
+ // Split in characters
$characters = _text_captcha_utf8_split($word);
- // lose characters
+ // Lose characters
$lost = array();
$lose_quantity = variable_get('lost_character_captcha_quantity', 1);
- for ($i=0; $i<$lose_quantity; $i++) {
- // pick a random character
+ for ($i = 0; $i < $lose_quantity; $i++) {
+ // Pick a random character
$n = array_rand($characters);
while ($characters[$n] == LOST_CHARACTER_CAPTCHA_HINTER) {
$n = array_rand($characters);
}
- // save it for building the solution
+ // Save it for building the solution
$lost[] = $characters[$n];
- // and lose it in the given word
+ // And lose it in the given word
if (variable_get('lost_character_captcha_enable_hint', TRUE)) {
$characters[$n] = LOST_CHARACTER_CAPTCHA_HINTER;
}
@@ -65,7 +65,7 @@ function lost_character_captcha_captcha($op, $captcha_type='') {
unset($characters[$n]);
}
}
- // build the CAPTCHA
+ // Build the CAPTCHA
sort($lost);
$given_word = implode('', $characters);
$solution = implode('', $lost);
@@ -75,7 +75,7 @@ function lost_character_captcha_captcha($op, $captcha_type='') {
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(
@@ -89,19 +89,18 @@ function lost_character_captcha_captcha($op, $captcha_type='') {
);
return $captcha;
}
- break;
}
}
/**
* Process the response before validation.
*/
-function lost_character_process($element, $edit, &$form_state, $complete_form) {
+function lost_character_process($element, $edit) {
$response = $element['#value'];
- // remove white spaces
+ // Remove white spaces
$parts = _text_captcha_whitespace_explode($response);
$response = implode('', $parts);
- // split in utf8 characters, sort and rejoin
+ // Split in utf8 characters, sort and rejoin
$characters = _text_captcha_utf8_split($response);
sort($characters);
$response = implode('', $characters);
diff --git a/text_captcha/text_captcha.inc b/text_captcha/text_captcha.inc
index 0a41671..476211e 100644
--- a/text_captcha/text_captcha.inc
+++ b/text_captcha/text_captcha.inc
@@ -16,8 +16,8 @@ function _text_captcha_whitespace_explode($string) {
*/
function _text_captcha_utf8_split($str) {
$characters = array();
- $len = strlen($str);
- for ($i=0; $i < $len; ) {
+ $len = drupal_strlen($str);
+ for ($i = 0; $i < $len;) {
$chr = ord($str[$i]);
if (($chr & 0x80) == 0x00) { // one byte character (0zzzzzzz)
$width = 1;
@@ -37,7 +37,7 @@ function _text_captcha_utf8_split($str) {
return $characters;
}
}
- $characters[] = substr($str, $i, $width);
+ $characters[] = drupal_substr($str, $i, $width);
$i += $width;
}
return $characters;
@@ -48,7 +48,7 @@ function _text_captcha_utf8_split($str) {
* Helper function for getting the content of a word pool
* Locale dependent
*/
-function _text_captcha_word_pool_get_content($name_base, $lang_code, $default_value, $explode=FALSE) {
+function _text_captcha_word_pool_get_content($name_base, $lang_code, $default_value, $explode = FALSE) {
if (module_exists('locale')) {
if (!$lang_code) {
global $language;
@@ -69,7 +69,7 @@ function _text_captcha_word_pool_get_content($name_base, $lang_code, $default_va
* Helper function for setting word pool form items
* Locale dependent
*/
-function _text_captcha_word_pool_form_items(&$form, $name_base, $title, $description, $default_value, $rows=3) {
+function _text_captcha_word_pool_form_items(&$form, $name_base, $title, $description, $default_value, $rows = 3) {
if (module_exists('locale')) {
// Locale available
$langs = locale_language_list();
@@ -117,7 +117,7 @@ function _text_captcha_word_pool_validate_word_length($name, $words, $minimum_le
* Helper function for validating the word pool
* Locale dependent
*/
-function _text_captcha_word_pool_validate($name_base, $form_values, $minimum_count, $minimum_length=NULL, $too_short_message='') {
+function _text_captcha_word_pool_validate($name_base, $form_values, $minimum_count, $minimum_length = NULL, $too_short_message = '') {
if (module_exists('locale')) {
$langs = locale_language_list();
foreach ($langs as $lang_code => $lang_name) {
@@ -125,7 +125,7 @@ function _text_captcha_word_pool_validate($name_base, $form_values, $minimum_cou
if (count($words) < $minimum_count) {
form_set_error("{$name_base}_{$lang_code}", t('You should provide at least @num words', array('@num' => $minimum_count)));
}
- // check the minimum word length (if needed)
+ // Check the minimum word length (if needed)
if ($minimum_length) {
_text_captcha_word_pool_validate_word_length("{$name_base}_{$lang_code}", $words, $minimum_length, $too_short_message);
}