24bc638bb7295b20cecdcf9e1f9bf922be43f14a
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Web / RequestBuilder.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Web;
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 TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
18 use TYPO3\CMS\Extbase\Mvc\Exception as MvcException;
19
20 /**
21 * Builds a web request.
22 */
23 class RequestBuilder implements \TYPO3\CMS\Core\SingletonInterface
24 {
25 /**
26 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
27 */
28 protected $objectManager;
29
30 /**
31 * This is the vendor name of the extension
32 *
33 * @var string
34 */
35 protected $vendorName;
36
37 /**
38 * This is a unique key for a plugin (not the extension key!)
39 *
40 * @var string
41 */
42 protected $pluginName = 'plugin';
43
44 /**
45 * The name of the extension (in UpperCamelCase)
46 *
47 * @var string
48 */
49 protected $extensionName;
50
51 /**
52 * The default controller name
53 *
54 * @var string
55 */
56 protected $defaultControllerName = '';
57
58 /**
59 * The default format of the response object
60 *
61 * @var string
62 */
63 protected $defaultFormat = 'html';
64
65 /**
66 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
67 *
68 * @var array
69 */
70 protected $allowedControllerActions = [];
71
72 /**
73 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
74 */
75 protected $configurationManager;
76
77 /**
78 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
79 */
80 protected $extensionService;
81
82 /**
83 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
84 */
85 protected $environmentService;
86
87 /**
88 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
89 */
90 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
91 {
92 $this->objectManager = $objectManager;
93 }
94
95 /**
96 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
97 */
98 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
99 {
100 $this->configurationManager = $configurationManager;
101 }
102
103 /**
104 * @param \TYPO3\CMS\Extbase\Service\ExtensionService $extensionService
105 */
106 public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
107 {
108 $this->extensionService = $extensionService;
109 }
110
111 /**
112 * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
113 */
114 public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
115 {
116 $this->environmentService = $environmentService;
117 }
118
119 /**
120 * @throws MvcException
121 */
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
148 /**
149 * Builds a web request object from the raw HTTP information and the configuration
150 *
151 * @return \TYPO3\CMS\Extbase\Mvc\Web\Request The web request as an object
152 */
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);
164 /** @var $request \TYPO3\CMS\Extbase\Mvc\Web\Request */
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
187 /**
188 * Returns the current ControllerName extracted from given $parameters.
189 * If no controller is specified, the defaultControllerName will be returned.
190 * If that's not available, an exception is thrown.
191 *
192 * @param array $parameters
193 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
194 * @throws MvcException if the controller could not be resolved
195 * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
196 * @return string
197 */
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 }
204 return $this->defaultControllerName;
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 } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
212 return $this->defaultControllerName;
213 }
214 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException(
215 '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.',
216 1313855173
217 );
218 }
219 return filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
220 }
221
222 /**
223 * Returns the current actionName extracted from given $parameters.
224 * If no action is specified, the defaultActionName will be returned.
225 * If that's not available or the specified action is not defined in the current plugin, an exception is thrown.
226 *
227 * @param string $controllerName
228 * @param array $parameters
229 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException
230 * @throws MvcException
231 * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
232 * @return string
233 */
234 protected function resolveActionName($controllerName, array $parameters)
235 {
236 $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
237 if (!isset($parameters['action']) || $parameters['action'] === '') {
238 if ($defaultActionName === '') {
239 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);
240 }
241 return $defaultActionName;
242 }
243 $actionName = $parameters['action'];
244 $allowedActionNames = $this->allowedControllerActions[$controllerName];
245 if (!in_array($actionName, $allowedActionNames)) {
246 $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
247 if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
248 throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857898);
249 } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
250 return $defaultActionName;
251 }
252 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);
253 }
254 return filter_var($actionName, FILTER_SANITIZE_STRING);
255 }
256
257 /**
258 * Transforms the convoluted _FILES superglobal into a manageable form.
259 *
260 * @param array $convolutedFiles The _FILES superglobal
261 * @return array Untangled files
262 * @see TYPO3\Flow\Utility\Environment
263 */
264 protected function untangleFilesArray(array $convolutedFiles)
265 {
266 $untangledFiles = [];
267 $fieldPaths = [];
268 foreach ($convolutedFiles as $firstLevelFieldName => $fieldInformation) {
269 if (!is_array($fieldInformation['error'])) {
270 $fieldPaths[] = [$firstLevelFieldName];
271 } else {
272 $newFieldPaths = $this->calculateFieldPaths($fieldInformation['error'], $firstLevelFieldName);
273 array_walk($newFieldPaths, function (&$value, $key) {
274 $value = explode('/', $value);
275 });
276 $fieldPaths = array_merge($fieldPaths, $newFieldPaths);
277 }
278 }
279 foreach ($fieldPaths as $fieldPath) {
280 if (count($fieldPath) === 1) {
281 $fileInformation = $convolutedFiles[$fieldPath[0]];
282 } else {
283 $fileInformation = [];
284 foreach ($convolutedFiles[$fieldPath[0]] as $key => $subStructure) {
285 try {
286 $fileInformation[$key] = \TYPO3\CMS\Core\Utility\ArrayUtility::getValueByPath($subStructure, array_slice($fieldPath, 1));
287 } catch (\RuntimeException $e) {
288 // do nothing if the path is invalid
289 }
290 }
291 }
292 $untangledFiles = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($untangledFiles, $fieldPath, $fileInformation);
293 }
294 return $untangledFiles;
295 }
296
297 /**
298 * Returns an array of all possibles "field paths" for the given array.
299 *
300 * @param array $structure The array to walk through
301 * @param string $firstLevelFieldName
302 * @return array An array of paths (as strings) in the format "key1/key2/key3" ...
303 */
304 protected function calculateFieldPaths(array $structure, $firstLevelFieldName = null)
305 {
306 $fieldPaths = [];
307 if (is_array($structure)) {
308 foreach ($structure as $key => $subStructure) {
309 $fieldPath = ($firstLevelFieldName !== null ? $firstLevelFieldName . '/' : '') . $key;
310 if (is_array($subStructure)) {
311 foreach ($this->calculateFieldPaths($subStructure) as $subFieldPath) {
312 $fieldPaths[] = $fieldPath . '/' . $subFieldPath;
313 }
314 } else {
315 $fieldPaths[] = $fieldPath;
316 }
317 }
318 }
319 return $fieldPaths;
320 }
321 }