Request.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. class Request
  13. {
  14. /**
  15. * @var object 对象实例
  16. */
  17. protected static $instance;
  18. protected $method;
  19. /**
  20. * @var string 域名(含协议和端口)
  21. */
  22. protected $domain;
  23. /**
  24. * @var string URL地址
  25. */
  26. protected $url;
  27. /**
  28. * @var string 基础URL
  29. */
  30. protected $baseUrl;
  31. /**
  32. * @var string 当前执行的文件
  33. */
  34. protected $baseFile;
  35. /**
  36. * @var string 访问的ROOT地址
  37. */
  38. protected $root;
  39. /**
  40. * @var string pathinfo
  41. */
  42. protected $pathinfo;
  43. /**
  44. * @var string pathinfo(不含后缀)
  45. */
  46. protected $path;
  47. /**
  48. * @var array 当前路由信息
  49. */
  50. protected $routeInfo = [];
  51. /**
  52. * @var array 环境变量
  53. */
  54. protected $env;
  55. /**
  56. * @var array 当前调度信息
  57. */
  58. protected $dispatch = [];
  59. protected $module;
  60. protected $controller;
  61. protected $action;
  62. // 当前语言集
  63. protected $langset;
  64. /**
  65. * @var array 请求参数
  66. */
  67. protected $param = [];
  68. protected $get = [];
  69. protected $post = [];
  70. protected $request = [];
  71. protected $route = [];
  72. protected $put;
  73. protected $session = [];
  74. protected $file = [];
  75. protected $cookie = [];
  76. protected $server = [];
  77. protected $header = [];
  78. /**
  79. * @var array 资源类型
  80. */
  81. protected $mimeType = [
  82. 'xml' => 'application/xml,text/xml,application/x-xml',
  83. 'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
  84. 'js' => 'text/javascript,application/javascript,application/x-javascript',
  85. 'css' => 'text/css',
  86. 'rss' => 'application/rss+xml',
  87. 'yaml' => 'application/x-yaml,text/yaml',
  88. 'atom' => 'application/atom+xml',
  89. 'pdf' => 'application/pdf',
  90. 'text' => 'text/plain',
  91. 'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*',
  92. 'csv' => 'text/csv',
  93. 'html' => 'text/html,application/xhtml+xml,*/*',
  94. ];
  95. protected $content;
  96. // 全局过滤规则
  97. protected $filter;
  98. // Hook扩展方法
  99. protected static $hook = [];
  100. // 绑定的属性
  101. protected $bind = [];
  102. // php://input
  103. protected $input;
  104. // 请求缓存
  105. protected $cache;
  106. // 缓存是否检查
  107. protected $isCheckCache;
  108. /**
  109. * 是否合并Param
  110. * @var bool
  111. */
  112. protected $mergeParam = false;
  113. /**
  114. * 构造函数
  115. * @access protected
  116. * @param array $options 参数
  117. */
  118. protected function __construct($options = [])
  119. {
  120. foreach ($options as $name => $item) {
  121. if (property_exists($this, $name)) {
  122. $this->$name = $item;
  123. }
  124. }
  125. if (is_null($this->filter)) {
  126. $this->filter = Config::get('default_filter');
  127. }
  128. // 保存 php://input
  129. $this->input = file_get_contents('php://input');
  130. }
  131. public function __call($method, $args)
  132. {
  133. if (array_key_exists($method, self::$hook)) {
  134. array_unshift($args, $this);
  135. return call_user_func_array(self::$hook[$method], $args);
  136. } else {
  137. throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
  138. }
  139. }
  140. /**
  141. * Hook 方法注入
  142. * @access public
  143. * @param string|array $method 方法名
  144. * @param mixed $callback callable
  145. * @return void
  146. */
  147. public static function hook($method, $callback = null)
  148. {
  149. if (is_array($method)) {
  150. self::$hook = array_merge(self::$hook, $method);
  151. } else {
  152. self::$hook[$method] = $callback;
  153. }
  154. }
  155. /**
  156. * 初始化
  157. * @access public
  158. * @param array $options 参数
  159. * @return \think\Request
  160. */
  161. public static function instance($options = [])
  162. {
  163. if (is_null(self::$instance)) {
  164. self::$instance = new static($options);
  165. }
  166. return self::$instance;
  167. }
  168. /**
  169. * 销毁当前请求对象
  170. * @access public
  171. * @return void
  172. */
  173. public static function destroy()
  174. {
  175. if (!is_null(self::$instance)) {
  176. self::$instance = null;
  177. }
  178. }
  179. /**
  180. * 创建一个URL请求
  181. * @access public
  182. * @param string $uri URL地址
  183. * @param string $method 请求类型
  184. * @param array $params 请求参数
  185. * @param array $cookie
  186. * @param array $files
  187. * @param array $server
  188. * @param string $content
  189. * @return \think\Request
  190. */
  191. public static function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
  192. {
  193. $server['PATH_INFO'] = '';
  194. $server['REQUEST_METHOD'] = strtoupper($method);
  195. $info = parse_url($uri);
  196. if (isset($info['host'])) {
  197. $server['SERVER_NAME'] = $info['host'];
  198. $server['HTTP_HOST'] = $info['host'];
  199. }
  200. if (isset($info['scheme'])) {
  201. if ('https' === $info['scheme']) {
  202. $server['HTTPS'] = 'on';
  203. $server['SERVER_PORT'] = 443;
  204. } else {
  205. unset($server['HTTPS']);
  206. $server['SERVER_PORT'] = 80;
  207. }
  208. }
  209. if (isset($info['port'])) {
  210. $server['SERVER_PORT'] = $info['port'];
  211. $server['HTTP_HOST'] = $server['HTTP_HOST'] . ':' . $info['port'];
  212. }
  213. if (isset($info['user'])) {
  214. $server['PHP_AUTH_USER'] = $info['user'];
  215. }
  216. if (isset($info['pass'])) {
  217. $server['PHP_AUTH_PW'] = $info['pass'];
  218. }
  219. if (!isset($info['path'])) {
  220. $info['path'] = '/';
  221. }
  222. $options = [];
  223. $options[strtolower($method)] = $params;
  224. $queryString = '';
  225. if (isset($info['query'])) {
  226. parse_str(html_entity_decode($info['query']), $query);
  227. if (!empty($params)) {
  228. $params = array_replace($query, $params);
  229. $queryString = http_build_query($params, '', '&');
  230. } else {
  231. $params = $query;
  232. $queryString = $info['query'];
  233. }
  234. } elseif (!empty($params)) {
  235. $queryString = http_build_query($params, '', '&');
  236. }
  237. if ($queryString) {
  238. parse_str($queryString, $get);
  239. $options['get'] = isset($options['get']) ? array_merge($get, $options['get']) : $get;
  240. }
  241. $server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
  242. $server['QUERY_STRING'] = $queryString;
  243. $options['cookie'] = $cookie;
  244. $options['param'] = $params;
  245. $options['file'] = $files;
  246. $options['server'] = $server;
  247. $options['url'] = $server['REQUEST_URI'];
  248. $options['baseUrl'] = $info['path'];
  249. $options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');
  250. $options['method'] = $server['REQUEST_METHOD'];
  251. $options['domain'] = isset($info['scheme']) ? $info['scheme'] . '://' . $server['HTTP_HOST'] : '';
  252. $options['content'] = $content;
  253. self::$instance = new self($options);
  254. return self::$instance;
  255. }
  256. /**
  257. * 设置或获取当前包含协议的域名
  258. * @access public
  259. * @param string $domain 域名
  260. * @return string
  261. */
  262. public function domain($domain = null)
  263. {
  264. if (!is_null($domain)) {
  265. $this->domain = $domain;
  266. return $this;
  267. } elseif (!$this->domain) {
  268. $this->domain = $this->scheme() . '://' . $this->host();
  269. }
  270. return $this->domain;
  271. }
  272. /**
  273. * 设置或获取当前完整URL 包括QUERY_STRING
  274. * @access public
  275. * @param string|true $url URL地址 true 带域名获取
  276. * @return string
  277. */
  278. public function url($url = null)
  279. {
  280. if (!is_null($url) && true !== $url) {
  281. $this->url = $url;
  282. return $this;
  283. } elseif (!$this->url) {
  284. if (IS_CLI) {
  285. $this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  286. } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
  287. $this->url = $_SERVER['HTTP_X_REWRITE_URL'];
  288. } elseif (isset($_SERVER['REQUEST_URI'])) {
  289. $this->url = $_SERVER['REQUEST_URI'];
  290. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
  291. $this->url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
  292. } else {
  293. $this->url = '';
  294. }
  295. }
  296. return true === $url ? $this->domain() . $this->url : $this->url;
  297. }
  298. /**
  299. * 设置或获取当前URL 不含QUERY_STRING
  300. * @access public
  301. * @param string $url URL地址
  302. * @return string
  303. */
  304. public function baseUrl($url = null)
  305. {
  306. if (!is_null($url) && true !== $url) {
  307. $this->baseUrl = $url;
  308. return $this;
  309. } elseif (!$this->baseUrl) {
  310. $str = $this->url();
  311. $this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str;
  312. }
  313. return true === $url ? $this->domain() . $this->baseUrl : $this->baseUrl;
  314. }
  315. /**
  316. * 设置或获取当前执行的文件 SCRIPT_NAME
  317. * @access public
  318. * @param string $file 当前执行的文件
  319. * @return string
  320. */
  321. public function baseFile($file = null)
  322. {
  323. if (!is_null($file) && true !== $file) {
  324. $this->baseFile = $file;
  325. return $this;
  326. } elseif (!$this->baseFile) {
  327. $url = '';
  328. if (!IS_CLI) {
  329. $script_name = basename($_SERVER['SCRIPT_FILENAME']);
  330. if (basename($_SERVER['SCRIPT_NAME']) === $script_name) {
  331. $url = $_SERVER['SCRIPT_NAME'];
  332. } elseif (basename($_SERVER['PHP_SELF']) === $script_name) {
  333. $url = $_SERVER['PHP_SELF'];
  334. } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $script_name) {
  335. $url = $_SERVER['ORIG_SCRIPT_NAME'];
  336. } elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $script_name)) !== false) {
  337. $url = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $script_name;
  338. } elseif (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) {
  339. $url = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']));
  340. }
  341. }
  342. $this->baseFile = $url;
  343. }
  344. return true === $file ? $this->domain() . $this->baseFile : $this->baseFile;
  345. }
  346. /**
  347. * 设置或获取URL访问根地址
  348. * @access public
  349. * @param string $url URL地址
  350. * @return string
  351. */
  352. public function root($url = null)
  353. {
  354. if (!is_null($url) && true !== $url) {
  355. $this->root = $url;
  356. return $this;
  357. } elseif (!$this->root) {
  358. $file = $this->baseFile();
  359. if ($file && 0 !== strpos($this->url(), $file)) {
  360. $file = str_replace('\\', '/', dirname($file));
  361. }
  362. $this->root = rtrim($file, '/');
  363. }
  364. return true === $url ? $this->domain() . $this->root : $this->root;
  365. }
  366. /**
  367. * 获取当前请求URL的pathinfo信息(含URL后缀)
  368. * @access public
  369. * @return string
  370. */
  371. public function pathinfo()
  372. {
  373. if (is_null($this->pathinfo)) {
  374. if (isset($_GET[Config::get('var_pathinfo')])) {
  375. // 判断URL里面是否有兼容模式参数
  376. $_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')];
  377. unset($_GET[Config::get('var_pathinfo')]);
  378. } elseif (IS_CLI) {
  379. // CLI模式下 index.php module/controller/action/params/...
  380. $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  381. }
  382. // 分析PATHINFO信息
  383. if (!isset($_SERVER['PATH_INFO'])) {
  384. foreach (Config::get('pathinfo_fetch') as $type) {
  385. if (!empty($_SERVER[$type])) {
  386. $_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME'])) ?
  387. substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
  388. break;
  389. }
  390. }
  391. }
  392. $this->pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : ltrim($_SERVER['PATH_INFO'], '/');
  393. }
  394. return $this->pathinfo;
  395. }
  396. /**
  397. * 获取当前请求URL的pathinfo信息(不含URL后缀)
  398. * @access public
  399. * @return string
  400. */
  401. public function path()
  402. {
  403. if (is_null($this->path)) {
  404. $suffix = Config::get('url_html_suffix');
  405. $pathinfo = $this->pathinfo();
  406. if (false === $suffix) {
  407. // 禁止伪静态访问
  408. $this->path = $pathinfo;
  409. } elseif ($suffix) {
  410. // 去除正常的URL后缀
  411. $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
  412. } else {
  413. // 允许任何后缀访问
  414. $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
  415. }
  416. }
  417. return $this->path;
  418. }
  419. /**
  420. * 当前URL的访问后缀
  421. * @access public
  422. * @return string
  423. */
  424. public function ext()
  425. {
  426. return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
  427. }
  428. /**
  429. * 获取当前请求的时间
  430. * @access public
  431. * @param bool $float 是否使用浮点类型
  432. * @return integer|float
  433. */
  434. public function time($float = false)
  435. {
  436. return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
  437. }
  438. /**
  439. * 当前请求的资源类型
  440. * @access public
  441. * @return false|string
  442. */
  443. public function type()
  444. {
  445. $accept = $this->server('HTTP_ACCEPT');
  446. if (empty($accept)) {
  447. return false;
  448. }
  449. foreach ($this->mimeType as $key => $val) {
  450. $array = explode(',', $val);
  451. foreach ($array as $k => $v) {
  452. if (stristr($accept, $v)) {
  453. return $key;
  454. }
  455. }
  456. }
  457. return false;
  458. }
  459. /**
  460. * 设置资源类型
  461. * @access public
  462. * @param string|array $type 资源类型名
  463. * @param string $val 资源类型
  464. * @return void
  465. */
  466. public function mimeType($type, $val = '')
  467. {
  468. if (is_array($type)) {
  469. $this->mimeType = array_merge($this->mimeType, $type);
  470. } else {
  471. $this->mimeType[$type] = $val;
  472. }
  473. }
  474. /**
  475. * 当前的请求类型
  476. * @access public
  477. * @param bool $method true 获取原始请求类型
  478. * @return string
  479. */
  480. public function method($method = false)
  481. {
  482. if (true === $method) {
  483. // 获取原始请求类型
  484. return $this->server('REQUEST_METHOD') ?: 'GET';
  485. } elseif (!$this->method) {
  486. if (isset($_POST[Config::get('var_method')])) {
  487. $method = strtoupper($_POST[Config::get('var_method')]);
  488. if (in_array($method, ['GET', 'POST', 'DELETE', 'PUT', 'PATCH'])) {
  489. $this->method = $method;
  490. $this->{$this->method}($_POST);
  491. } else {
  492. $this->method = 'POST';
  493. }
  494. unset($_POST[Config::get('var_method')]);
  495. } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
  496. $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
  497. } else {
  498. $this->method = $this->server('REQUEST_METHOD') ?: 'GET';
  499. }
  500. }
  501. return $this->method;
  502. }
  503. /**
  504. * 是否为GET请求
  505. * @access public
  506. * @return bool
  507. */
  508. public function isGet()
  509. {
  510. return $this->method() == 'GET';
  511. }
  512. /**
  513. * 是否为POST请求
  514. * @access public
  515. * @return bool
  516. */
  517. public function isPost()
  518. {
  519. return $this->method() == 'POST';
  520. }
  521. /**
  522. * 是否为PUT请求
  523. * @access public
  524. * @return bool
  525. */
  526. public function isPut()
  527. {
  528. return $this->method() == 'PUT';
  529. }
  530. /**
  531. * 是否为DELTE请求
  532. * @access public
  533. * @return bool
  534. */
  535. public function isDelete()
  536. {
  537. return $this->method() == 'DELETE';
  538. }
  539. /**
  540. * 是否为HEAD请求
  541. * @access public
  542. * @return bool
  543. */
  544. public function isHead()
  545. {
  546. return $this->method() == 'HEAD';
  547. }
  548. /**
  549. * 是否为PATCH请求
  550. * @access public
  551. * @return bool
  552. */
  553. public function isPatch()
  554. {
  555. return $this->method() == 'PATCH';
  556. }
  557. /**
  558. * 是否为OPTIONS请求
  559. * @access public
  560. * @return bool
  561. */
  562. public function isOptions()
  563. {
  564. return $this->method() == 'OPTIONS';
  565. }
  566. /**
  567. * 是否为cli
  568. * @access public
  569. * @return bool
  570. */
  571. public function isCli()
  572. {
  573. return PHP_SAPI == 'cli';
  574. }
  575. /**
  576. * 是否为cgi
  577. * @access public
  578. * @return bool
  579. */
  580. public function isCgi()
  581. {
  582. return strpos(PHP_SAPI, 'cgi') === 0;
  583. }
  584. /**
  585. * 获取当前请求的参数
  586. * @access public
  587. * @param string|array $name 变量名
  588. * @param mixed $default 默认值
  589. * @param string|array $filter 过滤方法
  590. * @return mixed
  591. */
  592. public function param($name = '', $default = null, $filter = '')
  593. {
  594. if (empty($this->mergeParam)) {
  595. $method = $this->method(true);
  596. // 自动获取请求变量
  597. switch ($method) {
  598. case 'POST':
  599. $vars = $this->post(false);
  600. break;
  601. case 'PUT':
  602. case 'DELETE':
  603. case 'PATCH':
  604. $vars = $this->put(false);
  605. break;
  606. default:
  607. $vars = [];
  608. }
  609. // 当前请求参数和URL地址中的参数合并
  610. $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false));
  611. $this->mergeParam = true;
  612. }
  613. if (true === $name) {
  614. // 获取包含文件上传信息的数组
  615. $file = $this->file();
  616. $data = is_array($file) ? array_merge($this->param, $file) : $this->param;
  617. return $this->input($data, '', $default, $filter);
  618. }
  619. return $this->input($this->param, $name, $default, $filter);
  620. }
  621. /**
  622. * 设置获取路由参数
  623. * @access public
  624. * @param string|array $name 变量名
  625. * @param mixed $default 默认值
  626. * @param string|array $filter 过滤方法
  627. * @return mixed
  628. */
  629. public function route($name = '', $default = null, $filter = '')
  630. {
  631. if (is_array($name)) {
  632. $this->param = [];
  633. $this->mergeParam = false;
  634. return $this->route = array_merge($this->route, $name);
  635. }
  636. return $this->input($this->route, $name, $default, $filter);
  637. }
  638. /**
  639. * 设置获取GET参数
  640. * @access public
  641. * @param string|array $name 变量名
  642. * @param mixed $default 默认值
  643. * @param string|array $filter 过滤方法
  644. * @return mixed
  645. */
  646. public function get($name = '', $default = null, $filter = '')
  647. {
  648. if (empty($this->get)) {
  649. $this->get = $_GET;
  650. }
  651. if (is_array($name)) {
  652. $this->param = [];
  653. $this->mergeParam = false;
  654. return $this->get = array_merge($this->get, $name);
  655. }
  656. return $this->input($this->get, $name, $default, $filter);
  657. }
  658. /**
  659. * 设置获取POST参数
  660. * @access public
  661. * @param string $name 变量名
  662. * @param mixed $default 默认值
  663. * @param string|array $filter 过滤方法
  664. * @return mixed
  665. */
  666. public function post($name = '', $default = null, $filter = '')
  667. {
  668. if (empty($this->post)) {
  669. $content = $this->input;
  670. if (empty($_POST) && false !== strpos($this->contentType(), 'application/json')) {
  671. $this->post = (array) json_decode($content, true);
  672. } else {
  673. $this->post = $_POST;
  674. }
  675. }
  676. if (is_array($name)) {
  677. $this->param = [];
  678. $this->mergeParam = false;
  679. return $this->post = array_merge($this->post, $name);
  680. }
  681. return $this->input($this->post, $name, $default, $filter);
  682. }
  683. /**
  684. * 设置获取PUT参数
  685. * @access public
  686. * @param string|array $name 变量名
  687. * @param mixed $default 默认值
  688. * @param string|array $filter 过滤方法
  689. * @return mixed
  690. */
  691. public function put($name = '', $default = null, $filter = '')
  692. {
  693. if (is_null($this->put)) {
  694. $content = $this->input;
  695. if (false !== strpos($this->contentType(), 'application/json')) {
  696. $this->put = (array) json_decode($content, true);
  697. } else {
  698. parse_str($content, $this->put);
  699. }
  700. }
  701. if (is_array($name)) {
  702. $this->param = [];
  703. $this->mergeParam = false;
  704. return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
  705. }
  706. return $this->input($this->put, $name, $default, $filter);
  707. }
  708. /**
  709. * 设置获取DELETE参数
  710. * @access public
  711. * @param string|array $name 变量名
  712. * @param mixed $default 默认值
  713. * @param string|array $filter 过滤方法
  714. * @return mixed
  715. */
  716. public function delete($name = '', $default = null, $filter = '')
  717. {
  718. return $this->put($name, $default, $filter);
  719. }
  720. /**
  721. * 设置获取PATCH参数
  722. * @access public
  723. * @param string|array $name 变量名
  724. * @param mixed $default 默认值
  725. * @param string|array $filter 过滤方法
  726. * @return mixed
  727. */
  728. public function patch($name = '', $default = null, $filter = '')
  729. {
  730. return $this->put($name, $default, $filter);
  731. }
  732. /**
  733. * 获取request变量
  734. * @param string $name 数据名称
  735. * @param string $default 默认值
  736. * @param string|array $filter 过滤方法
  737. * @return mixed
  738. */
  739. public function request($name = '', $default = null, $filter = '')
  740. {
  741. if (empty($this->request)) {
  742. $this->request = $_REQUEST;
  743. }
  744. if (is_array($name)) {
  745. $this->param = [];
  746. $this->mergeParam = false;
  747. return $this->request = array_merge($this->request, $name);
  748. }
  749. return $this->input($this->request, $name, $default, $filter);
  750. }
  751. /**
  752. * 获取session数据
  753. * @access public
  754. * @param string|array $name 数据名称
  755. * @param string $default 默认值
  756. * @param string|array $filter 过滤方法
  757. * @return mixed
  758. */
  759. public function session($name = '', $default = null, $filter = '')
  760. {
  761. if (empty($this->session)) {
  762. $this->session = Session::get();
  763. }
  764. if (is_array($name)) {
  765. return $this->session = array_merge($this->session, $name);
  766. }
  767. return $this->input($this->session, $name, $default, $filter);
  768. }
  769. /**
  770. * 获取cookie参数
  771. * @access public
  772. * @param string|array $name 数据名称
  773. * @param string $default 默认值
  774. * @param string|array $filter 过滤方法
  775. * @return mixed
  776. */
  777. public function cookie($name = '', $default = null, $filter = '')
  778. {
  779. if (empty($this->cookie)) {
  780. $this->cookie = Cookie::get();
  781. }
  782. if (is_array($name)) {
  783. return $this->cookie = array_merge($this->cookie, $name);
  784. } elseif (!empty($name)) {
  785. $data = Cookie::has($name) ? Cookie::get($name) : $default;
  786. } else {
  787. $data = $this->cookie;
  788. }
  789. // 解析过滤器
  790. $filter = $this->getFilter($filter, $default);
  791. if (is_array($data)) {
  792. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  793. reset($data);
  794. } else {
  795. $this->filterValue($data, $name, $filter);
  796. }
  797. return $data;
  798. }
  799. /**
  800. * 获取server参数
  801. * @access public
  802. * @param string|array $name 数据名称
  803. * @param string $default 默认值
  804. * @param string|array $filter 过滤方法
  805. * @return mixed
  806. */
  807. public function server($name = '', $default = null, $filter = '')
  808. {
  809. if (empty($this->server)) {
  810. $this->server = $_SERVER;
  811. }
  812. if (is_array($name)) {
  813. return $this->server = array_merge($this->server, $name);
  814. }
  815. return $this->input($this->server, false === $name ? false : strtoupper($name), $default, $filter);
  816. }
  817. /**
  818. * 获取上传的文件信息
  819. * @access public
  820. * @param string|array $name 名称
  821. * @return null|array|\think\File
  822. */
  823. public function file($name = '')
  824. {
  825. if (empty($this->file)) {
  826. $this->file = isset($_FILES) ? $_FILES : [];
  827. }
  828. if (is_array($name)) {
  829. return $this->file = array_merge($this->file, $name);
  830. }
  831. $files = $this->file;
  832. if (!empty($files)) {
  833. // 处理上传文件
  834. $array = [];
  835. foreach ($files as $key => $file) {
  836. if (is_array($file['name'])) {
  837. $item = [];
  838. $keys = array_keys($file);
  839. $count = count($file['name']);
  840. for ($i = 0; $i < $count; $i++) {
  841. if (empty($file['tmp_name'][$i]) || !is_file($file['tmp_name'][$i])) {
  842. continue;
  843. }
  844. $temp['key'] = $key;
  845. foreach ($keys as $_key) {
  846. $temp[$_key] = $file[$_key][$i];
  847. }
  848. $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
  849. }
  850. $array[$key] = $item;
  851. } else {
  852. if ($file instanceof File) {
  853. $array[$key] = $file;
  854. } else {
  855. if (empty($file['tmp_name']) || !is_file($file['tmp_name'])) {
  856. continue;
  857. }
  858. $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
  859. }
  860. }
  861. }
  862. if (strpos($name, '.')) {
  863. list($name, $sub) = explode('.', $name);
  864. }
  865. if ('' === $name) {
  866. // 获取全部文件
  867. return $array;
  868. } elseif (isset($sub) && isset($array[$name][$sub])) {
  869. return $array[$name][$sub];
  870. } elseif (isset($array[$name])) {
  871. return $array[$name];
  872. }
  873. }
  874. return;
  875. }
  876. /**
  877. * 获取环境变量
  878. * @param string|array $name 数据名称
  879. * @param string $default 默认值
  880. * @param string|array $filter 过滤方法
  881. * @return mixed
  882. */
  883. public function env($name = '', $default = null, $filter = '')
  884. {
  885. if (empty($this->env)) {
  886. $this->env = $_ENV;
  887. }
  888. if (is_array($name)) {
  889. return $this->env = array_merge($this->env, $name);
  890. }
  891. return $this->input($this->env, false === $name ? false : strtoupper($name), $default, $filter);
  892. }
  893. /**
  894. * 设置或者获取当前的Header
  895. * @access public
  896. * @param string|array $name header名称
  897. * @param string $default 默认值
  898. * @return string
  899. */
  900. public function header($name = '', $default = null)
  901. {
  902. if (empty($this->header)) {
  903. $header = [];
  904. if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
  905. $header = $result;
  906. } else {
  907. $server = $this->server ?: $_SERVER;
  908. foreach ($server as $key => $val) {
  909. if (0 === strpos($key, 'HTTP_')) {
  910. $key = str_replace('_', '-', strtolower(substr($key, 5)));
  911. $header[$key] = $val;
  912. }
  913. }
  914. if (isset($server['CONTENT_TYPE'])) {
  915. $header['content-type'] = $server['CONTENT_TYPE'];
  916. }
  917. if (isset($server['CONTENT_LENGTH'])) {
  918. $header['content-length'] = $server['CONTENT_LENGTH'];
  919. }
  920. }
  921. $this->header = array_change_key_case($header);
  922. }
  923. if (is_array($name)) {
  924. return $this->header = array_merge($this->header, $name);
  925. }
  926. if ('' === $name) {
  927. return $this->header;
  928. }
  929. $name = str_replace('_', '-', strtolower($name));
  930. return isset($this->header[$name]) ? $this->header[$name] : $default;
  931. }
  932. /**
  933. * 获取变量 支持过滤和默认值
  934. * @param array $data 数据源
  935. * @param string|false $name 字段名
  936. * @param mixed $default 默认值
  937. * @param string|array $filter 过滤函数
  938. * @return mixed
  939. */
  940. public function input($data = [], $name = '', $default = null, $filter = '')
  941. {
  942. if (false === $name) {
  943. // 获取原始数据
  944. return $data;
  945. }
  946. $name = (string) $name;
  947. if ('' != $name) {
  948. // 解析name
  949. if (strpos($name, '/')) {
  950. list($name, $type) = explode('/', $name);
  951. } else {
  952. $type = 's';
  953. }
  954. // 按.拆分成多维数组进行判断
  955. foreach (explode('.', $name) as $val) {
  956. if (isset($data[$val])) {
  957. $data = $data[$val];
  958. } else {
  959. // 无输入数据,返回默认值
  960. return $default;
  961. }
  962. }
  963. if (is_object($data)) {
  964. return $data;
  965. }
  966. }
  967. // 解析过滤器
  968. $filter = $this->getFilter($filter, $default);
  969. if (is_array($data)) {
  970. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  971. reset($data);
  972. } else {
  973. $this->filterValue($data, $name, $filter);
  974. }
  975. if (isset($type) && $data !== $default) {
  976. // 强制类型转换
  977. $this->typeCast($data, $type);
  978. }
  979. return $data;
  980. }
  981. /**
  982. * 设置或获取当前的过滤规则
  983. * @param mixed $filter 过滤规则
  984. * @return mixed
  985. */
  986. public function filter($filter = null)
  987. {
  988. if (is_null($filter)) {
  989. return $this->filter;
  990. } else {
  991. $this->filter = $filter;
  992. }
  993. }
  994. protected function getFilter($filter, $default)
  995. {
  996. if (is_null($filter)) {
  997. $filter = [];
  998. } else {
  999. $filter = $filter ?: $this->filter;
  1000. if (is_string($filter) && false === strpos($filter, '/')) {
  1001. $filter = explode(',', $filter);
  1002. } else {
  1003. $filter = (array) $filter;
  1004. }
  1005. }
  1006. $filter[] = $default;
  1007. return $filter;
  1008. }
  1009. /**
  1010. * 递归过滤给定的值
  1011. * @param mixed $value 键值
  1012. * @param mixed $key 键名
  1013. * @param array $filters 过滤方法+默认值
  1014. * @return mixed
  1015. */
  1016. private function filterValue(&$value, $key, $filters)
  1017. {
  1018. $default = array_pop($filters);
  1019. foreach ($filters as $filter) {
  1020. if (is_callable($filter)) {
  1021. // 调用函数或者方法过滤
  1022. $value = call_user_func($filter, $value ?? '');
  1023. } elseif (is_scalar($value)) {
  1024. if (false !== strpos($filter, '/')) {
  1025. // 正则过滤
  1026. if (!preg_match($filter, $value)) {
  1027. // 匹配不成功返回默认值
  1028. $value = $default;
  1029. break;
  1030. }
  1031. } elseif (!empty($filter)) {
  1032. // filter函数不存在时, 则使用filter_var进行过滤
  1033. // filter为非整形值时, 调用filter_id取得过滤id
  1034. $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
  1035. if (false === $value) {
  1036. $value = $default;
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. }
  1042. return $this->filterExp($value);
  1043. }
  1044. /**
  1045. * 过滤表单中的表达式
  1046. * @param string $value
  1047. * @return void
  1048. */
  1049. public function filterExp(&$value)
  1050. {
  1051. // 过滤查询特殊字符
  1052. if (is_string($value) && preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT LIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOT EXISTS|NOTEXISTS|EXISTS|NOT NULL|NOTNULL|NULL|BETWEEN TIME|NOT BETWEEN TIME|NOTBETWEEN TIME|NOTIN|NOT IN|IN)$/i', $value)) {
  1053. $value .= ' ';
  1054. }
  1055. // TODO 其他安全过滤
  1056. }
  1057. /**
  1058. * 强制类型转换
  1059. * @param string $data
  1060. * @param string $type
  1061. * @return mixed
  1062. */
  1063. private function typeCast(&$data, $type)
  1064. {
  1065. switch (strtolower($type)) {
  1066. // 数组
  1067. case 'a':
  1068. $data = (array) $data;
  1069. break;
  1070. // 数字
  1071. case 'd':
  1072. $data = (int) $data;
  1073. break;
  1074. // 浮点
  1075. case 'f':
  1076. $data = (float) $data;
  1077. break;
  1078. // 布尔
  1079. case 'b':
  1080. $data = (boolean) $data;
  1081. break;
  1082. // 字符串
  1083. case 's':
  1084. default:
  1085. if (is_scalar($data)) {
  1086. $data = (string) $data;
  1087. } else {
  1088. throw new \InvalidArgumentException('variable type error:' . gettype($data));
  1089. }
  1090. }
  1091. }
  1092. /**
  1093. * 是否存在某个请求参数
  1094. * @access public
  1095. * @param string $name 变量名
  1096. * @param string $type 变量类型
  1097. * @param bool $checkEmpty 是否检测空值
  1098. * @return mixed
  1099. */
  1100. public function has($name, $type = 'param', $checkEmpty = false)
  1101. {
  1102. if (empty($this->$type)) {
  1103. $param = $this->$type();
  1104. } else {
  1105. $param = $this->$type;
  1106. }
  1107. // 按.拆分成多维数组进行判断
  1108. foreach (explode('.', $name) as $val) {
  1109. if (isset($param[$val])) {
  1110. $param = $param[$val];
  1111. } else {
  1112. return false;
  1113. }
  1114. }
  1115. return ($checkEmpty && '' === $param) ? false : true;
  1116. }
  1117. /**
  1118. * 获取指定的参数
  1119. * @access public
  1120. * @param string|array $name 变量名
  1121. * @param string $type 变量类型
  1122. * @return mixed
  1123. */
  1124. public function only($name, $type = 'param')
  1125. {
  1126. $param = $this->$type();
  1127. if (is_string($name)) {
  1128. $name = explode(',', $name);
  1129. }
  1130. $item = [];
  1131. foreach ($name as $key) {
  1132. if (isset($param[$key])) {
  1133. $item[$key] = $param[$key];
  1134. }
  1135. }
  1136. return $item;
  1137. }
  1138. /**
  1139. * 排除指定参数获取
  1140. * @access public
  1141. * @param string|array $name 变量名
  1142. * @param string $type 变量类型
  1143. * @return mixed
  1144. */
  1145. public function except($name, $type = 'param')
  1146. {
  1147. $param = $this->$type();
  1148. if (is_string($name)) {
  1149. $name = explode(',', $name);
  1150. }
  1151. foreach ($name as $key) {
  1152. if (isset($param[$key])) {
  1153. unset($param[$key]);
  1154. }
  1155. }
  1156. return $param;
  1157. }
  1158. /**
  1159. * 当前是否ssl
  1160. * @access public
  1161. * @return bool
  1162. */
  1163. public function isSsl()
  1164. {
  1165. $server = array_merge($_SERVER, $this->server);
  1166. if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) {
  1167. return true;
  1168. } elseif (isset($server['REQUEST_SCHEME']) && 'https' == $server['REQUEST_SCHEME']) {
  1169. return true;
  1170. } elseif (isset($server['SERVER_PORT']) && ('443' == $server['SERVER_PORT'])) {
  1171. return true;
  1172. } elseif (isset($server['HTTP_X_FORWARDED_PROTO']) && 'https' == $server['HTTP_X_FORWARDED_PROTO']) {
  1173. return true;
  1174. } elseif (Config::get('https_agent_name') && isset($server[Config::get('https_agent_name')])) {
  1175. return true;
  1176. }
  1177. return false;
  1178. }
  1179. /**
  1180. * 当前是否Ajax请求
  1181. * @access public
  1182. * @param bool $ajax true 获取原始ajax请求
  1183. * @return bool
  1184. */
  1185. public function isAjax($ajax = false)
  1186. {
  1187. $value = $this->server('HTTP_X_REQUESTED_WITH', '', 'strtolower');
  1188. $result = ('xmlhttprequest' == $value) ? true : false;
  1189. if (true === $ajax) {
  1190. return $result;
  1191. } else {
  1192. $result = $this->param(Config::get('var_ajax')) ? true : $result;
  1193. $this->mergeParam = false;
  1194. return $result;
  1195. }
  1196. }
  1197. /**
  1198. * 当前是否Pjax请求
  1199. * @access public
  1200. * @param bool $pjax true 获取原始pjax请求
  1201. * @return bool
  1202. */
  1203. public function isPjax($pjax = false)
  1204. {
  1205. $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
  1206. if (true === $pjax) {
  1207. return $result;
  1208. } else {
  1209. $result = $this->param(Config::get('var_pjax')) ? true : $result;
  1210. $this->mergeParam = false;
  1211. return $result;
  1212. }
  1213. }
  1214. /**
  1215. * 获取客户端IP地址
  1216. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  1217. * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
  1218. * @return mixed
  1219. */
  1220. public function ip($type = 0, $adv = true)
  1221. {
  1222. $type = $type ? 1 : 0;
  1223. static $ip = null;
  1224. if (null !== $ip) {
  1225. return $ip[$type];
  1226. }
  1227. $httpAgentIp = Config::get('http_agent_ip');
  1228. if ($httpAgentIp && isset($_SERVER[$httpAgentIp])) {
  1229. $ip = $_SERVER[$httpAgentIp];
  1230. } elseif ($adv) {
  1231. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  1232. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  1233. $pos = array_search('unknown', $arr);
  1234. if (false !== $pos) {
  1235. unset($arr[$pos]);
  1236. }
  1237. $ip = trim(current($arr));
  1238. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  1239. $ip = $_SERVER['HTTP_CLIENT_IP'];
  1240. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1241. $ip = $_SERVER['REMOTE_ADDR'];
  1242. }
  1243. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1244. $ip = $_SERVER['REMOTE_ADDR'];
  1245. }
  1246. // IP地址合法验证
  1247. $long = sprintf("%u", ip2long($ip));
  1248. $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
  1249. return $ip[$type];
  1250. }
  1251. /**
  1252. * 检测是否使用手机访问
  1253. * @access public
  1254. * @return bool
  1255. */
  1256. public function isMobile()
  1257. {
  1258. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  1259. return true;
  1260. } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
  1261. return true;
  1262. } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
  1263. return true;
  1264. } elseif (isset($_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', $_SERVER['HTTP_USER_AGENT'])) {
  1265. return true;
  1266. } else {
  1267. return false;
  1268. }
  1269. }
  1270. /**
  1271. * 当前URL地址中的scheme参数
  1272. * @access public
  1273. * @return string
  1274. */
  1275. public function scheme()
  1276. {
  1277. return $this->isSsl() ? 'https' : 'http';
  1278. }
  1279. /**
  1280. * 当前请求URL地址中的query参数
  1281. * @access public
  1282. * @return string
  1283. */
  1284. public function query()
  1285. {
  1286. return $this->server('QUERY_STRING');
  1287. }
  1288. /**
  1289. * 当前请求的host
  1290. * @access public
  1291. * @param bool $strict true 仅仅获取HOST
  1292. * @return string
  1293. */
  1294. public function host($strict = false)
  1295. {
  1296. if (isset($_SERVER['HTTP_X_REAL_HOST'])) {
  1297. $host = $_SERVER['HTTP_X_REAL_HOST'];
  1298. } else {
  1299. $host = $this->server('HTTP_HOST');
  1300. }
  1301. return true === $strict && strpos($host, ':') ? strstr($host, ':', true) : $host;
  1302. }
  1303. /**
  1304. * 当前请求URL地址中的port参数
  1305. * @access public
  1306. * @return integer
  1307. */
  1308. public function port()
  1309. {
  1310. return $this->server('SERVER_PORT');
  1311. }
  1312. /**
  1313. * 当前请求 SERVER_PROTOCOL
  1314. * @access public
  1315. * @return integer
  1316. */
  1317. public function protocol()
  1318. {
  1319. return $this->server('SERVER_PROTOCOL');
  1320. }
  1321. /**
  1322. * 当前请求 REMOTE_PORT
  1323. * @access public
  1324. * @return integer
  1325. */
  1326. public function remotePort()
  1327. {
  1328. return $this->server('REMOTE_PORT');
  1329. }
  1330. /**
  1331. * 当前请求 HTTP_CONTENT_TYPE
  1332. * @access public
  1333. * @return string
  1334. */
  1335. public function contentType()
  1336. {
  1337. $contentType = $this->server('CONTENT_TYPE');
  1338. if ($contentType) {
  1339. if (strpos($contentType, ';')) {
  1340. list($type) = explode(';', $contentType);
  1341. } else {
  1342. $type = $contentType;
  1343. }
  1344. return trim($type);
  1345. }
  1346. return '';
  1347. }
  1348. /**
  1349. * 获取当前请求的路由信息
  1350. * @access public
  1351. * @param array $route 路由名称
  1352. * @return array
  1353. */
  1354. public function routeInfo($route = [])
  1355. {
  1356. if (!empty($route)) {
  1357. $this->routeInfo = $route;
  1358. } else {
  1359. return $this->routeInfo;
  1360. }
  1361. }
  1362. /**
  1363. * 设置或者获取当前请求的调度信息
  1364. * @access public
  1365. * @param array $dispatch 调度信息
  1366. * @return array
  1367. */
  1368. public function dispatch($dispatch = null)
  1369. {
  1370. if (!is_null($dispatch)) {
  1371. $this->dispatch = $dispatch;
  1372. }
  1373. return $this->dispatch;
  1374. }
  1375. /**
  1376. * 设置或者获取当前的模块名
  1377. * @access public
  1378. * @param string $module 模块名
  1379. * @return string|Request
  1380. */
  1381. public function module($module = null)
  1382. {
  1383. if (!is_null($module)) {
  1384. $this->module = $module;
  1385. return $this;
  1386. } else {
  1387. return $this->module ?: '';
  1388. }
  1389. }
  1390. /**
  1391. * 设置或者获取当前的控制器名
  1392. * @access public
  1393. * @param string $controller 控制器名
  1394. * @return string|Request
  1395. */
  1396. public function controller($controller = null)
  1397. {
  1398. if (!is_null($controller)) {
  1399. $this->controller = $controller;
  1400. return $this;
  1401. } else {
  1402. return $this->controller ?: '';
  1403. }
  1404. }
  1405. /**
  1406. * 设置或者获取当前的操作名
  1407. * @access public
  1408. * @param string $action 操作名
  1409. * @return string|Request
  1410. */
  1411. public function action($action = null)
  1412. {
  1413. if (!is_null($action) && !is_bool($action)) {
  1414. $this->action = $action;
  1415. return $this;
  1416. } else {
  1417. $name = $this->action ?: '';
  1418. return true === $action ? $name : strtolower($name);
  1419. }
  1420. }
  1421. /**
  1422. * 设置或者获取当前的语言
  1423. * @access public
  1424. * @param string $lang 语言名
  1425. * @return string|Request
  1426. */
  1427. public function langset($lang = null)
  1428. {
  1429. if (!is_null($lang)) {
  1430. $this->langset = $lang;
  1431. return $this;
  1432. } else {
  1433. return $this->langset ?: '';
  1434. }
  1435. }
  1436. /**
  1437. * 设置或者获取当前请求的content
  1438. * @access public
  1439. * @return string
  1440. */
  1441. public function getContent()
  1442. {
  1443. if (is_null($this->content)) {
  1444. $this->content = $this->input;
  1445. }
  1446. return $this->content;
  1447. }
  1448. /**
  1449. * 获取当前请求的php://input
  1450. * @access public
  1451. * @return string
  1452. */
  1453. public function getInput()
  1454. {
  1455. return $this->input;
  1456. }
  1457. /**
  1458. * 生成请求令牌
  1459. * @access public
  1460. * @param string $name 令牌名称
  1461. * @param mixed $type 令牌生成方法
  1462. * @return string
  1463. */
  1464. public function token($name = '__token__', $type = 'md5')
  1465. {
  1466. $type = is_callable($type) ? $type : 'md5';
  1467. $token = call_user_func($type, $_SERVER['REQUEST_TIME_FLOAT']);
  1468. if ($this->isAjax()) {
  1469. header($name . ': ' . $token);
  1470. }
  1471. Session::set($name, $token);
  1472. return $token;
  1473. }
  1474. /**
  1475. * 设置当前地址的请求缓存
  1476. * @access public
  1477. * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
  1478. * @param mixed $expire 缓存有效期
  1479. * @param array $except 缓存排除
  1480. * @param string $tag 缓存标签
  1481. * @return void
  1482. */
  1483. public function cache($key, $expire = null, $except = [], $tag = null)
  1484. {
  1485. if (!is_array($except)) {
  1486. $tag = $except;
  1487. $except = [];
  1488. }
  1489. if (false !== $key && $this->isGet() && !$this->isCheckCache) {
  1490. // 标记请求缓存检查
  1491. $this->isCheckCache = true;
  1492. if (false === $expire) {
  1493. // 关闭当前缓存
  1494. return;
  1495. }
  1496. if ($key instanceof \Closure) {
  1497. $key = call_user_func_array($key, [$this]);
  1498. } elseif (true === $key) {
  1499. foreach ($except as $rule) {
  1500. if (0 === stripos($this->url(), $rule)) {
  1501. return;
  1502. }
  1503. }
  1504. // 自动缓存功能
  1505. $key = '__URL__';
  1506. } elseif (strpos($key, '|')) {
  1507. list($key, $fun) = explode('|', $key);
  1508. }
  1509. // 特殊规则替换
  1510. if (false !== strpos($key, '__')) {
  1511. $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__', ''], [$this->module, $this->controller, $this->action, md5($this->url(true))], $key);
  1512. }
  1513. if (false !== strpos($key, ':')) {
  1514. $param = $this->param();
  1515. foreach ($param as $item => $val) {
  1516. if (is_string($val) && false !== strpos($key, ':' . $item)) {
  1517. $key = str_replace(':' . $item, $val, $key);
  1518. }
  1519. }
  1520. } elseif (strpos($key, ']')) {
  1521. if ('[' . $this->ext() . ']' == $key) {
  1522. // 缓存某个后缀的请求
  1523. $key = md5($this->url());
  1524. } else {
  1525. return;
  1526. }
  1527. }
  1528. if (isset($fun)) {
  1529. $key = $fun($key);
  1530. }
  1531. if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) {
  1532. // 读取缓存
  1533. $response = Response::create()->code(304);
  1534. throw new \think\exception\HttpResponseException($response);
  1535. } elseif (Cache::has($key)) {
  1536. list($content, $header) = Cache::get($key);
  1537. $response = Response::create($content)->header($header);
  1538. throw new \think\exception\HttpResponseException($response);
  1539. } else {
  1540. $this->cache = [$key, $expire, $tag];
  1541. }
  1542. }
  1543. }
  1544. /**
  1545. * 读取请求缓存设置
  1546. * @access public
  1547. * @return array
  1548. */
  1549. public function getCache()
  1550. {
  1551. return $this->cache;
  1552. }
  1553. /**
  1554. * 设置当前请求绑定的对象实例
  1555. * @access public
  1556. * @param string|array $name 绑定的对象标识
  1557. * @param mixed $obj 绑定的对象实例
  1558. * @return mixed
  1559. */
  1560. public function bind($name, $obj = null)
  1561. {
  1562. if (is_array($name)) {
  1563. $this->bind = array_merge($this->bind, $name);
  1564. } else {
  1565. $this->bind[$name] = $obj;
  1566. }
  1567. }
  1568. public function __set($name, $value)
  1569. {
  1570. $this->bind[$name] = $value;
  1571. }
  1572. public function __get($name)
  1573. {
  1574. return isset($this->bind[$name]) ? $this->bind[$name] : null;
  1575. }
  1576. public function __isset($name)
  1577. {
  1578. return isset($this->bind[$name]);
  1579. }
  1580. }