diff options
author | Florian Pritz <bluewind@xinu.at> | 2015-03-08 19:27:20 +0100 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2015-03-08 19:27:20 +0100 |
commit | a4a7a0e88fc07d06ea955274bcc75e8bf03cb078 (patch) | |
tree | ec4cdae4a194538dd2aa7cf516d96611abc75d20 /application/service/files.php | |
parent | f4f108a41281037370eaec6c7b8fb3e6e35a30f5 (diff) |
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 <bluewind@xinu.at>
Diffstat (limited to 'application/service/files.php')
-rw-r--r-- | application/service/files.php | 50 |
1 files changed, 50 insertions, 0 deletions
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; + } } |