*************** *** 226,243 **** } else { $form['captcha_description'] = array( - '#type' => 'textfield', - '#title' => t('Captcha description'), - '#description' => t('With this description you can explain the purpose of the captcha challenge to the user.'), - '#default_value' => _captcha_get_description(), - '#maxlength' => 256, - ); } - // field for captcha persistence $form['captcha_persistence'] = array( '#type' => 'checkbox', - '#title' => t('Persistent captchas'), - '#description' => t('If checked, the user will always have to solve a captcha. If not checked, the captcha check for a form will be omitted during the rest of the session once the user has successfully solved a captcha for that form.'), '#default_value' => variable_get('captcha_persistence', TRUE), ); // submit button --- 227,244 ---- } else { $form['captcha_description'] = array( + '#type' => 'textfield', + '#title' => t('CAPTCHA description'), + '#description' => t('With this description you can explain the purpose of the CAPTCHA to the user.'), + '#default_value' => _captcha_get_description(), + '#maxlength' => 256, + ); } + // field for CAPTCHA persistence $form['captcha_persistence'] = array( '#type' => 'checkbox', + '#title' => t('Persistent CAPTCHAs'), + '#description' => t('If checked, the user will always have to solve a CAPTCHA. If not checked, the CAPTCHA check for a form will be omitted during the rest of the session once the user has successfully solved a CAPTCHA for that form.'), '#default_value' => variable_get('captcha_persistence', TRUE), ); // submit button *************** *** 295,322 **** variable_set('captcha_description', $form_values['captcha_description']); } variable_set('captcha_persistence', $form_values['captcha_persistence']); - drupal_set_message(t('Your captcha settings were saved.'), 'status'); } } /** * Implementation of hook_form_alter(). * - * This function adds a captcha to forms for untrusted users if needed and adds - * captcha adminstration links for site adminstrators if this option is enabled. */ function captcha_form_alter($form_id, &$form) { global $user; - if (!user_access('skip captcha challenges')) { - // Visitor does not have permission to skip the captcha challenge - // Do not present captcha if not captcha-persistent and user has already solved a captcha for this form if(!variable_get('captcha_persistence', TRUE) && ($_SESSION['captcha'][$form_id]['success'] === TRUE)) { return; } - // Get captcha type and module for this form. Return if no captcha was set. $result = db_query("SELECT module, type FROM {captcha_points} WHERE form_id = '%s'", $form_id); if (!$result) { return; --- 296,323 ---- variable_set('captcha_description', $form_values['captcha_description']); } variable_set('captcha_persistence', $form_values['captcha_persistence']); + drupal_set_message(t('The CAPTCHA settings were saved.'), 'status'); } } /** * Implementation of hook_form_alter(). * + * This function adds a CAPTCHA to forms for untrusted users if needed and adds + * CAPTCHA adminstration links for site adminstrators if this option is enabled. */ function captcha_form_alter($form_id, &$form) { global $user; + if (!user_access('skip CAPTCHA')) { + // Visitor does not have permission to skip the CAPTCHA + // Do not present CAPTCHA if not CAPTCHA-persistent and user has already solved a CAPTCHA for this form if(!variable_get('captcha_persistence', TRUE) && ($_SESSION['captcha'][$form_id]['success'] === TRUE)) { return; } + // Get CAPTCHA type and module for this form. Return if no CAPTCHA was set. $result = db_query("SELECT module, type FROM {captcha_points} WHERE form_id = '%s'", $form_id); if (!$result) { return; *************** *** 408,423 **** // handle the pre_render functions $form['#pre_render'] = ((array) $form['#pre_render']) + array('captcha_pre_render', 'captcha_pre_render_place_captcha'); - // Add a validation function for the captcha part of the form $form['captcha']['#validate'] = ((array) $form['captcha']['#validate']) + array('captcha_validate' => array()); - // prevent caching of the page with this captcha enabled form global $conf; $conf['cache'] = FALSE; - } - elseif (user_access('administer captcha') && variable_get('captcha_administration_mode', FALSE) && arg(0) != 'admin') { - // For administrators: show captcha info and offer link to configure it $result = db_query("SELECT module, type FROM {captcha_points} WHERE form_id = '%s'", $form_id); if (!$result) { return; --- 409,424 ---- // handle the pre_render functions $form['#pre_render'] = ((array) $form['#pre_render']) + array('captcha_pre_render', 'captcha_pre_render_place_captcha'); + // Add a validation function for the CAPTCHA part of the form $form['captcha']['#validate'] = ((array) $form['captcha']['#validate']) + array('captcha_validate' => array()); + // prevent caching of the page with this CAPTCHA enabled form global $conf; $conf['cache'] = FALSE; + } + elseif (user_access('administer CAPTCHA settings') && variable_get('captcha_administration_mode', FALSE) && arg(0) != 'admin') { + // For administrators: show CAPTCHA info and offer link to configure it $result = db_query("SELECT module, type FROM {captcha_points} WHERE form_id = '%s'", $form_id); if (!$result) { return; *************** *** 457,470 **** $captcha_token = $form_values['#post']['captcha_token']; // Check if captcha_token exists if (!isset($_SESSION['captcha'][$form_id][$captcha_token])) { - form_set_error('captcha_token', t('Invalid captcha token.')); } // Check answer if ($captcha_response === $_SESSION['captcha'][$form_id][$captcha_token]) { $_SESSION['captcha'][$form_id]['success'] = TRUE; } else { - form_set_error('captcha_response', t('The answer you entered for the captcha challenge was not correct.')); // If CAPTCHA was on a login form: stop validating, quit the current request // and forward to the current page (like a reload) to prevent loging in. // We do that because the log in procedure, which happens after --- 458,471 ---- $captcha_token = $form_values['#post']['captcha_token']; // Check if captcha_token exists if (!isset($_SESSION['captcha'][$form_id][$captcha_token])) { + form_set_error('captcha_token', t('Invalid CAPTCHA token.')); } // Check answer if ($captcha_response === $_SESSION['captcha'][$form_id][$captcha_token]) { $_SESSION['captcha'][$form_id]['success'] = TRUE; } else { + form_set_error('captcha_response', t('The answer you entered for the CAPTCHA was not correct.')); // If CAPTCHA was on a login form: stop validating, quit the current request // and forward to the current page (like a reload) to prevent loging in. // We do that because the log in procedure, which happens after *************** *** 486,521 **** /** * Implementation of form #pre_render. * - * The main purpose of this function is to store the solution of the captcha - * challenge in the $_SESSION variable. */ function captcha_pre_render($form_id, &$form) { - // Unset the captcha challenge if non-captcha persistent and the captcha has // already been successfully solved for this form. // This needs to be done in this pre_render phase when previewing for example // nodes and comments before submission. // On submission of such a forms for preview, captcha_form_alter() is called - // *before* the captcha validation function (which sets // $_SESSION['captcha'][$form_id]['success'] to TRUE on a correctly answered - // captcha). After this the form_values are entered in the generated form // and this form is presented with the preview. - // This means that captcha_form_alter() can't know if the captcha was - // correctly answered and consequently adds a captcha challenge to the form. // The pre_render phase happens after the validation phase and makes it - // possible to remove the captcha from the form after all. if (!variable_get('captcha_persistence', TRUE) && ($_SESSION['captcha'][$form_id]['success'] === TRUE)) { unset($form['captcha']); return; } - // count the number of unsolved captcha challenges and flush those if too many // minus 1 is needed because 'success' is also an item of $_SESSION['captcha'][$form_id] if (count($_SESSION['captcha'][$form_id]) - 1 > CAPTCHA_UNSOLVED_CHALLENGES_MAX) { unset($_SESSION['captcha'][$form_id]); - drupal_set_message(t('You can\'t request more than @num captcha challenges without solving them. Your previous captcha challenges were flushed.', array('@num' => CAPTCHA_UNSOLVED_CHALLENGES_MAX))); } - // store the current captcha solution in $_SESSION $captcha_token = $form['captcha']['captcha_token']['#value']; $_SESSION['captcha'][$form_id][$captcha_token] = $form['captcha']['captcha_solution']['#value']; $_SESSION['captcha'][$form_id]['success'] = FALSE; --- 487,522 ---- /** * Implementation of form #pre_render. * + * The main purpose of this function is to store the solution of the CAPTCHA + * in the $_SESSION variable. */ function captcha_pre_render($form_id, &$form) { + // Unset the CAPTCHA if non-CAPTCHA persistent and the CAPTCHA has // already been successfully solved for this form. // This needs to be done in this pre_render phase when previewing for example // nodes and comments before submission. // On submission of such a forms for preview, captcha_form_alter() is called + // *before* the CAPTCHA validation function (which sets // $_SESSION['captcha'][$form_id]['success'] to TRUE on a correctly answered + // CAPTCHA). After this the form_values are entered in the generated form // and this form is presented with the preview. + // This means that captcha_form_alter() can't know if the CAPTCHA was + // correctly answered and consequently adds a CAPTCHA to the form. // The pre_render phase happens after the validation phase and makes it + // possible to remove the CAPTCHA from the form after all. if (!variable_get('captcha_persistence', TRUE) && ($_SESSION['captcha'][$form_id]['success'] === TRUE)) { unset($form['captcha']); return; } + // count the number of unsolved CAPTCHAs and flush those if too many // minus 1 is needed because 'success' is also an item of $_SESSION['captcha'][$form_id] if (count($_SESSION['captcha'][$form_id]) - 1 > CAPTCHA_UNSOLVED_CHALLENGES_MAX) { unset($_SESSION['captcha'][$form_id]); + drupal_set_message(t('You can\'t request more than @num CAPTCHAs without solving them. Your previous CAPTCHA were flushed.', array('@num' => CAPTCHA_UNSOLVED_CHALLENGES_MAX))); } + // store the current CAPTCHA solution in $_SESSION $captcha_token = $form['captcha']['captcha_token']['#value']; $_SESSION['captcha'][$form_id][$captcha_token] = $form['captcha']['captcha_solution']['#value']; $_SESSION['captcha'][$form_id]['success'] = FALSE;