e199d92ad4de01615ab914fc6d1e360a7e77da9b
[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 * @inject
28 */
29 protected $objectManager;
30
31 /**
32 * This is the vendor name of the extension
33 *
34 * @var string
35 */
36 protected $vendorName;
37
38 /**
39 * This is a unique key for a plugin (not the extension key!)
40 *
41 * @var string
42 */
43 protected $pluginName = 'plugin';
44
45 /**
46 * The name of the extension (in UpperCamelCase)
47 *
48 * @var string
49 */
50 protected $extensionName;
51
52 /**
53 * The default controller name
54 *
55 * @var string
56 */
57 protected $defaultControllerName;
58
59 /**
60 * The default format of the response object
61 *
62 * @var string
63 */
64 protected $defaultFormat = 'html';
65
66 /**
67 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
68 *
69 * @var array
70 */
71 protected $allowedControllerActions = array();
72
73 /**
74 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
75 * @inject
76 */
77 protected $configurationManager;
78
79 /**
80 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
81 * @inject
82 */
83 protected $extensionService;
84
85 /**
86 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
87 * @inject
88 */
89 protected $environmentService;
90
91 /**
92 * @throws MvcException
93 * @return void
94 */
95 protected function loadDefaultValues() {
96 $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
97 if (empty($configuration['extensionName'])) {
98 throw new MvcException('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
99 }
100 if (empty($configuration['pluginName'])) {
101 throw new MvcException('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
102 }
103 if (!empty($configuration['vendorName'])) {
104 $this->vendorName = $configuration['vendorName'];
105 } else {
106 $this->vendorName = NULL;
107 }
108 $this->extensionName = $configuration['extensionName'];
109 $this->pluginName = $configuration['pluginName'];
110 $this->defaultControllerName = current(array_keys($configuration['controllerConfiguration']));
111 $this->allowedControllerActions = array();
112 foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
113 $this->allowedControllerActions[$controllerName] = $controllerActions['actions'];
114 }
115 if (!empty($configuration['format'])) {
116 $this->defaultFormat = $configuration['format'];
117 }
118 }
119
120 /**
121 * Builds a web request object from the raw HTTP information and the configuration
122 *
123 * @return \TYPO3\CMS\Extbase\Mvc\Web\Request The web request as an object
124 */
125 public function build() {
126 $this->loadDefaultValues();
127 $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName);
128 $parameters = \TYPO3\CMS\Core\Utility\GeneralUtility::_GPmerged($pluginNamespace);
129 $files = $this->untangleFilesArray($_FILES);
130 if (isset($files[$pluginNamespace]) && is_array($files[$pluginNamespace])) {
131 $parameters = \TYPO3\CMS\Extbase\Utility\ArrayUtility::arrayMergeRecursiveOverrule($parameters, $files[$pluginNamespace]);
132 }
133 $controllerName = $this->resolveControllerName($parameters);
134 $actionName = $this->resolveActionName($controllerName, $parameters);
135 /** @var $request \TYPO3\CMS\Extbase\Mvc\Web\Request */
136 $request = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
137 if ($this->vendorName !== NULL) {
138 $request->setControllerVendorName($this->vendorName);
139 }
140 $request->setPluginName($this->pluginName);
141 $request->setControllerExtensionName($this->extensionName);
142 $request->setControllerName($controllerName);
143 $request->setControllerActionName($actionName);
144 $request->setRequestUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
145 $request->setBaseUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
146 $request->setMethod($this->environmentService->getServerRequestMethod());
147 if (is_string($parameters['format']) && strlen($parameters['format'])) {
148 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
149 } else {
150 $request->setFormat($this->defaultFormat);
151 }
152 foreach ($parameters as $argumentName => $argumentValue) {
153 $request->setArgument($argumentName, $argumentValue);
154 }
155 return $request;
156 }
157
158 /**
159 * Returns the current ControllerName extracted from given $parameters.
160 * If no controller is specified, the defaultControllerName will be returned.
161 * If that's not available, an exception is thrown.
162 *
163 * @param array $parameters
164 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
165 * @throws MvcException if the controller could not be resolved
166 * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
167 * @return string
168 */
169 protected function resolveControllerName(array $parameters) {
170 if (!isset($parameters['controller']) || strlen($parameters['controller']) === 0) {
171 if (strlen($this->defaultControllerName) === 0) {
172 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);
173 }
174 return $this->defaultControllerName;
175 }
176 $allowedControllerNames = array_keys($this->allowedControllerActions);
177 if (!in_array($parameters['controller'], $allowedControllerNames)) {
178 $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
179 if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
180 throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857897);
181 } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
182 return $this->defaultControllerName;
183 }
184 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException('The controller "' . $parameters['controller'] . '" is not allowed by this plugin. Please check for TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', 1313855173);
185 }
186 return filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
187 }
188
189 /**
190 * Returns the current actionName extracted from given $parameters.
191 * If no action is specified, the defaultActionName will be returned.
192 * If that's not available or the specified action is not defined in the current plugin, an exception is thrown.
193 *
194 * @param string $controllerName
195 * @param array $parameters
196 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException
197 * @throws MvcException
198 * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
199 * @return string
200 */
201 protected function resolveActionName($controllerName, array $parameters) {
202 $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
203 if (!isset($parameters['action']) || strlen($parameters['action']) === 0) {
204 if (strlen($defaultActionName) === 0) {
205 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);
206 }
207 return $defaultActionName;
208 }
209 $actionName = $parameters['action'];
210 $allowedActionNames = $this->allowedControllerActions[$controllerName];
211 if (!in_array($actionName, $allowedActionNames)) {
212 $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
213 if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
214 throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857898);
215 } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
216 return $defaultActionName;
217 }
218 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);
219 }
220 return filter_var($actionName, FILTER_SANITIZE_STRING);
221 }
222
223 /**
224 * Transforms the convoluted _FILES superglobal into a manageable form.
225 *
226 * @param array $convolutedFiles The _FILES superglobal
227 * @return array Untangled files
228 * @see TYPO3\Flow\Utility\Environment
229 */
230 protected function untangleFilesArray(array $convolutedFiles) {
231 $untangledFiles = array();
232 $fieldPaths = array();
233 foreach ($convolutedFiles as $firstLevelFieldName => $fieldInformation) {
234 if (!is_array($fieldInformation['error'])) {
235 $fieldPaths[] = array($firstLevelFieldName);
236 } else {
237 $newFieldPaths = $this->calculateFieldPaths($fieldInformation['error'], $firstLevelFieldName);
238 array_walk($newFieldPaths, function (&$value, $key) {
239 $value = explode('/', $value);
240 });
241 $fieldPaths = array_merge($fieldPaths, $newFieldPaths);
242 }
243 }
244 foreach ($fieldPaths as $fieldPath) {
245 if (count($fieldPath) === 1) {
246 $fileInformation = $convolutedFiles[$fieldPath[0]];
247 } else {
248 $fileInformation = array();
249 foreach ($convolutedFiles[$fieldPath[0]] as $key => $subStructure) {
250 $fileInformation[$key] = \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($subStructure, array_slice($fieldPath, 1));
251 }
252 }
253 $untangledFiles = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($untangledFiles, $fieldPath, $fileInformation);
254 }
255 return $untangledFiles;
256 }
257
258 /**
259 * Returns an array of all possibles "field paths" for the given array.
260 *
261 * @param array $structure The array to walk through
262 * @param string $firstLevelFieldName
263 * @return array An array of paths (as strings) in the format "key1/key2/key3" ...
264 */
265 protected function calculateFieldPaths(array $structure, $firstLevelFieldName = NULL) {
266 $fieldPaths = array();
267 if (is_array($structure)) {
268 foreach ($structure as $key => $subStructure) {
269 $fieldPath = ($firstLevelFieldName !== NULL ? $firstLevelFieldName . '/' : '') . $key;
270 if (is_array($subStructure)) {
271 foreach ($this->calculateFieldPaths($subStructure) as $subFieldPath) {
272 $fieldPaths[] = $fieldPath . '/' . $subFieldPath;
273 }
274 } else {
275 $fieldPaths[] = $fieldPath;
276 }
277 }
278 }
279 return $fieldPaths;
280 }
281
282 }