You are here

function captcha_form_alter in CAPTCHA 6.2

Same name and namespace in other branches
  1. 8 captcha.module \captcha_form_alter()
  2. 5.3 captcha.module \captcha_form_alter()
  3. 6 captcha.module \captcha_form_alter()
  4. 7 captcha.module \captcha_form_alter()

Implementation of hook_form_alter().

This function adds a CAPTCHA to forms for untrusted users if needed and adds CAPTCHA administration links for site administrators if this option is enabled.

File

./captcha.module, line 359
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_form_alter(&$form, $form_state, $form_id) {
  if (!user_access('skip CAPTCHA')) {

    // Visitor does not have permission to skip CAPTCHAs.
    module_load_include('inc', 'captcha');

    // Get CAPTCHA type and module for given form_id.
    $captcha_point = captcha_get_form_id_setting($form_id);
    if ($captcha_point && !empty($captcha_point->captcha_type)) {
      module_load_include('inc', 'captcha');

      // Build CAPTCHA form element.
      $captcha_element = array(
        '#type' => 'captcha',
        '#captcha_type' => $captcha_point->module . '/' . $captcha_point->captcha_type,
      );

      // Add a CAPTCHA description if required.
      if (variable_get('captcha_add_captcha_description', TRUE)) {
        $captcha_element['#description'] = _captcha_get_description();
      }

      // Get placement in form and insert in form.
      $captcha_placement = _captcha_get_captcha_placement($form_id, $form);
      _captcha_insert_captcha_element($form, $captcha_placement, $captcha_element);

      // Add #submit functions to invalidate captcha
      $form['#submit'][] = 'captcha_submit_invalidate_session';
    }
  }
  else {
    if (variable_get('captcha_administration_mode', FALSE) && user_access('administer CAPTCHA settings') && (arg(0) != 'admin' || variable_get('captcha_allow_on_admin_pages', FALSE))) {

      // Add CAPTCHA administration tools.
      module_load_include('inc', 'captcha');
      $captcha_point = captcha_get_form_id_setting($form_id);

      // For administrators: show CAPTCHA info and offer link to configure it
      $captcha_element = array(
        '#type' => 'fieldset',
        '#title' => t('CAPTCHA'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#attributes' => array(
          'class' => 'captcha-admin-links',
        ),
      );
      if ($captcha_point !== NULL && $captcha_point->captcha_type) {
        $captcha_element['#title'] = t('CAPTCHA: challenge "@type" enabled', array(
          '@type' => $captcha_point->captcha_type,
        ));
        $captcha_element['#description'] = t('Untrusted users will see a CAPTCHA here (!settings).', array(
          '!settings' => l(t('general CAPTCHA settings'), 'admin/user/captcha'),
        ));
        $captcha_element['challenge'] = array(
          '#type' => 'item',
          '#title' => t('Enabled challenge'),
          '#value' => t('"@type" by module "@module" (!change, !disable)', array(
            '@type' => $captcha_point->captcha_type,
            '@module' => $captcha_point->module,
            '!change' => l(t('change'), "admin/user/captcha/captcha/captcha_point/{$form_id}", array(
              'query' => drupal_get_destination(),
            )),
            '!disable' => l(t('disable'), "admin/user/captcha/captcha/captcha_point/{$form_id}/disable", array(
              'query' => drupal_get_destination(),
            )),
          )),
        );

        // Add an example challenge with solution.
        // This does not work with the reCAPTCHA and Egglue challenges as
        // discussed in http://drupal.org/node/487032 and
        // http://drupal.org/node/525586. As a temporary workaround, we
        // blacklist the reCAPTCHA and Egglue challenges and do not show
        // an example challenge.
        // TODO: Once the issues mentioned above are fixed, this workaround
        // should be removed.
        if ($captcha_point->module != 'recaptcha' && $captcha_point->module != 'egglue_captcha') {
          $captcha_element['example'] = array(
            '#type' => 'fieldset',
            '#title' => t('Example'),
            '#description' => t('This is a pre-solved, non-blocking example of this challenge.'),
          );
          $captcha_element['example']['example_captcha'] = array(
            '#type' => 'captcha',
            '#captcha_type' => $captcha_point->module . '/' . $captcha_point->captcha_type,
            '#captcha_admin_mode' => TRUE,
          );
        }
      }
      else {
        $captcha_element['#title'] = t('CAPTCHA: no challenge enabled');
        $captcha_element['add_captcha'] = array(
          '#value' => l(t('Place a CAPTCHA here for untrusted users.'), "admin/user/captcha/captcha/captcha_point/{$form_id}", array(
            'query' => drupal_get_destination(),
          )),
        );
      }

      // Get placement in form and insert in form.
      $captcha_placement = _captcha_get_captcha_placement($form_id, $form);
      _captcha_insert_captcha_element($form, $captcha_placement, $captcha_element);
    }
  }

  // Add a warning about caching on the Perfomance settings page.
  if ($form_id == 'system_performance_settings') {
    $form['page_cache']['cache']['#description'] .= '<p><strong class="error">' . t('Warning: the CAPTCHA module will disable the caching of pages that contain a CAPTCHA.') . '</strong></p>';
  }
}