From 3e076d8cff472426d0f78d601546b5887b68d75d Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 15 Feb 2021 17:04:00 +0100 Subject: [PATCH 1/4] MDL-70901 backup: check handle before calling fclose() @fclose no longer catches error in PHP 8.0 when handle is null --- backup/util/loggers/file_logger.class.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backup/util/loggers/file_logger.class.php b/backup/util/loggers/file_logger.class.php index 98ff74acf39..d1a7554eeb2 100644 --- a/backup/util/loggers/file_logger.class.php +++ b/backup/util/loggers/file_logger.class.php @@ -50,11 +50,18 @@ class file_logger extends base_logger { } public function __destruct() { - @fclose($this->fhandle); // Blindy close the file handler (no exceptions in destruct) + if (is_resource($this->fhandle)) { + // Blindy close the file handler (no exceptions in destruct). + @fclose($this->fhandle); + } } public function __sleep() { - @fclose($this->fhandle); // Blindy close the file handler before serialization + if (is_resource($this->fhandle)) { + // Blindy close the file handler before serialization. + @fclose($this->fhandle); + $this->fhandle = null; + } return array('level', 'showdate', 'showlevel', 'next', 'fullpath'); } From a1a72f720ea2fc70e3ed530dbf7b4fc1138e8d0b Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 15 Feb 2021 19:39:50 +0100 Subject: [PATCH 2/4] MDL-70901 core_files: @ does no mask errors in php8 anymore --- lib/filestorage/file_system_filedir.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/filestorage/file_system_filedir.php b/lib/filestorage/file_system_filedir.php index 103dcca41e5..3b0c509d96d 100644 --- a/lib/filestorage/file_system_filedir.php +++ b/lib/filestorage/file_system_filedir.php @@ -378,7 +378,9 @@ class file_system_filedir extends file_system { // Let's try to prevent some race conditions. $prev = ignore_user_abort(true); - @unlink($hashfile.'.tmp'); + if (file_exists($hashfile.'.tmp')) { + @unlink($hashfile.'.tmp'); + } if (!copy($pathname, $hashfile.'.tmp')) { // Borked permissions or out of disk space. @unlink($hashfile.'.tmp'); @@ -467,7 +469,10 @@ class file_system_filedir extends file_system { } rename($hashfile.'.tmp', $hashfile); chmod($hashfile, $this->filepermissions); // Fix permissions if needed. - @unlink($hashfile.'.tmp'); // Just in case anything fails in a weird way. + if (file_exists($hashfile.'.tmp')) { + // Just in case anything fails in a weird way. + @unlink($hashfile.'.tmp'); + } ignore_user_abort($prev); return array($contenthash, $filesize, $newfile); From 2d059d1657bbd8f2a8685e57fc998dfd07d7973b Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 15 Feb 2021 19:45:23 +0100 Subject: [PATCH 3/4] MDL-70901 cache: @ no longer masks errors in unlink in php 8 --- cache/stores/file/lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index dd5e05825e4..0c08710d551 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -399,7 +399,7 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i public function delete($key) { $filename = $key.'.cache'; $file = $this->file_path_for_key($key); - if (@unlink($file)) { + if (file_exists($file) && @unlink($file)) { unset($this->keys[$filename]); return true; } From b88f1a84bfa944b5890728ea20c8ebd888fb9b3f Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 22 Feb 2021 16:17:21 +0100 Subject: [PATCH 4/4] MDL-70901 core: @ no longer masks errors in PHP 8.0 --- backup/util/loggers/file_logger.class.php | 2 +- lib/classes/task/file_temp_cleanup_task.php | 6 +++++- lib/filestorage/file_system_filedir.php | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/backup/util/loggers/file_logger.class.php b/backup/util/loggers/file_logger.class.php index d1a7554eeb2..97ae661339a 100644 --- a/backup/util/loggers/file_logger.class.php +++ b/backup/util/loggers/file_logger.class.php @@ -97,7 +97,7 @@ class file_logger extends base_logger { } else { $content = $prefix . str_repeat('  ', $depth) . htmlentities($message, ENT_QUOTES, 'UTF-8') . '
' . PHP_EOL; } - if (false === fwrite($this->fhandle, $content)) { + if (!is_resource($this->fhandle) || (false === fwrite($this->fhandle, $content))) { throw new base_logger_exception('error_writing_file', $this->fullpath); } return true; diff --git a/lib/classes/task/file_temp_cleanup_task.php b/lib/classes/task/file_temp_cleanup_task.php index 05bfd5cccbd..d4971dc84d3 100644 --- a/lib/classes/task/file_temp_cleanup_task.php +++ b/lib/classes/task/file_temp_cleanup_task.php @@ -90,7 +90,11 @@ class file_temp_cleanup_task extends scheduled_task { } else { // Return the time modified to the original date only for real files. if ($iter->isDir() && !$iter->isDot()) { - touch($node, $modifieddateobject[$node]); + try { + @touch($node, $modifieddateobject[$node]); + } catch (\Throwable $t) { + null; + } } } } diff --git a/lib/filestorage/file_system_filedir.php b/lib/filestorage/file_system_filedir.php index 3b0c509d96d..8c26da3b6dd 100644 --- a/lib/filestorage/file_system_filedir.php +++ b/lib/filestorage/file_system_filedir.php @@ -395,7 +395,10 @@ class file_system_filedir extends file_system { } rename($hashfile.'.tmp', $hashfile); chmod($hashfile, $this->filepermissions); // Fix permissions if needed. - @unlink($hashfile.'.tmp'); // Just in case anything fails in a weird way. + if (file_exists($hashfile.'.tmp')) { + // Just in case anything fails in a weird way. + @unlink($hashfile.'.tmp'); + } ignore_user_abort($prev); return array($contenthash, $filesize, $newfile);