‪TYPO3CMS  10.4
ServerRequestFactory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use Psr\Http\Message\ServerRequestFactoryInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Message\UploadedFileInterface;
23 use Psr\Http\Message\UriInterface;
25 
33 class ‪ServerRequestFactory implements ServerRequestFactoryInterface
34 {
47  public function ‪createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
48  {
49  return new ‪ServerRequest($uri, $method, null, [], $serverParams);
50  }
51 
59  public static function ‪fromGlobals()
60  {
61  $serverParameters = $_SERVER;
62  $headers = static::prepareHeaders($serverParameters);
63 
64  $method = $serverParameters['REQUEST_METHOD'] ?? 'GET';
65  $uri = new ‪Uri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
66 
67  $request = new ‪ServerRequest(
68  $uri,
69  $method,
70  'php://input',
71  $headers,
72  $serverParameters,
73  static::normalizeUploadedFiles($_FILES)
74  );
75 
76  if (!empty($_COOKIE)) {
77  $request = $request->withCookieParams($_COOKIE);
78  }
79  $queryParameters = GeneralUtility::_GET();
80  if (!empty($queryParameters)) {
81  $request = $request->withQueryParams($queryParameters);
82  }
83  $parsedBody = GeneralUtility::_POST();
84  if (empty($parsedBody) && in_array($method, ['PUT', 'PATCH', 'DELETE'])) {
85  parse_str((string)file_get_contents('php://input'), $parsedBody);
86  }
87  if (!empty($parsedBody)) {
88  $request = $request->withParsedBody($parsedBody);
89  }
90  return $request;
91  }
92 
100  protected static function ‪prepareHeaders(array $server)
101  {
102  $headers = [];
103  foreach ($server as $key => $value) {
104  if (!is_string($key)) {
105  continue;
106  }
107  if (strpos($key, 'HTTP_COOKIE') === 0) {
108  // Cookies are handled using the $_COOKIE superglobal
109  continue;
110  }
111  if (!empty($value)) {
112  if (strpos($key, 'HTTP_') === 0) {
113  $name = str_replace('_', ' ', substr($key, 5));
114  $name = str_replace(' ', '-', ucwords(strtolower($name)));
115  $name = strtolower($name);
116  $headers[$name] = $value;
117  } elseif (strpos($key, 'CONTENT_') === 0) {
118  $name = substr($key, 8); // Content-
119  $name = 'Content-' . (($name === 'MD5') ? $name : ucfirst(strtolower($name)));
120  $name = strtolower($name);
121  $headers[$name] = $value;
122  }
123  }
124  }
125  return $headers;
126  }
127 
137  protected static function ‪normalizeUploadedFiles(array $files)
138  {
139  $normalizedFileUploads = [];
140  foreach ($files as $key => $value) {
141  if ($value instanceof UploadedFileInterface) {
142  $normalizedFileUploads[$key] = $value;
143  } elseif (is_array($value)) {
144  if (isset($value['tmp_name'])) {
145  $uploadedFiles = ‪self::createUploadedFile($value);
146  if ($uploadedFiles) {
147  $normalizedFileUploads[$key] = $uploadedFiles;
148  }
149  } else {
150  $normalizedFileUploads[$key] = ‪self::normalizeUploadedFiles($value);
151  }
152  } else {
153  throw new \InvalidArgumentException('Invalid value in files specification.', 1436717282);
154  }
155  }
156  return $normalizedFileUploads;
157  }
158 
168  protected static function ‪createUploadedFile(array $value)
169  {
170  if (is_array($value['tmp_name'])) {
171  $files = [];
172  foreach (array_keys($value['tmp_name']) as $key) {
173  $data = [
174  'tmp_name' => $value['tmp_name'][$key],
175  'size' => $value['size'][$key],
176  'error' => $value['error'][$key],
177  'name' => $value['name'][$key],
178  'type' => $value['type'][$key]
179  ];
180  $result = ‪self::createUploadedFile($data);
181  if ($result) {
182  $files[$key] = $result;
183  }
184  }
185  return $files;
186  }
187  if (!empty($value['tmp_name'])) {
188  return new ‪UploadedFile($value['tmp_name'], $value['size'], $value['error'], $value['name'], $value['type']);
189  }
190  return null;
191  }
192 }
‪TYPO3\CMS\Core\Http\ServerRequestFactory\createServerRequest
‪ServerRequestInterface createServerRequest(string $method, $uri, array $serverParams=[])
Definition: ServerRequestFactory.php:47
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Core\Http\ServerRequestFactory\prepareHeaders
‪static array prepareHeaders(array $server)
Definition: ServerRequestFactory.php:100
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http\ServerRequestFactory\createUploadedFile
‪static UploadedFileInterface[] UploadedFileInterface null createUploadedFile(array $value)
Definition: ServerRequestFactory.php:168
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18
‪TYPO3\CMS\Core\Http\ServerRequestFactory\normalizeUploadedFiles
‪static array normalizeUploadedFiles(array $files)
Definition: ServerRequestFactory.php:137