You are here

function honeypot_form_alter in Honeypot 7

Same name and namespace in other branches
  1. 8 honeypot.module \honeypot_form_alter()
  2. 6 honeypot.module \honeypot_form_alter()
  3. 2.0.x honeypot.module \honeypot_form_alter()

Implements hook_form_alter().

Add Honeypot features to forms enabled in the Honeypot admin interface.

File

./honeypot.module, line 62
Honeypot module, for deterring spam bots from completing Drupal forms.

Code

function honeypot_form_alter(&$form, &$form_state, $form_id) {

  // Don't use for maintenance mode forms (install, update, etc.).
  if (defined('MAINTENANCE_MODE')) {
    return;
  }
  $unprotected_forms = array(
    'user_login',
    'user_login_block',
    'search_form',
    'search_block_form',
    'views_exposed_form',
    'honeypot_admin_form',
  );

  // If configured to protect all forms, add protection to every form.
  if (variable_get('honeypot_protect_all_forms', 0) && !in_array($form_id, $unprotected_forms)) {

    // Don't protect system forms - only admins should have access, and system
    // forms may be programmatically submitted by drush and other modules.
    if (preg_match('/[^a-zA-Z]system_/', $form_id) === 0 && preg_match('/[^a-zA-Z]search_/', $form_id) === 0 && preg_match('/[^a-zA-Z]views_exposed_form_/', $form_id) === 0) {
      honeypot_add_form_protection($form, $form_state, array(
        'honeypot',
        'time_restriction',
      ));
    }
  }
  elseif ($forms_to_protect = honeypot_get_protected_forms()) {
    foreach ($forms_to_protect as $protect_form_id) {

      // For most forms, do a straight check on the form ID.
      if ($form_id == $protect_form_id) {
        honeypot_add_form_protection($form, $form_state, array(
          'honeypot',
          'time_restriction',
        ));
      }
      elseif ($protect_form_id == 'webforms' && strpos($form_id, 'webform_client_form') !== FALSE) {
        honeypot_add_form_protection($form, $form_state, array(
          'honeypot',
          'time_restriction',
        ));
      }
    }
  }
}