|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\http; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Startup; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Http Response utilities. |
|
9
|
|
|
* |
|
10
|
|
|
* Ubiquity\utils\http$UResponse |
|
11
|
|
|
* This class is part of Ubiquity |
|
12
|
|
|
* |
|
13
|
|
|
* @author jcheron <[email protected]> |
|
14
|
|
|
* @version 1.1.3 |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
class UResponse { |
|
18
|
|
|
public static $headers = [ ]; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Send a raw HTTP header |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $headerField the header field |
|
24
|
|
|
* @param string $value the header value |
|
25
|
|
|
* @param boolean $replace The optional replace parameter indicates whether the header should replace a previous similar header |
|
26
|
|
|
* @param int $responseCode Forces the HTTP response code to the specified value |
|
27
|
|
|
*/ |
|
28
|
7 |
|
public static function header($headerField, $value, bool $replace = true, int $responseCode = 0): void { |
|
29
|
7 |
|
self::$headers [trim ( $headerField )] = trim ( $value ); |
|
30
|
7 |
|
Startup::getHttpInstance ()->header ( trim ( $headerField ), trim ( $value ), $replace, $responseCode ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Forwards to url using Location header. |
|
35
|
|
|
* @param string $url |
|
36
|
|
|
* @param bool $preserveUrl |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function forward(string $url,bool $preserveUrl=true): void { |
|
39
|
|
|
self::header('Location',$preserveUrl?$url:URequest::getUrl($url)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $headerField |
|
45
|
|
|
* @param mixed $values |
|
46
|
|
|
*/ |
|
47
|
|
|
private static function _headerArray($headerField, $values): void { |
|
48
|
|
|
if (\is_array ( $values )) { |
|
49
|
|
|
$values = \implode ( ', ', $values ); |
|
50
|
|
|
} |
|
51
|
|
|
self::header ( $headerField, $values ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets header content-type |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $contentType |
|
58
|
|
|
* @param string $encoding |
|
59
|
|
|
*/ |
|
60
|
5 |
|
public static function setContentType($contentType, $encoding = null): void { |
|
61
|
5 |
|
$value = $contentType; |
|
62
|
5 |
|
if (isset ( $encoding )) |
|
63
|
2 |
|
$value .= '; charset=' . $encoding; |
|
64
|
5 |
|
self::header ( 'Content-Type', $value, true ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Forces the disabling of the browser cache |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public static function noCache(): void { |
|
71
|
1 |
|
self::header ( 'Cache-Control', 'no-cache, must-revalidate' ); |
|
72
|
1 |
|
self::header ( 'Expires', 'Sat, 26 Jul 1997 05:00:00 GMT' ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Checks if or where headers have been sent |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $file If the optional file and line parameters are set,headers_sent will put the PHP source file nameand line number where output started in the fileand line variables. |
|
79
|
|
|
* @param int $line The line number where the output started. |
|
80
|
|
|
* @return boolean |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function isSent(&$file = null, &$line = null): bool { |
|
83
|
|
|
return Startup::getHttpInstance ()->headersSent ( $file, $line ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sets the response content-type to application/json |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public static function asJSON(): void { |
|
90
|
2 |
|
self::header ( 'Content-Type', 'application/json' ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Tests if response content-type is application/json |
|
95
|
|
|
* Only Works if UResponse has been used for setting headers |
|
96
|
|
|
* |
|
97
|
|
|
* @return boolean |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public static function isJSON(): bool { |
|
100
|
2 |
|
return isset ( self::$headers ['Content-Type'] ) && self::$headers ['Content-Type'] === 'application/json'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Sets the response content-type to text/html |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $encoding default: utf-8 |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function asHtml($encoding = 'utf-8'): void { |
|
109
|
|
|
self::setContentType ( 'text/html', $encoding ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Sets the response content-type to application/xml |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $encoding default: utf-8 |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public static function asXml($encoding = 'utf-8'): void { |
|
118
|
1 |
|
self::setContentType ( 'application/xml', $encoding ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Sets the response content-type to plain/text |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $encoding default: utf-8 |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function asText($encoding = 'utf-8'): void { |
|
127
|
|
|
self::setContentType ( 'plain/text', $encoding ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Sets the Accept header |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $value one of Http accept values |
|
134
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function setAccept($value): void { |
|
137
|
|
|
self::header ( 'Accept', $value ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Enables CORS |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $origin The allowed origin (default: '*') |
|
144
|
|
|
* @param string $methods The allowed methods (default: 'GET, POST, PUT, DELETE, PATCH, OPTIONS') |
|
145
|
|
|
* @param string $headers The allowed headers (default: 'X-Requested-With, Content-Type, Accept, Origin, Authorization') |
|
146
|
|
|
* @since Ubiquity 2.0.11 |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function enableCors($origin = '*', $methods = 'GET, POST, PUT, DELETE, PATCH, OPTIONS', $headers = 'X-Requested-With, Content-Type, Accept, Origin, Authorization'): void { |
|
149
|
|
|
self::setAccessControlOrigin ( $origin ); |
|
150
|
|
|
self::setAccessControlMethods ( $methods ); |
|
151
|
|
|
self::setAccessControlHeaders ( $headers ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Sets the Access-Control-Allow-Origin field value |
|
156
|
|
|
* Only a single origin can be specified. |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $origin |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function setAccessControlOrigin($origin = '*'): void { |
|
161
|
|
|
self::header ( 'Access-Control-Allow-Origin', $origin ); |
|
162
|
|
|
if ($origin !== '*') { |
|
163
|
|
|
self::header ( 'Vary', 'Origin' ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Sets the Access-Control-Allow-Methods field value |
|
169
|
|
|
* |
|
170
|
|
|
* @param string|array $methods |
|
171
|
|
|
*/ |
|
172
|
|
|
public static function setAccessControlMethods($methods): void { |
|
173
|
|
|
self::_headerArray ( 'Access-Control-Allow-Methods', $methods ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Sets the Access-Control-Allow-Headers field value |
|
178
|
|
|
* |
|
179
|
|
|
* @param string|array $headers |
|
180
|
|
|
*/ |
|
181
|
|
|
public static function setAccessControlHeaders($headers): void { |
|
182
|
|
|
self::_headerArray ( 'Access-Control-Allow-Headers', $headers ); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Set the Authorization header field |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $authorization |
|
189
|
|
|
*/ |
|
190
|
|
|
public static function setAuthorization($authorization): void { |
|
191
|
|
|
self::header ( 'Authorization', $authorization ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Sets the response code |
|
196
|
|
|
* |
|
197
|
|
|
* @param int $value |
|
198
|
|
|
*/ |
|
199
|
|
|
public static function setResponseCode($value) { |
|
200
|
|
|
return \http_response_code ( $value ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get the response code |
|
205
|
|
|
* |
|
206
|
|
|
* @return int|bool |
|
207
|
|
|
*/ |
|
208
|
|
|
public static function getResponseCode() { |
|
209
|
|
|
return \http_response_code (); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|