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:
El RIDO 2016-07-15 17:02:59 +02:00
parent 4d10fd9690
commit f8bc40b4e4
13 changed files with 404 additions and 12 deletions

View file

@ -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);
}
}