TYPO3 CMS  TYPO3_8-7
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 
19 
24 {
28  protected $objectManager;
29 
35  protected $vendorName;
36 
42  protected $pluginName = 'plugin';
43 
49  protected $extensionName;
50 
56  protected $defaultControllerName = '';
57 
63  protected $defaultFormat = 'html';
64 
70  protected $allowedControllerActions = [];
71 
76 
80  protected $extensionService;
81 
86 
90  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
91  {
92  $this->objectManager = $objectManager;
93  }
94 
99  {
100  $this->configurationManager = $configurationManager;
101  }
102 
106  public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
107  {
108  $this->extensionService = $extensionService;
109  }
110 
114  public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
115  {
116  $this->environmentService = $environmentService;
117  }
118 
122  protected function loadDefaultValues()
123  {
124  $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
125  if (empty($configuration['extensionName'])) {
126  throw new MvcException('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
127  }
128  if (empty($configuration['pluginName'])) {
129  throw new MvcException('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
130  }
131  if (!empty($configuration['vendorName'])) {
132  $this->vendorName = $configuration['vendorName'];
133  } else {
134  $this->vendorName = null;
135  }
136  $this->extensionName = $configuration['extensionName'];
137  $this->pluginName = $configuration['pluginName'];
138  $this->defaultControllerName = (string)current(array_keys($configuration['controllerConfiguration']));
139  $this->allowedControllerActions = [];
140  foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
141  $this->allowedControllerActions[$controllerName] = $controllerActions['actions'];
142  }
143  if (!empty($configuration['format'])) {
144  $this->defaultFormat = $configuration['format'];
145  }
146  }
147 
153  public function build()
154  {
155  $this->loadDefaultValues();
156  $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName);
157  $parameters = \TYPO3\CMS\Core\Utility\GeneralUtility::_GPmerged($pluginNamespace);
158  $files = $this->untangleFilesArray($_FILES);
159  if (isset($files[$pluginNamespace]) && is_array($files[$pluginNamespace])) {
160  $parameters = array_replace_recursive($parameters, $files[$pluginNamespace]);
161  }
162  $controllerName = $this->resolveControllerName($parameters);
163  $actionName = $this->resolveActionName($controllerName, $parameters);
165  $request = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
166  if ($this->vendorName !== null) {
167  $request->setControllerVendorName($this->vendorName);
168  }
169  $request->setPluginName($this->pluginName);
170  $request->setControllerExtensionName($this->extensionName);
171  $request->setControllerName($controllerName);
172  $request->setControllerActionName($actionName);
173  $request->setRequestUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
174  $request->setBaseUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
175  $request->setMethod($this->environmentService->getServerRequestMethod());
176  if (is_string($parameters['format']) && $parameters['format'] !== '') {
177  $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
178  } else {
179  $request->setFormat($this->defaultFormat);
180  }
181  foreach ($parameters as $argumentName => $argumentValue) {
182  $request->setArgument($argumentName, $argumentValue);
183  }
184  return $request;
185  }
186 
198  protected function resolveControllerName(array $parameters)
199  {
200  if (!isset($parameters['controller']) || $parameters['controller'] === '') {
201  if (empty($this->defaultControllerName)) {
202  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);
203  }
205  }
206  $allowedControllerNames = array_keys($this->allowedControllerActions);
207  if (!in_array($parameters['controller'], $allowedControllerNames)) {
208  $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
209  if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
210  throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857897);
211  }
212  if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
214  }
215  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException(
216  '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.',
217  1313855173
218  );
219  }
220  return filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
221  }
222 
235  protected function resolveActionName($controllerName, array $parameters)
236  {
237  $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
238  if (!isset($parameters['action']) || $parameters['action'] === '') {
239  if ($defaultActionName === '') {
240  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);
241  }
242  return $defaultActionName;
243  }
244  $actionName = $parameters['action'];
245  $allowedActionNames = $this->allowedControllerActions[$controllerName];
246  if (!in_array($actionName, $allowedActionNames)) {
247  $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
248  if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
249  throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857898);
250  }
251  if (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
252  return $defaultActionName;
253  }
254  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException('The action "' . $actionName . '" (controller "' . $controllerName . '") is not allowed by this plugin. Please check TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', 1313855175);
255  }
256  return filter_var($actionName, FILTER_SANITIZE_STRING);
257  }
258 
266  protected function untangleFilesArray(array $convolutedFiles)
267  {
268  $untangledFiles = [];
269  $fieldPaths = [];
270  foreach ($convolutedFiles as $firstLevelFieldName => $fieldInformation) {
271  if (!is_array($fieldInformation['error'])) {
272  $fieldPaths[] = [$firstLevelFieldName];
273  } else {
274  $newFieldPaths = $this->calculateFieldPaths($fieldInformation['error'], $firstLevelFieldName);
275  array_walk($newFieldPaths, function (&$value, $key) {
276  $value = explode('/', $value);
277  });
278  $fieldPaths = array_merge($fieldPaths, $newFieldPaths);
279  }
280  }
281  foreach ($fieldPaths as $fieldPath) {
282  if (count($fieldPath) === 1) {
283  $fileInformation = $convolutedFiles[$fieldPath[0]];
284  } else {
285  $fileInformation = [];
286  foreach ($convolutedFiles[$fieldPath[0]] as $key => $subStructure) {
287  try {
288  $fileInformation[$key] = \TYPO3\CMS\Core\Utility\ArrayUtility::getValueByPath($subStructure, array_slice($fieldPath, 1));
289  } catch (\RuntimeException $e) {
290  // do nothing if the path is invalid
291  }
292  }
293  }
294  $untangledFiles = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($untangledFiles, $fieldPath, $fileInformation);
295  }
296  return $untangledFiles;
297  }
298 
306  protected function calculateFieldPaths(array $structure, $firstLevelFieldName = null)
307  {
308  $fieldPaths = [];
309  if (is_array($structure)) {
310  foreach ($structure as $key => $subStructure) {
311  $fieldPath = ($firstLevelFieldName !== null ? $firstLevelFieldName . '/' : '') . $key;
312  if (is_array($subStructure)) {
313  foreach ($this->calculateFieldPaths($subStructure) as $subFieldPath) {
314  $fieldPaths[] = $fieldPath . '/' . $subFieldPath;
315  }
316  } else {
317  $fieldPaths[] = $fieldPath;
318  }
319  }
320  }
321  return $fieldPaths;
322  }
323 }
resolveActionName($controllerName, array $parameters)
static setValueByPath(array $array, $path, $value, $delimiter='/')
static getValueByPath(array $array, $path, $delimiter='/')
injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
calculateFieldPaths(array $structure, $firstLevelFieldName=null)
injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
untangleFilesArray(array $convolutedFiles)