Request.php 51 KB

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