cleanup of TrafficLimiter #342

This commit is contained in:
El RIDO 2018-07-29 15:43:28 +02:00
parent f9c8441edb
commit 5db3412b69
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
1 changed files with 8 additions and 14 deletions

View File

@ -101,27 +101,22 @@ class TrafficLimiter extends AbstractPersistence
} }
$file = 'traffic_limiter.php'; $file = 'traffic_limiter.php';
if (!self::_exists($file)) { if (self::_exists($file)) {
self::_store( require self::getPath($file);
$file, $tl = $GLOBALS['traffic_limiter'];
'<?php' . PHP_EOL . } else {
'$GLOBALS[\'traffic_limiter\'] = array();' . PHP_EOL $tl = array();
);
} }
$path = self::getPath($file);
require $path;
$now = time();
$tl = $GLOBALS['traffic_limiter'];
// purge file of expired hashes to keep it small // purge file of expired hashes to keep it small
$now = time();
foreach ($tl as $key => $time) { foreach ($tl as $key => $time) {
if ($time + self::$_limit < $now) { if ($time + self::$_limit < $now) {
unset($tl[$key]); unset($tl[$key]);
} }
} }
// this hash is used as an array key, hence a shorter hash is used // this hash is used as an array key, hence a shorter algo is used
$hash = self::getHash('sha256'); $hash = self::getHash('sha256');
if (array_key_exists($hash, $tl) && ($tl[$hash] + self::$_limit >= $now)) { if (array_key_exists($hash, $tl) && ($tl[$hash] + self::$_limit >= $now)) {
$result = false; $result = false;
@ -132,8 +127,7 @@ class TrafficLimiter extends AbstractPersistence
self::_store( self::_store(
$file, $file,
'<?php' . PHP_EOL . '<?php' . PHP_EOL .
'$GLOBALS[\'traffic_limiter\'] = ' . '$GLOBALS[\'traffic_limiter\'] = ' . var_export($tl, true) . ';'
var_export($tl, true) . ';' . PHP_EOL
); );
return $result; return $result;
} }