You are here

function _honeypot_time_restriction_validate in Honeypot 7

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

Validate honeypot's time restriction field.

1 string reference to '_honeypot_time_restriction_validate'
honeypot_add_form_protection in ./honeypot.module
Form builder function to add different types of protection to forms.

File

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

Code

function _honeypot_time_restriction_validate(&$element, &$form_state) {
  if (!empty($form_state['programmed'])) {

    // Don't do anything if the form was submitted programmatically.
    return;
  }

  // Don't do anything if the triggering element is a preview button.
  if ($form_state['triggering_element']['#value'] == t('Preview')) {
    return;
  }
  if ($form_state['values']['honeypot_time'] == 'no_js_available') {

    // Set an error, but do not penalize the user as it might be a legitimate
    // attempt.
    form_set_error('', t('You seem to have javascript disabled. Please confirm your form submission.'));
    if (variable_get('honeypot_log', 0)) {
      $variables = array(
        '%form' => $form_state['values']['form_id'],
      );
      watchdog('honeypot', 'User tried to submit form %form without javascript enabled.', $variables);
    }

    // Update the value in $form_state and $element.
    $form_state['values']['honeypot_time'] = honeypot_get_signed_timestamp(REQUEST_TIME);
    $element['#value'] = $form_state['values']['honeypot_time'];
    return;
  }
  $honeypot_time = FALSE;

  // Update the honeypot_time for JS requests and get the $honeypot_time value.
  if (strpos($form_state['values']['honeypot_time'], 'js_token:') === 0) {
    $interval = _honeypot_get_interval_from_signed_js_value($form_state['values']['honeypot_time']);
    if ($interval) {

      // Set correct value for timestamp validation.
      $honeypot_time = REQUEST_TIME - $interval;

      // Update form_state and element values so they're correct.
      $form_state['values']['honeypot_time'] = honeypot_get_signed_timestamp($honeypot_time);
      $element['#value'] = $form_state['values']['honeypot_time'];
    }
  }
  else {

    // Get the time value.
    $honeypot_time = honeypot_get_time_from_signed_timestamp($form_state['values']['honeypot_time']);
  }

  // Get the honeypot_time_limit.
  $time_limit = honeypot_get_time_limit($form_state['values']);

  // Make sure current time - (time_limit + form time value) is greater than 0.
  // If not, throw an error.
  if (!$honeypot_time || REQUEST_TIME < $honeypot_time + $time_limit) {
    _honeypot_log($form_state['values']['form_id'], 'honeypot_time');

    // Get the time limit again, since it increases after first failure.
    $time_limit = honeypot_get_time_limit($form_state['values']);

    // Update the honeypot_time value in the form state and element.
    $form_state['values']['honeypot_time'] = honeypot_get_signed_timestamp(REQUEST_TIME);
    $element['#value'] = $form_state['values']['honeypot_time'];
    form_set_error('', t('There was a problem with your form submission. Please wait @limit seconds and try again.', array(
      '@limit' => $time_limit,
    )));
  }
}