1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------- |
4
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
5
|
|
|
// +---------------------------------------------------------------------- |
6
|
|
|
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved. |
7
|
|
|
// +---------------------------------------------------------------------- |
8
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
9
|
|
|
// +---------------------------------------------------------------------- |
10
|
|
|
// | Author: liu21st <[email protected]> |
11
|
|
|
// +---------------------------------------------------------------------- |
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace think; |
15
|
|
|
|
16
|
|
|
use ArrayAccess; |
17
|
|
|
use think\facade\Lang; |
|
|
|
|
18
|
|
|
use think\file\UploadedFile; |
19
|
|
|
use think\route\Rule; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 请求管理类 |
23
|
|
|
* @package think |
24
|
|
|
*/ |
25
|
|
|
class Request implements ArrayAccess |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* 兼容PATH_INFO获取 |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $pathinfoFetch = ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL']; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* PATHINFO变量名 用于兼容模式 |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $varPathinfo = 's'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 请求类型 |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $varMethod = '_method'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 表单ajax伪装变量 |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $varAjax = '_ajax'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* 表单pjax伪装变量 |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $varPjax = '_pjax'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 域名根 |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $rootDomain = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 特殊域名根标识 用于识别com.cn org.cn 这种 |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $domainSpecialSuffix = ['com', 'net', 'org', 'edu', 'gov', 'mil', 'co', 'info']; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* HTTPS代理标识 |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
protected $httpsAgentName = ''; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 前端代理服务器IP |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $proxyServerIp = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 前端代理服务器真实IP头 |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
protected $proxyServerIpHeader = ['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP']; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* 请求类型 |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $method; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 域名(含协议及端口) |
95
|
|
|
* @var string |
96
|
|
|
*/ |
97
|
|
|
protected $domain; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* HOST(含端口) |
101
|
|
|
* @var string |
102
|
|
|
*/ |
103
|
|
|
protected $host; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 子域名 |
107
|
|
|
* @var string |
108
|
|
|
*/ |
109
|
|
|
protected $subDomain; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* 泛域名 |
113
|
|
|
* @var string |
114
|
|
|
*/ |
115
|
|
|
protected $panDomain; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* 当前URL地址 |
119
|
|
|
* @var string |
120
|
|
|
*/ |
121
|
|
|
protected $url; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* 基础URL |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
|
|
protected $baseUrl; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* 当前执行的文件 |
131
|
|
|
* @var string |
132
|
|
|
*/ |
133
|
|
|
protected $baseFile; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* 访问的ROOT地址 |
137
|
|
|
* @var string |
138
|
|
|
*/ |
139
|
|
|
protected $root; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* pathinfo |
143
|
|
|
* @var string |
144
|
|
|
*/ |
145
|
|
|
protected $pathinfo; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* pathinfo(不含后缀) |
149
|
|
|
* @var string |
150
|
|
|
*/ |
151
|
|
|
protected $path; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* 当前请求的IP地址 |
155
|
|
|
* @var string |
156
|
|
|
*/ |
157
|
|
|
protected $realIP; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* 当前控制器名 |
161
|
|
|
* @var string |
162
|
|
|
*/ |
163
|
|
|
protected $controller; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* 当前操作名 |
167
|
|
|
* @var string |
168
|
|
|
*/ |
169
|
|
|
protected $action; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* 当前请求参数 |
173
|
|
|
* @var array |
174
|
|
|
*/ |
175
|
|
|
protected $param = []; |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* 当前GET参数 |
179
|
|
|
* @var array |
180
|
|
|
*/ |
181
|
|
|
protected $get = []; |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* 当前POST参数 |
185
|
|
|
* @var array |
186
|
|
|
*/ |
187
|
|
|
protected $post = []; |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* 当前REQUEST参数 |
191
|
|
|
* @var array |
192
|
|
|
*/ |
193
|
|
|
protected $request = []; |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* 当前路由对象 |
197
|
|
|
* @var Rule |
198
|
|
|
*/ |
199
|
|
|
protected $rule; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* 当前ROUTE参数 |
203
|
|
|
* @var array |
204
|
|
|
*/ |
205
|
|
|
protected $route = []; |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* 中间件传递的参数 |
209
|
|
|
* @var array |
210
|
|
|
*/ |
211
|
|
|
protected $middleware = []; |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* 当前PUT参数 |
215
|
|
|
* @var array |
216
|
|
|
*/ |
217
|
|
|
protected $put; |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* SESSION对象 |
221
|
|
|
* @var Session |
222
|
|
|
*/ |
223
|
|
|
protected $session; |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* COOKIE数据 |
227
|
|
|
* @var array |
228
|
|
|
*/ |
229
|
|
|
protected $cookie = []; |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* ENV对象 |
233
|
|
|
* @var Env |
234
|
|
|
*/ |
235
|
|
|
protected $env; |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* 当前SERVER参数 |
239
|
|
|
* @var array |
240
|
|
|
*/ |
241
|
|
|
protected $server = []; |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* 当前FILE参数 |
245
|
|
|
* @var array |
246
|
|
|
*/ |
247
|
|
|
protected $file = []; |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* 当前HEADER参数 |
251
|
|
|
* @var array |
252
|
|
|
*/ |
253
|
|
|
protected $header = []; |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* 资源类型定义 |
257
|
|
|
* @var array |
258
|
|
|
*/ |
259
|
|
|
protected $mimeType = [ |
260
|
|
|
'xml' => 'application/xml,text/xml,application/x-xml', |
261
|
|
|
'json' => 'application/json,text/x-json,application/jsonrequest,text/json', |
262
|
|
|
'js' => 'text/javascript,application/javascript,application/x-javascript', |
263
|
|
|
'css' => 'text/css', |
264
|
|
|
'rss' => 'application/rss+xml', |
265
|
|
|
'yaml' => 'application/x-yaml,text/yaml', |
266
|
|
|
'atom' => 'application/atom+xml', |
267
|
|
|
'pdf' => 'application/pdf', |
268
|
|
|
'text' => 'text/plain', |
269
|
|
|
'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*', |
270
|
|
|
'csv' => 'text/csv', |
271
|
|
|
'html' => 'text/html,application/xhtml+xml,*/*', |
272
|
|
|
]; |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* 当前请求内容 |
276
|
|
|
* @var string |
277
|
|
|
*/ |
278
|
|
|
protected $content; |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* 全局过滤规则 |
282
|
|
|
* @var array |
283
|
|
|
*/ |
284
|
|
|
protected $filter; |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* php://input内容 |
288
|
|
|
* @var string |
289
|
|
|
*/ |
290
|
|
|
// php://input |
291
|
|
|
protected $input; |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* 请求安全Key |
295
|
|
|
* @var string |
296
|
|
|
*/ |
297
|
|
|
protected $secureKey; |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* 是否合并Param |
301
|
|
|
* @var bool |
302
|
|
|
*/ |
303
|
|
|
protected $mergeParam = false; |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* 架构函数 |
307
|
|
|
* @access public |
308
|
|
|
*/ |
309
|
27 |
|
public function __construct() |
310
|
|
|
{ |
311
|
|
|
// 保存 php://input |
312
|
27 |
|
$this->input = file_get_contents('php://input'); |
313
|
|
|
} |
314
|
|
|
|
315
|
27 |
|
public static function __make(App $app) |
316
|
|
|
{ |
317
|
27 |
|
$request = new static(); |
318
|
|
|
|
319
|
27 |
|
if (function_exists('apache_request_headers') && $result = apache_request_headers()) { |
320
|
|
|
$header = $result; |
321
|
|
|
} else { |
322
|
27 |
|
$header = []; |
323
|
27 |
|
$server = $_SERVER; |
324
|
27 |
|
foreach ($server as $key => $val) { |
325
|
27 |
|
if (str_starts_with($key, 'HTTP_')) { |
326
|
|
|
$key = str_replace('_', '-', strtolower(substr($key, 5))); |
327
|
|
|
$header[$key] = $val; |
328
|
|
|
} |
329
|
|
|
} |
330
|
27 |
|
if (isset($server['CONTENT_TYPE'])) { |
331
|
|
|
$header['content-type'] = $server['CONTENT_TYPE']; |
332
|
|
|
} |
333
|
27 |
|
if (isset($server['CONTENT_LENGTH'])) { |
334
|
|
|
$header['content-length'] = $server['CONTENT_LENGTH']; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
27 |
|
$request->header = array_change_key_case($header); |
|
|
|
|
339
|
27 |
|
$request->server = $_SERVER; |
340
|
27 |
|
$request->env = $app->env; |
341
|
|
|
|
342
|
27 |
|
$inputData = $request->getInputData($request->input); |
343
|
|
|
|
344
|
27 |
|
$request->get = $_GET; |
345
|
27 |
|
$request->post = $_POST ?: $inputData; |
346
|
27 |
|
$request->put = $inputData; |
347
|
27 |
|
$request->request = $_REQUEST; |
348
|
27 |
|
$request->cookie = $_COOKIE; |
349
|
27 |
|
$request->file = $_FILES ?? []; |
350
|
|
|
|
351
|
27 |
|
return $request; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* 设置当前包含协议的域名 |
356
|
|
|
* @access public |
357
|
|
|
* @param string $domain 域名 |
358
|
|
|
* @return $this |
359
|
|
|
*/ |
360
|
|
|
public function setDomain(string $domain) |
361
|
|
|
{ |
362
|
|
|
$this->domain = $domain; |
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* 获取当前包含协议的域名 |
368
|
|
|
* @access public |
369
|
|
|
* @param bool $port 是否需要去除端口号 |
370
|
|
|
* @return string |
371
|
|
|
*/ |
372
|
|
|
public function domain(bool $port = false): string |
373
|
|
|
{ |
374
|
|
|
return $this->scheme() . '://' . $this->host($port); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* 设置根域名 |
379
|
|
|
* @param string $domain |
380
|
|
|
* @return $this |
381
|
|
|
*/ |
382
|
|
|
public function setRootDomain(string $domain) |
383
|
|
|
{ |
384
|
|
|
$this->rootDomain = $domain; |
385
|
|
|
return $this; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* 获取当前根域名 |
390
|
|
|
* @access public |
391
|
|
|
* @return string |
392
|
|
|
*/ |
393
|
27 |
|
public function rootDomain(): string |
394
|
|
|
{ |
395
|
27 |
|
$root = $this->rootDomain; |
396
|
|
|
|
397
|
27 |
|
if (!$root) { |
398
|
27 |
|
$item = explode('.', $this->host(true)); |
399
|
27 |
|
$count = count($item); |
400
|
27 |
|
if ($count > 1) { |
401
|
3 |
|
$root = $item[$count - 2] . '.' . $item[$count - 1]; |
402
|
3 |
|
if ($count > 2 && in_array($item[$count - 2], $this->domainSpecialSuffix)) { |
403
|
2 |
|
$root = $item[$count - 3] . '.' . $root; |
404
|
|
|
} |
405
|
|
|
} else { |
406
|
24 |
|
$root = $item[0]; |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
27 |
|
return $root; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* 设置当前泛域名的值 |
415
|
|
|
* @access public |
416
|
|
|
* @param string $domain 域名 |
417
|
|
|
* @return $this |
418
|
|
|
*/ |
419
|
|
|
public function setSubDomain(string $domain) |
420
|
|
|
{ |
421
|
|
|
$this->subDomain = $domain; |
422
|
|
|
return $this; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* 获取当前子域名 |
427
|
|
|
* @access public |
428
|
|
|
* @return string |
429
|
|
|
*/ |
430
|
27 |
|
public function subDomain(): string |
431
|
|
|
{ |
432
|
27 |
|
if (is_null($this->subDomain)) { |
|
|
|
|
433
|
|
|
// 获取当前主域名 |
434
|
27 |
|
$rootDomain = $this->rootDomain(); |
435
|
|
|
|
436
|
27 |
|
if ($rootDomain) { |
437
|
27 |
|
$sub = stristr($this->host(), $rootDomain, true); |
438
|
27 |
|
$this->subDomain = $sub ? rtrim($sub, '.') : ''; |
439
|
|
|
} else { |
440
|
|
|
$this->subDomain = ''; |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
27 |
|
return $this->subDomain; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* 设置当前泛域名的值 |
449
|
|
|
* @access public |
450
|
|
|
* @param string $domain 域名 |
451
|
|
|
* @return $this |
452
|
|
|
*/ |
453
|
|
|
public function setPanDomain(string $domain) |
454
|
|
|
{ |
455
|
|
|
$this->panDomain = $domain; |
456
|
|
|
return $this; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* 获取当前泛域名的值 |
461
|
|
|
* @access public |
462
|
|
|
* @return string |
463
|
|
|
*/ |
464
|
3 |
|
public function panDomain(): string |
465
|
|
|
{ |
466
|
3 |
|
return $this->panDomain ?: ''; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* 设置当前完整URL 包括QUERY_STRING |
471
|
|
|
* @access public |
472
|
|
|
* @param string $url URL地址 |
473
|
|
|
* @return $this |
474
|
|
|
*/ |
475
|
|
|
public function setUrl(string $url) |
476
|
|
|
{ |
477
|
|
|
$this->url = $url; |
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* 获取当前完整URL 包括QUERY_STRING |
483
|
|
|
* @access public |
484
|
|
|
* @param bool $complete 是否包含完整域名 |
485
|
|
|
* @return string |
486
|
|
|
*/ |
487
|
|
|
public function url(bool $complete = false): string |
488
|
|
|
{ |
489
|
|
|
if ($this->url) { |
490
|
|
|
$url = $this->url; |
491
|
|
|
} elseif ($this->server('HTTP_X_REWRITE_URL')) { |
492
|
|
|
$url = $this->server('HTTP_X_REWRITE_URL'); |
493
|
|
|
} elseif ($this->server('REQUEST_URI')) { |
494
|
|
|
$url = $this->server('REQUEST_URI'); |
495
|
|
|
} elseif ($this->server('ORIG_PATH_INFO')) { |
496
|
|
|
$url = $this->server('ORIG_PATH_INFO') . (!empty($this->server('QUERY_STRING')) ? '?' . $this->server('QUERY_STRING') : ''); |
497
|
|
|
} elseif (isset($_SERVER['argv'][1])) { |
498
|
|
|
$url = $_SERVER['argv'][1]; |
499
|
|
|
} else { |
500
|
|
|
$url = ''; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
return $complete ? $this->domain() . $url : $url; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* 设置当前URL 不含QUERY_STRING |
508
|
|
|
* @access public |
509
|
|
|
* @param string $url URL地址 |
510
|
|
|
* @return $this |
511
|
|
|
*/ |
512
|
|
|
public function setBaseUrl(string $url) |
513
|
|
|
{ |
514
|
|
|
$this->baseUrl = $url; |
515
|
|
|
return $this; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* 获取当前URL 不含QUERY_STRING |
520
|
|
|
* @access public |
521
|
|
|
* @param bool $complete 是否包含完整域名 |
522
|
|
|
* @return string |
523
|
|
|
*/ |
524
|
|
|
public function baseUrl(bool $complete = false): string |
525
|
|
|
{ |
526
|
|
|
if (!$this->baseUrl) { |
527
|
|
|
$str = $this->url(); |
528
|
|
|
$this->baseUrl = str_contains($str, '?') ? strstr($str, '?', true) : $str; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
return $complete ? $this->domain() . $this->baseUrl : $this->baseUrl; |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* 获取当前执行的文件 SCRIPT_NAME |
536
|
|
|
* @access public |
537
|
|
|
* @param bool $complete 是否包含完整域名 |
538
|
|
|
* @return string |
539
|
|
|
*/ |
540
|
|
|
public function baseFile(bool $complete = false): string |
541
|
|
|
{ |
542
|
|
|
if (!$this->baseFile) { |
543
|
|
|
$url = ''; |
544
|
|
|
if (!$this->isCli()) { |
545
|
|
|
$script_name = basename($this->server('SCRIPT_FILENAME')); |
546
|
|
|
if (basename($this->server('SCRIPT_NAME')) === $script_name) { |
547
|
|
|
$url = $this->server('SCRIPT_NAME'); |
548
|
|
|
} elseif (basename($this->server('PHP_SELF')) === $script_name) { |
549
|
|
|
$url = $this->server('PHP_SELF'); |
550
|
|
|
} elseif (basename($this->server('ORIG_SCRIPT_NAME')) === $script_name) { |
551
|
|
|
$url = $this->server('ORIG_SCRIPT_NAME'); |
552
|
|
|
} elseif (($pos = strpos($this->server('PHP_SELF'), '/' . $script_name)) !== false) { |
553
|
|
|
$url = substr($this->server('SCRIPT_NAME'), 0, $pos) . '/' . $script_name; |
554
|
|
|
} elseif ($this->server('DOCUMENT_ROOT') && str_starts_with($this->server('SCRIPT_FILENAME'), $this->server('DOCUMENT_ROOT'))) { |
555
|
|
|
$url = str_replace('\\', '/', str_replace($this->server('DOCUMENT_ROOT'), '', $this->server('SCRIPT_FILENAME'))); |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
$this->baseFile = $url; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
return $complete ? $this->domain() . $this->baseFile : $this->baseFile; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* 设置URL访问根地址 |
566
|
|
|
* @access public |
567
|
|
|
* @param string $url URL地址 |
568
|
|
|
* @return $this |
569
|
|
|
*/ |
570
|
|
|
public function setRoot(string $url) |
571
|
|
|
{ |
572
|
|
|
$this->root = $url; |
573
|
|
|
return $this; |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
/** |
577
|
|
|
* 获取URL访问根地址 |
578
|
|
|
* @access public |
579
|
|
|
* @param bool $complete 是否包含完整域名 |
580
|
|
|
* @return string |
581
|
|
|
*/ |
582
|
|
|
public function root(bool $complete = false): string |
583
|
|
|
{ |
584
|
|
|
if (!$this->root) { |
585
|
|
|
$file = $this->baseFile(); |
586
|
|
|
if ($file && !str_starts_with($this->url(), $file)) { |
587
|
|
|
$file = str_replace('\\', '/', dirname($file)); |
588
|
|
|
} |
589
|
|
|
$this->root = rtrim($file, '/'); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
return $complete ? $this->domain() . $this->root : $this->root; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* 获取URL访问根目录 |
597
|
|
|
* @access public |
598
|
|
|
* @return string |
599
|
|
|
*/ |
600
|
|
|
public function rootUrl(): string |
601
|
|
|
{ |
602
|
|
|
$base = $this->root(); |
603
|
|
|
$root = str_contains($base, '.') ? ltrim(dirname($base), DIRECTORY_SEPARATOR) : $base; |
604
|
|
|
|
605
|
|
|
if ('' != $root) { |
606
|
|
|
$root = '/' . ltrim($root, '/'); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
return $root; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* 设置当前请求的pathinfo |
614
|
|
|
* @access public |
615
|
|
|
* @param string $pathinfo |
616
|
|
|
* @return $this |
617
|
|
|
*/ |
618
|
|
|
public function setPathinfo(string $pathinfo) |
619
|
|
|
{ |
620
|
|
|
$this->pathinfo = $pathinfo; |
621
|
|
|
return $this; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* 获取当前请求URL的pathinfo信息(含URL后缀) |
626
|
|
|
* @access public |
627
|
|
|
* @return string |
628
|
|
|
*/ |
629
|
|
|
public function pathinfo(): string |
630
|
|
|
{ |
631
|
|
|
if (is_null($this->pathinfo)) { |
|
|
|
|
632
|
|
|
if (isset($_GET[$this->varPathinfo])) { |
633
|
|
|
// 判断URL里面是否有兼容模式参数 |
634
|
|
|
$pathinfo = $_GET[$this->varPathinfo]; |
635
|
|
|
unset($_GET[$this->varPathinfo]); |
636
|
|
|
unset($this->get[$this->varPathinfo]); |
637
|
|
|
} elseif ($this->server('PATH_INFO')) { |
638
|
|
|
$pathinfo = $this->server('PATH_INFO'); |
639
|
|
|
} elseif (str_contains(PHP_SAPI, 'cli')) { |
640
|
|
|
$pathinfo = str_contains($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI'); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
// 分析PATHINFO信息 |
644
|
|
|
if (!isset($pathinfo)) { |
645
|
|
|
foreach ($this->pathinfoFetch as $type) { |
646
|
|
|
if ($this->server($type)) { |
647
|
|
|
$pathinfo = str_starts_with($this->server($type), $this->server('SCRIPT_NAME')) ? |
648
|
|
|
substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type); |
649
|
|
|
break; |
650
|
|
|
} |
651
|
|
|
} |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
if (!empty($pathinfo)) { |
655
|
|
|
unset($this->get[$pathinfo], $this->request[$pathinfo]); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
$this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/'); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
return $this->pathinfo; |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* 当前URL的访问后缀 |
666
|
|
|
* @access public |
667
|
|
|
* @return string |
668
|
|
|
*/ |
669
|
|
|
public function ext(): string |
670
|
|
|
{ |
671
|
|
|
return pathinfo($this->pathinfo(), PATHINFO_EXTENSION); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* 获取当前请求的时间 |
676
|
|
|
* @access public |
677
|
|
|
* @param bool $float 是否使用浮点类型 |
678
|
|
|
* @return integer|float |
679
|
|
|
*/ |
680
|
|
|
public function time(bool $float = false) |
681
|
|
|
{ |
682
|
|
|
return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME'); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* 当前请求的资源类型 |
687
|
|
|
* @access public |
688
|
|
|
* @return string |
689
|
|
|
*/ |
690
|
21 |
|
public function type(): string |
691
|
|
|
{ |
692
|
21 |
|
$accept = $this->server('HTTP_ACCEPT'); |
693
|
|
|
|
694
|
21 |
|
if (empty($accept)) { |
695
|
21 |
|
return ''; |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
foreach ($this->mimeType as $key => $val) { |
699
|
|
|
$array = explode(',', $val); |
700
|
|
|
foreach ($array as $k => $v) { |
701
|
|
|
if (stristr($accept, $v)) { |
702
|
|
|
return $key; |
703
|
|
|
} |
704
|
|
|
} |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
return ''; |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* 设置资源类型 |
712
|
|
|
* @access public |
713
|
|
|
* @param string|array $type 资源类型名 |
714
|
|
|
* @param string $val 资源类型 |
715
|
|
|
* @return void |
716
|
|
|
*/ |
717
|
|
|
public function mimeType($type, $val = ''): void |
718
|
|
|
{ |
719
|
|
|
if (is_array($type)) { |
720
|
|
|
$this->mimeType = array_merge($this->mimeType, $type); |
721
|
|
|
} else { |
722
|
|
|
$this->mimeType[$type] = $val; |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* 设置请求类型 |
728
|
|
|
* @access public |
729
|
|
|
* @param string $method 请求类型 |
730
|
|
|
* @return $this |
731
|
|
|
*/ |
732
|
|
|
public function setMethod(string $method) |
733
|
|
|
{ |
734
|
|
|
$this->method = strtoupper($method); |
735
|
|
|
return $this; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* 当前的请求类型 |
740
|
|
|
* @access public |
741
|
|
|
* @param bool $origin 是否获取原始请求类型 |
742
|
|
|
* @return string |
743
|
|
|
*/ |
744
|
|
|
public function method(bool $origin = false): string |
745
|
|
|
{ |
746
|
|
|
if ($origin) { |
747
|
|
|
// 获取原始请求类型 |
748
|
|
|
return $this->server('REQUEST_METHOD') ?: 'GET'; |
749
|
|
|
} elseif (!$this->method) { |
750
|
|
|
if (isset($this->post[$this->varMethod])) { |
751
|
|
|
$method = strtolower($this->post[$this->varMethod]); |
752
|
|
|
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { |
753
|
|
|
$this->method = strtoupper($method); |
754
|
|
|
$this->{$method} = $this->post; |
755
|
|
|
} else { |
756
|
|
|
$this->method = 'POST'; |
757
|
|
|
} |
758
|
|
|
unset($this->post[$this->varMethod]); |
759
|
|
|
} elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) { |
760
|
|
|
$this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')); |
761
|
|
|
} else { |
762
|
|
|
$this->method = $this->server('REQUEST_METHOD') ?: 'GET'; |
763
|
|
|
} |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
return $this->method; |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
/** |
770
|
|
|
* 是否为GET请求 |
771
|
|
|
* @access public |
772
|
|
|
* @return bool |
773
|
|
|
*/ |
774
|
|
|
public function isGet(): bool |
775
|
|
|
{ |
776
|
|
|
return $this->method() == 'GET'; |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* 是否为POST请求 |
781
|
|
|
* @access public |
782
|
|
|
* @return bool |
783
|
|
|
*/ |
784
|
|
|
public function isPost(): bool |
785
|
|
|
{ |
786
|
|
|
return $this->method() == 'POST'; |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* 是否为PUT请求 |
791
|
|
|
* @access public |
792
|
|
|
* @return bool |
793
|
|
|
*/ |
794
|
|
|
public function isPut(): bool |
795
|
|
|
{ |
796
|
|
|
return $this->method() == 'PUT'; |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* 是否为DELTE请求 |
801
|
|
|
* @access public |
802
|
|
|
* @return bool |
803
|
|
|
*/ |
804
|
|
|
public function isDelete(): bool |
805
|
|
|
{ |
806
|
|
|
return $this->method() == 'DELETE'; |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
/** |
810
|
|
|
* 是否为HEAD请求 |
811
|
|
|
* @access public |
812
|
|
|
* @return bool |
813
|
|
|
*/ |
814
|
|
|
public function isHead(): bool |
815
|
|
|
{ |
816
|
|
|
return $this->method() == 'HEAD'; |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
/** |
820
|
|
|
* 是否为PATCH请求 |
821
|
|
|
* @access public |
822
|
|
|
* @return bool |
823
|
|
|
*/ |
824
|
|
|
public function isPatch(): bool |
825
|
|
|
{ |
826
|
|
|
return $this->method() == 'PATCH'; |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
/** |
830
|
|
|
* 是否为OPTIONS请求 |
831
|
|
|
* @access public |
832
|
|
|
* @return bool |
833
|
|
|
*/ |
834
|
|
|
public function isOptions(): bool |
835
|
|
|
{ |
836
|
|
|
return $this->method() == 'OPTIONS'; |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
/** |
840
|
|
|
* 是否为cli |
841
|
|
|
* @access public |
842
|
|
|
* @return bool |
843
|
|
|
*/ |
844
|
|
|
public function isCli(): bool |
845
|
|
|
{ |
846
|
|
|
return PHP_SAPI == 'cli'; |
847
|
|
|
} |
848
|
|
|
|
849
|
|
|
/** |
850
|
|
|
* 是否为cgi |
851
|
|
|
* @access public |
852
|
|
|
* @return bool |
853
|
|
|
*/ |
854
|
|
|
public function isCgi(): bool |
855
|
|
|
{ |
856
|
|
|
return str_starts_with(PHP_SAPI, 'cgi'); |
857
|
|
|
} |
858
|
|
|
|
859
|
|
|
/** |
860
|
|
|
* 获取当前请求的参数 |
861
|
|
|
* @access public |
862
|
|
|
* @param string|array $name 变量名 |
863
|
|
|
* @param mixed $default 默认值 |
864
|
|
|
* @param string|array|null $filter 过滤方法 |
865
|
|
|
* @return mixed |
866
|
|
|
*/ |
867
|
27 |
|
public function param($name = '', $default = null, string|array|null $filter = '') |
868
|
|
|
{ |
869
|
27 |
|
if (empty($this->mergeParam)) { |
870
|
27 |
|
$method = $this->method(true); |
871
|
|
|
|
872
|
|
|
// 自动获取请求变量 |
873
|
27 |
|
$vars = match ($method) { |
874
|
27 |
|
'POST' => $this->post(false), |
875
|
27 |
|
'PUT','DELETE','PATCH' => $this->put(false), |
876
|
27 |
|
default => [], |
877
|
27 |
|
}; |
878
|
|
|
|
879
|
|
|
// 当前请求参数和URL地址中的参数合并 |
880
|
27 |
|
$this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false)); |
|
|
|
|
881
|
|
|
|
882
|
27 |
|
$this->mergeParam = true; |
883
|
|
|
} |
884
|
|
|
|
885
|
27 |
|
if (is_array($name)) { |
886
|
|
|
return $this->only($name, $this->param, $filter); |
887
|
|
|
} |
888
|
|
|
|
889
|
27 |
|
return $this->input($this->param, $name, $default, $filter); |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* 获取包含文件在内的请求参数 |
894
|
|
|
* @access public |
895
|
|
|
* @param string|array $name 变量名 |
896
|
|
|
* @param string|array|null $filter 过滤方法 |
897
|
|
|
* @return mixed |
898
|
|
|
*/ |
899
|
|
|
public function all(string|array $name = '', string|array|null $filter = '') |
900
|
|
|
{ |
901
|
|
|
$data = array_merge($this->param(), $this->file() ?: []); |
|
|
|
|
902
|
|
|
|
903
|
|
|
if (is_array($name)) { |
904
|
|
|
$data = $this->only($name, $data, $filter); |
905
|
|
|
} elseif ($name) { |
906
|
|
|
$data = $data[$name] ?? null; |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
return $data; |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
/** |
913
|
|
|
* 设置路由变量 |
914
|
|
|
* @access public |
915
|
|
|
* @param Rule $rule 路由对象 |
916
|
|
|
* @return $this |
917
|
|
|
*/ |
918
|
27 |
|
public function setRule(Rule $rule) |
919
|
|
|
{ |
920
|
27 |
|
$this->rule = $rule; |
921
|
27 |
|
return $this; |
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
/** |
925
|
|
|
* 获取当前路由对象 |
926
|
|
|
* @access public |
927
|
|
|
* @return Rule|null |
928
|
|
|
*/ |
929
|
3 |
|
public function rule() |
930
|
|
|
{ |
931
|
3 |
|
return $this->rule; |
932
|
|
|
} |
933
|
|
|
|
934
|
|
|
/** |
935
|
|
|
* 设置路由变量 |
936
|
|
|
* @access public |
937
|
|
|
* @param array $route 路由变量 |
938
|
|
|
* @return $this |
939
|
|
|
*/ |
940
|
27 |
|
public function setRoute(array $route) |
941
|
|
|
{ |
942
|
27 |
|
$this->route = array_merge($this->route, $route); |
943
|
27 |
|
$this->mergeParam = false; |
944
|
27 |
|
return $this; |
945
|
|
|
} |
946
|
|
|
|
947
|
|
|
/** |
948
|
|
|
* 获取路由参数 |
949
|
|
|
* @access public |
950
|
|
|
* @param string|array|bool $name 变量名 |
951
|
|
|
* @param mixed $default 默认值 |
952
|
|
|
* @param string|array|null $filter 过滤方法 |
953
|
|
|
* @return mixed |
954
|
|
|
*/ |
955
|
27 |
|
public function route(string|array|bool $name = '', $default = null, string|array|null $filter = '') |
956
|
|
|
{ |
957
|
27 |
|
if (is_array($name)) { |
958
|
|
|
return $this->only($name, $this->route, $filter); |
959
|
|
|
} |
960
|
|
|
|
961
|
27 |
|
return $this->input($this->route, $name, $default, $filter); |
962
|
|
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* 获取GET参数 |
966
|
|
|
* @access public |
967
|
|
|
* @param string|array|bool $name 变量名 |
968
|
|
|
* @param mixed $default 默认值 |
969
|
|
|
* @param string|array|null $filter 过滤方法 |
970
|
|
|
* @return mixed |
971
|
|
|
*/ |
972
|
27 |
|
public function get(string|array|bool $name = '', $default = null, string|array|null $filter = '') |
973
|
|
|
{ |
974
|
27 |
|
if (is_array($name)) { |
975
|
|
|
return $this->only($name, $this->get, $filter); |
976
|
|
|
} |
977
|
|
|
|
978
|
27 |
|
return $this->input($this->get, $name, $default, $filter); |
979
|
|
|
} |
980
|
|
|
|
981
|
|
|
/** |
982
|
|
|
* 获取中间件传递的参数 |
983
|
|
|
* @access public |
984
|
|
|
* @param string $name 变量名 |
985
|
|
|
* @param mixed $default 默认值 |
986
|
|
|
* @return mixed |
987
|
|
|
*/ |
988
|
|
|
public function middleware(string $name, $default = null) |
989
|
|
|
{ |
990
|
|
|
return $this->middleware[$name] ?? $default; |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
/** |
994
|
|
|
* 获取POST参数 |
995
|
|
|
* @access public |
996
|
|
|
* @param bool|string|array $name 变量名 |
997
|
|
|
* @param mixed $default 默认值 |
998
|
|
|
* @param string|array|null $filter 过滤方法 |
999
|
|
|
* @return mixed |
1000
|
|
|
*/ |
1001
|
3 |
|
public function post(string|array|bool $name = '', $default = null, string|array|null $filter = '') |
1002
|
|
|
{ |
1003
|
3 |
|
if (is_array($name)) { |
1004
|
|
|
return $this->only($name, $this->post, $filter); |
1005
|
|
|
} |
1006
|
|
|
|
1007
|
3 |
|
return $this->input($this->post, $name, $default, $filter); |
1008
|
|
|
} |
1009
|
|
|
|
1010
|
|
|
/** |
1011
|
|
|
* 获取PUT参数 |
1012
|
|
|
* @access public |
1013
|
|
|
* @param string|array|bool $name 变量名 |
1014
|
|
|
* @param mixed $default 默认值 |
1015
|
|
|
* @param string|array|null $filter 过滤方法 |
1016
|
|
|
* @return mixed |
1017
|
|
|
*/ |
1018
|
|
|
public function put(string|array|bool $name = '', $default = null, string|array|null $filter = '') |
1019
|
|
|
{ |
1020
|
|
|
if (is_array($name)) { |
1021
|
|
|
return $this->only($name, $this->put, $filter); |
1022
|
|
|
} |
1023
|
|
|
|
1024
|
|
|
return $this->input($this->put, $name, $default, $filter); |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
27 |
|
protected function getInputData(string $content): array |
1028
|
|
|
{ |
1029
|
27 |
|
$contentType = $this->contentType(); |
1030
|
27 |
|
if ('application/x-www-form-urlencoded' == $contentType) { |
1031
|
|
|
parse_str($content, $data); |
1032
|
|
|
return $data; |
1033
|
27 |
|
} elseif (str_contains($contentType, 'json')) { |
1034
|
|
|
return (array) json_decode($content, true); |
1035
|
|
|
} |
1036
|
|
|
|
1037
|
27 |
|
return []; |
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
/** |
1041
|
|
|
* 设置获取DELETE参数 |
1042
|
|
|
* @access public |
1043
|
|
|
* @param mixed $name 变量名 |
1044
|
|
|
* @param mixed $default 默认值 |
1045
|
|
|
* @param string|array|null $filter 过滤方法 |
1046
|
|
|
* @return mixed |
1047
|
|
|
*/ |
1048
|
|
|
public function delete(string|array|bool $name = '', $default = null, string|array|null $filter = '') |
1049
|
|
|
{ |
1050
|
|
|
return $this->put($name, $default, $filter); |
1051
|
|
|
} |
1052
|
|
|
|
1053
|
|
|
/** |
1054
|
|
|
* 设置获取PATCH参数 |
1055
|
|
|
* @access public |
1056
|
|
|
* @param mixed $name 变量名 |
1057
|
|
|
* @param mixed $default 默认值 |
1058
|
|
|
* @param string|array|null $filter 过滤方法 |
1059
|
|
|
* @return mixed |
1060
|
|
|
*/ |
1061
|
|
|
public function patch(string|array|bool $name = '', $default = null, string|array|null $filter = '') |
1062
|
|
|
{ |
1063
|
|
|
return $this->put($name, $default, $filter); |
1064
|
|
|
} |
1065
|
|
|
|
1066
|
|
|
/** |
1067
|
|
|
* 获取request变量 |
1068
|
|
|
* @access public |
1069
|
|
|
* @param string|array $name 数据名称 |
1070
|
|
|
* @param mixed $default 默认值 |
1071
|
|
|
* @param string|array|null $filter 过滤方法 |
1072
|
|
|
* @return mixed |
1073
|
|
|
*/ |
1074
|
|
|
public function request(string|array|bool $name = '', $default = null, string|array|null $filter = '') |
1075
|
|
|
{ |
1076
|
|
|
if (is_array($name)) { |
1077
|
|
|
return $this->only($name, $this->request, $filter); |
1078
|
|
|
} |
1079
|
|
|
|
1080
|
|
|
return $this->input($this->request, $name, $default, $filter); |
1081
|
|
|
} |
1082
|
|
|
|
1083
|
|
|
/** |
1084
|
|
|
* 获取环境变量 |
1085
|
|
|
* @access public |
1086
|
|
|
* @param string $name 数据名称 |
1087
|
|
|
* @param string $default 默认值 |
1088
|
|
|
* @return mixed |
1089
|
|
|
*/ |
1090
|
|
|
public function env(string $name = '', ?string $default = null) |
1091
|
|
|
{ |
1092
|
|
|
if (empty($name)) { |
1093
|
|
|
return $this->env->get(); |
1094
|
|
|
} |
1095
|
|
|
return $this->env->get(strtoupper($name), $default); |
1096
|
|
|
} |
1097
|
|
|
|
1098
|
|
|
/** |
1099
|
|
|
* 获取session数据 |
1100
|
|
|
* @access public |
1101
|
|
|
* @param string $name 数据名称 |
1102
|
|
|
* @param string $default 默认值 |
1103
|
|
|
* @return mixed |
1104
|
|
|
*/ |
1105
|
|
|
public function session(string $name = '', $default = null) |
1106
|
|
|
{ |
1107
|
|
|
if ('' === $name) { |
1108
|
|
|
return $this->session->all(); |
|
|
|
|
1109
|
|
|
} |
1110
|
|
|
return $this->session->get($name, $default); |
|
|
|
|
1111
|
|
|
} |
1112
|
|
|
|
1113
|
|
|
/** |
1114
|
|
|
* 获取cookie参数 |
1115
|
|
|
* @access public |
1116
|
|
|
* @param mixed $name 数据名称 |
1117
|
|
|
* @param string $default 默认值 |
1118
|
|
|
* @param string|array|null $filter 过滤方法 |
1119
|
|
|
* @return mixed |
1120
|
|
|
*/ |
1121
|
|
|
public function cookie(string $name = '', $default = null, string|array|null $filter = '') |
1122
|
|
|
{ |
1123
|
|
|
if (!empty($name)) { |
1124
|
|
|
$data = $this->getData($this->cookie, $name, $default); |
1125
|
|
|
} else { |
1126
|
|
|
$data = $this->cookie; |
1127
|
|
|
} |
1128
|
|
|
|
1129
|
|
|
// 解析过滤器 |
1130
|
|
|
$filter = $this->getFilter($filter, $default); |
1131
|
|
|
|
1132
|
|
|
if (is_array($data)) { |
1133
|
|
|
array_walk_recursive($data, [$this, 'filterValue'], $filter); |
1134
|
|
|
} else { |
1135
|
|
|
$this->filterValue($data, $name, $filter); |
1136
|
|
|
} |
1137
|
|
|
|
1138
|
|
|
return $data; |
1139
|
|
|
} |
1140
|
|
|
|
1141
|
|
|
/** |
1142
|
|
|
* 获取server参数 |
1143
|
|
|
* @access public |
1144
|
|
|
* @param string $name 数据名称 |
1145
|
|
|
* @param string $default 默认值 |
1146
|
|
|
* @return mixed |
1147
|
|
|
*/ |
1148
|
21 |
|
public function server(string $name = '', string $default = '') |
1149
|
|
|
{ |
1150
|
21 |
|
if (empty($name)) { |
1151
|
|
|
return $this->server; |
1152
|
|
|
} |
1153
|
21 |
|
return $this->server[strtoupper($name)] ?? $default; |
1154
|
|
|
} |
1155
|
|
|
|
1156
|
|
|
/** |
1157
|
|
|
* 获取上传的文件信息 |
1158
|
|
|
* @access public |
1159
|
|
|
* @param string $name 名称 |
1160
|
|
|
* @return null|array|UploadedFile |
1161
|
|
|
*/ |
1162
|
|
|
public function file(string $name = '') |
1163
|
|
|
{ |
1164
|
|
|
$files = $this->file; |
1165
|
|
|
if (!empty($files)) { |
1166
|
|
|
if (str_contains($name, '.')) { |
1167
|
|
|
[$name, $sub] = explode('.', $name); |
1168
|
|
|
} |
1169
|
|
|
|
1170
|
|
|
// 处理上传文件 |
1171
|
|
|
$array = $this->dealUploadFile($files, $name); |
1172
|
|
|
|
1173
|
|
|
if ('' === $name) { |
1174
|
|
|
// 获取全部文件 |
1175
|
|
|
return $array; |
1176
|
|
|
} elseif (isset($sub) && isset($array[$name][$sub])) { |
1177
|
|
|
return $array[$name][$sub]; |
1178
|
|
|
} elseif (isset($array[$name])) { |
1179
|
|
|
return $array[$name]; |
1180
|
|
|
} |
1181
|
|
|
} |
1182
|
|
|
} |
1183
|
|
|
|
1184
|
|
|
protected function dealUploadFile(array $files, string $name): array |
1185
|
|
|
{ |
1186
|
|
|
$array = []; |
1187
|
|
|
foreach ($files as $key => $file) { |
1188
|
|
|
if (is_array($file['name'])) { |
1189
|
|
|
$item = []; |
1190
|
|
|
$keys = array_keys($file); |
1191
|
|
|
$count = count($file['name']); |
1192
|
|
|
|
1193
|
|
|
for ($i = 0; $i < $count; $i++) { |
1194
|
|
|
if ($file['error'][$i] > 0) { |
1195
|
|
|
if ($name == $key) { |
1196
|
|
|
$this->throwUploadFileError($file['error'][$i]); |
1197
|
|
|
} else { |
1198
|
|
|
continue; |
1199
|
|
|
} |
1200
|
|
|
} |
1201
|
|
|
|
1202
|
|
|
$temp['key'] = $key; |
1203
|
|
|
|
1204
|
|
|
foreach ($keys as $_key) { |
1205
|
|
|
$temp[$_key] = $file[$_key][$i]; |
1206
|
|
|
} |
1207
|
|
|
|
1208
|
|
|
$item[] = new UploadedFile($temp['tmp_name'], $temp['name'], $temp['type'], $temp['error']); |
1209
|
|
|
} |
1210
|
|
|
|
1211
|
|
|
$array[$key] = $item; |
1212
|
|
|
} else { |
1213
|
|
|
if ($file instanceof File) { |
1214
|
|
|
$array[$key] = $file; |
1215
|
|
|
} else { |
1216
|
|
|
if ($file['error'] > 0) { |
1217
|
|
|
if ($key == $name) { |
1218
|
|
|
$this->throwUploadFileError($file['error']); |
1219
|
|
|
} else { |
1220
|
|
|
continue; |
1221
|
|
|
} |
1222
|
|
|
} |
1223
|
|
|
|
1224
|
|
|
$array[$key] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']); |
1225
|
|
|
} |
1226
|
|
|
} |
1227
|
|
|
} |
1228
|
|
|
|
1229
|
|
|
return $array; |
1230
|
|
|
} |
1231
|
|
|
|
1232
|
|
|
protected function throwUploadFileError($error) |
1233
|
|
|
{ |
1234
|
|
|
static $fileUploadErrors = [ |
1235
|
|
|
1 => 'upload File size exceeds the maximum value', |
1236
|
|
|
2 => 'upload File size exceeds the maximum value', |
1237
|
|
|
3 => 'only the portion of file is uploaded', |
1238
|
|
|
4 => 'no file to uploaded', |
1239
|
|
|
6 => 'upload temp dir not found', |
1240
|
|
|
7 => 'file write error', |
1241
|
|
|
]; |
1242
|
|
|
|
1243
|
|
|
$msg = Lang::get($fileUploadErrors[$error]); |
|
|
|
|
1244
|
|
|
throw new Exception($msg, $error); |
1245
|
|
|
} |
1246
|
|
|
|
1247
|
|
|
/** |
1248
|
|
|
* 设置或者获取当前的Header |
1249
|
|
|
* @access public |
1250
|
|
|
* @param string $name header名称 |
1251
|
|
|
* @param string $default 默认值 |
1252
|
|
|
* @return string|array|null |
1253
|
|
|
*/ |
1254
|
27 |
|
public function header(string $name = '', ?string $default = null) |
1255
|
|
|
{ |
1256
|
27 |
|
if ('' === $name) { |
1257
|
|
|
return $this->header; |
1258
|
|
|
} |
1259
|
|
|
|
1260
|
27 |
|
$name = str_replace('_', '-', strtolower($name)); |
1261
|
27 |
|
return $this->header[$name] ?? $default; |
1262
|
|
|
} |
1263
|
|
|
|
1264
|
|
|
/** |
1265
|
|
|
* 获取变量 支持过滤和默认值 |
1266
|
|
|
* @access public |
1267
|
|
|
* @param array $data 数据源 |
1268
|
|
|
* @param string|false $name 字段名 |
1269
|
|
|
* @param mixed $default 默认值 |
1270
|
|
|
* @param string|array|null $filter 过滤函数 |
1271
|
|
|
* @return mixed |
1272
|
|
|
*/ |
1273
|
27 |
|
public function input(array $data = [], string|bool $name = '', $default = null, string|array|null $filter = '') |
1274
|
|
|
{ |
1275
|
27 |
|
if (false === $name) { |
1276
|
|
|
// 获取原始数据 |
1277
|
27 |
|
return $data; |
1278
|
|
|
} |
1279
|
|
|
|
1280
|
27 |
|
$name = (string) $name; |
1281
|
27 |
|
if ('' != $name) { |
1282
|
|
|
// 解析name |
1283
|
|
|
if (str_contains($name, '/')) { |
1284
|
|
|
[$name, $type] = explode('/', $name); |
1285
|
|
|
} |
1286
|
|
|
|
1287
|
|
|
$data = $this->getData($data, $name); |
1288
|
|
|
|
1289
|
|
|
if (is_null($data)) { |
1290
|
|
|
return $default; |
1291
|
|
|
} |
1292
|
|
|
|
1293
|
|
|
if (is_object($data)) { |
1294
|
|
|
return $data; |
1295
|
|
|
} |
1296
|
|
|
} |
1297
|
|
|
|
1298
|
27 |
|
$data = $this->filterData($data, $filter, $name, $default); |
1299
|
|
|
|
1300
|
27 |
|
if (isset($type) && $data !== $default) { |
1301
|
|
|
// 强制类型转换 |
1302
|
|
|
$this->typeCast($data, $type); |
1303
|
|
|
} |
1304
|
|
|
|
1305
|
27 |
|
return $data; |
1306
|
|
|
} |
1307
|
|
|
|
1308
|
27 |
|
protected function filterData($data, $filter, $name, $default) |
1309
|
|
|
{ |
1310
|
|
|
// 解析过滤器 |
1311
|
27 |
|
$filter = $this->getFilter($filter, $default); |
1312
|
|
|
|
1313
|
27 |
|
if (is_array($data)) { |
1314
|
27 |
|
array_walk_recursive($data, [$this, 'filterValue'], $filter); |
1315
|
|
|
} else { |
1316
|
|
|
$this->filterValue($data, $name, $filter); |
1317
|
|
|
} |
1318
|
|
|
|
1319
|
27 |
|
return $data; |
1320
|
|
|
} |
1321
|
|
|
|
1322
|
|
|
/** |
1323
|
|
|
* 强制类型转换 |
1324
|
|
|
* @access protected |
1325
|
|
|
* @param mixed $data |
1326
|
|
|
* @param string $type |
1327
|
|
|
* @return mixed |
1328
|
|
|
*/ |
1329
|
|
|
protected function typeCast(&$data, string $type) |
1330
|
|
|
{ |
1331
|
|
|
$type = strtolower($type); |
1332
|
|
|
if (in_array($type, ['a', 'b', 'd', 'f', 's'])) { |
1333
|
|
|
$data = match ($type) { |
1334
|
|
|
'a' => (array) $data, // 数组 |
1335
|
|
|
'b' => (bool) $data, // 布尔 |
1336
|
|
|
'd' => (int) $data, // 数字 |
1337
|
|
|
'f' => (float) $data, // 浮点 |
1338
|
|
|
's' => is_scalar($data) ? (string) $data : throw new \InvalidArgumentException('variable type error:' . gettype($data)), //字符串 |
1339
|
|
|
}; |
1340
|
|
|
} |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
|
|
/** |
1344
|
|
|
* 获取数据 |
1345
|
|
|
* @access protected |
1346
|
|
|
* @param array $data 数据源 |
1347
|
|
|
* @param string $name 字段名 |
1348
|
|
|
* @param mixed $default 默认值 |
1349
|
|
|
* @return mixed |
1350
|
|
|
*/ |
1351
|
|
|
protected function getData(array $data, string $name, $default = null) |
1352
|
|
|
{ |
1353
|
|
|
foreach (explode('.', $name) as $val) { |
1354
|
|
|
if (isset($data[$val])) { |
1355
|
|
|
$data = $data[$val]; |
1356
|
|
|
} else { |
1357
|
|
|
return $default; |
1358
|
|
|
} |
1359
|
|
|
} |
1360
|
|
|
|
1361
|
|
|
return $data; |
1362
|
|
|
} |
1363
|
|
|
|
1364
|
|
|
/** |
1365
|
|
|
* 设置或获取当前的过滤规则 |
1366
|
|
|
* @access public |
1367
|
|
|
* @param mixed $filter 过滤规则 |
1368
|
|
|
* @return mixed |
1369
|
|
|
*/ |
1370
|
|
|
public function filter($filter = null) |
1371
|
|
|
{ |
1372
|
|
|
if (is_null($filter)) { |
1373
|
|
|
return $this->filter; |
1374
|
|
|
} |
1375
|
|
|
|
1376
|
|
|
$this->filter = $filter; |
1377
|
|
|
|
1378
|
|
|
return $this; |
1379
|
|
|
} |
1380
|
|
|
|
1381
|
27 |
|
protected function getFilter($filter, $default): array |
1382
|
|
|
{ |
1383
|
27 |
|
if (is_null($filter)) { |
1384
|
|
|
$filter = []; |
1385
|
|
|
} else { |
1386
|
27 |
|
$filter = $filter ?: $this->filter; |
1387
|
27 |
|
if (is_string($filter) && !str_contains($filter, '/')) { |
1388
|
|
|
$filter = explode(',', $filter); |
1389
|
|
|
} else { |
1390
|
27 |
|
$filter = (array) $filter; |
1391
|
|
|
} |
1392
|
|
|
} |
1393
|
|
|
|
1394
|
27 |
|
$filter[] = $default; |
1395
|
|
|
|
1396
|
27 |
|
return $filter; |
1397
|
|
|
} |
1398
|
|
|
|
1399
|
|
|
/** |
1400
|
|
|
* 递归过滤给定的值 |
1401
|
|
|
* @access public |
1402
|
|
|
* @param mixed $value 键值 |
1403
|
|
|
* @param mixed $key 键名 |
1404
|
|
|
* @param array $filters 过滤方法+默认值 |
1405
|
|
|
* @return mixed |
1406
|
|
|
*/ |
1407
|
|
|
public function filterValue(&$value, $key, $filters) |
1408
|
|
|
{ |
1409
|
|
|
$default = array_pop($filters); |
1410
|
|
|
|
1411
|
|
|
foreach ($filters as $filter) { |
1412
|
|
|
if (is_callable($filter)) { |
1413
|
|
|
// 调用函数或者方法过滤 |
1414
|
|
|
if (is_null($value)) { |
1415
|
|
|
continue; |
1416
|
|
|
} |
1417
|
|
|
|
1418
|
|
|
$value = call_user_func($filter, $value); |
1419
|
|
|
} elseif (is_scalar($value)) { |
1420
|
|
|
if (is_string($filter) && str_contains($filter, '/')) { |
1421
|
|
|
// 正则过滤 |
1422
|
|
|
if (!preg_match($filter, $value)) { |
1423
|
|
|
// 匹配不成功返回默认值 |
1424
|
|
|
$value = $default; |
1425
|
|
|
break; |
1426
|
|
|
} |
1427
|
|
|
} elseif (!empty($filter)) { |
1428
|
|
|
// filter函数不存在时, 则使用filter_var进行过滤 |
1429
|
|
|
// filter为非整形值时, 调用filter_id取得过滤id |
1430
|
|
|
$value = filter_var($value, is_int($filter) ? $filter : filter_id($filter)); |
1431
|
|
|
if (false === $value) { |
1432
|
|
|
$value = $default; |
1433
|
|
|
break; |
1434
|
|
|
} |
1435
|
|
|
} |
1436
|
|
|
} |
1437
|
|
|
} |
1438
|
|
|
|
1439
|
|
|
return $value; |
1440
|
|
|
} |
1441
|
|
|
|
1442
|
|
|
/** |
1443
|
|
|
* 是否存在某个请求参数 |
1444
|
|
|
* @access public |
1445
|
|
|
* @param string $name 变量名 |
1446
|
|
|
* @param string $type 变量类型 |
1447
|
|
|
* @param bool $checkEmpty 是否检测空值 |
1448
|
|
|
* @return bool |
1449
|
|
|
*/ |
1450
|
|
|
public function has(string $name, string $type = 'param', bool $checkEmpty = false): bool |
1451
|
|
|
{ |
1452
|
|
|
if (!in_array($type, ['param', 'get', 'post', 'put', 'patch', 'route', 'delete', 'cookie', 'session', 'env', 'request', 'server', 'header', 'file'])) { |
1453
|
|
|
return false; |
1454
|
|
|
} |
1455
|
|
|
|
1456
|
|
|
$param = empty($this->$type) ? $this->$type() : $this->$type; |
1457
|
|
|
|
1458
|
|
|
if (is_object($param)) { |
1459
|
|
|
return $param->has($name); |
1460
|
|
|
} |
1461
|
|
|
|
1462
|
|
|
// 按.拆分成多维数组进行判断 |
1463
|
|
|
foreach (explode('.', $name) as $val) { |
1464
|
|
|
if (isset($param[$val])) { |
1465
|
|
|
$param = $param[$val]; |
1466
|
|
|
} else { |
1467
|
|
|
return false; |
1468
|
|
|
} |
1469
|
|
|
} |
1470
|
|
|
|
1471
|
|
|
return ($checkEmpty && '' === $param) ? false : true; |
1472
|
|
|
} |
1473
|
|
|
|
1474
|
|
|
/** |
1475
|
|
|
* 获取指定的参数 |
1476
|
|
|
* @access public |
1477
|
|
|
* @param array $name 变量名 |
1478
|
|
|
* @param mixed $data 数据或者变量类型 |
1479
|
|
|
* @param string|array|null $filter 过滤方法 |
1480
|
|
|
* @return array |
1481
|
|
|
*/ |
1482
|
|
|
public function only(array $name, $data = 'param', string|array|null $filter = ''): array |
1483
|
|
|
{ |
1484
|
|
|
$data = is_array($data) ? $data : $this->$data(); |
1485
|
|
|
|
1486
|
|
|
$item = []; |
1487
|
|
|
foreach ($name as $key => $val) { |
1488
|
|
|
|
1489
|
|
|
if (is_int($key)) { |
1490
|
|
|
$default = null; |
1491
|
|
|
$key = $val; |
1492
|
|
|
if (!key_exists($key, $data)) { |
1493
|
|
|
continue; |
1494
|
|
|
} |
1495
|
|
|
} else { |
1496
|
|
|
$default = $val; |
1497
|
|
|
} |
1498
|
|
|
|
1499
|
|
|
$item[$key] = $this->filterData($data[$key] ?? $default, $filter, $key, $default); |
1500
|
|
|
} |
1501
|
|
|
|
1502
|
|
|
return $item; |
1503
|
|
|
} |
1504
|
|
|
|
1505
|
|
|
/** |
1506
|
|
|
* 排除指定参数获取 |
1507
|
|
|
* @access public |
1508
|
|
|
* @param array $name 变量名 |
1509
|
|
|
* @param string $type 变量类型 |
1510
|
|
|
* @return mixed |
1511
|
|
|
*/ |
1512
|
|
|
public function except(array $name, string $type = 'param'): array |
1513
|
|
|
{ |
1514
|
|
|
$param = $this->$type(); |
1515
|
|
|
|
1516
|
|
|
foreach ($name as $key) { |
1517
|
|
|
if (isset($param[$key])) { |
1518
|
|
|
unset($param[$key]); |
1519
|
|
|
} |
1520
|
|
|
} |
1521
|
|
|
|
1522
|
|
|
return $param; |
1523
|
|
|
} |
1524
|
|
|
|
1525
|
|
|
/** |
1526
|
|
|
* 当前是否ssl |
1527
|
|
|
* @access public |
1528
|
|
|
* @return bool |
1529
|
|
|
*/ |
1530
|
|
|
public function isSsl(): bool |
1531
|
|
|
{ |
1532
|
|
|
if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) { |
1533
|
|
|
return true; |
1534
|
|
|
} elseif ('https' == $this->server('REQUEST_SCHEME')) { |
1535
|
|
|
return true; |
1536
|
|
|
} elseif ('443' == $this->server('SERVER_PORT')) { |
1537
|
|
|
return true; |
1538
|
|
|
} elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) { |
1539
|
|
|
return true; |
1540
|
|
|
} elseif ($this->httpsAgentName && $this->server($this->httpsAgentName)) { |
1541
|
|
|
return true; |
1542
|
|
|
} |
1543
|
|
|
|
1544
|
|
|
return false; |
1545
|
|
|
} |
1546
|
|
|
|
1547
|
|
|
/** |
1548
|
|
|
* 当前是否JSON请求 |
1549
|
|
|
* @access public |
1550
|
|
|
* @return bool |
1551
|
|
|
*/ |
1552
|
21 |
|
public function isJson(): bool |
1553
|
|
|
{ |
1554
|
21 |
|
$acceptType = $this->type(); |
1555
|
|
|
|
1556
|
21 |
|
return str_contains($acceptType, 'json'); |
1557
|
|
|
} |
1558
|
|
|
|
1559
|
|
|
/** |
1560
|
|
|
* 当前是否Ajax请求 |
1561
|
|
|
* @access public |
1562
|
|
|
* @param bool $ajax true 获取原始ajax请求 |
1563
|
|
|
* @return bool |
1564
|
|
|
*/ |
1565
|
|
|
public function isAjax(bool $ajax = false): bool |
1566
|
|
|
{ |
1567
|
|
|
$value = $this->server('HTTP_X_REQUESTED_WITH'); |
1568
|
|
|
$result = $value && 'xmlhttprequest' == strtolower($value) ? true : false; |
1569
|
|
|
|
1570
|
|
|
if (true === $ajax) { |
1571
|
|
|
return $result; |
1572
|
|
|
} |
1573
|
|
|
|
1574
|
|
|
return $this->param($this->varAjax) ? true : $result; |
1575
|
|
|
} |
1576
|
|
|
|
1577
|
|
|
/** |
1578
|
|
|
* 当前是否Pjax请求 |
1579
|
|
|
* @access public |
1580
|
|
|
* @param bool $pjax true 获取原始pjax请求 |
1581
|
|
|
* @return bool |
1582
|
|
|
*/ |
1583
|
|
|
public function isPjax(bool $pjax = false): bool |
1584
|
|
|
{ |
1585
|
|
|
$result = !empty($this->server('HTTP_X_PJAX')) ? true : false; |
1586
|
|
|
|
1587
|
|
|
if (true === $pjax) { |
1588
|
|
|
return $result; |
1589
|
|
|
} |
1590
|
|
|
|
1591
|
|
|
return $this->param($this->varPjax) ? true : $result; |
1592
|
|
|
} |
1593
|
|
|
|
1594
|
|
|
/** |
1595
|
|
|
* 获取客户端IP地址 |
1596
|
|
|
* @access public |
1597
|
|
|
* @return string |
1598
|
|
|
*/ |
1599
|
|
|
public function ip(): string |
1600
|
|
|
{ |
1601
|
|
|
if (!empty($this->realIP)) { |
1602
|
|
|
return $this->realIP; |
1603
|
|
|
} |
1604
|
|
|
|
1605
|
|
|
$this->realIP = $this->server('REMOTE_ADDR', ''); |
1606
|
|
|
|
1607
|
|
|
// 如果指定了前端代理服务器IP以及其会发送的IP头 |
1608
|
|
|
// 则尝试获取前端代理服务器发送过来的真实IP |
1609
|
|
|
$proxyIp = $this->proxyServerIp; |
1610
|
|
|
$proxyIpHeader = $this->proxyServerIpHeader; |
1611
|
|
|
|
1612
|
|
|
if (count($proxyIp) > 0 && count($proxyIpHeader) > 0) { |
1613
|
|
|
// 从指定的HTTP头中依次尝试获取IP地址 |
1614
|
|
|
// 直到获取到一个合法的IP地址 |
1615
|
|
|
foreach ($proxyIpHeader as $header) { |
1616
|
|
|
$tempIP = $this->server($header); |
1617
|
|
|
|
1618
|
|
|
if (empty($tempIP)) { |
1619
|
|
|
continue; |
1620
|
|
|
} |
1621
|
|
|
|
1622
|
|
|
$tempIP = trim(explode(',', $tempIP)[0]); |
|
|
|
|
1623
|
|
|
|
1624
|
|
|
if (!$this->isValidIP($tempIP)) { |
1625
|
|
|
$tempIP = null; |
1626
|
|
|
} else { |
1627
|
|
|
break; |
1628
|
|
|
} |
1629
|
|
|
} |
1630
|
|
|
|
1631
|
|
|
// tempIP不为空,说明获取到了一个IP地址 |
1632
|
|
|
// 这时我们检查 REMOTE_ADDR 是不是指定的前端代理服务器之一 |
1633
|
|
|
// 如果是的话说明该 IP头 是由前端代理服务器设置的 |
1634
|
|
|
// 否则则是伪装的 |
1635
|
|
|
if (!empty($tempIP)) { |
1636
|
|
|
$realIPBin = $this->ip2bin($this->realIP); |
1637
|
|
|
|
1638
|
|
|
foreach ($proxyIp as $ip) { |
1639
|
|
|
$serverIPElements = explode('/', $ip); |
1640
|
|
|
$serverIP = $serverIPElements[0]; |
1641
|
|
|
$serverIPPrefix = $serverIPElements[1] ?? 128; |
1642
|
|
|
$serverIPBin = $this->ip2bin($serverIP); |
1643
|
|
|
|
1644
|
|
|
// IP类型不符 |
1645
|
|
|
if (strlen($realIPBin) !== strlen($serverIPBin)) { |
1646
|
|
|
continue; |
1647
|
|
|
} |
1648
|
|
|
|
1649
|
|
|
if (strncmp($realIPBin, $serverIPBin, (int) $serverIPPrefix) === 0) { |
1650
|
|
|
$this->realIP = $tempIP; |
1651
|
|
|
break; |
1652
|
|
|
} |
1653
|
|
|
} |
1654
|
|
|
} |
1655
|
|
|
} |
1656
|
|
|
|
1657
|
|
|
if (!$this->isValidIP($this->realIP)) { |
1658
|
|
|
$this->realIP = '0.0.0.0'; |
1659
|
|
|
} |
1660
|
|
|
|
1661
|
|
|
return $this->realIP; |
1662
|
|
|
} |
1663
|
|
|
|
1664
|
|
|
/** |
1665
|
|
|
* 检测是否是合法的IP地址 |
1666
|
|
|
* |
1667
|
|
|
* @param string $ip IP地址 |
1668
|
|
|
* @param string $type IP地址类型 (ipv4, ipv6) |
1669
|
|
|
* |
1670
|
|
|
* @return boolean |
1671
|
|
|
*/ |
1672
|
|
|
public function isValidIP(string $ip, string $type = ''): bool |
1673
|
|
|
{ |
1674
|
|
|
$flag = match (strtolower($type)) { |
1675
|
|
|
'ipv4' => FILTER_FLAG_IPV4, |
1676
|
|
|
'ipv6' => FILTER_FLAG_IPV6, |
1677
|
|
|
default => 0, |
1678
|
|
|
}; |
1679
|
|
|
|
1680
|
|
|
return boolval(filter_var($ip, FILTER_VALIDATE_IP, $flag)); |
1681
|
|
|
} |
1682
|
|
|
|
1683
|
|
|
/** |
1684
|
|
|
* 将IP地址转换为二进制字符串 |
1685
|
|
|
* |
1686
|
|
|
* @param string $ip |
1687
|
|
|
* |
1688
|
|
|
* @return string |
1689
|
|
|
*/ |
1690
|
|
|
public function ip2bin(string $ip): string |
1691
|
|
|
{ |
1692
|
|
|
if ($this->isValidIP($ip, 'ipv6')) { |
1693
|
|
|
$IPHex = str_split(bin2hex(inet_pton($ip)), 4); |
1694
|
|
|
foreach ($IPHex as $key => $value) { |
1695
|
|
|
$IPHex[$key] = intval($value, 16); |
1696
|
|
|
} |
1697
|
|
|
$IPBin = vsprintf('%016b%016b%016b%016b%016b%016b%016b%016b', $IPHex); |
|
|
|
|
1698
|
|
|
} else { |
1699
|
|
|
$IPHex = str_split(bin2hex(inet_pton($ip)), 2); |
1700
|
|
|
foreach ($IPHex as $key => $value) { |
1701
|
|
|
$IPHex[$key] = intval($value, 16); |
1702
|
|
|
} |
1703
|
|
|
$IPBin = vsprintf('%08b%08b%08b%08b', $IPHex); |
1704
|
|
|
} |
1705
|
|
|
|
1706
|
|
|
return $IPBin; |
1707
|
|
|
} |
1708
|
|
|
|
1709
|
|
|
/** |
1710
|
|
|
* 检测是否使用手机访问 |
1711
|
|
|
* @access public |
1712
|
|
|
* @return bool |
1713
|
|
|
*/ |
1714
|
|
|
public function isMobile(): bool |
1715
|
|
|
{ |
1716
|
|
|
if ($this->server('HTTP_VIA') && stristr($this->server('HTTP_VIA'), "wap")) { |
1717
|
|
|
return true; |
1718
|
|
|
} elseif ($this->server('HTTP_ACCEPT') && str_contains(strtoupper($this->server('HTTP_ACCEPT')), "VND.WAP.WML")) { |
1719
|
|
|
return true; |
1720
|
|
|
} elseif ($this->server('HTTP_X_WAP_PROFILE') || $this->server('HTTP_PROFILE')) { |
1721
|
|
|
return true; |
1722
|
|
|
} elseif ($this->server('HTTP_USER_AGENT') && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $this->server('HTTP_USER_AGENT'))) { |
1723
|
|
|
return true; |
1724
|
|
|
} |
1725
|
|
|
|
1726
|
|
|
return false; |
1727
|
|
|
} |
1728
|
|
|
|
1729
|
|
|
/** |
1730
|
|
|
* 当前URL地址中的scheme参数 |
1731
|
|
|
* @access public |
1732
|
|
|
* @return string |
1733
|
|
|
*/ |
1734
|
|
|
public function scheme(): string |
1735
|
|
|
{ |
1736
|
|
|
return $this->isSsl() ? 'https' : 'http'; |
1737
|
|
|
} |
1738
|
|
|
|
1739
|
|
|
/** |
1740
|
|
|
* 当前请求URL地址中的query参数 |
1741
|
|
|
* @access public |
1742
|
|
|
* @return string |
1743
|
|
|
*/ |
1744
|
|
|
public function query(): string |
1745
|
|
|
{ |
1746
|
|
|
return $this->server('QUERY_STRING', ''); |
1747
|
|
|
} |
1748
|
|
|
|
1749
|
|
|
/** |
1750
|
|
|
* 设置当前请求的host(包含端口) |
1751
|
|
|
* @access public |
1752
|
|
|
* @param string $host 主机名(含端口) |
1753
|
|
|
* @return $this |
1754
|
|
|
*/ |
1755
|
|
|
public function setHost(string $host) |
1756
|
|
|
{ |
1757
|
|
|
$this->host = $host; |
1758
|
|
|
|
1759
|
|
|
return $this; |
1760
|
|
|
} |
1761
|
|
|
|
1762
|
|
|
/** |
1763
|
|
|
* 当前请求的host |
1764
|
|
|
* @access public |
1765
|
|
|
* @param bool $strict true 仅仅获取HOST |
1766
|
|
|
* @return string |
1767
|
|
|
*/ |
1768
|
|
|
public function host(bool $strict = false): string |
1769
|
|
|
{ |
1770
|
|
|
if ($this->host) { |
1771
|
|
|
$host = $this->host; |
1772
|
|
|
} else { |
1773
|
|
|
$host = strval($this->server('HTTP_X_FORWARDED_HOST') ?: $this->server('HTTP_HOST')); |
1774
|
|
|
} |
1775
|
|
|
|
1776
|
|
|
return true === $strict && str_contains($host, ':') ? strstr($host, ':', true) : $host; |
1777
|
|
|
} |
1778
|
|
|
|
1779
|
|
|
/** |
1780
|
|
|
* 当前请求URL地址中的port参数 |
1781
|
|
|
* @access public |
1782
|
|
|
* @return int |
1783
|
|
|
*/ |
1784
|
|
|
public function port(): int |
1785
|
|
|
{ |
1786
|
|
|
return (int) ($this->server('HTTP_X_FORWARDED_PORT') ?: $this->server('SERVER_PORT', '')); |
1787
|
|
|
} |
1788
|
|
|
|
1789
|
|
|
/** |
1790
|
|
|
* 当前请求 SERVER_PROTOCOL |
1791
|
|
|
* @access public |
1792
|
|
|
* @return string |
1793
|
|
|
*/ |
1794
|
|
|
public function protocol(): string |
1795
|
|
|
{ |
1796
|
|
|
return $this->server('SERVER_PROTOCOL', ''); |
1797
|
|
|
} |
1798
|
|
|
|
1799
|
|
|
/** |
1800
|
|
|
* 当前请求 REMOTE_PORT |
1801
|
|
|
* @access public |
1802
|
|
|
* @return int |
1803
|
|
|
*/ |
1804
|
|
|
public function remotePort(): int |
1805
|
|
|
{ |
1806
|
|
|
return (int) $this->server('REMOTE_PORT', ''); |
1807
|
|
|
} |
1808
|
|
|
|
1809
|
|
|
/** |
1810
|
|
|
* 当前请求 HTTP_CONTENT_TYPE |
1811
|
|
|
* @access public |
1812
|
|
|
* @return string |
1813
|
|
|
*/ |
1814
|
27 |
|
public function contentType(): string |
1815
|
|
|
{ |
1816
|
27 |
|
$contentType = $this->header('Content-Type'); |
1817
|
|
|
|
1818
|
27 |
|
if ($contentType) { |
1819
|
|
|
if (str_contains($contentType, ';')) { |
|
|
|
|
1820
|
|
|
[$type] = explode(';', $contentType); |
|
|
|
|
1821
|
|
|
} else { |
1822
|
|
|
$type = $contentType; |
1823
|
|
|
} |
1824
|
|
|
return trim($type); |
|
|
|
|
1825
|
|
|
} |
1826
|
|
|
|
1827
|
27 |
|
return ''; |
1828
|
|
|
} |
1829
|
|
|
|
1830
|
|
|
/** |
1831
|
|
|
* 获取当前请求的安全Key |
1832
|
|
|
* @access public |
1833
|
|
|
* @return string |
1834
|
|
|
*/ |
1835
|
|
|
public function secureKey(): string |
1836
|
|
|
{ |
1837
|
|
|
if (is_null($this->secureKey)) { |
|
|
|
|
1838
|
|
|
$this->secureKey = uniqid('', true); |
1839
|
|
|
} |
1840
|
|
|
|
1841
|
|
|
return $this->secureKey; |
1842
|
|
|
} |
1843
|
|
|
|
1844
|
|
|
/** |
1845
|
|
|
* 设置当前的控制器名 |
1846
|
|
|
* @access public |
1847
|
|
|
* @param string $controller 控制器名 |
1848
|
|
|
* @return $this |
1849
|
|
|
*/ |
1850
|
12 |
|
public function setController(string $controller) |
1851
|
|
|
{ |
1852
|
12 |
|
$this->controller = $controller; |
1853
|
12 |
|
return $this; |
1854
|
|
|
} |
1855
|
|
|
|
1856
|
|
|
/** |
1857
|
|
|
* 设置当前的操作名 |
1858
|
|
|
* @access public |
1859
|
|
|
* @param string $action 操作名 |
1860
|
|
|
* @return $this |
1861
|
|
|
*/ |
1862
|
12 |
|
public function setAction(string $action) |
1863
|
|
|
{ |
1864
|
12 |
|
$this->action = $action; |
1865
|
12 |
|
return $this; |
1866
|
|
|
} |
1867
|
|
|
|
1868
|
|
|
/** |
1869
|
|
|
* 获取当前的控制器名 |
1870
|
|
|
* @access public |
1871
|
|
|
* @param bool $convert 转换为小写 |
1872
|
|
|
* @return string |
1873
|
|
|
*/ |
1874
|
|
|
public function controller(bool $convert = false): string |
1875
|
|
|
{ |
1876
|
|
|
$name = $this->controller ?: ''; |
1877
|
|
|
return $convert ? strtolower($name) : $name; |
1878
|
|
|
} |
1879
|
|
|
|
1880
|
|
|
/** |
1881
|
|
|
* 获取当前的操作名 |
1882
|
|
|
* @access public |
1883
|
|
|
* @param bool $convert 转换为小写 |
1884
|
|
|
* @return string |
1885
|
|
|
*/ |
1886
|
6 |
|
public function action(bool $convert = false): string |
1887
|
|
|
{ |
1888
|
6 |
|
$name = $this->action ?: ''; |
1889
|
6 |
|
return $convert ? strtolower($name) : $name; |
1890
|
|
|
} |
1891
|
|
|
|
1892
|
|
|
/** |
1893
|
|
|
* 设置或者获取当前请求的content |
1894
|
|
|
* @access public |
1895
|
|
|
* @return string |
1896
|
|
|
*/ |
1897
|
|
|
public function getContent(): string |
1898
|
|
|
{ |
1899
|
|
|
if (is_null($this->content)) { |
|
|
|
|
1900
|
|
|
$this->content = $this->input; |
1901
|
|
|
} |
1902
|
|
|
|
1903
|
|
|
return $this->content; |
1904
|
|
|
} |
1905
|
|
|
|
1906
|
|
|
/** |
1907
|
|
|
* 获取当前请求的php://input |
1908
|
|
|
* @access public |
1909
|
|
|
* @return string |
1910
|
|
|
*/ |
1911
|
|
|
public function getInput(): string |
1912
|
|
|
{ |
1913
|
|
|
return $this->input; |
1914
|
|
|
} |
1915
|
|
|
|
1916
|
|
|
/** |
1917
|
|
|
* 生成请求令牌 |
1918
|
|
|
* @access public |
1919
|
|
|
* @param string $name 令牌名称 |
1920
|
|
|
* @param mixed $type 令牌生成方法 |
1921
|
|
|
* @return string |
1922
|
|
|
*/ |
1923
|
|
|
public function buildToken(string $name = '__token__', $type = 'md5'): string |
1924
|
|
|
{ |
1925
|
|
|
$type = is_callable($type) ? $type : 'md5'; |
1926
|
|
|
$token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT')); |
1927
|
|
|
|
1928
|
|
|
$this->session->set($name, $token); |
|
|
|
|
1929
|
|
|
|
1930
|
|
|
return $token; |
1931
|
|
|
} |
1932
|
|
|
|
1933
|
|
|
/** |
1934
|
|
|
* 检查请求令牌 |
1935
|
|
|
* @access public |
1936
|
|
|
* @param string $token 令牌名称 |
1937
|
|
|
* @param array $data 表单数据 |
1938
|
|
|
* @return bool |
1939
|
|
|
*/ |
1940
|
|
|
public function checkToken(string $token = '__token__', array $data = []): bool |
1941
|
|
|
{ |
1942
|
|
|
if (in_array($this->method(), ['GET', 'HEAD', 'OPTIONS'], true)) { |
1943
|
|
|
return true; |
1944
|
|
|
} |
1945
|
|
|
|
1946
|
|
|
if (!$this->session->has($token)) { |
|
|
|
|
1947
|
|
|
// 令牌数据无效 |
1948
|
|
|
return false; |
1949
|
|
|
} |
1950
|
|
|
|
1951
|
|
|
// Header验证 |
1952
|
|
|
if ($this->header('X-CSRF-TOKEN') && $this->session->get($token) === $this->header('X-CSRF-TOKEN')) { |
1953
|
|
|
// 防止重复提交 |
1954
|
|
|
$this->session->delete($token); // 验证完成销毁session |
|
|
|
|
1955
|
|
|
return true; |
1956
|
|
|
} |
1957
|
|
|
|
1958
|
|
|
if (empty($data)) { |
1959
|
|
|
$data = $this->post(); |
1960
|
|
|
} |
1961
|
|
|
|
1962
|
|
|
// 令牌验证 |
1963
|
|
|
if (isset($data[$token]) && $this->session->get($token) === $data[$token]) { |
1964
|
|
|
// 防止重复提交 |
1965
|
|
|
$this->session->delete($token); // 验证完成销毁session |
1966
|
|
|
return true; |
1967
|
|
|
} |
1968
|
|
|
|
1969
|
|
|
// 开启TOKEN重置 |
1970
|
|
|
$this->session->delete($token); |
1971
|
|
|
return false; |
1972
|
|
|
} |
1973
|
|
|
|
1974
|
|
|
/** |
1975
|
|
|
* 设置在中间件传递的数据 |
1976
|
|
|
* @access public |
1977
|
|
|
* @param array $middleware 数据 |
1978
|
|
|
* @return $this |
1979
|
|
|
*/ |
1980
|
|
|
public function withMiddleware(array $middleware) |
1981
|
|
|
{ |
1982
|
|
|
$this->middleware = array_merge($this->middleware, $middleware); |
1983
|
|
|
return $this; |
1984
|
|
|
} |
1985
|
|
|
|
1986
|
|
|
/** |
1987
|
|
|
* 设置GET数据 |
1988
|
|
|
* @access public |
1989
|
|
|
* @param array $get 数据 |
1990
|
|
|
* @return $this |
1991
|
|
|
*/ |
1992
|
|
|
public function withGet(array $get) |
1993
|
|
|
{ |
1994
|
|
|
$this->get = $get; |
1995
|
|
|
return $this; |
1996
|
|
|
} |
1997
|
|
|
|
1998
|
|
|
/** |
1999
|
|
|
* 设置POST数据 |
2000
|
|
|
* @access public |
2001
|
|
|
* @param array $post 数据 |
2002
|
|
|
* @return $this |
2003
|
|
|
*/ |
2004
|
|
|
public function withPost(array $post) |
2005
|
|
|
{ |
2006
|
|
|
$this->post = $post; |
2007
|
|
|
return $this; |
2008
|
|
|
} |
2009
|
|
|
|
2010
|
|
|
/** |
2011
|
|
|
* 设置COOKIE数据 |
2012
|
|
|
* @access public |
2013
|
|
|
* @param array $cookie 数据 |
2014
|
|
|
* @return $this |
2015
|
|
|
*/ |
2016
|
|
|
public function withCookie(array $cookie) |
2017
|
|
|
{ |
2018
|
|
|
$this->cookie = $cookie; |
2019
|
|
|
return $this; |
2020
|
|
|
} |
2021
|
|
|
|
2022
|
|
|
/** |
2023
|
|
|
* 设置SESSION数据 |
2024
|
|
|
* @access public |
2025
|
|
|
* @param Session $session 数据 |
2026
|
|
|
* @return $this |
2027
|
|
|
*/ |
2028
|
|
|
public function withSession(Session $session) |
2029
|
|
|
{ |
2030
|
|
|
$this->session = $session; |
2031
|
|
|
return $this; |
2032
|
|
|
} |
2033
|
|
|
|
2034
|
|
|
/** |
2035
|
|
|
* 设置SERVER数据 |
2036
|
|
|
* @access public |
2037
|
|
|
* @param array $server 数据 |
2038
|
|
|
* @return $this |
2039
|
|
|
*/ |
2040
|
|
|
public function withServer(array $server) |
2041
|
|
|
{ |
2042
|
|
|
$this->server = array_change_key_case($server, CASE_UPPER); |
2043
|
|
|
return $this; |
2044
|
|
|
} |
2045
|
|
|
|
2046
|
|
|
/** |
2047
|
|
|
* 设置HEADER数据 |
2048
|
|
|
* @access public |
2049
|
|
|
* @param array $header 数据 |
2050
|
|
|
* @return $this |
2051
|
|
|
*/ |
2052
|
|
|
public function withHeader(array $header) |
2053
|
|
|
{ |
2054
|
|
|
$this->header = array_change_key_case($header); |
2055
|
|
|
return $this; |
2056
|
|
|
} |
2057
|
|
|
|
2058
|
|
|
/** |
2059
|
|
|
* 设置ENV数据 |
2060
|
|
|
* @access public |
2061
|
|
|
* @param Env $env 数据 |
2062
|
|
|
* @return $this |
2063
|
|
|
*/ |
2064
|
|
|
public function withEnv(Env $env) |
2065
|
|
|
{ |
2066
|
|
|
$this->env = $env; |
2067
|
|
|
return $this; |
2068
|
|
|
} |
2069
|
|
|
|
2070
|
|
|
/** |
2071
|
|
|
* 设置php://input数据 |
2072
|
|
|
* @access public |
2073
|
|
|
* @param string $input RAW数据 |
2074
|
|
|
* @return $this |
2075
|
|
|
*/ |
2076
|
|
|
public function withInput(string $input) |
2077
|
|
|
{ |
2078
|
|
|
$this->input = $input; |
2079
|
|
|
if (!empty($input)) { |
2080
|
|
|
$inputData = $this->getInputData($input); |
2081
|
|
|
if (!empty($inputData)) { |
2082
|
|
|
$this->post = $inputData; |
2083
|
|
|
$this->put = $inputData; |
2084
|
|
|
} |
2085
|
|
|
} |
2086
|
|
|
return $this; |
2087
|
|
|
} |
2088
|
|
|
|
2089
|
|
|
/** |
2090
|
|
|
* 设置文件上传数据 |
2091
|
|
|
* @access public |
2092
|
|
|
* @param array $files 上传信息 |
2093
|
|
|
* @return $this |
2094
|
|
|
*/ |
2095
|
|
|
public function withFiles(array $files) |
2096
|
|
|
{ |
2097
|
|
|
$this->file = $files; |
2098
|
|
|
return $this; |
2099
|
|
|
} |
2100
|
|
|
|
2101
|
|
|
/** |
2102
|
|
|
* 设置ROUTE变量 |
2103
|
|
|
* @access public |
2104
|
|
|
* @param array $route 数据 |
2105
|
|
|
* @return $this |
2106
|
|
|
*/ |
2107
|
|
|
public function withRoute(array $route) |
2108
|
|
|
{ |
2109
|
|
|
$this->route = $route; |
2110
|
|
|
return $this; |
2111
|
|
|
} |
2112
|
|
|
|
2113
|
|
|
/** |
2114
|
|
|
* 设置中间传递数据 |
2115
|
|
|
* @access public |
2116
|
|
|
* @param string $name 参数名 |
2117
|
|
|
* @param mixed $value 值 |
2118
|
|
|
*/ |
2119
|
|
|
public function __set(string $name, $value) |
2120
|
|
|
{ |
2121
|
|
|
$this->middleware[$name] = $value; |
2122
|
|
|
} |
2123
|
|
|
|
2124
|
|
|
/** |
2125
|
|
|
* 获取中间传递数据的值 |
2126
|
|
|
* @access public |
2127
|
|
|
* @param string $name 名称 |
2128
|
|
|
* @return mixed |
2129
|
|
|
*/ |
2130
|
|
|
public function __get(string $name) |
2131
|
|
|
{ |
2132
|
|
|
return $this->middleware($name); |
2133
|
|
|
} |
2134
|
|
|
|
2135
|
|
|
/** |
2136
|
|
|
* 检测中间传递数据的值 |
2137
|
|
|
* @access public |
2138
|
|
|
* @param string $name 名称 |
2139
|
|
|
* @return boolean |
2140
|
|
|
*/ |
2141
|
|
|
public function __isset(string $name): bool |
2142
|
|
|
{ |
2143
|
|
|
return isset($this->middleware[$name]); |
2144
|
|
|
} |
2145
|
|
|
|
2146
|
|
|
// ArrayAccess |
2147
|
|
|
public function offsetExists(mixed $name): bool |
2148
|
|
|
{ |
2149
|
|
|
return $this->has($name); |
2150
|
|
|
} |
2151
|
|
|
|
2152
|
|
|
public function offsetGet(mixed $name): mixed |
2153
|
|
|
{ |
2154
|
|
|
return $this->param($name); |
2155
|
|
|
} |
2156
|
|
|
|
2157
|
|
|
public function offsetSet(mixed $name, mixed $value): void {} |
2158
|
|
|
|
2159
|
|
|
public function offsetUnset(mixed $name): void {} |
2160
|
|
|
} |
2161
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: