‪TYPO3CMS  10.4
FormDefinitionArrayConverter.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
30 
37 {
41  protected ‪$sourceTypes = ['string'];
42 
46  protected ‪$targetType = FormDefinitionArray::class;
47 
51  protected ‪$priority = 10;
52 
56  protected ‪$configurationService;
57 
69  public function ‪convertFrom($source, ‪$targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null)
70  {
71  $rawFormDefinitionArray = json_decode($source, true);
72 
73  if (json_last_error() !== JSON_ERROR_NONE) {
74  throw new ‪PropertyException('Unable to decode JSON source: ' . json_last_error_msg(), 1512578002);
75  }
76 
77  $formDefinitionValidationService = $this->‪getFormDefinitionValidationService();
78  $formDefinitionConversionService = $this->‪getFormDefinitionConversionService();
79 
80  // Extend the hmac hashing key with the "per form editor session (load / save)" unique key.
81  // @see \TYPO3\CMS\Form\Domain\Configuration\FormDefinitionConversionService::addHmacData
82  $sessionToken = $this->‪retrieveSessionToken();
83 
84  $prototypeName = $rawFormDefinitionArray['prototypeName'] ?? null;
85  $identifier = $rawFormDefinitionArray['identifier'] ?? null;
86 
87  // A modification of the properties "prototypeName" and "identifier" from the root form element
88  // through the form editor is always forbidden.
89  try {
90  if (!$formDefinitionValidationService->isPropertyValueEqualToHistoricalValue([$identifier, 'identifier'], $identifier, $rawFormDefinitionArray['_orig_identifier'] ?? [], $sessionToken)) {
91  throw new ‪PropertyException('Unauthorized modification of "identifier".', 1528538324);
92  }
93 
94  if (!$formDefinitionValidationService->isPropertyValueEqualToHistoricalValue([$identifier, 'prototypeName'], $prototypeName, $rawFormDefinitionArray['_orig_prototypeName'] ?? [], $sessionToken)) {
95  throw new ‪PropertyException('Unauthorized modification of "prototype name".', 1528538323);
96  }
97  } catch (‪PropertyException $e) {
98  throw new ‪PropertyException('Unauthorized modification of "prototype name" or "identifier".', 1528538322);
99  }
100 
101  $formDefinitionValidationService->validateFormDefinitionProperties($rawFormDefinitionArray, $prototypeName, $sessionToken);
102 
103  // @todo move all the transformations to FormDefinitionConversionService
104  $rawFormDefinitionArray = $this->‪filterEmptyArrays($rawFormDefinitionArray);
105  $rawFormDefinitionArray = $this->‪transformMultiValueElementsForFormFramework($rawFormDefinitionArray);
106  // @todo: replace with rte parsing
107  $rawFormDefinitionArray = ‪ArrayUtility::stripTagsFromValuesRecursive($rawFormDefinitionArray);
108  $rawFormDefinitionArray = $formDefinitionConversionService->removeHmacData($rawFormDefinitionArray);
109 
110  $formDefinitionArray = GeneralUtility::makeInstance(FormDefinitionArray::class, $rawFormDefinitionArray);
111  return $formDefinitionArray;
112  }
113 
133  protected function ‪transformMultiValueElementsForFormFramework(array $input): array
134  {
135  ‪$output = [];
136 
137  foreach ($input as $key => $value) {
138  if (is_int($key) && is_array($value) && isset($value['_label']) && isset($value['_value'])) {
139  $key = $value['_value'];
140  $value = $value['_label'];
141  }
142 
143  if (is_array($value)) {
145  } else {
146  ‪$output[$key] = $value;
147  }
148  }
149 
150  return ‪$output;
151  }
152 
160  protected function ‪filterEmptyArrays(array $array): array
161  {
162  foreach ($array as $key => $value) {
163  if (!is_array($value)) {
164  continue;
165  }
166  if (empty($value)) {
167  unset($array[$key]);
168  continue;
169  }
170  $array[$key] = $this->‪filterEmptyArrays($value);
171  if (empty($array[$key])) {
172  unset($array[$key]);
173  }
174  }
175 
176  return $array;
177  }
178 
182  protected function ‪retrieveSessionToken(): string
183  {
184  return $this->‪getBackendUser()->‪getSessionData('extFormProtectionSessionToken');
185  }
186 
191  {
192  return GeneralUtility::makeInstance(FormDefinitionValidationService::class);
193  }
194 
199  {
200  return GeneralUtility::makeInstance(FormDefinitionConversionService::class);
201  }
202 
206  protected function ‪getBackendUser(): ‪BackendUserAuthentication
207  {
208  return ‪$GLOBALS['BE_USER'];
209  }
210 }
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\$priority
‪int $priority
Definition: FormDefinitionArrayConverter.php:48
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinitionConversionService
Definition: FormDefinitionConversionService.php:35
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinitionValidationService
Definition: FormDefinitionValidationService.php:36
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\$configurationService
‪ConfigurationService $configurationService
Definition: FormDefinitionArrayConverter.php:52
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\filterEmptyArrays
‪array filterEmptyArrays(array $array)
Definition: FormDefinitionArrayConverter.php:156
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter
Definition: FormDefinitionArrayConverter.php:18
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\convertFrom
‪FormDefinitionArray convertFrom($source, $targetType, array $convertedChildProperties=[], PropertyMappingConfigurationInterface $configuration=null)
Definition: FormDefinitionArrayConverter.php:65
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:22
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\getFormDefinitionConversionService
‪FormDefinitionConversionService getFormDefinitionConversionService()
Definition: FormDefinitionArrayConverter.php:194
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\retrieveSessionToken
‪string retrieveSessionToken()
Definition: FormDefinitionArrayConverter.php:178
‪TYPO3\CMS\Form\Type\FormDefinitionArray
Definition: FormDefinitionArray.php:25
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getSessionData
‪mixed getSessionData($key)
Definition: AbstractUserAuthentication.php:1206
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Utility\ArrayUtility\stripTagsFromValuesRecursive
‪static array stripTagsFromValuesRecursive(array $array)
Definition: ArrayUtility.php:897
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService
Definition: ConfigurationService.php:51
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\transformMultiValueElementsForFormFramework
‪array transformMultiValueElementsForFormFramework(array $input)
Definition: FormDefinitionArrayConverter.php:129
‪TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
Definition: AbstractTypeConverter.php:34
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter
Definition: FormDefinitionArrayConverter.php:37
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\$sourceTypes
‪array< string > $sourceTypes
Definition: FormDefinitionArrayConverter.php:40
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\$targetType
‪string $targetType
Definition: FormDefinitionArrayConverter.php:44
‪TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException
Definition: PropertyException.php:26
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: FormDefinitionArrayConverter.php:202
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter\getFormDefinitionValidationService
‪FormDefinitionValidationService getFormDefinitionValidationService()
Definition: FormDefinitionArrayConverter.php:186