TYPO3 CMS  TYPO3_6-2
Request.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Form;
3 
23 
30  protected $prefix = 'tx_form';
31 
38  protected $method = 'post';
39 
47  protected $sessionData = array();
48 
57  public function setPrefix($prefix = 'tx_form') {
58  if (empty($prefix)) {
59  $prefix = 'tx_form';
60  }
61  $prefix = preg_replace('/\\s/', '_', (string) $prefix);
62  $this->prefix = preg_replace('/[^a-zA-Z0-9_\\-]/', '', $prefix);
63  }
64 
70  public function getPrefix() {
71  return $this->prefix;
72  }
73 
81  public function setMethod($method = 'get') {
82  $allowedMethods = array(
83  'post',
84  'get',
85  'session'
86  );
87  $method = strtolower((string) $method);
88  if ($GLOBALS['TSFE']->loginUser) {
89  $this->sessionData = $GLOBALS['TSFE']->fe_user->getKey('user', $this->prefix);
90  } else {
91  $this->sessionData = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->prefix);
92  }
93  if (!empty($this->sessionData)) {
94  $method = 'session';
95  }
96  if (!in_array($method, $allowedMethods)) {
97  $method = 'post';
98  }
99  $this->method = $method;
100  }
101 
107  public function getMethod() {
108  return $this->method;
109  }
110 
119  public function get($key) {
120  switch (TRUE) {
121  case $this->method === 'get' && isset($_GET[$this->prefix][$key]):
122  return $_GET[$this->prefix][$key];
123  case $this->method === 'post' && isset($_POST[$this->prefix][$key]):
124  return $_POST[$this->prefix][$key];
125  case $this->method === 'session' && isset($this->sessionData[$key]):
126  return $this->sessionData[$key];
127  default:
128  return NULL;
129  }
130  }
131 
138  public function has($key) {
139  switch (TRUE) {
140  case $this->method === 'get' && isset($_GET[$this->prefix][$key]):
141  return TRUE;
142  case $this->method === 'post' && isset($_POST[$this->prefix][$key]):
143  return TRUE;
144  case $this->method === 'session' && isset($this->sessionData[$key]):
145  return TRUE;
146  default:
147  return FALSE;
148  }
149  }
150 
156  public function hasRequest() {
157  switch (TRUE) {
158  case $this->method === 'get' && isset($_GET[$this->prefix]):
159  return TRUE;
160  case $this->method === 'post' && isset($_POST[$this->prefix]):
161  return TRUE;
162  case $this->method === 'session' && !empty($this->sessionData):
163  return TRUE;
164  default:
165  return FALSE;
166  }
167  }
168 
178  public function getQuery($key = NULL, $default = NULL) {
179  if ($key === NULL) {
180  return $_GET[$this->prefix];
181  }
182  return isset($_GET[$this->prefix][$key]) ? $_GET[$this->prefix][$key] : $default;
183  }
184 
194  public function getPost($key = NULL, $default = NULL) {
195  if ($key === NULL) {
196  return $_POST[$this->prefix];
197  }
198  return isset($_POST[$this->prefix][$key]) ? $_POST[$this->prefix][$key] : $default;
199  }
200 
210  public function getSession($key = NULL, $default = NULL) {
211  if ($key === NULL) {
212  return $this->sessionData;
213  }
214  return isset($this->sessionData[$key]) ? $this->sessionData[$key] : $default;
215  }
216 
227  public function getByMethod($key = NULL, $default = NULL) {
228  if ($this->method === 'get') {
229  return $this->getQuery($key, $default);
230  } elseif ($this->method === 'post') {
231  return $this->getPost($key, $default);
232  } elseif ($this->method === 'session') {
233  return $this->getSession($key, $default);
234  } else {
235  return FALSE;
236  }
237  }
238 
244  public function storeSession() {
245  if ($GLOBALS['TSFE']->loginUser) {
246  $GLOBALS['TSFE']->fe_user->setKey('user', $this->prefix, $this->getByMethod());
247  } else {
248  $GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefix, $this->getByMethod());
249  }
250  $GLOBALS['TSFE']->storeSessionData();
251  }
252 
258  public function destroySession() {
259  $this->removeFiles();
260  if ($GLOBALS['TSFE']->loginUser) {
261  $GLOBALS['TSFE']->fe_user->setKey('user', $this->prefix, NULL);
262  } else {
263  $GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefix, NULL);
264  }
265  $GLOBALS['TSFE']->storeSessionData();
266  }
267 
274  public function storeFiles() {
275  $formData = $this->getByMethod();
276  if (isset($_FILES[$this->prefix]) && is_array($_FILES[$this->prefix])) {
277  foreach ($_FILES[$this->prefix]['tmp_name'] as $fieldName => $uploadedFile) {
278  if (
279  $_FILES[$this->prefix]['error'][$fieldName] !== UPLOAD_ERR_OK
280  || !is_uploaded_file($_FILES[$this->prefix]['tmp_name'][$fieldName])
281  ) {
282  unset($formData[$fieldName]);
283  continue;
284  }
285  # Remove items with blacklisted keys
286  $formData[$fieldName] = array_diff_key(
287  $formData[$fieldName],
288  array('tempFilename' => 1, 'originalFilename' => 1, 'type' => 1, 'size' => 1)
289  );
290 
291  if (is_uploaded_file($uploadedFile)) {
292  $tempFilename = \TYPO3\CMS\Core\Utility\GeneralUtility::upload_to_tempfile($uploadedFile);
293  if (TYPO3_OS === 'WIN') {
294  $tempFilename = \TYPO3\CMS\Core\Utility\GeneralUtility::fixWindowsFilePath($tempFilename);
295  }
296  if ($tempFilename !== '') {
297  // Use finfo to get the mime type
298  $finfo = finfo_open(FILEINFO_MIME_TYPE);
299  $mimeType = finfo_file($finfo, $tempFilename);
300  finfo_close($finfo);
301  $formData[$fieldName] = array(
302  'tempFilename' => $tempFilename,
303  'originalFilename' => $_FILES[$this->prefix]['name'][$fieldName],
304  'type' => $mimeType,
305  'size' => (int)$_FILES[$this->prefix]['size'][$fieldName]
306  );
307  }
308  }
309  }
310  }
311  switch ($this->getMethod()) {
312  case 'post':
313  $_POST[$this->prefix] = $formData;
314  break;
315  case 'get':
316  $_GET[$this->prefix] = $formData;
317  break;
318  case 'session':
319  $this->sessionData = $formData;
320  break;
321  }
322  }
323 
329  protected function removeFiles() {
330  $values = $this->getByMethod();
331  if (is_array($values)) {
332  foreach ($values as $value) {
333  if (is_array($value) && isset($value['tempFilename'])) {
335  }
336  }
337  }
338  }
339 
340 }
static unlink_tempfile($uploadedTempFileName)
getSession($key=NULL, $default=NULL)
Definition: Request.php:210
setPrefix($prefix='tx_form')
Definition: Request.php:57
getByMethod($key=NULL, $default=NULL)
Definition: Request.php:227
setMethod($method='get')
Definition: Request.php:81
getQuery($key=NULL, $default=NULL)
Definition: Request.php:178
getPost($key=NULL, $default=NULL)
Definition: Request.php:194
static upload_to_tempfile($uploadedFileName)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]