Skip to content

Commit a2a381a

Browse files
authored
[5.3] Refactor all instances of File::makesafe() to use framework (joomla#43359)
* Refactor all occurences of File::makesafe() to use framework * Updating joomla/filesystem to 3.0.2 * Update LocalAdapter.php
1 parent 29eb0b1 commit a2a381a

File tree

5 files changed

+91
-34
lines changed

5 files changed

+91
-34
lines changed

administrator/components/com_contenthistory/src/Helper/ContenthistoryHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
namespace Joomla\Component\Contenthistory\Administrator\Helper;
1212

1313
use Joomla\CMS\Factory;
14-
use Joomla\CMS\Filesystem\File;
1514
use Joomla\CMS\Language\Text;
1615
use Joomla\CMS\Table\ContentHistory;
1716
use Joomla\CMS\Table\ContentType;
1817
use Joomla\CMS\Table\Table;
1918
use Joomla\Database\ParameterType;
19+
use Joomla\Filesystem\File;
2020
use Joomla\Filesystem\Folder;
2121
use Joomla\Filesystem\Path;
2222

administrator/components/com_templates/src/Helper/TemplateHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
use Joomla\CMS\Component\ComponentHelper;
1414
use Joomla\CMS\Factory;
15-
use Joomla\CMS\Filesystem\File;
1615
use Joomla\CMS\Language\Text;
16+
use Joomla\Filesystem\File;
1717

1818
// phpcs:disable PSR1.Files.SideEffects
1919
\defined('_JEXEC') or die;

administrator/components/com_templates/src/Model/TemplateModel.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Joomla\CMS\Component\ComponentHelper;
1515
use Joomla\CMS\Date\Date;
1616
use Joomla\CMS\Factory;
17-
use Joomla\CMS\Filesystem\File;
1817
use Joomla\CMS\Form\Form;
1918
use Joomla\CMS\Image\Image;
2019
use Joomla\CMS\Language\Text;
@@ -24,6 +23,8 @@
2423
use Joomla\Component\Templates\Administrator\Helper\TemplateHelper;
2524
use Joomla\Component\Templates\Administrator\Helper\TemplatesHelper;
2625
use Joomla\Database\ParameterType;
26+
use Joomla\Filesystem\Exception\FilesystemException;
27+
use Joomla\Filesystem\File;
2728
use Joomla\Filesystem\Folder;
2829
use Joomla\Filesystem\Path;
2930
use Joomla\Utilities\ArrayHelper;
@@ -747,7 +748,10 @@ public function copy()
747748
$dst = Path::clean($toPath . '/' . $folder . '/' . $languageFile);
748749

749750
if (is_file($src)) {
750-
File::copy($src, $dst);
751+
try {
752+
File::copy($src, $dst);
753+
} catch (FilesystemException $exception) {
754+
}
751755
}
752756
}
753757
}
@@ -811,7 +815,12 @@ protected function fixTemplateName()
811815

812816
foreach ($files as $file) {
813817
$newFile = '/' . str_replace($oldName, $newName, basename($file));
814-
$result = File::move($file, \dirname($file) . $newFile) && $result;
818+
819+
try {
820+
$result = File::move($file, \dirname($file) . $newFile) && $result;
821+
} catch (FilesystemException $exception) {
822+
$result = false;
823+
}
815824
}
816825

817826
// Edit XML file
@@ -826,7 +835,12 @@ protected function fixTemplateName()
826835
$pattern[] = '#<media(.*)' . $oldName . '(.*)>#';
827836
$replace[] = '<media${1}' . $newName . '${2}>';
828837
$contents = preg_replace($pattern, $replace, $contents);
829-
$result = File::write($xmlFile, $contents) && $result;
838+
839+
try {
840+
$result = File::write($xmlFile, $contents) && $result;
841+
} catch (FilesystemException $exception) {
842+
$result = false;
843+
}
830844
}
831845

832846
return $result;
@@ -998,9 +1012,9 @@ public function save($data)
9981012
return false;
9991013
}
10001014

1001-
$return = File::write($filePath, $data['source']);
1002-
1003-
if (!$return) {
1015+
try {
1016+
File::write($filePath, $data['source']);
1017+
} catch (FilesystemException $exception) {
10041018
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_ERROR_FAILED_TO_SAVE_FILENAME', $fileName), 'error');
10051019

10061020
return false;
@@ -1252,7 +1266,11 @@ public function createTemplateOverride($overridePath, $htmlPath)
12521266
$htmlFilePath = File::stripExt($htmlFilePath) . '-' . $today->format('Ymd-His') . '.' . File::getExt($htmlFilePath);
12531267
}
12541268

1255-
$return = File::copy($file, $htmlFilePath, '', true);
1269+
try {
1270+
$return = File::copy($file, $htmlFilePath, '', true);
1271+
} catch (FilesystemException $exception) {
1272+
$return = false;
1273+
}
12561274
}
12571275

