Request.php 48 KB

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