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 6b783e7909774a79898f04b048254923ce9fdd72..c0fb9be48720da5057633f4d12b9cbe59620d407 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 @@ '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 323e2711b71e0332ec342d5bbdc0f98e738829d9..a5fb3b4f462fd369cf9f03c16656784045d65e97 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 c1e68af3b1cbf51dac10bc57df1e8c02059aab50..6cec0e9496101abef3bdabfd7065777c46f3fc70 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 @@ '. t('The challenge in this CAPTCHA is to determine the lost character(s) of a given word.') .'

'; + case 'admin/config/people/captcha/lost_character_captcha': + return '

' . t('The challenge in this CAPTCHA is to determine the lost character(s) of a given word.') . '

'; } } /** - * 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 0a41671ad4ffad04dcae20752e2819d71a21932d..476211e8cbd9abb24939af65d9eb1ffad5b56140 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); }