You are here

function _captcha_required_for_user in CAPTCHA 8

Same name and namespace in other branches
  1. 6.2 captcha.inc \_captcha_required_for_user()
  2. 7 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::preRenderProcess in src/Element/Captcha.php
Pre-render callback for additional processing of a CAPTCHA form element.
Captcha::processCaptchaElement in src/Element/Captcha.php
Process callback for CAPTCHA form element.

File

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

Code

function _captcha_required_for_user($captcha_sid, $form_id) {

  // Get the CAPTCHA persistence setting.
  $captcha_persistence = \Drupal::config('captcha.settings')
    ->get('persistence');

  // 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 = \Drupal::database()
    ->select('captcha_sessions', 'cs')
    ->fields('cs', [
    'status',
  ])
    ->condition('csid', $captcha_sid)
    ->execute()
    ->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'] : [];
    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;
}