[BUGFIX] Form localization depending on old class names
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Request.php
1 <?php
2 namespace TYPO3\CMS\Form;
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\Core\Utility\GeneralUtility;
18
19 /**
20 * Request Handler for Form
21 *
22 * @author Patrick Broens <patrick@patrickbroens.nl>
23 */
24 class Request implements \TYPO3\CMS\Core\SingletonInterface {
25
26 /**
27 * Prefix for the name attributes
28 * of the superglobals $_POST and $_GET.
29 *
30 * @var string
31 */
32 protected $prefix = 'tx_form';
33
34 /**
35 * Method used for submitting data
36 * Can be "get" or "post"
37 *
38 * @var string
39 */
40 protected $method = 'post';
41
42 /**
43 * Session data
44 *
45 * Will only be stored when the form has been submitted successfully
46 *
47 * @var array
48 */
49 protected $sessionData = array();
50
51 /**
52 * TypoScript Frontend Controller
53 *
54 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
55 */
56 protected $frontendController;
57
58
59 /**
60 * Constructor
61 */
62 public function __construct() {
63 $this->frontendController = $GLOBALS['TSFE'];
64 }
65
66 /**
67 * Set the prefix used in the form
68 * If prefix is available in configuration array of the object, it will take it from there.
69 * Otherwise if not given at all, it will set it to default
70 *
71 * @param string $prefix Value containing characters a-z, A-Z, 0-9, _ and -
72 * @return void
73 */
74 public function setPrefix($prefix = 'tx_form') {
75 if (empty($prefix)) {
76 $prefix = 'tx_form';
77 }
78 $prefix = preg_replace('/\\s/', '_', (string)$prefix);
79 $this->prefix = preg_replace('/[^a-zA-Z0-9_\\-]/', '', $prefix);
80 }
81
82 /**
83 * Get the prefix
84 *
85 * @return string
86 */
87 public function getPrefix() {
88 return $this->prefix;
89 }
90
91 /**
92 * Set the method used for submitting the data
93 * When used right it will only allow data send by the right method
94 *
95 * @param string $method The method
96 * @return void
97 */
98 public function setMethod($method = 'get') {
99 $allowedMethods = array(
100 'post',
101 'get',
102 'session'
103 );
104 $method = strtolower((string)$method);
105 if ($this->frontendController->loginUser) {
106 $this->sessionData = $this->frontendController->fe_user->getKey('user', $this->prefix);
107 } else {
108 $this->sessionData = $this->frontendController->fe_user->getKey('ses', $this->prefix);
109 }
110 if (!empty($this->sessionData)) {
111 $method = 'session';
112 }
113 if (!in_array($method, $allowedMethods)) {
114 $method = 'post';
115 }
116 $this->method = $method;
117 }
118
119 /**
120 * Returns the method of this request handler
121 *
122 * @return string
123 */
124 public function getMethod() {
125 return $this->method;
126 }
127
128 /**
129 * Access values contained in the superglobals as public members
130 * POST and GET are filtered by prefix of the form
131 * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV
132 *
133 * @param string $key Parameter to look for
134 * @return mixed
135 */
136 public function get($key) {
137 switch (TRUE) {
138 case $this->method === 'get' && isset($_GET[$this->prefix][$key]):
139 return $_GET[$this->prefix][$key];
140 case $this->method === 'post' && isset($_POST[$this->prefix][$key]):
141 return $_POST[$this->prefix][$key];
142 case $this->method === 'session' && isset($this->sessionData[$key]):
143 return $this->sessionData[$key];
144 default:
145 return NULL;
146 }
147 }
148
149 /**
150 * Check to see if a property is set
151 *
152 * @param string $key Parameter to look for
153 * @return bool
154 */
155 public function has($key) {
156 switch (TRUE) {
157 case $this->method === 'get' && isset($_GET[$this->prefix][$key]):
158 return TRUE;
159 case $this->method === 'post' && isset($_POST[$this->prefix][$key]):
160 return TRUE;
161 case $this->method === 'session' && isset($this->sessionData[$key]):
162 return TRUE;
163 default:
164 return FALSE;
165 }
166 }
167
168 /**
169 * Check to see if there is a request
170 *
171 * @return bool
172 */
173 public function hasRequest() {
174 switch (TRUE) {
175 case $this->method === 'get' && isset($_GET[$this->prefix]):
176 return TRUE;
177 case $this->method === 'post' && isset($_POST[$this->prefix]):
178 return TRUE;
179 case $this->method === 'session' && !empty($this->sessionData):
180 return TRUE;
181 default:
182 return FALSE;
183 }
184 }
185
186 /**
187 * Retrieve a member of the $_GET superglobal within the prefix
188 *
189 * If no $key is passed, returns the entire $_GET array within the prefix.
190 *
191 * @param string $key Parameter to search for
192 * @param mixed $default Default value to use if key not found
193 * @return mixed Returns NULL if key does not exist
194 */
195 public function getQuery($key = NULL, $default = NULL) {
196 if ($key === NULL) {
197 return $_GET[$this->prefix];
198 }
199 return isset($_GET[$this->prefix][$key]) ? $_GET[$this->prefix][$key] : $default;
200 }
201
202 /**
203 * Retrieve a member of the $_POST superglobal within the prefix
204 *
205 * If no $key is passed, returns the entire $_POST array within the prefix.
206 *
207 * @param string $key Parameter to search for
208 * @param mixed $default Default value to use if key not found
209 * @return mixed Returns NULL if key does not exist
210 */
211 public function getPost($key = NULL, $default = NULL) {
212 if ($key === NULL) {
213 return $_POST[$this->prefix];
214 }
215 return isset($_POST[$this->prefix][$key]) ? $_POST[$this->prefix][$key] : $default;
216 }
217
218 /**
219 * Retrieve a member of the $sessionData variable
220 *
221 * If no $key is passed, returns the entire $sessionData array
222 *
223 * @param string $key Parameter to search for
224 * @param mixed $default Default value to use if key not found
225 * @return mixed Returns NULL if key does not exist
226 */
227 public function getSession($key = NULL, $default = NULL) {
228 if ($key === NULL) {
229 return $this->sessionData;
230 }
231 return isset($this->sessionData[$key]) ? $this->sessionData[$key] : $default;
232 }
233
234 /**
235 * Retrieve a member of the $_POST or $_GET superglobals or session data
236 * according to the used method.
237 *
238 * If no $key is passed, it returns the entire method array within the prefix.
239 *
240 * @param string $key The member name
241 * @param string $default Default value if there is no $_POST, $_GET or session variable
242 * @return mixed The member, or FALSE when wrong method is used
243 */
244 public function getByMethod($key = NULL, $default = NULL) {
245 if ($this->method === 'get') {
246 return $this->getQuery($key, $default);
247 } elseif ($this->method === 'post') {
248 return $this->getPost($key, $default);
249 } elseif ($this->method === 'session') {
250 return $this->getSession($key, $default);
251 } else {
252 return FALSE;
253 }
254 }
255
256 /**
257 * Store the form input in a session
258 *
259 * @return void
260 */
261 public function storeSession() {
262 if ($this->frontendController->loginUser) {
263 $this->frontendController->fe_user->setKey('user', $this->prefix, $this->getByMethod());
264 } else {
265 $this->frontendController->fe_user->setKey('ses', $this->prefix, $this->getByMethod());
266 }
267 $this->frontendController->storeSessionData();
268 }
269
270 /**
271 * Destroy the session data for the form
272 *
273 * @return void
274 */
275 public function destroySession() {
276 $this->removeFiles();
277 if ($this->frontendController->loginUser) {
278 $this->frontendController->fe_user->setKey('user', $this->prefix, NULL);
279 } else {
280 $this->frontendController->fe_user->setKey('ses', $this->prefix, NULL);
281 }
282 $this->frontendController->storeSessionData();
283 }
284
285 /**
286 * Store uploaded files in the typo3temp and return the information of those
287 * files
288 *
289 * @return void
290 */
291 public function storeFiles() {
292 $formData = $this->getByMethod();
293 if (isset($_FILES[$this->prefix]) && is_array($_FILES[$this->prefix])) {
294 foreach ($_FILES[$this->prefix]['tmp_name'] as $fieldName => $uploadedFile) {
295 if (is_uploaded_file($uploadedFile)) {
296 $tempFilename = GeneralUtility::upload_to_tempfile($uploadedFile);
297 if (TYPO3_OS === 'WIN') {
298 $tempFilename = GeneralUtility::fixWindowsFilePath($tempFilename);
299 }
300 if ($tempFilename !== '') {
301 // Use finfo to get the mime type
302 $finfo = finfo_open(FILEINFO_MIME_TYPE);
303 $mimeType = finfo_file($finfo, $tempFilename);
304 finfo_close($finfo);
305 $formData[$fieldName] = array(
306 'tempFilename' => $tempFilename,
307 'originalFilename' => $_FILES[$this->prefix]['name'][$fieldName],
308 'type' => $mimeType,
309 'size' => (int)$_FILES[$this->prefix]['size'][$fieldName]
310 );
311 }
312 }
313 }
314 }
315 switch ($this->getMethod()) {
316 case 'post':
317 $_POST[$this->prefix] = $formData;
318 break;
319 case 'get':
320 $_GET[$this->prefix] = $formData;
321 break;
322 case 'session':
323 $this->sessionData = $formData;
324 break;
325 }
326 }
327
328 /**
329 * Remove uploaded files from the typo3temp
330 *
331 * @return void
332 */
333 protected function removeFiles() {
334 $values = $this->getByMethod();
335 if (is_array($values)) {
336 foreach ($values as $value) {
337 if (is_array($value) && isset($value['tempFilename'])) {
338 GeneralUtility::unlink_tempfile($value['tempFilename']);
339 }
340 }
341 }
342 }
343
344 }