12581276
return $return;
@@ -1273,7 +1291,11 @@ public function deleteFile($file)
12731291
$app = Factory::getApplication();
12741292
$filePath = $this->getBasePath() . urldecode(base64_decode($file));
12751293

1276-
$return = File::delete($filePath);
1294+
try {
1295+
$return = File::delete($filePath);
1296+
} catch (FilesystemException $exception) {
1297+
$return = false;
1298+
}
12771299

12781300
if (!$return) {
12791301
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_DELETE_ERROR'), 'error');
@@ -1356,7 +1378,9 @@ public function uploadFile($file, $location)
13561378
return false;
13571379
}
13581380

1359-
if (!File::upload($file['tmp_name'], Path::clean($path . '/' . $location . '/' . $fileName))) {
1381+
try {
1382+
File::upload($file['tmp_name'], Path::clean($path . '/' . $location . '/' . $fileName));
1383+
} catch (FilesystemException $exception) {
13601384
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_UPLOAD_ERROR'), 'error');
13611385

13621386
return false;
@@ -1697,11 +1721,15 @@ public function copyFile($newName, $location, $file)
16971721
return false;
16981722
}
16991723

1700-
if (File::copy($path . $relPath, $newPath)) {
1701-
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_FILE_COPY_SUCCESS', $newName . '.' . $ext));
1702-
1703-
return true;
1724+
try {
1725+
File::copy($path . $relPath, $newPath);
1726+
} catch (FilesystemException $exception) {
1727+
return false;
17041728
}
1729+
1730+
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_FILE_COPY_SUCCESS', $newName . '.' . $ext));
1731+
1732+
return true;
17051733
}
17061734

17071735
return false;
@@ -1908,7 +1936,9 @@ public function child()
19081936
Folder::create($toPath . '/html');
19091937

19101938
// Copy the template definition from the parent template
1911-
if (!File::copy($fromPath, $toPath . '/templateDetails.xml')) {
1939+
try {
1940+
File::copy($fromPath, $toPath . '/templateDetails.xml');
1941+
} catch (FilesystemException $exception) {
19121942
return false;
19131943
}
19141944

@@ -1989,9 +2019,9 @@ public function child()
19892019
$dom->formatOutput = true;
19902020
$dom->loadXML($xml->asXML());
19912021

1992-
$result = File::write($xmlFile, $dom->saveXML());
1993-
1994-
if (!$result) {
2022+
try {
2023+
$result = File::write($xmlFile, $dom->saveXML());
2024+
} catch (FilesystemException $exception) {
19952025
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
19962026

19972027
return false;

libraries/src/Helper/MediaHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
use enshrined\svgSanitize\Sanitizer;
1313
use Joomla\CMS\Component\ComponentHelper;
1414
use Joomla\CMS\Factory;
15-
use Joomla\CMS\Filesystem\File;
1615
use Joomla\CMS\Filter\InputFilter;
1716
use Joomla\CMS\Language\Text;
1817
use Joomla\CMS\Plugin\PluginHelper;
18+
use Joomla\Filesystem\File;
1919
use Joomla\Registry\Registry;
2020

2121
// phpcs:disable PSR1.Files.SideEffects

plugins/filesystem/local/src/Adapter/LocalAdapter.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use Joomla\CMS\Date\Date;
1414
use Joomla\CMS\Factory;
15-
use Joomla\CMS\Filesystem\File;
1615
use Joomla\CMS\Helper\MediaHelper;
1716
use Joomla\CMS\HTML\HTMLHelper;
1817
use Joomla\CMS\Image\Exception\UnparsableImageException;
@@ -25,6 +24,7 @@
2524
use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
2625
use Joomla\Component\Media\Administrator\Exception\InvalidPathException;
2726
use Joomla\Filesystem\Exception\FilesystemException;
27+
use Joomla\Filesystem\File;
2828
use Joomla\Filesystem\Folder;
2929
use Joomla\Filesystem\Path;
3030

@@ -260,7 +260,10 @@ public function createFile(string $name, string $path, $data): string
260260

261261
$this->checkContent($localPath, $data);
262262

263-
File::write($localPath, $data);
263+
try {
264+
File::write($localPath, $data);
265+
} catch (FilesystemException $exception) {
266+
}
264267

265268
if ($this->thumbnails && MediaHelper::isImage(pathinfo($localPath)['basename'])) {
266269
$thumbnailPaths = $this->getLocalThumbnailPaths($localPath);
@@ -298,7 +301,10 @@ public function updateFile(string $name, string $path, $data)
298301

299302
$this->checkContent($localPath, $data);
300303

301-
File::write($localPath, $data);
304+
try {
305+
File::write($localPath, $data);
306+
} catch (FilesystemException $exception) {
307+
}
302308

303309
if ($this->thumbnails && MediaHelper::isImage(pathinfo($localPath)['basename'])) {
304310
$thumbnailPaths = $this->getLocalThumbnailPaths($localPath);
@@ -328,11 +334,15 @@ public function delete(string $path)
328334
$thumbnailPaths = $this->getLocalThumbnailPaths($localPath);
329335

330336
if (is_file($localPath)) {
331-
if ($this->thumbnails && !empty($thumbnailPaths['fs']) && is_file($thumbnailPaths['fs'])) {
332-
File::delete($thumbnailPaths['fs']);
333-
}
337+
try {
338+
if ($this->thumbnails && !empty($thumbnailPaths['fs']) && is_file($thumbnailPaths['fs'])) {
339+
File::delete($thumbnailPaths['fs']);
340+
}
334341

335-
$success = File::delete($localPath);
342+
$success = File::delete($localPath);
343+
} catch (\Throwable $exception) {
344+
throw new \Exception('Delete not possible!', 500, $exception);
345+
}
336346
} else {
337347
if (!is_dir(Path::clean($localPath))) {
338348
throw new FileNotFoundException();
@@ -525,7 +535,9 @@ private function copyFile(string $sourcePath, string $destinationPath, bool $for
525535
throw new \Exception(Text::_('COM_MEDIA_COPY_FILE_NOT_POSSIBLE_FILE_ALREADY_EXISTS'));
526536
}
527537

528-
if (!File::copy($sourcePath, $destinationPath)) {
538+
try {
539+
File::copy($sourcePath, $destinationPath);
540+
} catch (FilesystemException $exception) {
529541
throw new \Exception(Text::_('COM_MEDIA_COPY_FILE_NOT_POSSIBLE'));
530542
}
531543
}
@@ -548,7 +560,11 @@ private function copyFolder(string $sourcePath, string $destinationPath, bool $f
548560
throw new \Exception(Text::_('COM_MEDIA_COPY_FOLDER_ALREADY_EXISTS'));
549561
}
550562

551-
if (is_file($destinationPath) && !File::delete($destinationPath)) {
563+
try {
564+
if (is_file($destinationPath)) {
565+
File::delete($destinationPath);
566+
}
567+
} catch (FilesystemException $exception) {
552568
throw new \Exception(Text::_('COM_MEDIA_COPY_FOLDER_DESTINATION_CAN_NOT_DELETE'));
553569
}
554570

@@ -634,7 +650,9 @@ private function moveFile(string $sourcePath, string $destinationPath, bool $for
634650
throw new \Exception(Text::_('COM_MEDIA_MOVE_FILE_ALREADY_EXISTS'));
635651
}
636652

637-
if (!File::move($sourcePath, $destinationPath)) {
653+
try {
654+
File::move($sourcePath, $destinationPath);
655+
} catch (FilesystemException $exception) {
638656
throw new \Exception(Text::_('COM_MEDIA_MOVE_FILE_NOT_POSSIBLE'));
639657
}
640658
}
@@ -657,7 +675,11 @@ private function moveFolder(string $sourcePath, string $destinationPath, bool $f
657675
throw new \Exception(Text::_('COM_MEDIA_MOVE_FOLDER_ALREADY_EXISTS'));
658676
}
659677

660-
if (is_file($destinationPath) && !File::delete($destinationPath)) {
678+
try {
679+
if (is_file($destinationPath)) {
680+
File::delete($destinationPath);
681+
}
682+
} catch (FilesystemException $exception) {
661683
throw new \Exception(Text::_('COM_MEDIA_MOVE_FOLDER_NOT_POSSIBLE'));
662684
}
663685

@@ -828,13 +850,18 @@ private function checkContent(string $localPath, string $mediaContent)
828850
// @todo find a better way to check the input, by not writing the file to the disk
829851
$tmpFile = Path::clean(\dirname($localPath) . '/' . uniqid() . '.' . strtolower(File::getExt($name)));
830852

831-
if (!File::write($tmpFile, $mediaContent)) {
853+
try {
854+
File::write($tmpFile, $mediaContent);
855+
} catch (FilesystemException $exception) {
832856
throw new \Exception(Text::_('JLIB_MEDIA_ERROR_UPLOAD_INPUT'), 500);
833857
}
834858

835859
$can = $helper->canUpload(['name' => $name, 'size' => \strlen($mediaContent), 'tmp_name' => $tmpFile], 'com_media');
836860

837-
File::delete($tmpFile);
861+
try {
862+
File::delete($tmpFile);
863+
} catch (FilesystemException $exception) {
864+
}
838865

839866
if (!$can) {
840867
throw new \Exception(Text::_('JLIB_MEDIA_ERROR_UPLOAD_INPUT'), 403);

0 commit comments

Comments
 (0)