2012-04-29 19:15:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ZeroBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-09-03 22:22:59 +02:00
|
|
|
* @version 0.20
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-11-01 01:15:14 +01:00
|
|
|
* trafficlimiter
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* Handles traffic limiting, so no user does more than one call per 10 seconds.
|
|
|
|
*/
|
2013-11-01 01:15:14 +01:00
|
|
|
class trafficlimiter extends persistence
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-08-16 15:55:31 +02:00
|
|
|
* time limit in seconds, defaults to 10s
|
|
|
|
*
|
2012-04-29 19:15:06 +02:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $_limit = 10;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the time limit in seconds
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param int $limit
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setLimit($limit)
|
|
|
|
{
|
|
|
|
self::$_limit = $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* traffic limiter
|
|
|
|
*
|
|
|
|
* Make sure the IP address makes at most 1 request every 10 seconds.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $ip
|
2015-08-27 21:41:21 +02:00
|
|
|
* @throws Exception
|
2012-04-29 19:15:06 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function canPass($ip)
|
|
|
|
{
|
2013-10-30 23:54:42 +01:00
|
|
|
// disable limits if set to less then 1
|
|
|
|
if (self::$_limit < 1) return true;
|
|
|
|
|
2013-11-01 01:15:14 +01:00
|
|
|
$file = 'traffic_limiter.php';
|
|
|
|
if (!self::_exists($file))
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2013-11-01 01:15:14 +01:00
|
|
|
self::_store(
|
2012-04-29 19:15:06 +02:00
|
|
|
$file,
|
|
|
|
'<?php' . PHP_EOL .
|
|
|
|
'$GLOBALS[\'traffic_limiter\'] = array();' . PHP_EOL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-11-01 01:15:14 +01:00
|
|
|
$path = self::getPath($file);
|
|
|
|
require $path;
|
2012-08-26 00:49:11 +02:00
|
|
|
$now = time();
|
2012-04-29 19:15:06 +02:00
|
|
|
$tl = $GLOBALS['traffic_limiter'];
|
|
|
|
|
|
|
|
// purge file of expired IPs to keep it small
|
|
|
|
foreach($tl as $key => $time)
|
|
|
|
{
|
2012-08-26 00:49:11 +02:00
|
|
|
if ($time + self::$_limit < $now)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
|
|
|
unset($tl[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 00:49:11 +02:00
|
|
|
if (array_key_exists($ip, $tl) && ($tl[$ip] + self::$_limit >= $now))
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
|
|
|
$result = false;
|
|
|
|
} else {
|
|
|
|
$tl[$ip] = time();
|
|
|
|
$result = true;
|
|
|
|
}
|
2013-11-01 01:15:14 +01:00
|
|
|
self::_store(
|
2012-04-29 19:15:06 +02:00
|
|
|
$file,
|
|
|
|
'<?php' . PHP_EOL .
|
|
|
|
'$GLOBALS[\'traffic_limiter\'] = ' .
|
|
|
|
var_export($tl, true) . ';' . PHP_EOL
|
|
|
|
);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|