[TASK] Use null coalescing operator where possible
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Mvc / Property / PropertyMappingConfiguration.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Mvc\Property;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Resource\ResourceFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\PathUtility;
21 use TYPO3\CMS\Extbase\Object\ObjectManager;
22 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
23 use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
24 use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
25 use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;
26 use TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator;
27
28 /**
29 * Scope: frontend
30 */
31 class PropertyMappingConfiguration
32 {
33
34 /**
35 * Set the property mapping configuration for the file upload element.
36 * * Add the UploadedFileReferenceConverter to convert an uploaded file to an
37 * FileReference.
38 * * Add the MimeTypeValidator to the UploadedFileReferenceConverter to
39 * delete non valid filetypes directly.
40 * * Setup the storage:
41 * If the property "saveToFileMount" exist for this element it will be used.
42 * If this file mount or the property "saveToFileMount" does not exist
43 * the folder in which the form definition lies (persistence identifier) will be used.
44 * If the form is generated programmatically and therefore no
45 * persistence identifier exist the default storage "1:/user_upload/" will be used.
46 *
47 * @param RenderableInterface $renderable
48 * @internal
49 * @todo: could we find a not so ugly solution for that?
50 */
51 public function afterBuildingFinished(RenderableInterface $renderable)
52 {
53 if ($renderable instanceof FileUpload) {
54 /** @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration */
55 $propertyMappingConfiguration = $renderable->getRootForm()->getProcessingRule($renderable->getIdentifier())->getPropertyMappingConfiguration();
56
57 $mimeTypeValidator = GeneralUtility::makeInstance(ObjectManager::class)
58 ->get(MimeTypeValidator::class, ['allowedMimeTypes' => $renderable->getProperties()['allowedMimeTypes']]);
59
60 $processingRule = $renderable->getRootForm()->getProcessingRule($renderable->getIdentifier());
61 $validators = [$mimeTypeValidator];
62 foreach ($processingRule->getValidators() as $validator) {
63 if (!($validator instanceof NotEmptyValidator)) {
64 $validators[] = $validator;
65 $processingRule->removeValidator($validator);
66 }
67 }
68
69 $uploadConfiguration = [
70 UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS => $validators,
71 UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_CONFLICT_MODE => 'rename',
72 ];
73
74 $saveToFileMountIdentifier = $renderable->getProperties()['saveToFileMount'] ?? '';
75 if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
76 $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
77 } else {
78 $persistenceIdentifier = $renderable->getRootForm()->getPersistenceIdentifier();
79 if (!empty($persistenceIdentifier)) {
80 $pathinfo = PathUtility::pathinfo($persistenceIdentifier);
81 $saveToFileMountIdentifier = $pathinfo['dirname'];
82 if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
83 $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
84 }
85 }
86 }
87
88 $propertyMappingConfiguration->setTypeConverterOptions(UploadedFileReferenceConverter::class, $uploadConfiguration);
89 }
90 }
91
92 /**
93 * @param string $saveToFileMountIdentifier
94 * @return bool
95 * @internal
96 */
97 protected function checkSaveFileMountAccess(string $saveToFileMountIdentifier): bool
98 {
99 if (empty($saveToFileMountIdentifier)) {
100 return false;
101 }
102
103 $resourceFactory = GeneralUtility::makeInstance(ObjectManager::class)
104 ->get(ResourceFactory::class);
105
106 try {
107 $resourceFactory->getFolderObjectFromCombinedIdentifier($saveToFileMountIdentifier);
108 return true;
109 } catch (\InvalidArgumentException $e) {
110 return false;
111 }
112 }
113 }