TYPO3 CMS  TYPO3_6-2
RequestBuilder.php
Go to the documentation of this file.
1 <?php
3 
20 
25  protected $objectManager;
26 
32  protected $vendorName;
33 
39  protected $pluginName = 'plugin';
40 
46  protected $extensionName;
47 
54 
60  protected $defaultFormat = 'html';
61 
67  protected $allowedControllerActions = array();
68 
74 
79  protected $extensionService;
80 
86 
91  protected function loadDefaultValues() {
92  $configuration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
93  if (empty($configuration['extensionName'])) {
94  throw new \TYPO3\CMS\Extbase\Mvc\Exception('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
95  }
96  if (empty($configuration['pluginName'])) {
97  throw new \TYPO3\CMS\Extbase\Mvc\Exception('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
98  }
99  if (!empty($configuration['vendorName'])) {
100  $this->vendorName = $configuration['vendorName'];
101  } else {
102  $this->vendorName = NULL;
103  }
104  $this->extensionName = $configuration['extensionName'];
105  $this->pluginName = $configuration['pluginName'];
106  $this->defaultControllerName = current(array_keys($configuration['controllerConfiguration']));
107  $this->allowedControllerActions = array();
108  foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
109  $this->allowedControllerActions[$controllerName] = $controllerActions['actions'];
110  }
111  if (!empty($configuration['format'])) {
112  $this->defaultFormat = $configuration['format'];
113  }
114  }
115 
121  public function build() {
122  $this->loadDefaultValues();
123  $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName);
125  $files = $this->untangleFilesArray($_FILES);
126  if (isset($files[$pluginNamespace]) && is_array($files[$pluginNamespace])) {
128  }
129  $controllerName = $this->resolveControllerName($parameters);
130  $actionName = $this->resolveActionName($controllerName, $parameters);
132  $request = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
133  if ($this->vendorName !== NULL) {
134  $request->setControllerVendorName($this->vendorName);
135  }
136  $request->setPluginName($this->pluginName);
137  $request->setControllerExtensionName($this->extensionName);
138  $request->setControllerName($controllerName);
139  $request->setControllerActionName($actionName);
140  $request->setRequestUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
141  $request->setBaseUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
142  $request->setMethod($this->environmentService->getServerRequestMethod());
143  if (is_string($parameters['format']) && strlen($parameters['format'])) {
144  $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
145  } else {
146  $request->setFormat($this->defaultFormat);
147  }
148  foreach ($parameters as $argumentName => $argumentValue) {
149  $request->setArgument($argumentName, $argumentValue);
150  }
151  return $request;
152  }
153 
165  protected function resolveControllerName(array $parameters) {
166  if (!isset($parameters['controller']) || strlen($parameters['controller']) === 0) {
167  if (strlen($this->defaultControllerName) === 0) {
168  throw new \TYPO3\CMS\Extbase\Mvc\Exception('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);
169  }
171  }
172  $allowedControllerNames = array_keys($this->allowedControllerActions);
173  if (!in_array($parameters['controller'], $allowedControllerNames)) {
174  $configuration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
175  if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (boolean) $configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
176  throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857897);
177  } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (boolean) $configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
179  }
180  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);
181  }
182  return filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
183  }
184 
197  protected function resolveActionName($controllerName, array $parameters) {
198  $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
199  if (!isset($parameters['action']) || strlen($parameters['action']) === 0) {
200  if (strlen($defaultActionName) === 0) {
201  throw new \TYPO3\CMS\Extbase\Mvc\Exception('The default action can not be determined for controller "' . $controllerName . '". Please check TYPO3\\CMS\\Extbase\\Utility\\ExtensionUtility::configurePlugin() in your ext_localconf.php.', 1295479651);
202  }
203  return $defaultActionName;
204  }
205  $actionName = $parameters['action'];
206  $allowedActionNames = $this->allowedControllerActions[$controllerName];
207  if (!in_array($actionName, $allowedActionNames)) {
208  $configuration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
209  if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (boolean) $configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
210  throw new \TYPO3\CMS\Core\Error\Http\PageNotFoundException('The requested resource was not found', 1313857898);
211  } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (boolean) $configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
212  return $defaultActionName;
213  }
214  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);
215  }
216  return filter_var($actionName, FILTER_SANITIZE_STRING);
217  }
218 
226  protected function untangleFilesArray(array $convolutedFiles) {
227  $untangledFiles = array();
228  $fieldPaths = array();
229  foreach ($convolutedFiles as $firstLevelFieldName => $fieldInformation) {
230  if (!is_array($fieldInformation['error'])) {
231  $fieldPaths[] = array($firstLevelFieldName);
232  } else {
233  $newFieldPaths = $this->calculateFieldPaths($fieldInformation['error'], $firstLevelFieldName);
234  array_walk($newFieldPaths, function (&$value, $key) {
235  $value = explode('/', $value);
236  });
237  $fieldPaths = array_merge($fieldPaths, $newFieldPaths);
238  }
239  }
240  foreach ($fieldPaths as $fieldPath) {
241  if (count($fieldPath) === 1) {
242  $fileInformation = $convolutedFiles[$fieldPath[0]];
243  } else {
244  $fileInformation = array();
245  foreach ($convolutedFiles[$fieldPath[0]] as $key => $subStructure) {
246  $fileInformation[$key] = \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($subStructure, array_slice($fieldPath, 1));
247  }
248  }
249  $untangledFiles = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($untangledFiles, $fieldPath, $fileInformation);
250  }
251  return $untangledFiles;
252  }
253 
261  protected function calculateFieldPaths(array $structure, $firstLevelFieldName = NULL) {
262  $fieldPaths = array();
263  if (is_array($structure)) {
264  foreach ($structure as $key => $subStructure) {
265  $fieldPath = ($firstLevelFieldName !== NULL ? $firstLevelFieldName . '/' : '') . $key;
266  if (is_array($subStructure)) {
267  foreach ($this->calculateFieldPaths($subStructure) as $subFieldPath) {
268  $fieldPaths[] = $fieldPath . '/' . $subFieldPath;
269  }
270  } else {
271  $fieldPaths[] = $fieldPath;
272  }
273  }
274  }
275  return $fieldPaths;
276  }
277 }
resolveActionName($controllerName, array $parameters)
calculateFieldPaths(array $structure, $firstLevelFieldName=NULL)
$parameters
Definition: FileDumpEID.php:15
untangleFilesArray(array $convolutedFiles)
static arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, $dontAddNewKeys=FALSE, $emptyValuesOverride=TRUE)
static setValueByPath($subject, $path, $value)
static getValueByPath(array &$array, $path)