暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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