Request.php 58 KB

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