From a4a7a0e88fc07d06ea955274bcc75e8bf03cb078 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Mar 2015 19:27:20 +0100 Subject: Unify file/cron and mfile->valid_id Create a testable function doing all the verification/removal, add tests and use it for both cases. Signed-off-by: Florian Pritz --- application/service/files.php | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'application/service/files.php') diff --git a/application/service/files.php b/application/service/files.php index e12e636be..1097c5201 100644 --- a/application/service/files.php +++ b/application/service/files.php @@ -281,4 +281,54 @@ class files { "url_id" => $url_id, ); } + + static public function valid_id(array $filedata, array $config, $model, $current_date) + { + assert(isset($filedata["hash"])); + assert(isset($filedata["id"])); + assert(isset($filedata["user"])); + assert(isset($filedata["date"])); + assert(isset($config["upload_max_age"])); + assert(isset($config["sess_expiration"])); + assert(isset($config["small_upload_size"])); + + $file = $model->file($filedata['hash']); + + if (!$model->file_exists($file)) { + $model->delete_hash($filedata["hash"]); + return false; + } + + if ($filedata["user"] == 0) { + if ($filedata["date"] < $current_date - $config["sess_expiration"]) { + $model->delete_id($filedata["id"]); + return false; + } + } + + // 0 age disables age checks + if ($config['upload_max_age'] == 0) return true; + + // small files don't expire + if ($model->filesize($file) <= $config["small_upload_size"]) { + return true; + } + + // files older than this should be removed + $remove_before = $current_date - $config["upload_max_age"]; + + if ($filedata["date"] < $remove_before) { + // if the file has been uploaded multiple times the mtime is the time + // of the last upload + $mtime = $model->filemtime($file); + if ($mtime < $remove_before) { + $model->delete_hash($filedata["hash"]); + } else { + $model->delete_id($filedata["id"]); + } + return false; + } + + return true; + } } -- cgit v1.2.3-24-g4f1b