2016-07-15 11:02:59 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PrivateBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 07:56:52 -04:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2018-08-11 13:29:58 -04:00
|
|
|
* @version 1.2.1
|
2016-07-15 11:02:59 -04:00
|
|
|
*/
|
2016-12-12 12:43:23 -05:00
|
|
|
|
2016-12-12 12:49:08 -05:00
|
|
|
namespace PrivateBin\Persistence;
|
2016-08-09 05:54:42 -04:00
|
|
|
|
|
|
|
use PrivateBin\Configuration;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-07-15 11:02:59 -04:00
|
|
|
/**
|
2016-08-09 05:54:42 -04:00
|
|
|
* PurgeLimiter
|
2016-07-15 11:02:59 -04:00
|
|
|
*
|
2016-08-09 05:54:42 -04:00
|
|
|
* Handles purge limiting, so purging is not triggered too frequently.
|
2016-07-15 11:02:59 -04:00
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
class PurgeLimiter extends AbstractPersistence
|
2016-07-15 11:02:59 -04: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
|
|
|
|
*/
|
|
|
|
public static function setLimit($limit)
|
|
|
|
{
|
|
|
|
self::$_limit = $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set configuration options of the traffic limiter
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2016-08-09 05:54:42 -04:00
|
|
|
* @param Configuration $conf
|
2016-07-15 11:02:59 -04:00
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
public static function setConfiguration(Configuration $conf)
|
2016-07-15 11:02:59 -04: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 02:19:35 -04:00
|
|
|
if (self::$_limit < 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-15 11:02:59 -04:00
|
|
|
|
2018-07-29 10:05:57 -04:00
|
|
|
$now = time();
|
|
|
|
$file = 'purge_limiter.php';
|
|
|
|
if (self::_exists($file)) {
|
|
|
|
require self::getPath($file);
|
|
|
|
$pl = $GLOBALS['purge_limiter'];
|
|
|
|
if ($pl + self::$_limit >= $now) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-15 11:02:59 -04:00
|
|
|
}
|
|
|
|
|
2018-07-29 10:05:57 -04:00
|
|
|
$content = '<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $now . ';';
|
|
|
|
self::_store($file, $content);
|
|
|
|
return true;
|
2016-07-15 11:02:59 -04:00
|
|
|
}
|
|
|
|
}
|