Request.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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. $method = strtoupper($_POST[Config::get('var_method')]);
  472. if (in_array($method, ['GET', 'POST', 'DELETE', 'PUT', 'PATCH'])) {
  473. $this->method = $method;
  474. $this->{$this->method}($_POST);
  475. } else {
  476. $this->method = 'POST';
  477. }
  478. unset($_POST[Config::get('var_method')]);
  479. } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
  480. $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
  481. } else {
  482. $this->method = IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
  483. }
  484. }
  485. return $this->method;
  486. }
  487. /**
  488. * 是否为GET请求
  489. * @access public
  490. * @return bool
  491. */
  492. public function isGet()
  493. {
  494. return $this->method() == 'GET';
  495. }
  496. /**
  497. * 是否为POST请求
  498. * @access public
  499. * @return bool
  500. */
  501. public function isPost()
  502. {
  503. return $this->method() == 'POST';
  504. }
  505. /**
  506. * 是否为PUT请求
  507. * @access public
  508. * @return bool
  509. */
  510. public function isPut()
  511. {
  512. return $this->method() == 'PUT';
  513. }
  514. /**
  515. * 是否为DELTE请求
  516. * @access public
  517. * @return bool
  518. */
  519. public function isDelete()
  520. {
  521. return $this->method() == 'DELETE';
  522. }
  523. /**
  524. * 是否为HEAD请求
  525. * @access public
  526. * @return bool
  527. */
  528. public function isHead()
  529. {
  530. return $this->method() == 'HEAD';
  531. }
  532. /**
  533. * 是否为PATCH请求
  534. * @access public
  535. * @return bool
  536. */
  537. public function isPatch()
  538. {
  539. return $this->method() == 'PATCH';
  540. }
  541. /**
  542. * 是否为OPTIONS请求
  543. * @access public
  544. * @return bool
  545. */
  546. public function isOptions()
  547. {
  548. return $this->method() == 'OPTIONS';
  549. }
  550. /**
  551. * 是否为cli
  552. * @access public
  553. * @return bool
  554. */
  555. public function isCli()
  556. {
  557. return PHP_SAPI == 'cli';
  558. }
  559. /**
  560. * 是否为cgi
  561. * @access public
  562. * @return bool
  563. */
  564. public function isCgi()
  565. {
  566. return strpos(PHP_SAPI, 'cgi') === 0;
  567. }
  568. /**
  569. * 获取当前请求的参数
  570. * @access public
  571. * @param string|array $name 变量名
  572. * @param mixed $default 默认值
  573. * @param string|array $filter 过滤方法
  574. * @return mixed
  575. */
  576. public function param($name = '', $default = null, $filter = '')
  577. {
  578. if (empty($this->param)) {
  579. $method = $this->method(true);
  580. // 自动获取请求变量
  581. switch ($method) {
  582. case 'POST':
  583. $vars = $this->post(false);
  584. break;
  585. case 'PUT':
  586. case 'DELETE':
  587. case 'PATCH':
  588. $vars = $this->put(false);
  589. break;
  590. default:
  591. $vars = [];
  592. }
  593. // 当前请求参数和URL地址中的参数合并
  594. $this->param = array_merge($this->get(false), $vars, $this->route(false));
  595. }
  596. if (true === $name) {
  597. // 获取包含文件上传信息的数组
  598. $file = $this->file();
  599. $data = is_array($file) ? array_merge($this->param, $file) : $this->param;
  600. return $this->input($data, '', $default, $filter);
  601. }
  602. return $this->input($this->param, $name, $default, $filter);
  603. }
  604. /**
  605. * 设置获取路由参数
  606. * @access public
  607. * @param string|array $name 变量名
  608. * @param mixed $default 默认值
  609. * @param string|array $filter 过滤方法
  610. * @return mixed
  611. */
  612. public function route($name = '', $default = null, $filter = '')
  613. {
  614. if (is_array($name)) {
  615. $this->param = [];
  616. return $this->route = array_merge($this->route, $name);
  617. }
  618. return $this->input($this->route, $name, $default, $filter);
  619. }
  620. /**
  621. * 设置获取GET参数
  622. * @access public
  623. * @param string|array $name 变量名
  624. * @param mixed $default 默认值
  625. * @param string|array $filter 过滤方法
  626. * @return mixed
  627. */
  628. public function get($name = '', $default = null, $filter = '')
  629. {
  630. if (empty($this->get)) {
  631. $this->get = $_GET;
  632. }
  633. if (is_array($name)) {
  634. $this->param = [];
  635. return $this->get = array_merge($this->get, $name);
  636. }
  637. return $this->input($this->get, $name, $default, $filter);
  638. }
  639. /**
  640. * 设置获取POST参数
  641. * @access public
  642. * @param string $name 变量名
  643. * @param mixed $default 默认值
  644. * @param string|array $filter 过滤方法
  645. * @return mixed
  646. */
  647. public function post($name = '', $default = null, $filter = '')
  648. {
  649. if (empty($this->post)) {
  650. $content = $this->input;
  651. if (empty($_POST) && false !== strpos($this->contentType(), 'application/json')) {
  652. $this->post = (array) json_decode($content, true);
  653. } else {
  654. $this->post = $_POST;
  655. }
  656. }
  657. if (is_array($name)) {
  658. $this->param = [];
  659. return $this->post = array_merge($this->post, $name);
  660. }
  661. return $this->input($this->post, $name, $default, $filter);
  662. }
  663. /**
  664. * 设置获取PUT参数
  665. * @access public
  666. * @param string|array $name 变量名
  667. * @param mixed $default 默认值
  668. * @param string|array $filter 过滤方法
  669. * @return mixed
  670. */
  671. public function put($name = '', $default = null, $filter = '')
  672. {
  673. if (is_null($this->put)) {
  674. $content = $this->input;
  675. if (false !== strpos($this->contentType(), 'application/json')) {
  676. $this->put = (array) json_decode($content, true);
  677. } else {
  678. parse_str($content, $this->put);
  679. }
  680. }
  681. if (is_array($name)) {
  682. $this->param = [];
  683. return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
  684. }
  685. return $this->input($this->put, $name, $default, $filter);
  686. }
  687. /**
  688. * 设置获取DELETE参数
  689. * @access public
  690. * @param string|array $name 变量名
  691. * @param mixed $default 默认值
  692. * @param string|array $filter 过滤方法
  693. * @return mixed
  694. */
  695. public function delete($name = '', $default = null, $filter = '')
  696. {
  697. return $this->put($name, $default, $filter);
  698. }
  699. /**
  700. * 设置获取PATCH参数
  701. * @access public
  702. * @param string|array $name 变量名
  703. * @param mixed $default 默认值
  704. * @param string|array $filter 过滤方法
  705. * @return mixed
  706. */
  707. public function patch($name = '', $default = null, $filter = '')
  708. {
  709. return $this->put($name, $default, $filter);
  710. }
  711. /**
  712. * 获取request变量
  713. * @param string $name 数据名称
  714. * @param string $default 默认值
  715. * @param string|array $filter 过滤方法
  716. * @return mixed
  717. */
  718. public function request($name = '', $default = null, $filter = '')
  719. {
  720. if (empty($this->request)) {
  721. $this->request = $_REQUEST;
  722. }
  723. if (is_array($name)) {
  724. $this->param = [];
  725. return $this->request = array_merge($this->request, $name);
  726. }
  727. return $this->input($this->request, $name, $default, $filter);
  728. }
  729. /**
  730. * 获取session数据
  731. * @access public
  732. * @param string|array $name 数据名称
  733. * @param string $default 默认值
  734. * @param string|array $filter 过滤方法
  735. * @return mixed
  736. */
  737. public function session($name = '', $default = null, $filter = '')
  738. {
  739. if (empty($this->session)) {
  740. $this->session = Session::get();
  741. }
  742. if (is_array($name)) {
  743. return $this->session = array_merge($this->session, $name);
  744. }
  745. return $this->input($this->session, $name, $default, $filter);
  746. }
  747. /**
  748. * 获取cookie参数
  749. * @access public
  750. * @param string|array $name 数据名称
  751. * @param string $default 默认值
  752. * @param string|array $filter 过滤方法
  753. * @return mixed
  754. */
  755. public function cookie($name = '', $default = null, $filter = '')
  756. {
  757. if (empty($this->cookie)) {
  758. $this->cookie = Cookie::get();
  759. }
  760. if (is_array($name)) {
  761. return $this->cookie = array_merge($this->cookie, $name);
  762. } elseif (!empty($name)) {
  763. $data = Cookie::has($name) ? Cookie::get($name) : $default;
  764. } else {
  765. $data = $this->cookie;
  766. }
  767. // 解析过滤器
  768. $filter = $this->getFilter($filter, $default);
  769. if (is_array($data)) {
  770. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  771. reset($data);
  772. } else {
  773. $this->filterValue($data, $name, $filter);
  774. }
  775. return $data;
  776. }
  777. /**
  778. * 获取server参数
  779. * @access public
  780. * @param string|array $name 数据名称
  781. * @param string $default 默认值
  782. * @param string|array $filter 过滤方法
  783. * @return mixed
  784. */
  785. public function server($name = '', $default = null, $filter = '')
  786. {
  787. if (empty($this->server)) {
  788. $this->server = $_SERVER;
  789. }
  790. if (is_array($name)) {
  791. return $this->server = array_merge($this->server, $name);
  792. }
  793. return $this->input($this->server, false === $name ? false : strtoupper($name), $default, $filter);
  794. }
  795. /**
  796. * 获取上传的文件信息
  797. * @access public
  798. * @param string|array $name 名称
  799. * @return null|array|\think\File
  800. */
  801. public function file($name = '')
  802. {
  803. if (empty($this->file)) {
  804. $this->file = isset($_FILES) ? $_FILES : [];
  805. }
  806. if (is_array($name)) {
  807. return $this->file = array_merge($this->file, $name);
  808. }
  809. $files = $this->file;
  810. if (!empty($files)) {
  811. // 处理上传文件
  812. $array = [];
  813. foreach ($files as $key => $file) {
  814. if (is_array($file['name'])) {
  815. $item = [];
  816. $keys = array_keys($file);
  817. $count = count($file['name']);
  818. for ($i = 0; $i < $count; $i++) {
  819. if (empty($file['tmp_name'][$i]) || !is_file($file['tmp_name'][$i])) {
  820. continue;
  821. }
  822. $temp['key'] = $key;
  823. foreach ($keys as $_key) {
  824. $temp[$_key] = $file[$_key][$i];
  825. }
  826. $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
  827. }
  828. $array[$key] = $item;
  829. } else {
  830. if ($file instanceof File) {
  831. $array[$key] = $file;
  832. } else {
  833. if (empty($file['tmp_name']) || !is_file($file['tmp_name'])) {
  834. continue;
  835. }
  836. $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
  837. }
  838. }
  839. }
  840. if (strpos($name, '.')) {
  841. list($name, $sub) = explode('.', $name);
  842. }
  843. if ('' === $name) {
  844. // 获取全部文件
  845. return $array;
  846. } elseif (isset($sub) && isset($array[$name][$sub])) {
  847. return $array[$name][$sub];
  848. } elseif (isset($array[$name])) {
  849. return $array[$name];
  850. }
  851. }
  852. return;
  853. }
  854. /**
  855. * 获取环境变量
  856. * @param string|array $name 数据名称
  857. * @param string $default 默认值
  858. * @param string|array $filter 过滤方法
  859. * @return mixed
  860. */
  861. public function env($name = '', $default = null, $filter = '')
  862. {
  863. if (empty($this->env)) {
  864. $this->env = $_ENV;
  865. }
  866. if (is_array($name)) {
  867. return $this->env = array_merge($this->env, $name);
  868. }
  869. return $this->input($this->env, false === $name ? false : strtoupper($name), $default, $filter);
  870. }
  871. /**
  872. * 设置或者获取当前的Header
  873. * @access public
  874. * @param string|array $name header名称
  875. * @param string $default 默认值
  876. * @return string
  877. */
  878. public function header($name = '', $default = null)
  879. {
  880. if (empty($this->header)) {
  881. $header = [];
  882. if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
  883. $header = $result;
  884. } else {
  885. $server = $this->server ?: $_SERVER;
  886. foreach ($server as $key => $val) {
  887. if (0 === strpos($key, 'HTTP_')) {
  888. $key = str_replace('_', '-', strtolower(substr($key, 5)));
  889. $header[$key] = $val;
  890. }
  891. }
  892. if (isset($server['CONTENT_TYPE'])) {
  893. $header['content-type'] = $server['CONTENT_TYPE'];
  894. }
  895. if (isset($server['CONTENT_LENGTH'])) {
  896. $header['content-length'] = $server['CONTENT_LENGTH'];
  897. }
  898. }
  899. $this->header = array_change_key_case($header);
  900. }
  901. if (is_array($name)) {
  902. return $this->header = array_merge($this->header, $name);
  903. }
  904. if ('' === $name) {
  905. return $this->header;
  906. }
  907. $name = str_replace('_', '-', strtolower($name));
  908. return isset($this->header[$name]) ? $this->header[$name] : $default;
  909. }
  910. /**
  911. * 获取变量 支持过滤和默认值
  912. * @param array $data 数据源
  913. * @param string|false $name 字段名
  914. * @param mixed $default 默认值
  915. * @param string|array $filter 过滤函数
  916. * @return mixed
  917. */
  918. public function input($data = [], $name = '', $default = null, $filter = '')
  919. {
  920. if (false === $name) {
  921. // 获取原始数据
  922. return $data;
  923. }
  924. $name = (string) $name;
  925. if ('' != $name) {
  926. // 解析name
  927. if (strpos($name, '/')) {
  928. list($name, $type) = explode('/', $name);
  929. } else {
  930. $type = 's';
  931. }
  932. // 按.拆分成多维数组进行判断
  933. foreach (explode('.', $name) as $val) {
  934. if (isset($data[$val])) {
  935. $data = $data[$val];
  936. } else {
  937. // 无输入数据,返回默认值
  938. return $default;
  939. }
  940. }
  941. if (is_object($data)) {
  942. return $data;
  943. }
  944. }
  945. // 解析过滤器
  946. $filter = $this->getFilter($filter, $default);
  947. if (is_array($data)) {
  948. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  949. reset($data);
  950. } else {
  951. $this->filterValue($data, $name, $filter);
  952. }
  953. if (isset($type) && $data !== $default) {
  954. // 强制类型转换
  955. $this->typeCast($data, $type);
  956. }
  957. return $data;
  958. }
  959. /**
  960. * 设置或获取当前的过滤规则
  961. * @param mixed $filter 过滤规则
  962. * @return mixed
  963. */
  964. public function filter($filter = null)
  965. {
  966. if (is_null($filter)) {
  967. return $this->filter;
  968. } else {
  969. $this->filter = $filter;
  970. }
  971. }
  972. protected function getFilter($filter, $default)
  973. {
  974. if (is_null($filter)) {
  975. $filter = [];
  976. } else {
  977. $filter = $filter ?: $this->filter;
  978. if (is_string($filter) && false === strpos($filter, '/')) {
  979. $filter = explode(',', $filter);
  980. } else {
  981. $filter = (array) $filter;
  982. }
  983. }
  984. $filter[] = $default;
  985. return $filter;
  986. }
  987. /**
  988. * 递归过滤给定的值
  989. * @param mixed $value 键值
  990. * @param mixed $key 键名
  991. * @param array $filters 过滤方法+默认值
  992. * @return mixed
  993. */
  994. private function filterValue(&$value, $key, $filters)
  995. {
  996. $default = array_pop($filters);
  997. foreach ($filters as $filter) {
  998. if (is_callable($filter)) {
  999. // 调用函数或者方法过滤
  1000. $value = call_user_func($filter, $value);
  1001. } elseif (is_scalar($value)) {
  1002. if (false !== strpos($filter, '/')) {
  1003. // 正则过滤
  1004. if (!preg_match($filter, $value)) {
  1005. // 匹配不成功返回默认值
  1006. $value = $default;
  1007. break;
  1008. }
  1009. } elseif (!empty($filter)) {
  1010. // filter函数不存在时, 则使用filter_var进行过滤
  1011. // filter为非整形值时, 调用filter_id取得过滤id
  1012. $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
  1013. if (false === $value) {
  1014. $value = $default;
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. }
  1020. return $this->filterExp($value);
  1021. }
  1022. /**
  1023. * 过滤表单中的表达式
  1024. * @param string $value
  1025. * @return void
  1026. */
  1027. public function filterExp(&$value)
  1028. {
  1029. // 过滤查询特殊字符
  1030. 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)) {
  1031. $value .= ' ';
  1032. }
  1033. // TODO 其他安全过滤
  1034. }
  1035. /**
  1036. * 强制类型转换
  1037. * @param string $data
  1038. * @param string $type
  1039. * @return mixed
  1040. */
  1041. private function typeCast(&$data, $type)
  1042. {
  1043. switch (strtolower($type)) {
  1044. // 数组
  1045. case 'a':
  1046. $data = (array) $data;
  1047. break;
  1048. // 数字
  1049. case 'd':
  1050. $data = (int) $data;
  1051. break;
  1052. // 浮点
  1053. case 'f':
  1054. $data = (float) $data;
  1055. break;
  1056. // 布尔
  1057. case 'b':
  1058. $data = (boolean) $data;
  1059. break;
  1060. // 字符串
  1061. case 's':
  1062. default:
  1063. if (is_scalar($data)) {
  1064. $data = (string) $data;
  1065. } else {
  1066. throw new \InvalidArgumentException('variable type error:' . gettype($data));
  1067. }
  1068. }
  1069. }
  1070. /**
  1071. * 是否存在某个请求参数
  1072. * @access public
  1073. * @param string $name 变量名
  1074. * @param string $type 变量类型
  1075. * @param bool $checkEmpty 是否检测空值
  1076. * @return mixed
  1077. */
  1078. public function has($name, $type = 'param', $checkEmpty = false)
  1079. {
  1080. if (empty($this->$type)) {
  1081. $param = $this->$type();
  1082. } else {
  1083. $param = $this->$type;
  1084. }
  1085. // 按.拆分成多维数组进行判断
  1086. foreach (explode('.', $name) as $val) {
  1087. if (isset($param[$val])) {
  1088. $param = $param[$val];
  1089. } else {
  1090. return false;
  1091. }
  1092. }
  1093. return ($checkEmpty && '' === $param) ? false : true;
  1094. }
  1095. /**
  1096. * 获取指定的参数
  1097. * @access public
  1098. * @param string|array $name 变量名
  1099. * @param string $type 变量类型
  1100. * @return mixed
  1101. */
  1102. public function only($name, $type = 'param')
  1103. {
  1104. $param = $this->$type();
  1105. if (is_string($name)) {
  1106. $name = explode(',', $name);
  1107. }
  1108. $item = [];
  1109. foreach ($name as $key) {
  1110. if (isset($param[$key])) {
  1111. $item[$key] = $param[$key];
  1112. }
  1113. }
  1114. return $item;
  1115. }
  1116. /**
  1117. * 排除指定参数获取
  1118. * @access public
  1119. * @param string|array $name 变量名
  1120. * @param string $type 变量类型
  1121. * @return mixed
  1122. */
  1123. public function except($name, $type = 'param')
  1124. {
  1125. $param = $this->$type();
  1126. if (is_string($name)) {
  1127. $name = explode(',', $name);
  1128. }
  1129. foreach ($name as $key) {
  1130. if (isset($param[$key])) {
  1131. unset($param[$key]);
  1132. }
  1133. }
  1134. return $param;
  1135. }
  1136. /**
  1137. * 当前是否ssl
  1138. * @access public
  1139. * @return bool
  1140. */
  1141. public function isSsl()
  1142. {
  1143. $server = array_merge($_SERVER, $this->server);
  1144. if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) {
  1145. return true;
  1146. } elseif (isset($server['REQUEST_SCHEME']) && 'https' == $server['REQUEST_SCHEME']) {
  1147. return true;
  1148. } elseif (isset($server['SERVER_PORT']) && ('443' == $server['SERVER_PORT'])) {
  1149. return true;
  1150. } elseif (isset($server['HTTP_X_FORWARDED_PROTO']) && 'https' == $server['HTTP_X_FORWARDED_PROTO']) {
  1151. return true;
  1152. } elseif (Config::get('https_agent_name') && isset($server[Config::get('https_agent_name')])) {
  1153. return true;
  1154. }
  1155. return false;
  1156. }
  1157. /**
  1158. * 当前是否Ajax请求
  1159. * @access public
  1160. * @param bool $ajax true 获取原始ajax请求
  1161. * @return bool
  1162. */
  1163. public function isAjax($ajax = false)
  1164. {
  1165. $value = $this->server('HTTP_X_REQUESTED_WITH', '', 'strtolower');
  1166. $result = ('xmlhttprequest' == $value) ? true : false;
  1167. if (true === $ajax) {
  1168. return $result;
  1169. } else {
  1170. return $this->param(Config::get('var_ajax')) ? true : $result;
  1171. }
  1172. }
  1173. /**
  1174. * 当前是否Pjax请求
  1175. * @access public
  1176. * @param bool $pjax true 获取原始pjax请求
  1177. * @return bool
  1178. */
  1179. public function isPjax($pjax = false)
  1180. {
  1181. $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
  1182. if (true === $pjax) {
  1183. return $result;
  1184. } else {
  1185. return $this->param(Config::get('var_pjax')) ? true : $result;
  1186. }
  1187. }
  1188. /**
  1189. * 获取客户端IP地址
  1190. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  1191. * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
  1192. * @return mixed
  1193. */
  1194. public function ip($type = 0, $adv = true)
  1195. {
  1196. $type = $type ? 1 : 0;
  1197. static $ip = null;
  1198. if (null !== $ip) {
  1199. return $ip[$type];
  1200. }
  1201. if ($adv) {
  1202. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  1203. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  1204. $pos = array_search('unknown', $arr);
  1205. if (false !== $pos) {
  1206. unset($arr[$pos]);
  1207. }
  1208. $ip = trim(current($arr));
  1209. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  1210. $ip = $_SERVER['HTTP_CLIENT_IP'];
  1211. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1212. $ip = $_SERVER['REMOTE_ADDR'];
  1213. }
  1214. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1215. $ip = $_SERVER['REMOTE_ADDR'];
  1216. }
  1217. // IP地址合法验证
  1218. $long = sprintf("%u", ip2long($ip));
  1219. $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
  1220. return $ip[$type];
  1221. }
  1222. /**
  1223. * 检测是否使用手机访问
  1224. * @access public
  1225. * @return bool
  1226. */
  1227. public function isMobile()
  1228. {
  1229. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  1230. return true;
  1231. } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
  1232. return true;
  1233. } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
  1234. return true;
  1235. } 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'])) {
  1236. return true;
  1237. } else {
  1238. return false;
  1239. }
  1240. }
  1241. /**
  1242. * 当前URL地址中的scheme参数
  1243. * @access public
  1244. * @return string
  1245. */
  1246. public function scheme()
  1247. {
  1248. return $this->isSsl() ? 'https' : 'http';
  1249. }
  1250. /**
  1251. * 当前请求URL地址中的query参数
  1252. * @access public
  1253. * @return string
  1254. */
  1255. public function query()
  1256. {
  1257. return $this->server('QUERY_STRING');
  1258. }
  1259. /**
  1260. * 当前请求的host
  1261. * @access public
  1262. * @return string
  1263. */
  1264. public function host()
  1265. {
  1266. if (isset($_SERVER['HTTP_X_REAL_HOST'])) {
  1267. return $_SERVER['HTTP_X_REAL_HOST'];
  1268. }
  1269. return $this->server('HTTP_HOST');
  1270. }
  1271. /**
  1272. * 当前请求URL地址中的port参数
  1273. * @access public
  1274. * @return integer
  1275. */
  1276. public function port()
  1277. {
  1278. return $this->server('SERVER_PORT');
  1279. }
  1280. /**
  1281. * 当前请求 SERVER_PROTOCOL
  1282. * @access public
  1283. * @return integer
  1284. */
  1285. public function protocol()
  1286. {
  1287. return $this->server('SERVER_PROTOCOL');
  1288. }
  1289. /**
  1290. * 当前请求 REMOTE_PORT
  1291. * @access public
  1292. * @return integer
  1293. */
  1294. public function remotePort()
  1295. {
  1296. return $this->server('REMOTE_PORT');
  1297. }
  1298. /**
  1299. * 当前请求 HTTP_CONTENT_TYPE
  1300. * @access public
  1301. * @return string
  1302. */
  1303. public function contentType()
  1304. {
  1305. $contentType = $this->server('CONTENT_TYPE');
  1306. if ($contentType) {
  1307. if (strpos($contentType, ';')) {
  1308. list($type) = explode(';', $contentType);
  1309. } else {
  1310. $type = $contentType;
  1311. }
  1312. return trim($type);
  1313. }
  1314. return '';
  1315. }
  1316. /**
  1317. * 获取当前请求的路由信息
  1318. * @access public
  1319. * @param array $route 路由名称
  1320. * @return array
  1321. */
  1322. public function routeInfo($route = [])
  1323. {
  1324. if (!empty($route)) {
  1325. $this->routeInfo = $route;
  1326. } else {
  1327. return $this->routeInfo;
  1328. }
  1329. }
  1330. /**
  1331. * 设置或者获取当前请求的调度信息
  1332. * @access public
  1333. * @param array $dispatch 调度信息
  1334. * @return array
  1335. */
  1336. public function dispatch($dispatch = null)
  1337. {
  1338. if (!is_null($dispatch)) {
  1339. $this->dispatch = $dispatch;
  1340. }
  1341. return $this->dispatch;
  1342. }
  1343. /**
  1344. * 设置或者获取当前的模块名
  1345. * @access public
  1346. * @param string $module 模块名
  1347. * @return string|Request
  1348. */
  1349. public function module($module = null)
  1350. {
  1351. if (!is_null($module)) {
  1352. $this->module = $module;
  1353. return $this;
  1354. } else {
  1355. return $this->module ?: '';
  1356. }
  1357. }
  1358. /**
  1359. * 设置或者获取当前的控制器名
  1360. * @access public
  1361. * @param string $controller 控制器名
  1362. * @return string|Request
  1363. */
  1364. public function controller($controller = null)
  1365. {
  1366. if (!is_null($controller)) {
  1367. $this->controller = $controller;
  1368. return $this;
  1369. } else {
  1370. return $this->controller ?: '';
  1371. }
  1372. }
  1373. /**
  1374. * 设置或者获取当前的操作名
  1375. * @access public
  1376. * @param string $action 操作名
  1377. * @return string|Request
  1378. */
  1379. public function action($action = null)
  1380. {
  1381. if (!is_null($action)) {
  1382. $this->action = $action;
  1383. return $this;
  1384. } else {
  1385. return $this->action ?: '';
  1386. }
  1387. }
  1388. /**
  1389. * 设置或者获取当前的语言
  1390. * @access public
  1391. * @param string $lang 语言名
  1392. * @return string|Request
  1393. */
  1394. public function langset($lang = null)
  1395. {
  1396. if (!is_null($lang)) {
  1397. $this->langset = $lang;
  1398. return $this;
  1399. } else {
  1400. return $this->langset ?: '';
  1401. }
  1402. }
  1403. /**
  1404. * 设置或者获取当前请求的content
  1405. * @access public
  1406. * @return string
  1407. */
  1408. public function getContent()
  1409. {
  1410. if (is_null($this->content)) {
  1411. $this->content = $this->input;
  1412. }
  1413. return $this->content;
  1414. }
  1415. /**
  1416. * 获取当前请求的php://input
  1417. * @access public
  1418. * @return string
  1419. */
  1420. public function getInput()
  1421. {
  1422. return $this->input;
  1423. }
  1424. /**
  1425. * 生成请求令牌
  1426. * @access public
  1427. * @param string $name 令牌名称
  1428. * @param mixed $type 令牌生成方法
  1429. * @return string
  1430. */
  1431. public function token($name = '__token__', $type = 'md5')
  1432. {
  1433. $type = is_callable($type) ? $type : 'md5';
  1434. $token = call_user_func($type, $_SERVER['REQUEST_TIME_FLOAT']);
  1435. if ($this->isAjax()) {
  1436. header($name . ': ' . $token);
  1437. }
  1438. Session::set($name, $token);
  1439. return $token;
  1440. }
  1441. /**
  1442. * 设置当前地址的请求缓存
  1443. * @access public
  1444. * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
  1445. * @param mixed $expire 缓存有效期
  1446. * @param array $except 缓存排除
  1447. * @param string $tag 缓存标签
  1448. * @return void
  1449. */
  1450. public function cache($key, $expire = null, $except = [], $tag = null)
  1451. {
  1452. if (!is_array($except)) {
  1453. $tag = $except;
  1454. $except = [];
  1455. }
  1456. if (false !== $key && $this->isGet() && !$this->isCheckCache) {
  1457. // 标记请求缓存检查
  1458. $this->isCheckCache = true;
  1459. if (false === $expire) {
  1460. // 关闭当前缓存
  1461. return;
  1462. }
  1463. if ($key instanceof \Closure) {
  1464. $key = call_user_func_array($key, [$this]);
  1465. } elseif (true === $key) {
  1466. foreach ($except as $rule) {
  1467. if (0 === stripos($this->url(), $rule)) {
  1468. return;
  1469. }
  1470. }
  1471. // 自动缓存功能
  1472. $key = '__URL__';
  1473. } elseif (strpos($key, '|')) {
  1474. list($key, $fun) = explode('|', $key);
  1475. }
  1476. // 特殊规则替换
  1477. if (false !== strpos($key, '__')) {
  1478. $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__', ''], [$this->module, $this->controller, $this->action, md5($this->url(true))], $key);
  1479. }
  1480. if (false !== strpos($key, ':')) {
  1481. $param = $this->param();
  1482. foreach ($param as $item => $val) {
  1483. if (is_string($val) && false !== strpos($key, ':' . $item)) {
  1484. $key = str_replace(':' . $item, $val, $key);
  1485. }
  1486. }
  1487. } elseif (strpos($key, ']')) {
  1488. if ('[' . $this->ext() . ']' == $key) {
  1489. // 缓存某个后缀的请求
  1490. $key = md5($this->url());
  1491. } else {
  1492. return;
  1493. }
  1494. }
  1495. if (isset($fun)) {
  1496. $key = $fun($key);
  1497. }
  1498. if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) {
  1499. // 读取缓存
  1500. $response = Response::create()->code(304);
  1501. throw new \think\exception\HttpResponseException($response);
  1502. } elseif (Cache::has($key)) {
  1503. list($content, $header) = Cache::get($key);
  1504. $response = Response::create($content)->header($header);
  1505. throw new \think\exception\HttpResponseException($response);
  1506. } else {
  1507. $this->cache = [$key, $expire, $tag];
  1508. }
  1509. }
  1510. }
  1511. /**
  1512. * 读取请求缓存设置
  1513. * @access public
  1514. * @return array
  1515. */
  1516. public function getCache()
  1517. {
  1518. return $this->cache;
  1519. }
  1520. /**
  1521. * 设置当前请求绑定的对象实例
  1522. * @access public
  1523. * @param string|array $name 绑定的对象标识
  1524. * @param mixed $obj 绑定的对象实例
  1525. * @return mixed
  1526. */
  1527. public function bind($name, $obj = null)
  1528. {
  1529. if (is_array($name)) {
  1530. $this->bind = array_merge($this->bind, $name);
  1531. } else {
  1532. $this->bind[$name] = $obj;
  1533. }
  1534. }
  1535. public function __set($name, $value)
  1536. {
  1537. $this->bind[$name] = $value;
  1538. }
  1539. public function __get($name)
  1540. {
  1541. return isset($this->bind[$name]) ? $this->bind[$name] : null;
  1542. }
  1543. public function __isset($name)
  1544. {
  1545. return isset($this->bind[$name]);
  1546. }
  1547. }