From d7efd2ca0f3dfac2d4f8bc0d7451fdbbb7fe054a Mon Sep 17 00:00:00 2001 From: Banana Date: Sun, 22 Dec 2019 16:47:54 +0100 Subject: [PATCH] lifetime of a paste --- CHANGELOG | 2 ++ TODO | 5 +---- documentation/lifetime.txt | 3 +++ webroot/config.default.php | 4 ++++ webroot/lib/mancubus.class.php | 23 ++++++++++++++++++++++- 5 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 documentation/lifetime.txt diff --git a/CHANGELOG b/CHANGELOG index 50a6da4..2f6db08 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ TBA version 0.2 Helium * Using https://www.jwz.org/base64-shortlinks/ more correctly * Simple flood protection + * Lifetime for each paste. + * Deletion of paste older than the lifetime 20191221 version 0.1 Hydrogen diff --git a/TODO b/TODO index 97f40df..0f6dee6 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,6 @@ * Documentation -* lifetime -* deletion of old ones * add force download parameter * HTTPS for selfpaste bash client * multiple secrets * extending allowed filetypes -* creation or even access with basic auth -* flood protection \ No newline at end of file +* creation or even access with basic auth \ No newline at end of file diff --git a/documentation/lifetime.txt b/documentation/lifetime.txt new file mode 100644 index 0000000..f010fe2 --- /dev/null +++ b/documentation/lifetime.txt @@ -0,0 +1,3 @@ +The default lifetime of a paste is 7 days. Anything older will be deleted. + +The default flood prevention time maximum is 30 sec. \ No newline at end of file diff --git a/webroot/config.default.php b/webroot/config.default.php index bfd7566..16a9508 100644 --- a/webroot/config.default.php +++ b/webroot/config.default.php @@ -23,3 +23,7 @@ define('SELFPASTE_ALLOWED_FILETYPES','text/plain,text/comma-separated-values,tex # needed to respond with the correct link for your paste # please NO / at the end define('SELFPASTE_URL','http://your.tld/path/selfpaste/webroot'); +# time in seconds how long a paste will be available. Default 7 days = 604800 sec +define('SELFPASTE_PASTE_LIFETIME',604800); +# time in seconds how long the flood protection should take action. Default 30sec +define('SELFPASTE_FLOOD_LIFETIME',30); diff --git a/webroot/lib/mancubus.class.php b/webroot/lib/mancubus.class.php index c14e6e7..861a866 100644 --- a/webroot/lib/mancubus.class.php +++ b/webroot/lib/mancubus.class.php @@ -124,6 +124,8 @@ class Mancubus { $ret = $this->_checkAllowedFiletype(); $ret = $this->_checkStorage(); $ret = $this->_moveUploadedFile(); + + $this->_checkLifetime(); } catch (Exception $e) { $ret['message'] = $e->getMessage(); @@ -294,9 +296,28 @@ class Mancubus { $now = time(); foreach ($iterator as $file) { if($file->isDot() || $file->isDir() || Summoner::startsWith($file->getFilename(),'.')) continue; - if ($now - $file->getCTime() >= 30) { // 30 sec + if ($now - $file->getCTime() >= SELFPASTE_FLOOD_LIFETIME) { unlink(SELFPASTE_UPLOAD_DIR.'/'.$file->getFilename()); } } } + + /** + * delete all pastes older than SELFPASTE_PASTE_LIFETIME + */ + private function _checkLifetime() { + $iterator = new RecursiveDirectoryIterator(SELFPASTE_UPLOAD_DIR); + $now = time(); + + foreach (new RecursiveIteratorIterator($iterator) as $file) { + $fname = $file->getFilename(); + if($file->isDir() + || Summoner::startsWith($file->getFilename(),'.') + || isset($fname[4]) + ) continue; + if ($now - $file->getCTime() >= SELFPASTE_PASTE_LIFETIME) { + unlink(SELFPASTE_UPLOAD_DIR.'/'.$fname); + } + } + } } \ No newline at end of file -- 2.39.5