Extbase:
[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 script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Builds a web request.
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 *
32 * @scope prototype
33 */
34 class Tx_Extbase_MVC_Web_RequestBuilder {
35
36 /**
37 * This is a unique key for a plugin (not the extension key!)
38 *
39 * @var string
40 **/
41 protected $pluginKey = 'plugin';
42
43 /**
44 * The name of the extension (in UpperCamelCase)
45 *
46 * @var string
47 **/
48 protected $extensionName = 'Extbase';
49
50 /**
51 * The default controller name
52 *
53 * @var string
54 **/
55 protected $defaultControllerName = 'Default';
56
57 /**
58 * The default action of the default controller
59 *
60 * @var string
61 **/
62 protected $defaultActionName = 'index';
63
64 /**
65 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
66 *
67 * @var array
68 **/
69 protected $allowedControllerActions;
70
71 public function initialize($configuration) {
72 if (!empty($configuration['pluginKey'])) {
73 $this->pluginKey = $configuration['pluginKey'];
74 }
75 if (!empty($configuration['extensionName'])) {
76 $this->extensionName = $configuration['extensionName'];
77 }
78 if (!empty($configuration['controller'])) {
79 $this->defaultControllerName = $configuration['controller'];
80 }
81 if (!empty($configuration['action'])) {
82 $this->defaultActionName = $configuration['action'];
83 }
84 $allowedControllerActions = array();
85 if (is_array($configuration['switchableControllerActions.'])) {
86 foreach ($configuration['switchableControllerActions.'] as $controllerConfiguration) {
87 $controllerActions = t3lib_div::trimExplode(',', $controllerConfiguration['actions']);
88 foreach ($controllerActions as $actionName) {
89 $allowedControllerActions[$controllerConfiguration['controller']][] = $actionName;
90 }
91 }
92 }
93 $this->allowedControllerActions = $allowedControllerActions;
94 }
95
96 /**
97 * Builds a web request object from the raw HTTP information and the configuration
98 *
99 * @return Tx_Extbase_MVC_Web_Request The web request as an object
100 */
101 public function build() {
102 $parameters = t3lib_div::_GET('tx_' . strtolower($this->extensionName) . '_' . strtolower($this->pluginKey));
103 if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
104 $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
105 $allowedActions = $this->allowedControllerActions[$controllerName];
106 if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
107 $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
108 } else {
109 $actionName = $this->defaultActionName;
110 }
111 } else {
112 $controllerName = $this->defaultControllerName;
113 $actionName = $this->defaultActionName;
114 }
115
116 $request = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Request');
117 $request->setPluginKey($this->pluginKey);
118 $request->setExtensionName($this->extensionName);
119 $request->setControllerName($controllerName);
120 $request->setControllerActionName($actionName);
121 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
122 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
123 // TODO Revise the GParrayMerged method
124 foreach (t3lib_div::GParrayMerged('tx_' . strtolower($this->extensionName) . '_' . strtolower($this->pluginKey)) as $key => $value) {
125 $request->setArgument($key, $value);
126 }
127 return $request;
128 }
129
130
131 }
132 ?>