Request.php 50 KB

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