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:
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user