‪TYPO3CMS  10.4
HttpUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Symfony\Polyfill\Intl\Idn\Idn;
19 
24 {
25  // HTTP Headers, see https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
26  // INFORMATIONAL CODES
27  const ‪HTTP_STATUS_100 = 'HTTP/1.1 100 Continue';
28  const ‪HTTP_STATUS_101 = 'HTTP/1.1 101 Switching Protocols';
29  const ‪HTTP_STATUS_102 = 'HTTP/1.1 102 Processing';
30  const ‪HTTP_STATUS_103 = 'HTTP/1.1 103 Early Hints';
31  // SUCCESS CODES
32  const ‪HTTP_STATUS_200 = 'HTTP/1.1 200 OK';
33  const ‪HTTP_STATUS_201 = 'HTTP/1.1 201 Created';
34  const ‪HTTP_STATUS_202 = 'HTTP/1.1 202 Accepted';
35  const ‪HTTP_STATUS_203 = 'HTTP/1.1 203 Non-Authoritative Information';
36  const ‪HTTP_STATUS_204 = 'HTTP/1.1 204 No Content';
37  const ‪HTTP_STATUS_205 = 'HTTP/1.1 205 Reset Content';
38  const ‪HTTP_STATUS_206 = 'HTTP/1.1 206 Partial Content';
39  const ‪HTTP_STATUS_207 = 'HTTP/1.1 207 Multi-status';
40  const ‪HTTP_STATUS_208 = 'HTTP/1.1 208 Already Reported';
41  const ‪HTTP_STATUS_226 = 'HTTP/1.1 226 IM Used';
42  // REDIRECTION CODES
43  const ‪HTTP_STATUS_300 = 'HTTP/1.1 300 Multiple Choices';
44  const ‪HTTP_STATUS_301 = 'HTTP/1.1 301 Moved Permanently';
45  const ‪HTTP_STATUS_302 = 'HTTP/1.1 302 Found';
46  const ‪HTTP_STATUS_303 = 'HTTP/1.1 303 See Other';
47  const ‪HTTP_STATUS_304 = 'HTTP/1.1 304 Not Modified';
48  const ‪HTTP_STATUS_305 = 'HTTP/1.1 305 Use Proxy';
49  const ‪HTTP_STATUS_306 = 'HTTP/1.1 306 Switch Proxy'; // Deprecated
50  const ‪HTTP_STATUS_307 = 'HTTP/1.1 307 Temporary Redirect';
51  const ‪HTTP_STATUS_308 = 'HTTP/1.1 308 Permanent Redirect';
52  // CLIENT ERROR
53  const ‪HTTP_STATUS_400 = 'HTTP/1.1 400 Bad Request';
54  const ‪HTTP_STATUS_401 = 'HTTP/1.1 401 Unauthorized';
55  const ‪HTTP_STATUS_402 = 'HTTP/1.1 402 Payment Required';
56  const ‪HTTP_STATUS_403 = 'HTTP/1.1 403 Forbidden';
57  const ‪HTTP_STATUS_404 = 'HTTP/1.1 404 Not Found';
58  const ‪HTTP_STATUS_405 = 'HTTP/1.1 405 Method Not Allowed';
59  const ‪HTTP_STATUS_406 = 'HTTP/1.1 406 Not Acceptable';
60  const ‪HTTP_STATUS_407 = 'HTTP/1.1 407 Proxy Authentication Required';
61  const ‪HTTP_STATUS_408 = 'HTTP/1.1 408 Request Timeout';
62  const ‪HTTP_STATUS_409 = 'HTTP/1.1 409 Conflict';
63  const ‪HTTP_STATUS_410 = 'HTTP/1.1 410 Gone';
64  const ‪HTTP_STATUS_411 = 'HTTP/1.1 411 Length Required';
65  const ‪HTTP_STATUS_412 = 'HTTP/1.1 412 Precondition Failed';
66  const ‪HTTP_STATUS_413 = 'HTTP/1.1 413 Request Entity Too Large';
67  const ‪HTTP_STATUS_414 = 'HTTP/1.1 414 URI Too Long';
68  const ‪HTTP_STATUS_415 = 'HTTP/1.1 415 Unsupported Media Type';
69  const ‪HTTP_STATUS_416 = 'HTTP/1.1 416 Requested range not satisfiable';
70  const ‪HTTP_STATUS_417 = 'HTTP/1.1 417 Expectation Failed';
71  const ‪HTTP_STATUS_418 = 'HTTP/1.1 418 I\'m a teapot';
72  const ‪HTTP_STATUS_422 = 'HTTP/1.1 422 Unprocessable Entity';
73  const ‪HTTP_STATUS_423 = 'HTTP/1.1 423 Locked';
74  const ‪HTTP_STATUS_424 = 'HTTP/1.1 424 Failed Dependency';
75  const ‪HTTP_STATUS_425 = 'HTTP/1.1 425 Unordered Collection';
76  const ‪HTTP_STATUS_426 = 'HTTP/1.1 426 Upgrade Required';
77  const ‪HTTP_STATUS_428 = 'HTTP/1.1 428 Precondition Required';
78  const ‪HTTP_STATUS_429 = 'HTTP/1.1 429 Too Many Requests';
79  const ‪HTTP_STATUS_431 = 'HTTP/1.1 431 Request Header Fields Too Large';
80  const ‪HTTP_STATUS_451 = 'HTTP/1.1 451 Unavailable For Legal Reasons';
81  // SERVER ERROR
82  const ‪HTTP_STATUS_500 = 'HTTP/1.1 500 Internal Server Error';
83  const ‪HTTP_STATUS_501 = 'HTTP/1.1 501 Not Implemented';
84  const ‪HTTP_STATUS_502 = 'HTTP/1.1 502 Bad Gateway';
85  const ‪HTTP_STATUS_503 = 'HTTP/1.1 503 Service Unavailable';
86  const ‪HTTP_STATUS_504 = 'HTTP/1.1 504 Gateway Time-out';
87  const ‪HTTP_STATUS_505 = 'HTTP/1.1 505 Version not Supported';
88  const ‪HTTP_STATUS_506 = 'HTTP/1.1 506 Variant Also Negotiates';
89  const ‪HTTP_STATUS_507 = 'HTTP/1.1 507 Insufficient Storage';
90  const ‪HTTP_STATUS_508 = 'HTTP/1.1 508 Loop Detected';
91  const ‪HTTP_STATUS_509 = 'HTTP/1.1 509 Bandwidth Limit Exceeded';
92  const ‪HTTP_STATUS_511 = 'HTTP/1.1 511 Network Authentication Required';
93  // URL Schemes
94  const ‪SCHEME_HTTP = 1;
95  const ‪SCHEME_HTTPS = 2;
96 
106  public static function ‪redirect($url, $httpStatus = self::HTTP_STATUS_303)
107  {
108  ‪self::setResponseCode($httpStatus);
109  header('Location: ' . GeneralUtility::locationHeaderUrl($url));
110  die;
111  }
112 
118  public static function ‪setResponseCode($httpStatus = self::HTTP_STATUS_303)
119  {
120  header($httpStatus);
121  }
122 
128  public static function ‪setResponseCodeAndExit($httpStatus = self::HTTP_STATUS_303)
129  {
130  ‪self::setResponseCode($httpStatus);
131  die;
132  }
133 
141  public static function ‪buildUrl(array $urlParts)
142  {
143  return (isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : '') .
144  (isset($urlParts['user']) ? $urlParts['user'] .
145  (isset($urlParts['pass']) ? ':' . $urlParts['pass'] : '') . '@' : '') .
146  ($urlParts['host'] ?? '') .
147  (isset($urlParts['port']) ? ':' . $urlParts['port'] : '') .
148  ($urlParts['path'] ?? '') .
149  (isset($urlParts['query']) ? '?' . $urlParts['query'] : '') .
150  (isset($urlParts['fragment']) ? '#' . $urlParts['fragment'] : '');
151  }
152 
163  public static function ‪buildQueryString(array $parameters, string $prependCharacter = '', bool $skipEmptyParameters = false): string
164  {
165  if (empty($parameters)) {
166  return '';
167  }
168 
169  if ($skipEmptyParameters) {
170  // This callback filters empty strings, array and null but keeps zero integers
171  $parameters = ‪ArrayUtility::filterRecursive(
172  $parameters,
173  function ($item) {
174  return $item !== '' && $item !== [] && $item !== null;
175  }
176  );
177  }
178 
179  $queryString = http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
180  $prependCharacter = $prependCharacter === '?' || $prependCharacter === '&' ? $prependCharacter : '';
181 
182  return $queryString && $prependCharacter ? $prependCharacter . $queryString : $queryString;
183  }
184 
195  public static function ‪idn_to_ascii(string $domain)
196  {
197  if (defined('INTL_IDNA_VARIANT_UTS46') && !defined('TYPO3_IDN_TO_ASCII_USE_COMPAT')) {
198  return ‪idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
199  }
200  // This is done due to some heavy old systems where native functionality is there, but does not support UTS46 yet.
201  if (!defined('TYPO3_IDN_TO_ASCII_USE_COMPAT')) {
202  define('TYPO3_IDN_TO_ASCII_USE_COMPAT', true);
203  }
204  if (!defined('INTL_IDNA_VARIANT_UTS46')) {
205  define('INTL_IDNA_VARIANT_UTS46', 1);
206  }
207  $result = [];
208  return Idn::idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $result);
209  }
210 }
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_503
‪const HTTP_STATUS_503
Definition: HttpUtility.php:85
‪TYPO3\CMS\Core\Utility\HttpUtility\idn_to_ascii
‪static string bool idn_to_ascii(string $domain)
Definition: HttpUtility.php:195
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_501
‪const HTTP_STATUS_501
Definition: HttpUtility.php:83
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_400
‪const HTTP_STATUS_400
Definition: HttpUtility.php:53
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_404
‪const HTTP_STATUS_404
Definition: HttpUtility.php:57
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_418
‪const HTTP_STATUS_418
Definition: HttpUtility.php:71
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_101
‪const HTTP_STATUS_101
Definition: HttpUtility.php:28
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_413
‪const HTTP_STATUS_413
Definition: HttpUtility.php:66
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_416
‪const HTTP_STATUS_416
Definition: HttpUtility.php:69
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_411
‪const HTTP_STATUS_411
Definition: HttpUtility.php:64
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_226
‪const HTTP_STATUS_226
Definition: HttpUtility.php:41
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_208
‪const HTTP_STATUS_208
Definition: HttpUtility.php:40
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_405
‪const HTTP_STATUS_405
Definition: HttpUtility.php:58
‪TYPO3\CMS\Core\Utility\HttpUtility\setResponseCodeAndExit
‪static setResponseCodeAndExit($httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:128
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_511
‪const HTTP_STATUS_511
Definition: HttpUtility.php:92
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_100
‪const HTTP_STATUS_100
Definition: HttpUtility.php:27
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_207
‪const HTTP_STATUS_207
Definition: HttpUtility.php:39
‪TYPO3\CMS\Core\Utility\ArrayUtility\filterRecursive
‪static array filterRecursive(array $array, callable $callback=null)
Definition: ArrayUtility.php:918
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_202
‪const HTTP_STATUS_202
Definition: HttpUtility.php:34
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_431
‪const HTTP_STATUS_431
Definition: HttpUtility.php:79
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_200
‪const HTTP_STATUS_200
Definition: HttpUtility.php:32
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_507
‪const HTTP_STATUS_507
Definition: HttpUtility.php:89
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_429
‪const HTTP_STATUS_429
Definition: HttpUtility.php:78
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_303
‪const HTTP_STATUS_303
Definition: HttpUtility.php:46
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_302
‪const HTTP_STATUS_302
Definition: HttpUtility.php:45
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_502
‪const HTTP_STATUS_502
Definition: HttpUtility.php:84
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_426
‪const HTTP_STATUS_426
Definition: HttpUtility.php:76
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_307
‪const HTTP_STATUS_307
Definition: HttpUtility.php:50
‪TYPO3\CMS\Core\Utility\HttpUtility\buildUrl
‪static string buildUrl(array $urlParts)
Definition: HttpUtility.php:141
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_102
‪const HTTP_STATUS_102
Definition: HttpUtility.php:29
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_305
‪const HTTP_STATUS_305
Definition: HttpUtility.php:48
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_428
‪const HTTP_STATUS_428
Definition: HttpUtility.php:77
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:163
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_402
‪const HTTP_STATUS_402
Definition: HttpUtility.php:55
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_422
‪const HTTP_STATUS_422
Definition: HttpUtility.php:72
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_401
‪const HTTP_STATUS_401
Definition: HttpUtility.php:54
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_301
‪const HTTP_STATUS_301
Definition: HttpUtility.php:44
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_504
‪const HTTP_STATUS_504
Definition: HttpUtility.php:86
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_425
‪const HTTP_STATUS_425
Definition: HttpUtility.php:75
‪TYPO3\CMS\Core\Utility\HttpUtility\SCHEME_HTTP
‪const SCHEME_HTTP
Definition: HttpUtility.php:94
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_508
‪const HTTP_STATUS_508
Definition: HttpUtility.php:90
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_505
‪const HTTP_STATUS_505
Definition: HttpUtility.php:87
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_409
‪const HTTP_STATUS_409
Definition: HttpUtility.php:62
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_424
‪const HTTP_STATUS_424
Definition: HttpUtility.php:74
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_403
‪const HTTP_STATUS_403
Definition: HttpUtility.php:56
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_412
‪const HTTP_STATUS_412
Definition: HttpUtility.php:65
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_451
‪const HTTP_STATUS_451
Definition: HttpUtility.php:80
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_300
‪const HTTP_STATUS_300
Definition: HttpUtility.php:43
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_406
‪const HTTP_STATUS_406
Definition: HttpUtility.php:59
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_306
‪const HTTP_STATUS_306
Definition: HttpUtility.php:49
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_308
‪const HTTP_STATUS_308
Definition: HttpUtility.php:51
‪TYPO3\CMS\Core\Utility\HttpUtility\SCHEME_HTTPS
‪const SCHEME_HTTPS
Definition: HttpUtility.php:95
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_304
‪const HTTP_STATUS_304
Definition: HttpUtility.php:47
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_417
‪const HTTP_STATUS_417
Definition: HttpUtility.php:70
‪TYPO3\CMS\Core\Utility\HttpUtility\setResponseCode
‪static setResponseCode($httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:118
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_415
‪const HTTP_STATUS_415
Definition: HttpUtility.php:68
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:24
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_103
‪const HTTP_STATUS_103
Definition: HttpUtility.php:30
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_407
‪const HTTP_STATUS_407
Definition: HttpUtility.php:60
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_205
‪const HTTP_STATUS_205
Definition: HttpUtility.php:37
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_423
‪const HTTP_STATUS_423
Definition: HttpUtility.php:73
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_506
‪const HTTP_STATUS_506
Definition: HttpUtility.php:88
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_206
‪const HTTP_STATUS_206
Definition: HttpUtility.php:38
‪TYPO3\CMS\Core\Utility\HttpUtility\redirect
‪static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:106
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_500
‪const HTTP_STATUS_500
Definition: HttpUtility.php:82
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_414
‪const HTTP_STATUS_414
Definition: HttpUtility.php:67
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_509
‪const HTTP_STATUS_509
Definition: HttpUtility.php:91
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_203
‪const HTTP_STATUS_203
Definition: HttpUtility.php:35
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_201
‪const HTTP_STATUS_201
Definition: HttpUtility.php:33
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_204
‪const HTTP_STATUS_204
Definition: HttpUtility.php:36
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_410
‪const HTTP_STATUS_410
Definition: HttpUtility.php:63
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_408
‪const HTTP_STATUS_408
Definition: HttpUtility.php:61