2 namespace TYPO3\CMS\Extbase\Mvc\Web
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
;
18 use TYPO3\CMS\Extbase\Mvc\Exception
as MvcException
;
21 * Builds a web request.
23 class RequestBuilder
implements \TYPO3\CMS\Core\SingletonInterface
26 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
28 protected $objectManager;
31 * This is the vendor name of the extension
35 protected $vendorName;
38 * This is a unique key for a plugin (not the extension key!)
42 protected $pluginName = 'plugin';
45 * The name of the extension (in UpperCamelCase)
49 protected $extensionName;
52 * The default controller name
56 protected $defaultControllerName = '';
59 * The default format of the response object
63 protected $defaultFormat = 'html';
66 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
70 protected $allowedControllerActions = [];
73 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
75 protected $configurationManager;
78 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
80 protected $extensionService;
83 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
85 protected $environmentService;
88 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
90 public function injectObjectManager(\TYPO3\CMS\Extbase\
Object\ObjectManagerInterface
$objectManager)
92 $this->objectManager
= $objectManager;
96 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
98 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
$configurationManager)
100 $this->configurationManager
= $configurationManager;
104 * @param \TYPO3\CMS\Extbase\Service\ExtensionService $extensionService
106 public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService
$extensionService)
108 $this->extensionService
= $extensionService;
112 * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
114 public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService
$environmentService)
116 $this->environmentService
= $environmentService;
120 * @throws MvcException
123 protected function loadDefaultValues()
125 $configuration = $this->configurationManager
->getConfiguration(ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
);
126 if (empty($configuration['extensionName'])) {
127 throw new MvcException('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
129 if (empty($configuration['pluginName'])) {
130 throw new MvcException('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
132 if (!empty($configuration['vendorName'])) {
133 $this->vendorName
= $configuration['vendorName'];
135 $this->vendorName
= null;
137 $this->extensionName
= $configuration['extensionName'];
138 $this->pluginName
= $configuration['pluginName'];
139 $this->defaultControllerName
= (string)current(array_keys($configuration['controllerConfiguration']));
140 $this->allowedControllerActions
= [];
141 foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
142 $this->allowedControllerActions
[$controllerName] = $controllerActions['actions'];
144 if (!empty($configuration['format'])) {
145 $this->defaultFormat
= $configuration['format'];
150 * Builds a web request object from the raw HTTP information and the configuration
152 * @return \TYPO3\CMS\Extbase\Mvc\Web\Request The web request as an object
154 public function build()
156 $this->loadDefaultValues();
157 $pluginNamespace = $this->extensionService
->getPluginNamespace($this->extensionName
, $this->pluginName
);
158 $parameters = \TYPO3\CMS\Core\Utility\GeneralUtility
::_GPmerged($pluginNamespace);
159 $files = $this->untangleFilesArray($_FILES);
160 if (isset($files[$pluginNamespace]) && is_array($files[$pluginNamespace])) {
161 $parameters = \TYPO3\CMS\Extbase\Utility\ArrayUtility
::arrayMergeRecursiveOverrule($parameters, $files[$pluginNamespace]);
163 $controllerName = $this->resolveControllerName($parameters);
164 $actionName = $this->resolveActionName($controllerName, $parameters);
165 /** @var $request \TYPO3\CMS\Extbase\Mvc\Web\Request */
166 $request = $this->objectManager
->get(\TYPO3\CMS\Extbase\Mvc\Web\Request
::class);
167 if ($this->vendorName
!== null) {
168 $request->setControllerVendorName($this->vendorName
);
170 $request->setPluginName($this->pluginName
);
171 $request->setControllerExtensionName($this->extensionName
);
172 $request->setControllerName($controllerName);
173 $request->setControllerActionName($actionName);
174 $request->setRequestUri(\TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_REQUEST_URL'));
175 $request->setBaseUri(\TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_SITE_URL'));
176 $request->setMethod($this->environmentService
->getServerRequestMethod());
177 if (is_string($parameters['format']) && $parameters['format'] !== '') {
178 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING
));
180 $request->setFormat($this->defaultFormat
);
182 foreach ($parameters as $argumentName => $argumentValue) {
183 $request->setArgument($argumentName, $argumentValue);
189 * Returns the current ControllerName extracted from given $parameters.
190 * If no controller is specified, the defaultControllerName will be returned.
191 * If that's not available, an exception is thrown.
193 * @param array $parameters
194 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
195 * @throws MvcException if the controller could not be resolved
196 * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
199 protected function resolveControllerName(array $parameters)
201 if (!isset($parameters['controller']) ||
$parameters['controller'] === '') {
202 if (empty($this->defaultControllerName
)) {
203 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);
205 return $this->defaultControllerName
;
207 $allowedControllerNames = array_keys($this->allowedControllerActions
);
208 if (!in_array($parameters['controller'], $allowedControllerNames)) {
209 $configuration = $this->configurationManager
->getConfiguration(ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
);
210 if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (bool)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
211 throw new \TYPO3\CMS\Core\Error\Http\
PageNotFoundException('The requested resource was not found', 1313857897);
212 } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
213 return $this->defaultControllerName
;
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.',
220 return filter_var($parameters['controller'], FILTER_SANITIZE_STRING
);
224 * Returns the current actionName extracted from given $parameters.
225 * If no action is specified, the defaultActionName will be returned.
226 * If that's not available or the specified action is not defined in the current plugin, an exception is thrown.
228 * @param string $controllerName
229 * @param array $parameters
230 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException
231 * @throws MvcException
232 * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
235 protected function resolveActionName($controllerName, array $parameters)
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);
242 return $defaultActionName;
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 } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (bool)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
251 return $defaultActionName;
253 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 return filter_var($actionName, FILTER_SANITIZE_STRING
);
259 * Transforms the convoluted _FILES superglobal into a manageable form.
261 * @param array $convolutedFiles The _FILES superglobal
262 * @return array Untangled files
263 * @see TYPO3\Flow\Utility\Environment
265 protected function untangleFilesArray(array $convolutedFiles)
267 $untangledFiles = [];
269 foreach ($convolutedFiles as $firstLevelFieldName => $fieldInformation) {
270 if (!is_array($fieldInformation['error'])) {
271 $fieldPaths[] = [$firstLevelFieldName];
273 $newFieldPaths = $this->calculateFieldPaths($fieldInformation['error'], $firstLevelFieldName);
274 array_walk($newFieldPaths, function (&$value, $key) {
275 $value = explode('/', $value);
277 $fieldPaths = array_merge($fieldPaths, $newFieldPaths);
280 foreach ($fieldPaths as $fieldPath) {
281 if (count($fieldPath) === 1) {
282 $fileInformation = $convolutedFiles[$fieldPath[0]];
284 $fileInformation = [];
285 foreach ($convolutedFiles[$fieldPath[0]] as $key => $subStructure) {
286 $fileInformation[$key] = \TYPO3\CMS\Extbase\Utility\ArrayUtility
::getValueByPath($subStructure, array_slice($fieldPath, 1));
289 $untangledFiles = \TYPO3\CMS\Extbase\Utility\ArrayUtility
::setValueByPath($untangledFiles, $fieldPath, $fileInformation);
291 return $untangledFiles;
295 * Returns an array of all possibles "field paths" for the given array.
297 * @param array $structure The array to walk through
298 * @param string $firstLevelFieldName
299 * @return array An array of paths (as strings) in the format "key1/key2/key3" ...
301 protected function calculateFieldPaths(array $structure, $firstLevelFieldName = null)
304 if (is_array($structure)) {
305 foreach ($structure as $key => $subStructure) {
306 $fieldPath = ($firstLevelFieldName !== null ?
$firstLevelFieldName . '/' : '') . $key;
307 if (is_array($subStructure)) {
308 foreach ($this->calculateFieldPaths($subStructure) as $subFieldPath) {
309 $fieldPaths[] = $fieldPath . '/' . $subFieldPath;
312 $fieldPaths[] = $fieldPath;