You are here

function honeypot_get_time_limit in Honeypot 8

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

Look up the time limit for the current user.

Parameters

array $form_values: Array of form values (optional).

1 call to honeypot_get_time_limit()
_honeypot_time_restriction_validate in ./honeypot.module
Validate honeypot's time restriction field.

File

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

Code

function honeypot_get_time_limit(array $form_values = []) {
  $account = \Drupal::currentUser();
  $honeypot_time_limit = \Drupal::config('honeypot.settings')
    ->get('time_limit');

  // Only calculate time limit if honeypot_time_limit has a value > 0.
  if ($honeypot_time_limit) {
    $expire_time = \Drupal::config('honeypot.settings')
      ->get('expire');

    // Query the {honeypot_user} table to determine the number of failed
    // submissions for the current user.
    $uid = $account
      ->id();
    $query = \Drupal::database()
      ->select('honeypot_user', 'hu')
      ->condition('uid', $uid)
      ->condition('timestamp', \Drupal::time()
      ->getRequestTime() - $expire_time, '>');

    // For anonymous users, take the hostname into account.
    if ($uid === 0) {
      $hostname = \Drupal::request()
        ->getClientIp();
      $query
        ->condition('hostname', $hostname);
    }
    $number = $query
      ->countQuery()
      ->execute()
      ->fetchField();

    // Don't add more than 30 days' worth of extra time.
    $honeypot_time_limit = (int) min($honeypot_time_limit + exp($number) - 1, $expire_time);

    // TODO - Only accepts two args.
    $additions = \Drupal::moduleHandler()
      ->invokeAll('honeypot_time_limit', [
      $honeypot_time_limit,
      $form_values,
      $number,
    ]);
    if (count($additions)) {
      $honeypot_time_limit += array_sum($additions);
    }
  }
  return $honeypot_time_limit;
}