You are here

function _webform_submission_limit_check in Webform 5.2

Same name and namespace in other branches
  1. 5 webform.inc \_webform_submission_limit_check()
  2. 6.2 webform_submissions.inc \_webform_submission_limit_check()

Check if the current user has exceeded the limit on this form.

Parameters

$node: The webform node to be checked.

Return value

Boolean TRUE if the user has exceeded their limit. FALSE otherwise.

2 calls to _webform_submission_limit_check()
webform_client_form_validate in ./webform.module
webform_view in ./webform.module
Implementation of hook_view().

File

./webform_submissions.inc, line 279
This file is loaded when handling submissions, either submitting new, editing, or viewing. It also contains all CRUD functions for submissions.

Code

function _webform_submission_limit_check($node) {
  global $user;

  // Check if submission limiting is enabled.
  if ($node->webform['submit_limit'] == '-1') {
    return FALSE;

    // No check enabled.
  }

  // Retrieve submission data for this IP address or username from the database.
  $query = 'SELECT submitted, uid, remote_addr ' . 'FROM {webform_submissions} ' . "WHERE (( 0 = %d AND remote_addr = '%s') OR (uid > 0 AND uid = %d)) " . 'AND submitted > %d AND nid = %d';

  // Fetch all the entries from the database within the submit interval with this username and IP.
  $ip_address = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
  $result = db_query($query, $user->uid, $ip_address, $user->uid, $node->webform['submit_interval'] != -1 ? time() - $node->webform['submit_interval'] : $node->webform['submit_interval'], $node->nid);
  $num_submissions_database = db_num_rows($result);

  // Double check the submission history from the users machine using cookies.
  $num_submissions_cookie = 0;
  if ($user->uid == 0 && variable_get('webform_use_cookies', 0)) {
    $cookie_name = 'webform-' . $node->nid;
    if (isset($_COOKIE[$cookie_name]) && is_array($_COOKIE[$cookie_name])) {
      foreach ($_COOKIE[$cookie_name] as $key => $timestamp) {
        if ($node->webform['submit_interval'] != -1 && $timestamp <= time() - $node->webform['submit_interval']) {

          // Remove the cookie if past the required time interval.
          setcookie($cookie_name . '[' . $key . ']', '', 0);
        }
      }

      // Count the number of submissions recorded in cookies.
      $num_submissions_cookie = count($_COOKIE[$cookie_name]);
    }
    else {
      $num_submissions_cookie = 0;
    }
  }
  if ($num_submissions_database >= $node->webform['submit_limit'] || $num_submissions_cookie >= $node->webform['submit_limit']) {

    // Limit exceeded.
    return TRUE;
  }

  // Limit not exceeded.
  return FALSE;
}