1+ <?php
2+
3+ namespace PH7 \PhpHttpResponseHeader ;
4+
5+ use PH7 \JustHttp \StatusCode ;
6+
7+ class Http
8+ {
9+ protected const STATUS_CODE = [
10+ 100 => '100 Continue ' ,
11+ 101 => '101 Switching Protocols ' ,
12+ 102 => '102 Processing ' ,
13+ 103 => 'Early Hints ' ,
14+ 200 => '200 OK ' ,
15+ 201 => '201 Created ' ,
16+ 202 => '202 Accepted ' ,
17+ 203 => '203 Non-Authoritative Information ' ,
18+ 204 => '204 No Content ' ,
19+ 205 => '205 Reset Content ' ,
20+ 206 => '206 Partial Content ' ,
21+ 207 => '207 Multi-Status ' ,
22+ 208 => '208 Already Reported ' ,
23+ 226 => '226 IM Used ' ,
24+ 300 => '300 Multiple Choices ' ,
25+ 301 => '301 Moved Permanently ' ,
26+ 302 => '302 Found ' ,
27+ 303 => '303 See Other ' ,
28+ 304 => '304 Not Modified ' ,
29+ 305 => '305 Use Proxy ' ,
30+ 307 => '307 Temporary Redirect ' ,
31+ 308 => '308 Permanent Redirect ' ,
32+ 400 => '400 Bad Request ' ,
33+ 401 => '401 Unauthorized ' ,
34+ 402 => '402 Payment Required ' ,
35+ 403 => '403 Forbidden ' ,
36+ 404 => '404 Not Found ' ,
37+ 405 => '405 Method Not Allowed ' ,
38+ 406 => '406 Not Acceptable ' ,
39+ 407 => '407 Proxy Authentication Required ' ,
40+ 408 => '408 Request Time-out ' ,
41+ 409 => '409 Conflict ' ,
42+ 410 => '410 Gone ' ,
43+ 411 => '411 Length Required ' ,
44+ 412 => '412 Precondition Failed ' ,
45+ 413 => '413 Request Entity Too Large ' ,
46+ 414 => '414 Request-URI Too Large ' ,
47+ 415 => '415 Unsupported Media Type ' ,
48+ 416 => '416 Requested range not satisfiable ' ,
49+ 417 => '417 Expectation Failed ' ,
50+ 422 => '422 Unprocessable Entity ' ,
51+ 423 => '423 Locked ' ,
52+ 424 => '424 Method Failure ' ,
53+ 425 => '425 Unordered Collection ' ,
54+ 426 => '426 Upgrade Required ' ,
55+ 428 => '428 Precondition Required ' ,
56+ 429 => '429 Too Many Requests ' ,
57+ 431 => '431 Request Header Fields Too Large ' ,
58+ 500 => '500 Internal Server Error ' ,
59+ 501 => '501 Not Implemented ' ,
60+ 502 => '502 Bad Gateway ' ,
61+ 503 => '503 Service Unavailable ' ,
62+ 504 => '504 Gateway Time-out ' ,
63+ 505 => '505 HTTP Version Unsupported ' ,
64+ 506 => '506 Variant Also Negotiates ' ,
65+ 507 => '507 Insufficient Storage ' ,
66+ 508 => '508 Loop Detected ' ,
67+ 509 => '509 Bandwidth Limit Exceede ' ,
68+ 510 => '510 Not Extended ' ,
69+ 511 => '511 Network Authentication Required ' ,
70+ 598 => '598 Network read timeout error ' ,
71+ 599 => '599 Network connect timeout error '
72+ ];
73+
74+
75+ /**
76+ * Give the HTTP status code name (e.g. "204 No Content").
77+ *
78+ * @param int $iStatus The "code" for the HTTP status.
79+ *
80+ * @return string|bool $iStatus Returns the "HTTP status code" if found, FALSE otherwise.
81+ */
82+ public static function getStatusCode (int $ iStatus ): string |bool
83+ {
84+ return !empty (static ::STATUS_CODE [$ iStatus ]) ? static ::STATUS_CODE [$ iStatus ] : false ;
85+ }
86+
87+ /**
88+ * Retrieve the list with headers that are sent or to be sent.
89+ */
90+ public static function getHeadersList (): array
91+ {
92+ return headers_list ();
93+ }
94+
95+ /**
96+ * Set one or multiple headers.
97+ *
98+ * @param string|array $mHeaders Headers to send.
99+ *
100+ * @throws Exception
101+ */
102+ public static function setHeaders (string |array $ mHeaders ): void
103+ {
104+ // Header already sent
105+ if (static ::isSent ()) {
106+ throw new Exception ('Headers were already sent. ' );
107+ }
108+
109+ // Loop elements and set header
110+ foreach ((array )$ mHeaders as $ sHeader ) {
111+ header ((string )$ sHeader );
112+ }
113+ }
114+
115+ /**
116+ * Parse headers for a given status code.
117+ *
118+ * @param int $iCode The code to use, possible values are: 200, 301, 302, 304, 307, 400, 401, 403, 404, 410, 500, 501, ...
119+ *
120+ * @throws Exception
121+ */
122+ public static function setHeadersByCode (int $ iCode = StatusCode::OK ): void
123+ {
124+ if (!static ::getStatusCode ($ iCode )) {
125+ $ iCode = StatusCode::OK ;
126+ }
127+
128+ static ::setHeaders (static ::getProtocol () . ' ' . static ::getStatusCode ($ iCode ));
129+ }
130+
131+ /**
132+ * Set a HTTP Content Type.
133+ *
134+ * @param string $sType Example: "text/xml".
135+ *
136+ * @throws Exception
137+ */
138+ public static function setContentType (string $ sType ): void
139+ {
140+ static ::setHeaders ('Content-Type: ' . $ sType );
141+ }
142+
143+ /**
144+ * Set the HTTP status code for the maintenance page.
145+ *
146+ * @param int $iMaintenanceTime Time site will be down for (in seconds).
147+ */
148+ public static function setMaintenanceCode (int $ iMaintenanceTime ): void
149+ {
150+ header (static ::getProtocol () . ' 503 Service Temporarily Unavailable ' );
151+ header ('Retry-After: ' . $ iMaintenanceTime );
152+ }
153+
154+
155+ /**
156+ * @return string|null The HTTP server protocol.
157+ */
158+ public static function getProtocol (): ?string
159+ {
160+ return Server::getVariable (Server::SERVER_PROTOCOL );
161+ }
162+
163+ /**
164+ * Checks if any headers were already sent.
165+ *
166+ * @return bool TRUE if the headers were sent, FALSE if not.
167+ */
168+ private static function isSent (): bool
169+ {
170+ return headers_sent ();
171+ }
172+ }
0 commit comments