Request.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. <?php
  2. namespace daswork;
  3. class Request
  4. {
  5. /**
  6. * @var object 对象实例
  7. */
  8. protected static $instance;
  9. protected $method;
  10. /**
  11. * @var string 域名(含协议和端口)
  12. */
  13. protected $domain;
  14. /**
  15. * @var string URL地址
  16. */
  17. protected $url;
  18. /**
  19. * @var string 基础URL
  20. */
  21. protected $baseUrl;
  22. /**
  23. * @var string 当前执行的文件
  24. */
  25. protected $baseFile;
  26. /**
  27. * @var string 访问的ROOT地址
  28. */
  29. protected $root;
  30. /**
  31. * @var string pathinfo
  32. */
  33. protected $pathinfo;
  34. /**
  35. * @var string pathinfo(不含后缀)
  36. */
  37. protected $path;
  38. /**
  39. * @var array 当前路由信息
  40. */
  41. protected $routeInfo = [];
  42. /**
  43. * @var array 当前调度信息
  44. */
  45. protected $dispatch = [];
  46. protected $module;
  47. protected $controller;
  48. protected $action;
  49. /**
  50. * @var array 请求参数
  51. */
  52. protected $param = [];
  53. protected $get = [];
  54. protected $post = [];
  55. protected $request = [];
  56. protected $route = [];
  57. protected $put;
  58. protected $session = [];
  59. protected $file = [];
  60. protected $cookie = [];
  61. protected $server = [];
  62. protected $header = [];
  63. /**
  64. * @var array 资源类型
  65. */
  66. protected $mimeType = [
  67. 'xml' => 'application/xml,text/xml,application/x-xml',
  68. 'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
  69. 'js' => 'text/javascript,application/javascript,application/x-javascript',
  70. 'css' => 'text/css',
  71. 'rss' => 'application/rss+xml',
  72. 'yaml' => 'application/x-yaml,text/yaml',
  73. 'atom' => 'application/atom+xml',
  74. 'pdf' => 'application/pdf',
  75. 'text' => 'text/plain',
  76. 'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*',
  77. 'csv' => 'text/csv',
  78. 'html' => 'text/html,application/xhtml+xml,*/*',
  79. ];
  80. protected $content;
  81. // Hook扩展方法
  82. protected static $hook = [];
  83. // 绑定的属性
  84. protected $bind = [];
  85. // php://input
  86. protected $input;
  87. // 请求缓存
  88. protected $cache;
  89. // 缓存是否检查
  90. protected $isCheckCache;
  91. /**
  92. * 构造函数
  93. * @access protected
  94. * @param array $options 参数
  95. */
  96. protected function __construct($options = [])
  97. {
  98. foreach ($options as $name => $item) {
  99. if (property_exists($this, $name)) {
  100. $this->$name = $item;
  101. }
  102. }
  103. // 保存 php://input
  104. $this->input = file_get_contents('php://input');
  105. }
  106. /**
  107. * 初始化
  108. * @access public
  109. * @param array $options 参数
  110. * @return \think\Request
  111. */
  112. public static function instance($options = [])
  113. {
  114. if (is_null(self::$instance)) {
  115. self::$instance = new static($options);
  116. }
  117. return self::$instance;
  118. }
  119. /**
  120. * 设置或获取当前包含协议的域名
  121. * @access public
  122. * @param string $domain 域名
  123. * @return string
  124. */
  125. public function domain($domain = null)
  126. {
  127. if (!is_null($domain)) {
  128. $this->domain = $domain;
  129. return $this;
  130. }
  131. return $this->domain;
  132. }
  133. /**
  134. * 设置或获取当前完整URL 包括QUERY_STRING
  135. * @access public
  136. * @param string|true $url URL地址 true 带域名获取
  137. * @return string
  138. */
  139. public function url($url = null)
  140. {
  141. if (!is_null($url) && true !== $url) {
  142. $this->url = $url;
  143. return $this;
  144. } elseif (!$this->url) {
  145. if (IS_CLI) {
  146. $this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  147. } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
  148. $this->url = $_SERVER['HTTP_X_REWRITE_URL'];
  149. } elseif (isset($_SERVER['REQUEST_URI'])) {
  150. $this->url = $_SERVER['REQUEST_URI'];
  151. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
  152. $this->url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
  153. } else {
  154. $this->url = '';
  155. }
  156. }
  157. return true === $url ? $this->domain() . $this->url : $this->url;
  158. }
  159. /**
  160. * 设置或获取当前URL 不含QUERY_STRING
  161. * @access public
  162. * @param string $url URL地址
  163. * @return string
  164. */
  165. public function baseUrl($url = null)
  166. {
  167. if (!is_null($url) && true !== $url) {
  168. $this->baseUrl = $url;
  169. return $this;
  170. } elseif (!$this->baseUrl) {
  171. $str = $this->url();
  172. $this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str;
  173. }
  174. return true === $url ? $this->domain() . $this->baseUrl : $this->baseUrl;
  175. }
  176. /**
  177. * 设置或获取当前执行的文件 SCRIPT_NAME
  178. * @access public
  179. * @param string $file 当前执行的文件
  180. * @return string
  181. */
  182. public function baseFile($file = null)
  183. {
  184. if (!is_null($file) && true !== $file) {
  185. $this->baseFile = $file;
  186. return $this;
  187. } elseif (!$this->baseFile) {
  188. $url = '';
  189. if (!IS_CLI) {
  190. $script_name = basename($_SERVER['SCRIPT_FILENAME']);
  191. if (basename($_SERVER['SCRIPT_NAME']) === $script_name) {
  192. $url = $_SERVER['SCRIPT_NAME'];
  193. } elseif (basename($_SERVER['PHP_SELF']) === $script_name) {
  194. $url = $_SERVER['PHP_SELF'];
  195. } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $script_name) {
  196. $url = $_SERVER['ORIG_SCRIPT_NAME'];
  197. } elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $script_name)) !== false) {
  198. $url = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $script_name;
  199. } elseif (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) {
  200. $url = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']));
  201. }
  202. }
  203. $this->baseFile = $url;
  204. }
  205. return true === $file ? $this->domain() . $this->baseFile : $this->baseFile;
  206. }
  207. /**
  208. * 设置或获取URL访问根地址
  209. * @access public
  210. * @param string $url URL地址
  211. * @return string
  212. */
  213. public function root($url = null)
  214. {
  215. if (!is_null($url) && true !== $url) {
  216. $this->root = $url;
  217. return $this;
  218. } elseif (!$this->root) {
  219. $file = $this->baseFile();
  220. if ($file && 0 !== strpos($this->url(), $file)) {
  221. $file = str_replace('\\', '/', dirname($file));
  222. }
  223. $this->root = rtrim($file, '/');
  224. }
  225. return true === $url ? $this->domain() . $this->root : $this->root;
  226. }
  227. /**
  228. * 获取当前请求URL的pathinfo信息(含URL后缀)
  229. * @access public
  230. * @return string
  231. */
  232. public function pathinfo()
  233. {
  234. if (is_null($this->pathinfo)) {
  235. if (isset($_GET[Config::get('var_pathinfo')])) {
  236. // 判断URL里面是否有兼容模式参数
  237. $_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')];
  238. unset($_GET[Config::get('var_pathinfo')]);
  239. } elseif (IS_CLI) {
  240. // CLI模式下 index.php module/controller/action/params/...
  241. $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  242. }
  243. // 分析PATHINFO信息
  244. if (!isset($_SERVER['PATH_INFO'])) {
  245. foreach (Config::get('pathinfo_fetch') as $type) {
  246. if (!empty($_SERVER[$type])) {
  247. $_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME'])) ?
  248. substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
  249. break;
  250. }
  251. }
  252. }
  253. $this->pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : ltrim($_SERVER['PATH_INFO'], '/');
  254. }
  255. return $this->pathinfo;
  256. }
  257. /**
  258. * 获取当前请求URL的pathinfo信息(不含URL后缀)
  259. * @access public
  260. * @return string
  261. */
  262. public function path()
  263. {
  264. if (is_null($this->path)) {
  265. $suffix = Config::get('url_html_suffix');
  266. $pathinfo = $this->pathinfo();
  267. if (false === $suffix) {
  268. // 禁止伪静态访问
  269. $this->path = $pathinfo;
  270. } elseif ($suffix) {
  271. // 去除正常的URL后缀
  272. $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
  273. } else {
  274. // 允许任何后缀访问
  275. $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
  276. }
  277. }
  278. return $this->path;
  279. }
  280. /**
  281. * 当前URL的访问后缀
  282. * @access public
  283. * @return string
  284. */
  285. public function ext()
  286. {
  287. return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
  288. }
  289. /**
  290. * 获取当前请求的时间
  291. * @access public
  292. * @param bool $float 是否使用浮点类型
  293. * @return integer|float
  294. */
  295. public function time($float = false)
  296. {
  297. return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
  298. }
  299. /**
  300. * 当前请求的资源类型
  301. * @access public
  302. * @return false|string
  303. */
  304. public function type()
  305. {
  306. $accept = $this->server('HTTP_ACCEPT');
  307. if (empty($accept)) {
  308. return false;
  309. }
  310. foreach ($this->mimeType as $key => $val) {
  311. $array = explode(',', $val);
  312. foreach ($array as $k => $v) {
  313. if (stristr($accept, $v)) {
  314. return $key;
  315. }
  316. }
  317. }
  318. return false;
  319. }
  320. /**
  321. * 设置资源类型
  322. * @access public
  323. * @param string|array $type 资源类型名
  324. * @param string $val 资源类型
  325. * @return void
  326. */
  327. public function mimeType($type, $val = '')
  328. {
  329. if (is_array($type)) {
  330. $this->mimeType = array_merge($this->mimeType, $type);
  331. } else {
  332. $this->mimeType[$type] = $val;
  333. }
  334. }
  335. /**
  336. * 当前的请求类型
  337. * @access public
  338. * @param bool $method true 获取原始请求类型
  339. * @return string
  340. */
  341. public function method($method = false)
  342. {
  343. if (true === $method) {
  344. // 获取原始请求类型
  345. return IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
  346. } elseif (!$this->method) {
  347. if (isset($_POST[Config::get('var_method')])) {
  348. $this->method = strtoupper($_POST[Config::get('var_method')]);
  349. $this->{$this->method}($_POST);
  350. } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
  351. $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
  352. } else {
  353. $this->method = IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
  354. }
  355. }
  356. return $this->method;
  357. }
  358. /**
  359. * 是否为GET请求
  360. * @access public
  361. * @return bool
  362. */
  363. public function isGet()
  364. {
  365. return $this->method() == 'GET';
  366. }
  367. /**
  368. * 是否为POST请求
  369. * @access public
  370. * @return bool
  371. */
  372. public function isPost()
  373. {
  374. return $this->method() == 'POST';
  375. }
  376. /**
  377. * 是否为PUT请求
  378. * @access public
  379. * @return bool
  380. */
  381. public function isPut()
  382. {
  383. return $this->method() == 'PUT';
  384. }
  385. /**
  386. * 是否为DELTE请求
  387. * @access public
  388. * @return bool
  389. */
  390. public function isDelete()
  391. {
  392. return $this->method() == 'DELETE';
  393. }
  394. /**
  395. * 是否为HEAD请求
  396. * @access public
  397. * @return bool
  398. */
  399. public function isHead()
  400. {
  401. return $this->method() == 'HEAD';
  402. }
  403. /**
  404. * 是否为PATCH请求
  405. * @access public
  406. * @return bool
  407. */
  408. public function isPatch()
  409. {
  410. return $this->method() == 'PATCH';
  411. }
  412. /**
  413. * 是否为OPTIONS请求
  414. * @access public
  415. * @return bool
  416. */
  417. public function isOptions()
  418. {
  419. return $this->method() == 'OPTIONS';
  420. }
  421. /**
  422. * 是否为cli
  423. * @access public
  424. * @return bool
  425. */
  426. public function isCli()
  427. {
  428. return PHP_SAPI == 'cli';
  429. }
  430. /**
  431. * 是否为cgi
  432. * @access public
  433. * @return bool
  434. */
  435. public function isCgi()
  436. {
  437. return strpos(PHP_SAPI, 'cgi') === 0;
  438. }
  439. /**
  440. * 获取当前请求的参数
  441. * @access public
  442. * @param string|array $name 变量名
  443. * @param mixed $default 默认值
  444. * @param string|array $filter 过滤方法
  445. * @return mixed
  446. */
  447. public function param($name = '', $default = null, $filter = '')
  448. {
  449. if (empty($this->param)) {
  450. $method = $this->method(true);
  451. // 自动获取请求变量
  452. switch ($method) {
  453. case 'POST':
  454. $vars = $this->post(false);
  455. break;
  456. case 'PUT':
  457. case 'DELETE':
  458. case 'PATCH':
  459. $vars = $this->put(false);
  460. break;
  461. default:
  462. $vars = [];
  463. }
  464. // 当前请求参数和URL地址中的参数合并
  465. $this->param = array_merge($this->get(false), $vars, $this->route(false));
  466. }
  467. return $this->input($this->param, $name, $default, $filter);
  468. }
  469. /**
  470. * 设置获取路由参数
  471. * @access public
  472. * @param string|array $name 变量名
  473. * @param mixed $default 默认值
  474. * @param string|array $filter 过滤方法
  475. * @return mixed
  476. */
  477. public function route($name = '', $default = null, $filter = '')
  478. {
  479. if (is_array($name)) {
  480. $this->param = [];
  481. return $this->route = array_merge($this->route, $name);
  482. }
  483. return $this->input($this->route, $name, $default, $filter);
  484. }
  485. /**
  486. * 设置获取GET参数
  487. * @access public
  488. * @param string|array $name 变量名
  489. * @param mixed $default 默认值
  490. * @param string|array $filter 过滤方法
  491. * @return mixed
  492. */
  493. public function get($name = '', $default = null, $filter = '')
  494. {
  495. if (empty($this->get)) {
  496. $this->get = $_GET;
  497. }
  498. if (is_array($name)) {
  499. $this->param = [];
  500. return $this->get = array_merge($this->get, $name);
  501. }
  502. return $this->input($this->get, $name, $default, $filter);
  503. }
  504. /**
  505. * 设置获取POST参数
  506. * @access public
  507. * @param string $name 变量名
  508. * @param mixed $default 默认值
  509. * @param string|array $filter 过滤方法
  510. * @return mixed
  511. */
  512. public function post($name = '', $default = null, $filter = '')
  513. {
  514. if (empty($this->post)) {
  515. $content = $this->input;
  516. if (empty($_POST) && false !== strpos($this->contentType(), 'application/json')) {
  517. $this->post = (array) json_decode($content, true);
  518. } else {
  519. $this->post = $_POST;
  520. }
  521. }
  522. if (is_array($name)) {
  523. $this->param = [];
  524. return $this->post = array_merge($this->post, $name);
  525. }
  526. return $this->input($this->post, $name, $default, $filter);
  527. }
  528. /**
  529. * 设置获取PUT参数
  530. * @access public
  531. * @param string|array $name 变量名
  532. * @param mixed $default 默认值
  533. * @param string|array $filter 过滤方法
  534. * @return mixed
  535. */
  536. public function put($name = '', $default = null, $filter = '')
  537. {
  538. if (is_null($this->put)) {
  539. $content = $this->input;
  540. if (false !== strpos($this->contentType(), 'application/json')) {
  541. $this->put = (array) json_decode($content, true);
  542. } else {
  543. parse_str($content, $this->put);
  544. }
  545. }
  546. if (is_array($name)) {
  547. $this->param = [];
  548. return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
  549. }
  550. return $this->input($this->put, $name, $default, $filter);
  551. }
  552. /**
  553. * 设置获取DELETE参数
  554. * @access public
  555. * @param string|array $name 变量名
  556. * @param mixed $default 默认值
  557. * @param string|array $filter 过滤方法
  558. * @return mixed
  559. */
  560. public function delete($name = '', $default = null, $filter = '')
  561. {
  562. return $this->put($name, $default, $filter);
  563. }
  564. /**
  565. * 设置获取PATCH参数
  566. * @access public
  567. * @param string|array $name 变量名
  568. * @param mixed $default 默认值
  569. * @param string|array $filter 过滤方法
  570. * @return mixed
  571. */
  572. public function patch($name = '', $default = null, $filter = '')
  573. {
  574. return $this->put($name, $default, $filter);
  575. }
  576. /**
  577. * 获取request变量
  578. * @param string $name 数据名称
  579. * @param string $default 默认值
  580. * @param string|array $filter 过滤方法
  581. * @return mixed
  582. */
  583. public function request($name = '', $default = null, $filter = '')
  584. {
  585. if (empty($this->request)) {
  586. $this->request = $_REQUEST;
  587. }
  588. if (is_array($name)) {
  589. $this->param = [];
  590. return $this->request = array_merge($this->request, $name);
  591. }
  592. return $this->input($this->request, $name, $default, $filter);
  593. }
  594. /**
  595. * 获取session数据
  596. * @access public
  597. * @param string|array $name 数据名称
  598. * @param string $default 默认值
  599. * @param string|array $filter 过滤方法
  600. * @return mixed
  601. */
  602. public function session($name = '', $default = null, $filter = '')
  603. {
  604. if (empty($this->session)) {
  605. $this->session = Session::get();
  606. }
  607. if (is_array($name)) {
  608. return $this->session = array_merge($this->session, $name);
  609. }
  610. return $this->input($this->session, $name, $default, $filter);
  611. }
  612. /**
  613. * 获取server参数
  614. * @access public
  615. * @param string|array $name 数据名称
  616. * @param string $default 默认值
  617. * @param string|array $filter 过滤方法
  618. * @return mixed
  619. */
  620. public function server($name = '', $default = null, $filter = '')
  621. {
  622. if (empty($this->server)) {
  623. $this->server = $_SERVER;
  624. }
  625. if (is_array($name)) {
  626. return $this->server = array_merge($this->server, $name);
  627. }
  628. return $this->input($this->server, false === $name ? false : strtoupper($name), $default, $filter);
  629. }
  630. /**
  631. * 获取环境变量
  632. * @param string|array $name 数据名称
  633. * @param string $default 默认值
  634. * @param string|array $filter 过滤方法
  635. * @return mixed
  636. */
  637. public function env($name = '', $default = null, $filter = '')
  638. {
  639. if (empty($this->env)) {
  640. $this->env = $_ENV;
  641. }
  642. if (is_array($name)) {
  643. return $this->env = array_merge($this->env, $name);
  644. }
  645. return $this->input($this->env, false === $name ? false : strtoupper($name), $default, $filter);
  646. }
  647. /**
  648. * 设置或者获取当前的Header
  649. * @access public
  650. * @param string|array $name header名称
  651. * @param string $default 默认值
  652. * @return string
  653. */
  654. public function header($name = '', $default = null)
  655. {
  656. if (empty($this->header)) {
  657. $header = [];
  658. if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
  659. $header = $result;
  660. } else {
  661. $server = $this->server ?: $_SERVER;
  662. foreach ($server as $key => $val) {
  663. if (0 === strpos($key, 'HTTP_')) {
  664. $key = str_replace('_', '-', strtolower(substr($key, 5)));
  665. $header[$key] = $val;
  666. }
  667. }
  668. if (isset($server['CONTENT_TYPE'])) {
  669. $header['content-type'] = $server['CONTENT_TYPE'];
  670. }
  671. if (isset($server['CONTENT_LENGTH'])) {
  672. $header['content-length'] = $server['CONTENT_LENGTH'];
  673. }
  674. }
  675. $this->header = array_change_key_case($header);
  676. }
  677. if (is_array($name)) {
  678. return $this->header = array_merge($this->header, $name);
  679. }
  680. if ('' === $name) {
  681. return $this->header;
  682. }
  683. $name = str_replace('_', '-', strtolower($name));
  684. return isset($this->header[$name]) ? $this->header[$name] : $default;
  685. }
  686. /**
  687. * 获取变量 支持过滤和默认值
  688. * @param array $data 数据源
  689. * @param string|false $name 字段名
  690. * @param mixed $default 默认值
  691. * @param string|array $filter 过滤函数
  692. * @return mixed
  693. */
  694. public function input($data = [], $name = '', $default = null, $filter = '')
  695. {
  696. if (false === $name) {
  697. // 获取原始数据
  698. return $data;
  699. }
  700. $name = (string) $name;
  701. if ('' != $name) {
  702. // 解析name
  703. if (strpos($name, '/')) {
  704. list($name, $type) = explode('/', $name);
  705. } else {
  706. $type = 's';
  707. }
  708. // 按.拆分成多维数组进行判断
  709. foreach (explode('.', $name) as $val) {
  710. if (isset($data[$val])) {
  711. $data = $data[$val];
  712. } else {
  713. // 无输入数据,返回默认值
  714. return $default;
  715. }
  716. }
  717. if (is_object($data)) {
  718. return $data;
  719. }
  720. }
  721. // 解析过滤器
  722. $filter = $this->getFilter($filter, $default);
  723. if (is_array($data)) {
  724. array_walk_recursive($data, [$this, 'filterValue'], $filter);
  725. reset($data);
  726. } else {
  727. $this->filterValue($data, $name, $filter);
  728. }
  729. if (isset($type) && $data !== $default) {
  730. // 强制类型转换
  731. $this->typeCast($data, $type);
  732. }
  733. return $data;
  734. }
  735. /**
  736. * 设置或获取当前的过滤规则
  737. * @param mixed $filter 过滤规则
  738. * @return mixed
  739. */
  740. public function filter($filter = null)
  741. {
  742. if (is_null($filter)) {
  743. return $this->filter;
  744. } else {
  745. $this->filter = $filter;
  746. }
  747. }
  748. protected function getFilter($filter, $default)
  749. {
  750. if (is_null($filter)) {
  751. $filter = [];
  752. } else {
  753. $filter = $filter ?: $this->filter;
  754. if (is_string($filter) && false === strpos($filter, '/')) {
  755. $filter = explode(',', $filter);
  756. } else {
  757. $filter = (array) $filter;
  758. }
  759. }
  760. $filter[] = $default;
  761. return $filter;
  762. }
  763. /**
  764. * 递归过滤给定的值
  765. * @param mixed $value 键值
  766. * @param mixed $key 键名
  767. * @param array $filters 过滤方法+默认值
  768. * @return mixed
  769. */
  770. private function filterValue(&$value, $key, $filters)
  771. {
  772. $default = array_pop($filters);
  773. foreach ($filters as $filter) {
  774. if (is_callable($filter)) {
  775. // 调用函数或者方法过滤
  776. $value = call_user_func($filter, $value);
  777. } elseif (is_scalar($value)) {
  778. if (false !== strpos($filter, '/')) {
  779. // 正则过滤
  780. if (!preg_match($filter, $value)) {
  781. // 匹配不成功返回默认值
  782. $value = $default;
  783. break;
  784. }
  785. } elseif (!empty($filter)) {
  786. // filter函数不存在时, 则使用filter_var进行过滤
  787. // filter为非整形值时, 调用filter_id取得过滤id
  788. $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
  789. if (false === $value) {
  790. $value = $default;
  791. break;
  792. }
  793. }
  794. }
  795. }
  796. return $this->filterExp($value);
  797. }
  798. /**
  799. * 过滤表单中的表达式
  800. * @param string $value
  801. * @return void
  802. */
  803. public function filterExp(&$value)
  804. {
  805. // 过滤查询特殊字符
  806. if (is_string($value) && preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i', $value)) {
  807. $value .= ' ';
  808. }
  809. // TODO 其他安全过滤
  810. }
  811. /**
  812. * 强制类型转换
  813. * @param string $data
  814. * @param string $type
  815. * @return mixed
  816. */
  817. private function typeCast(&$data, $type)
  818. {
  819. switch (strtolower($type)) {
  820. // 数组
  821. case 'a':
  822. $data = (array) $data;
  823. break;
  824. // 数字
  825. case 'd':
  826. $data = (int) $data;
  827. break;
  828. // 浮点
  829. case 'f':
  830. $data = (float) $data;
  831. break;
  832. // 布尔
  833. case 'b':
  834. $data = (boolean) $data;
  835. break;
  836. // 字符串
  837. case 's':
  838. default:
  839. if (is_scalar($data)) {
  840. $data = (string) $data;
  841. } else {
  842. throw new \InvalidArgumentException('variable type error:' . gettype($data));
  843. }
  844. }
  845. }
  846. /**
  847. * 是否存在某个请求参数
  848. * @access public
  849. * @param string $name 变量名
  850. * @param string $type 变量类型
  851. * @param bool $checkEmpty 是否检测空值
  852. * @return mixed
  853. */
  854. public function has($name, $type = 'param', $checkEmpty = false)
  855. {
  856. if (empty($this->$type)) {
  857. $param = $this->$type();
  858. } else {
  859. $param = $this->$type;
  860. }
  861. // 按.拆分成多维数组进行判断
  862. foreach (explode('.', $name) as $val) {
  863. if (isset($param[$val])) {
  864. $param = $param[$val];
  865. } else {
  866. return false;
  867. }
  868. }
  869. return ($checkEmpty && '' === $param) ? false : true;
  870. }
  871. /**
  872. * 获取指定的参数
  873. * @access public
  874. * @param string|array $name 变量名
  875. * @param string $type 变量类型
  876. * @return mixed
  877. */
  878. public function only($name, $type = 'param')
  879. {
  880. $param = $this->$type();
  881. if (is_string($name)) {
  882. $name = explode(',', $name);
  883. }
  884. $item = [];
  885. foreach ($name as $key) {
  886. if (isset($param[$key])) {
  887. $item[$key] = $param[$key];
  888. }
  889. }
  890. return $item;
  891. }
  892. /**
  893. * 排除指定参数获取
  894. * @access public
  895. * @param string|array $name 变量名
  896. * @param string $type 变量类型
  897. * @return mixed
  898. */
  899. public function except($name, $type = 'param')
  900. {
  901. $param = $this->$type();
  902. if (is_string($name)) {
  903. $name = explode(',', $name);
  904. }
  905. foreach ($name as $key) {
  906. if (isset($param[$key])) {
  907. unset($param[$key]);
  908. }
  909. }
  910. return $param;
  911. }
  912. /**
  913. * 当前是否Ajax请求
  914. * @access public
  915. * @param bool $ajax true 获取原始ajax请求
  916. * @return bool
  917. */
  918. public function isAjax($ajax = false)
  919. {
  920. $value = $this->server('HTTP_X_REQUESTED_WITH', '', 'strtolower');
  921. $result = ('xmlhttprequest' == $value) ? true : false;
  922. if (true === $ajax) {
  923. return $result;
  924. } else {
  925. return $this->param(Config::get('var_ajax')) ? true : $result;
  926. }
  927. }
  928. /**
  929. * 当前是否Pjax请求
  930. * @access public
  931. * @param bool $pjax true 获取原始pjax请求
  932. * @return bool
  933. */
  934. public function isPjax($pjax = false)
  935. {
  936. $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
  937. if (true === $pjax) {
  938. return $result;
  939. } else {
  940. return $this->param(Config::get('var_pjax')) ? true : $result;
  941. }
  942. }
  943. /**
  944. * 获取客户端IP地址
  945. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  946. * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
  947. * @return mixed
  948. */
  949. public function ip($type = 0, $adv = false)
  950. {
  951. $type = $type ? 1 : 0;
  952. static $ip = null;
  953. if (null !== $ip) {
  954. return $ip[$type];
  955. }
  956. if ($adv) {
  957. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  958. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  959. $pos = array_search('unknown', $arr);
  960. if (false !== $pos) {
  961. unset($arr[$pos]);
  962. }
  963. $ip = trim(current($arr));
  964. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  965. $ip = $_SERVER['HTTP_CLIENT_IP'];
  966. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  967. $ip = $_SERVER['REMOTE_ADDR'];
  968. }
  969. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  970. $ip = $_SERVER['REMOTE_ADDR'];
  971. }
  972. // IP地址合法验证
  973. $long = sprintf("%u", ip2long($ip));
  974. $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
  975. return $ip[$type];
  976. }
  977. /**
  978. * 检测是否使用手机访问
  979. * @access public
  980. * @return bool
  981. */
  982. public function isMobile()
  983. {
  984. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  985. return true;
  986. } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
  987. return true;
  988. } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
  989. return true;
  990. } 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'])) {
  991. return true;
  992. } else {
  993. return false;
  994. }
  995. }
  996. /**
  997. * 当前请求URL地址中的query参数
  998. * @access public
  999. * @return string
  1000. */
  1001. public function query()
  1002. {
  1003. return $this->server('QUERY_STRING');
  1004. }
  1005. /**
  1006. * 当前请求的host
  1007. * @access public
  1008. * @return string
  1009. */
  1010. public function host()
  1011. {
  1012. return $this->server('HTTP_HOST');
  1013. }
  1014. /**
  1015. * 当前请求URL地址中的port参数
  1016. * @access public
  1017. * @return integer
  1018. */
  1019. public function port()
  1020. {
  1021. return $this->server('SERVER_PORT');
  1022. }
  1023. /**
  1024. * 当前请求 SERVER_PROTOCOL
  1025. * @access public
  1026. * @return integer
  1027. */
  1028. public function protocol()
  1029. {
  1030. return $this->server('SERVER_PROTOCOL');
  1031. }
  1032. /**
  1033. * 当前请求 REMOTE_PORT
  1034. * @access public
  1035. * @return integer
  1036. */
  1037. public function remotePort()
  1038. {
  1039. return $this->server('REMOTE_PORT');
  1040. }
  1041. /**
  1042. * 当前请求 HTTP_CONTENT_TYPE
  1043. * @access public
  1044. * @return string
  1045. */
  1046. public function contentType()
  1047. {
  1048. $contentType = $this->server('CONTENT_TYPE');
  1049. if ($contentType) {
  1050. if (strpos($contentType, ';')) {
  1051. list($type) = explode(';', $contentType);
  1052. } else {
  1053. $type = $contentType;
  1054. }
  1055. return trim($type);
  1056. }
  1057. return '';
  1058. }
  1059. /**
  1060. * 获取当前请求的路由信息
  1061. * @access public
  1062. * @param array $route 路由名称
  1063. * @return array
  1064. */
  1065. public function routeInfo($route = [])
  1066. {
  1067. if (!empty($route)) {
  1068. $this->routeInfo = $route;
  1069. } else {
  1070. return $this->routeInfo;
  1071. }
  1072. }
  1073. /**
  1074. * 设置或者获取当前请求的调度信息
  1075. * @access public
  1076. * @param array $dispatch 调度信息
  1077. * @return array
  1078. */
  1079. public function dispatch($dispatch = null)
  1080. {
  1081. if (!is_null($dispatch)) {
  1082. $this->dispatch = $dispatch;
  1083. }
  1084. return $this->dispatch;
  1085. }
  1086. /**
  1087. * 设置或者获取当前的模块名
  1088. * @access public
  1089. * @param string $module 模块名
  1090. * @return string|Request
  1091. */
  1092. public function module($module = null)
  1093. {
  1094. if (!is_null($module)) {
  1095. $this->module = $module;
  1096. return $this;
  1097. } else {
  1098. return $this->module ?: '';
  1099. }
  1100. }
  1101. /**
  1102. * 设置或者获取当前的控制器名
  1103. * @access public
  1104. * @param string $controller 控制器名
  1105. * @return string|Request
  1106. */
  1107. public function controller($controller = null)
  1108. {
  1109. if (!is_null($controller)) {
  1110. $this->controller = $controller;
  1111. return $this;
  1112. } else {
  1113. return $this->controller ?: '';
  1114. }
  1115. }
  1116. /**
  1117. * 设置或者获取当前的操作名
  1118. * @access public
  1119. * @param string $action 操作名
  1120. * @return string|Request
  1121. */
  1122. public function action($action = null)
  1123. {
  1124. if (!is_null($action)) {
  1125. $this->action = $action;
  1126. return $this;
  1127. } else {
  1128. return $this->action ?: '';
  1129. }
  1130. }
  1131. /**
  1132. * 设置或者获取当前的语言
  1133. * @access public
  1134. * @param string $lang 语言名
  1135. * @return string|Request
  1136. */
  1137. public function langset($lang = null)
  1138. {
  1139. if (!is_null($lang)) {
  1140. $this->langset = $lang;
  1141. return $this;
  1142. } else {
  1143. return $this->langset ?: '';
  1144. }
  1145. }
  1146. /**
  1147. * 设置或者获取当前请求的content
  1148. * @access public
  1149. * @return string
  1150. */
  1151. public function getContent()
  1152. {
  1153. if (is_null($this->content)) {
  1154. $this->content = $this->input;
  1155. }
  1156. return $this->content;
  1157. }
  1158. /**
  1159. * 获取当前请求的php://input
  1160. * @access public
  1161. * @return string
  1162. */
  1163. public function getInput()
  1164. {
  1165. return $this->input;
  1166. }
  1167. /**
  1168. * 生成请求令牌
  1169. * @access public
  1170. * @param string $name 令牌名称
  1171. * @param mixed $type 令牌生成方法
  1172. * @return string
  1173. */
  1174. public function token($name = '__token__', $type = 'md5')
  1175. {
  1176. $type = is_callable($type) ? $type : 'md5';
  1177. $token = call_user_func($type, $_SERVER['REQUEST_TIME_FLOAT']);
  1178. if ($this->isAjax()) {
  1179. header($name . ': ' . $token);
  1180. }
  1181. Session::set($name, $token);
  1182. return $token;
  1183. }
  1184. }