[BUGFIX] Fix invalid type hints in EXT:form's file upload converter
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Mvc / Property / TypeConverter / UploadedFileReferenceConverter.php
index 1533957..567b7c2 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Form\Mvc\Property\TypeConverter;
  * The TYPO3 project - inspiring people to share!
  */
 
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Log\LogManager;
 use TYPO3\CMS\Core\Resource\File as File;
 use TYPO3\CMS\Core\Resource\FileReference as CoreFileReference;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Resource\File as File;
 use TYPO3\CMS\Core\Resource\FileReference as CoreFileReference;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -25,6 +26,7 @@ use TYPO3\CMS\Extbase\Property\Exception\TypeConverterException;
 use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface;
 use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
 use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
 use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface;
 use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
 use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
+use TYPO3\CMS\Form\Service\TranslationService;
 
 /**
  * Class UploadedFileReferenceConverter
 
 /**
  * Class UploadedFileReferenceConverter
@@ -130,11 +132,11 @@ class UploadedFileReferenceConverter extends AbstractTypeConverter
      * Actually convert from $source to $targetType, taking into account the fully
      * built $convertedChildProperties and $configuration.
      *
      * Actually convert from $source to $targetType, taking into account the fully
      * built $convertedChildProperties and $configuration.
      *
-     * @param string|int $source
+     * @param array $source
      * @param string $targetType
      * @param array $convertedChildProperties
      * @param PropertyMappingConfigurationInterface $configuration
      * @param string $targetType
      * @param array $convertedChildProperties
      * @param PropertyMappingConfigurationInterface $configuration
-     * @return AbstractFileFolder
+     * @return AbstractFileFolder|Error|null
      * @internal
      */
     public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)
      * @internal
      */
     public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)
@@ -142,12 +144,17 @@ class UploadedFileReferenceConverter extends AbstractTypeConverter
         if (!isset($source['error']) || $source['error'] === \UPLOAD_ERR_NO_FILE) {
             if (isset($source['submittedFile']['resourcePointer'])) {
                 try {
         if (!isset($source['error']) || $source['error'] === \UPLOAD_ERR_NO_FILE) {
             if (isset($source['submittedFile']['resourcePointer'])) {
                 try {
+                    // File references use numeric resource pointers, direct
+                    // file relations are using "file:" prefix (e.g. "file:5")
                     $resourcePointer = $this->hashService->validateAndStripHmac($source['submittedFile']['resourcePointer']);
                     if (strpos($resourcePointer, 'file:') === 0) {
                     $resourcePointer = $this->hashService->validateAndStripHmac($source['submittedFile']['resourcePointer']);
                     if (strpos($resourcePointer, 'file:') === 0) {
-                        $fileUid = substr($resourcePointer, 5);
+                        $fileUid = (int)substr($resourcePointer, 5);
                         return $this->createFileReferenceFromFalFileObject($this->resourceFactory->getFileObject($fileUid));
                     }
                         return $this->createFileReferenceFromFalFileObject($this->resourceFactory->getFileObject($fileUid));
                     }
-                    return $this->createFileReferenceFromFalFileReferenceObject($this->resourceFactory->getFileReferenceObject($resourcePointer), $resourcePointer);
+                    return $this->createFileReferenceFromFalFileReferenceObject(
+                        $this->resourceFactory->getFileReferenceObject($resourcePointer),
+                        (int)$resourcePointer
+                    );
                 } catch (\InvalidArgumentException $e) {
                     // Nothing to do. No file is uploaded and resource pointer is invalid. Discard!
                 }
                 } catch (\InvalidArgumentException $e) {
                     // Nothing to do. No file is uploaded and resource pointer is invalid. Discard!
                 }
@@ -238,6 +245,10 @@ class UploadedFileReferenceConverter extends AbstractTypeConverter
     }
 
     /**
     }
 
     /**
+     * In case no $resourcePointer is given a new file reference domain object
+     * will be returned. Otherwise the file reference is reconstituted from
+     * storage and will be updated(!) with the provided $falFileReference.
+     *
      * @param CoreFileReference $falFileReference
      * @param int $resourcePointer
      * @return ExtbaseFileReference
      * @param CoreFileReference $falFileReference
      * @param int $resourcePointer
      * @return ExtbaseFileReference
@@ -265,23 +276,32 @@ class UploadedFileReferenceConverter extends AbstractTypeConverter
      */
     protected function getUploadErrorMessage(int $errorCode): string
     {
      */
     protected function getUploadErrorMessage(int $errorCode): string
     {
+        $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
         switch ($errorCode) {
             case \UPLOAD_ERR_INI_SIZE:
         switch ($errorCode) {
             case \UPLOAD_ERR_INI_SIZE:
-                return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
+                $logger->error('The uploaded file exceeds the upload_max_filesize directive in php.ini.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530345', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
             case \UPLOAD_ERR_FORM_SIZE:
             case \UPLOAD_ERR_FORM_SIZE:
-                return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
+                $logger->error('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530345', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
             case \UPLOAD_ERR_PARTIAL:
             case \UPLOAD_ERR_PARTIAL:
-                return 'The uploaded file was only partially uploaded';
+                $logger->error('The uploaded file was only partially uploaded.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530346', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
             case \UPLOAD_ERR_NO_FILE:
             case \UPLOAD_ERR_NO_FILE:
-                return 'No file was uploaded';
+                $logger->error('No file was uploaded.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530347', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
             case \UPLOAD_ERR_NO_TMP_DIR:
             case \UPLOAD_ERR_NO_TMP_DIR:
-                return 'Missing a temporary folder';
+                $logger->error('Missing a temporary folder.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
             case \UPLOAD_ERR_CANT_WRITE:
             case \UPLOAD_ERR_CANT_WRITE:
-                return 'Failed to write file to disk';
+                $logger->error('Failed to write file to disk.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
             case \UPLOAD_ERR_EXTENSION:
             case \UPLOAD_ERR_EXTENSION:
-                return 'File upload stopped by extension';
+                $logger->error('File upload stopped by extension.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
             default:
             default:
-                return 'Unknown upload error';
+                $logger->error('Unknown upload error.', []);
+                return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
         }
     }
 }
         }
     }
 }