function captcha_form_alter in CAPTCHA 7
Same name and namespace in other branches
- 8 captcha.module \captcha_form_alter()
- 5.3 captcha.module \captcha_form_alter()
- 6.2 captcha.module \captcha_form_alter()
- 6 captcha.module \captcha_form_alter()
Implements 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 397 - 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 has skip CAPTCHA permission, doesn't do anything.
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 no captcha point found, check to see if there is a setting for the base form id
if (!$captcha_point) {
if (isset($form_state['build_info']['base_form_id'])) {
$captcha_point = captcha_get_form_id_setting($form_state['build_info']['base_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);
}
}
// If user has access to administer CAPTCHA settings and
// 'Add CAPTCHA administration links to forms' is enabled, display the
// helper widget.
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' => array(
'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 (<a href="@settings">general CAPTCHA settings</a>).', array(
'@settings' => url('admin/config/people/captcha'),
));
$captcha_element['challenge'] = array(
'#type' => 'item',
'#title' => t('Enabled challenge'),
'#markup' => t('%type by module %module (<a href="@change">change</a>, <a href="@disable">disable</a>)', array(
'%type' => $captcha_point->captcha_type,
'%module' => $captcha_point->module,
'@change' => url("admin/config/people/captcha/captcha/captcha_point/{$form_id}", array(
'query' => drupal_get_destination(),
)),
'@disable' => url("admin/config/people/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(
'#markup' => l(t('Place a CAPTCHA here for untrusted users.'), "admin/config/people/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') {
$icon = theme('image', array(
'path' => 'misc/watchdog-warning.png',
'width' => 18,
'height' => 18,
'alt' => t('warning'),
'title' => t('warning'),
));
$form['caching']['captcha'] = array(
'#type' => 'item',
'#title' => t('CAPTCHA'),
'#markup' => t('!icon The CAPTCHA module will disable the caching of pages that contain a CAPTCHA element.', array(
'!icon' => '<span class="icon">' . $icon . '</span>',
)),
'#attributes' => array(
'class' => array(
'warning',
),
),
);
}
}