You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.2 KiB
102 lines
2.2 KiB
![]()
8 years ago
|
<?php
|
||
|
/**
|
||
|
* PrivateBin
|
||
|
*
|
||
|
* a zero-knowledge paste bin
|
||
|
*
|
||
|
* @link https://github.com/PrivateBin/PrivateBin
|
||
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
||
![]()
8 years ago
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
||
![]()
8 years ago
|
* @version 0.22
|
||
|
*/
|
||
|
|
||
![]()
8 years ago
|
namespace PrivateBin\Persistence;
|
||
|
|
||
|
use PrivateBin\Configuration;
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
/**
|
||
![]()
8 years ago
|
* PurgeLimiter
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* Handles purge limiting, so purging is not triggered too frequently.
|
||
![]()
8 years ago
|
*/
|
||
![]()
8 years ago
|
class PurgeLimiter extends AbstractPersistence
|
||
![]()
8 years ago
|
{
|
||
|
/**
|
||
|
* time limit in seconds, defaults to 300s
|
||
|
*
|
||
|
* @access private
|
||
|
* @static
|
||
|
* @var int
|
||
|
*/
|
||
|
private static $_limit = 300;
|
||
|
|
||
|
/**
|
||
|
* set the time limit in seconds
|
||
|
*
|
||
|
* @access public
|
||
|
* @static
|
||
|
* @param int $limit
|
||
|
* @return void
|
||
|
*/
|
||
|
public static function setLimit($limit)
|
||
|
{
|
||
|
self::$_limit = $limit;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* set configuration options of the traffic limiter
|
||
|
*
|
||
|
* @access public
|
||
|
* @static
|
||
![]()
8 years ago
|
* @param Configuration $conf
|
||
![]()
8 years ago
|
* @return void
|
||
|
*/
|
||
![]()
8 years ago
|
public static function setConfiguration(Configuration $conf)
|
||
![]()
8 years ago
|
{
|
||
|
self::setLimit($conf->getKey('limit', 'purge'));
|
||
|
self::setPath($conf->getKey('dir', 'purge'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* check if the purge can be performed
|
||
|
*
|
||
|
* @access public
|
||
|
* @static
|
||
|
* @throws Exception
|
||
|
* @return bool
|
||
|
*/
|
||
|
public static function canPurge()
|
||
|
{
|
||
|
// disable limits if set to less then 1
|
||
![]()
8 years ago
|
if (self::$_limit < 1) {
|
||
|
return true;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
$file = 'purge_limiter.php';
|
||
|
$now = time();
|
||
![]()
8 years ago
|
if (!self::_exists($file)) {
|
||
![]()
8 years ago
|
self::_store(
|
||
|
$file,
|
||
|
'<?php' . PHP_EOL .
|
||
|
'$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
|
||
|
);
|
||
|
}
|
||
|
|
||
|
$path = self::getPath($file);
|
||
|
require $path;
|
||
|
$pl = $GLOBALS['purge_limiter'];
|
||
|
|
||
![]()
8 years ago
|
if ($pl + self::$_limit >= $now) {
|
||
![]()
8 years ago
|
$result = false;
|
||
![]()
8 years ago
|
} else {
|
||
![]()
8 years ago
|
$result = true;
|
||
|
self::_store(
|
||
|
$file,
|
||
|
'<?php' . PHP_EOL .
|
||
|
'$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
|
||
|
);
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
}
|