You are here

function _webform_submission_limit_check in Webform 5

Same name and namespace in other branches
  1. 5.2 webform_submissions.inc \_webform_submission_limit_check()
  2. 6.2 webform_submissions.inc \_webform_submission_limit_check()
1 call to _webform_submission_limit_check()
webform_client_form_validate in ./webform.module

File

./webform.inc, line 261

Code

function _webform_submission_limit_check($node, $form_values) {
  global $user, $db_type;

  // check if submission limiting is enabled.
  if ($node->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 = %d )" . "AND submitted > %d AND nid = %d";

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

  // Double check the submission history from the users machine using cookies.
  if (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 ($timestamp <= time() - $node->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->submit_limit || $num_submissions_cookie >= $node->submit_limit) {

    // Limit exceeded.
    return $num_submissions_database;
  }
  else {

    // Increment a cookie for triple recording of the submission.
    if (variable_get("webform_use_cookies", 0)) {
      $attempted_key = 0;
      if ($num_submissions_cookie > 0) {
        while (array_key_exists($attempted_key, $_COOKIE[$cookie_name])) {
          $attempted_key++;
        }
      }

      // Set a cookie including the server's submission time.
      // The cookie expires in the length of the interval plus a day to compensate for different timezones.
      setcookie($cookie_name . "[" . $attempted_key . "]", time(), time() + $node->submit_interval + 86400);
    }

    // Limit not exceeded.
    return false;
  }
}