Index: README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/captcha/README.txt,v retrieving revision 1.12 diff -u -r1.12 README.txt --- README.txt 4 Mar 2006 01:57:40 -0000 1.12 +++ README.txt 28 Dec 2006 06:46:30 -0000 @@ -1,3 +1,5 @@ +$Id$ + ##Captcha Readme## To Install: Index: captcha.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/captcha/captcha.module,v retrieving revision 1.29 diff -u -r1.29 captcha.module --- captcha.module 23 Dec 2006 14:22:09 -0000 1.29 +++ captcha.module 5 Jan 2007 03:11:24 -0000 @@ -1,50 +1,74 @@ Adds a Captcha to the registration form.

"; - $output .= "

More help needed here.

"; - break; - case 'admin/modules#description': - case 'admin/modules/captcha': - case 'admin/captcha': - $output = t("Adds a Captcha to the registration form."); + $output .= '

'. t('Adds a Captcha to various forms to help prevent spam submissions.') .'

'; + // TODO: More help needed here. break; } return $output; } +/** + * Implementation of hook_menu(). + */ +function captcha_menu($may_cache) { + $items = array(); + if ($may_cache) { + $items[] = array( + 'path' => 'admin/settings/captcha', + 'title' => t('Captcha'), + 'description' => t('Administer how and where Captchas are used.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('captcha_admin_settings'), + 'access' => user_access('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + ); + } + return $items; +} -function captcha_settings() { +/** + * Helper function generates admin settings page. + */ +function captcha_admin_settings() { - //this is where you can add more captcha points + // This is where you can add more captcha points. $captcha_points = array( - 'comment_form' => t('Comment Form'), - 'user_login' => t('User Login Form'), - 'user_login_block' => t('User Login Form Block'), - 'user_edit' => t('User Edit Form'), - 'user_register' => t('User Registration Form'), - 'user_pass' => t('User Forgot Password Form'), - 'contact_mail_user' => t('User Contact Form'), - 'contact_mail_page' => t('Sitewide Contact Form'), - 'node_form' => t('Create a node'), - ); + 'comment_form' => t('Comment form'), + 'user_login' => t('User login form'), + 'user_login_block' => t('User login form block'), + 'user_edit' => t('User edit form'), + 'user_register' => t('User registration form'), + 'user_pass' => t('User forgot password form'), + 'contact_mail_user' => t('User contact form'), + 'contact_mail_page' => t('Sitewide contact form'), + 'node_form' => t('Create a node'), + ); $roles = user_roles(); foreach($roles as $role) { - $varsuffix = strtr($role,' ','_') .'_captcha'; - $form[$varsuffix] = array('#type' => 'fieldset', '#title' => t('Captcha Points for the role %role', array('%role' => $role)), '#collapsible' => TRUE, '#collapsed' => TRUE); - foreach($captcha_points as $captcha_point=>$captcha_point_description) { + $varsuffix = strtr($role, ' ', '_') .'_captcha'; + $form[$varsuffix] = array( + '#type' => 'fieldset', + '#title' => t('Captcha points for the role @role', array('@role' => $role)), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + foreach($captcha_points as $captcha_point => $captcha_point_description) { $varname = $captcha_point .'_'. $varsuffix; $form[$varsuffix][$varname] = array( '#type' => 'checkbox', '#title' => $captcha_point_description, - '#default_value' => variable_get($varname, NULL) + '#default_value' => variable_get($varname, NULL), ); } } @@ -57,9 +81,9 @@ $form['captcha_type'] = array( '#type' => 'select', '#title' => t('Type of captcha to use'), - '#default_value' => variable_get('captcha_type','captcha'), + '#default_value' => variable_get('captcha_type', 'captcha'), '#options' => $captchamodules, - '#description' => t('Select what kind of challenge you want to pose to the user') + '#description' => t('Select what kind of challenge you want to pose to the user.'), ); $form['captcha_description'] = array( @@ -71,43 +95,43 @@ $form['captcha_override_module_description'] = array( '#type' => 'checkbox', - '#title' => t('Override module's form item description?'), + '#title' => t("Override module's form item description"), '#default_value' => variable_get('captcha_override_module_description', 0), - '#description' => t('Override the captcha module's form item description to the one set above.'), + '#description' => t("Override the captcha module's form item description to the one set above."), ); - - return $form; + return system_settings_form($form); } - - -function captcha_form_alter($formid, &$form) { - +/** + * Implementation of hook_form_alter(). + */ +function captcha_form_alter($form_id, &$form) { global $user; $captcha_type = variable_get("captcha_type", NULL); - if (!$captcha_type) return; + if (!$captcha_type) { + return; + } - $flag = true; + $flag = TRUE; $trigger = NULL; foreach($user->roles as $role) { - $candidate_trigger = $formid .'_'. strtr($role,' ','_') .'_captcha'; + $candidate_trigger = $form_id .'_'. strtr($role, ' ', '_') .'_captcha'; if (variable_get($candidate_trigger, NULL)) { $trigger = $candidate_trigger; } else { - $flag = false; + $flag = FALSE; break; } } if ($flag && isset($trigger)) { $form['#submit'] = array('captcha_submit' => array()) + $form['#submit']; - if (!_captcha_validate($_POST['edit']['captcha_response'])) { - //use call_func because module_invoke does not allow call by reference. + if (!_captcha_validate($_POST['captcha_response'])) { if (module_hook($captcha_type, 'captchachallenge')) { - call_user_func_array($captcha_type.'_captchachallenge', array(&$form, &$_SESSION['captcha'])); + call_user_func_array($captcha_type .'_captchachallenge', array(&$form, &$_SESSION['captcha'])); if(variable_get('captcha_override_module_description', 0)) { unset($form['captcha_response']['#description']); } @@ -124,10 +148,11 @@ } } - /** -* On submit, captcha is reset -*/ + * Implementation of hook_submit(). + * + * On submit, captcha is reset. + */ function captcha_submit() { if($_SESSION['captcha_correct']) { unset($_SESSION['captcha_correct']); @@ -135,25 +160,33 @@ } } - +/** + * Helper function validates captchas. + */ function _captcha_validate($captcha_response) { - if ($_SESSION['captcha_correct']) return TRUE; - if (is_array($captcha_response)) $captcha_response = $captcha_response['#value']; - if (trim($captcha_response) == '') return FALSE; + if ($_SESSION['captcha_correct']) { + return TRUE; + } + if (is_array($captcha_response)) { + $captcha_response = $captcha_response['#value']; + } + if (trim($captcha_response) == '') { + return FALSE; + } global $user; $captcha_type = variable_get("captcha_type", NULL); $trigger = NULL; if (module_hook($captcha_type, 'captchavalidate')) { - call_user_func_array($captcha_type.'_captchavalidate', array(&$captcha_response, &$_SESSION['captcha_correct'])); + call_user_func_array($captcha_type .'_captchavalidate', array(&$captcha_response, &$_SESSION['captcha_correct'])); } return $_SESSION['captcha_correct']; } -/* +/** * Default implementation of the captcha challenge & validation */ function captcha_captchachallenge(&$form, &$captcha) { @@ -161,19 +194,20 @@ $x = rand(1,10); $y = rand(1,10); - $captcha = ($x + $y) . ''; + $captcha = (string)($x + $y); $form['captcha_response'] = array ( '#type' => 'textfield', '#title' => t('Math Question: What is %problem?', array('%problem' => $x .' + '. $y)), - '#defaultvalue' => '', '#description' => t('Please solve the math problem above and type in the result. e.g. for 1+1, type 2.'), '#weight' => 0, '#required' => TRUE, - '#validate' => array('_captcha_validate' => array()) + '#validate' => array('_captcha_validate' => array()), ); - } +/** + * Default implementation of the captcha validation function. + */ function captcha_captchavalidate(&$captcha_word, &$correct) { $captcha_word = drupal_strtolower($captcha_word); if ($captcha_word == $_SESSION['captcha']) { @@ -184,5 +218,3 @@ form_set_error('captcha_response', t('The answer you entered to the math problem is incorrect.')); } } - -?> --- modules/captcha/captcha.info +++ modules/captcha/captcha.info @@ -0,0 +1,3 @@ +; $Id$ +name = Captcha +description = Adds a Captcha to various forms to help prevent spam submissions.