TYPO3 CMS  TYPO3_7-6
ServerRequestFactory.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Http;
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 
19 
28 {
36  public static function fromGlobals()
37  {
38  $serverParameters = $_SERVER;
39  $headers = static::prepareHeaders($serverParameters);
40 
41  $method = isset($serverParameters['REQUEST_METHOD']) ? $serverParameters['REQUEST_METHOD'] : 'GET';
42  $uri = new Uri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
43 
44  $request = new ServerRequest(
45  $uri,
46  $method,
47  'php://input',
48  $headers,
49  $serverParameters,
50  static::normalizeUploadedFiles($_FILES)
51  );
52 
53  if (!empty($_COOKIE)) {
54  $request = $request->withCookieParams($_COOKIE);
55  }
56  $queryParameters = GeneralUtility::_GET();
57  if (!empty($queryParameters)) {
58  $request = $request->withQueryParams($queryParameters);
59  }
60  $parsedBody = GeneralUtility::_POST();
61  if (!empty($parsedBody)) {
62  $request = $request->withParsedBody($parsedBody);
63  }
64  return $request;
65  }
66 
74  protected static function prepareHeaders(array $server)
75  {
76  $headers = [];
77  foreach ($server as $key => $value) {
78  if (strpos($key, 'HTTP_COOKIE') === 0) {
79  // Cookies are handled using the $_COOKIE superglobal
80  continue;
81  }
82  if (!empty($value)) {
83  if (strpos($key, 'HTTP_') === 0) {
84  $name = strtr(substr($key, 5), '_', ' ');
85  $name = strtr(ucwords(strtolower($name)), ' ', '-');
86  $name = strtolower($name);
87  $headers[$name] = $value;
88  } elseif (strpos($key, 'CONTENT_') === 0) {
89  $name = substr($key, 8); // Content-
90  $name = 'Content-' . (($name == 'MD5') ? $name : ucfirst(strtolower($name)));
91  $name = strtolower($name);
92  $headers[$name] = $value;
93  }
94  }
95  }
96  return $headers;
97  }
98 
108  protected static function normalizeUploadedFiles(array $files)
109  {
110  $normalizedFileUploads = [];
111  foreach ($files as $key => $value) {
112  if ($value instanceof UploadedFileInterface) {
113  $normalizedFileUploads[$key] = $value;
114  } elseif (is_array($value)) {
115  if (isset($value['tmp_name'])) {
116  $uploadedFiles = self::createUploadedFile($value);
117  if ($uploadedFiles) {
118  $normalizedFileUploads[$key] = $uploadedFiles;
119  }
120  } else {
121  $normalizedFileUploads[$key] = self::normalizeUploadedFiles($value);
122  }
123  } else {
124  throw new \InvalidArgumentException('Invalid value in files specification.', 1436717282);
125  }
126  }
127  return $normalizedFileUploads;
128  }
129 
139  protected static function createUploadedFile(array $value)
140  {
141  if (is_array($value['tmp_name'])) {
142  $files = [];
143  foreach (array_keys($value['tmp_name']) as $key) {
144  $data = [
145  'tmp_name' => $value['tmp_name'][$key],
146  'size' => $value['size'][$key],
147  'error' => $value['error'][$key],
148  'name' => $value['name'][$key],
149  'type' => $value['type'][$key]
150  ];
151  $result = self::createUploadedFile($data);
152  if ($result) {
153  $files[$key] = $result;
154  }
155  }
156  return $files;
157  } elseif (!empty($value['tmp_name'])) {
158  return new UploadedFile($value['tmp_name'], $value['size'], $value['error'], $value['name'], $value['type']);
159  }
160  return null;
161  }
162 }