[TASK] Centralize imagefile_ext, mediafile_ext and textfile_ext 55/63155/8
authorBenni Mack <benni@typo3.org>
Mon, 3 Feb 2020 08:53:03 +0000 (09:53 +0100)
committerBenni Mack <benni@typo3.org>
Fri, 6 Mar 2020 11:26:09 +0000 (12:26 +0100)
Most access to the configuration options are made against AbstractFile
objects in FAL, which is now centralized in one place in AbstractFile.

This reduces errors when checking for file patterns.

Resolves: #90305
Releases: master
Change-Id: I7835d877bae1addf830ede3645ff8f1faa113052
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63155
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Susanne Moog <look@susi.dev>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack <benni@typo3.org>
15 files changed:
typo3/sysext/backend/Classes/Clipboard/Clipboard.php
typo3/sysext/backend/Classes/Controller/ContentElement/ElementInformationController.php
typo3/sysext/backend/Classes/Controller/File/FileController.php
typo3/sysext/backend/Classes/Form/Container/InlineRecordContainer.php
typo3/sysext/backend/Classes/Form/Element/FileInfoElement.php
typo3/sysext/core/Classes/Resource/AbstractFile.php
typo3/sysext/core/Classes/Resource/Processing/LocalPreviewHelper.php
typo3/sysext/core/Classes/Utility/File/ExtendedFileUtility.php
typo3/sysext/filelist/Classes/ContextMenu/ItemProviders/FileProvider.php
typo3/sysext/filelist/Classes/Controller/File/EditFileController.php
typo3/sysext/filelist/Classes/FileFacade.php
typo3/sysext/filelist/Classes/FileList.php
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/recordlist/Classes/Browser/FileBrowser.php
typo3/sysext/setup/Classes/Controller/SetupModuleController.php

index 77d588f..7c937f3 100644 (file)
@@ -385,13 +385,7 @@ class Clipboard
                             $thumb = [];
                             $folder = $fileObject instanceof \TYPO3\CMS\Core\Resource\Folder;
                             $size = $folder ? '' : '(' . GeneralUtility::formatSize($fileObject->getSize()) . 'bytes)';
-                            if (
-                                !$folder
-                                && GeneralUtility::inList(
-                                    $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
-                                    $fileObject->getExtension()
-                                )
-                            ) {
+                            if (!$folder && $fileObject->isImage()) {
                                 $thumb = [
                                     'image' => $fileObject->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, [])->getPublicUrl(true),
                                     'title' => htmlspecialchars($fileObject->getName())
index b1d880c..08c5471 100644 (file)
@@ -310,7 +310,6 @@ class ElementInformationController
         } else {
             $rendererRegistry = GeneralUtility::makeInstance(RendererRegistry::class);
             $fileRenderer = $rendererRegistry->getRenderer($this->fileObject);
-            $fileExtension = $this->fileObject->getExtension();
             $preview['url'] = $this->fileObject->getPublicUrl(true);
 
             $width = '590m';
@@ -327,7 +326,7 @@ class ElementInformationController
                 );
 
             // else check if we can create an Image preview
-            } elseif (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileExtension)) {
+            } elseif ($this->fileObject->isImage()) {
                 $preview['fileObject'] = $this->fileObject;
                 $preview['width'] = $width;
                 $preview['height'] = $height;
index 056f7ec..ec2c35e 100644 (file)
@@ -246,8 +246,7 @@ class FileController
      */
     protected function getFileEditRedirect(File $file): ?string
     {
-        $textFileExtensionList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? '';
-        if (!GeneralUtility::inList($textFileExtensionList, $file->getExtension())) {
+        if (!$file->isTextFile()) {
             return null;
         }
         $properties = $file->getProperties();
@@ -279,7 +278,7 @@ class FileController
     {
         if ($result instanceof File) {
             $thumbUrl = '';
-            if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
+            if ($result->isImage()) {
                 $processedFile = $result->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, []);
                 if ($processedFile) {
                     $thumbUrl = $processedFile->getPublicUrl(true);
index 1421799..7ad0aa0 100644 (file)
@@ -367,8 +367,7 @@ class InlineRecordContainer extends AbstractContainer
                     }
                     $imageSetup = array_merge(['width' => '45', 'height' => '45c'], $imageSetup);
 
-                    if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails']
-                        && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileObject->getExtension())) {
+                    if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails'] && $fileObject->isImage()) {
                         $processedImage = $fileObject->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, $imageSetup);
                         // Only use a thumbnail if the processing process was successful by checking if image width is set
                         if ($processedImage->getProperty('width')) {
index 4fe2156..1a0062d 100644 (file)
@@ -72,7 +72,7 @@ class FileInfoElement extends AbstractFormElement
                     . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
                     . '</span>';
             }
-            if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
+            if ($file->isImage()) {
                 $processedFile = $file->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, ['width' => 150, 'height' => 150]);
                 $previewImage = $processedFile->getPublicUrl(true);
                 if ($previewImage) {
index a26d3a8..b07f47c 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Core\Resource;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
 use TYPO3\CMS\Core\Utility\PathUtility;
 
@@ -319,6 +320,33 @@ abstract class AbstractFile implements FileInterface
         return (int)$this->properties['type'];
     }
 
+    /**
+     * Useful to find out if this file can be previewed or resized as image.
+     * @return bool true if File has an image-extension according to $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
+     */
+    public function isImage(): bool
+    {
+        return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] ?? ''), $this->getExtension());
+    }
+
+    /**
+     * Useful to find out if this file has a file extension based on any of the registered media extensions
+     * @return bool true if File is a media-extension according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']
+     */
+    public function isMediaFile(): bool
+    {
+        return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'] ?? ''), $this->getExtension());
+    }
+
+    /**
+     * Useful to find out if this file can be edited.
+     *
+     * @return bool true if File is a text-based file extension according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext']
+     */
+    public function isTextFile(): bool
+    {
+        return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? ''), $this->getExtension());
+    }
     /******************
      * CONTENTS RELATED
      ******************/
