| Total Complexity | 307 |
| Total Lines | 2139 |
| Duplicated Lines | 0 % |
| Coverage | 11% |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | 9 | $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 |
||
| 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; |
|
| 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], $this->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 | |||
| 640 | if (!empty($pathinfo)) { |
||
| 641 | unset($this->get[$pathinfo]); |
||
| 642 | } |
||
| 643 | |||
| 644 | $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/'); |
||
| 645 | } |
||
| 646 | |||
| 647 | return $this->pathinfo; |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * 当前URL的访问后缀 |
||
| 652 | * @access public |
||
| 653 | * @return string |
||
| 654 | */ |
||
| 655 | public function ext(): string |
||
| 656 | { |
||
| 657 | return pathinfo($this->pathinfo(), PATHINFO_EXTENSION); |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * 获取当前请求的时间 |
||
| 662 | * @access public |
||
| 663 | * @param bool $float 是否使用浮点类型 |
||
| 664 | * @return integer|float |
||
| 665 | */ |
||
| 666 | public function time(bool $float = false) |
||
| 667 | { |
||
| 668 | return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME'); |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * 当前请求的资源类型 |
||
| 673 | * @access public |
||
| 674 | * @return string |
||
| 675 | */ |
||
| 676 | 9 | public function type(): string |
|
| 677 | { |
||
| 678 | 9 | $accept = $this->server('HTTP_ACCEPT'); |
|
| 679 | |||
| 680 | 9 | if (empty($accept)) { |
|
| 681 | 9 | return ''; |
|
| 682 | } |
||
| 683 | |||
| 684 | foreach ($this->mimeType as $key => $val) { |
||
| 685 | $array = explode(',', $val); |
||
| 686 | foreach ($array as $k => $v) { |
||
| 687 | if (stristr($accept, $v)) { |
||
| 688 | return $key; |
||
| 689 | } |
||
| 690 | } |
||
| 691 | } |
||
| 692 | |||
| 693 | return ''; |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * 设置资源类型 |
||
| 698 | * @access public |
||
| 699 | * @param string|array $type 资源类型名 |
||
| 700 | * @param string $val 资源类型 |
||
| 701 | * @return void |
||
| 702 | */ |
||
| 703 | public function mimeType($type, $val = ''): void |
||
| 704 | { |
||
| 705 | if (is_array($type)) { |
||
| 706 | $this->mimeType = array_merge($this->mimeType, $type); |
||
| 707 | } else { |
||
| 708 | $this->mimeType[$type] = $val; |
||
| 709 | } |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * 设置请求类型 |
||
| 714 | * @access public |
||
| 715 | * @param string $method 请求类型 |
||
| 716 | * @return $this |
||
| 717 | */ |
||
| 718 | public function setMethod(string $method) |
||
| 719 | { |
||
| 720 | $this->method = strtoupper($method); |
||
| 721 | return $this; |
||
| 722 | } |
||
| 723 | |||
| 724 | /** |
||
| 725 | * 当前的请求类型 |
||
| 726 | * @access public |
||
| 727 | * @param bool $origin 是否获取原始请求类型 |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | public function method(bool $origin = false): string |
||
| 731 | { |
||
| 732 | if ($origin) { |
||
| 733 | // 获取原始请求类型 |
||
| 734 | return $this->server('REQUEST_METHOD') ?: 'GET'; |
||
| 735 | } elseif (!$this->method) { |
||
| 736 | if (isset($this->post[$this->varMethod])) { |
||
| 737 | $method = strtolower($this->post[$this->varMethod]); |
||
| 738 | if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { |
||
| 739 | $this->method = strtoupper($method); |
||
| 740 | $this->{$method} = $this->post; |
||
| 741 | } else { |
||
| 742 | $this->method = 'POST'; |
||
| 743 | } |
||
| 744 | unset($this->post[$this->varMethod]); |
||
| 745 | } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) { |
||
| 746 | $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')); |
||
| 747 | } else { |
||
| 748 | $this->method = $this->server('REQUEST_METHOD') ?: 'GET'; |
||
| 749 | } |
||
| 750 | } |
||
| 751 | |||
| 752 | return $this->method; |
||
| 753 | } |
||
| 754 | |||
| 755 | /** |
||
| 756 | * 是否为GET请求 |
||
| 757 | * @access public |
||
| 758 | * @return bool |
||
| 759 | */ |
||
| 760 | public function isGet(): bool |
||
| 761 | { |
||
| 762 | return $this->method() == 'GET'; |
||
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * 是否为POST请求 |
||
| 767 | * @access public |
||
| 768 | * @return bool |
||
| 769 | */ |
||
| 770 | public function isPost(): bool |
||
| 771 | { |
||
| 772 | return $this->method() == 'POST'; |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * 是否为PUT请求 |
||
| 777 | * @access public |
||
| 778 | * @return bool |
||
| 779 | */ |
||
| 780 | public function isPut(): bool |
||
| 781 | { |
||
| 782 | return $this->method() == 'PUT'; |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * 是否为DELTE请求 |
||
| 787 | * @access public |
||
| 788 | * @return bool |
||
| 789 | */ |
||
| 790 | public function isDelete(): bool |
||
| 791 | { |
||
| 792 | return $this->method() == 'DELETE'; |
||
| 793 | } |
||
| 794 | |||
| 795 | /** |
||
| 796 | * 是否为HEAD请求 |
||
| 797 | * @access public |
||
| 798 | * @return bool |
||
| 799 | */ |
||
| 800 | public function isHead(): bool |
||
| 801 | { |
||
| 802 | return $this->method() == 'HEAD'; |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * 是否为PATCH请求 |
||
| 807 | * @access public |
||
| 808 | * @return bool |
||
| 809 | */ |
||
| 810 | public function isPatch(): bool |
||
| 811 | { |
||
| 812 | return $this->method() == 'PATCH'; |
||
| 813 | } |
||
| 814 | |||
| 815 | /** |
||
| 816 | * 是否为OPTIONS请求 |
||
| 817 | * @access public |
||
| 818 | * @return bool |
||
| 819 | */ |
||
| 820 | public function isOptions(): bool |
||
| 821 | { |
||
| 822 | return $this->method() == 'OPTIONS'; |
||
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * 是否为cli |
||
| 827 | * @access public |
||
| 828 | * @return bool |
||
| 829 | */ |
||
| 830 | public function isCli(): bool |
||
| 831 | { |
||
| 832 | return PHP_SAPI == 'cli'; |
||
| 833 | } |
||
| 834 | |||
| 835 | /** |
||
| 836 | * 是否为cgi |
||
| 837 | * @access public |
||
| 838 | * @return bool |
||
| 839 | */ |
||
| 840 | public function isCgi(): bool |
||
| 841 | { |
||
| 842 | return strpos(PHP_SAPI, 'cgi') === 0; |
||
| 843 | } |
||
| 844 | |||
| 845 | /** |
||
| 846 | * 获取当前请求的参数 |
||
| 847 | * @access public |
||
| 848 | * @param string|array $name 变量名 |
||
| 849 | * @param mixed $default 默认值 |
||
| 850 | * @param string|array $filter 过滤方法 |
||
| 851 | * @return mixed |
||
| 852 | */ |
||
| 853 | public function param($name = '', $default = null, $filter = '') |
||
| 854 | { |
||
| 855 | if (empty($this->mergeParam)) { |
||
| 856 | $method = $this->method(true); |
||
| 857 | |||
| 858 | // 自动获取请求变量 |
||
| 859 | switch ($method) { |
||
| 860 | case 'POST': |
||
| 861 | $vars = $this->post(false); |
||
| 862 | break; |
||
| 863 | case 'PUT': |
||
| 864 | case 'DELETE': |
||
| 865 | case 'PATCH': |
||
| 866 | $vars = $this->put(false); |
||
| 867 | break; |
||
| 868 | default: |
||
| 869 | $vars = []; |
||
| 870 | } |
||
| 871 | |||
| 872 | // 当前请求参数和URL地址中的参数合并 |
||
| 873 | $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false)); |
||
| 874 | |||
| 875 | $this->mergeParam = true; |
||
| 876 | } |
||
| 877 | |||
| 878 | if (is_array($name)) { |
||
| 879 | return $this->only($name, $this->param, $filter); |
||
| 880 | } |
||
| 881 | |||
| 882 | return $this->input($this->param, $name, $default, $filter); |
||
| 883 | } |
||
| 884 | |||
| 885 | /** |
||
| 886 | * 设置路由变量 |
||
| 887 | * @access public |
||
| 888 | * @param Rule $rule 路由对象 |
||
| 889 | * @return $this |
||
| 890 | */ |
||
| 891 | public function setRule(Rule $rule) |
||
| 892 | { |
||
| 893 | $this->rule = $rule; |
||
| 894 | return $this; |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * 获取当前路由对象 |
||
| 899 | * @access public |
||
| 900 | * @return Rule|null |
||
| 901 | */ |
||
| 902 | public function rule() |
||
| 903 | { |
||
| 904 | return $this->rule; |
||
| 905 | } |
||
| 906 | |||
| 907 | /** |
||
| 908 | * 设置路由变量 |
||
| 909 | * @access public |
||
| 910 | * @param array $route 路由变量 |
||
| 911 | * @return $this |
||
| 912 | */ |
||
| 913 | public function setRoute(array $route) |
||
| 914 | { |
||
| 915 | $this->route = array_merge($this->route, $route); |
||
| 916 | return $this; |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * 获取路由参数 |
||
| 921 | * @access public |
||
| 922 | * @param mixed $name 变量名 |
||
| 923 | * @param mixed $default 默认值 |
||
| 924 | * @param string|array $filter 过滤方法 |
||
| 925 | * @return mixed |
||
| 926 | */ |
||
| 927 | public function route($name = '', $default = null, $filter = '') |
||
| 928 | { |
||
| 929 | if (is_array($name)) { |
||
| 930 | return $this->only($name, $this->route, $filter); |
||
| 931 | } |
||
| 932 | |||
| 933 | return $this->input($this->route, $name, $default, $filter); |
||
| 934 | } |
||
| 935 | |||
| 936 | /** |
||
| 937 | * 获取GET参数 |
||
| 938 | * @access public |
||
| 939 | * @param mixed $name 变量名 |
||
| 940 | * @param mixed $default 默认值 |
||
| 941 | * @param string|array $filter 过滤方法 |
||
| 942 | * @return mixed |
||
| 943 | */ |
||
| 944 | public function get($name = '', $default = null, $filter = '') |
||
| 945 | { |
||
| 946 | if (is_array($name)) { |
||
| 947 | return $this->only($name, $this->get, $filter); |
||
| 948 | } |
||
| 949 | |||
| 950 | return $this->input($this->get, $name, $default, $filter); |
||
| 951 | } |
||
| 952 | |||
| 953 | /** |
||
| 954 | * 获取中间件传递的参数 |
||
| 955 | * @access public |
||
| 956 | * @param mixed $name 变量名 |
||
| 957 | * @param mixed $default 默认值 |
||
| 958 | * @return mixed |
||
| 959 | */ |
||
| 960 | public function middleware($name, $default = null) |
||
| 961 | { |
||
| 962 | return $this->middleware[$name] ?? $default; |
||
| 963 | } |
||
| 964 | |||
| 965 | /** |
||
| 966 | * 获取POST参数 |
||
| 967 | * @access public |
||
| 968 | * @param mixed $name 变量名 |
||
| 969 | * @param mixed $default 默认值 |
||
| 970 | * @param string|array $filter 过滤方法 |
||
| 971 | * @return mixed |
||
| 972 | */ |
||
| 973 | public function post($name = '', $default = null, $filter = '') |
||
| 974 | { |
||
| 975 | if (is_array($name)) { |
||
| 976 | return $this->only($name, $this->post, $filter); |
||
| 977 | } |
||
| 978 | |||
| 979 | return $this->input($this->post, $name, $default, $filter); |
||
| 980 | } |
||
| 981 | |||
| 982 | /** |
||
| 983 | * 获取PUT参数 |
||
| 984 | * @access public |
||
| 985 | * @param mixed $name 变量名 |
||
| 986 | * @param mixed $default 默认值 |
||
| 987 | * @param string|array $filter 过滤方法 |
||
| 988 | * @return mixed |
||
| 989 | */ |
||
| 990 | public function put($name = '', $default = null, $filter = '') |
||
| 991 | { |
||
| 992 | if (is_array($name)) { |
||
| 993 | return $this->only($name, $this->put, $filter); |
||
| 994 | } |
||
| 995 | |||
| 996 | return $this->input($this->put, $name, $default, $filter); |
||
| 997 | } |
||
| 998 | |||
| 999 | 9 | protected function getInputData($content): array |
|
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * 设置获取DELETE参数 |
||
| 1013 | * @access public |
||
| 1014 | * @param mixed $name 变量名 |
||
| 1015 | * @param mixed $default 默认值 |
||
| 1016 | * @param string|array $filter 过滤方法 |
||
| 1017 | * @return mixed |
||
| 1018 | */ |
||
| 1019 | public function delete($name = '', $default = null, $filter = '') |
||
| 1020 | { |
||
| 1021 | return $this->put($name, $default, $filter); |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * 设置获取PATCH参数 |
||
| 1026 | * @access public |
||
| 1027 | * @param mixed $name 变量名 |
||
| 1028 | * @param mixed $default 默认值 |
||
| 1029 | * @param string|array $filter 过滤方法 |
||
| 1030 | * @return mixed |
||
| 1031 | */ |
||
| 1032 | public function patch($name = '', $default = null, $filter = '') |
||
| 1033 | { |
||
| 1034 | return $this->put($name, $default, $filter); |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * 获取request变量 |
||
| 1039 | * @access public |
||
| 1040 | * @param mixed $name 数据名称 |
||
| 1041 | * @param mixed $default 默认值 |
||
| 1042 | * @param string|array $filter 过滤方法 |
||
| 1043 | * @return mixed |
||
| 1044 | */ |
||
| 1045 | public function request($name = '', $default = null, $filter = '') |
||
| 1046 | { |
||
| 1047 | if (is_array($name)) { |
||
| 1048 | return $this->only($name, $this->request, $filter); |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | return $this->input($this->request, $name, $default, $filter); |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * 获取环境变量 |
||
| 1056 | * @access public |
||
| 1057 | * @param string $name 数据名称 |
||
| 1058 | * @param string $default 默认值 |
||
| 1059 | * @return mixed |
||
| 1060 | */ |
||
| 1061 | public function env(string $name = '', string $default = null) |
||
| 1062 | { |
||
| 1063 | if (empty($name)) { |
||
| 1064 | return $this->env->get(); |
||
| 1065 | } else { |
||
| 1066 | $name = strtoupper($name); |
||
| 1067 | } |
||
| 1068 | |||
| 1069 | return $this->env->get($name, $default); |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * 获取session数据 |
||
| 1074 | * @access public |
||
| 1075 | * @param string $name 数据名称 |
||
| 1076 | * @param string $default 默认值 |
||
| 1077 | * @return mixed |
||
| 1078 | */ |
||
| 1079 | public function session(string $name = '', $default = null) |
||
| 1080 | { |
||
| 1081 | if ('' === $name) { |
||
| 1082 | return $this->session->get(); |
||
| 1083 | } |
||
| 1084 | |||
| 1085 | return $this->getData($this->session->get(), $name, $default); |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * 获取cookie参数 |
||
| 1090 | * @access public |
||
| 1091 | * @param mixed $name 数据名称 |
||
| 1092 | * @param string $default 默认值 |
||
| 1093 | * @param string|array $filter 过滤方法 |
||
| 1094 | * @return mixed |
||
| 1095 | */ |
||
| 1096 | public function cookie(string $name = '', $default = null, $filter = '') |
||
| 1097 | { |
||
| 1098 | if (!empty($name)) { |
||
| 1099 | $data = $this->getData($this->cookie, $name, $default); |
||
| 1100 | } else { |
||
| 1101 | $data = $this->cookie; |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | // 解析过滤器 |
||
| 1105 | $filter = $this->getFilter($filter, $default); |
||
| 1106 | |||
| 1107 | if (is_array($data)) { |
||
| 1108 | array_walk_recursive($data, [$this, 'filterValue'], $filter); |
||
| 1109 | reset($data); |
||
| 1110 | } else { |
||
| 1111 | $this->filterValue($data, $name, $filter); |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | return $data; |
||
| 1115 | } |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * 获取server参数 |
||
| 1119 | * @access public |
||
| 1120 | * @param string $name 数据名称 |
||
| 1121 | * @param string $default 默认值 |
||
| 1122 | * @return mixed |
||
| 1123 | */ |
||
| 1124 | 9 | public function server(string $name = '', string $default = '') |
|
| 1125 | { |
||
| 1126 | 9 | if (empty($name)) { |
|
| 1127 | return $this->server; |
||
| 1128 | } else { |
||
| 1129 | 9 | $name = strtoupper($name); |
|
| 1130 | } |
||
| 1131 | |||
| 1132 | 9 | return $this->server[$name] ?? $default; |
|
| 1133 | } |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * 获取上传的文件信息 |
||
| 1137 | * @access public |
||
| 1138 | * @param string $name 名称 |
||
| 1139 | * @return null|array|\think\file\UploadedFile |
||
| 1140 | */ |
||
| 1141 | public function file(string $name = '') |
||
| 1142 | { |
||
| 1143 | $files = $this->file; |
||
| 1144 | if (!empty($files)) { |
||
| 1145 | |||
| 1146 | if (strpos($name, '.')) { |
||
| 1147 | list($name, $sub) = explode('.', $name); |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | // 处理上传文件 |
||
| 1151 | $array = $this->dealUploadFile($files, $name); |
||
| 1152 | |||
| 1153 | if ('' === $name) { |
||
| 1154 | // 获取全部文件 |
||
| 1155 | return $array; |
||
| 1156 | } elseif (isset($sub) && isset($array[$name][$sub])) { |
||
| 1157 | return $array[$name][$sub]; |
||
| 1158 | } elseif (isset($array[$name])) { |
||
| 1159 | return $array[$name]; |
||
| 1160 | } |
||
| 1161 | } |
||
| 1162 | } |
||
| 1163 | |||
| 1164 | protected function dealUploadFile(array $files, string $name): array |
||
| 1165 | { |
||
| 1166 | $array = []; |
||
| 1167 | foreach ($files as $key => $file) { |
||
| 1168 | if (is_array($file['name'])) { |
||
| 1169 | $item = []; |
||
| 1170 | $keys = array_keys($file); |
||
| 1171 | $count = count($file['name']); |
||
| 1172 | |||
| 1173 | for ($i = 0; $i < $count; $i++) { |
||
| 1174 | if ($file['error'][$i] > 0) { |
||
| 1175 | if ($name == $key) { |
||
| 1176 | $this->throwUploadFileError($file['error'][$i]); |
||
| 1177 | } else { |
||
| 1178 | continue; |
||
| 1179 | } |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | $temp['key'] = $key; |
||
| 1183 | |||
| 1184 | foreach ($keys as $_key) { |
||
| 1185 | $temp[$_key] = $file[$_key][$i]; |
||
| 1186 | } |
||
| 1187 | |||
| 1188 | $item[] = new UploadedFile($temp['tmp_name'], $temp['name'], $temp['type'], $temp['error']); |
||
| 1189 | } |
||
| 1190 | |||
| 1191 | $array[$key] = $item; |
||
| 1192 | } else { |
||
| 1193 | if ($file instanceof File) { |
||
| 1194 | $array[$key] = $file; |
||
| 1195 | } else { |
||
| 1196 | if ($file['error'] > 0) { |
||
| 1197 | if ($key == $name) { |
||
| 1198 | $this->throwUploadFileError($file['error']); |
||
| 1199 | } else { |
||
| 1200 | continue; |
||
| 1201 | } |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | $array[$key] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']); |
||
| 1205 | } |
||
| 1206 | } |
||
| 1207 | } |
||
| 1208 | |||
| 1209 | return $array; |
||
| 1210 | } |
||
| 1211 | |||
| 1212 | protected function throwUploadFileError($error) |
||
| 1213 | { |
||
| 1214 | static $fileUploadErrors = [ |
||
| 1215 | 1 => 'upload File size exceeds the maximum value', |
||
| 1216 | 2 => 'upload File size exceeds the maximum value', |
||
| 1217 | 3 => 'only the portion of file is uploaded', |
||
| 1218 | 4 => 'no file to uploaded', |
||
| 1219 | 6 => 'upload temp dir not found', |
||
| 1220 | 7 => 'file write error', |
||
| 1221 | ]; |
||
| 1222 | |||
| 1223 | $msg = $fileUploadErrors[$error]; |
||
| 1224 | throw new Exception($msg, $error); |
||
| 1225 | } |
||
| 1226 | |||
| 1227 | /** |
||
| 1228 | * 设置或者获取当前的Header |
||
| 1229 | * @access public |
||
| 1230 | * @param string $name header名称 |
||
| 1231 | * @param string $default 默认值 |
||
| 1232 | * @return string|array |
||
| 1233 | */ |
||
| 1234 | public function header(string $name = '', string $default = null) |
||
| 1235 | { |
||
| 1236 | if ('' === $name) { |
||
| 1237 | return $this->header; |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | $name = str_replace('_', '-', strtolower($name)); |
||
| 1241 | |||
| 1242 | return $this->header[$name] ?? $default; |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * 获取变量 支持过滤和默认值 |
||
| 1247 | * @access public |
||
| 1248 | * @param array $data 数据源 |
||
| 1249 | * @param string|false $name 字段名 |
||
| 1250 | * @param mixed $default 默认值 |
||
| 1251 | * @param string|array $filter 过滤函数 |
||
| 1252 | * @return mixed |
||
| 1253 | */ |
||
| 1254 | public function input(array $data = [], $name = '', $default = null, $filter = '') |
||
| 1255 | { |
||
| 1256 | if (false === $name) { |
||
| 1257 | // 获取原始数据 |
||
| 1258 | return $data; |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | $name = (string) $name; |
||
| 1262 | if ('' != $name) { |
||
| 1263 | // 解析name |
||
| 1264 | if (strpos($name, '/')) { |
||
| 1265 | list($name, $type) = explode('/', $name); |
||
| 1266 | } |
||
| 1267 | |||
| 1268 | $data = $this->getData($data, $name); |
||
| 1269 | |||
| 1270 | if (is_null($data)) { |
||
| 1271 | return $default; |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | if (is_object($data)) { |
||
| 1275 | return $data; |
||
| 1276 | } |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | $data = $this->filterData($data, $filter, $name, $default); |
||
| 1280 | |||
| 1281 | if (isset($type) && $data !== $default) { |
||
| 1282 | // 强制类型转换 |
||
| 1283 | $this->typeCast($data, $type); |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | return $data; |
||
| 1287 | } |
||
| 1288 | |||
| 1289 | protected function filterData($data, $filter, $name, $default) |
||
| 1290 | { |
||
| 1291 | // 解析过滤器 |
||
| 1292 | $filter = $this->getFilter($filter, $default); |
||
| 1293 | |||
| 1294 | if (is_array($data)) { |
||
| 1295 | array_walk_recursive($data, [$this, 'filterValue'], $filter); |
||
| 1296 | reset($data); |
||
| 1297 | } else { |
||
| 1298 | $this->filterValue($data, $name, $filter); |
||
| 1299 | } |
||
| 1300 | |||
| 1301 | return $data; |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * 强制类型转换 |
||
| 1306 | * @access public |
||
| 1307 | * @param mixed $data |
||
| 1308 | * @param string $type |
||
| 1309 | * @return mixed |
||
| 1310 | */ |
||
| 1311 | private function typeCast(&$data, string $type) |
||
| 1312 | { |
||
| 1313 | switch (strtolower($type)) { |
||
| 1314 | // 数组 |
||
| 1315 | case 'a': |
||
| 1316 | $data = (array) $data; |
||
| 1317 | break; |
||
| 1318 | // 数字 |
||
| 1319 | case 'd': |
||
| 1320 | $data = (int) $data; |
||
| 1321 | break; |
||
| 1322 | // 浮点 |
||
| 1323 | case 'f': |
||
| 1324 | $data = (float) $data; |
||
| 1325 | break; |
||
| 1326 | // 布尔 |
||
| 1327 | case 'b': |
||
| 1328 | $data = (boolean) $data; |
||
| 1329 | break; |
||
| 1330 | // 字符串 |
||
| 1331 | case 's': |
||
| 1332 | if (is_scalar($data)) { |
||
| 1333 | $data = (string) $data; |
||
| 1334 | } else { |
||
| 1335 | throw new \InvalidArgumentException('variable type error:' . gettype($data)); |
||
| 1336 | } |
||
| 1337 | break; |
||
| 1338 | } |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | /** |
||
| 1342 | * 获取数据 |
||
| 1343 | * @access public |
||
| 1344 | * @param array $data 数据源 |
||
| 1345 | * @param string $name 字段名 |
||
| 1346 | * @param mixed $default 默认值 |
||
| 1347 | * @return mixed |
||
| 1348 | */ |
||
| 1349 | protected function getData(array $data, string $name, $default = null) |
||
| 1350 | { |
||
| 1351 | foreach (explode('.', $name) as $val) { |
||
| 1352 | if (isset($data[$val])) { |
||
| 1353 | $data = $data[$val]; |
||
| 1354 | } else { |
||
| 1355 | return $default; |
||
| 1356 | } |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | return $data; |
||
| 1360 | } |
||
| 1361 | |||
| 1362 | /** |
||
| 1363 | * 设置或获取当前的过滤规则 |
||
| 1364 | * @access public |
||
| 1365 | * @param mixed $filter 过滤规则 |
||
| 1366 | * @return mixed |
||
| 1367 | */ |
||
| 1368 | public function filter($filter = null) |
||
| 1369 | { |
||
| 1370 | if (is_null($filter)) { |
||
| 1371 | return $this->filter; |
||
| 1372 | } |
||
| 1373 | |||
| 1374 | $this->filter = $filter; |
||
| 1375 | |||
| 1376 | return $this; |
||
| 1377 | } |
||
| 1378 | |||
| 1379 | protected function getFilter($filter, $default): array |
||
| 1380 | { |
||
| 1381 | if (is_null($filter)) { |
||
| 1382 | $filter = []; |
||
| 1383 | } else { |
||
| 1384 | $filter = $filter ?: $this->filter; |
||
| 1385 | if (is_string($filter) && false === strpos($filter, '/')) { |
||
| 1386 | $filter = explode(',', $filter); |
||
| 1387 | } else { |
||
| 1388 | $filter = (array) $filter; |
||
| 1389 | } |
||
| 1390 | } |
||
| 1391 | |||
| 1392 | $filter[] = $default; |
||
| 1393 | |||
| 1394 | return $filter; |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * 递归过滤给定的值 |
||
| 1399 | * @access public |
||
| 1400 | * @param mixed $value 键值 |
||
| 1401 | * @param mixed $key 键名 |
||
| 1402 | * @param array $filters 过滤方法+默认值 |
||
| 1403 | * @return mixed |
||
| 1404 | */ |
||
| 1405 | private function filterValue(&$value, $key, $filters) |
||
| 1406 | { |
||
| 1407 | $default = array_pop($filters); |
||
| 1408 | |||
| 1409 | foreach ($filters as $filter) { |
||
| 1410 | if (is_callable($filter)) { |
||
| 1411 | // 调用函数或者方法过滤 |
||
| 1412 | $value = call_user_func($filter, $value); |
||
| 1413 | } elseif (is_scalar($value)) { |
||
| 1414 | if (is_string($filter) && false !== strpos($filter, '/')) { |
||
| 1415 | // 正则过滤 |
||
| 1416 | if (!preg_match($filter, $value)) { |
||
| 1417 | // 匹配不成功返回默认值 |
||
| 1418 | $value = $default; |
||
| 1419 | break; |
||
| 1420 | } |
||
| 1421 | } elseif (!empty($filter)) { |
||
| 1422 | // filter函数不存在时, 则使用filter_var进行过滤 |
||
| 1423 | // filter为非整形值时, 调用filter_id取得过滤id |
||
| 1424 | $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter)); |
||
| 1425 | if (false === $value) { |
||
| 1426 | $value = $default; |
||
| 1427 | break; |
||
| 1428 | } |
||
| 1429 | } |
||
| 1430 | } |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | return $value; |
||
| 1434 | } |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * 是否存在某个请求参数 |
||
| 1438 | * @access public |
||
| 1439 | * @param string $name 变量名 |
||
| 1440 | * @param string $type 变量类型 |
||
| 1441 | * @param bool $checkEmpty 是否检测空值 |
||
| 1442 | * @return bool |
||
| 1443 | */ |
||
| 1444 | public function has(string $name, string $type = 'param', bool $checkEmpty = false): bool |
||
| 1445 | { |
||
| 1446 | if (!in_array($type, ['param', 'get', 'post', 'put', 'patch', 'route', 'delete', 'cookie', 'session', 'env', 'request', 'server', 'header', 'file'])) { |
||
| 1447 | return false; |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | $param = empty($this->$type) ? $this->$type() : $this->$type; |
||
| 1451 | |||
| 1452 | if (is_object($param)) { |
||
| 1453 | return $param->has($name); |
||
| 1454 | } |
||
| 1455 | |||
| 1456 | // 按.拆分成多维数组进行判断 |
||
| 1457 | foreach (explode('.', $name) as $val) { |
||
| 1458 | if (isset($param[$val])) { |
||
| 1459 | $param = $param[$val]; |
||
| 1460 | } else { |
||
| 1461 | return false; |
||
| 1462 | } |
||
| 1463 | } |
||
| 1464 | |||
| 1465 | return ($checkEmpty && '' === $param) ? false : true; |
||
| 1466 | } |
||
| 1467 | |||
| 1468 | /** |
||
| 1469 | * 获取指定的参数 |
||
| 1470 | * @access public |
||
| 1471 | * @param array $name 变量名 |
||
| 1472 | * @param mixed $data 数据或者变量类型 |
||
| 1473 | * @param string|array $filter 过滤方法 |
||
| 1474 | * @return array |
||
| 1475 | */ |
||
| 1476 | public function only(array $name, $data = 'param', $filter = ''): array |
||
| 1477 | { |
||
| 1478 | $data = is_array($data) ? $data : $this->$data(); |
||
| 1479 | |||
| 1480 | $item = []; |
||
| 1481 | foreach ($name as $key => $val) { |
||
| 1482 | |||
| 1483 | if (is_int($key)) { |
||
| 1484 | $default = null; |
||
| 1485 | $key = $val; |
||
| 1486 | if (!isset($data[$key])) { |
||
| 1487 | continue; |
||
| 1488 | } |
||
| 1489 | } else { |
||
| 1490 | $default = $val; |
||
| 1491 | } |
||
| 1492 | |||
| 1493 | $item[$key] = $this->filterData($data[$key] ?? $default, $filter, $key, $default); |
||
| 1494 | } |
||
| 1495 | |||
| 1496 | return $item; |
||
| 1497 | } |
||
| 1498 | |||
| 1499 | /** |
||
| 1500 | * 排除指定参数获取 |
||
| 1501 | * @access public |
||
| 1502 | * @param array $name 变量名 |
||
| 1503 | * @param string $type 变量类型 |
||
| 1504 | * @return mixed |
||
| 1505 | */ |
||
| 1506 | public function except(array $name, string $type = 'param'): array |
||
| 1507 | { |
||
| 1508 | $param = $this->$type(); |
||
| 1509 | |||
| 1510 | foreach ($name as $key) { |
||
| 1511 | if (isset($param[$key])) { |
||
| 1512 | unset($param[$key]); |
||
| 1513 | } |
||
| 1514 | } |
||
| 1515 | |||
| 1516 | return $param; |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * 当前是否ssl |
||
| 1521 | * @access public |
||
| 1522 | * @return bool |
||
| 1523 | */ |
||
| 1524 | public function isSsl(): bool |
||
| 1525 | { |
||
| 1526 | if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) { |
||
| 1527 | return true; |
||
| 1528 | } elseif ('https' == $this->server('REQUEST_SCHEME')) { |
||
| 1529 | return true; |
||
| 1530 | } elseif ('443' == $this->server('SERVER_PORT')) { |
||
| 1531 | return true; |
||
| 1532 | } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) { |
||
| 1533 | return true; |
||
| 1534 | } elseif ($this->httpsAgentName && $this->server($this->httpsAgentName)) { |
||
| 1535 | return true; |
||
| 1536 | } |
||
| 1537 | |||
| 1538 | return false; |
||
| 1539 | } |
||
| 1540 | |||
| 1541 | /** |
||
| 1542 | * 当前是否JSON请求 |
||
| 1543 | * @access public |
||
| 1544 | * @return bool |
||
| 1545 | */ |
||
| 1546 | 9 | public function isJson(): bool |
|
| 1547 | { |
||
| 1548 | 9 | $contentType = $this->contentType(); |
|
| 1549 | 9 | $acceptType = $this->type(); |
|
| 1550 | |||
| 1551 | 9 | return false !== strpos($contentType, 'json') || false !== strpos($acceptType, 'json'); |
|
| 1552 | } |
||
| 1553 | |||
| 1554 | /** |
||
| 1555 | * 当前是否Ajax请求 |
||
| 1556 | * @access public |
||
| 1557 | * @param bool $ajax true 获取原始ajax请求 |
||
| 1558 | * @return bool |
||
| 1559 | */ |
||
| 1560 | public function isAjax(bool $ajax = false): bool |
||
| 1561 | { |
||
| 1562 | $value = $this->server('HTTP_X_REQUESTED_WITH'); |
||
| 1563 | $result = $value && 'xmlhttprequest' == strtolower($value) ? true : false; |
||
| 1564 | |||
| 1565 | if (true === $ajax) { |
||
| 1566 | return $result; |
||
| 1567 | } |
||
| 1568 | |||
| 1569 | return $this->param($this->varAjax) ? true : $result; |
||
| 1570 | } |
||
| 1571 | |||
| 1572 | /** |
||
| 1573 | * 当前是否Pjax请求 |
||
| 1574 | * @access public |
||
| 1575 | * @param bool $pjax true 获取原始pjax请求 |
||
| 1576 | * @return bool |
||
| 1577 | */ |
||
| 1578 | public function isPjax(bool $pjax = false): bool |
||
| 1579 | { |
||
| 1580 | $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false; |
||
| 1581 | |||
| 1582 | if (true === $pjax) { |
||
| 1583 | return $result; |
||
| 1584 | } |
||
| 1585 | |||
| 1586 | return $this->param($this->varPjax) ? true : $result; |
||
| 1587 | } |
||
| 1588 | |||
| 1589 | /** |
||
| 1590 | * 获取客户端IP地址 |
||
| 1591 | * @access public |
||
| 1592 | * @return string |
||
| 1593 | */ |
||
| 1594 | public function ip(): string |
||
| 1595 | { |
||
| 1596 | if (!empty($this->realIP)) { |
||
| 1597 | return $this->realIP; |
||
| 1598 | } |
||
| 1599 | |||
| 1600 | $this->realIP = $this->server('REMOTE_ADDR', ''); |
||
| 1601 | |||
| 1602 | // 如果指定了前端代理服务器IP以及其会发送的IP头 |
||
| 1603 | // 则尝试获取前端代理服务器发送过来的真实IP |
||
| 1604 | $proxyIp = $this->proxyServerIp; |
||
| 1605 | $proxyIpHeader = $this->proxyServerIpHeader; |
||
| 1606 | |||
| 1607 | if (count($proxyIp) > 0 && count($proxyIpHeader) > 0) { |
||
| 1608 | // 从指定的HTTP头中依次尝试获取IP地址 |
||
| 1609 | // 直到获取到一个合法的IP地址 |
||
| 1610 | foreach ($proxyIpHeader as $header) { |
||
| 1611 | $tempIP = $this->server($header); |
||
| 1612 | |||
| 1613 | if (empty($tempIP)) { |
||
| 1614 | continue; |
||
| 1615 | } |
||
| 1616 | |||
| 1617 | $tempIP = trim(explode(',', $tempIP)[0]); |
||
| 1618 | |||
| 1619 | if (!$this->isValidIP($tempIP)) { |
||
| 1620 | $tempIP = null; |
||
| 1621 | } else { |
||
| 1622 | break; |
||
| 1623 | } |
||
| 1624 | } |
||
| 1625 | |||
| 1626 | // tempIP不为空,说明获取到了一个IP地址 |
||
| 1627 | // 这时我们检查 REMOTE_ADDR 是不是指定的前端代理服务器之一 |
||
| 1628 | // 如果是的话说明该 IP头 是由前端代理服务器设置的 |
||
| 1629 | // 否则则是伪装的 |
||
| 1630 | if ($tempIP) { |
||
| 1631 | $realIPBin = $this->ip2bin($this->realIP); |
||
| 1632 | |||
| 1633 | foreach ($proxyIp as $ip) { |
||
| 1634 | $serverIPElements = explode('/', $ip); |
||
| 1635 | $serverIP = $serverIPElements[0]; |
||
| 1636 | $serverIPPrefix = $serverIPElements[1] ?? 128; |
||
| 1637 | $serverIPBin = $this->ip2bin($serverIP); |
||
| 1638 | |||
| 1639 | // IP类型不符 |
||
| 1640 | if (strlen($realIPBin) !== strlen($serverIPBin)) { |
||
| 1641 | continue; |
||
| 1642 | } |
||
| 1643 | |||
| 1644 | if (strncmp($realIPBin, $serverIPBin, (int) $serverIPPrefix) === 0) { |
||
| 1645 | $this->realIP = $tempIP; |
||
| 1646 | break; |
||
| 1647 | } |
||
| 1648 | } |
||
| 1649 | } |
||
| 1650 | } |
||
| 1651 | |||
| 1652 | if (!$this->isValidIP($this->realIP)) { |
||
| 1653 | $this->realIP = '0.0.0.0'; |
||
| 1654 | } |
||
| 1655 | |||
| 1656 | return $this->realIP; |
||
| 1657 | } |
||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * 检测是否是合法的IP地址 |
||
| 1661 | * |
||
| 1662 | * @param string $ip IP地址 |
||
| 1663 | * @param string $type IP地址类型 (ipv4, ipv6) |
||
| 1664 | * |
||
| 1665 | * @return boolean |
||
| 1666 | */ |
||
| 1667 | public function isValidIP(string $ip, string $type = ''): bool |
||
| 1668 | { |
||
| 1669 | switch (strtolower($type)) { |
||
| 1670 | case 'ipv4': |
||
| 1671 | $flag = FILTER_FLAG_IPV4; |
||
| 1672 | break; |
||
| 1673 | case 'ipv6': |
||
| 1674 | $flag = FILTER_FLAG_IPV6; |
||
| 1675 | break; |
||
| 1676 | default: |
||
| 1677 | $flag = null; |
||
| 1678 | break; |
||
| 1679 | } |
||
| 1680 | |||
| 1681 | return boolval(filter_var($ip, FILTER_VALIDATE_IP, $flag)); |
||
| 1682 | } |
||
| 1683 | |||
| 1684 | /** |
||
| 1685 | * 将IP地址转换为二进制字符串 |
||
| 1686 | * |
||
| 1687 | * @param string $ip |
||
| 1688 | * |
||
| 1689 | * @return string |
||
| 1690 | */ |
||
| 1691 | public function ip2bin(string $ip): string |
||
| 1692 | { |
||
| 1693 | if ($this->isValidIP($ip, 'ipv6')) { |
||
| 1694 | $IPHex = str_split(bin2hex(inet_pton($ip)), 4); |
||
| 1695 | foreach ($IPHex as $key => $value) { |
||
| 1696 | $IPHex[$key] = intval($value, 16); |
||
| 1697 | } |
||
| 1698 | $IPBin = vsprintf('%016b%016b%016b%016b%016b%016b%016b%016b', $IPHex); |
||
| 1699 | } else { |
||
| 1700 | $IPHex = str_split(bin2hex(inet_pton($ip)), 2); |
||
| 1701 | foreach ($IPHex as $key => $value) { |
||
| 1702 | $IPHex[$key] = intval($value, 16); |
||
| 1703 | } |
||
| 1704 | $IPBin = vsprintf('%08b%08b%08b%08b', $IPHex); |
||
| 1705 | } |
||
| 1706 | |||
| 1707 | return $IPBin; |
||
| 1708 | } |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * 检测是否使用手机访问 |
||
| 1712 | * @access public |
||
| 1713 | * @return bool |
||
| 1714 | */ |
||
| 1715 | public function isMobile(): bool |
||
| 1716 | { |
||
| 1717 | if ($this->server('HTTP_VIA') && stristr($this->server('HTTP_VIA'), "wap")) { |
||
| 1718 | return true; |
||
| 1719 | } elseif ($this->server('HTTP_ACCEPT') && strpos(strtoupper($this->server('HTTP_ACCEPT')), "VND.WAP.WML")) { |
||
| 1720 | return true; |
||
| 1721 | } elseif ($this->server('HTTP_X_WAP_PROFILE') || $this->server('HTTP_PROFILE')) { |
||
| 1722 | return true; |
||
| 1723 | } 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'))) { |
||
| 1724 | return true; |
||
| 1725 | } |
||
| 1726 | |||
| 1727 | return false; |
||
| 1728 | } |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * 当前URL地址中的scheme参数 |
||
| 1732 | * @access public |
||
| 1733 | * @return string |
||
| 1734 | */ |
||
| 1735 | public function scheme(): string |
||
| 1736 | { |
||
| 1737 | return $this->isSsl() ? 'https' : 'http'; |
||
| 1738 | } |
||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * 当前请求URL地址中的query参数 |
||
| 1742 | * @access public |
||
| 1743 | * @return string |
||
| 1744 | */ |
||
| 1745 | public function query(): string |
||
| 1746 | { |
||
| 1747 | return $this->server('QUERY_STRING', ''); |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | /** |
||
| 1751 | * 设置当前请求的host(包含端口) |
||
| 1752 | * @access public |
||
| 1753 | * @param string $host 主机名(含端口) |
||
| 1754 | * @return $this |
||
| 1755 | */ |
||
| 1756 | public function setHost(string $host) |
||
| 1757 | { |
||
| 1758 | $this->host = $host; |
||
| 1759 | |||
| 1760 | return $this; |
||
| 1761 | } |
||
| 1762 | |||
| 1763 | /** |
||
| 1764 | * 当前请求的host |
||
| 1765 | * @access public |
||
| 1766 | * @param bool $strict true 仅仅获取HOST |
||
| 1767 | * @return string |
||
| 1768 | */ |
||
| 1769 | 3 | public function host(bool $strict = false): string |
|
| 1770 | { |
||
| 1771 | 3 | if ($this->host) { |
|
| 1772 | $host = $this->host; |
||
| 1773 | } else { |
||
| 1774 | 3 | $host = strval($this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST')); |
|
| 1775 | } |
||
| 1776 | |||
| 1777 | 3 | return true === $strict && strpos($host, ':') ? strstr($host, ':', true) : $host; |
|
| 1778 | } |
||
| 1779 | |||
| 1780 | /** |
||
| 1781 | * 当前请求URL地址中的port参数 |
||
| 1782 | * @access public |
||
| 1783 | * @return string |
||
| 1784 | */ |
||
| 1785 | public function port(): string |
||
| 1786 | { |
||
| 1787 | return $this->server('SERVER_PORT', ''); |
||
| 1788 | } |
||
| 1789 | |||
| 1790 | /** |
||
| 1791 | * 当前请求 SERVER_PROTOCOL |
||
| 1792 | * @access public |
||
| 1793 | * @return string |
||
| 1794 | */ |
||
| 1795 | public function protocol(): string |
||
| 1796 | { |
||
| 1797 | return $this->server('SERVER_PROTOCOL', ''); |
||
| 1798 | } |
||
| 1799 | |||
| 1800 | /** |
||
| 1801 | * 当前请求 REMOTE_PORT |
||
| 1802 | * @access public |
||
| 1803 | * @return string |
||
| 1804 | */ |
||
| 1805 | public function remotePort(): string |
||
| 1806 | { |
||
| 1807 | return $this->server('REMOTE_PORT', ''); |
||
| 1808 | } |
||
| 1809 | |||
| 1810 | /** |
||
| 1811 | * 当前请求 HTTP_CONTENT_TYPE |
||
| 1812 | * @access public |
||
| 1813 | * @return string |
||
| 1814 | */ |
||
| 1815 | 9 | public function contentType(): string |
|
| 1816 | { |
||
| 1817 | 9 | $contentType = $this->server('CONTENT_TYPE'); |
|
| 1818 | |||
| 1819 | 9 | if ($contentType) { |
|
| 1820 | if (strpos($contentType, ';')) { |
||
| 1821 | list($type) = explode(';', $contentType); |
||
| 1822 | } else { |
||
| 1823 | $type = $contentType; |
||
| 1824 | } |
||
| 1825 | return trim($type); |
||
| 1826 | } |
||
| 1827 | |||
| 1828 | 9 | return ''; |
|
| 1829 | } |
||
| 1830 | |||
| 1831 | /** |
||
| 1832 | * 获取当前请求的安全Key |
||
| 1833 | * @access public |
||
| 1834 | * @return string |
||
| 1835 | */ |
||
| 1836 | public function secureKey(): string |
||
| 1837 | { |
||
| 1838 | if (is_null($this->secureKey)) { |
||
| 1839 | $this->secureKey = uniqid('', true); |
||
| 1840 | } |
||
| 1841 | |||
| 1842 | return $this->secureKey; |
||
| 1843 | } |
||
| 1844 | |||
| 1845 | /** |
||
| 1846 | * 设置当前的应用名 |
||
| 1847 | * @access public |
||
| 1848 | * @param string $app 应用名 |
||
| 1849 | * @return $this |
||
| 1850 | */ |
||
| 1851 | 5 | public function setApp(string $app) |
|
| 1852 | { |
||
| 1853 | 5 | $this->app = $app; |
|
| 1854 | 5 | return $this; |
|
| 1855 | } |
||
| 1856 | |||
| 1857 | /** |
||
| 1858 | * 设置当前的控制器名 |
||
| 1859 | * @access public |
||
| 1860 | * @param string $controller 控制器名 |
||
| 1861 | * @return $this |
||
| 1862 | */ |
||
| 1863 | public function setController(string $controller) |
||
| 1864 | { |
||
| 1865 | $this->controller = $controller; |
||
| 1866 | return $this; |
||
| 1867 | } |
||
| 1868 | |||
| 1869 | /** |
||
| 1870 | * 设置当前的操作名 |
||
| 1871 | * @access public |
||
| 1872 | * @param string $action 操作名 |
||
| 1873 | * @return $this |
||
| 1874 | */ |
||
| 1875 | public function setAction(string $action) |
||
| 1876 | { |
||
| 1877 | $this->action = $action; |
||
| 1878 | return $this; |
||
| 1879 | } |
||
| 1880 | |||
| 1881 | /** |
||
| 1882 | * 获取当前的应用名 |
||
| 1883 | * @access public |
||
| 1884 | * @return string |
||
| 1885 | */ |
||
| 1886 | public function app(): string |
||
| 1887 | { |
||
| 1888 | return $this->app ?: ''; |
||
| 1889 | } |
||
| 1890 | |||
| 1891 | /** |
||
| 1892 | * 获取当前的控制器名 |
||
| 1893 | * @access public |
||
| 1894 | * @param bool $convert 转换为小写 |
||
| 1895 | * @return string |
||
| 1896 | */ |
||
| 1897 | public function controller(bool $convert = false): string |
||
| 1898 | { |
||
| 1899 | $name = $this->controller ?: ''; |
||
| 1900 | return $convert ? strtolower($name) : $name; |
||
| 1901 | } |
||
| 1902 | |||
| 1903 | /** |
||
| 1904 | * 获取当前的操作名 |
||
| 1905 | * @access public |
||
| 1906 | * @param bool $convert 转换为小写 |
||
| 1907 | * @return string |
||
| 1908 | */ |
||
| 1909 | public function action(bool $convert = false): string |
||
| 1910 | { |
||
| 1911 | $name = $this->action ?: ''; |
||
| 1912 | return $convert ? strtolower($name) : $name; |
||
| 1913 | } |
||
| 1914 | |||
| 1915 | /** |
||
| 1916 | * 设置或者获取当前请求的content |
||
| 1917 | * @access public |
||
| 1918 | * @return string |
||
| 1919 | */ |
||
| 1920 | public function getContent(): string |
||
| 1927 | } |
||
| 1928 | |||
| 1929 | /** |
||
| 1930 | * 获取当前请求的php://input |
||
| 1931 | * @access public |
||
| 1932 | * @return string |
||
| 1933 | */ |
||
| 1934 | public function getInput(): string |
||
| 1935 | { |
||
| 1936 | return $this->input; |
||
| 1937 | } |
||
| 1938 | |||
| 1939 | /** |
||
| 1940 | * 生成请求令牌 |
||
| 1941 | * @access public |
||
| 1942 | * @param string $name 令牌名称 |
||
| 1943 | * @param mixed $type 令牌生成方法 |
||
| 1944 | * @return string |
||
| 1945 | */ |
||
| 1946 | public function buildToken(string $name = '__token__', $type = 'md5'): string |
||
| 1947 | { |
||
| 1948 | $type = is_callable($type) ? $type : 'md5'; |
||
| 1949 | $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT')); |
||
| 1950 | |||
| 1951 | $this->session->set($name, $token); |
||
| 1952 | |||
| 1953 | return $token; |
||
| 1954 | } |
||
| 1955 | |||
| 1956 | /** |
||
| 1957 | * 检查请求令牌 |
||
| 1958 | * @access public |
||
| 1959 | * @param string $name 令牌名称 |
||
| 1960 | * @param array $data 表单数据 |
||
| 1961 | * @return bool |
||
| 1962 | */ |
||
| 1963 | public function checkToken(string $token = '__token__', array $data = []): bool |
||
| 1964 | { |
||
| 1965 | if (in_array($this->method(), ['GET', 'HEAD', 'OPTIONS'], true)) { |
||
| 1966 | return true; |
||
| 1967 | } |
||
| 1968 | |||
| 1969 | if (!$this->session->has($token)) { |
||
| 1970 | // 令牌数据无效 |
||
| 1971 | return false; |
||
| 1972 | } |
||
| 1973 | |||
| 1974 | // Header验证 |
||
| 1975 | if ($this->header('X-CSRF-TOKEN') && $this->session->get($token) === $this->header('X-CSRF-TOKEN')) { |
||
| 1976 | // 防止重复提交 |
||
| 1977 | $this->session->delete($token); // 验证完成销毁session |
||
| 1978 | return true; |
||
| 1979 | } |
||
| 1980 | |||
| 1981 | if (empty($data)) { |
||
| 1982 | $data = $this->post(); |
||
| 1983 | } |
||
| 1984 | |||
| 1985 | // 令牌验证 |
||
| 1986 | if (isset($data[$token]) && $this->session->get($token) === $data[$token]) { |
||
| 1987 | // 防止重复提交 |
||
| 1988 | $this->session->delete($token); // 验证完成销毁session |
||
| 1989 | return true; |
||
| 1990 | } |
||
| 1991 | |||
| 1992 | // 开启TOKEN重置 |
||
| 1993 | $this->session->delete($token); |
||
| 1994 | return false; |
||
| 1995 | } |
||
| 1996 | |||
| 1997 | /** |
||
| 1998 | * 设置在中间件传递的数据 |
||
| 1999 | * @access public |
||
| 2000 | * @param array $middleware 数据 |
||
| 2001 | * @return $this |
||
| 2002 | */ |
||
| 2003 | public function withMiddleware(array $middleware) |
||
| 2004 | { |
||
| 2005 | $this->middleware = array_merge($this->middleware, $middleware); |
||
| 2006 | return $this; |
||
| 2007 | } |
||
| 2008 | |||
| 2009 | /** |
||
| 2010 | * 设置GET数据 |
||
| 2011 | * @access public |
||
| 2012 | * @param array $get 数据 |
||
| 2013 | * @return $this |
||
| 2014 | */ |
||
| 2015 | public function withGet(array $get) |
||
| 2016 | { |
||
| 2017 | $this->get = $get; |
||
| 2018 | return $this; |
||
| 2019 | } |
||
| 2020 | |||
| 2021 | /** |
||
| 2022 | * 设置POST数据 |
||
| 2023 | * @access public |
||
| 2024 | * @param array $post 数据 |
||
| 2025 | * @return $this |
||
| 2026 | */ |
||
| 2027 | public function withPost(array $post) |
||
| 2031 | } |
||
| 2032 | |||
| 2033 | /** |
||
| 2034 | * 设置COOKIE数据 |
||
| 2035 | * @access public |
||
| 2036 | * @param array $cookie 数据 |
||
| 2037 | * @return $this |
||
| 2038 | */ |
||
| 2039 | public function withCookie(array $cookie) |
||
| 2040 | { |
||
| 2041 | $this->cookie = $cookie; |
||
| 2042 | return $this; |
||
| 2043 | } |
||
| 2044 | |||
| 2045 | /** |
||
| 2046 | * 设置SESSION数据 |
||
| 2047 | * @access public |
||
| 2048 | * @param Session $session 数据 |
||
| 2049 | * @return $this |
||
| 2050 | */ |
||
| 2051 | public function withSession(Session $session) |
||
| 2052 | { |
||
| 2053 | $this->session = $session; |
||
| 2054 | return $this; |
||
| 2055 | } |
||
| 2056 | |||
| 2057 | /** |
||
| 2058 | * 设置SERVER数据 |
||
| 2059 | * @access public |
||
| 2060 | * @param array $server 数据 |
||
| 2061 | * @return $this |
||
| 2062 | */ |
||
| 2063 | public function withServer(array $server) |
||
| 2064 | { |
||
| 2065 | $this->server = array_change_key_case($server, CASE_UPPER); |
||
| 2066 | return $this; |
||
| 2067 | } |
||
| 2068 | |||
| 2069 | /** |
||
| 2070 | * 设置HEADER数据 |
||
| 2071 | * @access public |
||
| 2072 | * @param array $header 数据 |
||
| 2073 | * @return $this |
||
| 2074 | */ |
||
| 2075 | public function withHeader(array $header) |
||
| 2076 | { |
||
| 2077 | $this->header = array_change_key_case($header); |
||
| 2078 | return $this; |
||
| 2079 | } |
||
| 2080 | |||
| 2081 | /** |
||
| 2082 | * 设置ENV数据 |
||
| 2083 | * @access public |
||
| 2084 | * @param Env $env 数据 |
||
| 2085 | * @return $this |
||
| 2086 | */ |
||
| 2087 | public function withEnv(Env $env) |
||
| 2088 | { |
||
| 2089 | $this->env = $env; |
||
| 2090 | return $this; |
||
| 2091 | } |
||
| 2092 | |||
| 2093 | /** |
||
| 2094 | * 设置php://input数据 |
||
| 2095 | * @access public |
||
| 2096 | * @param string $input RAW数据 |
||
| 2097 | * @return $this |
||
| 2098 | */ |
||
| 2099 | public function withInput(string $input) |
||
| 2100 | { |
||
| 2101 | $this->input = $input; |
||
| 2102 | return $this; |
||
| 2103 | } |
||
| 2104 | |||
| 2105 | /** |
||
| 2106 | * 设置文件上传数据 |
||
| 2107 | * @access public |
||
| 2108 | * @param array $files 上传信息 |
||
| 2109 | * @return $this |
||
| 2110 | */ |
||
| 2111 | public function withFiles(array $files) |
||
| 2112 | { |
||
| 2113 | $this->file = $files; |
||
| 2114 | return $this; |
||
| 2115 | } |
||
| 2116 | |||
| 2117 | /** |
||
| 2118 | * 设置ROUTE变量 |
||
| 2119 | * @access public |
||
| 2120 | * @param array $route 数据 |
||
| 2121 | * @return $this |
||
| 2122 | */ |
||
| 2123 | public function withRoute(array $route) |
||
| 2124 | { |
||
| 2125 | $this->route = $route; |
||
| 2126 | return $this; |
||
| 2127 | } |
||
| 2128 | |||
| 2129 | /** |
||
| 2130 | * 设置中间传递数据 |
||
| 2131 | * @access public |
||
| 2132 | * @param string $name 参数名 |
||
| 2133 | * @param mixed $value 值 |
||
| 2134 | */ |
||
| 2135 | public function __set(string $name, $value) |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | /** |
||
| 2141 | * 获取中间传递数据的值 |
||
| 2142 | * @access public |
||
| 2143 | * @param string $name 名称 |
||
| 2144 | * @return mixed |
||
| 2145 | */ |
||
| 2146 | public function __get(string $name) |
||
| 2147 | { |
||
| 2148 | return $this->middleware($name); |
||
| 2149 | } |
||
| 2150 | |||
| 2151 | /** |
||
| 2152 | * 检测请求数据的值 |
||
| 2153 | * @access public |
||
| 2154 | * @param string $name 名称 |
||
| 2155 | * @return boolean |
||
| 2156 | */ |
||
| 2157 | public function __isset(string $name): bool |
||
| 2158 | { |
||
| 2159 | return isset($this->param[$name]); |
||
| 2160 | } |
||
| 2161 | } |
||
| 2162 |