Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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