1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bluz Framework Component |
4
|
|
|
* |
5
|
|
|
* @copyright Bluz PHP Team |
6
|
|
|
* @link https://github.com/bluzphp/framework |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Bluz\Proxy; |
12
|
|
|
|
13
|
|
|
use Bluz\Common\Exception\ComponentException; |
14
|
|
|
use Bluz\Http\RequestMethod; |
15
|
|
|
use Bluz\Request\RequestFactory; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
use Zend\Diactoros\ServerRequest as Instance; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Proxy to Request |
21
|
|
|
* |
22
|
|
|
* Example of usage |
23
|
|
|
* <code> |
24
|
|
|
* use Bluz\Proxy\Request; |
25
|
|
|
* |
26
|
|
|
* Request::getParam('foo'); |
27
|
|
|
* </code> |
28
|
|
|
* |
29
|
|
|
* @package Bluz\Proxy |
30
|
|
|
* @author Anton Shevchuk |
31
|
|
|
* |
32
|
|
|
* @todo Proxy class should be clean |
33
|
|
|
* |
34
|
|
|
* @method static Instance getInstance() |
35
|
|
|
* |
36
|
|
|
* @method static UriInterface getUri() |
37
|
|
|
* @see \Zend\Diactoros\RequestTrait::getUri() |
38
|
|
|
*/ |
39
|
|
|
final class Request |
40
|
|
|
{ |
41
|
|
|
use ProxyTrait; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @const string HTTP content types |
45
|
|
|
*/ |
46
|
|
|
public const TYPE_ANY = '*/*'; |
47
|
|
|
public const TYPE_HTML = 'text/html'; |
48
|
|
|
public const TYPE_JSON = 'application/json'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array|null Accepted type |
52
|
|
|
*/ |
53
|
|
|
static private $accept; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Init instance |
57
|
|
|
* |
58
|
|
|
* @throws ComponentException |
59
|
|
|
*/ |
60
|
|
|
private static function initInstance() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
throw new ComponentException('Class `Proxy\\Request` required external initialization'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve a member of the $_GET super global |
67
|
|
|
* |
68
|
|
|
* If no $key is passed, returns the entire $_GET array. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
* @param string $default Default value to use if key not found |
72
|
|
|
* |
73
|
|
|
* @return string Returns null if key does not exist |
74
|
|
|
*/ |
75
|
77 |
|
public static function getQuery($key = null, $default = null) |
76
|
|
|
{ |
77
|
77 |
|
return RequestFactory::get($key, self::getInstance()->getQueryParams(), $default); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retrieve a member of the $_POST super global |
82
|
|
|
* |
83
|
|
|
* If no $key is passed, returns the entire $_POST array. |
84
|
|
|
* |
85
|
|
|
* @param string $key |
86
|
|
|
* @param string $default Default value to use if key not found |
87
|
|
|
* |
88
|
|
|
* @return string Returns null if key does not exist |
89
|
|
|
*/ |
90
|
67 |
|
public static function getPost($key = null, $default = null) |
91
|
|
|
{ |
92
|
67 |
|
return RequestFactory::get($key, (array)self::getInstance()->getParsedBody(), $default); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retrieve a member of the $_SERVER super global |
97
|
|
|
* |
98
|
|
|
* If no $key is passed, returns the entire $_SERVER array. |
99
|
|
|
* |
100
|
|
|
* @param string $key |
101
|
|
|
* @param string $default Default value to use if key not found |
102
|
|
|
* |
103
|
|
|
* @return string Returns null if key does not exist |
104
|
|
|
*/ |
105
|
8 |
|
public static function getServer($key = null, $default = null) |
106
|
|
|
{ |
107
|
8 |
|
return RequestFactory::get($key, self::getInstance()->getServerParams(), $default); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieve a member of the $_COOKIE super global |
112
|
|
|
* |
113
|
|
|
* If no $key is passed, returns the entire $_COOKIE array. |
114
|
|
|
* |
115
|
|
|
* @param string $key |
116
|
|
|
* @param string $default Default value to use if key not found |
117
|
|
|
* |
118
|
|
|
* @return string Returns null if key does not exist |
119
|
|
|
*/ |
120
|
1 |
|
public static function getCookie($key = null, $default = null) |
121
|
|
|
{ |
122
|
1 |
|
return RequestFactory::get($key, self::getInstance()->getCookieParams(), $default); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieve a member of the $_ENV super global |
127
|
|
|
* |
128
|
|
|
* If no $key is passed, returns the entire $_ENV array. |
129
|
|
|
* |
130
|
|
|
* @param string $key |
131
|
|
|
* @param string $default Default value to use if key not found |
132
|
|
|
* |
133
|
|
|
* @return string Returns null if key does not exist |
134
|
|
|
*/ |
135
|
1 |
|
public static function getEnv($key = null, $default = null) |
136
|
|
|
{ |
137
|
1 |
|
return RequestFactory::get($key, $_ENV, $default); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Search for a header value |
142
|
|
|
* |
143
|
|
|
* @param string $header |
144
|
|
|
* @param mixed $default |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
40 |
|
public static function getHeader($header, $default = null) |
149
|
|
|
{ |
150
|
40 |
|
return RequestFactory::getHeader($header, self::getInstance()->getHeaders(), $default); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Access values contained in the superglobals as public members |
155
|
|
|
* Order of precedence: 1. GET, 2. POST |
156
|
|
|
* |
157
|
|
|
* @param string $key |
158
|
|
|
* @param null $default |
159
|
|
|
* |
160
|
|
|
* @return string|null |
161
|
|
|
* @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
162
|
|
|
*/ |
163
|
77 |
|
public static function getParam($key, $default = null) |
164
|
|
|
{ |
165
|
|
|
return |
166
|
77 |
|
self::getQuery($key) ?? |
167
|
66 |
|
self::getPost($key) ?? |
168
|
77 |
|
$default; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get all params from GET and POST or PUT |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
36 |
|
public static function getParams() |
177
|
|
|
{ |
178
|
36 |
|
$body = (array)self::getInstance()->getParsedBody(); |
179
|
36 |
|
$query = (array)self::getInstance()->getQueryParams(); |
180
|
36 |
|
return array_merge($body, $query); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get uploaded file |
185
|
|
|
* |
186
|
|
|
* @param string $name |
187
|
|
|
* |
188
|
|
|
* @return \Zend\Diactoros\UploadedFile |
189
|
|
|
*/ |
190
|
|
|
public static function getFile($name) |
191
|
|
|
{ |
192
|
|
|
return RequestFactory::get($name, self::getInstance()->getUploadedFiles()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get the client's IP address |
197
|
|
|
* |
198
|
|
|
* @param bool $checkProxy |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
1 |
|
public static function getClientIp($checkProxy = true) |
203
|
|
|
{ |
204
|
1 |
|
$result = null; |
205
|
1 |
|
if ($checkProxy) { |
206
|
1 |
|
$result = self::getServer('HTTP_CLIENT_IP') ?? self::getServer('HTTP_X_FORWARDED_FOR') ?? null; |
207
|
|
|
} |
208
|
1 |
|
return $result ?? self::getServer('REMOTE_ADDR'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get module |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
46 |
|
public static function getModule(): string |
217
|
|
|
{ |
218
|
46 |
|
return self::getParam('_module', Router::getDefaultModule()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get controller |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
46 |
|
public static function getController(): string |
227
|
|
|
{ |
228
|
46 |
|
return self::getParam('_controller', Router::getDefaultController()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get method |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
25 |
|
public static function getMethod(): string |
237
|
|
|
{ |
238
|
25 |
|
return self::getParam('_method', self::getInstance()->getMethod()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get Accept MIME Type |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
40 |
|
public static function getAccept(): array |
247
|
|
|
{ |
248
|
40 |
|
if (!self::$accept) { |
249
|
|
|
// save to static variable |
250
|
40 |
|
self::$accept = []; |
251
|
|
|
|
252
|
|
|
// get header from request |
253
|
40 |
|
$header = self::getHeader('accept'); |
254
|
|
|
|
255
|
|
|
// nothing ... |
256
|
40 |
|
if (!$header) { |
257
|
37 |
|
return self::$accept; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
// make array if types |
261
|
3 |
|
$header = explode(',', $header); |
262
|
3 |
|
$header = array_map('trim', $header); |
263
|
|
|
|
264
|
3 |
|
foreach ($header as $a) { |
265
|
|
|
// the default quality is 1. |
266
|
3 |
|
$q = 1; |
267
|
|
|
// check if there is a different quality |
268
|
3 |
|
if (strpos($a, ';q=') or strpos($a, '; q=')) { |
269
|
|
|
// divide "mime/type;q=X" into two parts: "mime/type" i "X" |
270
|
|
|
[$a, $q] = preg_split('/;([ ]?)q=/', $a); |
271
|
|
|
} |
272
|
|
|
// remove other extension |
273
|
3 |
|
if (strpos($a, ';')) { |
274
|
|
|
$a = substr($a, 0, strpos($a, ';')); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// mime-type $a is accepted with the quality $q |
278
|
|
|
// WARNING: $q == 0 means, that mime-type isn’t supported! |
279
|
3 |
|
self::$accept[$a] = (float)$q; |
280
|
|
|
} |
281
|
3 |
|
arsort(self::$accept); |
282
|
|
|
} |
283
|
3 |
|
return self::$accept; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Reset accept for tests |
288
|
|
|
* |
289
|
|
|
* @return void |
290
|
|
|
*/ |
291
|
849 |
|
public static function resetAccept() |
292
|
|
|
{ |
293
|
849 |
|
self::$accept = null; |
294
|
849 |
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Check Accept header |
298
|
|
|
* |
299
|
|
|
* @param array $allowTypes |
300
|
|
|
* |
301
|
|
|
* @return string|false |
302
|
|
|
*/ |
303
|
40 |
|
public static function checkAccept(array $allowTypes = []) |
304
|
|
|
{ |
305
|
40 |
|
$accept = self::getAccept(); |
306
|
|
|
|
307
|
|
|
// if no parameter was passed, just return first mime type from parsed data |
308
|
40 |
|
if (empty($allowTypes)) { |
309
|
|
|
return current(array_keys($accept)); |
310
|
|
|
} |
311
|
|
|
|
312
|
40 |
|
$allowTypes = array_map('strtolower', $allowTypes); |
313
|
|
|
|
314
|
|
|
// let’s check our supported types: |
315
|
40 |
|
foreach ($accept as $mime => $quality) { |
316
|
3 |
|
if ($quality && in_array($mime, $allowTypes)) { |
317
|
3 |
|
return $mime; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
// no mime-type found |
321
|
38 |
|
return false; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Check CLI |
326
|
|
|
* |
327
|
|
|
* @return bool |
328
|
|
|
*/ |
329
|
1 |
|
public static function isCli(): bool |
330
|
|
|
{ |
331
|
1 |
|
return (PHP_SAPI === 'cli'); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Check HTTP |
336
|
|
|
* |
337
|
|
|
* @return bool |
338
|
|
|
*/ |
339
|
3 |
|
public static function isHttp(): bool |
340
|
|
|
{ |
341
|
3 |
|
return (PHP_SAPI !== 'cli'); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Is this a GET method request? |
346
|
|
|
* |
347
|
|
|
* @return bool |
348
|
|
|
*/ |
349
|
2 |
|
public static function isGet(): bool |
350
|
|
|
{ |
351
|
2 |
|
return (self::getInstance()->getMethod() === RequestMethod::GET); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Is this a POST method request? |
356
|
|
|
* |
357
|
|
|
* @return bool |
358
|
|
|
*/ |
359
|
2 |
|
public static function isPost(): bool |
360
|
|
|
{ |
361
|
2 |
|
return (self::getInstance()->getMethod() === RequestMethod::POST); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Is this a PUT method request? |
366
|
|
|
* |
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
1 |
|
public static function isPut(): bool |
370
|
|
|
{ |
371
|
1 |
|
return (self::getInstance()->getMethod() === RequestMethod::PUT); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Is this a DELETE method request? |
376
|
|
|
* |
377
|
|
|
* @return bool |
378
|
|
|
*/ |
379
|
1 |
|
public static function isDelete(): bool |
380
|
|
|
{ |
381
|
1 |
|
return (self::getInstance()->getMethod() === RequestMethod::DELETE); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Is the request a Javascript XMLHttpRequest? |
386
|
|
|
* |
387
|
|
|
* @return bool |
388
|
|
|
*/ |
389
|
9 |
|
public static function isXmlHttpRequest(): bool |
390
|
|
|
{ |
391
|
9 |
|
return (self::getHeader('X-Requested-With') === 'XMLHttpRequest'); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|