Request.php 52 KB

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