‪TYPO3CMS  9.5
RequestBuilder.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Psr\Http\Message\ServerRequestInterface;
23 
29 {
33  protected ‪$objectManager;
34 
40  protected ‪$vendorName;
41 
47  protected ‪$pluginName = 'plugin';
48 
54  protected ‪$extensionName;
55 
61  protected ‪$defaultControllerName = '';
62 
68  protected ‪$defaultFormat = 'html';
69 
75  protected ‪$allowedControllerActions = [];
76 
81 
86 
90  protected ‪$environmentService;
91 
95  public function ‪injectObjectManager(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager)
96  {
97  $this->objectManager = ‪$objectManager;
98  }
99 
104  {
105  $this->configurationManager = ‪$configurationManager;
106  }
107 
111  public function ‪injectExtensionService(\‪TYPO3\CMS\‪Extbase\Service\ExtensionService ‪$extensionService)
112  {
113  $this->extensionService = ‪$extensionService;
114  }
115 
119  public function ‪injectEnvironmentService(\‪TYPO3\CMS\‪Extbase\Service\EnvironmentService ‪$environmentService)
120  {
121  $this->environmentService = ‪$environmentService;
122  }
123 
127  protected function ‪loadDefaultValues()
128  {
129  $configuration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
130  if (empty($configuration['extensionName'])) {
131  throw new ‪MvcException('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
132  }
133  if (empty($configuration['pluginName'])) {
134  throw new ‪MvcException('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
135  }
136  if (!empty($configuration['vendorName'])) {
137  $this->vendorName = $configuration['vendorName'];
138  } else {
139  $this->vendorName = null;
140  }
141  $this->extensionName = $configuration['extensionName'];
142  $this->pluginName = $configuration['pluginName'];
143  $this->defaultControllerName = (string)current(array_keys($configuration['controllerConfiguration']));
144  $this->allowedControllerActions = [];
145  foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
146  $this->allowedControllerActions[$controllerName] = $controllerActions['actions'] ?? null;
147  }
148  if (!empty($configuration['format'])) {
149  $this->defaultFormat = $configuration['format'];
150  }
151  }
157  public function ‪build()
158  {
159  $this->‪loadDefaultValues();
160  $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName);
162  $typo3Request = ‪$GLOBALS['TYPO3_REQUEST'] ?? null;
163  if ($typo3Request instanceof ServerRequestInterface) {
164  $queryArguments = $typo3Request->getAttribute('routing');
165  if ($queryArguments instanceof PageArguments) {
166  $getParameters = $queryArguments->get($pluginNamespace) ?? [];
167  } else {
168  $getParameters = $typo3Request->getQueryParams()[$pluginNamespace] ?? [];
169  }
170  $bodyParameters = $typo3Request->getParsedBody()[$pluginNamespace] ?? [];
171  $bodyParameters = is_array($bodyParameters) ? $bodyParameters : [];
172  $parameters = is_array($getParameters) ? $getParameters : [];
173  ‪ArrayUtility::mergeRecursiveWithOverrule($parameters, $bodyParameters);
174  } else {
175  $parameters = \TYPO3\CMS\Core\Utility\GeneralUtility::_GPmerged($pluginNamespace);
176  }
177 
178  $files = $this->‪untangleFilesArray($_FILES);
179  if (is_array($files[$pluginNamespace] ?? null)) {
180  $parameters = array_replace_recursive($parameters, $files[$pluginNamespace]);
181  }
182 
183  $controllerName = $this->‪resolveControllerName($parameters);
184  $actionName = $this->‪resolveActionName($controllerName, $parameters);
186  $request = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Web\Request::class);
187  if ($this->vendorName !== null) {
188  $request->setControllerVendorName($this->vendorName);
189  }
190  $request->setPluginName($this->pluginName);
191  $request->setControllerExtensionName($this->extensionName);
192  $request->setControllerName($controllerName);
193  $request->setControllerActionName($actionName);
194  // @todo Use Environment
195  $request->setRequestUri(\‪TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
196  $request->setBaseUri(\‪TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
197  $request->setMethod($this->environmentService->getServerRequestMethod());
198  if (isset($parameters['format']) && is_string($parameters['format']) && $parameters['format'] !== '') {
199  $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
200  } else {
201  $request->setFormat($this->defaultFormat);
202  }
203  foreach ($parameters as $argumentName => $argumentValue) {
204  $request->setArgument($argumentName, $argumentValue);
205  }
206  return $request;
207  }
208 
220  protected function ‪resolveControllerName(array $parameters)
221  {
222  if (!isset($parameters['controller']) || $parameters['controller'] === '') {
223  if (empty($this->defaultControllerName)) {
224  throw new ‪MvcException('The default controller for extension "' . $this->extensionName . '" and plugin "' . $this->pluginName . '" can not be determined. Please check for TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', 1316104317);
225  }
227  }
228  $allowedControllerNames = array_keys($this->allowedControllerActions);
229  if (!in_array($parameters['controller'], $allowedControllerNames)) {
230  $configuration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
231  if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
232  throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857897);
233  }
234  if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
236  }
237  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException(
238  'The controller "' . $parameters['controller'] . '" is not allowed by plugin "' . $this->pluginName . '". Please check for TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.',
239  1313855173
240  );
241  }
242  return filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
243  }
244 
257  protected function ‪resolveActionName($controllerName, array $parameters)
258  {
259  $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
260  if (!isset($parameters['action']) || $parameters['action'] === '') {
261  if ($defaultActionName === '') {
262  throw new ‪MvcException('The default action can not be determined for controller "' . $controllerName . '". Please check TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', 1295479651);
263  }
264  return $defaultActionName;
265  }
266  $actionName = $parameters['action'];
267  $allowedActionNames = $this->allowedControllerActions[$controllerName];
268  if (!in_array($actionName, $allowedActionNames)) {
269  $configuration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
270  if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
271  throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857898);
272  }
273  if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
274  return $defaultActionName;
275  }
276  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException('The action "' . $actionName . '" (controller "' . $controllerName . '") is not allowed by this plugin / module. Please check TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php / TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configureModule() in your ext_tables.php.', 1313855175);
277  }
278  return filter_var($actionName, FILTER_SANITIZE_STRING);
279  }
280 
287  protected function ‪untangleFilesArray(array $convolutedFiles)
288  {
289  $untangledFiles = [];
290  $fieldPaths = [];
291  foreach ($convolutedFiles as $firstLevelFieldName => $fieldInformation) {
292  if (!is_array($fieldInformation['error'])) {
293  $fieldPaths[] = [$firstLevelFieldName];
294  } else {
295  $newFieldPaths = $this->‪calculateFieldPaths($fieldInformation['error'], $firstLevelFieldName);
296  array_walk($newFieldPaths, function (&$value, $key) {
297  $value = explode('/', $value);
298  });
299  $fieldPaths = array_merge($fieldPaths, $newFieldPaths);
300  }
301  }
302  foreach ($fieldPaths as $fieldPath) {
303  if (count($fieldPath) === 1) {
304  $fileInformation = $convolutedFiles[$fieldPath[0]];
305  } else {
306  $fileInformation = [];
307  foreach ($convolutedFiles[$fieldPath[0]] as $key => $subStructure) {
308  try {
309  $fileInformation[$key] = ‪ArrayUtility::getValueByPath($subStructure, array_slice($fieldPath, 1));
310  } catch (MissingArrayPathException $e) {
311  // do nothing if the path is invalid
312  }
313  }
314  }
315  $untangledFiles = ‪ArrayUtility::setValueByPath($untangledFiles, $fieldPath, $fileInformation);
316  }
317  return $untangledFiles;
318  }
319 
327  protected function ‪calculateFieldPaths(array $structure, $firstLevelFieldName = null)
328  {
329  $fieldPaths = [];
330  if (is_array($structure)) {
331  foreach ($structure as $key => $subStructure) {
332  $fieldPath = ($firstLevelFieldName !== null ? $firstLevelFieldName . '/' : '') . $key;
333  if (is_array($subStructure)) {
334  foreach ($this->‪calculateFieldPaths($subStructure) as $subFieldPath) {
335  $fieldPaths[] = $fieldPath . '/' . $subFieldPath;
336  }
337  } else {
338  $fieldPaths[] = $fieldPath;
339  }
340  }
341  }
342  return $fieldPaths;
343  }
344 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:25
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$vendorName
‪string $vendorName
Definition: RequestBuilder.php:38
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException
Definition: MissingArrayPathException.php:26
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$pluginName
‪string $pluginName
Definition: RequestBuilder.php:44
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$defaultFormat
‪string $defaultFormat
Definition: RequestBuilder.php:62
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$defaultControllerName
‪string $defaultControllerName
Definition: RequestBuilder.php:56
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:22
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\resolveActionName
‪string resolveActionName($controllerName, array $parameters)
Definition: RequestBuilder.php:247
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: RequestBuilder.php:32
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$allowedControllerActions
‪array $allowedControllerActions
Definition: RequestBuilder.php:68
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\resolveControllerName
‪string resolveControllerName(array $parameters)
Definition: RequestBuilder.php:210
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\injectConfigurationManager
‪injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
Definition: RequestBuilder.php:93
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:23
‪TYPO3\CMS\Core\Utility\ArrayUtility\getValueByPath
‪static mixed getValueByPath(array $array, $path, $delimiter='/')
Definition: ArrayUtility.php:179
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\injectEnvironmentService
‪injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
Definition: RequestBuilder.php:109
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: RequestBuilder.php:85
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$extensionService
‪TYPO3 CMS Extbase Service ExtensionService $extensionService
Definition: RequestBuilder.php:76
‪TYPO3\CMS\Extbase\Mvc\Exception
Definition: AmbiguousCommandIdentifierException.php:2
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\build
‪TYPO3 CMS Extbase Mvc Web Request build()
Definition: RequestBuilder.php:147
‪TYPO3\CMS\Extbase\Mvc\Web
Definition: AbstractRequestHandler.php:2
‪TYPO3\CMS\Core\Utility\ArrayUtility\setValueByPath
‪static array setValueByPath(array $array, $path, $value, $delimiter='/')
Definition: ArrayUtility.php:271
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$configurationManager
‪TYPO3 CMS Extbase Configuration ConfigurationManagerInterface $configurationManager
Definition: RequestBuilder.php:72
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\calculateFieldPaths
‪array calculateFieldPaths(array $structure, $firstLevelFieldName=null)
Definition: RequestBuilder.php:317
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\loadDefaultValues
‪loadDefaultValues()
Definition: RequestBuilder.php:117
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\untangleFilesArray
‪array untangleFilesArray(array $convolutedFiles)
Definition: RequestBuilder.php:277
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\injectExtensionService
‪injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
Definition: RequestBuilder.php:101
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$environmentService
‪TYPO3 CMS Extbase Service EnvironmentService $environmentService
Definition: RequestBuilder.php:80
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
Definition: RequestBuilder.php:29
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder\$extensionName
‪string $extensionName
Definition: RequestBuilder.php:50