Request.php 48 KB

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