annotate sites/all/modules/captcha/captcha.module @ 9:830c812b520f

added smtp module
author root <root@paio.local>
date Mon, 28 Oct 2013 15:34:27 +0000
parents b74b41bb73f0
children
rev   line source
danielebarchiesi@2 1 <?php
danielebarchiesi@2 2
danielebarchiesi@2 3 /**
danielebarchiesi@2 4 * @file
danielebarchiesi@2 5 * This module enables basic CAPTCHA functionality:
danielebarchiesi@2 6 * administrators can add a CAPTCHA to desired forms that users without
danielebarchiesi@2 7 * the 'skip CAPTCHA' permission (typically anonymous visitors) have
danielebarchiesi@2 8 * to solve.
danielebarchiesi@2 9 *
danielebarchiesi@2 10 */
danielebarchiesi@2 11
danielebarchiesi@2 12 /**
danielebarchiesi@2 13 * Constants for CAPTCHA persistence.
danielebarchiesi@2 14 * TODO: change these integers to strings because the CAPTCHA settings form saves them as strings in the variables table anyway?
danielebarchiesi@2 15 */
danielebarchiesi@2 16
danielebarchiesi@2 17 // Always add a CAPTCHA (even on every page of a multipage workflow).
danielebarchiesi@2 18 define('CAPTCHA_PERSISTENCE_SHOW_ALWAYS', 0);
danielebarchiesi@2 19 // Only one CAPTCHA has to be solved per form instance/multi-step workflow.
danielebarchiesi@2 20 define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE', 1);
danielebarchiesi@2 21 // Once the user answered correctly for a CAPTCHA on a certain form type,
danielebarchiesi@2 22 // no more CAPTCHAs will be offered anymore for that form.
danielebarchiesi@2 23 define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE', 2);
danielebarchiesi@2 24 // Once the user answered correctly for a CAPTCHA on the site,
danielebarchiesi@2 25 // no more CAPTCHAs will be offered anymore.
danielebarchiesi@2 26 define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL', 3);
danielebarchiesi@2 27
danielebarchiesi@2 28 define('CAPTCHA_STATUS_UNSOLVED', 0);
danielebarchiesi@2 29 define('CAPTCHA_STATUS_SOLVED', 1);
danielebarchiesi@2 30 define('CAPTCHA_STATUS_EXAMPLE', 2);
danielebarchiesi@2 31
danielebarchiesi@2 32 define('CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE', 0);
danielebarchiesi@2 33 define('CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE', 1);
danielebarchiesi@2 34
danielebarchiesi@2 35 /**
danielebarchiesi@2 36 * Implementation of hook_help().
danielebarchiesi@2 37 */
danielebarchiesi@2 38 function captcha_help($path, $arg) {
danielebarchiesi@2 39 switch ($path) {
danielebarchiesi@2 40 case 'admin/help#captcha':
danielebarchiesi@2 41 $output = '<p>' . t('"CAPTCHA" is an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart". It is typically a challenge-response test to determine whether the user is human. The CAPTCHA module is a tool to fight automated submission by malicious users (spamming) of for example comments forms, user registration forms, guestbook forms, etc. You can extend the desired forms with an additional challenge, which should be easy for a human to solve correctly, but hard enough to keep automated scripts and spam bots out.') . '</p>';
danielebarchiesi@2 42 $output .= '<p>' . t('Note that the CAPTCHA module interacts with page caching (see <a href="!performancesettings">performance settings</a>). Because the challenge should be unique for each generated form, the caching of the page it appears on is prevented. Make sure that these forms do not appear on too many pages or you will lose much caching efficiency. For example, if you put a CAPTCHA on the user login block, which typically appears on each page for anonymous visitors, caching will practically be disabled. The comment submission forms are another example. In this case you should set the <em>Location of comment submission form</em> to <em>Display on separate page</em> in the comment settings of the relevant <a href="!contenttypes">content types</a> for better caching efficiency.',
danielebarchiesi@2 43 array(
danielebarchiesi@2 44 '!performancesettings' => url('admin/config/development/performance'),
danielebarchiesi@2 45 '!contenttypes' => url('admin/structure/types'),
danielebarchiesi@2 46 )
danielebarchiesi@2 47 ) . '</p>';
danielebarchiesi@2 48 $output .= '<p>' . t('CAPTCHA is a trademark of Carnegie Mellon University.') . '</p>';
danielebarchiesi@2 49 return $output;
danielebarchiesi@2 50 case 'admin/config/people/captcha':
danielebarchiesi@2 51 case 'admin/config/people/captcha/captcha':
danielebarchiesi@2 52 case 'admin/config/people/captcha/captcha/settings':
danielebarchiesi@2 53 $output = '<p>' . t('A CAPTCHA can be added to virtually each Drupal form. Some default forms are already provided in the form list, but arbitrary forms can be easily added and managed when the option <em>Add CAPTCHA administration links to forms</em> is enabled.') . '</p>';
danielebarchiesi@2 54 $output .= '<p>' . t('Users with the <em>Skip CAPTCHA</em> <a href="@perm">permission</a> won\'t be offered a challenge. Be sure to grant this permission to the trusted users (e.g. site administrators). If you want to test a protected form, be sure to do it as a user without the <em>Skip CAPTCHA</em> permission (e.g. as anonymous user).', array('@perm' => url('admin/people/permissions'))) . '</p>';
danielebarchiesi@2 55 return $output;
danielebarchiesi@2 56 }
danielebarchiesi@2 57 }
danielebarchiesi@2 58
danielebarchiesi@2 59 /**
danielebarchiesi@2 60 * Implementation of hook_menu().
danielebarchiesi@2 61 */
danielebarchiesi@2 62 function captcha_menu() {
danielebarchiesi@2 63 $items = array();
danielebarchiesi@2 64 // main configuration page of the basic CAPTCHA module
danielebarchiesi@2 65 $items['admin/config/people/captcha'] = array(
danielebarchiesi@2 66 'title' => 'CAPTCHA',
danielebarchiesi@2 67 'description' => 'Administer how and where CAPTCHAs are used.',
danielebarchiesi@2 68 'file' => 'captcha.admin.inc',
danielebarchiesi@2 69 'page callback' => 'drupal_get_form',
danielebarchiesi@2 70 'page arguments' => array('captcha_admin_settings'),
danielebarchiesi@2 71 'access arguments' => array('administer CAPTCHA settings'),
danielebarchiesi@2 72 'type' => MENU_NORMAL_ITEM,
danielebarchiesi@2 73 );
danielebarchiesi@2 74 // the default local task (needed when other modules want to offer
danielebarchiesi@2 75 // alternative CAPTCHA types and their own configuration page as local task)
danielebarchiesi@2 76 $items['admin/config/people/captcha/captcha'] = array(
danielebarchiesi@2 77 'title' => 'CAPTCHA',
danielebarchiesi@2 78 'access arguments' => array('administer CAPTCHA settings'),
danielebarchiesi@2 79 'type' => MENU_DEFAULT_LOCAL_TASK,
danielebarchiesi@2 80 'weight' => -20,
danielebarchiesi@2 81 );
danielebarchiesi@2 82 $items['admin/config/people/captcha/captcha/settings'] = array(
danielebarchiesi@2 83 'title' => 'General settings',
danielebarchiesi@2 84 'access arguments' => array('administer CAPTCHA settings'),
danielebarchiesi@2 85 'type' => MENU_DEFAULT_LOCAL_TASK,
danielebarchiesi@2 86 'weight' => 0,
danielebarchiesi@2 87 );
danielebarchiesi@2 88 $items['admin/config/people/captcha/captcha/examples'] = array(
danielebarchiesi@2 89 'title' => 'Examples',
danielebarchiesi@2 90 'description' => 'An overview of the available challenge types with examples.',
danielebarchiesi@2 91 'file' => 'captcha.admin.inc',
danielebarchiesi@2 92 'page callback' => 'drupal_get_form',
danielebarchiesi@2 93 'page arguments' => array('captcha_examples', 6, 7),
danielebarchiesi@2 94 'access arguments' => array('administer CAPTCHA settings'),
danielebarchiesi@2 95 'type' => MENU_LOCAL_TASK,
danielebarchiesi@2 96 'weight' => 5,
danielebarchiesi@2 97 );
danielebarchiesi@2 98 $items['admin/config/people/captcha/captcha/captcha_point'] = array(
danielebarchiesi@2 99 'title' => 'CAPTCHA point administration',
danielebarchiesi@2 100 'file' => 'captcha.admin.inc',
danielebarchiesi@2 101 'page callback' => 'captcha_point_admin',
danielebarchiesi@2 102 'page arguments' => array(6, 7),
danielebarchiesi@2 103 'access arguments' => array('administer CAPTCHA settings'),
danielebarchiesi@2 104 'type' => MENU_CALLBACK,
danielebarchiesi@2 105 );
danielebarchiesi@2 106 return $items;
danielebarchiesi@2 107 }
danielebarchiesi@2 108
danielebarchiesi@2 109 /**
danielebarchiesi@2 110 * Implementation of hook_permission().
danielebarchiesi@2 111 */
danielebarchiesi@2 112 function captcha_permission() {
danielebarchiesi@2 113 return array(
danielebarchiesi@2 114 'administer CAPTCHA settings' => array(
danielebarchiesi@2 115 'title' => t('Administer CAPTCHA settings'),
danielebarchiesi@2 116 ),
danielebarchiesi@2 117 'skip CAPTCHA' => array(
danielebarchiesi@2 118 'title' => t('Skip CAPTCHA'),
danielebarchiesi@2 119 'description' => t('Users with this permission will not be offered a CAPTCHA.'),
danielebarchiesi@2 120 ),
danielebarchiesi@2 121 );
danielebarchiesi@2 122 }
danielebarchiesi@2 123
danielebarchiesi@2 124 /**
danielebarchiesi@2 125 * Implementation of hook_theme().
danielebarchiesi@2 126 */
danielebarchiesi@2 127 function captcha_theme() {
danielebarchiesi@2 128 return array(
danielebarchiesi@2 129 'captcha_admin_settings_captcha_points' => array(
danielebarchiesi@2 130 'render element' => 'form',
danielebarchiesi@2 131 ),
danielebarchiesi@2 132 'captcha' => array(
danielebarchiesi@2 133 'render element' => 'element',
danielebarchiesi@2 134 ),
danielebarchiesi@2 135 );
danielebarchiesi@2 136 }
danielebarchiesi@2 137
danielebarchiesi@2 138 /**
danielebarchiesi@2 139 * Implementation of hook_cron().
danielebarchiesi@2 140 *
danielebarchiesi@2 141 * Remove old entries from captcha_sessions table.
danielebarchiesi@2 142 */
danielebarchiesi@2 143 function captcha_cron() {
danielebarchiesi@2 144 // Remove challenges older than 1 day.
danielebarchiesi@2 145 db_delete('captcha_sessions')
danielebarchiesi@2 146 ->condition('timestamp', REQUEST_TIME - 60*60*24, '<')
danielebarchiesi@2 147 ->execute();
danielebarchiesi@2 148 }
danielebarchiesi@2 149
danielebarchiesi@2 150
danielebarchiesi@2 151 /**
danielebarchiesi@2 152 * Implementation of hook_element_info().
danielebarchiesi@2 153 */
danielebarchiesi@2 154 function captcha_element_info() {
danielebarchiesi@2 155 // Define the CAPTCHA form element with default properties.
danielebarchiesi@2 156 $captcha_element = array(
danielebarchiesi@2 157 '#input' => TRUE,
danielebarchiesi@2 158 '#process' => array('captcha_element_process'),
danielebarchiesi@2 159 // The type of challenge: e.g. 'default', 'none', 'captcha/Math', 'image_captcha/Image', ...
danielebarchiesi@2 160 '#captcha_type' => 'default',
danielebarchiesi@2 161 '#default_value' => '',
danielebarchiesi@2 162 // CAPTCHA in admin mode: presolve the CAPTCHA and always show it (despite previous successful responses).
danielebarchiesi@2 163 '#captcha_admin_mode' => FALSE,
danielebarchiesi@2 164 // The default CAPTCHA validation function.
danielebarchiesi@2 165 // TODO: should this be a single string or an array of strings (combined in OR fashion)?
danielebarchiesi@2 166 '#captcha_validate' => 'captcha_validate_strict_equality',
danielebarchiesi@2 167 );
danielebarchiesi@2 168 // Override the default CAPTCHA validation function for case insensitive validation.
danielebarchiesi@2 169 // TODO: shouldn't this be done somewhere else, e.g. in form_alter?
danielebarchiesi@2 170 if (CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE == variable_get('captcha_default_validation', CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE)) {
danielebarchiesi@2 171 $captcha_element['#captcha_validate'] = 'captcha_validate_case_insensitive_equality';
danielebarchiesi@2 172 }
danielebarchiesi@2 173 return array('captcha' => $captcha_element);
danielebarchiesi@2 174 }
danielebarchiesi@2 175
danielebarchiesi@2 176 /**
danielebarchiesi@2 177 * Process callback for CAPTCHA form element.
danielebarchiesi@2 178 */
danielebarchiesi@2 179 function captcha_element_process($element, &$form_state, $complete_form) {
danielebarchiesi@2 180
danielebarchiesi@2 181 module_load_include('inc', 'captcha');
danielebarchiesi@2 182
danielebarchiesi@2 183 // Add Javascript for general CAPTCHA functionality.
danielebarchiesi@2 184 drupal_add_js(drupal_get_path('module', 'captcha') . '/captcha.js');
danielebarchiesi@2 185
danielebarchiesi@2 186 // Prevent caching of the page with CAPTCHA elements.
danielebarchiesi@2 187 // This needs to be done even if the CAPTCHA will be ommitted later:
danielebarchiesi@2 188 // other untrusted users should not get a cached page when
danielebarchiesi@2 189 // the current untrusted user can skip the current CAPTCHA.
danielebarchiesi@2 190 global $conf;
danielebarchiesi@2 191 $conf['cache'] = FALSE;
danielebarchiesi@2 192
danielebarchiesi@2 193 // Get the form ID of the form we are currently processing (which is not
danielebarchiesi@2 194 // necessary the same form that is submitted (if any).
danielebarchiesi@2 195 $this_form_id = $complete_form['form_id']['#value'];
danielebarchiesi@2 196
danielebarchiesi@2 197 // Get the CAPTCHA session ID.
danielebarchiesi@2 198 // If there is a submitted form: try to retrieve and reuse the
danielebarchiesi@2 199 // CAPTCHA session ID from the posted data.
danielebarchiesi@2 200 list($posted_form_id, $posted_captcha_sid) = _captcha_get_posted_captcha_info($element, $form_state, $this_form_id);
danielebarchiesi@2 201 if ($this_form_id == $posted_form_id && isset($posted_captcha_sid)) {
danielebarchiesi@2 202 $captcha_sid = $posted_captcha_sid;
danielebarchiesi@2 203 }
danielebarchiesi@2 204 else {
danielebarchiesi@2 205 // Generate a new CAPTCHA session if we could not reuse one from a posted form.
danielebarchiesi@2 206 $captcha_sid = _captcha_generate_captcha_session($this_form_id, CAPTCHA_STATUS_UNSOLVED);
danielebarchiesi@2 207 }
danielebarchiesi@2 208
danielebarchiesi@2 209 // Store CAPTCHA session ID as hidden field.
danielebarchiesi@2 210 // Strictly speaking, it is not necessary to send the CAPTCHA session id
danielebarchiesi@2 211 // with the form, as the one time CAPTCHA token (see lower) is enough.
danielebarchiesi@2 212 // However, we still send it along, because it can help debugging
danielebarchiesi@2 213 // problems on live sites with only access to the markup.
danielebarchiesi@2 214 $element['captcha_sid'] = array(
danielebarchiesi@2 215 '#type' => 'hidden',
danielebarchiesi@2 216 '#value' => $captcha_sid,
danielebarchiesi@2 217 );
danielebarchiesi@2 218
danielebarchiesi@2 219 // Additional one time CAPTCHA token: store in database and send with form.
danielebarchiesi@2 220 $captcha_token = md5(mt_rand());
danielebarchiesi@2 221 db_update('captcha_sessions')
danielebarchiesi@2 222 ->fields(array('token' => $captcha_token))
danielebarchiesi@2 223 ->condition('csid', $captcha_sid)
danielebarchiesi@2 224 ->execute();
danielebarchiesi@2 225 $element['captcha_token'] = array(
danielebarchiesi@2 226 '#type' => 'hidden',
danielebarchiesi@2 227 '#value' => $captcha_token,
danielebarchiesi@2 228 );
danielebarchiesi@2 229
danielebarchiesi@2 230 // Get implementing module and challenge for CAPTCHA.
danielebarchiesi@2 231 list($captcha_type_module, $captcha_type_challenge) = _captcha_parse_captcha_type($element['#captcha_type']);
danielebarchiesi@2 232
danielebarchiesi@2 233 // Store CAPTCHA information for further processing in
danielebarchiesi@2 234 // - $form_state['captcha_info'], which survives a form rebuild (e.g. node
danielebarchiesi@2 235 // preview), useful in _captcha_get_posted_captcha_info().
danielebarchiesi@2 236 // - $element['#captcha_info'], for post processing functions that do not
danielebarchiesi@2 237 // receive a $form_state argument (e.g. the pre_render callback).
danielebarchiesi@2 238 $form_state['captcha_info'] = array(
danielebarchiesi@2 239 'this_form_id' => $this_form_id,
danielebarchiesi@2 240 'posted_form_id' => $posted_form_id,
danielebarchiesi@2 241 'captcha_sid' => $captcha_sid,
danielebarchiesi@2 242 'module' => $captcha_type_module,
danielebarchiesi@2 243 'captcha_type' => $captcha_type_challenge,
danielebarchiesi@2 244 );
danielebarchiesi@2 245 $element['#captcha_info'] = array(
danielebarchiesi@2 246 'form_id' => $this_form_id,
danielebarchiesi@2 247 'captcha_sid' => $captcha_sid,
danielebarchiesi@2 248 );
danielebarchiesi@2 249
danielebarchiesi@2 250
danielebarchiesi@2 251 if (_captcha_required_for_user($captcha_sid, $this_form_id) || $element['#captcha_admin_mode']) {
danielebarchiesi@2 252 // Generate a CAPTCHA and its solution
danielebarchiesi@2 253 // (note that the CAPTCHA session ID is given as third argument).
danielebarchiesi@2 254 $captcha = module_invoke($captcha_type_module, 'captcha', 'generate', $captcha_type_challenge, $captcha_sid);
danielebarchiesi@2 255 if (!isset($captcha['form']) || !isset($captcha['solution'])) {
danielebarchiesi@2 256 // The selected module did not return what we expected: log about it and quit.
danielebarchiesi@2 257 watchdog('CAPTCHA',
danielebarchiesi@2 258 'CAPTCHA problem: unexpected result from hook_captcha() of module %module when trying to retrieve challenge type %type for form %form_id.',
danielebarchiesi@2 259 array('%type' => $captcha_type_challenge, '%module' => $captcha_type_module, '%form_id' => $this_form_id),
danielebarchiesi@2 260 WATCHDOG_ERROR);
danielebarchiesi@2 261 return $element;
danielebarchiesi@2 262 }
danielebarchiesi@2 263 // Add form elements from challenge as children to CAPTCHA form element.
danielebarchiesi@2 264 $element['captcha_widgets'] = $captcha['form'];
danielebarchiesi@2 265
danielebarchiesi@2 266 // Add a validation callback for the CAPTCHA form element (when not in admin mode).
danielebarchiesi@2 267 if (!$element['#captcha_admin_mode']) {
danielebarchiesi@2 268 $element['#element_validate'] = array('captcha_validate');
danielebarchiesi@2 269 }
danielebarchiesi@2 270
danielebarchiesi@2 271 // Set a custom CAPTCHA validate function if requested.
danielebarchiesi@2 272 if (isset($captcha['captcha_validate'])) {
danielebarchiesi@2 273 $element['#captcha_validate'] = $captcha['captcha_validate'];
danielebarchiesi@2 274 }
danielebarchiesi@2 275
danielebarchiesi@2 276 // Set the theme function.
danielebarchiesi@2 277 $element['#theme'] = 'captcha';
danielebarchiesi@2 278
danielebarchiesi@2 279 // Add pre_render callback for additional CAPTCHA processing.
danielebarchiesi@2 280 if (!isset($element['#pre_render'])) {
danielebarchiesi@2 281 $element['#pre_render'] = array();
danielebarchiesi@2 282 }
danielebarchiesi@2 283 $element['#pre_render'][] = 'captcha_pre_render_process';
danielebarchiesi@2 284
danielebarchiesi@2 285 // Store the solution in the #captcha_info array.
danielebarchiesi@2 286 $element['#captcha_info']['solution'] = $captcha['solution'];
danielebarchiesi@2 287
danielebarchiesi@2 288 // Make sure we can use a top level form value $form_state['values']['captcha_response'], even if the form has #tree=true.
danielebarchiesi@2 289 $element['#tree'] = FALSE;
danielebarchiesi@2 290
danielebarchiesi@2 291 }
danielebarchiesi@2 292
danielebarchiesi@2 293 return $element;
danielebarchiesi@2 294 }
danielebarchiesi@2 295
danielebarchiesi@2 296
danielebarchiesi@2 297 /**
danielebarchiesi@2 298 * Theme function for a CAPTCHA element.
danielebarchiesi@2 299 *
danielebarchiesi@2 300 * Render it in a fieldset if a description of the CAPTCHA
danielebarchiesi@2 301 * is available. Render it as is otherwise.
danielebarchiesi@2 302 */
danielebarchiesi@2 303 function theme_captcha($variables) {
danielebarchiesi@2 304 $element = $variables['element'];
danielebarchiesi@2 305 if (!empty($element['#description']) && isset($element['captcha_widgets'])) {
danielebarchiesi@2 306 $fieldset = array(
danielebarchiesi@2 307 '#type' => 'fieldset',
danielebarchiesi@2 308 '#title' => t('CAPTCHA'),
danielebarchiesi@2 309 '#description' => $element['#description'],
danielebarchiesi@2 310 '#children' => drupal_render_children($element),
danielebarchiesi@2 311 '#attributes' => array('class' => array('captcha')),
danielebarchiesi@2 312 );
danielebarchiesi@2 313 return theme('fieldset', array('element' => $fieldset));
danielebarchiesi@2 314 }
danielebarchiesi@2 315 else {
danielebarchiesi@2 316 return '<div class="captcha">' . drupal_render_children($element) . '</div>';
danielebarchiesi@2 317 }
danielebarchiesi@2 318 }
danielebarchiesi@2 319
danielebarchiesi@2 320
danielebarchiesi@2 321 /**
danielebarchiesi@2 322 * Implementation of hook_form_alter().
danielebarchiesi@2 323 *
danielebarchiesi@2 324 * This function adds a CAPTCHA to forms for untrusted users if needed and adds
danielebarchiesi@2 325 * CAPTCHA administration links for site administrators if this option is enabled.
danielebarchiesi@2 326 */
danielebarchiesi@2 327 function captcha_form_alter(&$form, &$form_state, $form_id) {
danielebarchiesi@2 328
danielebarchiesi@2 329 if (!user_access('skip CAPTCHA')) {
danielebarchiesi@2 330 // Visitor does not have permission to skip CAPTCHAs.
danielebarchiesi@2 331 module_load_include('inc', 'captcha');
danielebarchiesi@2 332
danielebarchiesi@2 333 // Get CAPTCHA type and module for given form_id.
danielebarchiesi@2 334 $captcha_point = captcha_get_form_id_setting($form_id);
danielebarchiesi@2 335 if ($captcha_point && $captcha_point->captcha_type) {
danielebarchiesi@2 336 module_load_include('inc', 'captcha');
danielebarchiesi@2 337 // Build CAPTCHA form element.
danielebarchiesi@2 338 $captcha_element = array(
danielebarchiesi@2 339 '#type' => 'captcha',
danielebarchiesi@2 340 '#captcha_type' => $captcha_point->module . '/' . $captcha_point->captcha_type,
danielebarchiesi@2 341 );
danielebarchiesi@2 342 // Add a CAPTCHA description if required.
danielebarchiesi@2 343 if (variable_get('captcha_add_captcha_description', TRUE)) {
danielebarchiesi@2 344 $captcha_element['#description'] = _captcha_get_description();
danielebarchiesi@2 345 }
danielebarchiesi@2 346
danielebarchiesi@2 347 // Get placement in form and insert in form.
danielebarchiesi@2 348 $captcha_placement = _captcha_get_captcha_placement($form_id, $form);
danielebarchiesi@2 349 _captcha_insert_captcha_element($form, $captcha_placement, $captcha_element);
danielebarchiesi@2 350 }
danielebarchiesi@2 351 }
danielebarchiesi@2 352 else if (
danielebarchiesi@2 353 variable_get('captcha_administration_mode', FALSE)
danielebarchiesi@2 354 && user_access('administer CAPTCHA settings')
danielebarchiesi@2 355 && (arg(0) != 'admin' || variable_get('captcha_allow_on_admin_pages', FALSE))
danielebarchiesi@2 356 ) {
danielebarchiesi@2 357 // Add CAPTCHA administration tools.
danielebarchiesi@2 358 module_load_include('inc', 'captcha');
danielebarchiesi@2 359
danielebarchiesi@2 360 $captcha_point = captcha_get_form_id_setting($form_id);
danielebarchiesi@2 361 // For administrators: show CAPTCHA info and offer link to configure it
danielebarchiesi@2 362 $captcha_element = array(
danielebarchiesi@2 363 '#type' => 'fieldset',
danielebarchiesi@2 364 '#title' => t('CAPTCHA'),
danielebarchiesi@2 365 '#collapsible' => TRUE,
danielebarchiesi@2 366 '#collapsed' => TRUE,
danielebarchiesi@2 367 '#attributes' => array('class' => array('captcha-admin-links')),
danielebarchiesi@2 368 );
danielebarchiesi@2 369 if ($captcha_point !== NULL && $captcha_point->captcha_type) {
danielebarchiesi@2 370 $captcha_element['#title'] = t('CAPTCHA: challenge "@type" enabled', array('@type' => $captcha_point->captcha_type));
danielebarchiesi@2 371 $captcha_element['#description'] = t('Untrusted users will see a CAPTCHA here (<a href="@settings">general CAPTCHA settings</a>).',
danielebarchiesi@2 372 array('@settings' => url('admin/config/people/captcha'))
danielebarchiesi@2 373 );
danielebarchiesi@2 374 $captcha_element['challenge'] = array(
danielebarchiesi@2 375 '#type' => 'item',
danielebarchiesi@2 376 '#title' => t('Enabled challenge'),
danielebarchiesi@2 377 '#markup' => t('%type by module %module (<a href="@change">change</a>, <a href="@disable">disable</a>)', array(
danielebarchiesi@2 378 '%type' => $captcha_point->captcha_type,
danielebarchiesi@2 379 '%module' => $captcha_point->module,
danielebarchiesi@2 380 '@change' => url("admin/config/people/captcha/captcha/captcha_point/$form_id", array('query' => drupal_get_destination())),
danielebarchiesi@2 381 '@disable' => url("admin/config/people/captcha/captcha/captcha_point/$form_id/disable", array('query' => drupal_get_destination())),
danielebarchiesi@2 382 )),
danielebarchiesi@2 383 );
danielebarchiesi@2 384 // Add an example challenge with solution.
danielebarchiesi@2 385 // This does not work with the reCAPTCHA and Egglue challenges as
danielebarchiesi@2 386 // discussed in http://drupal.org/node/487032 and
danielebarchiesi@2 387 // http://drupal.org/node/525586. As a temporary workaround, we
danielebarchiesi@2 388 // blacklist the reCAPTCHA and Egglue challenges and do not show
danielebarchiesi@2 389 // an example challenge.
danielebarchiesi@2 390 // TODO: Once the issues mentioned above are fixed, this workaround
danielebarchiesi@2 391 // should be removed.
danielebarchiesi@2 392 if ($captcha_point->module != 'recaptcha' && $captcha_point->module != 'egglue_captcha') {
danielebarchiesi@2 393 $captcha_element['example'] = array(
danielebarchiesi@2 394 '#type' => 'fieldset',
danielebarchiesi@2 395 '#title' => t('Example'),
danielebarchiesi@2 396 '#description' => t('This is a pre-solved, non-blocking example of this challenge.'),
danielebarchiesi@2 397 );
danielebarchiesi@2 398 $captcha_element['example']['example_captcha'] = array(
danielebarchiesi@2 399 '#type' => 'captcha',
danielebarchiesi@2 400 '#captcha_type' => $captcha_point->module . '/' . $captcha_point->captcha_type,
danielebarchiesi@2 401 '#captcha_admin_mode' => TRUE,
danielebarchiesi@2 402 );
danielebarchiesi@2 403 }
danielebarchiesi@2 404 }
danielebarchiesi@2 405 else {
danielebarchiesi@2 406 $captcha_element['#title'] = t('CAPTCHA: no challenge enabled');
danielebarchiesi@2 407 $captcha_element['add_captcha'] = array(
danielebarchiesi@2 408 '#markup' => l(t('Place a CAPTCHA here for untrusted users.'), "admin/config/people/captcha/captcha/captcha_point/$form_id", array('query' => drupal_get_destination()))
danielebarchiesi@2 409 );
danielebarchiesi@2 410
danielebarchiesi@2 411 }
danielebarchiesi@2 412 // Get placement in form and insert in form.
danielebarchiesi@2 413 $captcha_placement = _captcha_get_captcha_placement($form_id, $form);
danielebarchiesi@2 414 _captcha_insert_captcha_element($form, $captcha_placement, $captcha_element);
danielebarchiesi@2 415
danielebarchiesi@2 416 }
danielebarchiesi@2 417
danielebarchiesi@2 418 // Add a warning about caching on the Perfomance settings page.
danielebarchiesi@2 419 if ($form_id == 'system_performance_settings') {
danielebarchiesi@2 420 $icon = theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('warning'), 'title' => t('warning')));
danielebarchiesi@2 421 $form['caching']['captcha'] = array(
danielebarchiesi@2 422 '#type' => 'item',
danielebarchiesi@2 423 '#title' => t('CAPTCHA'),
danielebarchiesi@2 424 '#markup' => t('!icon The CAPTCHA module will disable the caching of pages that contain a CAPTCHA element.', array(
danielebarchiesi@2 425 '!icon' => '<span class="icon">' . $icon . '</span>')
danielebarchiesi@2 426 ),
danielebarchiesi@2 427 '#attributes' => array('class' => array('warning')),
danielebarchiesi@2 428 );
danielebarchiesi@2 429 }
danielebarchiesi@2 430
danielebarchiesi@2 431 }
danielebarchiesi@2 432
danielebarchiesi@2 433 /**
danielebarchiesi@2 434 * CAPTCHA validation function to tests strict equality.
danielebarchiesi@2 435 * @param $solution the solution of the test.
danielebarchiesi@2 436 * @param $response the response to the test.
danielebarchiesi@2 437 * @return TRUE when strictly equal, FALSE otherwise.
danielebarchiesi@2 438 */
danielebarchiesi@2 439 function captcha_validate_strict_equality($solution, $response) {
danielebarchiesi@2 440 return $solution === $response;
danielebarchiesi@2 441 }
danielebarchiesi@2 442
danielebarchiesi@2 443 /**
danielebarchiesi@2 444 * CAPTCHA validation function to tests case insensitive equality.
danielebarchiesi@2 445 * @param $solution the solution of the test.
danielebarchiesi@2 446 * @param $response the response to the test.
danielebarchiesi@2 447 * @return TRUE when case insensitive equal, FALSE otherwise.
danielebarchiesi@2 448 */
danielebarchiesi@2 449 function captcha_validate_case_insensitive_equality($solution, $response) {
danielebarchiesi@2 450 return drupal_strtolower($solution) === drupal_strtolower($response);
danielebarchiesi@2 451 }
danielebarchiesi@2 452
danielebarchiesi@2 453 /**
danielebarchiesi@2 454 * CAPTCHA validation function to tests equality while ignoring spaces.
danielebarchiesi@2 455 * @param $solution the solution of the test.
danielebarchiesi@2 456 * @param $response the response to the test.
danielebarchiesi@2 457 * @return TRUE when equal (ignoring spaces), FALSE otherwise.
danielebarchiesi@2 458 */
danielebarchiesi@2 459 function captcha_validate_ignore_spaces($solution, $response) {
danielebarchiesi@2 460 return preg_replace('/\s/', '', $solution) === preg_replace('/\s/', '', $response);
danielebarchiesi@2 461 }
danielebarchiesi@2 462
danielebarchiesi@2 463 /**
danielebarchiesi@2 464 * CAPTCHA validation function to tests case insensitive equality while ignoring spaces.
danielebarchiesi@2 465 * @param $solution the solution of the test.
danielebarchiesi@2 466 * @param $response the response to the test.
danielebarchiesi@2 467 * @return TRUE when equal (ignoring spaces), FALSE otherwise.
danielebarchiesi@2 468 */
danielebarchiesi@2 469 function captcha_validate_case_insensitive_ignore_spaces($solution, $response) {
danielebarchiesi@2 470 return preg_replace('/\s/', '', drupal_strtolower($solution)) === preg_replace('/\s/', '', drupal_strtolower($response));
danielebarchiesi@2 471 }
danielebarchiesi@2 472
danielebarchiesi@2 473 /**
danielebarchiesi@2 474 * Helper function for getting the posted CAPTCHA info (posted form_id and
danielebarchiesi@2 475 * CAPTCHA sessions ID) from a form in case it is posted.
danielebarchiesi@2 476 *
danielebarchiesi@2 477 * This function hides the form processing mess for several use cases an
danielebarchiesi@2 478 * browser bug workarounds.
danielebarchiesi@2 479 * For example: $element['#post'] can typically be used to get the posted
danielebarchiesi@2 480 * form_id and captcha_sid, but in the case of node preview situations
danielebarchiesi@2 481 * (with correct CAPTCHA response) that does not work and we can get them from
danielebarchiesi@2 482 * $form_state['clicked_button']['#post'].
danielebarchiesi@2 483 * However with Internet Explorer 7, the latter does not work either when
danielebarchiesi@2 484 * submitting the forms (with only one text field) with the enter key
danielebarchiesi@2 485 * (see http://drupal.org/node/534168), in which case we also have to check
danielebarchiesi@2 486 * $form_state['buttons']['button']['0']['#post'].
danielebarchiesi@2 487 *
danielebarchiesi@2 488 * @todo for Drupal 7 version: is this IE7 workaround still needed?
danielebarchiesi@2 489 *
danielebarchiesi@2 490 * @param $element the CAPTCHA element.
danielebarchiesi@2 491 * @param $form_state the form state structure to extract the info from.
danielebarchiesi@2 492 * @param $this_form_id the form ID of the form we are currently processing
danielebarchiesi@2 493 * (which is not necessarily the form that was posted).
danielebarchiesi@2 494 *
danielebarchiesi@2 495 * @return an array with $posted_form_id and $post_captcha_sid (with NULL values
danielebarchiesi@2 496 * if the values could not be found, e.g. for a fresh form).
danielebarchiesi@2 497 */
danielebarchiesi@2 498 function _captcha_get_posted_captcha_info($element, $form_state, $this_form_id) {
danielebarchiesi@2 499 if ($form_state['submitted'] && isset($form_state['captcha_info'])) {
danielebarchiesi@2 500 // We are handling (or rebuilding) an already submitted form,
danielebarchiesi@2 501 // so we already determined the posted form ID and CAPTCHA session ID
danielebarchiesi@2 502 // for this form (from before submitting). Reuse this info.
danielebarchiesi@2 503 $posted_form_id = $form_state['captcha_info']['posted_form_id'];
danielebarchiesi@2 504 $posted_captcha_sid = $form_state['captcha_info']['captcha_sid'];
danielebarchiesi@2 505 }
danielebarchiesi@2 506 else {
danielebarchiesi@2 507 // We have to determine the posted form ID and CAPTCHA session ID
danielebarchiesi@2 508 // from the post data.
danielebarchiesi@2 509 // Because we possibly use raw post data here,
danielebarchiesi@2 510 // we should be extra cautious and filter this data.
danielebarchiesi@2 511 $posted_form_id = isset($form_state['input']['form_id']) ?
danielebarchiesi@2 512 preg_replace("/[^a-z0-9_]/", "", (string) $form_state['input']['form_id'])
danielebarchiesi@2 513 : NULL;
danielebarchiesi@2 514 $posted_captcha_sid = isset($form_state['input']['captcha_sid']) ?
danielebarchiesi@2 515 (int) $form_state['input']['captcha_sid']
danielebarchiesi@2 516 : NULL;
danielebarchiesi@2 517 $posted_captcha_token = isset($form_state['input']['captcha_token']) ?
danielebarchiesi@2 518 preg_replace("/[^a-zA-Z0-9]/", "", (string) $form_state['input']['captcha_token'])
danielebarchiesi@2 519 : NULL;
danielebarchiesi@2 520
danielebarchiesi@2 521 if ($posted_form_id == $this_form_id) {
danielebarchiesi@2 522 // Check if the posted CAPTCHA token is valid for the posted CAPTCHA
danielebarchiesi@2 523 // session ID. Note that we could just check the validity of the CAPTCHA
danielebarchiesi@2 524 // token and extract the CAPTCHA session ID from that (without looking at
danielebarchiesi@2 525 // the actual posted CAPTCHA session ID). However, here we check both
danielebarchiesi@2 526 // the posted CAPTCHA token and session ID: it is a bit more stringent
danielebarchiesi@2 527 // and the database query should also be more efficient (because there is
danielebarchiesi@2 528 // an index on the CAPTCHA session ID).
danielebarchiesi@2 529 if ($posted_captcha_sid != NULL) {
danielebarchiesi@2 530 $expected_captcha_token = db_query(
danielebarchiesi@2 531 "SELECT token FROM {captcha_sessions} WHERE csid = :csid",
danielebarchiesi@2 532 array(':csid' => $posted_captcha_sid)
danielebarchiesi@2 533 )->fetchField();
danielebarchiesi@2 534 if ($expected_captcha_token !== $posted_captcha_token) {
danielebarchiesi@2 535 drupal_set_message(t('CAPTCHA session reuse attack detected.'), 'error');
danielebarchiesi@2 536 // Invalidate the CAPTCHA session.
danielebarchiesi@2 537 $posted_captcha_sid = NULL;
danielebarchiesi@2 538 }
danielebarchiesi@2 539 // Invalidate CAPTCHA token to avoid reuse.
danielebarchiesi@2 540 db_update('captcha_sessions')
danielebarchiesi@2 541 ->fields(array('token' => NULL))
danielebarchiesi@2 542 ->condition('csid', $posted_captcha_sid);
danielebarchiesi@2 543 }
danielebarchiesi@2 544 }
danielebarchiesi@2 545 else {
danielebarchiesi@2 546 // The CAPTCHA session ID is specific to the posted form.
danielebarchiesi@2 547 // Return NULL, so a new session will be generated for this other form.
danielebarchiesi@2 548 $posted_captcha_sid = NULL;
danielebarchiesi@2 549 }
danielebarchiesi@2 550 }
danielebarchiesi@2 551 return array($posted_form_id, $posted_captcha_sid);
danielebarchiesi@2 552 }
danielebarchiesi@2 553
danielebarchiesi@2 554 /**
danielebarchiesi@2 555 * CAPTCHA validation handler.
danielebarchiesi@2 556 *
danielebarchiesi@2 557 * This function is placed in the main captcha.module file to make sure that
danielebarchiesi@2 558 * it is available (even for cached forms, which don't fire
danielebarchiesi@2 559 * captcha_form_alter(), and subsequently don't include additional include
danielebarchiesi@2 560 * files).
danielebarchiesi@2 561 */
danielebarchiesi@2 562 function captcha_validate($element, &$form_state) {
danielebarchiesi@2 563
danielebarchiesi@2 564 $captcha_info = $form_state['captcha_info'];
danielebarchiesi@2 565 $form_id = $captcha_info['this_form_id'];
danielebarchiesi@2 566
danielebarchiesi@2 567 // Get CAPTCHA response.
danielebarchiesi@2 568 $captcha_response = $form_state['values']['captcha_response'];
danielebarchiesi@2 569
danielebarchiesi@2 570 // Get CAPTCHA session from CAPTCHA info
danielebarchiesi@2 571 // TODO: is this correct in all cases: see comment and code in previous revisions?
danielebarchiesi@2 572 $csid = $captcha_info['captcha_sid'];
danielebarchiesi@2 573
danielebarchiesi@2 574 $solution = db_query(
danielebarchiesi@2 575 'SELECT solution FROM {captcha_sessions} WHERE csid = :csid',
danielebarchiesi@2 576 array(':csid' => $csid)
danielebarchiesi@2 577 )
danielebarchiesi@2 578 ->fetchField();
danielebarchiesi@2 579
danielebarchiesi@2 580 // @todo: what is the result when there is no entry for the captcha_session? in D6 it was FALSE, what in D7?
danielebarchiesi@2 581 if ($solution === FALSE) {
danielebarchiesi@2 582 // Unknown challenge_id.
danielebarchiesi@2 583 // TODO: this probably never happens anymore now that there is detection
danielebarchiesi@2 584 // for CAPTCHA session reuse attacks in _captcha_get_posted_captcha_info().
danielebarchiesi@2 585 form_set_error('captcha', t('CAPTCHA validation error: unknown CAPTCHA session ID. Contact the site administrator if this problem persists.'));
danielebarchiesi@2 586 watchdog('CAPTCHA',
danielebarchiesi@2 587 'CAPTCHA validation error: unknown CAPTCHA session ID (%csid).',
danielebarchiesi@2 588 array('%csid' => var_export($csid, TRUE)),
danielebarchiesi@2 589 WATCHDOG_ERROR);
danielebarchiesi@2 590 }
danielebarchiesi@2 591 else {
danielebarchiesi@2 592 // Get CAPTCHA validate function or fall back on strict equality.
danielebarchiesi@2 593 $captcha_validate = $element['#captcha_validate'];
danielebarchiesi@2 594 if (!function_exists($captcha_validate)) {
danielebarchiesi@2 595 $captcha_validate = 'captcha_validate_strict_equality';
danielebarchiesi@2 596 }
danielebarchiesi@2 597 // Check the response with the CAPTCHA validation function.
danielebarchiesi@2 598 // Apart from the traditional expected $solution and received $response,
danielebarchiesi@2 599 // we also provide the CAPTCHA $element and $form_state arrays for more advanced use cases.
danielebarchiesi@2 600 if ($captcha_validate($solution, $captcha_response, $element, $form_state)) {
danielebarchiesi@2 601 // Correct answer.
danielebarchiesi@2 602
danielebarchiesi@2 603 // Store form_id in session (but only if it is useful to do so, avoid setting stuff in session unnecessarily).
danielebarchiesi@2 604 $captcha_persistence = variable_get('captcha_persistence', CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE);
danielebarchiesi@2 605 if ($captcha_persistence == CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL || $captcha_persistence == CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE) {
danielebarchiesi@2 606 $_SESSION['captcha_success_form_ids'][$form_id] = $form_id;
danielebarchiesi@2 607 }
danielebarchiesi@2 608
danielebarchiesi@2 609 // Record success.
danielebarchiesi@2 610 db_update('captcha_sessions')
danielebarchiesi@2 611 ->condition('csid', $csid)
danielebarchiesi@2 612 ->fields(array('status' => CAPTCHA_STATUS_SOLVED))
danielebarchiesi@2 613 ->expression('attempts', 'attempts + 1')
danielebarchiesi@2 614 ->execute();
danielebarchiesi@2 615 }
danielebarchiesi@2 616 else {
danielebarchiesi@2 617 // Wrong answer.
danielebarchiesi@2 618 db_update('captcha_sessions')
danielebarchiesi@2 619 ->condition('csid', $csid)
danielebarchiesi@2 620 ->expression('attempts', 'attempts + 1')
danielebarchiesi@2 621 ->execute();
danielebarchiesi@2 622 // set form error
danielebarchiesi@2 623 form_set_error('captcha_response', t('The answer you entered for the CAPTCHA was not correct.'));
danielebarchiesi@2 624 // update wrong response counter
danielebarchiesi@2 625 if (variable_get('captcha_enable_stats', FALSE)) {
danielebarchiesi@2 626 variable_set('captcha_wrong_response_counter', variable_get('captcha_wrong_response_counter', 0) + 1);
danielebarchiesi@2 627 }
danielebarchiesi@2 628 // log to watchdog if needed
danielebarchiesi@2 629 if (variable_get('captcha_log_wrong_responses', FALSE)) {
danielebarchiesi@2 630 watchdog('CAPTCHA',
danielebarchiesi@2 631 '%form_id post blocked by CAPTCHA module: challenge %challenge (by module %module), user answered "@response", but the solution was "@solution".',
danielebarchiesi@2 632 array('%form_id' => $form_id,
danielebarchiesi@2 633 '@response' => $captcha_response, '@solution' => $solution,
danielebarchiesi@2 634 '%challenge' => $captcha_info['captcha_type'], '%module' => $captcha_info['module'],
danielebarchiesi@2 635 ),
danielebarchiesi@2 636 WATCHDOG_NOTICE);
danielebarchiesi@2 637 }
danielebarchiesi@2 638 }
danielebarchiesi@2 639 }
danielebarchiesi@2 640 }
danielebarchiesi@2 641
danielebarchiesi@2 642 /**
danielebarchiesi@2 643 * Pre-render callback for additional processing of a CAPTCHA form element.
danielebarchiesi@2 644 *
danielebarchiesi@2 645 * This encompasses tasks that should happen after the general FAPI processing
danielebarchiesi@2 646 * (building, submission and validation) but before rendering (e.g. storing the solution).
danielebarchiesi@2 647 *
danielebarchiesi@2 648 * @param $element the CAPTCHA form element
danielebarchiesi@2 649 * @return the manipulated element
danielebarchiesi@2 650 */
danielebarchiesi@2 651 function captcha_pre_render_process($element) {
danielebarchiesi@2 652 module_load_include('inc', 'captcha');
danielebarchiesi@2 653
danielebarchiesi@2 654 // Get form and CAPTCHA information.
danielebarchiesi@2 655 $captcha_info = $element['#captcha_info'];
danielebarchiesi@2 656 $form_id = $captcha_info['form_id'];
danielebarchiesi@2 657 $captcha_sid = (int)($captcha_info['captcha_sid']);
danielebarchiesi@2 658 // Check if CAPTCHA is still required.
danielebarchiesi@2 659 // This check is done in a first phase during the element processing
danielebarchiesi@2 660 // (@see captcha_process), but it is also done here for better support
danielebarchiesi@2 661 // of multi-page forms. Take previewing a node submission for example:
danielebarchiesi@2 662 // when the challenge is solved correctely on preview, the form is still
danielebarchiesi@2 663 // not completely submitted, but the CAPTCHA can be skipped.
danielebarchiesi@2 664 if (_captcha_required_for_user($captcha_sid, $form_id) || $element['#captcha_admin_mode']) {
danielebarchiesi@2 665 // Update captcha_sessions table: store the solution of the generated CAPTCHA.
danielebarchiesi@2 666 _captcha_update_captcha_session($captcha_sid, $captcha_info['solution']);
danielebarchiesi@2 667
danielebarchiesi@2 668 // Handle the response field if it is available and if it is a textfield.
danielebarchiesi@2 669 if (isset($element['captcha_widgets']['captcha_response']['#type']) && $element['captcha_widgets']['captcha_response']['#type'] == 'textfield') {
danielebarchiesi@2 670 // Before rendering: presolve an admin mode challenge or
danielebarchiesi@2 671 // empty the value of the captcha_response form item.
danielebarchiesi@2 672 $value = $element['#captcha_admin_mode'] ? $captcha_info['solution'] : '';
danielebarchiesi@2 673 $element['captcha_widgets']['captcha_response']['#value'] = $value;
danielebarchiesi@2 674 }
danielebarchiesi@2 675 }
danielebarchiesi@2 676 else {
danielebarchiesi@2 677 // Remove CAPTCHA widgets from form.
danielebarchiesi@2 678 unset($element['captcha_widgets']);
danielebarchiesi@2 679 }
danielebarchiesi@2 680
danielebarchiesi@2 681 return $element;
danielebarchiesi@2 682 }
danielebarchiesi@2 683
danielebarchiesi@2 684 /**
danielebarchiesi@2 685 * Default implementation of hook_captcha().
danielebarchiesi@2 686 */
danielebarchiesi@2 687 function captcha_captcha($op, $captcha_type = '') {
danielebarchiesi@2 688 switch ($op) {
danielebarchiesi@2 689 case 'list':
danielebarchiesi@2 690 return array('Math');
danielebarchiesi@2 691 break;
danielebarchiesi@2 692
danielebarchiesi@2 693 case 'generate':
danielebarchiesi@2 694 if ($captcha_type == 'Math') {
danielebarchiesi@2 695 $result = array();
danielebarchiesi@2 696 $answer = mt_rand(1, 20);
danielebarchiesi@2 697 $x = mt_rand(1, $answer);
danielebarchiesi@2 698 $y = $answer - $x;
danielebarchiesi@2 699 $result['solution'] = "$answer";
danielebarchiesi@2 700 // Build challenge widget.
danielebarchiesi@2 701 // Note that we also use t() for the math challenge itself. This makes
danielebarchiesi@2 702 // it possible to 'rephrase' the challenge a bit through localization
danielebarchiesi@2 703 // or string overrides.
danielebarchiesi@2 704 $result['form']['captcha_response'] = array(
danielebarchiesi@2 705 '#type' => 'textfield',
danielebarchiesi@2 706 '#title' => t('Math question'),
danielebarchiesi@2 707 '#description' => t('Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.'),
danielebarchiesi@2 708 '#field_prefix' => t('@x + @y = ', array('@x' => $x, '@y' => $y)),
danielebarchiesi@2 709 '#size' => 4,
danielebarchiesi@2 710 '#maxlength' => 2,
danielebarchiesi@2 711 '#required' => TRUE,
danielebarchiesi@2 712 );
danielebarchiesi@2 713 return $result;
danielebarchiesi@2 714 }
danielebarchiesi@2 715 elseif ($captcha_type == 'Test') {
danielebarchiesi@2 716 // This challenge is not visible through the administrative interface
danielebarchiesi@2 717 // as it is not listed in captcha_captcha('list'),
danielebarchiesi@2 718 // but it is meant for debugging and testing purposes.
danielebarchiesi@2 719 // TODO for Drupal 7 version: This should be done with a mock module,
danielebarchiesi@2 720 // but Drupal 6 does not support this (mock modules can not be hidden).
danielebarchiesi@2 721 $result = array(
danielebarchiesi@2 722 'solution' => 'Test 123',
danielebarchiesi@2 723 'form' => array(),
danielebarchiesi@2 724 );
danielebarchiesi@2 725 $result['form']['captcha_response'] = array(
danielebarchiesi@2 726 '#type' => 'textfield',
danielebarchiesi@2 727 '#title' => t('Test one two three'),
danielebarchiesi@2 728 '#required' => TRUE,
danielebarchiesi@2 729 );
danielebarchiesi@2 730 return $result;
danielebarchiesi@2 731 }
danielebarchiesi@2 732 break;
danielebarchiesi@2 733 }
danielebarchiesi@2 734 }
danielebarchiesi@2 735
danielebarchiesi@2 736 /**
danielebarchiesi@2 737 * Implements hook_modules_enabled.
danielebarchiesi@2 738 */
danielebarchiesi@2 739 function captcha_modules_enabled() {
danielebarchiesi@2 740 // When new modules are enabled: clear the CAPTCHA placement cache, so that
danielebarchiesi@2 741 // new hook_captcha_placement_map hooks can be triggered.
danielebarchiesi@2 742 variable_del('captcha_placement_map_cache');
danielebarchiesi@2 743 }