Request.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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($params, '', '&');
  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|NOT EXISTS|NOTEXISTS|EXISTS|NOT NULL|NOTNULL|NULL|BETWEEN TIME|NOT BETWEEN TIME|NOTBETWEEN TIME|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. $httpAgentIp = Config::get('http_agent_ip');
  1196. if ($httpAgentIp && isset($_SERVER[$httpAgentIp])) {
  1197. $ip = $_SERVER[$httpAgentIp];
  1198. } elseif ($adv) {
  1199. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  1200. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  1201. $pos = array_search('unknown', $arr);
  1202. if (false !== $pos) {
  1203. unset($arr[$pos]);
  1204. }
  1205. $ip = trim(current($arr));
  1206. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  1207. $ip = $_SERVER['HTTP_CLIENT_IP'];
  1208. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1209. $ip = $_SERVER['REMOTE_ADDR'];
  1210. }
  1211. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  1212. $ip = $_SERVER['REMOTE_ADDR'];
  1213. }
  1214. // IP地址合法验证
  1215. $long = sprintf("%u", ip2long($ip));
  1216. $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
  1217. return $ip[$type];
  1218. }
  1219. /**
  1220. * 检测是否使用手机访问
  1221. * @access public
  1222. * @return bool
  1223. */
  1224. public function isMobile()
  1225. {
  1226. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  1227. return true;
  1228. } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
  1229. return true;
  1230. } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
  1231. return true;
  1232. } 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'])) {
  1233. return true;
  1234. } else {
  1235. return false;
  1236. }
  1237. }
  1238. /**
  1239. * 当前URL地址中的scheme参数
  1240. * @access public
  1241. * @return string
  1242. */
  1243. public function scheme()
  1244. {
  1245. return $this->isSsl() ? 'https' : 'http';
  1246. }
  1247. /**
  1248. * 当前请求URL地址中的query参数
  1249. * @access public
  1250. * @return string
  1251. */
  1252. public function query()
  1253. {
  1254. return $this->server('QUERY_STRING');
  1255. }
  1256. /**
  1257. * 当前请求的host
  1258. * @access public
  1259. * @return string
  1260. */
  1261. public function host()
  1262. {
  1263. if (isset($_SERVER['HTTP_X_REAL_HOST'])) {
  1264. return $_SERVER['HTTP_X_REAL_HOST'];
  1265. }
  1266. return $this->server('HTTP_HOST');
  1267. }
  1268. /**
  1269. * 当前请求URL地址中的port参数
  1270. * @access public
  1271. * @return integer
  1272. */
  1273. public function port()
  1274. {
  1275. return $this->server('SERVER_PORT');
  1276. }
  1277. /**
  1278. * 当前请求 SERVER_PROTOCOL
  1279. * @access public
  1280. * @return integer
  1281. */
  1282. public function protocol()
  1283. {
  1284. return $this->server('SERVER_PROTOCOL');
  1285. }
  1286. /**
  1287. * 当前请求 REMOTE_PORT
  1288. * @access public
  1289. * @return integer
  1290. */
  1291. public function remotePort()
  1292. {
  1293. return $this->server('REMOTE_PORT');
  1294. }
  1295. /**
  1296. * 当前请求 HTTP_CONTENT_TYPE
  1297. * @access public
  1298. * @return string
  1299. */
  1300. public function contentType()
  1301. {
  1302. $contentType = $this->server('CONTENT_TYPE');
  1303. if ($contentType) {
  1304. if (strpos($contentType, ';')) {
  1305. list($type) = explode(';', $contentType);
  1306. } else {
  1307. $type = $contentType;
  1308. }
  1309. return trim($type);
  1310. }
  1311. return '';
  1312. }
  1313. /**
  1314. * 获取当前请求的路由信息
  1315. * @access public
  1316. * @param array $route 路由名称
  1317. * @return array
  1318. */
  1319. public function routeInfo($route = [])
  1320. {
  1321. if (!empty($route)) {
  1322. $this->routeInfo = $route;
  1323. } else {
  1324. return $this->routeInfo;
  1325. }
  1326. }
  1327. /**
  1328. * 设置或者获取当前请求的调度信息
  1329. * @access public
  1330. * @param array $dispatch 调度信息
  1331. * @return array
  1332. */
  1333. public function dispatch($dispatch = null)
  1334. {
  1335. if (!is_null($dispatch)) {
  1336. $this->dispatch = $dispatch;
  1337. }
  1338. return $this->dispatch;
  1339. }
  1340. /**
  1341. * 设置或者获取当前的模块名
  1342. * @access public
  1343. * @param string $module 模块名
  1344. * @return string|Request
  1345. */
  1346. public function module($module = null)
  1347. {
  1348. if (!is_null($module)) {
  1349. $this->module = $module;
  1350. return $this;
  1351. } else {
  1352. return $this->module ?: '';
  1353. }
  1354. }
  1355. /**
  1356. * 设置或者获取当前的控制器名
  1357. * @access public
  1358. * @param string $controller 控制器名
  1359. * @return string|Request
  1360. */
  1361. public function controller($controller = null)
  1362. {
  1363. if (!is_null($controller)) {
  1364. $this->controller = $controller;
  1365. return $this;
  1366. } else {
  1367. return $this->controller ?: '';
  1368. }
  1369. }
  1370. /**
  1371. * 设置或者获取当前的操作名
  1372. * @access public
  1373. * @param string $action 操作名
  1374. * @return string|Request
  1375. */
  1376. public function action($action = null)
  1377. {
  1378. if (!is_null($action) && !is_bool($action)) {
  1379. $this->action = $action;
  1380. return $this;
  1381. } else {
  1382. $name = $this->action ?: '';
  1383. return true === $action ? $name : strtolower($name);
  1384. }
  1385. }
  1386. /**
  1387. * 设置或者获取当前的语言
  1388. * @access public
  1389. * @param string $lang 语言名
  1390. * @return string|Request
  1391. */
  1392. public function langset($lang = null)
  1393. {
  1394. if (!is_null($lang)) {
  1395. $this->langset = $lang;
  1396. return $this;
  1397. } else {
  1398. return $this->langset ?: '';
  1399. }
  1400. }
  1401. /**
  1402. * 设置或者获取当前请求的content
  1403. * @access public
  1404. * @return string
  1405. */
  1406. public function getContent()
  1407. {
  1408. if (is_null($this->content)) {
  1409. $this->content = $this->input;
  1410. }
  1411. return $this->content;
  1412. }
  1413. /**
  1414. * 获取当前请求的php://input
  1415. * @access public
  1416. * @return string
  1417. */
  1418. public function getInput()
  1419. {
  1420. return $this->input;
  1421. }
  1422. /**
  1423. * 生成请求令牌
  1424. * @access public
  1425. * @param string $name 令牌名称
  1426. * @param mixed $type 令牌生成方法
  1427. * @return string
  1428. */
  1429. public function token($name = '__token__', $type = 'md5')
  1430. {
  1431. $type = is_callable($type) ? $type : 'md5';
  1432. $token = call_user_func($type, $_SERVER['REQUEST_TIME_FLOAT']);
  1433. if ($this->isAjax()) {
  1434. header($name . ': ' . $token);
  1435. }
  1436. Session::set($name, $token);
  1437. return $token;
  1438. }
  1439. /**
  1440. * 设置当前地址的请求缓存
  1441. * @access public
  1442. * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
  1443. * @param mixed $expire 缓存有效期
  1444. * @param array $except 缓存排除
  1445. * @param string $tag 缓存标签
  1446. * @return void
  1447. */
  1448. public function cache($key, $expire = null, $except = [], $tag = null)
  1449. {
  1450. if (!is_array($except)) {
  1451. $tag = $except;
  1452. $except = [];
  1453. }
  1454. if (false !== $key && $this->isGet() && !$this->isCheckCache) {
  1455. // 标记请求缓存检查
  1456. $this->isCheckCache = true;
  1457. if (false === $expire) {
  1458. // 关闭当前缓存
  1459. return;
  1460. }
  1461. if ($key instanceof \Closure) {
  1462. $key = call_user_func_array($key, [$this]);
  1463. } elseif (true === $key) {
  1464. foreach ($except as $rule) {
  1465. if (0 === stripos($this->url(), $rule)) {
  1466. return;
  1467. }
  1468. }
  1469. // 自动缓存功能
  1470. $key = '__URL__';
  1471. } elseif (strpos($key, '|')) {
  1472. list($key, $fun) = explode('|', $key);
  1473. }
  1474. // 特殊规则替换
  1475. if (false !== strpos($key, '__')) {
  1476. $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__', ''], [$this->module, $this->controller, $this->action, md5($this->url(true))], $key);
  1477. }
  1478. if (false !== strpos($key, ':')) {
  1479. $param = $this->param();
  1480. foreach ($param as $item => $val) {
  1481. if (is_string($val) && false !== strpos($key, ':' . $item)) {
  1482. $key = str_replace(':' . $item, $val, $key);
  1483. }
  1484. }
  1485. } elseif (strpos($key, ']')) {
  1486. if ('[' . $this->ext() . ']' == $key) {
  1487. // 缓存某个后缀的请求
  1488. $key = md5($this->url());
  1489. } else {
  1490. return;
  1491. }
  1492. }
  1493. if (isset($fun)) {
  1494. $key = $fun($key);
  1495. }
  1496. if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) {
  1497. // 读取缓存
  1498. $response = Response::create()->code(304);
  1499. throw new \think\exception\HttpResponseException($response);
  1500. } elseif (Cache::has($key)) {
  1501. list($content, $header) = Cache::get($key);
  1502. $response = Response::create($content)->header($header);
  1503. throw new \think\exception\HttpResponseException($response);
  1504. } else {
  1505. $this->cache = [$key, $expire, $tag];
  1506. }
  1507. }
  1508. }
  1509. /**
  1510. * 读取请求缓存设置
  1511. * @access public
  1512. * @return array
  1513. */
  1514. public function getCache()
  1515. {
  1516. return $this->cache;
  1517. }
  1518. /**
  1519. * 设置当前请求绑定的对象实例
  1520. * @access public
  1521. * @param string|array $name 绑定的对象标识
  1522. * @param mixed $obj 绑定的对象实例
  1523. * @return mixed
  1524. */
  1525. public function bind($name, $obj = null)
  1526. {
  1527. if (is_array($name)) {
  1528. $this->bind = array_merge($this->bind, $name);
  1529. } else {
  1530. $this->bind[$name] = $obj;
  1531. }
  1532. }
  1533. public function __set($name, $value)
  1534. {
  1535. $this->bind[$name] = $value;
  1536. }
  1537. public function __get($name)
  1538. {
  1539. return isset($this->bind[$name]) ? $this->bind[$name] : null;
  1540. }
  1541. public function __isset($name)
  1542. {
  1543. return isset($this->bind[$name]);
  1544. }
  1545. }