[TASK] Merge submodule dbal into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Request.php
1 <?php
2 namespace TYPO3\CMS\Form;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 Patrick Broens (patrick@patrickbroens.nl)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Request Handler for Form
29 *
30 * @author Patrick Broens <patrick@patrickbroens.nl>
31 */
32 class Request implements \TYPO3\CMS\Core\SingletonInterface {
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 $prefix = preg_replace('/\\s/', '_', (string) $prefix);
72 $this->prefix = preg_replace('/[^a-zA-Z0-9_\\-]/', '', $prefix);
73 }
74
75 /**
76 * Get the prefix
77 *
78 * @return string
79 */
80 public function getPrefix() {
81 return $this->prefix;
82 }
83
84 /**
85 * Set the method used for submitting the data
86 * When used right it will only allow data send by the right method
87 *
88 * @param string $method The method
89 * @return void
90 */
91 public function setMethod($method = 'get') {
92 $allowedMethods = array(
93 'post',
94 'get',
95 'session'
96 );
97 $method = strtolower((string) $method);
98 if ($GLOBALS['TSFE']->loginUser) {
99 $this->sessionData = $GLOBALS['TSFE']->fe_user->getKey('user', $this->prefix);
100 } else {
101 $this->sessionData = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->prefix);
102 }
103 if (!empty($this->sessionData)) {
104 $method = 'session';
105 }
106 if (!in_array($method, $allowedMethods)) {
107 $method = 'post';
108 }
109 $this->method = $method;
110 }
111
112 /**
113 * Returns the method of this request handler
114 *
115 * @return string
116 */
117 public function getMethod() {
118 return $this->method;
119 }
120
121 /**
122 * Access values contained in the superglobals as public members
123 * POST and GET are filtered by prefix of the form
124 * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV
125 *
126 * @param string $key Parameter to look for
127 * @return mixed
128 */
129 public function get($key) {
130 switch (TRUE) {
131 case $this->method === 'get' && isset($_GET[$this->prefix][$key]):
132 return $_GET[$this->prefix][$key];
133 case $this->method === 'post' && isset($_POST[$this->prefix][$key]):
134 return $_POST[$this->prefix][$key];
135 case $this->method === 'session' && isset($this->sessionData[$key]):
136 return $this->sessionData[$key];
137 default:
138 return NULL;
139 }
140 }
141
142 /**
143 * Check to see if a property is set
144 *
145 * @param string $key Parameter to look for
146 * @return boolean
147 */
148 public function has($key) {
149 switch (TRUE) {
150 case $this->method === 'get' && isset($_GET[$this->prefix][$key]):
151 return TRUE;
152 case $this->method === 'post' && isset($_POST[$this->prefix][$key]):
153 return TRUE;
154 case $this->method === 'session' && isset($this->sessionData[$key]):
155 return TRUE;
156 default:
157 return FALSE;
158 }
159 }
160
161 /**
162 * Check to see if there is a request
163 *
164 * @return boolean
165 */
166 public function hasRequest() {
167 switch (TRUE) {
168 case $this->method === 'get' && isset($_GET[$this->prefix]):
169 return TRUE;
170 case $this->method === 'post' && isset($_POST[$this->prefix]):
171 return TRUE;
172 case $this->method === 'session' && !empty($this->sessionData):
173 return TRUE;
174 default:
175 return FALSE;
176 }
177 }
178
179 /**
180 * Retrieve a member of the $_GET superglobal within the prefix
181 *
182 * If no $key is passed, returns the entire $_GET array within the prefix.
183 *
184 * @param string $key Parameter to search for
185 * @param mixed $default Default value to use if key not found
186 * @return mixed Returns NULL if key does not exist
187 */
188 public function getQuery($key = NULL, $default = NULL) {
189 if ($key === NULL) {
190 return $_GET[$this->prefix];
191 }
192 return isset($_GET[$this->prefix][$key]) ? $_GET[$this->prefix][$key] : $default;
193 }
194
195 /**
196 * Retrieve a member of the $_POST superglobal within the prefix
197 *
198 * If no $key is passed, returns the entire $_POST array within the prefix.
199 *
200 * @param string $key Parameter to search for
201 * @param mixed $default Default value to use if key not found
202 * @return mixed Returns NULL if key does not exist
203 */
204 public function getPost($key = NULL, $default = NULL) {
205 if ($key === NULL) {
206 return $_POST[$this->prefix];
207 }
208 return isset($_POST[$this->prefix][$key]) ? $_POST[$this->prefix][$key] : $default;
209 }
210
211 /**
212 * Retrieve a member of the $sessionData variable
213 *
214 * If no $key is passed, returns the entire $sessionData array
215 *
216 * @param string $key Parameter to search for
217 * @param mixed $default Default value to use if key not found
218 * @return mixed Returns NULL if key does not exist
219 */
220 public function getSession($key = NULL, $default = NULL) {
221 if ($key === NULL) {
222 return $this->sessionData;
223 }
224 return isset($this->sessionData[$key]) ? $this->sessionData[$key] : $default;
225 }
226
227 /**
228 * Retrieve a member of the $_POST or $_GET superglobals or session data
229 * according to the used method.
230 *
231 * If no $key is passed, it returns the entire method array within the prefix.
232 *
233 * @param string $key The member name
234 * @param string $default Default value if there is no $_POST, $_GET or session variable
235 * @return mixed The member, or FALSE when wrong method is used
236 */
237 public function getByMethod($key = NULL, $default = NULL) {
238 if ($this->method === 'get') {
239 return $this->getQuery($key, $default);
240 } elseif ($this->method === 'post') {
241 return $this->getPost($key, $default);
242 } elseif ($this->method === 'session') {
243 return $this->getSession($key, $default);
244 } else {
245 return FALSE;
246 }
247 }
248
249 /**
250 * Store the form input in a session
251 *
252 * @return void
253 */
254 public function storeSession() {
255 if ($GLOBALS['TSFE']->loginUser) {
256 $GLOBALS['TSFE']->fe_user->setKey('user', $this->prefix, $this->getByMethod());
257 } else {
258 $GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefix, $this->getByMethod());
259 }
260 $GLOBALS['TSFE']->storeSessionData();
261 }
262
263 /**
264 * Destroy the session data for the form
265 *
266 * @return void
267 */
268 public function destroySession() {
269 $this->removeFiles();
270 if ($GLOBALS['TSFE']->loginUser) {
271 $GLOBALS['TSFE']->fe_user->setKey('user', $this->prefix, NULL);
272 } else {
273 $GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefix, NULL);
274 }
275 $GLOBALS['TSFE']->storeSessionData();
276 }
277
278 /**
279 * Store uploaded files in the typo3temp and return the information of those
280 * files
281 *
282 * @return void
283 */
284 public function storeFiles() {
285 $formData = $this->getByMethod();
286 if (isset($_FILES[$this->prefix]) && is_array($_FILES[$this->prefix])) {
287 foreach ($_FILES[$this->prefix]['tmp_name'] as $fieldName => $uploadedFile) {
288 if (is_uploaded_file($uploadedFile)) {
289 $tempFilename = \TYPO3\CMS\Core\Utility\GeneralUtility::upload_to_tempfile($uploadedFile);
290 if (TYPO3_OS === 'WIN') {
291 $tempFilename = \TYPO3\CMS\Core\Utility\GeneralUtility::fixWindowsFilePath($tempFilename);
292 }
293 if ($tempFilename !== '') {
294 // Use finfo to get the mime type
295 $finfo = finfo_open(FILEINFO_MIME_TYPE);
296 $mimeType = finfo_file($finfo, $tempFilename);
297 finfo_close($finfo);
298 $formData[$fieldName] = array(
299 'tempFilename' => $tempFilename,
300 'originalFilename' => $_FILES[$this->prefix]['name'][$fieldName],
301 'type' => $mimeType,
302 'size' => (int) $_FILES[$this->prefix]['size'][$fieldName]
303 );
304 }
305 }
306 }
307 }
308 switch ($this->getMethod()) {
309 case 'post':
310 $_POST[$this->prefix] = $formData;
311 break;
312 case 'get':
313 $_GET[$this->prefix] = $formData;
314 break;
315 case 'session':
316 $this->sessionData = $formData;
317 break;
318 }
319 }
320
321 /**
322 * Remove uploaded files from the typo3temp
323 *
324 * @return void
325 */
326 protected function removeFiles() {
327 $values = $this->getByMethod();
328 if (is_array($values)) {
329 foreach ($values as $value) {
330 if (is_array($value) && isset($value['tempFilename'])) {
331 \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($value['tempFilename']);
332 }
333 }
334 }
335 }
336
337 }
338
339 ?>