Request.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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. use think\facade\Cookie;
  13. use think\facade\Session;
  14. class Request
  15. {
  16. /**
  17. * 配置参数
  18. * @var array
  19. */
  20. protected $config = [
  21. // 表单请求类型伪装变量
  22. 'var_method' => '_method',
  23. // 表单ajax伪装变量
  24. 'var_ajax' => '_ajax',
  25. // 表单pjax伪装变量
  26. 'var_pjax' => '_pjax',
  27. // PATHINFO变量名 用于兼容模式
  28. 'var_pathinfo' => 's',
  29. // 兼容PATH_INFO获取
  30. 'pathinfo_fetch' => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],
  31. // 默认全局过滤方法 用逗号分隔多个
  32. 'default_filter' => '',
  33. // 域名根,如thinkphp.cn
  34. 'url_domain_root' => '',
  35. // HTTPS代理标识
  36. 'https_agent_name' => '',
  37. // IP代理获取标识
  38. 'http_agent_ip' => 'HTTP_X_REAL_IP',
  39. // URL伪静态后缀
  40. 'url_html_suffix' => 'html',
  41. ];
  42. /**
  43. * 请求类型
  44. * @var string
  45. */
  46. protected $method;
  47. /**
  48. * 主机名(含端口)
  49. * @var string
  50. */
  51. protected $host;
  52. /**
  53. * 域名(含协议及端口)
  54. * @var string
  55. */
  56. protected $domain;
  57. /**
  58. * 子域名
  59. * @var string
  60. */
  61. protected $subDomain;
  62. /**
  63. * 泛域名
  64. * @var string
  65. */
  66. protected $panDomain;
  67. /**
  68. * 当前URL地址
  69. * @var string
  70. */
  71. protected $url;
  72. /**
  73. * 基础URL
  74. * @var string
  75. */
  76. protected $baseUrl;
  77. /**
  78. * 当前执行的文件
  79. * @var string
  80. */
  81. protected $baseFile;
  82. /**
  83. * 访问的ROOT地址
  84. * @var string
  85. */
  86. protected $root;
  87. /**
  88. * pathinfo
  89. * @var string
  90. */
  91. protected $pathinfo;
  92. /**
  93. * pathinfo(不含后缀)
  94. * @var string
  95. */
  96. protected $path;
  97. /**
  98. * 当前路由信息
  99. * @var array
  100. */
  101. protected $routeInfo = [];
  102. /**
  103. * 当前调度信息
  104. * @var \think\route\Dispatch
  105. */
  106. protected $dispatch;
  107. /**
  108. * 当前模块名
  109. * @var string
  110. */
  111. protected $module;
  112. /**
  113. * 当前控制器名
  114. * @var string
  115. */
  116. protected $controller;
  117. /**
  118. * 当前操作名
  119. * @var string
  120. */
  121. protected $action;
  122. /**
  123. * 当前语言集
  124. * @var string
  125. */
  126. protected $langset;
  127. /**
  128. * 当前请求参数
  129. * @var array
  130. */
  131. protected $param = [];
  132. /**
  133. * 当前GET参数
  134. * @var array
  135. */
  136. protected $get = [];
  137. /**
  138. * 当前POST参数
  139. * @var array
  140. */
  141. protected $post = [];
  142. /**
  143. * 当前REQUEST参数
  144. * @var array
  145. */
  146. protected $request = [];
  147. /**
  148. * 当前ROUTE参数
  149. * @var array
  150. */
  151. protected $route = [];
  152. /**
  153. * 当前PUT参数
  154. * @var array
  155. */
  156. protected $put;
  157. /**
  158. * 当前SESSION参数
  159. * @var array
  160. */
  161. protected $session = [];
  162. /**
  163. * 当前FILE参数
  164. * @var array
  165. */
  166. protected $file = [];
  167. /**
  168. * 当前COOKIE参数
  169. * @var array
  170. */
  171. protected $cookie = [];
  172. /**
  173. * 当前SERVER参数
  174. * @var array
  175. */
  176. protected $server = [];
  177. /**
  178. * 当前ENV参数
  179. * @var array
  180. */
  181. protected $env = [];
  182. /**
  183. * 当前HEADER参数
  184. * @var array
  185. */
  186. protected $header = [];
  187. /**
  188. * 资源类型定义
  189. * @var array
  190. */
  191. protected $mimeType = [
  192. 'xml' => 'application/xml,text/xml,application/x-xml',
  193. 'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
  194. 'js' => 'text/javascript,application/javascript,application/x-javascript',
  195. 'css' => 'text/css',
  196. 'rss' => 'application/rss+xml',
  197. 'yaml' => 'application/x-yaml,text/yaml',
  198. 'atom' => 'application/atom+xml',
  199. 'pdf' => 'application/pdf',
  200. 'text' => 'text/plain',
  201. 'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*',
  202. 'csv' => 'text/csv',
  203. 'html' => 'text/html,application/xhtml+xml,*/*',
  204. ];
  205. /**
  206. * 当前请求内容
  207. * @var string
  208. */
  209. protected $content;
  210. /**
  211. * 全局过滤规则
  212. * @var array
  213. */
  214. protected $filter;
  215. /**
  216. * 扩展方法
  217. * @var array
  218. */
  219. protected $hook = [];
  220. /**
  221. * php://input内容
  222. * @var string
  223. */
  224. protected $input;
  225. /**
  226. * 请求缓存
  227. * @var array
  228. */
  229. protected $cache;
  230. /**
  231. * 缓存是否检查
  232. * @var bool
  233. */
  234. protected $isCheckCache;
  235. /**
  236. * 请求安全Key
  237. * @var string
  238. */
  239. protected $secureKey;
  240. /**
  241. * 是否合并Param
  242. * @var bool
  243. */
  244. protected $mergeParam = false;
  245. /**
  246. * 架构函数
  247. * @access public
  248. * @param array $options 参数
  249. */
  250. public function __construct(array $options = [])
  251. {
  252. $this->init($options);
  253. // 保存 php://input
  254. $this->input = file_get_contents('php://input');
  255. }
  256. public function init(array $options = [])
  257. {
  258. $this->config = array_merge($this->config, $options);
  259. if (is_null($this->filter) && !empty($this->config['default_filter'])) {
  260. $this->filter = $this->config['default_filter'];
  261. }
  262. }
  263. public function config($name = null)
  264. {
  265. if (is_null($name)) {
  266. return $this->config;
  267. }
  268. return isset($this->config[$name]) ? $this->config[$name] : null;
  269. }
  270. public static function __make(App $app, Config $config)
  271. {
  272. $request = new static($config->pull('app'));
  273. $request->server = $_SERVER;
  274. $request->env = $app['env']->get();
  275. return $request;
  276. }
  277. public function __call($method, $args)
  278. {
  279. if (array_key_exists($method, $this->hook)) {
  280. array_unshift($args, $this);
  281. return call_user_func_array($this->hook[$method], $args);
  282. }
  283. throw new Exception('method not exists:' . static::class . '->' . $method);
  284. }
  285. /**
  286. * Hook 方法注入
  287. * @access public
  288. * @param string|array $method 方法名
  289. * @param mixed $callback callable
  290. * @return void
  291. */
  292. public function hook($method, $callback = null)
  293. {
  294. if (is_array($method)) {
  295. $this->hook = array_merge($this->hook, $method);
  296. } else {
  297. $this->hook[$method] = $callback;
  298. }
  299. }
  300. /**
  301. * 创建一个URL请求
  302. * @access public
  303. * @param string $uri URL地址
  304. * @param string $method 请求类型
  305. * @param array $params 请求参数
  306. * @param array $cookie
  307. * @param array $files
  308. * @param array $server
  309. * @param string $content
  310. * @return \think\Request
  311. */
  312. public function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
  313. {
  314. $server['PATH_INFO'] = '';
  315. $server['REQUEST_METHOD'] = strtoupper($method);
  316. $info = parse_url($uri);
  317. if (isset($info['host'])) {
  318. $server['SERVER_NAME'] = $info['host'];
  319. $server['HTTP_HOST'] = $info['host'];
  320. }
  321. if (isset($info['scheme'])) {
  322. if ('https' === $info['scheme']) {
  323. $server['HTTPS'] = 'on';
  324. $server['SERVER_PORT'] = 443;
  325. } else {
  326. unset($server['HTTPS']);
  327. $server['SERVER_PORT'] = 80;
  328. }
  329. }
  330. if (isset($info['port'])) {
  331. $server['SERVER_PORT'] = $info['port'];
  332. $server['HTTP_HOST'] = $server['HTTP_HOST'] . ':' . $info['port'];
  333. }
  334. if (isset($info['user'])) {
  335. $server['PHP_AUTH_USER'] = $info['user'];
  336. }
  337. if (isset($info['pass'])) {
  338. $server['PHP_AUTH_PW'] = $info['pass'];
  339. }
  340. if (!isset($info['path'])) {
  341. $info['path'] = '/';
  342. }
  343. $options = [];
  344. $queryString = '';
  345. $options[strtolower($method)] = $params;
  346. if (isset($info['query'])) {
  347. parse_str(html_entity_decode($info['query']), $query);
  348. if (!empty($params)) {
  349. $params = array_replace($query, $params);
  350. $queryString = http_build_query($params, '', '&');
  351. } else {
  352. $params = $query;
  353. $queryString = $info['query'];
  354. }
  355. } elseif (!empty($params)) {
  356. $queryString = http_build_query($params, '', '&');
  357. }
  358. if ($queryString) {
  359. parse_str($queryString, $get);
  360. $options['get'] = isset($options['get']) ? array_merge($get, $options['get']) : $get;
  361. }
  362. $server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
  363. $server['QUERY_STRING'] = $queryString;
  364. $options['cookie'] = $cookie;
  365. $options['param'] = $params;
  366. $options['file'] = $files;
  367. $options['server'] = $server;
  368. $options['url'] = $server['REQUEST_URI'];
  369. $options['baseUrl'] = $info['path'];
  370. $options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');
  371. $options['method'] = $server['REQUEST_METHOD'];
  372. $options['domain'] = isset($info['scheme']) ? $info['scheme'] . '://' . $server['HTTP_HOST'] : '';
  373. $options['content'] = $content;
  374. $request = new static();
  375. foreach ($options as $name => $item) {
  376. if (property_exists($request, $name)) {
  377. $request->$name = $item;
  378. }
  379. }
  380. return $request;
  381. }
  382. /**
  383. * 获取当前包含协议、端口的域名
  384. * @access public
  385. * @param bool $port 是否需要去除端口号
  386. * @return string
  387. */
  388. public function domain($port = false)
  389. {
  390. return $this->scheme() . '://' . $this->host($port);
  391. }
  392. /**
  393. * 获取当前根域名
  394. * @access public
  395. * @return string
  396. */
  397. public function rootDomain()
  398. {
  399. $root = $this->config['url_domain_root'];
  400. if (!$root) {
  401. $item = explode('.', $this->host(true));
  402. $count = count($item);
  403. $root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0];
  404. }
  405. return $root;
  406. }
  407. /**
  408. * 获取当前子域名
  409. * @access public
  410. * @return string
  411. */
  412. public function subDomain()
  413. {
  414. if (is_null($this->subDomain)) {
  415. // 获取当前主域名
  416. $rootDomain = $this->config['url_domain_root'];
  417. if ($rootDomain) {
  418. // 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置
  419. $domain = explode('.', rtrim(stristr($this->host(true), $rootDomain, true), '.'));
  420. } else {
  421. $domain = explode('.', $this->host(true), -2);
  422. }
  423. $this->subDomain = implode('.', $domain);
  424. }
  425. return $this->subDomain;
  426. }
  427. /**
  428. * 设置当前泛域名的值
  429. * @access public
  430. * @param string $domain 域名
  431. * @return $this
  432. */
  433. public function setPanDomain($domain)
  434. {
  435. $this->panDomain = $domain;
  436. return $this;
  437. }
  438. /**
  439. * 获取当前泛域名的值
  440. * @access public
  441. * @return string
  442. */
  443. public function panDomain()
  444. {
  445. return $this->panDomain;
  446. }
  447. /**
  448. * 设置当前完整URL 包括QUERY_STRING
  449. * @access public
  450. * @param string $url URL
  451. * @return $this
  452. */
  453. public function setUrl($url)
  454. {
  455. $this->url = $url;
  456. return $this;
  457. }
  458. /**
  459. * 获取当前完整URL 包括QUERY_STRING
  460. * @access public
  461. * @param bool $complete 是否包含域名
  462. * @return string
  463. */
  464. public function url($complete = false)
  465. {
  466. if (!$this->url) {
  467. if ($this->isCli()) {
  468. $this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  469. } elseif ($this->server('HTTP_X_REWRITE_URL')) {
  470. $this->url = $this->server('HTTP_X_REWRITE_URL');
  471. } elseif ($this->server('REQUEST_URI')) {
  472. $this->url = $this->server('REQUEST_URI');
  473. } elseif ($this->server('ORIG_PATH_INFO')) {
  474. $this->url = $this->server('ORIG_PATH_INFO') . (!empty($this->server('QUERY_STRING')) ? '?' . $this->server('QUERY_STRING') : '');
  475. } else {
  476. $this->url = '';
  477. }
  478. }
  479. return $complete ? $this->domain() . $this->url : $this->url;
  480. }
  481. /**
  482. * 设置当前完整URL 不包括QUERY_STRING
  483. * @access public
  484. * @param string $url URL
  485. * @return $this
  486. */
  487. public function setBaseUrl($url)
  488. {
  489. $this->baseUrl = $url;
  490. return $this;
  491. }
  492. /**
  493. * 获取当前URL 不含QUERY_STRING
  494. * @access public
  495. * @param bool $domain 是否包含域名
  496. * @return string|$this
  497. */
  498. public function baseUrl($domain = false)
  499. {
  500. if (!$this->baseUrl) {
  501. $str = $this->url();
  502. $this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str;
  503. }
  504. return $domain ? $this->domain() . $this->baseUrl : $this->baseUrl;
  505. }
  506. /**
  507. * 设置或获取当前执行的文件 SCRIPT_NAME
  508. * @access public
  509. * @param bool $domain 是否包含域名
  510. * @return string|$this
  511. */
  512. public function baseFile($domain = false)
  513. {
  514. if (!$this->baseFile) {
  515. $url = '';
  516. if (!$this->isCli()) {
  517. $script_name = basename($this->server('SCRIPT_FILENAME'));
  518. if (basename($this->server('SCRIPT_NAME')) === $script_name) {
  519. $url = $this->server('SCRIPT_NAME');
  520. } elseif (basename($this->server('PHP_SELF')) === $script_name) {
  521. $url = $this->server('PHP_SELF');
  522. } elseif (basename($this->server('ORIG_SCRIPT_NAME')) === $script_name) {
  523. $url = $this->server('ORIG_SCRIPT_NAME');
  524. } elseif (($pos = strpos($this->server('PHP_SELF'), '/' . $script_name)) !== false) {
  525. $url = substr($this->server('SCRIPT_NAME'), 0, $pos) . '/' . $script_name;
  526. } elseif ($this->server('DOCUMENT_ROOT') && strpos($this->server('SCRIPT_FILENAME'), $this->server('DOCUMENT_ROOT')) === 0) {
  527. $url = str_replace('\\', '/', str_replace($this->server('DOCUMENT_ROOT'), '', $this->server('SCRIPT_FILENAME')));
  528. }
  529. }
  530. $this->baseFile = $url;
  531. }
  532. return $domain ? $this->domain() . $this->baseFile : $this->baseFile;
  533. }
  534. /**
  535. * 设置URL访问根地址
  536. * @access public
  537. * @param string $url URL地址
  538. * @return string|$this
  539. */
  540. public function setRoot($url = null)
  541. {
  542. $this->root = $url;
  543. return $this;
  544. }
  545. /**
  546. * 获取URL访问根地址
  547. * @access public
  548. * @param bool $domain 是否包含域名
  549. * @return string|$this
  550. */
  551. public function root($domain = false)
  552. {
  553. if (!$this->root) {
  554. $file = $this->baseFile();
  555. if ($file && 0 !== strpos($this->url(), $file)) {
  556. $file = str_replace('\\', '/', dirname($file));
  557. }
  558. $this->root = rtrim($file, '/');
  559. }
  560. return $domain ? $this->domain() . $this->root : $this->root;
  561. }
  562. /**
  563. * 获取URL访问根目录
  564. * @access public
  565. * @return string
  566. */
  567. public function rootUrl()
  568. {
  569. $base = $this->root();
  570. $root = strpos($base, '.') ? ltrim(dirname($base), DIRECTORY_SEPARATOR) : $base;
  571. if ('' != $root) {
  572. $root = '/' . ltrim($root, '/');
  573. }
  574. return $root;
  575. }
  576. public function setPathinfo($pathinfo)
  577. {
  578. $this->pathinfo = $pathinfo;
  579. return $this;
  580. }
  581. /**
  582. * 获取当前请求URL的pathinfo信息(含URL后缀)
  583. * @access public
  584. * @return string
  585. */
  586. public function pathinfo()
  587. {
  588. if (is_null($this->pathinfo)) {
  589. if (isset($_GET[$this->config['var_pathinfo']])) {
  590. // 判断URL里面是否有兼容模式参数
  591. $pathinfo = $_GET[$this->config['var_pathinfo']];
  592. unset($_GET[$this->config['var_pathinfo']]);
  593. } elseif ($this->isCli()) {
  594. // CLI模式下 index.php module/controller/action/params/...
  595. $pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  596. } elseif ('cli-server' == PHP_SAPI) {
  597. $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI');
  598. } elseif ($this->server('PATH_INFO')) {
  599. $pathinfo = $this->server('PATH_INFO');
  600. }
  601. // 分析PATHINFO信息
  602. if (!isset($pathinfo)) {
  603. foreach ($this->config['pathinfo_fetch'] as $type) {
  604. if ($this->server($type)) {
  605. $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ?
  606. substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type);
  607. break;
  608. }
  609. }
  610. }
  611. $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/');
  612. }
  613. return $this->pathinfo;
  614. }
  615. /**
  616. * 获取当前请求URL的pathinfo信息(不含URL后缀)
  617. * @access public
  618. * @return string
  619. */
  620. public function path()
  621. {
  622. if (is_null($this->path)) {
  623. $suffix = $this->config['url_html_suffix'];
  624. $pathinfo = $this->pathinfo();
  625. if (false === $suffix) {
  626. // 禁止伪静态访问
  627. $this->path = $pathinfo;
  628. } elseif ($suffix) {
  629. // 去除正常的URL后缀
  630. $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
  631. } else {
  632. // 允许任何后缀访问
  633. $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
  634. }
  635. }
  636. return $this->path;
  637. }
  638. /**
  639. * 当前URL的访问后缀
  640. * @access public
  641. * @return string
  642. */
  643. public function ext()
  644. {
  645. return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
  646. }
  647. /**
  648. * 获取当前请求的时间
  649. * @access public
  650. * @param bool $float 是否使用浮点类型
  651. * @return integer|float
  652. */
  653. public function time($float = false)
  654. {
  655. return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME');
  656. }
  657. /**
  658. * 当前请求的资源类型
  659. * @access public
  660. * @return false|string
  661. */
  662. public function type()
  663. {
  664. $accept = $this->server('HTTP_ACCEPT');
  665. if (empty($accept)) {
  666. return false;
  667. }
  668. foreach ($this->mimeType as $key => $val) {
  669. $array = explode(',', $val);
  670. foreach ($array as $k => $v) {
  671. if (stristr($accept, $v)) {
  672. return $key;
  673. }
  674. }
  675. }
  676. return false;
  677. }
  678. /**
  679. * 设置资源类型
  680. * @access public
  681. * @param string|array $type 资源类型名
  682. * @param string $val 资源类型
  683. * @return void
  684. */
  685. public function mimeType($type, $val = '')
  686. {
  687. if (is_array($type)) {
  688. $this->mimeType = array_merge($this->mimeType, $type);
  689. } else {
  690. $this->mimeType[$type] = $val;
  691. }
  692. }
  693. /**
  694. * 当前的请求类型
  695. * @access public
  696. * @param bool $origin 是否获取原始请求类型
  697. * @return string
  698. */
  699. public function method($origin = false)
  700. {
  701. if ($origin) {
  702. // 获取原始请求类型
  703. return $this->server('REQUEST_METHOD') ?: 'GET';
  704. } elseif (!$this->method) {
  705. if (isset($_POST[$this->config['var_method']])) {
  706. $method = strtolower($_POST[$this->config['var_method']]);
  707. if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
  708. $this->method = strtoupper($method);
  709. $this->{$method} = $_POST;
  710. } else {
  711. $this->method = 'POST';
  712. }
  713. unset($_POST[$this->config['var_method']]);
  714. } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
  715. $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
  716. } else {
  717. $this->method = $this->server('REQUEST_METHOD') ?: 'GET';
  718. }
  719. }
  720. return $this->method;
  721. }
  722. /**
  723. * 是否为GET请求
  724. * @access public
  725. * @return bool
  726. */
  727. public function isGet()
  728. {
  729. return $this->method() == 'GET';
  730. }
  731. /**
  732. * 是否为POST请求
  733. * @access public
  734. * @return bool
  735. */
  736. public function isPost()
  737. {
  738. return $this->method() == 'POST';
  739. }
  740. /**
  741. * 是否为PUT请求
  742. * @access public
  743. * @return bool
  744. */
  745. public function isPut()
  746. {
  747. return $this->method() == 'PUT';
  748. }
  749. /**
  750. * 是否为DELTE请求
  751. * @access public
  752. * @return bool
  753. */
  754. public function isDelete()
  755. {
  756. return $this->method() == 'DELETE';
  757. }
  758. /**
  759. * 是否为HEAD请求
  760. * @access public
  761. * @return bool
  762. */
  763. public function isHead()
  764. {
  765. return $this->method() == 'HEAD';
  766. }
  767. /**
  768. * 是否为PATCH请求
  769. * @access public
  770. * @return bool
  771. */
  772. public function isPatch()
  773. {
  774. return $this->method() == 'PATCH';
  775. }
  776. /**
  777. * 是否为OPTIONS请求
  778. * @access public
  779. * @return bool
  780. */
  781. public function isOptions()
  782. {
  783. return $this->method() == 'OPTIONS';
  784. }
  785. /**
  786. * 是否为cli
  787. * @access public
  788. * @return bool
  789. */
  790. public function isCli()
  791. {
  792. return PHP_SAPI == 'cli';
  793. }
  794. /**
  795. * 是否为cgi
  796. * @access public
  797. * @return bool
  798. */
  799. public function isCgi()
  800. {
  801. return strpos(PHP_SAPI, 'cgi') === 0;
  802. }
  803. /**
  804. * 获取当前请求的参数
  805. * @access public
  806. * @param mixed $name 变量名
  807. * @param mixed $default 默认值
  808. * @param string|array $filter 过滤方法
  809. * @return mixed
  810. */
  811. public function param($name = '', $default = null, $filter = '')
  812. {
  813. if (!$this->mergeParam) {
  814. $method = $this->method(true);
  815. // 自动获取请求变量
  816. switch ($method) {
  817. case 'POST':
  818. $vars = $this->post(false);
  819. break;
  820. case 'PUT':
  821. case 'DELETE':
  822. case 'PATCH':
  823. $vars = $this->put(false);
  824. break;
  825. default:
  826. $vars = [];
  827. }
  828. // 当前请求参数和URL地址中的参数合并
  829. $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false));
  830. $this->mergeParam = true;
  831. }
  832. if (true === $name) {
  833. // 获取包含文件上传信息的数组
  834. $file = $this->file();
  835. $data = is_array($file) ? array_merge($this->param, $file) : $this->param;
  836. return $this->input($data, '', $default, $filter);
  837. }
  838. return $this->input($this->param, $name, $default, $filter);
  839. }
  840. /**
  841. * 设置路由变量
  842. * @access public
  843. * @param array $route 路由变量
  844. * @return $this
  845. */
  846. public function setRouteVars(array $route)
  847. {
  848. $this->route = array_merge($this->route, $route);
  849. return $this;
  850. }
  851. /**
  852. * 获取路由参数
  853. * @access public
  854. * @param string|false $name 变量名
  855. * @param mixed $default 默认值
  856. * @param string|array $filter 过滤方法
  857. * @return mixed
  858. */
  859. public function route($name = '', $default = null, $filter = '')
  860. {
  861. return $this->input($this->route, $name, $default, $filter);
  862. }
  863. /**
  864. * 获取GET参数
  865. * @access public
  866. * @param string|false $name 变量名
  867. * @param mixed $default 默认值
  868. * @param string|array $filter 过滤方法
  869. * @return mixed
  870. */
  871. public function get($name = '', $default = null, $filter = '')
  872. {
  873. if (empty($this->get)) {
  874. $this->get = $_GET;
  875. }
  876. return $this->input($this->get, $name, $default, $filter);
  877. }
  878. /**
  879. * 获取POST参数
  880. * @access public
  881. * @param string|false $name 变量名
  882. * @param mixed $default 默认值
  883. * @param string|array $filter 过滤方法
  884. * @return mixed
  885. */
  886. public function post($name = '', $default = null, $filter = '')
  887. {
  888. if (empty($this->post)) {
  889. $this->post = !empty($_POST) ? $_POST : $this->getInputData($this->input);
  890. }
  891. return $this->input($this->post, $name, $default, $filter);
  892. }
  893. /**
  894. * 获取PUT参数
  895. * @access public
  896. * @param string|false $name 变量名
  897. * @param mixed $default 默认值
  898. * @param string|array $filter 过滤方法
  899. * @return mixed
  900. */
  901. public function put($name = '', $default = null, $filter = '')
  902. {
  903. if (is_null($this->put)) {
  904. $this->put = $this->getInputData($this->input);
  905. }
  906. return $this->input($this->put, $name, $default, $filter);
  907. }
  908. protected function getInputData($content)
  909. {
  910. if (false !== strpos($this->contentType(), 'application/json') || 0 === strpos($content, '{"')) {
  911. return (array) json_decode($content, true);
  912. } elseif (strpos($content, '=')) {
  913. parse_str($content, $data);
  914. return $data;
  915. }
  916. return [];
  917. }
  918. /**
  919. * 获取DELETE参数
  920. * @access public
  921. * @param string|false $name 变量名
  922. * @param mixed $default 默认值
  923. * @param string|array $filter 过滤方法
  924. * @return mixed
  925. */
  926. public function delete($name = '', $default = null, $filter = '')
  927. {
  928. return $this->put($name, $default, $filter);
  929. }
  930. /**
  931. * 获取PATCH参数
  932. * @access public
  933. * @param string|false $name 变量名
  934. * @param mixed $default 默认值
  935. * @param string|array $filter 过滤方法
  936. * @return mixed
  937. */
  938. public function patch($name = '', $default = null, $filter = '')
  939. {
  940. return $this->put($name, $default, $filter);
  941. }
  942. /**
  943. * 获取request变量
  944. * @access public
  945. * @param string|false $name 变量名
  946. * @param mixed $default 默认值
  947. * @param string|array $filter 过滤方法
  948. * @return mixed
  949. */
  950. public function request($name = '', $default = null, $filter = '')
  951. {
  952. if (empty($this->request)) {
  953. $this->request = $_REQUEST;
  954. }
  955. return $this->input($this->request, $name, $default, $filter);
  956. }
  957. /**
  958. * 获取session数据
  959. * @access public
  960. * @param string $name 数据名称
  961. * @param string $default 默认值
  962. * @return mixed
  963. */
  964. public function session($name = '', $default = null)
  965. {
  966. if (empty($this->session)) {
  967. $this->session = Session::get();
  968. }
  969. if ('' === $name) {
  970. return $this->session;
  971. }
  972. $data = $this->getData($this->session, $name);
  973. return is_null($data) ? $default : $data;
  974. }
  975. /**
  976. * 获取cookie参数
  977. * @access public
  978. * @param string $name 变量名
  979. * @param string $default 默认值
  980. * @param string|array $filter 过滤方法
  981. * @return mixed
  982. */
  983. public function cookie($name = '', $default = null, $filter = '')
  984. {
  985. if (empty($this->cookie)) {
  986. $this->cookie = Cookie::get();
  987. }
  988. if (!empty($name)) {
  989. $data = Cookie::has($name) ? Cookie::get($name) : $default;
  990. } else {
  991. $data = $this->cookie;
  992. }
  993. // 解析过滤器
  994. $filter = $this->getFilter($filter, $default);
  995. if (is_array($data)) {
  996. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  997. reset($data);
  998. } else {
  999. $this->filterValue($data, $name, $filter);
  1000. }
  1001. return $data;
  1002. }
  1003. /**
  1004. * 获取server参数
  1005. * @access public
  1006. * @param string $name 数据名称
  1007. * @param string $default 默认值
  1008. * @return mixed
  1009. */
  1010. public function server($name = '', $default = null)
  1011. {
  1012. if (empty($name)) {
  1013. return $this->server;
  1014. } else {
  1015. $name = strtoupper($name);
  1016. }
  1017. return isset($this->server[$name]) ? $this->server[$name] : $default;
  1018. }
  1019. /**
  1020. * 获取上传的文件信息
  1021. * @access public
  1022. * @param string $name 名称
  1023. * @return null|array|\think\File
  1024. */
  1025. public function file($name = '')
  1026. {
  1027. if (empty($this->file)) {
  1028. $this->file = isset($_FILES) ? $_FILES : [];
  1029. }
  1030. $files = $this->file;
  1031. if (!empty($files)) {
  1032. if (strpos($name, '.')) {
  1033. list($name, $sub) = explode('.', $name);
  1034. }
  1035. // 处理上传文件
  1036. $array = $this->dealUploadFile($files, $name);
  1037. if ('' === $name) {
  1038. // 获取全部文件
  1039. return $array;
  1040. } elseif (isset($sub) && isset($array[$name][$sub])) {
  1041. return $array[$name][$sub];
  1042. } elseif (isset($array[$name])) {
  1043. return $array[$name];
  1044. }
  1045. }
  1046. return;
  1047. }
  1048. protected function dealUploadFile($files, $name)
  1049. {
  1050. $array = [];
  1051. foreach ($files as $key => $file) {
  1052. if ($file instanceof File) {
  1053. $array[$key] = $file;
  1054. } elseif (is_array($file['name'])) {
  1055. $item = [];
  1056. $keys = array_keys($file);
  1057. $count = count($file['name']);
  1058. for ($i = 0; $i < $count; $i++) {
  1059. if ($file['error'][$i] > 0) {
  1060. if ($name == $key) {
  1061. $this->throwUploadFileError($file['error'][$i]);
  1062. } else {
  1063. continue;
  1064. }
  1065. }
  1066. $temp['key'] = $key;
  1067. foreach ($keys as $_key) {
  1068. $temp[$_key] = $file[$_key][$i];
  1069. }
  1070. $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
  1071. }
  1072. $array[$key] = $item;
  1073. } else {
  1074. if ($file['error'] > 0) {
  1075. if ($key == $name) {
  1076. $this->throwUploadFileError($file['error']);
  1077. } else {
  1078. continue;
  1079. }
  1080. }
  1081. $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
  1082. }
  1083. }
  1084. return $array;
  1085. }
  1086. protected function throwUploadFileError($error)
  1087. {
  1088. static $fileUploadErrors = [
  1089. 1 => 'upload File size exceeds the maximum value',
  1090. 2 => 'upload File size exceeds the maximum value',
  1091. 3 => 'only the portion of file is uploaded',
  1092. 4 => 'no file to uploaded',
  1093. 6 => 'upload temp dir not found',
  1094. 7 => 'file write error',
  1095. ];
  1096. $msg = $fileUploadErrors[$error];
  1097. throw new Exception($msg);
  1098. }
  1099. /**
  1100. * 获取环境变量
  1101. * @access public
  1102. * @param string $name 数据名称
  1103. * @param string $default 默认值
  1104. * @return mixed
  1105. */
  1106. public function env($name = '', $default = null)
  1107. {
  1108. if (empty($name)) {
  1109. return $this->env;
  1110. } else {
  1111. $name = strtoupper($name);
  1112. }
  1113. return isset($this->env[$name]) ? $this->env[$name] : $default;
  1114. }
  1115. /**
  1116. * 获取当前的Header
  1117. * @access public
  1118. * @param string $name header名称
  1119. * @param string $default 默认值
  1120. * @return string|array
  1121. */
  1122. public function header($name = '', $default = null)
  1123. {
  1124. if (empty($this->header)) {
  1125. $header = [];
  1126. if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
  1127. $header = $result;
  1128. } else {
  1129. $server = $this->server;
  1130. foreach ($server as $key => $val) {
  1131. if (0 === strpos($key, 'HTTP_')) {
  1132. $key = str_replace('_', '-', strtolower(substr($key, 5)));
  1133. $header[$key] = $val;
  1134. }
  1135. }
  1136. if (isset($server['CONTENT_TYPE'])) {
  1137. $header['content-type'] = $server['CONTENT_TYPE'];
  1138. }
  1139. if (isset($server['CONTENT_LENGTH'])) {
  1140. $header['content-length'] = $server['CONTENT_LENGTH'];
  1141. }
  1142. }
  1143. $this->header = array_change_key_case($header);
  1144. }
  1145. if ('' === $name) {
  1146. return $this->header;
  1147. }
  1148. $name = str_replace('_', '-', strtolower($name));
  1149. return isset($this->header[$name]) ? $this->header[$name] : $default;
  1150. }
  1151. /**
  1152. * 递归重置数组指针
  1153. * @access public
  1154. * @param array $data 数据源
  1155. * @return void
  1156. */
  1157. public function arrayReset(array &$data)
  1158. {
  1159. foreach ($data as &$value) {
  1160. if (is_array($value)) {
  1161. $this->arrayReset($value);
  1162. }
  1163. }
  1164. reset($data);
  1165. }
  1166. /**
  1167. * 获取变量 支持过滤和默认值
  1168. * @access public
  1169. * @param array $data 数据源
  1170. * @param string|false $name 字段名
  1171. * @param mixed $default 默认值
  1172. * @param string|array $filter 过滤函数
  1173. * @return mixed
  1174. */
  1175. public function input($data = [], $name = '', $default = null, $filter = '')
  1176. {
  1177. if (false === $name) {
  1178. // 获取原始数据
  1179. return $data;
  1180. }
  1181. $name = (string) $name;
  1182. if ('' != $name) {
  1183. // 解析name
  1184. if (strpos($name, '/')) {
  1185. list($name, $type) = explode('/', $name);
  1186. }
  1187. $data = $this->getData($data, $name);
  1188. if (is_null($data)) {
  1189. return $default;
  1190. }
  1191. if (is_object($data)) {
  1192. return $data;
  1193. }
  1194. }
  1195. // 解析过滤器
  1196. $filter = $this->getFilter($filter, $default);
  1197. if (is_array($data)) {
  1198. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  1199. if (version_compare(PHP_VERSION, '7.1.0', '<')) {
  1200. // 恢复PHP版本低于 7.1 时 array_walk_recursive 中消耗的内部指针
  1201. $this->arrayReset($data);
  1202. }
  1203. } else {
  1204. $this->filterValue($data, $name, $filter);
  1205. }
  1206. if (isset($type) && $data !== $default) {
  1207. // 强制类型转换
  1208. $this->typeCast($data, $type);
  1209. }
  1210. return $data;
  1211. }
  1212. /**
  1213. * 获取数据
  1214. * @access public
  1215. * @param array $data 数据源
  1216. * @param string|false $name 字段名
  1217. * @return mixed
  1218. */
  1219. protected function getData(array $data, $name)
  1220. {
  1221. foreach (explode('.', $name) as $val) {
  1222. if (isset($data[$val])) {
  1223. $data = $data[$val];
  1224. } else {
  1225. return;
  1226. }
  1227. }
  1228. return $data;
  1229. }
  1230. /**
  1231. * 设置或获取当前的过滤规则
  1232. * @access public
  1233. * @param mixed $filter 过滤规则
  1234. * @return mixed
  1235. */
  1236. public function filter($filter = null)
  1237. {
  1238. if (is_null($filter)) {
  1239. return $this->filter;
  1240. }
  1241. $this->filter = $filter;
  1242. }
  1243. protected function getFilter($filter, $default)
  1244. {
  1245. if (is_null($filter)) {
  1246. $filter = [];
  1247. } else {
  1248. $filter = $filter ?: $this->filter;
  1249. if (is_string($filter) && false === strpos($filter, '/')) {
  1250. $filter = explode(',', $filter);
  1251. } else {
  1252. $filter = (array) $filter;
  1253. }
  1254. }
  1255. $filter[] = $default;
  1256. return $filter;
  1257. }
  1258. /**
  1259. * 递归过滤给定的值
  1260. * @access public
  1261. * @param mixed $value 键值
  1262. * @param mixed $key 键名
  1263. * @param array $filters 过滤方法+默认值
  1264. * @return mixed
  1265. */
  1266. private function filterValue(&$value, $key, $filters)
  1267. {
  1268. $default = array_pop($filters);
  1269. foreach ($filters as $filter) {
  1270. if (is_callable($filter)) {
  1271. // 调用函数或者方法过滤
  1272. $value = call_user_func($filter, $value);
  1273. } elseif (is_scalar($value)) {
  1274. if (false !== strpos($filter, '/')) {
  1275. // 正则过滤
  1276. if (!preg_match($filter, $value)) {
  1277. // 匹配不成功返回默认值
  1278. $value = $default;
  1279. break;
  1280. }
  1281. } elseif (!empty($filter)) {
  1282. // filter函数不存在时, 则使用filter_var进行过滤
  1283. // filter为非整形值时, 调用filter_id取得过滤id
  1284. $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
  1285. if (false === $value) {
  1286. $value = $default;
  1287. break;
  1288. }
  1289. }
  1290. }
  1291. }
  1292. return $value;
  1293. }
  1294. /**
  1295. * 强制类型转换
  1296. * @access public
  1297. * @param string $data
  1298. * @param string $type
  1299. * @return mixed
  1300. */
  1301. private function typeCast(&$data, $type)
  1302. {
  1303. switch (strtolower($type)) {
  1304. // 数组
  1305. case 'a':
  1306. $data = (array) $data;
  1307. break;
  1308. // 数字
  1309. case 'd':
  1310. $data = (int) $data;
  1311. break;
  1312. // 浮点
  1313. case 'f':
  1314. $data = (float) $data;
  1315. break;
  1316. // 布尔
  1317. case 'b':
  1318. $data = (boolean) $data;
  1319. break;
  1320. // 字符串
  1321. case 's':
  1322. if (is_scalar($data)) {
  1323. $data = (string) $data;
  1324. } else {
  1325. throw new \InvalidArgumentException('variable type error:' . gettype($data));
  1326. }
  1327. break;
  1328. }
  1329. }
  1330. /**
  1331. * 是否存在某个请求参数
  1332. * @access public
  1333. * @param string $name 变量名
  1334. * @param string $type 变量类型
  1335. * @param bool $checkEmpty 是否检测空值
  1336. * @return mixed
  1337. */
  1338. public function has($name, $type = 'param', $checkEmpty = false)
  1339. {
  1340. if (!in_array($type, ['param', 'get', 'post', 'request', 'put', 'patch', 'file', 'session', 'cookie', 'env', 'header', 'route'])) {
  1341. return false;
  1342. }
  1343. if (empty($this->$type)) {
  1344. $param = $this->$type();
  1345. } else {
  1346. $param = $this->$type;
  1347. }
  1348. // 按.拆分成多维数组进行判断
  1349. foreach (explode('.', $name) as $val) {
  1350. if (isset($param[$val])) {
  1351. $param = $param[$val];
  1352. } else {
  1353. return false;
  1354. }
  1355. }
  1356. return ($checkEmpty && '' === $param) ? false : true;
  1357. }
  1358. /**
  1359. * 获取指定的参数
  1360. * @access public
  1361. * @param string|array $name 变量名
  1362. * @param string $type 变量类型
  1363. * @return mixed
  1364. */
  1365. public function only($name, $type = 'param')
  1366. {
  1367. $param = $this->$type();
  1368. if (is_string($name)) {
  1369. $name = explode(',', $name);
  1370. }
  1371. $item = [];
  1372. foreach ($name as $key => $val) {
  1373. if (is_int($key)) {
  1374. $default = null;
  1375. $key = $val;
  1376. } else {
  1377. $default = $val;
  1378. }
  1379. if (isset($param[$key])) {
  1380. $item[$key] = $param[$key];
  1381. } elseif (isset($default)) {
  1382. $item[$key] = $default;
  1383. }
  1384. }
  1385. return $item;
  1386. }
  1387. /**
  1388. * 排除指定参数获取
  1389. * @access public
  1390. * @param string|array $name 变量名
  1391. * @param string $type 变量类型
  1392. * @return mixed
  1393. */
  1394. public function except($name, $type = 'param')
  1395. {
  1396. $param = $this->$type();
  1397. if (is_string($name)) {
  1398. $name = explode(',', $name);
  1399. }
  1400. foreach ($name as $key) {
  1401. if (isset($param[$key])) {
  1402. unset($param[$key]);
  1403. }
  1404. }
  1405. return $param;
  1406. }
  1407. /**
  1408. * 当前是否ssl
  1409. * @access public
  1410. * @return bool
  1411. */
  1412. public function isSsl()
  1413. {
  1414. if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) {
  1415. return true;
  1416. } elseif ('https' == $this->server('REQUEST_SCHEME')) {
  1417. return true;
  1418. } elseif ('443' == $this->server('SERVER_PORT')) {
  1419. return true;
  1420. } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) {
  1421. return true;
  1422. } elseif ($this->config['https_agent_name'] && $this->server($this->config['https_agent_name'])) {
  1423. return true;
  1424. }
  1425. return false;
  1426. }
  1427. /**
  1428. * 当前是否Ajax请求
  1429. * @access public
  1430. * @param bool $ajax true 获取原始ajax请求
  1431. * @return bool
  1432. */
  1433. public function isAjax($ajax = false)
  1434. {
  1435. $value = $this->server('HTTP_X_REQUESTED_WITH');
  1436. $result = 'xmlhttprequest' == strtolower($value) ? true : false;
  1437. if (true === $ajax) {
  1438. return $result;
  1439. }
  1440. $result = $this->param($this->config['var_ajax']) ? true : $result;
  1441. $this->mergeParam = false;
  1442. return $result;
  1443. }
  1444. /**
  1445. * 当前是否Pjax请求
  1446. * @access public
  1447. * @param bool $pjax true 获取原始pjax请求
  1448. * @return bool
  1449. */
  1450. public function isPjax($pjax = false)
  1451. {
  1452. $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
  1453. if (true === $pjax) {
  1454. return $result;
  1455. }
  1456. $result = $this->param($this->config['var_pjax']) ? true : $result;
  1457. $this->mergeParam = false;
  1458. return $result;
  1459. }
  1460. /**
  1461. * 获取客户端IP地址
  1462. * @access public
  1463. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  1464. * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
  1465. * @return mixed
  1466. */
  1467. public function ip($type = 0, $adv = true)
  1468. {
  1469. $type = $type ? 1 : 0;
  1470. static $ip = null;
  1471. if (null !== $ip) {
  1472. return $ip[$type];
  1473. }
  1474. $httpAgentIp = $this->config['http_agent_ip'];
  1475. if ($httpAgentIp && $this->server($httpAgentIp)) {
  1476. $ip = $this->server($httpAgentIp);
  1477. } elseif ($adv) {
  1478. if ($this->server('HTTP_X_FORWARDED_FOR')) {
  1479. $arr = explode(',', $this->server('HTTP_X_FORWARDED_FOR'));
  1480. $pos = array_search('unknown', $arr);
  1481. if (false !== $pos) {
  1482. unset($arr[$pos]);
  1483. }
  1484. $ip = trim(current($arr));
  1485. } elseif ($this->server('HTTP_CLIENT_IP')) {
  1486. $ip = $this->server('HTTP_CLIENT_IP');
  1487. } elseif ($this->server('REMOTE_ADDR')) {
  1488. $ip = $this->server('REMOTE_ADDR');
  1489. }
  1490. } elseif ($this->server('REMOTE_ADDR')) {
  1491. $ip = $this->server('REMOTE_ADDR');
  1492. }
  1493. // IP地址类型
  1494. $ip_mode = (strpos($ip, ':') === false) ? 'ipv4' : 'ipv6';
  1495. // IP地址合法验证
  1496. if (filter_var($ip, FILTER_VALIDATE_IP) !== $ip) {
  1497. $ip = ('ipv4' === $ip_mode) ? '0.0.0.0' : '::';
  1498. }
  1499. // 如果是ipv4地址,则直接使用ip2long返回int类型ip;如果是ipv6地址,暂时不支持,直接返回0
  1500. $long_ip = ('ipv4' === $ip_mode) ? sprintf("%u", ip2long($ip)) : 0;
  1501. $ip = [$ip, $long_ip];
  1502. return $ip[$type];
  1503. }
  1504. /**
  1505. * 检测是否使用手机访问
  1506. * @access public
  1507. * @return bool
  1508. */
  1509. public function isMobile()
  1510. {
  1511. if ($this->server('HTTP_VIA') && stristr($this->server('HTTP_VIA'), "wap")) {
  1512. return true;
  1513. } elseif ($this->server('HTTP_ACCEPT') && strpos(strtoupper($this->server('HTTP_ACCEPT')), "VND.WAP.WML")) {
  1514. return true;
  1515. } elseif ($this->server('HTTP_X_WAP_PROFILE') || $this->server('HTTP_PROFILE')) {
  1516. return true;
  1517. } elseif ($this->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', $this->server('HTTP_USER_AGENT'))) {
  1518. return true;
  1519. }
  1520. return false;
  1521. }
  1522. /**
  1523. * 当前URL地址中的scheme参数
  1524. * @access public
  1525. * @return string
  1526. */
  1527. public function scheme()
  1528. {
  1529. return $this->isSsl() ? 'https' : 'http';
  1530. }
  1531. /**
  1532. * 当前请求URL地址中的query参数
  1533. * @access public
  1534. * @return string
  1535. */
  1536. public function query()
  1537. {
  1538. return $this->server('QUERY_STRING');
  1539. }
  1540. /**
  1541. * 设置当前请求的host(包含端口)
  1542. * @access public
  1543. * @param string $host 主机名(含端口)
  1544. * @return $this
  1545. */
  1546. public function setHost($host)
  1547. {
  1548. $this->host = $host;
  1549. return $this;
  1550. }
  1551. /**
  1552. * 当前请求的host
  1553. * @access public
  1554. * @param bool $strict true 仅仅获取HOST
  1555. * @return string
  1556. */
  1557. public function host($strict = false)
  1558. {
  1559. if (!$this->host) {
  1560. $this->host = $this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST');
  1561. }
  1562. return true === $strict && strpos($this->host, ':') ? strstr($this->host, ':', true) : $this->host;
  1563. }
  1564. /**
  1565. * 当前请求URL地址中的port参数
  1566. * @access public
  1567. * @return integer
  1568. */
  1569. public function port()
  1570. {
  1571. return $this->server('SERVER_PORT');
  1572. }
  1573. /**
  1574. * 当前请求 SERVER_PROTOCOL
  1575. * @access public
  1576. * @return string
  1577. */
  1578. public function protocol()
  1579. {
  1580. return $this->server('SERVER_PROTOCOL');
  1581. }
  1582. /**
  1583. * 当前请求 REMOTE_PORT
  1584. * @access public
  1585. * @return integer
  1586. */
  1587. public function remotePort()
  1588. {
  1589. return $this->server('REMOTE_PORT');
  1590. }
  1591. /**
  1592. * 当前请求 HTTP_CONTENT_TYPE
  1593. * @access public
  1594. * @return string
  1595. */
  1596. public function contentType()
  1597. {
  1598. $contentType = $this->server('CONTENT_TYPE');
  1599. if ($contentType) {
  1600. if (strpos($contentType, ';')) {
  1601. list($type) = explode(';', $contentType);
  1602. } else {
  1603. $type = $contentType;
  1604. }
  1605. return trim($type);
  1606. }
  1607. return '';
  1608. }
  1609. /**
  1610. * 获取当前请求的路由信息
  1611. * @access public
  1612. * @param array $route 路由名称
  1613. * @return array
  1614. */
  1615. public function routeInfo(array $route = [])
  1616. {
  1617. if (!empty($route)) {
  1618. $this->routeInfo = $route;
  1619. }
  1620. return $this->routeInfo;
  1621. }
  1622. /**
  1623. * 设置或者获取当前请求的调度信息
  1624. * @access public
  1625. * @param \think\route\Dispatch $dispatch 调度信息
  1626. * @return \think\route\Dispatch
  1627. */
  1628. public function dispatch($dispatch = null)
  1629. {
  1630. if (!is_null($dispatch)) {
  1631. $this->dispatch = $dispatch;
  1632. }
  1633. return $this->dispatch;
  1634. }
  1635. /**
  1636. * 获取当前请求的安全Key
  1637. * @access public
  1638. * @return string
  1639. */
  1640. public function secureKey()
  1641. {
  1642. if (is_null($this->secureKey)) {
  1643. $this->secureKey = uniqid('', true);
  1644. }
  1645. return $this->secureKey;
  1646. }
  1647. /**
  1648. * 设置当前的模块名
  1649. * @access public
  1650. * @param string $module 模块名
  1651. * @return $this
  1652. */
  1653. public function setModule($module)
  1654. {
  1655. $this->module = $module;
  1656. return $this;
  1657. }
  1658. /**
  1659. * 设置当前的控制器名
  1660. * @access public
  1661. * @param string $controller 控制器名
  1662. * @return $this
  1663. */
  1664. public function setController($controller)
  1665. {
  1666. $this->controller = $controller;
  1667. return $this;
  1668. }
  1669. /**
  1670. * 设置当前的操作名
  1671. * @access public
  1672. * @param string $action 操作名
  1673. * @return $this
  1674. */
  1675. public function setAction($action)
  1676. {
  1677. $this->action = $action;
  1678. return $this;
  1679. }
  1680. /**
  1681. * 获取当前的模块名
  1682. * @access public
  1683. * @return string
  1684. */
  1685. public function module()
  1686. {
  1687. return $this->module ?: '';
  1688. }
  1689. /**
  1690. * 获取当前的控制器名
  1691. * @access public
  1692. * @param bool $convert 转换为小写
  1693. * @return string
  1694. */
  1695. public function controller($convert = false)
  1696. {
  1697. $name = $this->controller ?: '';
  1698. return $convert ? strtolower($name) : $name;
  1699. }
  1700. /**
  1701. * 获取当前的操作名
  1702. * @access public
  1703. * @param bool $convert 转换为驼峰
  1704. * @return string
  1705. */
  1706. public function action($convert = false)
  1707. {
  1708. $name = $this->action ?: '';
  1709. return $convert ? $name : strtolower($name);
  1710. }
  1711. /**
  1712. * 设置当前的语言
  1713. * @access public
  1714. * @param string $lang 语言名
  1715. * @return $this
  1716. */
  1717. public function setLangset($lang)
  1718. {
  1719. $this->langset = $lang;
  1720. return $this;
  1721. }
  1722. /**
  1723. * 获取当前的语言
  1724. * @access public
  1725. * @return string
  1726. */
  1727. public function langset()
  1728. {
  1729. return $this->langset ?: '';
  1730. }
  1731. /**
  1732. * 设置或者获取当前请求的content
  1733. * @access public
  1734. * @return string
  1735. */
  1736. public function getContent()
  1737. {
  1738. if (is_null($this->content)) {
  1739. $this->content = $this->input;
  1740. }
  1741. return $this->content;
  1742. }
  1743. /**
  1744. * 获取当前请求的php://input
  1745. * @access public
  1746. * @return string
  1747. */
  1748. public function getInput()
  1749. {
  1750. return $this->input;
  1751. }
  1752. /**
  1753. * 生成请求令牌
  1754. * @access public
  1755. * @param string $name 令牌名称
  1756. * @param mixed $type 令牌生成方法
  1757. * @return string
  1758. */
  1759. public function token($name = '__token__', $type = null)
  1760. {
  1761. $type = is_callable($type) ? $type : 'md5';
  1762. $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT'));
  1763. if ($this->isAjax()) {
  1764. header($name . ': ' . $token);
  1765. }
  1766. facade\Session::set($name, $token);
  1767. return $token;
  1768. }
  1769. /**
  1770. * 设置当前地址的请求缓存
  1771. * @access public
  1772. * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
  1773. * @param mixed $expire 缓存有效期
  1774. * @param array $except 缓存排除
  1775. * @param string $tag 缓存标签
  1776. * @return mixed
  1777. */
  1778. public function cache($key, $expire = null, $except = [], $tag = null)
  1779. {
  1780. if (!is_array($except)) {
  1781. $tag = $except;
  1782. $except = [];
  1783. }
  1784. if (false === $key || !$this->isGet() || $this->isCheckCache || false === $expire) {
  1785. // 关闭当前缓存
  1786. return;
  1787. }
  1788. // 标记请求缓存检查
  1789. $this->isCheckCache = true;
  1790. foreach ($except as $rule) {
  1791. if (0 === stripos($this->url(), $rule)) {
  1792. return;
  1793. }
  1794. }
  1795. if ($key instanceof \Closure) {
  1796. $key = call_user_func_array($key, [$this]);
  1797. } elseif (true === $key) {
  1798. // 自动缓存功能
  1799. $key = '__URL__';
  1800. } elseif (strpos($key, '|')) {
  1801. list($key, $fun) = explode('|', $key);
  1802. }
  1803. // 特殊规则替换
  1804. if (false !== strpos($key, '__')) {
  1805. $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url(true))], $key);
  1806. }
  1807. if (false !== strpos($key, ':')) {
  1808. $param = $this->param();
  1809. foreach ($param as $item => $val) {
  1810. if (is_string($val) && false !== strpos($key, ':' . $item)) {
  1811. $key = str_replace(':' . $item, $val, $key);
  1812. }
  1813. }
  1814. } elseif (strpos($key, ']')) {
  1815. if ('[' . $this->ext() . ']' == $key) {
  1816. // 缓存某个后缀的请求
  1817. $key = md5($this->url());
  1818. } else {
  1819. return;
  1820. }
  1821. }
  1822. if (isset($fun)) {
  1823. $key = $fun($key);
  1824. }
  1825. $this->cache = [$key, $expire, $tag];
  1826. return $this->cache;
  1827. }
  1828. /**
  1829. * 读取请求缓存设置
  1830. * @access public
  1831. * @return array
  1832. */
  1833. public function getCache()
  1834. {
  1835. return $this->cache;
  1836. }
  1837. /**
  1838. * 设置GET数据
  1839. * @access public
  1840. * @param array $get 数据
  1841. * @return $this
  1842. */
  1843. public function withGet(array $get)
  1844. {
  1845. $this->get = $get;
  1846. return $this;
  1847. }
  1848. /**
  1849. * 设置POST数据
  1850. * @access public
  1851. * @param array $post 数据
  1852. * @return $this
  1853. */
  1854. public function withPost(array $post)
  1855. {
  1856. $this->post = $post;
  1857. return $this;
  1858. }
  1859. /**
  1860. * 设置php://input数据
  1861. * @access public
  1862. * @param string $input RAW数据
  1863. * @return $this
  1864. */
  1865. public function withInput($input)
  1866. {
  1867. $this->input = $input;
  1868. return $this;
  1869. }
  1870. /**
  1871. * 设置文件上传数据
  1872. * @access public
  1873. * @param array $files 上传信息
  1874. * @return $this
  1875. */
  1876. public function withFiles(array $files)
  1877. {
  1878. $this->file = $files;
  1879. return $this;
  1880. }
  1881. /**
  1882. * 设置COOKIE数据
  1883. * @access public
  1884. * @param array $cookie 数据
  1885. * @return $this
  1886. */
  1887. public function withCookie(array $cookie)
  1888. {
  1889. $this->cookie = $cookie;
  1890. return $this;
  1891. }
  1892. /**
  1893. * 设置SERVER数据
  1894. * @access public
  1895. * @param array $server 数据
  1896. * @return $this
  1897. */
  1898. public function withServer(array $server)
  1899. {
  1900. $this->server = array_change_key_case($server, CASE_UPPER);
  1901. return $this;
  1902. }
  1903. /**
  1904. * 设置HEADER数据
  1905. * @access public
  1906. * @param array $header 数据
  1907. * @return $this
  1908. */
  1909. public function withHeader(array $header)
  1910. {
  1911. $this->header = array_change_key_case($header);
  1912. return $this;
  1913. }
  1914. /**
  1915. * 设置ENV数据
  1916. * @access public
  1917. * @param array $env 数据
  1918. * @return $this
  1919. */
  1920. public function withEnv(array $env)
  1921. {
  1922. $this->env = $env;
  1923. return $this;
  1924. }
  1925. /**
  1926. * 设置ROUTE变量
  1927. * @access public
  1928. * @param array $route 数据
  1929. * @return $this
  1930. */
  1931. public function withRoute(array $route)
  1932. {
  1933. $this->route = $route;
  1934. return $this;
  1935. }
  1936. /**
  1937. * 设置请求数据
  1938. * @access public
  1939. * @param string $name 参数名
  1940. * @param mixed $value 值
  1941. */
  1942. public function __set($name, $value)
  1943. {
  1944. return $this->param[$name] = $value;
  1945. }
  1946. /**
  1947. * 获取请求数据的值
  1948. * @access public
  1949. * @param string $name 参数名
  1950. * @return mixed
  1951. */
  1952. public function __get($name)
  1953. {
  1954. return $this->param($name);
  1955. }
  1956. /**
  1957. * 检测请求数据的值
  1958. * @access public
  1959. * @param string $name 名称
  1960. * @return boolean
  1961. */
  1962. public function __isset($name)
  1963. {
  1964. return isset($this->param[$name]);
  1965. }
  1966. public function __debugInfo()
  1967. {
  1968. $data = get_object_vars($this);
  1969. unset($data['dispatch'], $data['config']);
  1970. return $data;
  1971. }
  1972. }