preparing unit test for model refactoring, refactoring traffic limiter

This commit is contained in:
El RIDO
2015-09-26 17:57:46 +02:00
parent d04eab52c9
commit 211d3e4622
4 changed files with 213 additions and 22 deletions

View File

@@ -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))