[FEATURE] Add system extension "redirects"
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / HttpUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
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
17 use Psr\Http\Message\ResponseInterface;
18
19 /**
20 * HTTP Utility class
21 */
22 class HttpUtility
23 {
24 // HTTP Headers, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for Details
25 const HTTP_STATUS_100 = 'HTTP/1.1 100 Continue';
26 const HTTP_STATUS_101 = 'HTTP/1.1 101 Switching Protocols';
27 const HTTP_STATUS_200 = 'HTTP/1.1 200 OK';
28 const HTTP_STATUS_201 = 'HTTP/1.1 201 Created';
29 const HTTP_STATUS_202 = 'HTTP/1.1 202 Accepted';
30 const HTTP_STATUS_203 = 'HTTP/1.1 203 Non-Authoritative Information';
31 const HTTP_STATUS_204 = 'HTTP/1.1 204 No Content';
32 const HTTP_STATUS_205 = 'HTTP/1.1 205 Reset Content';
33 const HTTP_STATUS_206 = 'HTTP/1.1 206 Partial Content';
34 const HTTP_STATUS_300 = 'HTTP/1.1 300 Multiple Choices';
35 const HTTP_STATUS_301 = 'HTTP/1.1 301 Moved Permanently';
36 const HTTP_STATUS_302 = 'HTTP/1.1 302 Found';
37 const HTTP_STATUS_303 = 'HTTP/1.1 303 See Other';
38 const HTTP_STATUS_304 = 'HTTP/1.1 304 Not Modified';
39 const HTTP_STATUS_305 = 'HTTP/1.1 305 Use Proxy';
40 const HTTP_STATUS_307 = 'HTTP/1.1 307 Temporary Redirect';
41 const HTTP_STATUS_400 = 'HTTP/1.1 400 Bad Request';
42 const HTTP_STATUS_401 = 'HTTP/1.1 401 Unauthorized';
43 const HTTP_STATUS_402 = 'HTTP/1.1 402 Payment Required';
44 const HTTP_STATUS_403 = 'HTTP/1.1 403 Forbidden';
45 const HTTP_STATUS_404 = 'HTTP/1.1 404 Not Found';
46 const HTTP_STATUS_405 = 'HTTP/1.1 405 Method Not Allowed';
47 const HTTP_STATUS_406 = 'HTTP/1.1 406 Not Acceptable';
48 const HTTP_STATUS_407 = 'HTTP/1.1 407 Proxy Authentication Required';
49 const HTTP_STATUS_408 = 'HTTP/1.1 408 Request Timeout';
50 const HTTP_STATUS_409 = 'HTTP/1.1 409 Conflict';
51 const HTTP_STATUS_410 = 'HTTP/1.1 410 Gone';
52 const HTTP_STATUS_411 = 'HTTP/1.1 411 Length Required';
53 const HTTP_STATUS_412 = 'HTTP/1.1 412 Precondition Failed';
54 const HTTP_STATUS_413 = 'HTTP/1.1 413 Request Entity Too Large';
55 const HTTP_STATUS_414 = 'HTTP/1.1 414 Request-URI Too Long';
56 const HTTP_STATUS_415 = 'HTTP/1.1 415 Unsupported Media Type';
57 const HTTP_STATUS_416 = 'HTTP/1.1 416 Requested Range Not Satisfiable';
58 const HTTP_STATUS_417 = 'HTTP/1.1 417 Expectation Failed';
59 const HTTP_STATUS_500 = 'HTTP/1.1 500 Internal Server Error';
60 const HTTP_STATUS_501 = 'HTTP/1.1 501 Not Implemented';
61 const HTTP_STATUS_502 = 'HTTP/1.1 502 Bad Gateway';
62 const HTTP_STATUS_503 = 'HTTP/1.1 503 Service Unavailable';
63 const HTTP_STATUS_504 = 'HTTP/1.1 504 Gateway Timeout';
64 const HTTP_STATUS_505 = 'HTTP/1.1 505 Version Not Supported';
65 // URL Schemes
66 const SCHEME_HTTP = 1;
67 const SCHEME_HTTPS = 2;
68
69 /**
70 * Sends a redirect header response and exits. Additionally the URL is
71 * checked and if needed corrected to match the format required for a
72 * Location redirect header. By default the HTTP status code sent is
73 * a 'HTTP/1.1 303 See Other'.
74 *
75 * @param string $url The target URL to redirect to
76 * @param string $httpStatus An optional HTTP status header. Default is 'HTTP/1.1 303 See Other'
77 */
78 public static function redirect($url, $httpStatus = self::HTTP_STATUS_303)
79 {
80 self::setResponseCode($httpStatus);
81 header('Location: ' . GeneralUtility::locationHeaderUrl($url));
82 die;
83 }
84
85 /**
86 * Set a specific response code like 404.
87 *
88 * @param string $httpStatus One of the HTTP_STATUS_* class class constants, default to self::HTTP_STATUS_303
89 */
90 public static function setResponseCode($httpStatus = self::HTTP_STATUS_303)
91 {
92 header($httpStatus);
93 }
94
95 /**
96 * Set a specific response code and exit script execution.
97 *
98 * @param string $httpStatus One of the HTTP_STATUS_* class class constants, default to self::HTTP_STATUS_303
99 */
100 public static function setResponseCodeAndExit($httpStatus = self::HTTP_STATUS_303)
101 {
102 self::setResponseCode($httpStatus);
103 die;
104 }
105
106 /**
107 * Builds a URL string from an array with the URL parts, as e.g. output by parse_url().
108 *
109 * @param array $urlParts
110 * @return string
111 * @see http://www.php.net/parse_url
112 */
113 public static function buildUrl(array $urlParts)
114 {
115 return (isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : '') .
116 (isset($urlParts['user']) ? $urlParts['user'] .
117 (isset($urlParts['pass']) ? ':' . $urlParts['pass'] : '') . '@' : '') .
118 ($urlParts['host'] ?? '') .
119 (isset($urlParts['port']) ? ':' . $urlParts['port'] : '') .
120 ($urlParts['path'] ?? '') .
121 (isset($urlParts['query']) ? '?' . $urlParts['query'] : '') .
122 (isset($urlParts['fragment']) ? '#' . $urlParts['fragment'] : '');
123 }
124
125 /**
126 * Send Response to client and exit
127 *
128 * @param ResponseInterface $response
129 * @internal not part of public/stable API yet
130 */
131 public static function sendResponse(ResponseInterface $response)
132 {
133 if (!headers_sent()) {
134 header('HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
135 foreach ($response->getHeaders() as $name => $values) {
136 header($name . ': ' . implode(', ', $values));
137 }
138 }
139 echo $response->getBody()->__toString();
140 exit;
141 }
142 }