'. 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, ); $items[] = array( 'path' => 'admin/settings/captcha/captcha', 'title' => t('Captcha'), 'description' => t('Administer how and where Captchas are used.'), 'access' => user_access('administer site configuration'), 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items[] = array( 'path' => 'admin/settings/captcha/points', 'title' => t('Captcha Points'), 'description' => t('Administer in what forms Captchas are applied.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('captcha_admin_points'), 'access' => user_access('administer site configuration'), 'type' => MENU_LOCAL_TASK, ); } return $items; } function _captcha_supported_challenges_types(){ static $captcha_challenges = NULL; if($captcha_challenges != NULL) { return $captcha_challenges; } $captcha_challenges['none'] = 'none'; foreach(module_implements('captcha') as $module) { $result = call_user_func_array($module .'_captcha', 'list'); if (isset($result)) { //&& is_array($result)) { foreach($result as $challenge) { $captcha_challenges[$module.'/'.$challenge] = $module.'/'.$challenge; } } } return $captcha_challenges; } function _captcha_points() { $captcha_points = variable_get('captcha_points',NULL); if($captcha_points == NULL) { $captcha_points = array( 'comment_form' => t('Comment form'), 'contact_mail_user' => t('User contact form'), 'contact_mail_page' => t('Sitewide contact form'), ); variable_set('captcha_points',serialize($captcha_points)); } else { $captcha_points = unserialize($captcha_points); } return $captcha_points; } function captcha_admin_points() { $captcha_points = _captcha_points(); $output = ''; if(function_exists('form_store_get_all')) { $form = array('#description' => t('Enable/Disable captcha points.')); $known_forms = form_store_get_all(); $form['captcha_points']['#tree'] = true; foreach($known_forms as $item) { $form['captcha_points'][$item->form_id] = array( '#type' => 'checkbox', '#title' => $item->form_id.' - '.$item->description, '#default_value' => isset($captcha_points[$item->form_id]), ); } $output = system_settings_form($form); } return $output; } function captcha_admin_points_submit($form_id, $form_values) { if($form_id == 'captcha_admin_points') { $known_forms = form_store_get_all(); $captcha_points = array(); foreach($form_values['captcha_points'] as $key => $selected) { if($selected) $captcha_points[$key] = $known_forms[$key]->description; } variable_set('captcha_points',serialize($captcha_points)); drupal_set_message(t('Your captcha points were sucessful updated, please configure the challenge options in the other tab.'),'status'); } } /** * Helper function generates admin settings page. */ function captcha_admin_settings() { $captcha_points = _captcha_points(); $roles = variable_get('captcha_roles',user_roles()); $captcha_challenges = _captcha_supported_challenges_types(); foreach($roles as $role) { $varsuffix = strtr($role, ' ', '_') .'_captcha'; $form[$varsuffix] = array( '#type' => 'fieldset', '#title' => t('Captcha points for the role @role', array('@role' => $role)), '#description' => t('Select what kind of challenge you want to pose to the user in each captcha point.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); foreach($captcha_points as $captcha_point => $captcha_point_description) { $varname = $captcha_point .'_'. $varsuffix; $form[$varsuffix][$varname] = array( '#type' => 'select', '#title' => t('@point', array('@point' => $captcha_point_description)), '#default_value' => variable_get($varname, 'captcha/Math'), '#options' => $captcha_challenges, ); } } return system_settings_form($form); } /** * Implementation of hook_form_alter(). */ function captcha_form_alter($form_id, &$form) { global $captcha; global $user; $seed = $form['form_token']['#default_value']; if($_SESSION['captcha'][$seed]['success'] === TRUE) return; foreach($user->roles as $role) { $candidate_trigger = $form_id .'_'. strtr($role, ' ', '_') .'_captcha'; $captcha_type = variable_get($candidate_trigger,NULL); if($captcha_type != NULL) { //Found one valid captcha challenge for this point. break; } } if($captcha_type == 'none') { //One of the user roles doesn't have captcha challenge for this point, aborting. return; } elseif($captcha_type == NULL) { //captcha_type null; return; } $challenge = explode('/',$captcha_type); $result = module_invoke($challenge[0], 'captcha', 'generate', $challenge[1]); $form = array_merge($form,$result['form']); $form['#validate'] += array('captcha_validate' => array()); $form['#pre_render'] = array('captcha_prerender'); $_SESSION['captcha'][$seed]['success'] = FALSE; $_SESSION['captcha'][$seed]['form_id'] = $form_id; $_SESSION['captcha'][$seed]['form'] = $result['form']; $_SESSION['captcha'][$seed]['new_value'] = $result['value']; $_SESSION['captcha'][$seed]['preprocess'] = isset($result['preprocess'])? $result['preprocess'] : FALSE; $_SESSION['captcha'][$seed]['module'] = $challenge[0]; $_SESSION['captcha'][$seed]['type'] = $challenge[1]; } function captcha_validate($form_id, $form_item) { $seed = $form_item['form_token']; if($_SESSION['captcha'][$seed]['preprocess']) { $value = module_invoke($_SESSION['captcha'][$seed]['module'],'captcha','process',$_SESSION['captcha'][$seed]['type'],$form_item['captcha_challenge']); } else { $value = $form_item['captcha_challenge']; } if($value == $_SESSION['captcha'][$seed]['value']) { $_SESSION['captcha'][$seed]['success'] = TRUE; return; } form_set_error('captcha_response', t('The answer you entered to the captcha challenge is incorrect.')); } /** * Implementation of form #pre_render. * */ function captcha_prerender($form_id, $form) { $seed = $form['#post']['form_token']; $_SESSION['captcha'][$seed]['value'] = $_SESSION['captcha'][$seed]['new_value']; if($_SESSION['captcha'][$seed]['success']) { unset($form['captcha_challenge']); unset($_SESSION['captcha'][$seed]); } } /** * Default implementation of hook_captcha */ function captcha_captcha() { $args = func_get_args(); $op = array_shift($args); switch($op) { case 'list': return array("Math"); case 'generate': $captcha_type = array_shift($args); $result = array(); if($captcha_type == "Math") { $answer = mt_rand(1, 20); $x = mt_rand(1, $answer); $y = $answer - $x; $result['value'] = (string)($answer); $result['form']['captcha_challenge'] = array ( '#type' => 'textfield', '#title' => t('Math Question: What is %problem?', array('%problem' => $x .' + '. $y)), '#description' => t('Please solve the math problem above and type in the result. e.g. for 1+1, type 2.'), '#weight' => 0, '#required' => TRUE, ); } return $result; } }