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