introducing automatic purging of expired pastes, triggered by default at least 5 minutes apart, deleting a maximum of 10 pastes - resolves #3
This commit is contained in:
@@ -70,6 +70,11 @@ class configuration
|
||||
'header' => null,
|
||||
'dir' => 'data',
|
||||
),
|
||||
'purge' => array(
|
||||
'limit' => 300,
|
||||
'batchsize' => 10,
|
||||
'dir' => 'data',
|
||||
),
|
||||
'model' => array(
|
||||
'class' => 'privatebin_data',
|
||||
),
|
||||
|
||||
@@ -35,6 +35,7 @@ class model
|
||||
* Factory constructor.
|
||||
*
|
||||
* @param configuration $conf
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(configuration $conf)
|
||||
{
|
||||
@@ -54,8 +55,24 @@ class model
|
||||
return $paste;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a purge is necessary and triggers it if yes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function purge()
|
||||
{
|
||||
purgelimiter::setConfiguration($this->_conf);
|
||||
if (purgelimiter::canPurge())
|
||||
{
|
||||
$this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets, and creates if neccessary, a store object
|
||||
*
|
||||
* @return privatebin_abstract
|
||||
*/
|
||||
private function _getStore()
|
||||
{
|
||||
|
||||
@@ -264,6 +264,7 @@ class privatebin
|
||||
// The user posts a standard paste.
|
||||
else
|
||||
{
|
||||
$this->_model->purge();
|
||||
$paste = $this->_model->getPaste();
|
||||
try {
|
||||
$paste->setData($data);
|
||||
|
||||
@@ -123,6 +123,35 @@ abstract class privatebin_abstract
|
||||
*/
|
||||
abstract public function existsComment($pasteid, $parentid, $commentid);
|
||||
|
||||
/**
|
||||
* Returns up to batch size number of paste ids that have expired
|
||||
*
|
||||
* @access protected
|
||||
* @param int $batchsize
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function _getExpiredPastes($batchsize);
|
||||
|
||||
/**
|
||||
* Perform a purge of old pastes, at most the given batchsize is deleted.
|
||||
*
|
||||
* @access public
|
||||
* @param int $batchsize
|
||||
* @return void
|
||||
*/
|
||||
public function purge($batchsize)
|
||||
{
|
||||
if ($batchsize < 1) return;
|
||||
$pastes = $this->_getExpiredPastes($batchsize);
|
||||
if (count($pastes))
|
||||
{
|
||||
foreach ($pastes as $pasteid)
|
||||
{
|
||||
$this->delete($pasteid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next free slot for comment from postdate.
|
||||
*
|
||||
|
||||
@@ -210,6 +210,67 @@ class privatebin_data extends privatebin_abstract
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns up to batch size number of paste ids that have expired
|
||||
*
|
||||
* @access private
|
||||
* @param int $batchsize
|
||||
* @return array
|
||||
*/
|
||||
protected function _getExpiredPastes($batchsize)
|
||||
{
|
||||
$pastes = array();
|
||||
$firstLevel = array_filter(
|
||||
scandir(self::$_dir),
|
||||
array('self', '_isFirstLevelDir')
|
||||
);
|
||||
if (count($firstLevel) > 0)
|
||||
{
|
||||
// try at most 10 times the $batchsize pastes before giving up
|
||||
for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i)
|
||||
{
|
||||
$firstKey = array_rand($firstLevel);
|
||||
$secondLevel = array_filter(
|
||||
scandir(self::$_dir . $firstLevel[$firstKey]),
|
||||
array('self', '_isSecondLevelDir')
|
||||
);
|
||||
|
||||
// skip this folder in the next checks if it is empty
|
||||
if (count($secondLevel) == 0)
|
||||
{
|
||||
unset($firstLevel[$firstKey]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$secondKey = array_rand($secondLevel);
|
||||
$path = self::$_dir . $firstLevel[$firstKey] . '/' . $secondLevel[$secondKey];
|
||||
if (!is_dir($path)) continue;
|
||||
$thirdLevel = array_filter(
|
||||
scandir($path),
|
||||
array('model_paste', 'isValidId')
|
||||
);
|
||||
if (count($thirdLevel) == 0) continue;
|
||||
$thirdKey = array_rand($thirdLevel);
|
||||
$pasteid = $thirdLevel[$thirdKey];
|
||||
if (in_array($pasteid, $pastes)) continue;
|
||||
|
||||
if ($this->exists($pasteid))
|
||||
{
|
||||
$data = $this->read($pasteid);
|
||||
if (
|
||||
property_exists($data->meta, 'expire_date') &&
|
||||
$data->meta->expire_date < time()
|
||||
)
|
||||
{
|
||||
$pastes[] = $pasteid;
|
||||
if (count($pastes) >= $batchsize) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $pastes;
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize privatebin
|
||||
*
|
||||
@@ -266,4 +327,30 @@ class privatebin_data extends privatebin_abstract
|
||||
{
|
||||
return self::_dataid2path($dataid) . $dataid . '.discussion/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the given element is a valid first level directory.
|
||||
*
|
||||
* @access private
|
||||
* @static
|
||||
* @param string $element
|
||||
* @return bool
|
||||
*/
|
||||
private static function _isFirstLevelDir($element)
|
||||
{
|
||||
return self::_isSecondLevelDir($element) && is_dir(self::$_dir . '/' . $element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the given element is a valid second level directory.
|
||||
*
|
||||
* @access private
|
||||
* @static
|
||||
* @param string $element
|
||||
* @return bool
|
||||
*/
|
||||
private static function _isSecondLevelDir($element)
|
||||
{
|
||||
return (bool) preg_match('/^[a-f0-9]{2}$/', $element);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ class privatebin_db extends privatebin_abstract
|
||||
* Test if a paste exists.
|
||||
*
|
||||
* @access public
|
||||
* @param string $dataid
|
||||
* @param string $pasteid
|
||||
* @return void
|
||||
*/
|
||||
public function exists($pasteid)
|
||||
@@ -381,7 +381,7 @@ class privatebin_db extends privatebin_abstract
|
||||
* Test if a comment exists.
|
||||
*
|
||||
* @access public
|
||||
* @param string $dataid
|
||||
* @param string $pasteid
|
||||
* @param string $parentid
|
||||
* @param string $commentid
|
||||
* @return void
|
||||
@@ -395,6 +395,30 @@ class privatebin_db extends privatebin_abstract
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns up to batch size number of paste ids that have expired
|
||||
*
|
||||
* @access private
|
||||
* @param int $batchsize
|
||||
* @return array
|
||||
*/
|
||||
protected function _getExpiredPastes($batchsize)
|
||||
{
|
||||
$pastes = array();
|
||||
$rows = self::_select(
|
||||
'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
|
||||
' WHERE expiredate < ? LIMIT ?', array(time(), $batchsize)
|
||||
);
|
||||
if (count($rows))
|
||||
{
|
||||
foreach ($rows as $row)
|
||||
{
|
||||
$pastes[] = $row['dataid'];
|
||||
}
|
||||
}
|
||||
return $pastes;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a statement
|
||||
*
|
||||
|
||||
99
lib/purgelimiter.php
Normal file
99
lib/purgelimiter.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user