You are here

function _captcha_required_for_user in CAPTCHA 7

Same name and namespace in other branches
  1. 8 captcha.inc \_captcha_required_for_user()
  2. 6.2 captcha.inc \_captcha_required_for_user()

Helper function for checking if CAPTCHA is required for user.

Based on the CAPTCHA persistence setting, the CAPTCHA session ID and user session info.

2 calls to _captcha_required_for_user()
captcha_element_process in ./captcha.module
Process callback for CAPTCHA form element.
captcha_pre_render_process in ./captcha.module
Pre-render callback for additional processing of a CAPTCHA form element.

File

./captcha.inc, line 204
General CAPTCHA functionality and helper functions.

Code

function _captcha_required_for_user($captcha_sid, $form_id) {

  // Get the CAPTCHA persistence setting.
  $captcha_persistence = variable_get('captcha_persistence', CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE);

  // First check: should we always add a CAPTCHA?
  if ($captcha_persistence == CAPTCHA_PERSISTENCE_SHOW_ALWAYS) {
    return TRUE;
  }

  // Get the status of the current CAPTCHA session.
  $captcha_session_status = db_query('SELECT status FROM {captcha_sessions} WHERE csid = :csid', array(
    ':csid' => $captcha_sid,
  ))
    ->fetchField();

  // Second check: if the current session is already solved: omit further CAPTCHAs.
  if ($captcha_session_status == CAPTCHA_STATUS_SOLVED) {
    return FALSE;
  }

  // Third check: look at the persistence level (per form instance, per form or per user).
  if ($captcha_persistence == CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE) {
    return TRUE;
  }
  else {
    $captcha_success_form_ids = isset($_SESSION['captcha_success_form_ids']) ? (array) $_SESSION['captcha_success_form_ids'] : array();
    switch ($captcha_persistence) {
      case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL:
        return count($captcha_success_form_ids) == 0;
      case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE:
        return !isset($captcha_success_form_ids[$form_id]);
    }
  }

  // We should never get to this point, but to be sure, we return TRUE.
  return TRUE;
}