2016-07-15 17:02:59 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PrivateBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 13:56:52 +02:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2016-08-25 09:53:31 +02:00
|
|
|
* @version 1.0
|
2016-07-15 17:02:59 +02:00
|
|
|
*/
|
2016-12-12 18:43:23 +01:00
|
|
|
|
|
|
|
namespace PrivateBin;\Persistence;
|
2016-08-09 11:54:42 +02:00
|
|
|
|
|
|
|
use PrivateBin\Configuration;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2016-07-15 17:02:59 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* PurgeLimiter
|
2016-07-15 17:02:59 +02:00
|
|
|
*
|
2016-08-09 11:54:42 +02:00
|
|
|
* Handles purge limiting, so purging is not triggered too frequently.
|
2016-07-15 17:02:59 +02:00
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class PurgeLimiter extends AbstractPersistence
|
2016-07-15 17:02:59 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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
|
2016-08-09 11:54:42 +02:00
|
|
|
* @param Configuration $conf
|
2016-07-15 17:02:59 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
public static function setConfiguration(Configuration $conf)
|
2016-07-15 17:02:59 +02:00
|
|
|
{
|
|
|
|
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
|
2016-07-26 08:19:35 +02:00
|
|
|
if (self::$_limit < 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-15 17:02:59 +02:00
|
|
|
|
2016-08-15 16:45:47 +02:00
|
|
|
$file = 'purge_limiter.php';
|
|
|
|
$now = time();
|
2016-08-09 12:21:32 +02:00
|
|
|
$content = '<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL;
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!self::_exists($file)) {
|
2016-08-09 12:21:32 +02:00
|
|
|
self::_store($file, $content);
|
2016-07-15 17:02:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$path = self::getPath($file);
|
|
|
|
require $path;
|
|
|
|
$pl = $GLOBALS['purge_limiter'];
|
|
|
|
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($pl + self::$_limit >= $now) {
|
2016-07-15 17:02:59 +02:00
|
|
|
$result = false;
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2016-07-15 17:02:59 +02:00
|
|
|
$result = true;
|
2016-08-09 12:21:32 +02:00
|
|
|
self::_store($file, $content);
|
2016-07-15 17:02:59 +02:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|