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

View File

@@ -215,18 +215,8 @@ class zerobin
$attachmentname = $has_attachmentname ? $_POST['attachmentname'] : '';
// Make sure last paste from the IP address was more than X seconds ago.
trafficlimiter::setLimit($this->_conf->getKey('limit', 'traffic'));
trafficlimiter::setPath($this->_conf->getKey('dir', 'traffic'));
$ipKey = 'REMOTE_ADDR';
if (($option = $this->_conf->getKey('header', 'traffic')) !== null)
{
$header = 'HTTP_' . $option;
if (array_key_exists($header, $_SERVER) && !empty($_SERVER[$header]))
{
$ipKey = $header;
}
}
if (!trafficlimiter::canPass($_SERVER[$ipKey])) return $this->_return_message(
trafficlimiter::setConfiguration($this->_conf);
if (!trafficlimiter::canPass()) return $this->_return_message(
1,
i18n::_(
'Please wait %d seconds between each post.',
@@ -334,7 +324,7 @@ class zerobin
{
$meta['nickname'] = $nick;
$vz = new vizhash16x16();
$pngdata = $vz->generate($_SERVER['REMOTE_ADDR']);
$pngdata = $vz->generate(trafficlimiter::getIp());
if ($pngdata != '')
{
$meta['vizhash'] = 'data:image/png;base64,' . base64_encode($pngdata);