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