captcha_form_alter

Versions
5
captcha_form_alter($form_id, &$form)
6
captcha_form_alter(&$form, $form_state, $form_id)

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.

Code

contrib/captcha/captcha.module, line 287

<?php
function captcha_form_alter(&$form, $form_state, $form_id) {

  if (arg(0) != 'admin' || variable_get('captcha_allow_on_admin_pages', FALSE)) {

    module_load_include('inc', 'captcha');

    if (!user_access('skip CAPTCHA')) {
      // Visitor does not have permission to skip the CAPTCHA

      // Get CAPTCHA type and module for given form_id.
      $captcha_point = captcha_get_form_id_setting($form_id);
      if ($captcha_point && $captcha_point->type) {
        module_load_include('inc', 'captcha');
        # Build CAPTCHA form element.
        $captcha_element = array(
          '#type' => 'captcha',
          '#captcha_type' => $captcha_point->module .'/'. $captcha_point->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);

      }
    }
    else if (user_access('administer CAPTCHA settings') && variable_get('captcha_administration_mode', FALSE)) {
      $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,
      );
      if ($captcha_point !== NULL && $captcha_point->type) {
        $captcha_element['#title'] = t('CAPTCHA: challenge "@type" enabled', array('@type' => $captcha_point->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->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->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);

    }
  }
}
?>