diff --git a/Changelog.md b/Changelog.md index 840a301..dc252a9 100644 --- a/Changelog.md +++ b/Changelog.md @@ -131,3 +131,5 @@ __*Why "v3.0.0" instead of "v1.0.0?"*__ Prior to phpMussel v3, the "phpMussel Co [2023.09.04; Maikuolan]: Added colouration to phpMussel's CLI mode (some code has been added to the core to facilitate this). The atHit method has been migrated from the Loader class to the Scanner class. [2023.09.16~18; Maikuolan]: Significantly refactored all L10N data. + +[2023.09.18; Maikuolan]: Better resource guarding. diff --git a/src/Loader.php b/src/Loader.php index cc8efc3..d40b1c5 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -8,7 +8,7 @@ * License: GNU/GPLv2 * @see LICENSE.txt * - * This file: The loader (last modified: 2023.09.17). + * This file: The loader (last modified: 2023.09.18). */ namespace phpMussel\Core; @@ -410,12 +410,12 @@ public function __construct( $WriteMode = 'ab'; $Data = $this->InstanceCache['PendingErrorLogData']; } - $Handle = fopen($File, $WriteMode); - if (is_resource($Handle)) { - fwrite($Handle, $Data); - fclose($Handle); - $this->logRotation($this->Configuration['core']['error_log']); + if (!is_resource($Handle = fopen($File, $WriteMode))) { + return false; } + fwrite($Handle, $Data); + fclose($Handle); + $this->logRotation($this->Configuration['core']['error_log']); return true; }); @@ -476,8 +476,7 @@ public function readFile(string $File): string return ''; } - $Handle = fopen($File, 'rb'); - if (!is_resource($Handle)) { + if (!is_resource($Handle = fopen($File, 'rb'))) { return ''; } $Data = fread($Handle, $Filesize); @@ -960,8 +959,7 @@ public function readFileContentGZ(string $File): string $Data = ''; if ($BlocksToRead > 0) { - $Handle = gzopen($File, 'rb'); - if (!is_resource($Handle)) { + if (!is_resource($Handle = gzopen($File, 'rb'))) { return ''; } $Done = 0; @@ -1041,12 +1039,7 @@ public function gZCompressFile(string $File): bool return false; } - $Handle = fopen($File, 'rb'); - if (!is_resource($Handle)) { - return false; - } - $HandleGZ = gzopen($File . '.gz', 'wb'); - if (!is_resource($HandleGZ)) { + if (!is_resource($Handle = fopen($File, 'rb')) || !is_resource($HandleGZ = gzopen($File . '.gz', 'wb'))) { return false; } while (!feof($Handle)) { @@ -1237,8 +1230,7 @@ public function updateConfiguration(): bool } else { return false; } - $Handle = fopen($this->ConfigurationPath, 'wb'); - if (!is_resource($Handle)) { + if (!is_resource($Handle = fopen($this->ConfigurationPath, 'wb'))) { return false; } $Err = fwrite($Handle, $Reconstructed); diff --git a/src/Scanner.php b/src/Scanner.php index c1543c1..9bb47e3 100644 --- a/src/Scanner.php +++ b/src/Scanner.php @@ -8,7 +8,7 @@ * License: GNU/GPLv2 * @see LICENSE.txt * - * This file: The scanner (last modified: 2023.09.04). + * This file: The scanner (last modified: 2023.09.18). */ namespace phpMussel\Core; @@ -108,7 +108,10 @@ public function __construct(\phpMussel\Core\Loader &$Loader) ]) . "\n"; $Truncate = $this->Loader->readBytes($this->Loader->Configuration['core']['truncate']); $WriteMode = (!file_exists($File) || ($Truncate > 0 && filesize($File) >= $Truncate)) ? 'wb' : 'ab'; - $Stream = fopen($File, $WriteMode); + if (!is_resource($Stream = fopen($File, $WriteMode))) { + trigger_error('The "writeToSerialLog" event failed to open "' . $File . '" for writing.'); + return false; + } fwrite($Stream, $Data); fclose($Stream); $this->Loader->logRotation($this->Loader->Configuration['core']['scan_log_serialized']); @@ -146,7 +149,10 @@ public function __construct(\phpMussel\Core\Loader &$Loader) $Truncate = $this->Loader->readBytes($this->Loader->Configuration['core']['truncate']); $WriteMode = ($Truncate > 0 && filesize($File) >= $Truncate) ? 'wb' : 'ab'; } - $Handle = fopen($File, 'ab'); + if (!is_resource($Handle = fopen($File, 'ab'))) { + trigger_error('The "writeToScanLog" event failed to open "' . $File . '" for writing.'); + return false; + } fwrite($Handle, $Results); fclose($Handle); $this->Loader->logRotation($this->Loader->Configuration['core']['scan_log']);