You are here

function _honeypot_time_restriction_validate in Honeypot 2.0.x

Same name and namespace in other branches
  1. 8 honeypot.module \_honeypot_time_restriction_validate()
  2. 6 honeypot.module \_honeypot_time_restriction_validate()
  3. 7 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 219
Honeypot module, for deterring spam bots from completing Drupal forms.

Code

function _honeypot_time_restriction_validate($element, FormStateInterface $form_state) {
  if ($form_state
    ->isProgrammed()) {

    // Don't do anything if the form was submitted programmatically.
    return;
  }
  $triggering_element = $form_state
    ->getTriggeringElement();

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

  // Get the time value.
  $identifier = $form_state
    ->getValue('honeypot_time', FALSE);
  $honeypot_time = \Drupal::service('keyvalue.expirable')
    ->get('honeypot_time_restriction')
    ->get($identifier, 0);

  // Get the honeypot_time_limit.
  $time_limit = honeypot_get_time_limit($form_state
    ->getValues());

  // Make sure current time - (time_limit + form time value) is greater than 0.
  // If not, throw an error.
  if (!$honeypot_time || \Drupal::time()
    ->getRequestTime() < $honeypot_time + $time_limit) {
    _honeypot_log($form_state
      ->getValue('form_id'), 'honeypot_time');
    $time_limit = honeypot_get_time_limit();
    \Drupal::service('keyvalue.expirable')
      ->get('honeypot_time_restriction')
      ->setWithExpire($identifier, \Drupal::time()
      ->getRequestTime(), 3600 * 24);
    $form_state
      ->setErrorByName('', t('There was a problem with your form submission. Please wait @limit seconds and try again.', [
      '@limit' => $time_limit,
    ]));
  }
}