|
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
|
|
|
class Request |
|
40
|
|
|
{ |
|
41
|
|
|
use ProxyTrait; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @const string HTTP content types |
|
45
|
|
|
*/ |
|
46
|
|
|
const TYPE_ANY = '*/*'; |
|
47
|
|
|
const TYPE_HTML = 'text/html'; |
|
48
|
|
|
const TYPE_JSON = 'application/json'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Init instance |
|
52
|
|
|
* |
|
53
|
|
|
* @throws ComponentException |
|
54
|
|
|
*/ |
|
55
|
|
|
protected static function initInstance() |
|
56
|
|
|
{ |
|
57
|
|
|
throw new ComponentException("Class `Proxy\\Request` required external initialization"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Retrieve a member of the $_GET super global |
|
62
|
|
|
* |
|
63
|
|
|
* If no $key is passed, returns the entire $_GET array. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @param string $default Default value to use if key not found |
|
67
|
|
|
* @return string Returns null if key does not exist |
|
68
|
|
|
*/ |
|
69
|
50 |
|
public static function getQuery($key = null, $default = null) |
|
70
|
|
|
{ |
|
71
|
50 |
|
return RequestFactory::get($key, self::getInstance()->getQueryParams(), $default); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieve a member of the $_POST super global |
|
76
|
|
|
* |
|
77
|
|
|
* If no $key is passed, returns the entire $_POST array. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $key |
|
80
|
|
|
* @param string $default Default value to use if key not found |
|
81
|
|
|
* @return string Returns null if key does not exist |
|
82
|
|
|
*/ |
|
83
|
43 |
|
public static function getPost($key = null, $default = null) |
|
84
|
|
|
{ |
|
85
|
43 |
|
return RequestFactory::get($key, (array)self::getInstance()->getParsedBody(), $default); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Retrieve a member of the $_SERVER super global |
|
90
|
|
|
* |
|
91
|
|
|
* If no $key is passed, returns the entire $_SERVER array. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $key |
|
94
|
|
|
* @param string $default Default value to use if key not found |
|
95
|
|
|
* @return string Returns null if key does not exist |
|
96
|
|
|
*/ |
|
97
|
44 |
|
public static function getServer($key = null, $default = null) |
|
98
|
|
|
{ |
|
99
|
44 |
|
return RequestFactory::get($key, self::getInstance()->getServerParams(), $default); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieve a member of the $_COOKIE super global |
|
104
|
|
|
* |
|
105
|
|
|
* If no $key is passed, returns the entire $_COOKIE array. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $key |
|
108
|
|
|
* @param string $default Default value to use if key not found |
|
109
|
|
|
* @return string Returns null if key does not exist |
|
110
|
|
|
*/ |
|
111
|
41 |
|
public static function getCookie($key = null, $default = null) |
|
112
|
|
|
{ |
|
113
|
41 |
|
return RequestFactory::get($key, self::getInstance()->getCookieParams(), $default); |
|
114
|
|
|
} |
|
115
|
|
|
/** |
|
116
|
|
|
* Retrieve a member of the $_ENV super global |
|
117
|
|
|
* |
|
118
|
|
|
* If no $key is passed, returns the entire $_ENV array. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $key |
|
121
|
|
|
* @param string $default Default value to use if key not found |
|
122
|
|
|
* @return string Returns null if key does not exist |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function getEnv($key = null, $default = null) |
|
125
|
|
|
{ |
|
126
|
|
|
return RequestFactory::get($key, $_ENV, $default); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Search for a header value |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $header |
|
133
|
|
|
* @param mixed $default |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
6 |
|
public static function getHeader($header, $default = null) |
|
137
|
|
|
{ |
|
138
|
6 |
|
return RequestFactory::getHeader($header, self::getInstance()->getHeaders(), $default); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Access values contained in the superglobals as public members |
|
143
|
|
|
* Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $key |
|
146
|
|
|
* @param null $default |
|
147
|
|
|
* @return string|null |
|
148
|
|
|
* @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
|
149
|
|
|
*/ |
|
150
|
50 |
|
public static function getParam($key, $default = null) |
|
151
|
|
|
{ |
|
152
|
|
|
return |
|
153
|
50 |
|
self::getQuery($key) ?? |
|
154
|
42 |
|
self::getPost($key) ?? |
|
155
|
41 |
|
self::getCookie($key) ?? |
|
156
|
40 |
|
self::getServer($key) ?? |
|
157
|
50 |
|
$default; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get all params from GET and POST or PUT |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
8 |
|
public static function getParams() |
|
166
|
|
|
{ |
|
167
|
8 |
|
$query = (array) self::getInstance()->getQueryParams(); |
|
168
|
8 |
|
$body = (array) self::getInstance()->getParsedBody(); |
|
169
|
|
|
|
|
170
|
8 |
|
return array_merge($body, $query); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get uploaded file |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $name |
|
177
|
|
|
* @return \Zend\Diactoros\UploadedFile |
|
178
|
|
|
*/ |
|
179
|
|
|
public static function getFile($name) |
|
180
|
|
|
{ |
|
181
|
|
|
return RequestFactory::get($name, self::getInstance()->getUploadedFiles()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the client's IP address |
|
186
|
|
|
* |
|
187
|
|
|
* @param bool $checkProxy |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public static function getClientIp($checkProxy = true) |
|
191
|
|
|
{ |
|
192
|
|
|
if ($checkProxy && self::getServer('HTTP_CLIENT_IP') != null) { |
|
193
|
|
|
return self::getServer('HTTP_CLIENT_IP'); |
|
194
|
|
|
} elseif ($checkProxy && self::getServer('HTTP_X_FORWARDED_FOR') != null) { |
|
195
|
|
|
return self::getServer('HTTP_X_FORWARDED_FOR'); |
|
196
|
|
|
} else { |
|
197
|
|
|
return self::getServer('REMOTE_ADDR'); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get module |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
43 |
|
public static function getModule() |
|
207
|
|
|
{ |
|
208
|
43 |
|
return self::getParam('_module', Router::getDefaultModule()); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get controller |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
43 |
|
public static function getController() |
|
217
|
|
|
{ |
|
218
|
43 |
|
return self::getParam('_controller', Router::getDefaultController()); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get method |
|
223
|
|
|
* |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
|
|
public static function getMethod() |
|
227
|
|
|
{ |
|
228
|
|
|
return self::getParam('_method', self::getInstance()->getMethod()); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Get Accept MIME Type |
|
233
|
|
|
* |
|
234
|
|
|
* @todo: refactoring this method, accept types should be stored in static? variable |
|
235
|
|
|
* @param array $allowTypes |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
6 |
|
public static function getAccept($allowTypes = []) |
|
239
|
|
|
{ |
|
240
|
6 |
|
static $accept; |
|
241
|
|
|
|
|
242
|
6 |
|
if (!$accept) { |
|
243
|
|
|
// get header from request |
|
244
|
1 |
|
$header = self::getHeader('accept'); |
|
245
|
|
|
|
|
246
|
|
|
// nothing ... |
|
247
|
1 |
|
if (!$header) { |
|
|
|
|
|
|
248
|
|
|
// @todo Do something for this |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
// make array if types |
|
252
|
1 |
|
$header = explode(',', $header); |
|
253
|
1 |
|
$header = array_map('trim', $header); |
|
254
|
|
|
|
|
255
|
|
|
// result store |
|
256
|
1 |
|
$types = []; |
|
257
|
|
|
|
|
258
|
1 |
|
foreach ($header as $a) { |
|
259
|
|
|
// the default quality is 1. |
|
260
|
1 |
|
$q = 1; |
|
261
|
|
|
// check if there is a different quality |
|
262
|
1 |
|
if (strpos($a, ';q=') or strpos($a, '; q=')) { |
|
263
|
|
|
// divide "mime/type;q=X" into two parts: "mime/type" i "X" |
|
264
|
|
|
$res = preg_split('/;([ ]?)q=/', $a); |
|
265
|
|
|
$a = $res[0]; |
|
266
|
|
|
$q = $res[1]; |
|
267
|
|
|
} |
|
268
|
|
|
// remove other extension |
|
269
|
1 |
|
if (strpos($a, ';')) { |
|
270
|
|
|
$a = substr($a, 0, strpos($a, ';')); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
// mime-type $a is accepted with the quality $q |
|
274
|
|
|
// WARNING: $q == 0 means, that mime-type isn’t supported! |
|
275
|
1 |
|
$types[$a] = (float) $q; |
|
276
|
|
|
} |
|
277
|
1 |
|
arsort($types); |
|
278
|
1 |
|
$accept = $types; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
// if no parameter was passed, just return parsed data |
|
282
|
6 |
|
if (empty($allowTypes)) { |
|
283
|
|
|
return $accept; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
6 |
|
$mimeTypes = array_map('strtolower', $allowTypes); |
|
287
|
|
|
|
|
288
|
|
|
// let’s check our supported types: |
|
289
|
6 |
|
foreach ($accept as $mime => $q) { |
|
290
|
6 |
|
if ($q && in_array($mime, $mimeTypes)) { |
|
291
|
6 |
|
return $mime; |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
// no mime-type found |
|
295
|
|
|
return null; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Check CLI |
|
300
|
|
|
* |
|
301
|
|
|
* @return bool |
|
302
|
|
|
*/ |
|
303
|
|
|
public static function isCli() |
|
304
|
|
|
{ |
|
305
|
|
|
return (PHP_SAPI === 'cli'); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Check HTTP |
|
310
|
|
|
* |
|
311
|
|
|
* @return bool |
|
312
|
|
|
*/ |
|
313
|
3 |
|
public static function isHttp() |
|
314
|
|
|
{ |
|
315
|
3 |
|
return (PHP_SAPI !== 'cli'); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Is this a GET method request? |
|
320
|
|
|
* |
|
321
|
|
|
* @return bool |
|
322
|
|
|
*/ |
|
323
|
|
|
public static function isGet() |
|
324
|
|
|
{ |
|
325
|
|
|
return (self::getInstance()->getMethod() === RequestMethod::GET); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Is this a POST method request? |
|
330
|
|
|
* |
|
331
|
|
|
* @return bool |
|
332
|
|
|
*/ |
|
333
|
|
|
public static function isPost() |
|
334
|
|
|
{ |
|
335
|
|
|
return (self::getInstance()->getMethod() === RequestMethod::POST); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Is this a PUT method request? |
|
340
|
|
|
* |
|
341
|
|
|
* @return bool |
|
342
|
|
|
*/ |
|
343
|
|
|
public static function isPut() |
|
344
|
|
|
{ |
|
345
|
|
|
return (self::getInstance()->getMethod() === RequestMethod::PUT); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Is this a DELETE method request? |
|
350
|
|
|
* |
|
351
|
|
|
* @return bool |
|
352
|
|
|
*/ |
|
353
|
|
|
public static function isDelete() |
|
354
|
|
|
{ |
|
355
|
|
|
return (self::getInstance()->getMethod() === RequestMethod::DELETE); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Is the request a Javascript XMLHttpRequest? |
|
360
|
|
|
* |
|
361
|
|
|
* @return bool |
|
362
|
|
|
*/ |
|
363
|
6 |
|
public static function isXmlHttpRequest() |
|
364
|
|
|
{ |
|
365
|
6 |
|
return (self::getHeader('X-Requested-With') == 'XMLHttpRequest'); |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
|
|
|
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.