Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Refactor all instances of File to use framework #43362

Open
wants to merge 6 commits into
base: 5.2-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -327,7 +328,11 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the upload fails, we should stop the execution...


}

// Unpack the downloaded package file.
$package = InstallerHelper::unpack($tmp_dest, true);
Expand Down
64 changes: 40 additions & 24 deletions administrator/components/com_joomlaupdate/src/Model/UpdateModel.php
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have logging 5 lines above, maybe log the exception?

}

// Download the package
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {

}
}

// 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) {
Expand Down Expand Up @@ -889,18 +897,22 @@ 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');
}
} catch (FilesystemException $exception) {

// Remove joomla.xml from the site's root.
if (is_file(JPATH_ROOT . '/joomla.xml')) {
File::delete(JPATH_ROOT . '/joomla.xml');
}

// Unset the update filename from the session.
Expand Down Expand Up @@ -983,9 +995,9 @@ public function upload()
$tmp_src = $userfile['tmp_name'];

// Move uploaded file.
$result = FileCMS::upload($tmp_src, $tmp_dest, false, true);

if (!$result) {
try {
File::upload($tmp_src, $tmp_dest, false);
} catch (FilesystemException $exception) {
throw new \RuntimeException(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 500);
}

Expand Down Expand Up @@ -1061,7 +1073,11 @@ public function removePackageFiles()

foreach ($files as $file) {
if ($file !== null && is_file($file)) {
File::delete($file);
try {
File::delete($file);
} catch (FilesystemException $exception) {

}
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions libraries/src/Log/Logger/FormattedtextLogger.php
Expand Up @@ -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
Expand Down Expand Up @@ -136,7 +137,9 @@ 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")) {
try {
File::append($this->path, implode("\n", $lines) . "\n");
} catch (FilesystemException $exception) {
throw new \RuntimeException('Cannot write to log file.');
}
}
Expand Down Expand Up @@ -165,7 +168,9 @@ public function addEntry(LogEntry $entry)
$line = $this->formatLine($entry);
$line .= "\n";

if (!File::append($this->path, $line)) {
try {
File::append($this->path, $line);
} catch (FilesystemException $exception) {
throw new \RuntimeException('Cannot write to log file.');
}
}
Expand Down Expand Up @@ -269,7 +274,9 @@ protected function initFile()
// Build the log file header.
$head = $this->generateFileHeader();

if (!File::write($this->path, $head)) {
try {
File::write($this->path, $head);
} catch (FilesystemException $exception) {
throw new \RuntimeException('Cannot write to log file.');
}
}
Expand Down