You are here

function captcha_pre_render_process in CAPTCHA 7

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

Pre-render callback for additional processing of a CAPTCHA form element.

This encompasses tasks that should happen after the general FAPI processing (building, submission and validation) but before rendering (e.g. storing the solution).

Parameters

array $element: the CAPTCHA form element

Return value

array the manipulated element

1 string reference to 'captcha_pre_render_process'
captcha_element_process in ./captcha.module
Process callback for CAPTCHA form element.

File

./captcha.module, line 795
This module enables basic CAPTCHA functionality: administrators can add a CAPTCHA to desired forms that users without the 'skip CAPTCHA' permission (typically anonymous visitors) have to solve.

Code

function captcha_pre_render_process($element) {
  module_load_include('inc', 'captcha');

  // Get form and CAPTCHA information.
  $captcha_info = $element['#captcha_info'];
  $form_id = $captcha_info['form_id'];
  $captcha_sid = (int) $captcha_info['captcha_sid'];

  // Check if CAPTCHA is still required.
  // This check is done in a first phase during the element processing
  // (@see captcha_process), but it is also done here for better support
  // of multi-page forms. Take previewing a node submission for example:
  // when the challenge is solved correctely on preview, the form is still
  // not completely submitted, but the CAPTCHA can be skipped.
  if (_captcha_required_for_user($captcha_sid, $form_id) || $element['#captcha_admin_mode'] || $element['#captcha_always']) {

    // Prevent caching of the page with CAPTCHA elements.
    // This needs to be done only if the captcha is actually displayed.
    // The captcha display will be skipped only in 2 cases:
    //   - The captcha sid has a solution already.
    //   - The user has a SESSION.
    // For a page to be cacheable, the user MUST not have a SESSION.
    // For a SID to be solved already, it must be a POST request as else
    // a new unsolved SID is generated.
    // Therefore it is fine to disable the cache at this late stage here.
    if (empty($captcha_info['cacheable'])) {

      // The cache is only disabled if the captcha itself is not cacheable.
      drupal_page_is_cacheable(FALSE);
    }

    // Update captcha_sessions table: store the solution of the generated CAPTCHA.
    _captcha_update_captcha_session($captcha_sid, $captcha_info['solution']);

    // Handle the response field if it is available and if it is a textfield.
    if (isset($element['captcha_widgets']['captcha_response']['#type']) && $element['captcha_widgets']['captcha_response']['#type'] == 'textfield') {

      // Before rendering: presolve an admin mode challenge or
      // empty the value of the captcha_response form item.
      $value = $element['#captcha_admin_mode'] ? $captcha_info['solution'] : '';
      $element['captcha_widgets']['captcha_response']['#value'] = $value;
    }
  }
  else {

    // Remove CAPTCHA widgets from form.
    unset($element['captcha_widgets']);
  }
  return $element;
}