Request.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  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. unset($this->get[$this->config['var_pathinfo']]);
  594. } elseif ($this->isCli()) {
  595. // CLI模式下 index.php module/controller/action/params/...
  596. $pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  597. } elseif ('cli-server' == PHP_SAPI) {
  598. $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI');
  599. } elseif ($this->server('PATH_INFO')) {
  600. $pathinfo = $this->server('PATH_INFO');
  601. }
  602. // 分析PATHINFO信息
  603. if (!isset($pathinfo)) {
  604. foreach ($this->config['pathinfo_fetch'] as $type) {
  605. if ($this->server($type)) {
  606. $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ?
  607. substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type);
  608. break;
  609. }
  610. }
  611. }
  612. if (!empty($pathinfo)) {
  613. unset($this->get[$pathinfo], $this->request[$pathinfo]);
  614. }
  615. $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/');
  616. }
  617. return $this->pathinfo;
  618. }
  619. /**
  620. * 获取当前请求URL的pathinfo信息(不含URL后缀)
  621. * @access public
  622. * @return string
  623. */
  624. public function path()
  625. {
  626. if (is_null($this->path)) {
  627. $suffix = $this->config['url_html_suffix'];
  628. $pathinfo = $this->pathinfo();
  629. if (false === $suffix) {
  630. // 禁止伪静态访问
  631. $this->path = $pathinfo;
  632. } elseif ($suffix) {
  633. // 去除正常的URL后缀
  634. $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
  635. } else {
  636. // 允许任何后缀访问
  637. $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
  638. }
  639. }
  640. return $this->path;
  641. }
  642. /**
  643. * 当前URL的访问后缀
  644. * @access public
  645. * @return string
  646. */
  647. public function ext()
  648. {
  649. return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
  650. }
  651. /**
  652. * 获取当前请求的时间
  653. * @access public
  654. * @param bool $float 是否使用浮点类型
  655. * @return integer|float
  656. */
  657. public function time($float = false)
  658. {
  659. return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME');
  660. }
  661. /**
  662. * 当前请求的资源类型
  663. * @access public
  664. * @return false|string
  665. */
  666. public function type()
  667. {
  668. $accept = $this->server('HTTP_ACCEPT');
  669. if (empty($accept)) {
  670. return false;
  671. }
  672. foreach ($this->mimeType as $key => $val) {
  673. $array = explode(',', $val);
  674. foreach ($array as $k => $v) {
  675. if (stristr($accept, $v)) {
  676. return $key;
  677. }
  678. }
  679. }
  680. return false;
  681. }
  682. /**
  683. * 设置资源类型
  684. * @access public
  685. * @param string|array $type 资源类型名
  686. * @param string $val 资源类型
  687. * @return void
  688. */
  689. public function mimeType($type, $val = '')
  690. {
  691. if (is_array($type)) {
  692. $this->mimeType = array_merge($this->mimeType, $type);
  693. } else {
  694. $this->mimeType[$type] = $val;
  695. }
  696. }
  697. /**
  698. * 当前的请求类型
  699. * @access public
  700. * @param bool $origin 是否获取原始请求类型
  701. * @return string
  702. */
  703. public function method($origin = false)
  704. {
  705. if ($origin) {
  706. // 获取原始请求类型
  707. return $this->server('REQUEST_METHOD') ?: 'GET';
  708. } elseif (!$this->method) {
  709. if (isset($_POST[$this->config['var_method']])) {
  710. $method = strtolower($_POST[$this->config['var_method']]);
  711. if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
  712. $this->method = strtoupper($method);
  713. $this->{$method} = $_POST;
  714. } else {
  715. $this->method = 'POST';
  716. }
  717. unset($_POST[$this->config['var_method']]);
  718. } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
  719. $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
  720. } else {
  721. $this->method = $this->server('REQUEST_METHOD') ?: 'GET';
  722. }
  723. }
  724. return $this->method;
  725. }
  726. /**
  727. * 是否为GET请求
  728. * @access public
  729. * @return bool
  730. */
  731. public function isGet()
  732. {
  733. return $this->method() == 'GET';
  734. }
  735. /**
  736. * 是否为POST请求
  737. * @access public
  738. * @return bool
  739. */
  740. public function isPost()
  741. {
  742. return $this->method() == 'POST';
  743. }
  744. /**
  745. * 是否为PUT请求
  746. * @access public
  747. * @return bool
  748. */
  749. public function isPut()
  750. {
  751. return $this->method() == 'PUT';
  752. }
  753. /**
  754. * 是否为DELTE请求
  755. * @access public
  756. * @return bool
  757. */
  758. public function isDelete()
  759. {
  760. return $this->method() == 'DELETE';
  761. }
  762. /**
  763. * 是否为HEAD请求
  764. * @access public
  765. * @return bool
  766. */
  767. public function isHead()
  768. {
  769. return $this->method() == 'HEAD';
  770. }
  771. /**
  772. * 是否为PATCH请求
  773. * @access public
  774. * @return bool
  775. */
  776. public function isPatch()
  777. {
  778. return $this->method() == 'PATCH';
  779. }
  780. /**
  781. * 是否为OPTIONS请求
  782. * @access public
  783. * @return bool
  784. */
  785. public function isOptions()
  786. {
  787. return $this->method() == 'OPTIONS';
  788. }
  789. /**
  790. * 是否为cli
  791. * @access public
  792. * @return bool
  793. */
  794. public function isCli()
  795. {
  796. return PHP_SAPI == 'cli';
  797. }
  798. /**
  799. * 是否为cgi
  800. * @access public
  801. * @return bool
  802. */
  803. public function isCgi()
  804. {
  805. return strpos(PHP_SAPI, 'cgi') === 0;
  806. }
  807. /**
  808. * 获取当前请求的参数
  809. * @access public
  810. * @param mixed $name 变量名
  811. * @param mixed $default 默认值
  812. * @param string|array $filter 过滤方法
  813. * @return mixed
  814. */
  815. public function param($name = '', $default = null, $filter = '')
  816. {
  817. if (!$this->mergeParam) {
  818. $method = $this->method(true);
  819. // 自动获取请求变量
  820. switch ($method) {
  821. case 'POST':
  822. $vars = $this->post(false);
  823. break;
  824. case 'PUT':
  825. case 'DELETE':
  826. case 'PATCH':
  827. $vars = $this->put(false);
  828. break;
  829. default:
  830. $vars = [];
  831. }
  832. // 当前请求参数和URL地址中的参数合并
  833. $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false));
  834. $this->mergeParam = true;
  835. }
  836. if (true === $name) {
  837. // 获取包含文件上传信息的数组
  838. $file = $this->file();
  839. $data = is_array($file) ? array_merge($this->param, $file) : $this->param;
  840. return $this->input($data, '', $default, $filter);
  841. }
  842. return $this->input($this->param, $name, $default, $filter);
  843. }
  844. /**
  845. * 设置路由变量
  846. * @access public
  847. * @param array $route 路由变量
  848. * @return $this
  849. */
  850. public function setRouteVars(array $route)
  851. {
  852. $this->route = array_merge($this->route, $route);
  853. return $this;
  854. }
  855. /**
  856. * 获取路由参数
  857. * @access public
  858. * @param string|false $name 变量名
  859. * @param mixed $default 默认值
  860. * @param string|array $filter 过滤方法
  861. * @return mixed
  862. */
  863. public function route($name = '', $default = null, $filter = '')
  864. {
  865. return $this->input($this->route, $name, $default, $filter);
  866. }
  867. /**
  868. * 获取GET参数
  869. * @access public
  870. * @param string|false $name 变量名
  871. * @param mixed $default 默认值
  872. * @param string|array $filter 过滤方法
  873. * @return mixed
  874. */
  875. public function get($name = '', $default = null, $filter = '')
  876. {
  877. if (empty($this->get)) {
  878. $this->get = $_GET;
  879. }
  880. return $this->input($this->get, $name, $default, $filter);
  881. }
  882. /**
  883. * 获取POST参数
  884. * @access public
  885. * @param string|false $name 变量名
  886. * @param mixed $default 默认值
  887. * @param string|array $filter 过滤方法
  888. * @return mixed
  889. */
  890. public function post($name = '', $default = null, $filter = '')
  891. {
  892. if (empty($this->post)) {
  893. $this->post = !empty($_POST) ? $_POST : $this->getInputData($this->input);
  894. }
  895. return $this->input($this->post, $name, $default, $filter);
  896. }
  897. /**
  898. * 获取PUT参数
  899. * @access public
  900. * @param string|false $name 变量名
  901. * @param mixed $default 默认值
  902. * @param string|array $filter 过滤方法
  903. * @return mixed
  904. */
  905. public function put($name = '', $default = null, $filter = '')
  906. {
  907. if (is_null($this->put)) {
  908. $this->put = $this->getInputData($this->input);
  909. }
  910. return $this->input($this->put, $name, $default, $filter);
  911. }
  912. protected function getInputData($content)
  913. {
  914. if (false !== strpos($this->contentType(), 'json')) {
  915. return (array) json_decode($content, true);
  916. } elseif (strpos($content, '=')) {
  917. parse_str($content, $data);
  918. return $data;
  919. }
  920. return [];
  921. }
  922. /**
  923. * 获取DELETE参数
  924. * @access public
  925. * @param string|false $name 变量名
  926. * @param mixed $default 默认值
  927. * @param string|array $filter 过滤方法
  928. * @return mixed
  929. */
  930. public function delete($name = '', $default = null, $filter = '')
  931. {
  932. return $this->put($name, $default, $filter);
  933. }
  934. /**
  935. * 获取PATCH参数
  936. * @access public
  937. * @param string|false $name 变量名
  938. * @param mixed $default 默认值
  939. * @param string|array $filter 过滤方法
  940. * @return mixed
  941. */
  942. public function patch($name = '', $default = null, $filter = '')
  943. {
  944. return $this->put($name, $default, $filter);
  945. }
  946. /**
  947. * 获取request变量
  948. * @access public
  949. * @param string|false $name 变量名
  950. * @param mixed $default 默认值
  951. * @param string|array $filter 过滤方法
  952. * @return mixed
  953. */
  954. public function request($name = '', $default = null, $filter = '')
  955. {
  956. if (empty($this->request)) {
  957. $this->request = $_REQUEST;
  958. }
  959. return $this->input($this->request, $name, $default, $filter);
  960. }
  961. /**
  962. * 获取session数据
  963. * @access public
  964. * @param string $name 数据名称
  965. * @param string $default 默认值
  966. * @return mixed
  967. */
  968. public function session($name = '', $default = null)
  969. {
  970. if (empty($this->session)) {
  971. $this->session = Session::get();
  972. }
  973. if ('' === $name) {
  974. return $this->session;
  975. }
  976. $data = $this->getData($this->session, $name);
  977. return is_null($data) ? $default : $data;
  978. }
  979. /**
  980. * 获取cookie参数
  981. * @access public
  982. * @param string $name 变量名
  983. * @param string $default 默认值
  984. * @param string|array $filter 过滤方法
  985. * @return mixed
  986. */
  987. public function cookie($name = '', $default = null, $filter = '')
  988. {
  989. if (empty($this->cookie)) {
  990. $this->cookie = Cookie::get();
  991. }
  992. if (!empty($name)) {
  993. $data = Cookie::has($name) ? Cookie::get($name) : $default;
  994. } else {
  995. $data = $this->cookie;
  996. }
  997. // 解析过滤器
  998. $filter = $this->getFilter($filter, $default);
  999. if (is_array($data)) {
  1000. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  1001. reset($data);
  1002. } else {
  1003. $this->filterValue($data, $name, $filter);
  1004. }
  1005. return $data;
  1006. }
  1007. /**
  1008. * 获取server参数
  1009. * @access public
  1010. * @param string $name 数据名称
  1011. * @param string $default 默认值
  1012. * @return mixed
  1013. */
  1014. public function server($name = '', $default = null)
  1015. {
  1016. if (empty($name)) {
  1017. return $this->server;
  1018. } else {
  1019. $name = strtoupper($name);
  1020. }
  1021. return isset($this->server[$name]) ? $this->server[$name] : $default;
  1022. }
  1023. /**
  1024. * 获取上传的文件信息
  1025. * @access public
  1026. * @param string $name 名称
  1027. * @return null|array|\think\File
  1028. */
  1029. public function file($name = '')
  1030. {
  1031. if (empty($this->file)) {
  1032. $this->file = isset($_FILES) ? $_FILES : [];
  1033. }
  1034. $files = $this->file;
  1035. if (!empty($files)) {
  1036. if (strpos($name, '.')) {
  1037. list($name, $sub) = explode('.', $name);
  1038. }
  1039. // 处理上传文件
  1040. $array = $this->dealUploadFile($files, $name);
  1041. if ('' === $name) {
  1042. // 获取全部文件
  1043. return $array;
  1044. } elseif (isset($sub) && isset($array[$name][$sub])) {
  1045. return $array[$name][$sub];
  1046. } elseif (isset($array[$name])) {
  1047. return $array[$name];
  1048. }
  1049. }
  1050. return;
  1051. }
  1052. protected function dealUploadFile($files, $name)
  1053. {
  1054. $array = [];
  1055. foreach ($files as $key => $file) {
  1056. if ($file instanceof File) {
  1057. $array[$key] = $file;
  1058. } elseif (is_array($file['name'])) {
  1059. $item = [];
  1060. $keys = array_keys($file);
  1061. $count = count($file['name']);
  1062. for ($i = 0; $i < $count; $i++) {
  1063. if ($file['error'][$i] > 0) {
  1064. if ($name == $key) {
  1065. $this->throwUploadFileError($file['error'][$i]);
  1066. } else {
  1067. continue;
  1068. }
  1069. }
  1070. $temp['key'] = $key;
  1071. foreach ($keys as $_key) {
  1072. $temp[$_key] = $file[$_key][$i];
  1073. }
  1074. $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
  1075. }
  1076. $array[$key] = $item;
  1077. } else {
  1078. if ($file['error'] > 0) {
  1079. if ($key == $name) {
  1080. $this->throwUploadFileError($file['error']);
  1081. } else {
  1082. continue;
  1083. }
  1084. }
  1085. $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
  1086. }
  1087. }
  1088. return $array;
  1089. }
  1090. protected function throwUploadFileError($error)
  1091. {
  1092. static $fileUploadErrors = [
  1093. 1 => 'upload File size exceeds the maximum value',
  1094. 2 => 'upload File size exceeds the maximum value',
  1095. 3 => 'only the portion of file is uploaded',
  1096. 4 => 'no file to uploaded',
  1097. 6 => 'upload temp dir not found',
  1098. 7 => 'file write error',
  1099. ];
  1100. $msg = $fileUploadErrors[$error];
  1101. throw new Exception($msg);
  1102. }
  1103. /**
  1104. * 获取环境变量
  1105. * @access public
  1106. * @param string $name 数据名称
  1107. * @param string $default 默认值
  1108. * @return mixed
  1109. */
  1110. public function env($name = '', $default = null)
  1111. {
  1112. if (empty($name)) {
  1113. return $this->env;
  1114. } else {
  1115. $name = strtoupper($name);
  1116. }
  1117. return isset($this->env[$name]) ? $this->env[$name] : $default;
  1118. }
  1119. /**
  1120. * 获取当前的Header
  1121. * @access public
  1122. * @param string $name header名称
  1123. * @param string $default 默认值
  1124. * @return string|array
  1125. */
  1126. public function header($name = '', $default = null)
  1127. {
  1128. if (empty($this->header)) {
  1129. $header = [];
  1130. if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
  1131. $header = $result;
  1132. } else {
  1133. $server = $this->server;
  1134. foreach ($server as $key => $val) {
  1135. if (0 === strpos($key, 'HTTP_')) {
  1136. $key = str_replace('_', '-', strtolower(substr($key, 5)));
  1137. $header[$key] = $val;
  1138. }
  1139. }
  1140. if (isset($server['CONTENT_TYPE'])) {
  1141. $header['content-type'] = $server['CONTENT_TYPE'];
  1142. }
  1143. if (isset($server['CONTENT_LENGTH'])) {
  1144. $header['content-length'] = $server['CONTENT_LENGTH'];
  1145. }
  1146. }
  1147. $this->header = array_change_key_case($header);
  1148. }
  1149. if ('' === $name) {
  1150. return $this->header;
  1151. }
  1152. $name = str_replace('_', '-', strtolower($name));
  1153. return isset($this->header[$name]) ? $this->header[$name] : $default;
  1154. }
  1155. /**
  1156. * 递归重置数组指针
  1157. * @access public
  1158. * @param array $data 数据源
  1159. * @return void
  1160. */
  1161. public function arrayReset(array &$data)
  1162. {
  1163. foreach ($data as &$value) {
  1164. if (is_array($value)) {
  1165. $this->arrayReset($value);
  1166. }
  1167. }
  1168. reset($data);
  1169. }
  1170. /**
  1171. * 获取变量 支持过滤和默认值
  1172. * @access public
  1173. * @param array $data 数据源
  1174. * @param string|false $name 字段名
  1175. * @param mixed $default 默认值
  1176. * @param string|array $filter 过滤函数
  1177. * @return mixed
  1178. */
  1179. public function input($data = [], $name = '', $default = null, $filter = '')
  1180. {
  1181. if (false === $name) {
  1182. // 获取原始数据
  1183. return $data;
  1184. }
  1185. $name = (string) $name;
  1186. if ('' != $name) {
  1187. // 解析name
  1188. if (strpos($name, '/')) {
  1189. list($name, $type) = explode('/', $name);
  1190. }
  1191. $data = $this->getData($data, $name);
  1192. if (is_null($data)) {
  1193. return $default;
  1194. }
  1195. if (is_object($data)) {
  1196. return $data;
  1197. }
  1198. }
  1199. // 解析过滤器
  1200. $filter = $this->getFilter($filter, $default);
  1201. if (is_array($data)) {
  1202. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  1203. if (version_compare(PHP_VERSION, '7.1.0', '<')) {
  1204. // 恢复PHP版本低于 7.1 时 array_walk_recursive 中消耗的内部指针
  1205. $this->arrayReset($data);
  1206. }
  1207. } else {
  1208. $this->filterValue($data, $name, $filter);
  1209. }
  1210. if (isset($type) && $data !== $default) {
  1211. // 强制类型转换
  1212. $this->typeCast($data, $type);
  1213. }
  1214. return $data;
  1215. }
  1216. /**
  1217. * 获取数据
  1218. * @access public
  1219. * @param array $data 数据源
  1220. * @param string|false $name 字段名
  1221. * @return mixed
  1222. */
  1223. protected function getData(array $data, $name)
  1224. {
  1225. foreach (explode('.', $name) as $val) {
  1226. if (isset($data[$val])) {
  1227. $data = $data[$val];
  1228. } else {
  1229. return;
  1230. }
  1231. }
  1232. return $data;
  1233. }
  1234. /**
  1235. * 设置或获取当前的过滤规则
  1236. * @access public
  1237. * @param mixed $filter 过滤规则
  1238. * @return mixed
  1239. */
  1240. public function filter($filter = null)
  1241. {
  1242. if (is_null($filter)) {
  1243. return $this->filter;
  1244. }
  1245. $this->filter = $filter;
  1246. }
  1247. protected function getFilter($filter, $default)
  1248. {
  1249. if (is_null($filter)) {
  1250. $filter = [];
  1251. } else {
  1252. $filter = $filter ?: $this->filter;
  1253. if (is_string($filter) && false === strpos($filter, '/')) {
  1254. $filter = explode(',', $filter);
  1255. } else {
  1256. $filter = (array) $filter;
  1257. }
  1258. }
  1259. $filter[] = $default;
  1260. return $filter;
  1261. }
  1262. /**
  1263. * 递归过滤给定的值
  1264. * @access public
  1265. * @param mixed $value 键值
  1266. * @param mixed $key 键名
  1267. * @param array $filters 过滤方法+默认值
  1268. * @return mixed
  1269. */
  1270. private function filterValue(&$value, $key, $filters)
  1271. {
  1272. $default = array_pop($filters);
  1273. foreach ($filters as $filter) {
  1274. if (is_callable($filter)) {
  1275. // 调用函数或者方法过滤
  1276. $value = call_user_func($filter, $value);
  1277. } elseif (is_scalar($value)) {
  1278. if (false !== strpos($filter, '/')) {
  1279. // 正则过滤
  1280. if (!preg_match($filter, $value)) {
  1281. // 匹配不成功返回默认值
  1282. $value = $default;
  1283. break;
  1284. }
  1285. } elseif (!empty($filter)) {
  1286. // filter函数不存在时, 则使用filter_var进行过滤
  1287. // filter为非整形值时, 调用filter_id取得过滤id
  1288. $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
  1289. if (false === $value) {
  1290. $value = $default;
  1291. break;
  1292. }
  1293. }
  1294. }
  1295. }
  1296. return $value;
  1297. }
  1298. /**
  1299. * 强制类型转换
  1300. * @access public
  1301. * @param string $data
  1302. * @param string $type
  1303. * @return mixed
  1304. */
  1305. private function typeCast(&$data, $type)
  1306. {
  1307. switch (strtolower($type)) {
  1308. // 数组
  1309. case 'a':
  1310. $data = (array) $data;
  1311. break;
  1312. // 数字
  1313. case 'd':
  1314. $data = (int) $data;
  1315. break;
  1316. // 浮点
  1317. case 'f':
  1318. $data = (float) $data;
  1319. break;
  1320. // 布尔
  1321. case 'b':
  1322. $data = (boolean) $data;
  1323. break;
  1324. // 字符串
  1325. case 's':
  1326. if (is_scalar($data)) {
  1327. $data = (string) $data;
  1328. } else {
  1329. throw new \InvalidArgumentException('variable type error:' . gettype($data));
  1330. }
  1331. break;
  1332. }
  1333. }
  1334. /**
  1335. * 是否存在某个请求参数
  1336. * @access public
  1337. * @param string $name 变量名
  1338. * @param string $type 变量类型
  1339. * @param bool $checkEmpty 是否检测空值
  1340. * @return mixed
  1341. */
  1342. public function has($name, $type = 'param', $checkEmpty = false)
  1343. {
  1344. if (!in_array($type, ['param', 'get', 'post', 'request', 'put', 'patch', 'file', 'session', 'cookie', 'env', 'header', 'route'])) {
  1345. return false;
  1346. }
  1347. if (empty($this->$type)) {
  1348. $param = $this->$type();
  1349. } else {
  1350. $param = $this->$type;
  1351. }
  1352. // 按.拆分成多维数组进行判断
  1353. foreach (explode('.', $name) as $val) {
  1354. if (isset($param[$val])) {
  1355. $param = $param[$val];
  1356. } else {
  1357. return false;
  1358. }
  1359. }
  1360. return ($checkEmpty && '' === $param) ? false : true;
  1361. }
  1362. /**
  1363. * 获取指定的参数
  1364. * @access public
  1365. * @param string|array $name 变量名
  1366. * @param string $type 变量类型
  1367. * @return mixed
  1368. */
  1369. public function only($name, $type = 'param')
  1370. {
  1371. $param = $this->$type();
  1372. if (is_string($name)) {
  1373. $name = explode(',', $name);
  1374. }
  1375. $item = [];
  1376. foreach ($name as $key => $val) {
  1377. if (is_int($key)) {
  1378. $default = null;
  1379. $key = $val;
  1380. } else {
  1381. $default = $val;
  1382. }
  1383. if (isset($param[$key])) {
  1384. $item[$key] = $param[$key];
  1385. } elseif (isset($default)) {
  1386. $item[$key] = $default;
  1387. }
  1388. }
  1389. return $item;
  1390. }
  1391. /**
  1392. * 排除指定参数获取
  1393. * @access public
  1394. * @param string|array $name 变量名
  1395. * @param string $type 变量类型
  1396. * @return mixed
  1397. */
  1398. public function except($name, $type = 'param')
  1399. {
  1400. $param = $this->$type();
  1401. if (is_string($name)) {
  1402. $name = explode(',', $name);
  1403. }
  1404. foreach ($name as $key) {
  1405. if (isset($param[$key])) {
  1406. unset($param[$key]);
  1407. }
  1408. }
  1409. return $param;
  1410. }
  1411. /**
  1412. * 当前是否ssl
  1413. * @access public
  1414. * @return bool
  1415. */
  1416. public function isSsl()
  1417. {
  1418. if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) {
  1419. return true;
  1420. } elseif ('https' == $this->server('REQUEST_SCHEME')) {
  1421. return true;
  1422. } elseif ('443' == $this->server('SERVER_PORT')) {
  1423. return true;
  1424. } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) {
  1425. return true;
  1426. } elseif ($this->config['https_agent_name'] && $this->server($this->config['https_agent_name'])) {
  1427. return true;
  1428. }
  1429. return false;
  1430. }
  1431. /**
  1432. * 当前是否JSON请求
  1433. * @access public
  1434. * @return bool
  1435. */
  1436. public function isJson()
  1437. {
  1438. return false !== strpos($this->type(), 'json');
  1439. }
  1440. /**
  1441. * 当前是否Ajax请求
  1442. * @access public
  1443. * @param bool $ajax true 获取原始ajax请求
  1444. * @return bool
  1445. */
  1446. public function isAjax($ajax = false)
  1447. {
  1448. $value = $this->server('HTTP_X_REQUESTED_WITH');
  1449. $result = 'xmlhttprequest' == strtolower($value) ? true : false;
  1450. if (true === $ajax) {
  1451. return $result;
  1452. }
  1453. $result = $this->param($this->config['var_ajax']) ? true : $result;
  1454. $this->mergeParam = false;
  1455. return $result;
  1456. }
  1457. /**
  1458. * 当前是否Pjax请求
  1459. * @access public
  1460. * @param bool $pjax true 获取原始pjax请求
  1461. * @return bool
  1462. */
  1463. public function isPjax($pjax = false)
  1464. {
  1465. $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
  1466. if (true === $pjax) {
  1467. return $result;
  1468. }
  1469. $result = $this->param($this->config['var_pjax']) ? true : $result;
  1470. $this->mergeParam = false;
  1471. return $result;
  1472. }
  1473. /**
  1474. * 获取客户端IP地址
  1475. * @access public
  1476. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  1477. * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
  1478. * @return mixed
  1479. */
  1480. public function ip($type = 0, $adv = true)
  1481. {
  1482. $type = $type ? 1 : 0;
  1483. static $ip = null;
  1484. if (null !== $ip) {
  1485. return $ip[$type];
  1486. }
  1487. $httpAgentIp = $this->config['http_agent_ip'];
  1488. if ($httpAgentIp && $this->server($httpAgentIp)) {
  1489. $ip = $this->server($httpAgentIp);
  1490. } elseif ($adv) {
  1491. if ($this->server('HTTP_X_FORWARDED_FOR')) {
  1492. $arr = explode(',', $this->server('HTTP_X_FORWARDED_FOR'));
  1493. $pos = array_search('unknown', $arr);
  1494. if (false !== $pos) {
  1495. unset($arr[$pos]);
  1496. }
  1497. $ip = trim(current($arr));
  1498. } elseif ($this->server('HTTP_CLIENT_IP')) {
  1499. $ip = $this->server('HTTP_CLIENT_IP');
  1500. } elseif ($this->server('REMOTE_ADDR')) {
  1501. $ip = $this->server('REMOTE_ADDR');
  1502. }
  1503. } elseif ($this->server('REMOTE_ADDR')) {
  1504. $ip = $this->server('REMOTE_ADDR');
  1505. }
  1506. // IP地址类型
  1507. $ip_mode = (strpos($ip, ':') === false) ? 'ipv4' : 'ipv6';
  1508. // IP地址合法验证
  1509. if (filter_var($ip, FILTER_VALIDATE_IP) !== $ip) {
  1510. $ip = ('ipv4' === $ip_mode) ? '0.0.0.0' : '::';
  1511. }
  1512. // 如果是ipv4地址,则直接使用ip2long返回int类型ip;如果是ipv6地址,暂时不支持,直接返回0
  1513. $long_ip = ('ipv4' === $ip_mode) ? sprintf("%u", ip2long($ip)) : 0;
  1514. $ip = [$ip, $long_ip];
  1515. return $ip[$type];
  1516. }
  1517. /**
  1518. * 检测是否使用手机访问
  1519. * @access public
  1520. * @return bool
  1521. */
  1522. public function isMobile()
  1523. {
  1524. if ($this->server('HTTP_VIA') && stristr($this->server('HTTP_VIA'), "wap")) {
  1525. return true;
  1526. } elseif ($this->server('HTTP_ACCEPT') && strpos(strtoupper($this->server('HTTP_ACCEPT')), "VND.WAP.WML")) {
  1527. return true;
  1528. } elseif ($this->server('HTTP_X_WAP_PROFILE') || $this->server('HTTP_PROFILE')) {
  1529. return true;
  1530. } 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'))) {
  1531. return true;
  1532. }
  1533. return false;
  1534. }
  1535. /**
  1536. * 当前URL地址中的scheme参数
  1537. * @access public
  1538. * @return string
  1539. */
  1540. public function scheme()
  1541. {
  1542. return $this->isSsl() ? 'https' : 'http';
  1543. }
  1544. /**
  1545. * 当前请求URL地址中的query参数
  1546. * @access public
  1547. * @return string
  1548. */
  1549. public function query()
  1550. {
  1551. return $this->server('QUERY_STRING');
  1552. }
  1553. /**
  1554. * 设置当前请求的host(包含端口)
  1555. * @access public
  1556. * @param string $host 主机名(含端口)
  1557. * @return $this
  1558. */
  1559. public function setHost($host)
  1560. {
  1561. $this->host = $host;
  1562. return $this;
  1563. }
  1564. /**
  1565. * 当前请求的host
  1566. * @access public
  1567. * @param bool $strict true 仅仅获取HOST
  1568. * @return string
  1569. */
  1570. public function host($strict = false)
  1571. {
  1572. if (!$this->host) {
  1573. $this->host = $this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST');
  1574. }
  1575. return true === $strict && strpos($this->host, ':') ? strstr($this->host, ':', true) : $this->host;
  1576. }
  1577. /**
  1578. * 当前请求URL地址中的port参数
  1579. * @access public
  1580. * @return integer
  1581. */
  1582. public function port()
  1583. {
  1584. return $this->server('SERVER_PORT');
  1585. }
  1586. /**
  1587. * 当前请求 SERVER_PROTOCOL
  1588. * @access public
  1589. * @return string
  1590. */
  1591. public function protocol()
  1592. {
  1593. return $this->server('SERVER_PROTOCOL');
  1594. }
  1595. /**
  1596. * 当前请求 REMOTE_PORT
  1597. * @access public
  1598. * @return integer
  1599. */
  1600. public function remotePort()
  1601. {
  1602. return $this->server('REMOTE_PORT');
  1603. }
  1604. /**
  1605. * 当前请求 HTTP_CONTENT_TYPE
  1606. * @access public
  1607. * @return string
  1608. */
  1609. public function contentType()
  1610. {
  1611. $contentType = $this->server('CONTENT_TYPE');
  1612. if ($contentType) {
  1613. if (strpos($contentType, ';')) {
  1614. list($type) = explode(';', $contentType);
  1615. } else {
  1616. $type = $contentType;
  1617. }
  1618. return trim($type);
  1619. }
  1620. return '';
  1621. }
  1622. /**
  1623. * 获取当前请求的路由信息
  1624. * @access public
  1625. * @param array $route 路由名称
  1626. * @return array
  1627. */
  1628. public function routeInfo(array $route = [])
  1629. {
  1630. if (!empty($route)) {
  1631. $this->routeInfo = $route;
  1632. }
  1633. return $this->routeInfo;
  1634. }
  1635. /**
  1636. * 设置或者获取当前请求的调度信息
  1637. * @access public
  1638. * @param \think\route\Dispatch $dispatch 调度信息
  1639. * @return \think\route\Dispatch
  1640. */
  1641. public function dispatch($dispatch = null)
  1642. {
  1643. if (!is_null($dispatch)) {
  1644. $this->dispatch = $dispatch;
  1645. }
  1646. return $this->dispatch;
  1647. }
  1648. /**
  1649. * 获取当前请求的安全Key
  1650. * @access public
  1651. * @return string
  1652. */
  1653. public function secureKey()
  1654. {
  1655. if (is_null($this->secureKey)) {
  1656. $this->secureKey = uniqid('', true);
  1657. }
  1658. return $this->secureKey;
  1659. }
  1660. /**
  1661. * 设置当前的模块名
  1662. * @access public
  1663. * @param string $module 模块名
  1664. * @return $this
  1665. */
  1666. public function setModule($module)
  1667. {
  1668. $this->module = $module;
  1669. return $this;
  1670. }
  1671. /**
  1672. * 设置当前的控制器名
  1673. * @access public
  1674. * @param string $controller 控制器名
  1675. * @return $this
  1676. */
  1677. public function setController($controller)
  1678. {
  1679. $this->controller = $controller;
  1680. return $this;
  1681. }
  1682. /**
  1683. * 设置当前的操作名
  1684. * @access public
  1685. * @param string $action 操作名
  1686. * @return $this
  1687. */
  1688. public function setAction($action)
  1689. {
  1690. $this->action = $action;
  1691. return $this;
  1692. }
  1693. /**
  1694. * 获取当前的模块名
  1695. * @access public
  1696. * @return string
  1697. */
  1698. public function module()
  1699. {
  1700. return $this->module ?: '';
  1701. }
  1702. /**
  1703. * 获取当前的控制器名
  1704. * @access public
  1705. * @param bool $convert 转换为小写
  1706. * @return string
  1707. */
  1708. public function controller($convert = false)
  1709. {
  1710. $name = $this->controller ?: '';
  1711. return $convert ? strtolower($name) : $name;
  1712. }
  1713. /**
  1714. * 获取当前的操作名
  1715. * @access public
  1716. * @param bool $convert 转换为驼峰
  1717. * @return string
  1718. */
  1719. public function action($convert = false)
  1720. {
  1721. $name = $this->action ?: '';
  1722. return $convert ? $name : strtolower($name);
  1723. }
  1724. /**
  1725. * 设置当前的语言
  1726. * @access public
  1727. * @param string $lang 语言名
  1728. * @return $this
  1729. */
  1730. public function setLangset($lang)
  1731. {
  1732. $this->langset = $lang;
  1733. return $this;
  1734. }
  1735. /**
  1736. * 获取当前的语言
  1737. * @access public
  1738. * @return string
  1739. */
  1740. public function langset()
  1741. {
  1742. return $this->langset ?: '';
  1743. }
  1744. /**
  1745. * 设置或者获取当前请求的content
  1746. * @access public
  1747. * @return string
  1748. */
  1749. public function getContent()
  1750. {
  1751. if (is_null($this->content)) {
  1752. $this->content = $this->input;
  1753. }
  1754. return $this->content;
  1755. }
  1756. /**
  1757. * 获取当前请求的php://input
  1758. * @access public
  1759. * @return string
  1760. */
  1761. public function getInput()
  1762. {
  1763. return $this->input;
  1764. }
  1765. /**
  1766. * 生成请求令牌
  1767. * @access public
  1768. * @param string $name 令牌名称
  1769. * @param mixed $type 令牌生成方法
  1770. * @return string
  1771. */
  1772. public function token($name = '__token__', $type = null)
  1773. {
  1774. $type = is_callable($type) ? $type : 'md5';
  1775. $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT'));
  1776. if ($this->isAjax()) {
  1777. header($name . ': ' . $token);
  1778. }
  1779. facade\Session::set($name, $token);
  1780. return $token;
  1781. }
  1782. /**
  1783. * 设置当前地址的请求缓存
  1784. * @access public
  1785. * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
  1786. * @param mixed $expire 缓存有效期
  1787. * @param array $except 缓存排除
  1788. * @param string $tag 缓存标签
  1789. * @return mixed
  1790. */
  1791. public function cache($key, $expire = null, $except = [], $tag = null)
  1792. {
  1793. if (!is_array($except)) {
  1794. $tag = $except;
  1795. $except = [];
  1796. }
  1797. if (false === $key || !$this->isGet() || $this->isCheckCache || false === $expire) {
  1798. // 关闭当前缓存
  1799. return;
  1800. }
  1801. // 标记请求缓存检查
  1802. $this->isCheckCache = true;
  1803. foreach ($except as $rule) {
  1804. if (0 === stripos($this->url(), $rule)) {
  1805. return;
  1806. }
  1807. }
  1808. if ($key instanceof \Closure) {
  1809. $key = call_user_func_array($key, [$this]);
  1810. } elseif (true === $key) {
  1811. // 自动缓存功能
  1812. $key = '__URL__';
  1813. } elseif (strpos($key, '|')) {
  1814. list($key, $fun) = explode('|', $key);
  1815. }
  1816. // 特殊规则替换
  1817. if (false !== strpos($key, '__')) {
  1818. $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url(true))], $key);
  1819. }
  1820. if (false !== strpos($key, ':')) {
  1821. $param = $this->param();
  1822. foreach ($param as $item => $val) {
  1823. if (is_string($val) && false !== strpos($key, ':' . $item)) {
  1824. $key = str_replace(':' . $item, $val, $key);
  1825. }
  1826. }
  1827. } elseif (strpos($key, ']')) {
  1828. if ('[' . $this->ext() . ']' == $key) {
  1829. // 缓存某个后缀的请求
  1830. $key = md5($this->url());
  1831. } else {
  1832. return;
  1833. }
  1834. }
  1835. if (isset($fun)) {
  1836. $key = $fun($key);
  1837. }
  1838. $this->cache = [$key, $expire, $tag];
  1839. return $this->cache;
  1840. }
  1841. /**
  1842. * 读取请求缓存设置
  1843. * @access public
  1844. * @return array
  1845. */
  1846. public function getCache()
  1847. {
  1848. return $this->cache;
  1849. }
  1850. /**
  1851. * 设置GET数据
  1852. * @access public
  1853. * @param array $get 数据
  1854. * @return $this
  1855. */
  1856. public function withGet(array $get)
  1857. {
  1858. $this->get = $get;
  1859. return $this;
  1860. }
  1861. /**
  1862. * 设置POST数据
  1863. * @access public
  1864. * @param array $post 数据
  1865. * @return $this
  1866. */
  1867. public function withPost(array $post)
  1868. {
  1869. $this->post = $post;
  1870. return $this;
  1871. }
  1872. /**
  1873. * 设置php://input数据
  1874. * @access public
  1875. * @param string $input RAW数据
  1876. * @return $this
  1877. */
  1878. public function withInput($input)
  1879. {
  1880. $this->input = $input;
  1881. return $this;
  1882. }
  1883. /**
  1884. * 设置文件上传数据
  1885. * @access public
  1886. * @param array $files 上传信息
  1887. * @return $this
  1888. */
  1889. public function withFiles(array $files)
  1890. {
  1891. $this->file = $files;
  1892. return $this;
  1893. }
  1894. /**
  1895. * 设置COOKIE数据
  1896. * @access public
  1897. * @param array $cookie 数据
  1898. * @return $this
  1899. */
  1900. public function withCookie(array $cookie)
  1901. {
  1902. $this->cookie = $cookie;
  1903. return $this;
  1904. }
  1905. /**
  1906. * 设置SERVER数据
  1907. * @access public
  1908. * @param array $server 数据
  1909. * @return $this
  1910. */
  1911. public function withServer(array $server)
  1912. {
  1913. $this->server = array_change_key_case($server, CASE_UPPER);
  1914. return $this;
  1915. }
  1916. /**
  1917. * 设置HEADER数据
  1918. * @access public
  1919. * @param array $header 数据
  1920. * @return $this
  1921. */
  1922. public function withHeader(array $header)
  1923. {
  1924. $this->header = array_change_key_case($header);
  1925. return $this;
  1926. }
  1927. /**
  1928. * 设置ENV数据
  1929. * @access public
  1930. * @param array $env 数据
  1931. * @return $this
  1932. */
  1933. public function withEnv(array $env)
  1934. {
  1935. $this->env = $env;
  1936. return $this;
  1937. }
  1938. /**
  1939. * 设置ROUTE变量
  1940. * @access public
  1941. * @param array $route 数据
  1942. * @return $this
  1943. */
  1944. public function withRoute(array $route)
  1945. {
  1946. $this->route = $route;
  1947. return $this;
  1948. }
  1949. /**
  1950. * 设置请求数据
  1951. * @access public
  1952. * @param string $name 参数名
  1953. * @param mixed $value 值
  1954. */
  1955. public function __set($name, $value)
  1956. {
  1957. return $this->param[$name] = $value;
  1958. }
  1959. /**
  1960. * 获取请求数据的值
  1961. * @access public
  1962. * @param string $name 参数名
  1963. * @return mixed
  1964. */
  1965. public function __get($name)
  1966. {
  1967. return $this->param($name);
  1968. }
  1969. /**
  1970. * 检测请求数据的值
  1971. * @access public
  1972. * @param string $name 名称
  1973. * @return boolean
  1974. */
  1975. public function __isset($name)
  1976. {
  1977. return isset($this->param[$name]);
  1978. }
  1979. public function __debugInfo()
  1980. {
  1981. $data = get_object_vars($this);
  1982. unset($data['dispatch'], $data['config']);
  1983. return $data;
  1984. }
  1985. }