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