Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Web / RequestBuilder.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Builds a web request.
30 *
31 * @package Extbase
32 * @subpackage MVC\Web
33 * @version $ID:$
34 *
35 * @scope prototype
36 */
37 class Tx_Extbase_MVC_Web_RequestBuilder {
38
39 /**
40 * This is a unique key for a plugin (not the extension key!)
41 *
42 * @var string
43 */
44 protected $pluginName = 'plugin';
45
46 /**
47 * The name of the extension (in UpperCamelCase)
48 *
49 * @var string
50 */
51 protected $extensionName = 'Extbase';
52
53 /**
54 * The default controller name
55 *
56 * @var string
57 */
58 protected $defaultControllerName = 'Standard';
59
60 /**
61 * The default action of the default controller
62 *
63 * @var string
64 */
65 protected $defaultActionName = 'index';
66
67 /**
68 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
69 *
70 * @var array
71 */
72 protected $allowedControllerActions;
73
74 public function initialize($configuration) {
75 if (!empty($configuration['pluginName'])) {
76 $this->pluginName = $configuration['pluginName'];
77 }
78 if (!empty($configuration['extensionName'])) {
79 $this->extensionName = $configuration['extensionName'];
80 }
81 if (!empty($configuration['controller'])) {
82 $this->defaultControllerName = $configuration['controller'];
83 } elseif (is_array($configuration['switchableControllerActions'])) {
84 $firstControllerActions = current($configuration['switchableControllerActions']);
85 $this->defaultControllerName = $firstControllerActions['controller'];
86 }
87 if (!empty($configuration['action'])) {
88 $this->defaultActionName = $configuration['action'];
89 } elseif (is_array($configuration['switchableControllerActions'])) {
90 $firstControllerActions = current($configuration['switchableControllerActions']);
91 $this->defaultActionName = array_shift(t3lib_div::trimExplode(',', $firstControllerActions['actions'], TRUE));
92 }
93 $allowedControllerActions = array();
94 if (is_array($configuration['switchableControllerActions'])) {
95 foreach ($configuration['switchableControllerActions'] as $controllerConfiguration) {
96 $controllerActions = t3lib_div::trimExplode(',', $controllerConfiguration['actions']);
97 foreach ($controllerActions as $actionName) {
98 $allowedControllerActions[$controllerConfiguration['controller']][] = $actionName;
99 }
100 }
101 }
102 $this->allowedControllerActions = $allowedControllerActions;
103 }
104
105 /**
106 * Builds a web request object from the raw HTTP information and the configuration
107 *
108 * @return Tx_Extbase_MVC_Web_Request The web request as an object
109 */
110 public function build() {
111 $parameters = t3lib_div::_GPmerged('tx_' . strtolower($this->extensionName) . '_' . strtolower($this->pluginName));
112
113 if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
114 $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
115 $allowedActions = $this->allowedControllerActions[$controllerName];
116 if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
117 $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
118 } else {
119 $actionName = $this->defaultActionName;
120 }
121 } else {
122 $controllerName = $this->defaultControllerName;
123 $actionName = $this->defaultActionName;
124 }
125
126 $request = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Request');
127 $request->setPluginName($this->pluginName);
128 $request->setControllerExtensionName($this->extensionName);
129 $request->setControllerName($controllerName);
130 $request->setControllerActionName($actionName);
131 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
132 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
133 $request->setMethod((isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : NULL);
134
135 if (is_string($parameters['format']) && (strlen($parameters['format']))) {
136 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
137 }
138
139 foreach ($parameters as $argumentName => $argumentValue) {
140 $request->setArgument($argumentName, $argumentValue);
141 }
142
143 return $request;
144 }
145
146
147 }
148 ?>