Request.php 47 KB

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