‪TYPO3CMS  11.5
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 
22 {
23  // HTTP Headers, see https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
24  // INFORMATIONAL CODES
25  public const ‪HTTP_STATUS_100 = 'HTTP/1.1 100 Continue';
26  public const ‪HTTP_STATUS_101 = 'HTTP/1.1 101 Switching Protocols';
27  public const ‪HTTP_STATUS_102 = 'HTTP/1.1 102 Processing';
28  public const ‪HTTP_STATUS_103 = 'HTTP/1.1 103 Early Hints';
29  // SUCCESS CODES
30  public const ‪HTTP_STATUS_200 = 'HTTP/1.1 200 OK';
31  public const ‪HTTP_STATUS_201 = 'HTTP/1.1 201 Created';
32  public const ‪HTTP_STATUS_202 = 'HTTP/1.1 202 Accepted';
33  public const ‪HTTP_STATUS_203 = 'HTTP/1.1 203 Non-Authoritative Information';
34  public const ‪HTTP_STATUS_204 = 'HTTP/1.1 204 No Content';
35  public const ‪HTTP_STATUS_205 = 'HTTP/1.1 205 Reset Content';
36  public const ‪HTTP_STATUS_206 = 'HTTP/1.1 206 Partial Content';
37  public const ‪HTTP_STATUS_207 = 'HTTP/1.1 207 Multi-status';
38  public const ‪HTTP_STATUS_208 = 'HTTP/1.1 208 Already Reported';
39  public const ‪HTTP_STATUS_226 = 'HTTP/1.1 226 IM Used';
40  // REDIRECTION CODES
41  public const ‪HTTP_STATUS_300 = 'HTTP/1.1 300 Multiple Choices';
42  public const ‪HTTP_STATUS_301 = 'HTTP/1.1 301 Moved Permanently';
43  public const ‪HTTP_STATUS_302 = 'HTTP/1.1 302 Found';
44  public const ‪HTTP_STATUS_303 = 'HTTP/1.1 303 See Other';
45  public const ‪HTTP_STATUS_304 = 'HTTP/1.1 304 Not Modified';
46  public const ‪HTTP_STATUS_305 = 'HTTP/1.1 305 Use Proxy';
47  public const ‪HTTP_STATUS_306 = 'HTTP/1.1 306 Switch Proxy'; // Deprecated
48  public const ‪HTTP_STATUS_307 = 'HTTP/1.1 307 Temporary Redirect';
49  public const ‪HTTP_STATUS_308 = 'HTTP/1.1 308 Permanent Redirect';
50  // CLIENT ERROR
51  public const ‪HTTP_STATUS_400 = 'HTTP/1.1 400 Bad Request';
52  public const ‪HTTP_STATUS_401 = 'HTTP/1.1 401 Unauthorized';
53  public const ‪HTTP_STATUS_402 = 'HTTP/1.1 402 Payment Required';
54  public const ‪HTTP_STATUS_403 = 'HTTP/1.1 403 Forbidden';
55  public const ‪HTTP_STATUS_404 = 'HTTP/1.1 404 Not Found';
56  public const ‪HTTP_STATUS_405 = 'HTTP/1.1 405 Method Not Allowed';
57  public const ‪HTTP_STATUS_406 = 'HTTP/1.1 406 Not Acceptable';
58  public const ‪HTTP_STATUS_407 = 'HTTP/1.1 407 Proxy Authentication Required';
59  public const ‪HTTP_STATUS_408 = 'HTTP/1.1 408 Request Timeout';
60  public const ‪HTTP_STATUS_409 = 'HTTP/1.1 409 Conflict';
61  public const ‪HTTP_STATUS_410 = 'HTTP/1.1 410 Gone';
62  public const ‪HTTP_STATUS_411 = 'HTTP/1.1 411 Length Required';
63  public const ‪HTTP_STATUS_412 = 'HTTP/1.1 412 Precondition Failed';
64  public const ‪HTTP_STATUS_413 = 'HTTP/1.1 413 Request Entity Too Large';
65  public const ‪HTTP_STATUS_414 = 'HTTP/1.1 414 URI Too Long';
66  public const ‪HTTP_STATUS_415 = 'HTTP/1.1 415 Unsupported Media Type';
67  public const ‪HTTP_STATUS_416 = 'HTTP/1.1 416 Requested range not satisfiable';
68  public const ‪HTTP_STATUS_417 = 'HTTP/1.1 417 Expectation Failed';
69  public const ‪HTTP_STATUS_418 = 'HTTP/1.1 418 I\'m a teapot';
70  public const ‪HTTP_STATUS_422 = 'HTTP/1.1 422 Unprocessable Entity';
71  public const ‪HTTP_STATUS_423 = 'HTTP/1.1 423 Locked';
72  public const ‪HTTP_STATUS_424 = 'HTTP/1.1 424 Failed Dependency';
73  public const ‪HTTP_STATUS_425 = 'HTTP/1.1 425 Unordered Collection';
74  public const ‪HTTP_STATUS_426 = 'HTTP/1.1 426 Upgrade Required';
75  public const ‪HTTP_STATUS_428 = 'HTTP/1.1 428 Precondition Required';
76  public const ‪HTTP_STATUS_429 = 'HTTP/1.1 429 Too Many Requests';
77  public const ‪HTTP_STATUS_431 = 'HTTP/1.1 431 Request Header Fields Too Large';
78  public const ‪HTTP_STATUS_451 = 'HTTP/1.1 451 Unavailable For Legal Reasons';
79  // SERVER ERROR
80  public const ‪HTTP_STATUS_500 = 'HTTP/1.1 500 Internal Server Error';
81  public const ‪HTTP_STATUS_501 = 'HTTP/1.1 501 Not Implemented';
82  public const ‪HTTP_STATUS_502 = 'HTTP/1.1 502 Bad Gateway';
83  public const ‪HTTP_STATUS_503 = 'HTTP/1.1 503 Service Unavailable';
84  public const ‪HTTP_STATUS_504 = 'HTTP/1.1 504 Gateway Time-out';
85  public const ‪HTTP_STATUS_505 = 'HTTP/1.1 505 Version not Supported';
86  public const ‪HTTP_STATUS_506 = 'HTTP/1.1 506 Variant Also Negotiates';
87  public const ‪HTTP_STATUS_507 = 'HTTP/1.1 507 Insufficient Storage';
88  public const ‪HTTP_STATUS_508 = 'HTTP/1.1 508 Loop Detected';
89  public const ‪HTTP_STATUS_509 = 'HTTP/1.1 509 Bandwidth Limit Exceeded';
90  public const ‪HTTP_STATUS_511 = 'HTTP/1.1 511 Network Authentication Required';
91  // URL Schemes
92  public const ‪SCHEME_HTTP = 1;
93  public const ‪SCHEME_HTTPS = 2;
94 
105  public static function ‪redirect($url, $httpStatus = self::HTTP_STATUS_303)
106  {
107  // Deprecation logged by setResponseCode()
108  ‪self::setResponseCode($httpStatus);
109  header('Location: ' . GeneralUtility::locationHeaderUrl($url));
110  die;
111  }
112 
119  public static function ‪setResponseCode($httpStatus = self::HTTP_STATUS_303)
120  {
121  trigger_error(
122  'All methods in ' . __CLASS__ . ', manipulationg HTTP headers, are deprecated and will be removed in v12.',
123  E_USER_DEPRECATED
124  );
125 
126  header($httpStatus);
127  }
128 
135  public static function ‪setResponseCodeAndExit($httpStatus = self::HTTP_STATUS_303)
136  {
137  // Deprecation logged by setResponseCode()
138  ‪self::setResponseCode($httpStatus);
139  die;
140  }
141 
149  public static function ‪buildUrl(array $urlParts)
150  {
151  return (isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : '') .
152  (isset($urlParts['user']) ? $urlParts['user'] .
153  (isset($urlParts['pass']) ? ':' . $urlParts['pass'] : '') . '@' : '') .
154  ($urlParts['host'] ?? '') .
155  (isset($urlParts['port']) ? ':' . $urlParts['port'] : '') .
156  ($urlParts['path'] ?? '') .
157  (isset($urlParts['query']) ? '?' . $urlParts['query'] : '') .
158  (isset($urlParts['fragment']) ? '#' . $urlParts['fragment'] : '');
159  }
160 
171  public static function ‪buildQueryString(array $parameters, string $prependCharacter = '', bool $skipEmptyParameters = false): string
172  {
173  if (empty($parameters)) {
174  return '';
175  }
176 
177  if ($skipEmptyParameters) {
178  // This callback filters empty strings, array and null but keeps zero integers
179  $parameters = ‪ArrayUtility::filterRecursive(
180  $parameters,
181  static function ($item) {
182  return $item !== '' && $item !== [] && $item !== null;
183  }
184  );
185  }
186 
187  $queryString = http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
188  $prependCharacter = $prependCharacter === '?' || $prependCharacter === '&' ? $prependCharacter : '';
189 
190  return $queryString && $prependCharacter ? $prependCharacter . $queryString : $queryString;
191  }
192 }
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_503
‪const HTTP_STATUS_503
Definition: HttpUtility.php:83
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_501
‪const HTTP_STATUS_501
Definition: HttpUtility.php:81
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_400
‪const HTTP_STATUS_400
Definition: HttpUtility.php:51
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_404
‪const HTTP_STATUS_404
Definition: HttpUtility.php:55
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_418
‪const HTTP_STATUS_418
Definition: HttpUtility.php:69
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_101
‪const HTTP_STATUS_101
Definition: HttpUtility.php:26
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_413
‪const HTTP_STATUS_413
Definition: HttpUtility.php:64
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_416
‪const HTTP_STATUS_416
Definition: HttpUtility.php:67
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_411
‪const HTTP_STATUS_411
Definition: HttpUtility.php:62
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_226
‪const HTTP_STATUS_226
Definition: HttpUtility.php:39
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_208
‪const HTTP_STATUS_208
Definition: HttpUtility.php:38
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_405
‪const HTTP_STATUS_405
Definition: HttpUtility.php:56
‪TYPO3\CMS\Core\Utility\HttpUtility\setResponseCodeAndExit
‪static setResponseCodeAndExit($httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:135
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_511
‪const HTTP_STATUS_511
Definition: HttpUtility.php:90
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_100
‪const HTTP_STATUS_100
Definition: HttpUtility.php:25
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_207
‪const HTTP_STATUS_207
Definition: HttpUtility.php:37
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_202
‪const HTTP_STATUS_202
Definition: HttpUtility.php:32
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_431
‪const HTTP_STATUS_431
Definition: HttpUtility.php:77
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_200
‪const HTTP_STATUS_200
Definition: HttpUtility.php:30
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_507
‪const HTTP_STATUS_507
Definition: HttpUtility.php:87
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_429
‪const HTTP_STATUS_429
Definition: HttpUtility.php:76
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_303
‪const HTTP_STATUS_303
Definition: HttpUtility.php:44
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_302
‪const HTTP_STATUS_302
Definition: HttpUtility.php:43
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_502
‪const HTTP_STATUS_502
Definition: HttpUtility.php:82
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_426
‪const HTTP_STATUS_426
Definition: HttpUtility.php:74
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_307
‪const HTTP_STATUS_307
Definition: HttpUtility.php:48
‪TYPO3\CMS\Core\Utility\HttpUtility\buildUrl
‪static string buildUrl(array $urlParts)
Definition: HttpUtility.php:149
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_102
‪const HTTP_STATUS_102
Definition: HttpUtility.php:27
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_305
‪const HTTP_STATUS_305
Definition: HttpUtility.php:46
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_428
‪const HTTP_STATUS_428
Definition: HttpUtility.php:75
‪TYPO3\CMS\Core\Utility\ArrayUtility\filterRecursive
‪static filterRecursive(array $array, ?callable $callback=null, int $mode=0)
Definition: ArrayUtility.php:931
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:171
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_402
‪const HTTP_STATUS_402
Definition: HttpUtility.php:53
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_422
‪const HTTP_STATUS_422
Definition: HttpUtility.php:70
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_401
‪const HTTP_STATUS_401
Definition: HttpUtility.php:52
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_301
‪const HTTP_STATUS_301
Definition: HttpUtility.php:42
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_504
‪const HTTP_STATUS_504
Definition: HttpUtility.php:84
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_425
‪const HTTP_STATUS_425
Definition: HttpUtility.php:73
‪TYPO3\CMS\Core\Utility\HttpUtility\SCHEME_HTTP
‪const SCHEME_HTTP
Definition: HttpUtility.php:92
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_508
‪const HTTP_STATUS_508
Definition: HttpUtility.php:88
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_505
‪const HTTP_STATUS_505
Definition: HttpUtility.php:85
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_409
‪const HTTP_STATUS_409
Definition: HttpUtility.php:60
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_424
‪const HTTP_STATUS_424
Definition: HttpUtility.php:72
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_403
‪const HTTP_STATUS_403
Definition: HttpUtility.php:54
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_412
‪const HTTP_STATUS_412
Definition: HttpUtility.php:63
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_451
‪const HTTP_STATUS_451
Definition: HttpUtility.php:78
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_300
‪const HTTP_STATUS_300
Definition: HttpUtility.php:41
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_406
‪const HTTP_STATUS_406
Definition: HttpUtility.php:57
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_306
‪const HTTP_STATUS_306
Definition: HttpUtility.php:47
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_308
‪const HTTP_STATUS_308
Definition: HttpUtility.php:49
‪TYPO3\CMS\Core\Utility\HttpUtility\SCHEME_HTTPS
‪const SCHEME_HTTPS
Definition: HttpUtility.php:93
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_304
‪const HTTP_STATUS_304
Definition: HttpUtility.php:45
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_417
‪const HTTP_STATUS_417
Definition: HttpUtility.php:68
‪TYPO3\CMS\Core\Utility\HttpUtility\setResponseCode
‪static setResponseCode($httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:119
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_415
‪const HTTP_STATUS_415
Definition: HttpUtility.php:66
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:22
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_103
‪const HTTP_STATUS_103
Definition: HttpUtility.php:28
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_407
‪const HTTP_STATUS_407
Definition: HttpUtility.php:58
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_205
‪const HTTP_STATUS_205
Definition: HttpUtility.php:35
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_423
‪const HTTP_STATUS_423
Definition: HttpUtility.php:71
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_506
‪const HTTP_STATUS_506
Definition: HttpUtility.php:86
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_206
‪const HTTP_STATUS_206
Definition: HttpUtility.php:36
‪TYPO3\CMS\Core\Utility\HttpUtility\redirect
‪static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:105
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_500
‪const HTTP_STATUS_500
Definition: HttpUtility.php:80
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_414
‪const HTTP_STATUS_414
Definition: HttpUtility.php:65
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_509
‪const HTTP_STATUS_509
Definition: HttpUtility.php:89
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_203
‪const HTTP_STATUS_203
Definition: HttpUtility.php:33
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_201
‪const HTTP_STATUS_201
Definition: HttpUtility.php:31
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_204
‪const HTTP_STATUS_204
Definition: HttpUtility.php:34
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_410
‪const HTTP_STATUS_410
Definition: HttpUtility.php:61
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_408
‪const HTTP_STATUS_408
Definition: HttpUtility.php:59