You are here

function honeypot_add_form_protection in Honeypot 7

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

Form builder function to add different types of protection to forms.

Parameters

array $options: Array of options to be added to form. Currently accepts 'honeypot' and 'time_restriction'.

Return value

array Returns elements to be placed in a form's elements array to prevent spam.

1 call to honeypot_add_form_protection()
honeypot_form_alter in ./honeypot.module
Implements hook_form_alter().

File

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

Code

function honeypot_add_form_protection(&$form, &$form_state, $options = array()) {
  global $user;

  // Allow other modules to alter the protections applied to this form.
  drupal_alter('honeypot_form_protections', $options, $form);

  // Don't add any protections if the user can bypass the Honeypot.
  if (user_access('bypass honeypot protection')) {
    return;
  }

  // Build the honeypot element.
  if (in_array('honeypot', $options)) {

    // Get the element name (default is generic 'url').
    $honeypot_element = variable_get('honeypot_element_name', 'url');

    // Add 'autocomplete="off"' if configured.
    $attributes = array();
    if (variable_get('honeypot_autocomplete_attribute', 1)) {
      $attributes = array(
        'autocomplete' => 'off',
      );
    }

    // Get the path to the honeypot css file.
    $honeypot_css = honeypot_get_css_file_path();

    // Build the honeypot element.
    $honeypot_class = $honeypot_element . '-textfield';
    $form[$honeypot_element] = array(
      '#type' => 'textfield',
      '#title' => t('Leave this field blank'),
      '#size' => 20,
      '#weight' => 100,
      '#attributes' => $attributes,
      '#element_validate' => array(
        '_honeypot_honeypot_validate',
      ),
      '#prefix' => '<div class="' . $honeypot_class . '">',
      '#suffix' => '</div>',
      // Hide honeypot using CSS.
      '#attached' => array(
        'css' => array(
          'data' => $honeypot_css,
        ),
      ),
    );
  }

  // Build the time restriction element (if it's not disabled).
  if (in_array('time_restriction', $options) && variable_get('honeypot_time_limit', 5) != 0) {

    // Set the current time in a hidden value to be checked later.
    $form['honeypot_time'] = array(
      '#type' => 'hidden',
      '#title' => t('Timestamp'),
      '#default_value' => honeypot_get_signed_timestamp(REQUEST_TIME),
      '#element_validate' => array(
        '_honeypot_time_restriction_validate',
      ),
    );

    // Disable page caching to make sure timestamp isn't cached.
    if (user_is_anonymous() && drupal_page_is_cacheable()) {

      // Use javascript implementation if this page should be cached.
      if (variable_get('honeypot_use_js_for_cached_pages', FALSE)) {
        $form['honeypot_time']['#default_value'] = 'no_js_available';
        $form['honeypot_time']['#attached']['library'][] = array(
          'honeypot',
          'timestamp.js',
        );
        $form['#attributes']['class'][] = 'honeypot-timestamp-js';
      }
      else {
        drupal_page_is_cacheable(FALSE);
      }
    }
  }

  // Allow other modules to react to addition of form protection.
  if (!empty($options)) {
    module_invoke_all('honeypot_add_form_protection', $options, $form);
  }
}