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