1 | <?php |
||
14 | class Response { |
||
15 | use |
||
16 | Singleton, |
||
17 | Psr7; |
||
18 | /** |
||
19 | * Protocol, for instance: `HTTP/1.0`, `HTTP/1.1` (default), HTTP/2.0 |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | public $protocol; |
||
24 | /** |
||
25 | * HTTP status code |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | public $code; |
||
30 | /** |
||
31 | * Headers are normalized to lowercase keys with hyphen as separator, for instance: `connection`, `referer`, `content-type`, `accept-language` |
||
32 | * |
||
33 | * Values might be strings in case of single value or array of strings in case of multiple values with the same field name |
||
34 | * |
||
35 | * @var string[][] |
||
36 | */ |
||
37 | public $headers; |
||
38 | /** |
||
39 | * String body (is used instead of `$this->body_stream` in most cases, ignored if `$this->body_stream` is present) |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $body; |
||
44 | /** |
||
45 | * Body in form of stream (might be used instead of `$this->body` in some cases, if present, `$this->body` is ignored) |
||
46 | * |
||
47 | * Stream is read/write |
||
48 | * |
||
49 | * @var resource |
||
50 | */ |
||
51 | public $body_stream; |
||
52 | /** |
||
53 | * Initialize response object with specified data |
||
54 | * |
||
55 | * @param string $body |
||
56 | * @param null|resource|string $body_stream String, like `php://temp` or resource, like `fopen('php://temp', 'a+b')`, if present, `$body` is ignored |
||
57 | * @param string[]|string[][] $headers Headers are normalized to lowercase keys with hyphen as separator, for instance: `connection`, `referer`, |
||
58 | * `content-type`, `accept-language`; Values might be strings in case of single value or array of strings in case |
||
59 | * of multiple values with the same field name |
||
60 | * @param int $code HTTP status code |
||
61 | * @param string $protocol Protocol, for instance: `HTTP/1.0`, `HTTP/1.1` (default), HTTP/2.0 |
||
62 | * |
||
63 | * @return Response |
||
64 | */ |
||
65 | 8 | function init ($body = '', $body_stream = null, $headers = [], $code = 200, $protocol = 'HTTP/1.1') { |
|
76 | /** |
||
77 | * Initialize with typical default settings (headers `Content-Type`, `Vary` and `X-UA-Compatible`, protocol taken from `cs\Request::$protocol`) |
||
78 | * |
||
79 | * @return Response |
||
80 | */ |
||
81 | 8 | function init_with_typical_default_settings () { |
|
94 | /** |
||
95 | * Set raw HTTP header |
||
96 | * |
||
97 | * @param string $field Field |
||
98 | * @param string $value Value, empty string will cause header removal |
||
99 | * @param bool $replace The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header |
||
100 | * of the same type. By default it will replace |
||
101 | * |
||
102 | * @return Response |
||
103 | */ |
||
104 | 34 | function header ($field, $value, $replace = true) { |
|
115 | /** |
||
116 | * Make redirect to specified location |
||
117 | * |
||
118 | * @param string $location |
||
119 | * @param int $code |
||
120 | * |
||
121 | * @return Response |
||
122 | */ |
||
123 | function redirect ($location, $code = 302) { |
||
129 | /** |
||
130 | * Function for setting cookies, taking into account cookies prefix. Parameters like in system `setcookie()` function, but `$path`, `$domain` and `$secure` |
||
131 | * are skipped, they are detected automatically |
||
132 | * |
||
133 | * This function have side effect of setting cookie on `cs\Request` object |
||
134 | * |
||
135 | * @param string $name |
||
136 | * @param string $value |
||
137 | * @param int $expire |
||
138 | * @param bool $httponly |
||
139 | * |
||
140 | * @return Response |
||
141 | */ |
||
142 | 10 | function cookie ($name, $value, $expire = 0, $httponly = false) { |
|
143 | 10 | $Request = Request::instance(); |
|
144 | 10 | $Config = Config::instance(); |
|
145 | 10 | $prefix = ''; |
|
146 | 10 | $domain = explode(':', $Request->host)[0]; |
|
147 | 10 | if ($Config) { |
|
148 | 10 | $prefix = $Config->core['cookie_prefix']; |
|
149 | 10 | $cookie_domains = $Config->core['cookie_domain']; |
|
150 | 10 | $domain = isset($cookie_domains[$Request->mirror_index]) ? $cookie_domains[$Request->mirror_index] : $cookie_domains[0]; |
|
151 | } |
||
152 | 10 | if ($value === '') { |
|
153 | 6 | unset($Request->cookie[$name], $Request->cookie[$prefix.$name]); |
|
154 | } else { |
||
155 | 10 | $Request->cookie[$name] = $value; |
|
156 | 10 | $Request->cookie[$prefix.$name] = $value; |
|
157 | } |
||
158 | $header = [ |
||
159 | 10 | rawurlencode($prefix.$name).'='.rawurlencode($value), |
|
160 | 10 | 'path=/' |
|
161 | ]; |
||
162 | 10 | if ($expire || !$value) { |
|
163 | 10 | $header[] = 'expires='.gmdate('D, d-M-Y H:i:s', $expire).' GMT'; |
|
164 | } |
||
165 | 10 | if ($domain) { |
|
166 | 10 | $header[] = "domain=$domain"; |
|
167 | } |
||
168 | 10 | if ($Request->secure) { |
|
169 | $header[] = 'secure'; |
||
170 | } |
||
171 | 10 | if ($httponly) { |
|
172 | 10 | $header[] = 'HttpOnly'; |
|
173 | } |
||
174 | 10 | $this->header('set-cookie', implode('; ', $header), false); |
|
175 | 10 | return $this; |
|
176 | } |
||
177 | /** |
||
178 | * Provides default output for all the response data using `header()`, `http_response_code()` and `echo` or `php://output` |
||
179 | */ |
||
180 | function output_default () { |
||
220 | } |
||
221 |