bc4fc0da14b961802289e9972d5ff83ae5168cc4
[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 $allowedMimeTypes = [];
58 $validators = [];
59 if (isset($renderable->getProperties()['allowedMimeTypes']) && \is_array($renderable->getProperties()['allowedMimeTypes'])) {
60 $allowedMimeTypes = array_filter($renderable->getProperties()['allowedMimeTypes']);
61 }
62 if (!empty($allowedMimeTypes)) {
63 $mimeTypeValidator = GeneralUtility::makeInstance(ObjectManager::class)
64 ->get(MimeTypeValidator::class, ['allowedMimeTypes' => $allowedMimeTypes]);
65 $validators = [$mimeTypeValidator];
66 }
67
68 $processingRule = $renderable->getRootForm()->getProcessingRule($renderable->getIdentifier());
69 foreach ($processingRule->getValidators() as $validator) {
70 if (!($validator instanceof NotEmptyValidator)) {
71 $validators[] = $validator;
72 $processingRule->removeValidator($validator);
73 }
74 }
75
76 $uploadConfiguration = [
77 UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS => $validators,
78 UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_CONFLICT_MODE => 'rename',
79 ];
80
81 $saveToFileMountIdentifier = $renderable->getProperties()['saveToFileMount'] ?? '';
82 if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
83 $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
84 } else {
85 $persistenceIdentifier = $renderable->getRootForm()->getPersistenceIdentifier();
86 if (!empty($persistenceIdentifier)) {
87 $pathinfo = PathUtility::pathinfo($persistenceIdentifier);
88 $saveToFileMountIdentifier = $pathinfo['dirname'];
89 if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
90 $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
91 }
92 }
93 }
94
95 $propertyMappingConfiguration->setTypeConverterOptions(UploadedFileReferenceConverter::class, $uploadConfiguration);
96 }
97 }
98
99 /**
100 * @param string $saveToFileMountIdentifier
101 * @return bool
102 * @internal
103 */
104 protected function checkSaveFileMountAccess(string $saveToFileMountIdentifier): bool
105 {
106 if (empty($saveToFileMountIdentifier)) {
107 return false;
108 }
109
110 $resourceFactory = GeneralUtility::makeInstance(ObjectManager::class)
111 ->get(ResourceFactory::class);
112
113 try {
114 $resourceFactory->getFolderObjectFromCombinedIdentifier($saveToFileMountIdentifier);
115 return true;
116 } catch (\InvalidArgumentException $e) {
117 return false;
118 }
119 }
120 }