‪TYPO3CMS  ‪main
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;
26 
34 class ‪ServerRequestFactory implements ServerRequestFactoryInterface
35 {
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  try {
66  $uri = new ‪Uri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
67  } catch (\InvalidArgumentException $e) {
68  if (‪Environment::isCli()) {
70  'Usage of ' . __METHOD__ . ' on CLI is discouraged. In case you rely on the method, you have to fake a valid request URL using $_SERVER.',
71  1701105725
72  );
73  }
74  throw $e;
75  }
76 
77  $request = new ‪ServerRequest(
78  $uri,
79  $method,
80  'php://input',
81  $headers,
82  $serverParameters,
83  static::normalizeUploadedFiles($_FILES)
84  );
85  if (!empty($_COOKIE)) {
86  $request = $request->withCookieParams($_COOKIE);
87  }
88  if (!empty($_GET)) {
89  $request = $request->withQueryParams($_GET);
90  }
91  $parsedBody = $_POST;
92  if (empty($parsedBody) && in_array($method, ['PUT', 'PATCH', 'DELETE'])) {
93  parse_str((string)file_get_contents('php://input'), $parsedBody);
94  }
95  if (!empty($parsedBody)) {
96  $request = $request->withParsedBody($parsedBody);
97  }
98  return $request;
99  }
100 
107  protected static function ‪prepareHeaders(array $server)
108  {
109  $headers = [];
110  foreach ($server as $key => $value) {
111  if (!is_string($key)) {
112  continue;
113  }
114  if (str_starts_with($key, 'HTTP_COOKIE')) {
115  // Cookies are handled using the $_COOKIE superglobal
116  continue;
117  }
118  if (!empty($value)) {
119  if (str_starts_with($key, 'HTTP_')) {
120  $name = str_replace('_', ' ', substr($key, 5));
121  $name = str_replace(' ', '-', ucwords(strtolower($name)));
122  $name = strtolower($name);
123  $headers[$name] = $value;
124  } elseif (str_starts_with($key, 'CONTENT_')) {
125  $name = substr($key, 8); // Content-
126  $name = 'Content-' . (($name === 'MD5') ? $name : ucfirst(strtolower($name)));
127  $name = strtolower($name);
128  $headers[$name] = $value;
129  }
130  }
131  }
132  return $headers;
133  }
134 
144  protected static function ‪normalizeUploadedFiles(array $files)
145  {
146  $normalizedFileUploads = [];
147  foreach ($files as $key => $value) {
148  if ($value instanceof UploadedFileInterface) {
149  $normalizedFileUploads[$key] = $value;
150  } elseif (is_array($value)) {
151  if (isset($value['tmp_name'])) {
152  $uploadedFiles = ‪self::createUploadedFile($value);
153  if ($uploadedFiles) {
154  $normalizedFileUploads[$key] = $uploadedFiles;
155  }
156  } else {
157  $normalizedFileUploads[$key] = ‪self::normalizeUploadedFiles($value);
158  }
159  } else {
160  throw new \InvalidArgumentException('Invalid value in files specification.', 1436717282);
161  }
162  }
163  return $normalizedFileUploads;
164  }
165 
175  protected static function ‪createUploadedFile(array $value)
176  {
177  if (is_array($value['tmp_name'])) {
178  $files = [];
179  foreach (array_keys($value['tmp_name']) as $key) {
180  $data = [
181  'tmp_name' => $value['tmp_name'][$key],
182  'error' => $value['error'][$key],
183  'name' => $value['name'][$key],
184  'type' => $value['type'][$key],
185  ];
186  if (isset($value['size'][$key])) {
187  $data['size'] = $value['size'][$key];
188  }
189  $result = ‪self::createUploadedFile($data);
190  if ($result) {
191  $files[$key] = $result;
192  }
193  }
194  return $files;
195  }
196  if (!empty($value['tmp_name'])) {
197  return new ‪UploadedFile($value['tmp_name'], $value['size'] ?? 0, $value['error'], $value['name'], $value['type']);
198  }
199  return null;
200  }
201 }
‪TYPO3\CMS\Core\Http\ServerRequestFactory\createServerRequest
‪createServerRequest(string $method, $uri, array $serverParams=[])
Definition: ServerRequestFactory.php:47
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Core\Http\ServerRequestFactory\prepareHeaders
‪static array prepareHeaders(array $server)
Definition: ServerRequestFactory.php:107
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:35
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Http\ServerRequestFactory\createUploadedFile
‪static UploadedFileInterface[] UploadedFileInterface null createUploadedFile(array $value)
Definition: ServerRequestFactory.php:175
‪TYPO3\CMS\Core\Http\InvalidRequestUrlOnCliException
Definition: InvalidRequestUrlOnCliException.php:25
‪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:144