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