preparing unit test for model refactoring, refactoring traffic limiter
This commit is contained in:
@@ -26,6 +26,15 @@ class trafficlimiter extends persistence
|
||||
*/
|
||||
private static $_limit = 10;
|
||||
|
||||
/**
|
||||
* key to fetch IP address
|
||||
*
|
||||
* @access private
|
||||
* @static
|
||||
* @var string
|
||||
*/
|
||||
private static $_ipKey = 'REMOTE_ADDR';
|
||||
|
||||
/**
|
||||
* set the time limit in seconds
|
||||
*
|
||||
@@ -39,6 +48,40 @@ class trafficlimiter extends persistence
|
||||
self::$_limit = $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* set configuration options of the traffic limiter
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param configuration $conf
|
||||
* @return void
|
||||
*/
|
||||
public static function setConfiguration(configuration $conf)
|
||||
{
|
||||
self::setLimit($conf->getKey('limit', 'traffic'));
|
||||
self::setPath($conf->getKey('dir', 'traffic'));
|
||||
if (($option = $conf->getKey('header', 'traffic')) !== null)
|
||||
{
|
||||
$httpHeader = 'HTTP_' . $option;
|
||||
if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader]))
|
||||
{
|
||||
self::$_ipKey = $httpHeader;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current visitors IP address
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @return string
|
||||
*/
|
||||
public static function getIp()
|
||||
{
|
||||
return $_SERVER[self::$_ipKey];
|
||||
}
|
||||
|
||||
/**
|
||||
* traffic limiter
|
||||
*
|
||||
@@ -46,14 +89,15 @@ class trafficlimiter extends persistence
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param string $ip
|
||||
* @throws Exception
|
||||
* @return bool
|
||||
*/
|
||||
public static function canPass($ip)
|
||||
public static function canPass()
|
||||
{
|
||||
// disable limits if set to less then 1
|
||||
if (self::$_limit < 1) return true;
|
||||
$ip = self::getIp();
|
||||
|
||||
// disable limits if set to less then 1
|
||||
if (self::$_limit < 1) return true;
|
||||
|
||||
$file = 'traffic_limiter.php';
|
||||
if (!self::_exists($file))
|
||||
|
||||
Reference in New Issue
Block a user