diff options
author | Dries Buytaert <dries@buytaert.net> | 2004-11-15 21:17:25 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2004-11-15 21:17:25 +0000 |
commit | 9bf33e5ac8afc192b32e5c5132407d2fd394cecd (patch) | |
tree | 0c81f199aeaa3180db9799a5905e538b6d55fcf2 /includes/common.inc | |
parent | f67c046d402b53b1c63cd680f98c6ded598c0dbc (diff) | |
download | brdo-9bf33e5ac8afc192b32e5c5132407d2fd394cecd.tar.gz brdo-9bf33e5ac8afc192b32e5c5132407d2fd394cecd.tar.bz2 |
- Added generic flood control mechanism to throttle certain operations per hostname (eg. posting comments, requesting passwords, sending e-mails). See flood_register_event() and flood_is_allowed() for details.
Diffstat (limited to 'includes/common.inc')
-rw-r--r-- | includes/common.inc | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/includes/common.inc b/includes/common.inc index 34e96a274..0f8312aeb 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -669,6 +669,33 @@ function valid_input_data($data) { * @} End of "defgroup validation". */ +/** + * Register an event for the current visitor (hostname/IP) to the flood control mechanism. + * + * @param $name + * The name of the event. + */ +function flood_register_event($name) { + db_query("INSERT INTO {flood} (event, hostname, timestamp) VALUES ('%s', '%s', %d)", $name, $_SERVER['REMOTE_ADDR'], time()); +} + +/** + * Check if the current visitor (hostname/IP) is allowed to proceed with the specified event. + * The user is allowed to proceed if he did not trigger the specified event more than + * $threshold times per hour. + * + * @param $name + * The name of the event. + * @param $number + * The maximum number of the specified event per hour (per visitor). + * @return + * True if the user did not exceed the hourly threshold. False otherwise. + */ +function flood_is_allowed($name, $threshold) { + $number = db_num_rows(db_query("SELECT event FROM {flood} WHERE event = '%s' AND hostname = '%s' AND timestamp > %d", $name, $_SERVER['REMOTE_ADDR'], time() - 3600)); + return ($number < $threshold ? TRUE : FALSE); +} + function check_form($text) { return drupal_specialchars($text, ENT_QUOTES); } |