|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Selami\Http; |
|
5
|
|
|
|
|
6
|
|
|
use Selami\Router; |
|
7
|
|
|
|
|
8
|
|
|
class Response |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $response; |
|
12
|
|
|
protected $outputType = Router::HTML; |
|
13
|
|
|
protected $headers = []; |
|
14
|
|
|
protected $statusCode = 200; |
|
15
|
|
|
protected $body; |
|
16
|
|
|
protected $redirectUri; |
|
17
|
|
|
protected $downloadFileName; |
|
18
|
|
|
protected $downloadFilePath; |
|
19
|
|
|
protected $version = '1.0'; |
|
20
|
|
|
protected $customContentType; |
|
21
|
|
|
protected static $statusTexts = [ |
|
22
|
|
|
100 => 'Continue', |
|
23
|
|
|
101 => 'Switching Protocols', |
|
24
|
|
|
102 => 'Processing', // RFC2518 |
|
25
|
|
|
200 => 'OK', |
|
26
|
|
|
201 => 'Created', |
|
27
|
|
|
202 => 'Accepted', |
|
28
|
|
|
203 => 'Non-Authoritative Information', |
|
29
|
|
|
204 => 'No Content', |
|
30
|
|
|
205 => 'Reset Content', |
|
31
|
|
|
206 => 'Partial Content', |
|
32
|
|
|
207 => 'Multi-Status', // RFC4918 |
|
33
|
|
|
208 => 'Already Reported', // RFC5842 |
|
34
|
|
|
226 => 'IM Used', // RFC3229 |
|
35
|
|
|
300 => 'Multiple Choices', |
|
36
|
|
|
301 => 'Moved Permanently', |
|
37
|
|
|
302 => 'Found', |
|
38
|
|
|
303 => 'See Other', |
|
39
|
|
|
304 => 'Not Modified', |
|
40
|
|
|
305 => 'Use Proxy', |
|
41
|
|
|
307 => 'Temporary Redirect', |
|
42
|
|
|
308 => 'Permanent Redirect', // RFC7238 |
|
43
|
|
|
400 => 'Bad Request', |
|
44
|
|
|
401 => 'Unauthorized', |
|
45
|
|
|
402 => 'Payment Required', |
|
46
|
|
|
403 => 'Forbidden', |
|
47
|
|
|
404 => 'Not Found', |
|
48
|
|
|
405 => 'Method Not Allowed', |
|
49
|
|
|
406 => 'Not Acceptable', |
|
50
|
|
|
407 => 'Proxy Authentication Required', |
|
51
|
|
|
408 => 'Request Timeout', |
|
52
|
|
|
409 => 'Conflict', |
|
53
|
|
|
410 => 'Gone', |
|
54
|
|
|
411 => 'Length Required', |
|
55
|
|
|
412 => 'Precondition Failed', |
|
56
|
|
|
413 => 'Payload Too Large', |
|
57
|
|
|
414 => 'URI Too Long', |
|
58
|
|
|
415 => 'Unsupported Media Type', |
|
59
|
|
|
416 => 'Range Not Satisfiable', |
|
60
|
|
|
417 => 'Expectation Failed', |
|
61
|
|
|
418 => 'I\'m a teapot', // RFC2324 |
|
62
|
|
|
421 => 'Misdirected Request', // RFC7540 |
|
63
|
|
|
422 => 'Unprocessable Entity', // RFC4918 |
|
64
|
|
|
423 => 'Locked', // RFC4918 |
|
65
|
|
|
424 => 'Failed Dependency', // RFC4918 |
|
66
|
|
|
425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817 |
|
67
|
|
|
426 => 'Upgrade Required', // RFC2817 |
|
68
|
|
|
428 => 'Precondition Required', // RFC6585 |
|
69
|
|
|
429 => 'Too Many Requests', // RFC6585 |
|
70
|
|
|
431 => 'Request Header Fields Too Large', // RFC6585 |
|
71
|
|
|
451 => 'Unavailable For Legal Reasons', // RFC7725 |
|
72
|
|
|
500 => 'Internal Server Error', |
|
73
|
|
|
501 => 'Not Implemented', |
|
74
|
|
|
502 => 'Bad Gateway', |
|
75
|
|
|
503 => 'Service Unavailable', |
|
76
|
|
|
504 => 'Gateway Timeout', |
|
77
|
|
|
505 => 'HTTP Version Not Supported', |
|
78
|
|
|
506 => 'Variant Also Negotiates (Experimental)', // RFC2295 |
|
79
|
|
|
507 => 'Insufficient Storage', // RFC4918 |
|
80
|
|
|
508 => 'Loop Detected', // RFC5842 |
|
81
|
|
|
510 => 'Not Extended', // RFC2774 |
|
82
|
|
|
511 => 'Network Authentication Required', // RFC6585 |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
public function setHeaders(array $headers = []) : void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->headers['X-Powered-By'] = 'Selami'; |
|
88
|
|
|
$this->headers['X-Frame-Options'] = 'SAMEORIGIN'; |
|
89
|
|
|
$this->headers['X-XSS-Protection'] = '1; mode=block'; |
|
90
|
|
|
$this->headers['Strict-Transport-Security'] = 'max-age=31536000'; |
|
91
|
|
|
foreach ($headers as $header => $value) { |
|
92
|
|
|
$this->setHeader($header, $value); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function setHeader(string $header, string $value) : void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->headers[$header] = $value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setStatusCode(int $statusCode) : void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->statusCode = $statusCode; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function setRedirect(string $redirectUri, int $code = 302) : void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->statusCode = $code; |
|
109
|
|
|
|
|
110
|
|
|
$this->redirectUri = $redirectUri; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function setBody(string $body) : void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->body = $body; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function setDownloadFilePath(string $filePath) : void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->downloadFilePath = $filePath; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function setDownloadFileName(string $fileName) : void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->downloadFileName = $fileName; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
public function getDownloadFilePath() : ?string |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->downloadFilePath; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getDownloadFileName() : ?string |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->downloadFileName; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
public function setCustomContentType(string $contentType) : void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->customContentType = $contentType; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
public function getCustomContentType() : ?string |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->customContentType; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function setData(array $body) : void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->body = json_encode($body); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function setOutputType(int $type) : void |
|
157
|
|
|
{ |
|
158
|
|
|
if ($type >=1 && $type<=7) { |
|
159
|
|
|
$this->outputType = $type; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function getOutputType() : int |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->outputType; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function getHeaders() : array |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->headers; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getStatusCode() : int |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->statusCode; |
|
176
|
|
|
} |
|
177
|
|
|
public function getBody() : string |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->body; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getRedirectUri() : string |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->redirectUri; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function sendHeaders() : void |
|
188
|
|
|
{ |
|
189
|
|
|
if ($this->outputType === Router::JSON) { |
|
190
|
|
|
$this->setHeader('Content-Type', 'application/json; charset=UTF-8'); |
|
191
|
|
|
} elseif ($this->outputType === Router::TEXT) { |
|
192
|
|
|
$this->setHeader('Content-Type', 'text/plain; charset=UTF-8'); |
|
193
|
|
|
} elseif ($this->outputType === Router::XML) { |
|
194
|
|
|
$this->setHeader('Content-Type', 'application/xml; charset=UTF-8'); |
|
195
|
|
|
} elseif ($this->outputType === Router::HTML) { |
|
196
|
|
|
$this->setHeader('Content-Type', 'text/html; charset=UTF-8'); |
|
197
|
|
|
} elseif ($this->outputType === Router::CUSTOM) { |
|
198
|
|
|
$this->setHeader('Content-Type', $this->customContentType ?? 'text/plain'); |
|
199
|
|
|
} elseif ($this->outputType === Router::DOWNLOAD) { |
|
200
|
|
|
$this->setHeader('Content-Type', 'application/octet-stream'); |
|
201
|
|
|
if ($this->downloadFileName !== null) { |
|
202
|
|
|
$this->setHeader( |
|
203
|
|
|
'Content-Disposition', |
|
204
|
|
|
'attachment; filename="'.$this->downloadFileName.'"' |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
if (!headers_sent()) { |
|
212
|
|
|
if ($this->outputType === Router::REDIRECT && $this->redirectUri !== null) { |
|
213
|
|
|
header('location:' . $this->redirectUri); |
|
214
|
|
|
exit; |
|
215
|
|
|
} |
|
216
|
|
|
foreach ($this->headers as $name => $value) { |
|
217
|
|
|
header($name . ': ' . $value, false, $this->statusCode); |
|
218
|
|
|
} |
|
219
|
|
|
header( |
|
220
|
|
|
sprintf( |
|
221
|
|
|
'HTTP/%s %s %s', |
|
222
|
|
|
$this->version, |
|
223
|
|
|
$this->statusCode, |
|
224
|
|
|
self::$statusTexts[$this->statusCode] |
|
225
|
|
|
), |
|
226
|
|
|
true, |
|
227
|
|
|
$this->statusCode |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function sendContent() : void |
|
233
|
|
|
{ |
|
234
|
|
|
echo $this->getContent(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
private function getContent() |
|
238
|
|
|
{ |
|
239
|
|
|
if ($this->downloadFilePath !== null && file_exists($this->downloadFilePath)) { |
|
240
|
|
|
return file_get_contents($this->downloadFilePath); |
|
241
|
|
|
} |
|
242
|
|
|
return $this->body; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function send() : void |
|
246
|
|
|
{ |
|
247
|
|
|
$this->sendHeaders(); |
|
248
|
|
|
$this->sendContent(); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|