|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\http; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Http Response utilities |
|
7
|
|
|
* @author jc |
|
8
|
|
|
* @version 1.0.0.0 |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
class Response { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Send a raw HTTP header |
|
15
|
|
|
* @param string $headerField the header field |
|
16
|
|
|
* @param string $value the header value |
|
17
|
|
|
* @param boolean $replace The optional replace parameter indicates whether the header should replace a previous similar header |
|
18
|
|
|
* @param int $responseCode Forces the HTTP response code to the specified value |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function header($headerField, $value, $replace=null, $responseCode=null) { |
|
|
|
|
|
|
21
|
|
|
\header(trim($headerField) . ": " . trim($value), $replace); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $headerField |
|
27
|
|
|
* @param mixed $values |
|
28
|
|
|
*/ |
|
29
|
|
|
private static function _headerArray($headerField, $values) { |
|
30
|
|
|
if (\is_array($values)) { |
|
31
|
|
|
$values=\implode(", ", $values); |
|
32
|
|
|
} |
|
33
|
|
|
self::header($headerField, $values); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function setContentType($contentType, $encoding=null) { |
|
37
|
|
|
$value=$contentType; |
|
38
|
|
|
if (isset($encoding)) |
|
39
|
|
|
$value.=' ;' . $encoding; |
|
40
|
|
|
self::header('content-type', $value); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Forces the disabling of the browser cache |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function noCache() { |
|
47
|
|
|
self::header('Cache-Control', 'no-cache, must-revalidate'); |
|
48
|
|
|
self::header('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Checks if or where headers have been sent |
|
53
|
|
|
* @return boolean |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function isSent() { |
|
56
|
|
|
return \headers_sent(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Sets the response content-type to application/json |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function asJSON() { |
|
63
|
|
|
self::header('Content-Type', 'application/json'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sets the response content-type to text/html |
|
68
|
|
|
* @param string $encoding default: utf-8 |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function asHtml($encoding='utf-8') { |
|
71
|
|
|
self::setContentType('text/html', $encoding); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Sets the response content-type to plain/text |
|
76
|
|
|
* @param string $encoding default: utf-8 |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function asText($encoding='utf-8') { |
|
79
|
|
|
self::setContentType('plain/text', $encoding); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Sets the Accept header |
|
84
|
|
|
* @param string $value one of Http accept values |
|
85
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function setAccept($value) { |
|
88
|
|
|
self::header('Accept', $value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Sets the Access-Control-Allow-Origin field value |
|
93
|
|
|
* @param string $origin |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function setAccessControlOrigin($origin) { |
|
96
|
|
|
self::header('Access-Control-Allow-Origin', $origin); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Sets the Access-Control-Allow-Methods field value |
|
101
|
|
|
* @param string|array $origin |
|
|
|
|
|
|
102
|
|
|
*/ |
|
103
|
|
|
public static function setAccessControlMethods($methods) { |
|
|
|
|
|
|
104
|
|
|
self::_headerArray('Access-Control-Allow-Methods', $origin); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Sets the Access-Control-Allow-Headers field value |
|
109
|
|
|
* @param string|array $headers |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function setAccessControlHeaders($headers) { |
|
112
|
|
|
self::_headerArray('Access-Control-Allow-Headers', $headers); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sets the response code |
|
117
|
|
|
* @param int $value |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function setResponseCode($value) { |
|
120
|
|
|
\http_response_code($value); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get the response code |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function getResponseCode() { |
|
128
|
|
|
return \http_response_code(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.