introducing automatic purging of expired pastes, triggered by default at least 5 minutes apart, deleting a maximum of 10 pastes - resolves #3
parent
4d10fd9690
commit
f8bc40b4e4
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* PrivateBin
|
||||
*
|
||||
* a zero-knowledge paste bin
|
||||
*
|
||||
* @link https://github.com/PrivateBin/PrivateBin
|
||||
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
||||
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
||||
* @version 0.22
|
||||
*/
|
||||
|
||||
/**
|
||||
* purgelimiter
|
||||
*
|
||||
* Handles purge limiting, so purging is not triggered to often.
|
||||
*/
|
||||
class purgelimiter extends persistence
|
||||
{
|
||||
/**
|
||||
* 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
|
||||
* @param configuration $conf
|
||||
* @return void
|
||||
*/
|
||||
public static function setConfiguration(configuration $conf)
|
||||
{
|
||||
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
|
||||
if (self::$_limit < 1) return true;
|
||||
|
||||
$file = 'purge_limiter.php';
|
||||
$now = time();
|
||||
if (!self::_exists($file))
|
||||
{
|
||||
self::_store(
|
||||
$file,
|
||||
'<?php' . PHP_EOL .
|
||||
'$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
|
||||
);
|
||||
}
|
||||
|
||||
$path = self::getPath($file);
|
||||
require $path;
|
||||
$pl = $GLOBALS['purge_limiter'];
|
||||
|
||||
if ($pl + self::$_limit >= $now)
|
||||
{
|
||||
$result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = true;
|
||||
self::_store(
|
||||
$file,
|
||||
'<?php' . PHP_EOL .
|
||||
'$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
class purgelimiterTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $_path;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
/* Setup Routine */
|
||||
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
||||
if(!is_dir($this->_path)) mkdir($this->_path);
|
||||
purgelimiter::setPath($this->_path);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
/* Tear Down Routine */
|
||||
helper::rmdir($this->_path);
|
||||
}
|
||||
|
||||
public function testLimit()
|
||||
{
|
||||
// initialize it
|
||||
purgelimiter::canPurge();
|
||||
|
||||
// try setting it
|
||||
purgelimiter::setLimit(1);
|
||||
$this->assertEquals(false, purgelimiter::canPurge());
|
||||
sleep(2);
|
||||
$this->assertEquals(true, purgelimiter::canPurge());
|
||||
|
||||
// disable it
|
||||
purgelimiter::setLimit(0);
|
||||
purgelimiter::canPurge();
|
||||
$this->assertEquals(true, purgelimiter::canPurge());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue