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