From 00a3005485ef068f9961570191d76a23ebd6fad4 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Fri, 14 Oct 2011 15:50:27 +0200 Subject: [PATCH] MDL-29773 prevent negative unix timestamps in the files table --- lib/filestorage/file_storage.php | 64 ++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index e62def54244..b0fd8a2216d 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -652,6 +652,16 @@ class file_storage { } } + if ($key === 'timecreated' or $key === 'timemodified') { + if (!is_number($value)) { + throw new file_exception('storedfileproblem', 'Invalid file '.$key); + } + if ($value < 0) { + //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak) + $value = 0; + } + } + $newrecord->$key = $value; } @@ -784,6 +794,29 @@ class file_storage { } $now = time(); + if (isset($file_record->timecreated)) { + if (!is_number($file_record->timecreated)) { + throw new file_exception('storedfileproblem', 'Invalid file timecreated'); + } + if ($file_record->timecreated < 0) { + //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak) + $file_record->timecreated = 0; + } + } else { + $file_record->timecreated = $now; + } + + if (isset($file_record->timemodified)) { + if (!is_number($file_record->timemodified)) { + throw new file_exception('storedfileproblem', 'Invalid file timemodified'); + } + if ($file_record->timemodified < 0) { + //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak) + $file_record->timemodified = 0; + } + } else { + $file_record->timemodified = $now; + } $newrecord = new stdClass(); @@ -794,8 +827,8 @@ class file_storage { $newrecord->filepath = $file_record->filepath; $newrecord->filename = $file_record->filename; - $newrecord->timecreated = empty($file_record->timecreated) ? $now : $file_record->timecreated; - $newrecord->timemodified = empty($file_record->timemodified) ? $now : $file_record->timemodified; + $newrecord->timecreated = $file_record->timecreated; + $newrecord->timemodified = $file_record->timemodified; $newrecord->mimetype = empty($file_record->mimetype) ? mimeinfo('type', $file_record->filename) : $file_record->mimetype; $newrecord->userid = empty($file_record->userid) ? null : $file_record->userid; $newrecord->source = empty($file_record->source) ? null : $file_record->source; @@ -873,6 +906,29 @@ class file_storage { } $now = time(); + if (isset($file_record->timecreated)) { + if (!is_number($file_record->timecreated)) { + throw new file_exception('storedfileproblem', 'Invalid file timecreated'); + } + if ($file_record->timecreated < 0) { + //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak) + $file_record->timecreated = 0; + } + } else { + $file_record->timecreated = $now; + } + + if (isset($file_record->timemodified)) { + if (!is_number($file_record->timemodified)) { + throw new file_exception('storedfileproblem', 'Invalid file timemodified'); + } + if ($file_record->timemodified < 0) { + //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak) + $file_record->timemodified = 0; + } + } else { + $file_record->timemodified = $now; + } $newrecord = new stdClass(); @@ -883,8 +939,8 @@ class file_storage { $newrecord->filepath = $file_record->filepath; $newrecord->filename = $file_record->filename; - $newrecord->timecreated = empty($file_record->timecreated) ? $now : $file_record->timecreated; - $newrecord->timemodified = empty($file_record->timemodified) ? $now : $file_record->timemodified; + $newrecord->timecreated = $file_record->timecreated; + $newrecord->timemodified = $file_record->timemodified; $newrecord->mimetype = empty($file_record->mimetype) ? mimeinfo('type', $file_record->filename) : $file_record->mimetype; $newrecord->userid = empty($file_record->userid) ? null : $file_record->userid; $newrecord->source = empty($file_record->source) ? null : $file_record->source;