Index: captcha.module =================================================================== RCS file: /cvs/drupal/contributions/modules/captcha/captcha.module,v retrieving revision 1.25 diff -u -r1.25 captcha.module --- captcha.module 26 Jul 2006 19:32:02 -0000 1.25 +++ captcha.module 27 Sep 2006 18:34:35 -0000 @@ -1,89 +1,116 @@ 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."); - break; + case 'admin/help#captcha': + return ''. t('Adds a Captcha to the registration form.') .'
'; } - return $output; } +/** + * Implementation of hook_menu() + */ +function captcha_menu($may_cache) { + $items = array(); + if ($may_cache) { + $items[] = array( + 'path' => 'admin/user/captcha', + 'callback' => 'captcha_settings', + 'title' => t('captcha settings'), + 'description' => t('Adds a Captcha to the registration form.'), + 'access' => user_access('administer site configuration') + ); + } + return $items; +} +/** + * Menu callback: settings page + */ function captcha_settings() { + return drupal_get_form('captcha_settings_form'); +} +/** + * Form builder for the captcha settings. + */ +function captcha_settings_form() { //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) { + 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) { $varname = $captcha_point .'_'. $varsuffix; $form[$varsuffix][$varname] = array( - '#type' => 'checkbox', - '#title' => $captcha_point_description, - '#default_value' => variable_get($varname, NULL) - ); + '#type' => 'checkbox', + '#title' => $captcha_point_description, + '#default_value' => variable_get($varname, NULL) + ); } } // preprocess array into a map - foreach(module_implements('captchachallenge') as $module) { + foreach (module_implements('captchachallenge') as $module) { $captchamodules[$module] = $module; } - + $form['captcha_type'] = array( - '#type' => 'select', - '#title' => t('Type of captcha to use'), - '#default_value' => variable_get('captcha_type','captcha'), - '#options' => $captchamodules, - '#description' => t('Select what kind of challenge you want to pose to the user') - ); + '#type' => 'select', + '#title' => t('Type of captcha to use'), + '#default_value' => variable_get('captcha_type', 'captcha'), + '#options' => $captchamodules, + '#description' => t('Select what kind of challenge you want to pose to the user') + ); - return $form; + return system_settings_form($form); } +/** +* Implementation of hook_form_alter(). +*/ function captcha_form_alter($formid, &$form) { - global $user; - $captcha_type = variable_get("captcha_type", NULL); - - if (!$captcha_type) return; + $captcha_type = variable_get('captcha_type', NULL); + + if (!$captcha_type) { + return; + } - $flag = true; + $flag = TRUE; $trigger = NULL; - foreach($user->roles as $role) { - $candidate_trigger = $formid .'_'. strtr($role,' ','_') .'_captcha'; + foreach ($user->roles as $role) { + $candidate_trigger = $formid .'_'. strtr($role, ' ', '_') .'_captcha'; if (variable_get($candidate_trigger, NULL)) { $trigger = $candidate_trigger; } else { - $flag = false; + $flag = FALSE; break; } } @@ -92,60 +119,41 @@ if (!_captcha_validate($_POST['edit']['captcha_response'])) { //use call_func because module_invoke does not allow call by reference. 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'])); } } } } - /** * On submit, captcha is reset */ function captcha_submit() { - if($_SESSION['captcha_correct']) { + if ($_SESSION['captcha_correct']) { unset($_SESSION['captcha_correct']); - unset($_SESSION['captcha']); - } -} - - -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; - - 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'])); + unset($_SESSION['captcha']); } - - return $_SESSION['captcha_correct']; } /* * Default implementation of the captcha challenge & validation */ function captcha_captchachallenge(&$form, &$captcha) { + $x = rand(1, 10); + $y = rand(1, 10); - $x = rand(1,10); - $y = rand(1,10); + $captcha = $x + $y; - $captcha = ($x + $y) . ''; - $form['captcha_response'] = array ( + $form['captcha_response'] = array( '#type' => 'textfield', - '#title' => t('Math Question: What is %problem?', array('%problem' => $x .' + '. $y)), + '#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, + '#size' => 10, + '#description' => t('Please solve the math problem above and type in the result. e.g. for 1 + 1, type 2'), '#required' => TRUE, - '#validate' => array('_captcha_validate' => array()) + '#validate' => array('_captcha_validate' => array()), + '#weight' => -1, ); - } function captcha_captchavalidate(&$captcha_word, &$correct) { @@ -159,4 +167,26 @@ } } +function _captcha_validate($captcha_response) { + global $user; + + if ($_SESSION['captcha_correct']) { + return TRUE; + } + if (is_array($captcha_response)) { + $captcha_response = $captcha_response['#value']; + } + if (trim($captcha_response) == '') { + return FALSE; + } + $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'])); + } + + return $_SESSION['captcha_correct']; +} + ?>