|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Suricate; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @SuppressWarnings("StaticAccess") |
|
9
|
|
|
*/ |
|
10
|
|
|
class Request |
|
11
|
|
|
{ |
|
12
|
|
|
const HTTP_METHOD_GET = 'GET'; |
|
13
|
|
|
const HTTP_METHOD_POST = 'POST'; |
|
14
|
|
|
const HTTP_METHOD_PUT = 'PUT'; |
|
15
|
|
|
const HTTP_METHOD_DELETE = 'DELETE'; |
|
16
|
|
|
const HTTP_METHOD_HEAD = 'HEAD'; |
|
17
|
|
|
const HTTP_METHOD_OPTIONS = 'OPTIONS'; |
|
18
|
|
|
|
|
19
|
|
|
private $method = self::HTTP_METHOD_GET; |
|
20
|
|
|
private $methods = [ |
|
21
|
|
|
self::HTTP_METHOD_GET => 'GET', |
|
22
|
|
|
self::HTTP_METHOD_POST => 'POST', |
|
23
|
|
|
self::HTTP_METHOD_PUT => 'PUT', |
|
24
|
|
|
self::HTTP_METHOD_DELETE => 'DELETE', |
|
25
|
|
|
self::HTTP_METHOD_HEAD => 'HEAD', |
|
26
|
|
|
self::HTTP_METHOD_OPTIONS => 'OPTIONS' |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
private $httpCodeString = [ |
|
30
|
|
|
100 => 'Continue', |
|
31
|
|
|
101 => 'Switching Protocols', |
|
32
|
|
|
102 => 'Processing', |
|
33
|
|
|
200 => 'OK', |
|
34
|
|
|
201 => 'Created', |
|
35
|
|
|
202 => 'Accepted', |
|
36
|
|
|
203 => 'Non-Authoritative Information', |
|
37
|
|
|
204 => 'No Content', |
|
38
|
|
|
205 => 'Reset Content', |
|
39
|
|
|
206 => 'Partial Content', |
|
40
|
|
|
207 => 'Multi-Status', |
|
41
|
|
|
208 => 'Already Reported', |
|
42
|
|
|
226 => 'IM Used', |
|
43
|
|
|
250 => 'Low on Storage Space', |
|
44
|
|
|
300 => 'Multiple Choices', |
|
45
|
|
|
301 => 'Moved Permanently', |
|
46
|
|
|
302 => 'Found', |
|
47
|
|
|
303 => 'See Other', |
|
48
|
|
|
304 => 'Not Modified', |
|
49
|
|
|
305 => 'Use Proxy', |
|
50
|
|
|
306 => '306 Switch Proxy', |
|
51
|
|
|
307 => 'Temporary Redirect', |
|
52
|
|
|
308 => 'Permanent Redirect', |
|
53
|
|
|
400 => 'Bad Request', |
|
54
|
|
|
401 => 'Unauthorized', |
|
55
|
|
|
402 => 'Payment Required', |
|
56
|
|
|
403 => 'Forbidden', |
|
57
|
|
|
404 => 'Not Found', |
|
58
|
|
|
405 => 'Method Not Allowed', |
|
59
|
|
|
406 => 'Not Acceptable', |
|
60
|
|
|
407 => 'Proxy Authentication Required', |
|
61
|
|
|
408 => 'Request Timeout', |
|
62
|
|
|
409 => 'Conflict', |
|
63
|
|
|
410 => 'Gone', |
|
64
|
|
|
411 => 'Length Required', |
|
65
|
|
|
412 => 'Precondition Failed', |
|
66
|
|
|
413 => 'Request Entity Too Large', |
|
67
|
|
|
414 => 'Request-URI Too Long', |
|
68
|
|
|
415 => 'Unsupported Media Type', |
|
69
|
|
|
416 => 'Requested Range Not Satisfiable', |
|
70
|
|
|
417 => 'Expectation Failed', |
|
71
|
|
|
422 => 'Unprocessable Entity', |
|
72
|
|
|
423 => 'Locked', |
|
73
|
|
|
424 => 'Failed Dependency', |
|
74
|
|
|
425 => 'Unordered Collection', |
|
75
|
|
|
426 => 'Upgrade Required', |
|
76
|
|
|
428 => 'Precondition Required', |
|
77
|
|
|
429 => 'Too Many Requests', |
|
78
|
|
|
431 => 'Request Header Fields Too Large', |
|
79
|
|
|
444 => 'No Response', |
|
80
|
|
|
449 => 'Retry With', |
|
81
|
|
|
450 => 'Blocked by Windows Parental Controls', |
|
82
|
|
|
494 => 'Request Header Too Large', |
|
83
|
|
|
495 => 'Cert Error', |
|
84
|
|
|
496 => 'No Cert', |
|
85
|
|
|
497 => 'HTTP to HTTPS', |
|
86
|
|
|
499 => 'Client Closed Request', |
|
87
|
|
|
500 => 'Internal Server Error', |
|
88
|
|
|
501 => 'Not Implemented', |
|
89
|
|
|
502 => 'Bad Gateway', |
|
90
|
|
|
503 => 'Service Unavailable', |
|
91
|
|
|
504 => 'Gateway Timeout', |
|
92
|
|
|
505 => 'HTTP Version Not Supported', |
|
93
|
|
|
506 => 'Variant Also Negotiates', |
|
94
|
|
|
507 => 'Insufficient Storage', |
|
95
|
|
|
508 => 'Loop Detected', |
|
96
|
|
|
509 => 'Bandwidth Limit Exceeded', |
|
97
|
|
|
510 => 'Not Extended', |
|
98
|
|
|
511 => 'Network Authentication Required' |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
private $httpCode; |
|
102
|
|
|
private $headers = []; |
|
103
|
|
|
private $requestUri = ''; |
|
104
|
|
|
private $url; |
|
105
|
|
|
private $body; |
|
106
|
|
|
private $path; |
|
107
|
|
|
private $query; |
|
108
|
|
|
|
|
109
|
18 |
|
public function __construct() |
|
110
|
|
|
{ |
|
111
|
18 |
|
$this->headers = []; |
|
112
|
18 |
|
$this->httpCode = 200; |
|
113
|
18 |
|
} |
|
114
|
|
|
|
|
115
|
9 |
|
public function parse() |
|
116
|
|
|
{ |
|
117
|
9 |
|
if (isset($_SERVER['REQUEST_URI'])) { |
|
118
|
5 |
|
$this->setRequestUri($_SERVER['REQUEST_URI']); |
|
119
|
5 |
|
$parseResult = parse_url($_SERVER['REQUEST_URI']); |
|
120
|
5 |
|
$this->path = dataGet($parseResult, 'path'); |
|
121
|
5 |
|
$this->query = dataGet($parseResult, 'query'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
9 |
|
if (isset($_SERVER['REQUEST_METHOD'])) { |
|
125
|
1 |
|
$this->setMethod($_SERVER['REQUEST_METHOD']); |
|
126
|
|
|
} |
|
127
|
9 |
|
if (isset($_POST['_method'])) { |
|
128
|
5 |
|
$this->setMethod($_POST['_method']); |
|
129
|
|
|
} |
|
130
|
9 |
|
} |
|
131
|
|
|
|
|
132
|
7 |
|
public function setMethod($method) |
|
133
|
|
|
{ |
|
134
|
7 |
|
if (!isset($this->methods[$method])) { |
|
135
|
1 |
|
throw new \InvalidArgumentException( |
|
136
|
1 |
|
'Invalid HTTP Method ' . $method |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
7 |
|
$this->method = $method; |
|
141
|
|
|
|
|
142
|
7 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
3 |
|
public function getMethod() |
|
146
|
|
|
{ |
|
147
|
3 |
|
return $this->method; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
public function setUrl($url) |
|
151
|
|
|
{ |
|
152
|
2 |
|
$this->url = $url; |
|
153
|
|
|
|
|
154
|
2 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
2 |
|
public function getUrl() |
|
157
|
|
|
{ |
|
158
|
2 |
|
return $this->url; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
10 |
|
public function setRequestUri($uri) |
|
162
|
|
|
{ |
|
163
|
10 |
|
$this->requestUri = $uri; |
|
164
|
|
|
|
|
165
|
10 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
11 |
|
public function getRequestUri() |
|
169
|
|
|
{ |
|
170
|
11 |
|
return $this->requestUri; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
public function getPath() |
|
174
|
|
|
{ |
|
175
|
1 |
|
return $this->path; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
public function getQuery() |
|
179
|
|
|
{ |
|
180
|
1 |
|
return $this->query; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
public static function getPostParam($variable, $defaultValue = null) |
|
184
|
|
|
{ |
|
185
|
1 |
|
if (array_key_exists($variable, $_POST)) { |
|
186
|
1 |
|
return $_POST[$variable]; |
|
187
|
|
|
} |
|
188
|
1 |
|
return $defaultValue; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
1 |
|
public static function getParam($variable, $defaultValue = null) |
|
192
|
|
|
{ |
|
193
|
1 |
|
if (array_key_exists($variable, $_GET)) { |
|
194
|
1 |
|
return $_GET[$variable]; |
|
195
|
|
|
} |
|
196
|
1 |
|
if (array_key_exists($variable, $_POST)) { |
|
197
|
1 |
|
return $_POST[$variable]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
1 |
|
return $defaultValue; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
public static function hasParam($variable) |
|
204
|
|
|
{ |
|
205
|
1 |
|
return array_key_exists($variable, $_GET) || |
|
206
|
1 |
|
array_key_exists($variable, $_POST); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Set request headers |
|
211
|
|
|
* |
|
212
|
|
|
* @param array $headers Headers to set key => $value |
|
213
|
|
|
* @return Request |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function setHeaders(array $headers): Request |
|
216
|
|
|
{ |
|
217
|
1 |
|
$this->headers = $headers; |
|
218
|
|
|
|
|
219
|
1 |
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Add specific header |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $header header name |
|
226
|
|
|
* @param string $value header value |
|
227
|
|
|
* @return Request |
|
228
|
|
|
*/ |
|
229
|
1 |
|
public function addHeader($header, $value): Request |
|
230
|
|
|
{ |
|
231
|
1 |
|
$this->headers[$header] = $value; |
|
232
|
|
|
|
|
233
|
1 |
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get request headers |
|
238
|
|
|
* |
|
239
|
|
|
* @return array |
|
240
|
|
|
*/ |
|
241
|
1 |
|
public function getHeaders(): array |
|
242
|
|
|
{ |
|
243
|
1 |
|
return $this->headers; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
1 |
|
public function setContentType( |
|
247
|
|
|
string $contentType, |
|
248
|
|
|
$encoding = null |
|
249
|
|
|
): Request { |
|
250
|
1 |
|
if ($encoding !== null) { |
|
251
|
1 |
|
$contentType .= '; charset=' . $encoding; |
|
252
|
|
|
} |
|
253
|
1 |
|
$this->addHeader('Content-type', $contentType); |
|
254
|
|
|
|
|
255
|
1 |
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
1 |
|
public function setBody(string $body): Request |
|
259
|
|
|
{ |
|
260
|
1 |
|
$this->body = $body; |
|
261
|
|
|
|
|
262
|
1 |
|
return $this; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
1 |
|
public function getBody(): string |
|
266
|
|
|
{ |
|
267
|
1 |
|
return $this->body; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function write() |
|
271
|
|
|
{ |
|
272
|
|
|
if (!headers_sent()) { |
|
273
|
|
|
if (substr(php_sapi_name(), 0, 3) == 'cgi') { |
|
274
|
|
|
$headerString = 'Status: ' . $this->getStringForHttpCode(); |
|
275
|
|
|
} else { |
|
276
|
|
|
$headerString = 'HTTP/1.1 ' . $this->getStringForHttpCode(); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
header($headerString); |
|
280
|
|
|
|
|
281
|
|
|
// Send headers |
|
282
|
|
|
foreach ($this->headers as $headerName => $headerValue) { |
|
283
|
|
|
header($headerName . ':' . $headerValue); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
TODO HANDLE HTTP RESPONSE CODE |
|
289
|
|
|
*/ |
|
290
|
|
|
if ($this->httpCode !== null) { |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
// Send body |
|
294
|
|
|
echo $this->body; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
// |
|
298
|
|
|
// HTTP Code |
|
299
|
|
|
// |
|
300
|
|
|
|
|
301
|
1 |
|
public function setHttpCode($code): Request |
|
302
|
|
|
{ |
|
303
|
1 |
|
$this->httpCode = $code; |
|
304
|
|
|
|
|
305
|
1 |
|
return $this; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
1 |
|
public function getHttpCode() |
|
309
|
|
|
{ |
|
310
|
1 |
|
return $this->httpCode; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Flash message |
|
315
|
|
|
* |
|
316
|
|
|
* @param string $type message type |
|
317
|
|
|
* @param array|string $data data to be flashed |
|
318
|
|
|
*/ |
|
319
|
|
|
public function flash(string $type, $data) |
|
320
|
|
|
{ |
|
321
|
|
|
Flash::writeMessage($type, $data); |
|
322
|
|
|
|
|
323
|
|
|
return $this; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
public function flashData($name, $value) |
|
327
|
|
|
{ |
|
328
|
|
|
Flash::writeData($name, $value); |
|
329
|
|
|
|
|
330
|
|
|
return $this; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
public function redirect($url, $httpCode = 302) |
|
334
|
|
|
{ |
|
335
|
|
|
$this->setHttpCode($httpCode); |
|
336
|
|
|
$this->addHeader('Location', $url); |
|
337
|
|
|
|
|
338
|
|
|
$this->write(); |
|
339
|
|
|
die(); |
|
|
|
|
|
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
public function redirectWithSuccess($url, $message) |
|
343
|
|
|
{ |
|
344
|
|
|
$this->flash('success', $message)->redirect($url); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
public function redirectWithInfo($url, $message) |
|
348
|
|
|
{ |
|
349
|
|
|
$this->flash('info', $message)->redirect($url); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
public function redirectWithError($url, $message) |
|
353
|
|
|
{ |
|
354
|
|
|
$this->flash('error', $message)->redirect($url); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
public function redirectWithData($url, $key, $value) |
|
358
|
|
|
{ |
|
359
|
|
|
$this->flashData($key, $value)->redirect($url); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* Check if request has a 200 OK Code |
|
364
|
|
|
* |
|
365
|
|
|
* @return boolean |
|
366
|
|
|
*/ |
|
367
|
1 |
|
public function isOK(): bool |
|
368
|
|
|
{ |
|
369
|
1 |
|
return $this->httpCode == 200; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Check if request has a 3XX HTTP code |
|
374
|
|
|
* |
|
375
|
|
|
* @return boolean |
|
376
|
|
|
*/ |
|
377
|
1 |
|
public function isRedirect(): bool |
|
378
|
|
|
{ |
|
379
|
1 |
|
return $this->httpCode >= 300 && $this->httpCode < 400; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Check is request has a 4XX HTTP code |
|
384
|
|
|
* |
|
385
|
|
|
* @return boolean |
|
386
|
|
|
*/ |
|
387
|
1 |
|
public function isClientError(): bool |
|
388
|
|
|
{ |
|
389
|
1 |
|
return $this->httpCode >= 400 && $this->httpCode < 500; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Check if request has a 5XX HTTP code |
|
394
|
|
|
* |
|
395
|
|
|
* @return boolean |
|
396
|
|
|
*/ |
|
397
|
1 |
|
public function isServerError(): bool |
|
398
|
|
|
{ |
|
399
|
1 |
|
return $this->httpCode >= 500 && $this->httpCode < 600; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
1 |
|
private function getStringForHttpCode() |
|
403
|
|
|
{ |
|
404
|
1 |
|
if (isset($this->httpCodeString[$this->httpCode])) { |
|
405
|
1 |
|
return $this->httpCode . |
|
406
|
1 |
|
' ' . |
|
407
|
1 |
|
$this->httpCodeString[$this->httpCode]; |
|
408
|
|
|
} |
|
409
|
1 |
|
} |
|
410
|
|
|
} |
|
411
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.