diff --git a/administrator/components/com_installer/src/Model/InstallModel.php b/administrator/components/com_installer/src/Model/InstallModel.php index 6ad08571ad286..953e90d38fbf5 100644 --- a/administrator/components/com_installer/src/Model/InstallModel.php +++ b/administrator/components/com_installer/src/Model/InstallModel.php @@ -14,7 +14,6 @@ use Joomla\CMS\Event\Installer\BeforeInstallationEvent; use Joomla\CMS\Event\Installer\BeforeInstallerEvent; use Joomla\CMS\Factory; -use Joomla\CMS\Filesystem\File; use Joomla\CMS\Installer\Installer; use Joomla\CMS\Installer\InstallerHelper; use Joomla\CMS\Language\Text; @@ -23,6 +22,8 @@ use Joomla\CMS\Router\Route; use Joomla\CMS\Updater\Update; use Joomla\CMS\Uri\Uri; +use Joomla\Filesystem\Exception\FilesystemException; +use Joomla\Filesystem\File; use Joomla\Filesystem\Path; // phpcs:disable PSR1.Files.SideEffects @@ -327,7 +328,13 @@ protected function _getPackageFromUpload() $tmp_src = $userfile['tmp_name']; // Move uploaded file. - File::upload($tmp_src, $tmp_dest, false, true); + try { + File::upload($tmp_src, $tmp_dest, false, true); + } catch (FilesystemException $exception) { + Factory::getApplication()->enqueueMessage(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 'error'); + + return false; + } // Unpack the downloaded package file. $package = InstallerHelper::unpack($tmp_dest, true); diff --git a/administrator/components/com_joomlaupdate/src/Model/UpdateModel.php b/administrator/components/com_joomlaupdate/src/Model/UpdateModel.php index f6fd1c7eac5a6..330faff124146 100644 --- a/administrator/components/com_joomlaupdate/src/Model/UpdateModel.php +++ b/administrator/components/com_joomlaupdate/src/Model/UpdateModel.php @@ -14,7 +14,6 @@ use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Extension\ExtensionHelper; use Joomla\CMS\Factory; -use Joomla\CMS\Filesystem\File as FileCMS; use Joomla\CMS\Filter\InputFilter; use Joomla\CMS\Http\Http; use Joomla\CMS\Http\HttpFactory; @@ -30,6 +29,7 @@ use Joomla\CMS\User\UserHelper; use Joomla\CMS\Version; use Joomla\Database\ParameterType; +use Joomla\Filesystem\Exception\FilesystemException; use Joomla\Filesystem\File; use Joomla\Registry\Registry; use Joomla\Utilities\ArrayHelper; @@ -508,7 +508,11 @@ protected function downloadPackage($url, $target) // Make sure the target does not exist. if (is_file($target)) { - File::delete($target); + try { + File::delete($target); + } catch (FilesystemException $exception) { + return false; + } } // Download the package @@ -526,9 +530,9 @@ protected function downloadPackage($url, $target) $body = $result->body; // Write the file to disk - $result = File::write($target, $body); - - if (!$result) { + try { + File::write($target, $body); + } catch (FilesystemException $exception) { return false; } @@ -611,14 +615,18 @@ public function createUpdateFile($basename = null): bool $configpath = JPATH_COMPONENT_ADMINISTRATOR . '/update.php'; if (is_file($configpath)) { - File::delete($configpath); + try { + File::delete($configpath); + } catch (FilesystemException $exception) { + return false; + } } // Write new file. First try with File. - $result = File::write($configpath, $data); - - // In case File failed but direct access could help. - if (!$result) { + try { + $result = File::write($configpath, $data); + } catch (FilesystemException $exception) { + // In case File failed but direct access could help. $fp = @fopen($configpath, 'wt'); if ($fp !== false) { @@ -889,18 +897,21 @@ public function cleanUp() $file = $app->getUserState('com_joomlaupdate.file', null); - if (is_file($tempdir . '/' . $file)) { - File::delete($tempdir . '/' . $file); - } + try { + if (is_file($tempdir . '/' . $file)) { + File::delete($tempdir . '/' . $file); + } - // Remove the update.php file used in Joomla 4.0.3 and later. - if (is_file(JPATH_COMPONENT_ADMINISTRATOR . '/update.php')) { - File::delete(JPATH_COMPONENT_ADMINISTRATOR . '/update.php'); - } + // Remove the update.php file used in Joomla 4.0.3 and later. + if (is_file(JPATH_COMPONENT_ADMINISTRATOR . '/update.php')) { + File::delete(JPATH_COMPONENT_ADMINISTRATOR . '/update.php'); + } - // Remove joomla.xml from the site's root. - if (is_file(JPATH_ROOT . '/joomla.xml')) { - File::delete(JPATH_ROOT . '/joomla.xml'); + // Remove joomla.xml from the site's root. + if (is_file(JPATH_ROOT . '/joomla.xml')) { + File::delete(JPATH_ROOT . '/joomla.xml'); + } + } catch (FilesystemException $exception) { } // Unset the update filename from the session. @@ -983,10 +994,10 @@ public function upload() $tmp_src = $userfile['tmp_name']; // Move uploaded file. - $result = FileCMS::upload($tmp_src, $tmp_dest, false, true); - - if (!$result) { - throw new \RuntimeException(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 500); + try { + File::upload($tmp_src, $tmp_dest, false); + } catch (FilesystemException $exception) { + throw new \RuntimeException(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 500, $exception); } Factory::getApplication()->setUserState('com_joomlaupdate.temp_file', $tmp_dest); @@ -1061,7 +1072,10 @@ public function removePackageFiles() foreach ($files as $file) { if ($file !== null && is_file($file)) { - File::delete($file); + try { + File::delete($file); + } catch (FilesystemException $exception) { + } } } } diff --git a/libraries/src/Log/Logger/FormattedtextLogger.php b/libraries/src/Log/Logger/FormattedtextLogger.php index a74fb082e641e..86e30ca99d110 100644 --- a/libraries/src/Log/Logger/FormattedtextLogger.php +++ b/libraries/src/Log/Logger/FormattedtextLogger.php @@ -10,11 +10,12 @@ namespace Joomla\CMS\Log\Logger; use Joomla\CMS\Factory; -use Joomla\CMS\Filesystem\File; use Joomla\CMS\Filesystem\Folder; use Joomla\CMS\Log\LogEntry; use Joomla\CMS\Log\Logger; use Joomla\CMS\Version; +use Joomla\Filesystem\Exception\FilesystemException; +use Joomla\Filesystem\File; use Joomla\Utilities\IpHelper; // phpcs:disable PSR1.Files.SideEffects @@ -136,8 +137,10 @@ public function __destruct() // Format all lines and write to file. $lines = array_map([$this, 'formatLine'], $this->deferredEntries); - if (!File::append($this->path, implode("\n", $lines) . "\n")) { - throw new \RuntimeException('Cannot write to log file.'); + try { + File::write($this->path, implode("\n", $lines) . "\n", false, true); + } catch (FilesystemException $exception) { + throw new \RuntimeException('Cannot write to log file.', 500, $exception); } } @@ -165,8 +168,10 @@ public function addEntry(LogEntry $entry) $line = $this->formatLine($entry); $line .= "\n"; - if (!File::append($this->path, $line)) { - throw new \RuntimeException('Cannot write to log file.'); + try { + File::write($this->path, $line, false, true); + } catch (FilesystemException $exception) { + throw new \RuntimeException('Cannot write to log file.', 500, $exception); } } } @@ -269,8 +274,10 @@ protected function initFile() // Build the log file header. $head = $this->generateFileHeader(); - if (!File::write($this->path, $head)) { - throw new \RuntimeException('Cannot write to log file.'); + try { + File::write($this->path, $head); + } catch (FilesystemException $exception) { + throw new \RuntimeException('Cannot write to log file.', 500, $exception); } }