2016-07-15 15:02:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PrivateBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 11:56:52 +00:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2021-04-05 14:44:12 +00:00
|
|
|
* @version 1.3.5
|
2016-07-15 15:02:59 +00:00
|
|
|
*/
|
2016-12-12 17:43:23 +00:00
|
|
|
|
2016-12-12 17:49:08 +00:00
|
|
|
namespace PrivateBin\Persistence;
|
2016-08-09 09:54:42 +00:00
|
|
|
|
|
|
|
use PrivateBin\Configuration;
|
2016-07-21 15:09:48 +00:00
|
|
|
|
2016-07-15 15:02:59 +00:00
|
|
|
/**
|
2016-08-09 09:54:42 +00:00
|
|
|
* PurgeLimiter
|
2016-07-15 15:02:59 +00:00
|
|
|
*
|
2016-08-09 09:54:42 +00:00
|
|
|
* Handles purge limiting, so purging is not triggered too frequently.
|
2016-07-15 15:02:59 +00:00
|
|
|
*/
|
2016-08-09 09:54:42 +00:00
|
|
|
class PurgeLimiter extends AbstractPersistence
|
2016-07-15 15:02:59 +00: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 09:54:42 +00:00
|
|
|
* @param Configuration $conf
|
2016-07-15 15:02:59 +00:00
|
|
|
*/
|
2016-08-09 09:54:42 +00:00
|
|
|
public static function setConfiguration(Configuration $conf)
|
2016-07-15 15:02:59 +00:00
|
|
|
{
|
|
|
|
self::setLimit($conf->getKey('limit', 'purge'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if the purge can be performed
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function canPurge()
|
|
|
|
{
|
|
|
|
// disable limits if set to less then 1
|
2016-07-26 06:19:35 +00:00
|
|
|
if (self::$_limit < 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-15 15:02:59 +00:00
|
|
|
|
2018-07-29 14:05:57 +00:00
|
|
|
$now = time();
|
2021-06-08 04:37:27 +00:00
|
|
|
$pl = (int) self::$_store->getValue('purge_limiter');
|
2021-06-07 19:53:42 +00:00
|
|
|
if ($pl + self::$_limit >= $now) {
|
|
|
|
return false;
|
2016-07-15 15:02:59 +00:00
|
|
|
}
|
2021-06-16 03:32:45 +00:00
|
|
|
$hasStored = self::$_store->setValue((string) $now, 'purge_limiter');
|
|
|
|
if (!$hasStored) {
|
|
|
|
error_log('failed to store the purge limiter, skipping purge cycle to avoid getting stuck in a purge loop');
|
|
|
|
}
|
|
|
|
return $hasStored;
|
2016-07-15 15:02:59 +00:00
|
|
|
}
|
|
|
|
}
|