|
1
|
|
|
<?php |
|
2
|
|
|
namespace suda\framework\server; |
|
3
|
|
|
|
|
4
|
|
|
use suda\framework\server\request\UriParser; |
|
5
|
|
|
use suda\framework\server\request\IndexFinder; |
|
6
|
|
|
use suda\framework\server\request\UploadedFile; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* 请求包装器 |
|
10
|
|
|
* 包装PHP请求 |
|
11
|
|
|
*/ |
|
12
|
|
|
class RequestWrapper |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* 远程地址 |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $remoteAddr = '0.0.0.0'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* 获取本地HOST |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $host = '127.0.0.1'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* 获取本地端口 |
|
30
|
|
|
* |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $port = 80; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 是否为安全模式 |
|
37
|
|
|
* |
|
38
|
|
|
* @var boolean |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $secure = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* 请求URI |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $uri = '/'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* 请求参数 |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $method = 'GET'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* 查询参数($_GET) |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $query = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* 表单参数($_POST) |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $parameter = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* 文件包装 |
|
72
|
|
|
* |
|
73
|
|
|
* @var UploadedFile[] |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $files = []; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* 请求索引 |
|
79
|
|
|
* |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $index; |
|
83
|
|
|
|
|
84
|
|
|
public static function create() |
|
85
|
|
|
{ |
|
86
|
|
|
$request = new static; |
|
87
|
|
|
$request->setRemoteAddr($request->filterRemoteAddr()); |
|
88
|
|
|
$request->setMethod(strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET')); |
|
89
|
|
|
$request->setHost($request->getHttpHost()); |
|
90
|
|
|
$request->setSecure($request->getSecure()); |
|
91
|
|
|
$request->setPort($request->getServerPort()); |
|
92
|
|
|
$request->createFiles(); |
|
93
|
|
|
$request->createUri(); |
|
94
|
|
|
$request->setParameter($_POST); |
|
|
|
|
|
|
95
|
|
|
return $request; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* 获取IP地址 |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
private function filterRemoteAddr():string |
|
104
|
|
|
{ |
|
105
|
|
|
static $ipFrom = ['HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR','HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP','HTTP_FORWARDED_FOR','HTTP_FORWARDED','REMOTE_ADDR']; |
|
106
|
|
|
foreach ($ipFrom as $key) { |
|
107
|
|
|
if (array_key_exists($key, $_SERVER)) { |
|
108
|
|
|
foreach (explode(',', $_SERVER[$key]) as $ip) { |
|
109
|
|
|
$ip = trim($ip); |
|
110
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { |
|
111
|
|
|
return $ip; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return '0.0.0.0'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* 从请求投中获取HOST |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
private function getHttpHost():string |
|
125
|
|
|
{ |
|
126
|
|
|
if (array_key_exists('HTTP_HOST', $_SERVER)) { |
|
127
|
|
|
if (strpos($rawHost, ':')) { |
|
|
|
|
|
|
128
|
|
|
list($host, $port) = \explode(':', $host); |
|
|
|
|
|
|
129
|
|
|
return $host; |
|
130
|
|
|
} |
|
131
|
|
|
return $_SERVER['HTTP_HOST']; |
|
132
|
|
|
} |
|
133
|
|
|
return 'localhost'; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* 获取端口 |
|
138
|
|
|
* |
|
139
|
|
|
* @return integer |
|
140
|
|
|
*/ |
|
141
|
|
|
private function getServerPort():int |
|
142
|
|
|
{ |
|
143
|
|
|
if (array_key_exists('SERVER_PORT', $_SERVER)) { |
|
144
|
|
|
return $_SERVER['SERVER_PORT']; |
|
145
|
|
|
} |
|
146
|
|
|
return $this->getSecure()?443:80; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* 获取HTTP |
|
151
|
|
|
* |
|
152
|
|
|
* @return boolean |
|
153
|
|
|
*/ |
|
154
|
|
|
private function getSecure():bool |
|
155
|
|
|
{ |
|
156
|
|
|
$https = array_key_exists('HTTPS', $_SERVER) && strcasecmp($_SERVER['HTTPS'], 'off') != 0; |
|
157
|
|
|
$scheme = array_key_exists('REQUEST_SCHEME', $_SERVER) && strcasecmp($_SERVER['REQUEST_SCHEME'], 'https') === 0; |
|
158
|
|
|
return $https || $scheme; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* 创建请求文件 |
|
163
|
|
|
* |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
|
|
private function createFiles() |
|
167
|
|
|
{ |
|
168
|
|
|
foreach ($_FILES as $name => $file) { |
|
169
|
|
|
$this->files[$name] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function createUri() |
|
174
|
|
|
{ |
|
175
|
|
|
$index = new IndexFinder; |
|
176
|
|
|
$this->index = $index->getIndexFile(); |
|
177
|
|
|
$url = new UriParser($_SERVER['REQUEST_URI'], $index); |
|
|
|
|
|
|
178
|
|
|
$this->query = $url->getQuery(); |
|
179
|
|
|
$this->uri = $url->getUri(); |
|
180
|
|
|
$_GET = $this->query; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get 远程地址 |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getRemoteAddr() |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->remoteAddr; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Set 远程地址 |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $remoteAddr 远程地址 |
|
197
|
|
|
* |
|
198
|
|
|
* @return self |
|
199
|
|
|
*/ |
|
200
|
|
|
public function setRemoteAddr(string $remoteAddr) |
|
201
|
|
|
{ |
|
202
|
|
|
$this->remoteAddr = $remoteAddr; |
|
203
|
|
|
|
|
204
|
|
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get 获取本地HOST |
|
209
|
|
|
* |
|
210
|
|
|
* @return string |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getHost() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->host; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Set 获取本地HOST |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $host 获取本地HOST |
|
221
|
|
|
* |
|
222
|
|
|
* @return self |
|
223
|
|
|
*/ |
|
224
|
|
|
public function setHost(string $host) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->host = $host; |
|
227
|
|
|
|
|
228
|
|
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Get 获取本地端口 |
|
233
|
|
|
* |
|
234
|
|
|
* @return int |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getPort() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->port; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Set 获取本地端口 |
|
243
|
|
|
* |
|
244
|
|
|
* @param int $port 获取本地端口 |
|
245
|
|
|
* |
|
246
|
|
|
* @return self |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setPort(int $port) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->port = $port; |
|
251
|
|
|
|
|
252
|
|
|
return $this; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Get 是否为安全模式 |
|
257
|
|
|
* |
|
258
|
|
|
* @return bool |
|
259
|
|
|
*/ |
|
260
|
|
|
public function isSecure():bool |
|
261
|
|
|
{ |
|
262
|
|
|
return $this->secure; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Set 是否为安全模式 |
|
267
|
|
|
* |
|
268
|
|
|
* @param bool $secure 是否为安全模式 |
|
269
|
|
|
* |
|
270
|
|
|
* @return self |
|
271
|
|
|
*/ |
|
272
|
|
|
public function setSecure(bool $secure) |
|
273
|
|
|
{ |
|
274
|
|
|
$this->secure = $secure; |
|
275
|
|
|
|
|
276
|
|
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Get 请求URI |
|
281
|
|
|
* |
|
282
|
|
|
* @return string |
|
283
|
|
|
*/ |
|
284
|
|
|
public function getUri() |
|
285
|
|
|
{ |
|
286
|
|
|
return $this->uri; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Set 请求URI |
|
291
|
|
|
* |
|
292
|
|
|
* @param string $uri 请求URI |
|
293
|
|
|
* |
|
294
|
|
|
* @return self |
|
295
|
|
|
*/ |
|
296
|
|
|
public function setUri(string $uri) |
|
297
|
|
|
{ |
|
298
|
|
|
$this->uri = $uri; |
|
299
|
|
|
|
|
300
|
|
|
return $this; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Get 请求参数 |
|
305
|
|
|
* |
|
306
|
|
|
* @return string |
|
307
|
|
|
*/ |
|
308
|
|
|
public function getMethod() |
|
309
|
|
|
{ |
|
310
|
|
|
return $this->method; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Set 请求参数 |
|
315
|
|
|
* |
|
316
|
|
|
* @param string $method 请求参数 |
|
317
|
|
|
* |
|
318
|
|
|
* @return self |
|
319
|
|
|
*/ |
|
320
|
|
|
public function setMethod(string $method) |
|
321
|
|
|
{ |
|
322
|
|
|
$this->method = $method; |
|
323
|
|
|
|
|
324
|
|
|
return $this; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* 获取查询参数 |
|
329
|
|
|
* |
|
330
|
|
|
* @param string $name |
|
331
|
|
|
* @param mixed $default |
|
332
|
|
|
* @return mixed |
|
333
|
|
|
*/ |
|
334
|
|
|
public function getQuery(string $name, $default = null) |
|
335
|
|
|
{ |
|
336
|
|
|
return $this->query[$name] ?? $default; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* 设置查询参数 |
|
341
|
|
|
* |
|
342
|
|
|
* @param string $name |
|
343
|
|
|
* @param mixed $parameter |
|
344
|
|
|
* @return self |
|
345
|
|
|
*/ |
|
346
|
|
|
public function setQuery(string $name, $query) |
|
|
|
|
|
|
347
|
|
|
{ |
|
348
|
|
|
$this->query = $query; |
|
349
|
|
|
|
|
350
|
|
|
return $this; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* 获取表单参数 |
|
355
|
|
|
* |
|
356
|
|
|
* @param string $name |
|
357
|
|
|
* @param mixed $default |
|
358
|
|
|
* @return mixed |
|
359
|
|
|
*/ |
|
360
|
|
|
public function getParameter(string $name, $default = null) |
|
361
|
|
|
{ |
|
362
|
|
|
return $this->parameter[$name] ?? $default; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* 设置表单参数 |
|
367
|
|
|
* |
|
368
|
|
|
* @param string $name |
|
369
|
|
|
* @param mixed $parameter |
|
370
|
|
|
* @return self |
|
371
|
|
|
*/ |
|
372
|
|
|
public function setParameter(string $name, $parameter) |
|
|
|
|
|
|
373
|
|
|
{ |
|
374
|
|
|
$this->parameter = $parameter; |
|
375
|
|
|
|
|
376
|
|
|
return $this; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Get 请求索引 |
|
381
|
|
|
* |
|
382
|
|
|
* @return string |
|
383
|
|
|
*/ |
|
384
|
|
|
public function getIndex() |
|
385
|
|
|
{ |
|
386
|
|
|
return $this->index; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* Set 请求索引 |
|
391
|
|
|
* |
|
392
|
|
|
* @param string $index 请求索引 |
|
393
|
|
|
* |
|
394
|
|
|
* @return self |
|
395
|
|
|
*/ |
|
396
|
|
|
public function setIndex(string $index) |
|
397
|
|
|
{ |
|
398
|
|
|
$this->index = $index; |
|
399
|
|
|
|
|
400
|
|
|
return $this; |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.