index 6a71628..675991d 100644 (file)
@@ -107,9 +107,7 @@ class LocalPreviewHelper
     protected function generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
     {
         // Check file extension
-        if ($file->getType() !== File::FILETYPE_IMAGE
-            && !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())
-        ) {
+        if ($file->getType() !== File::FILETYPE_IMAGE && !$file->isImage()) {
             // Create a default image
             $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
             $graphicalFunctions->getTemporaryImageWithText(
index 2d73f7a..efdc38f 100644 (file)
@@ -989,20 +989,20 @@ class ExtendedFileUtility extends BasicFileUtility
         // for backwards compatibility: the combined file identifier was the path+filename
         $fileIdentifier = $cmds['target'];
         $fileObject = $this->getFileObject($fileIdentifier);
-        // Example identifier for $cmds['target'] => "2:targetpath/targetfolder/"
-        $content = $cmds['data'];
         if (!$fileObject instanceof File) {
             $this->writeLog(SystemLogFileAction::EDIT, SystemLogErrorClassification::SYSTEM_ERROR, 123, 'Target "%s" was not a file!', [$fileIdentifier]);
             $this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotAFile', [$fileIdentifier]);
             return false;
         }
-        $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
-        if (!GeneralUtility::inList($extList, $fileObject->getExtension())) {
+        if (!$fileObject->isTextFile()) {
+            $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
             $this->writeLog(SystemLogFileAction::EDIT, SystemLogErrorClassification::USER_ERROR, 102, 'File extension "%s" is not a textfile format! (%s)', [$fileObject->getExtension(), $extList]);
             $this->addMessageToFlashMessageQueue('FileUtility.FileExtensionIsNotATextfileFormat', [$fileObject->getExtension(), $extList]);
             return false;
         }
         try {
+            // Example identifier for $cmds['target'] => "2:targetpath/targetfolder/"
+            $content = $cmds['data'];
             $fileObject->setContents($content);
             clearstatcache();
             $this->writeLog(SystemLogFileAction::EDIT, SystemLogErrorClassification::MESSAGE, 1, 'File saved to "%s", bytes: %s, MD5: %s ', [$fileObject->getIdentifier(), $fileObject->getSize(), md5($content)]);
index 6c317a7..edcbbf1 100644 (file)
@@ -182,7 +182,7 @@ class FileProvider extends \TYPO3\CMS\Backend\ContextMenu\ItemProviders\Abstract
     {
         return $this->isFile()
            && $this->record->checkActionPermission('write')
-           && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $this->record->getExtension());
+           && $this->record->isTextFile();
     }
 
     /**
index 38a886d..822a41f 100644 (file)
@@ -182,9 +182,9 @@ class EditFileController
         $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
         $assigns['fileName'] = $this->fileObject->getName();
 
-        $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
         try {
-            if (!$extList || !GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
+            $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
+            if (!$this->fileObject->isTextFile()) {
                 // @todo throw a minor exception here, not the global one
                 throw new \Exception('Files with that extension are not editable. Allowed extensions are: ' . $extList, 1476050135);
             }
index 379391e..32875c1 100644 (file)
@@ -20,6 +20,7 @@ use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Localization\LanguageService;
+use TYPO3\CMS\Core\Resource\AbstractFile;
 use TYPO3\CMS\Core\Resource\FileInterface;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -75,7 +76,8 @@ class FileFacade
     public function getIsEditable(): bool
     {
         return $this->getIsWritable()
-            && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $this->resource->getExtension());
+            && $this->resource instanceof AbstractFile
+            && $this->resource->isTextFile();
     }
 
     /**
@@ -282,7 +284,7 @@ class FileFacade
      */
     public function getIsImage()
     {
-        return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($this->getExtension()));
+        return $this->resource instanceof AbstractFile && $this->resource->isImage();
     }
 
     /**
index 871471d..b5f3220 100644 (file)
@@ -795,7 +795,7 @@ class FileList
                                 . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
                                 . '</span>';
                         // Thumbnails?
-                        } elseif ($this->thumbs && ($this->isImage($ext) || $this->isMediaFile($ext))) {
+                        } elseif ($this->thumbs && ($fileObject->isImage() || $fileObject->isMediaFile())) {
                             $imageUri = BackendUtility::getThumbnailUrl($fileObject->getUid(), [
                                 'width' => $this->thumbnailConfiguration->getWidth(),
                                 'height' => $this->thumbnailConfiguration->getHeight()
@@ -851,24 +851,26 @@ class FileList
 
     /**
      * Returns TRUE if $ext is an image-extension according to $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
+     * Use the AbstractFile->isImage() method if you're using File objects directly
      *
      * @param string $ext File extension
      * @return bool
      */
     public function isImage($ext)
     {
-        return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($ext));
+        return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']), strtolower($ext));
     }
 
     /**
      * Returns TRUE if $ext is a media-extension according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']
+     * Use the AbstractFile->isMediaFile() method if you're using File objects directly
      *
      * @param string $ext File extension
      * @return bool
      */
     public function isMediaFile($ext)
     {
-        return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'], strtolower($ext));
+        return GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']), strtolower($ext));
     }
 
     /**
@@ -994,7 +996,7 @@ class FileList
         $fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
 
         // Edit file content (if editable)
-        if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('write') && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $fileOrFolderObject->getExtension())) {
+        if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('write') && $fileOrFolderObject->isTextFile()) {
             $url = (string)$this->uriBuilder->buildUriFromRoute('file_edit', ['target' => $fullIdentifier]);
             $editOnClick = 'top.list_frame.location.href=' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+encodeURIComponent(top.list_frame.document.location.pathname+top.list_frame.document.location.search);return false;';
             $cells['edit'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($editOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editcontent') . '">'
index 6116b1a..db9eaa8 100644 (file)
@@ -4359,7 +4359,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
                     $processingConfiguration['stripProfile'] = $fileArray['stripProfile'];
                 }
                 // Check if we can handle this type of file for editing
-                if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileObject->getExtension())) {
+                if ($fileObject->isImage()) {
                     $maskArray = $fileArray['m.'];
                     // Must render mask images and include in hash-calculating
                     // - otherwise we cannot be sure the filename is unique for the setup!
index 1141112..b18f5a0 100644 (file)
@@ -262,10 +262,9 @@ class FileBrowser extends AbstractElementBrowser implements ElementBrowserInterf
         }
 
         foreach ($files as $fileObject) {
-            $fileExtension = $fileObject->getExtension();
             // Thumbnail/size generation:
             $imgInfo = [];
-            if (!$noThumbs && GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] . ',' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']), strtolower($fileExtension))) {
+            if (!$noThumbs && ($fileObject->isMediaFile() || $fileObject->isImage())) {
                 $processedFile = $fileObject->process(
                     ProcessedFile::CONTEXT_IMAGEPREVIEW,
                     $this->thumbnailConfiguration
@@ -295,7 +294,7 @@ class FileBrowser extends AbstractElementBrowser implements ElementBrowserInterf
                 'uid' => $fileObject->getUid(),
                 'fileName' => $fileObject->getName(),
                 'filePath' => $fileObject->getUid(),
-                'fileExt' => $fileExtension,
+                'fileExt' => $fileObject->getExtension(),
                 'fileIcon' => $icon
             ];
             if ($this->fileIsSelectableInFileList($fileObject, $imgInfo)) {
index 16a1e58..1bcf928 100644 (file)
@@ -900,7 +900,7 @@ class SetupModuleController
             }
 
             // Check if extension is allowed
-            if ($file && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
+            if ($file && $file->isImage()) {
 
                 // Create new file reference
                 $storeRec['sys_file_reference']['NEW1234'] = [