PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Request.php

http://typecho.googlecode.com/
PHP | 630 lines | 304 code | 74 blank | 252 comment | 77 complexity | cdf8132fdf7c8edef11ea0f1d2d42458 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. class Te_Request
  3. {
  4. /**
  5. * ???ip
  6. *
  7. * @var mixed
  8. * @access private
  9. */
  10. private $_ip;
  11. /**
  12. * ?????
  13. *
  14. * @var integer
  15. * @access private
  16. */
  17. private $_now;
  18. /**
  19. * _request
  20. *
  21. * @var array
  22. * @access private
  23. */
  24. private $_httpRequest = NULL;
  25. /**
  26. * ????
  27. *
  28. * @var string
  29. * @access private
  30. */
  31. private $_baseUrl = NULL;
  32. /**
  33. * ????
  34. *
  35. * @access private
  36. * @var string
  37. */
  38. private $_pathInfo = NULL;
  39. /**
  40. * ????
  41. *
  42. * @var string
  43. * @access private
  44. */
  45. private $_method;
  46. /**
  47. * ??????
  48. *
  49. * @var string
  50. * @access private
  51. */
  52. private $_requestUri;
  53. /**
  54. * ????
  55. *
  56. * @var array
  57. * @access private
  58. */
  59. private $_params = array();
  60. /**
  61. * json????
  62. *
  63. * @var array
  64. * @access private
  65. */
  66. private $_jsonParams = array();
  67. /**
  68. * ?????
  69. *
  70. * @var boolean
  71. * @access private
  72. */
  73. private $_commandMode = NULL;
  74. /**
  75. * ??
  76. *
  77. * @var string
  78. * @access private
  79. */
  80. private $_referer = false;
  81. /**
  82. * ??ssl
  83. *
  84. * @var mixed
  85. * @access private
  86. */
  87. private $_isSecure = NULL;
  88. /**
  89. * ??????
  90. *
  91. * @param string $key
  92. * @access private
  93. * @return string
  94. */
  95. private function getArg($key)
  96. {
  97. $value = strlen($key) > 1 ? getopt('', $key . ':') : getopt($key . ':');
  98. return $value[$key];
  99. }
  100. /**
  101. * ??????????
  102. *
  103. * @static
  104. * @access public
  105. * @return void
  106. */
  107. public function commandMode()
  108. {
  109. if (NULL === $this->_commandMode) {
  110. $this->_commandMode = isset($_SERVER['_']) || isset($_SERVER['COMMAND_MODE']);
  111. }
  112. return $this->_commandMode;
  113. }
  114. /**
  115. * ????????
  116. *
  117. * @static
  118. * @access public
  119. * @return void
  120. */
  121. public function getAgent()
  122. {
  123. return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
  124. }
  125. /**
  126. * ?????????????
  127. *
  128. * @static
  129. * @access public
  130. * @return void
  131. */
  132. public function getClientId()
  133. {
  134. return md5($this->getAgent() . "\0" . $this->getIp());
  135. }
  136. /**
  137. * ????????
  138. *
  139. * @param string $key ???
  140. * @param mixed $default
  141. * @access public
  142. * @return void
  143. */
  144. public function get($key, $default = NULL)
  145. {
  146. if (!isset($this->_params[$key])) {
  147. $paramKeys = explode('|', $key);
  148. $this->_params[$key] = $default;
  149. $request = array();
  150. if ($this->commandMode()) {
  151. $short = '';
  152. $long = array();
  153. foreach ($paramKeys as $paramKey) {
  154. if (strlen($paramKey) > 1) {
  155. $long[] = $paramKey . '::';
  156. } else {
  157. $short .= $paramKey . '::';
  158. }
  159. }
  160. $request = getopt($short, $long);
  161. } else {
  162. $request = $this->getHttpRequest();
  163. }
  164. foreach ($paramKeys as $paramKey) {
  165. if (isset($request[$paramKey])) {
  166. $this->_params[$key] = $request[$paramKey];
  167. } else if (false !== strpos($paramKey, ':')) {
  168. list($jsonParamKey, $jsonKey) = explode(':', $paramKey);
  169. $json = $this->getJson($jsonParamKey);
  170. if (isset($json[$jsonKey])) {
  171. $this->_params[$key] = $json[$jsonKey];
  172. }
  173. }
  174. }
  175. }
  176. return $this->_params[$key];
  177. }
  178. /**
  179. * ??????json??
  180. *
  181. * @param mixed $key
  182. * @access public
  183. * @return void
  184. */
  185. public function getJson($key)
  186. {
  187. if (!isset($this->_jsonParams[$key])) {
  188. $this->_jsonParams[$key] = NULL;
  189. if (!empty($_REQUEST[$key])) {
  190. $result = json_decode($this->get($key), true);
  191. if (NULL !== $result) {
  192. $this->_jsonParams[$key] = $result;
  193. }
  194. }
  195. }
  196. return $this->_jsonParams[$key];
  197. }
  198. /**
  199. * ????????
  200. *
  201. * @param mixed $key
  202. * @access public
  203. * @return array
  204. */
  205. public function getArray($key)
  206. {
  207. if (is_array($key)) {
  208. $result = array();
  209. foreach ($key as $k) {
  210. $result[$k] = $this->get($k);
  211. }
  212. return $result;
  213. } else {
  214. $result = $this->get($key, array());
  215. return is_array($result) ? $result : array($result);
  216. }
  217. }
  218. /**
  219. * ????????
  220. *
  221. * @static
  222. * @access public
  223. * @return array
  224. */
  225. public function getHttpRequest()
  226. {
  227. if (NULL === $this->_httpRequest) {
  228. $this->_httpRequest = array_merge($_POST, $_GET);
  229. }
  230. return $this->_httpRequest;
  231. }
  232. /**
  233. * ?????COOKIE?
  234. *
  235. * @access public
  236. * @param string $key ?????
  237. * @param string $default ?????
  238. * @return mixed
  239. */
  240. public function getCookie($key, $default = NULL)
  241. {
  242. return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default;
  243. }
  244. /**
  245. * ?????ip
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. public function getIp()
  251. {
  252. if (empty($this->_ip)) {
  253. switch (true) {
  254. case !empty($_SERVER['HTTP_X_FORWARDED_FOR']):
  255. list($this->_ip) = array_map('trim', explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
  256. break;
  257. case !empty($_SERVER['HTTP_CLIENT_IP']):
  258. $this->_ip = $_SERVER['HTTP_CLIENT_IP'];
  259. break;
  260. case !empty($_SERVER['REMOTE_ADDR']):
  261. $this->_ip = $_SERVER['REMOTE_ADDR'];
  262. break;
  263. default:
  264. $this->_ip = '-';
  265. break;
  266. }
  267. }
  268. return $this->_ip;
  269. }
  270. /**
  271. * ???????
  272. *
  273. * @static
  274. * @access public
  275. * @return void
  276. */
  277. public function getNow()
  278. {
  279. if (empty($this->_now)) {
  280. $this->_now = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
  281. }
  282. return $this->_now;
  283. }
  284. /**
  285. * ????
  286. *
  287. * @access public
  288. * @return string
  289. */
  290. public function getMethod()
  291. {
  292. if (empty($this->_method)) {
  293. $this->_method = strtoupper($_SERVER['REQUEST_METHOD']);
  294. }
  295. return $this->_method;
  296. }
  297. /**
  298. * ??????
  299. *
  300. * @access public
  301. * @return string
  302. */
  303. public function getRequestUri()
  304. {
  305. if (!empty($this->_requestUri)) {
  306. return $this->_requestUri;
  307. }
  308. //??requestUri
  309. $requestUri = '/';
  310. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
  311. $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
  312. } elseif (
  313. // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
  314. isset($_SERVER['IIS_WasUrlRewritten'])
  315. && $_SERVER['IIS_WasUrlRewritten'] == '1'
  316. && isset($_SERVER['UNENCODED_URL'])
  317. && $_SERVER['UNENCODED_URL'] != ''
  318. ) {
  319. $requestUri = $_SERVER['UNENCODED_URL'];
  320. } elseif (isset($_SERVER['REQUEST_URI'])) {
  321. $requestUri = $_SERVER['REQUEST_URI'];
  322. if (isset($_SERVER['HTTP_HOST']) && strstr($requestUri, $_SERVER['HTTP_HOST'])) {
  323. $parts = @parse_url($requestUri);
  324. if (false !== $parts) {
  325. $requestUri = (empty($parts['path']) ? '' : $parts['path'])
  326. . ((empty($parts['query'])) ? '' : '?' . $parts['query']);
  327. }
  328. }
  329. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  330. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  331. if (!empty($_SERVER['QUERY_STRING'])) {
  332. $requestUri .= '?' . $_SERVER['QUERY_STRING'];
  333. }
  334. }
  335. return $this->_requestUri = $requestUri;
  336. }
  337. /**
  338. * ??????
  339. *
  340. * @static
  341. * @access public
  342. * @return string
  343. */
  344. public function getBaseUrl()
  345. {
  346. //??baseUrl
  347. if (NULL !== $this->_baseUrl) {
  348. return $this->_baseUrl;
  349. }
  350. $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : '';
  351. if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
  352. $baseUrl = $_SERVER['SCRIPT_NAME'];
  353. } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
  354. $baseUrl = $_SERVER['PHP_SELF'];
  355. } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
  356. $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
  357. } else {
  358. // Backtrack up the script_filename to find the portion matching
  359. // php_self
  360. $path = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
  361. $file = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
  362. $segs = explode('/', trim($file, '/'));
  363. $segs = array_reverse($segs);
  364. $index = 0;
  365. $last = count($segs);
  366. $baseUrl = '';
  367. do {
  368. $seg = $segs[$index];
  369. $baseUrl = '/' . $seg . $baseUrl;
  370. ++$index;
  371. } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
  372. }
  373. // Does the baseUrl have anything in common with the request_uri?
  374. $finalBaseUrl = NULL;
  375. $requestUri = $this->getRequestUri();
  376. if (0 === strpos($requestUri, $baseUrl)) {
  377. // full $baseUrl matches
  378. $finalBaseUrl = $baseUrl;
  379. } else if (0 === strpos($requestUri, dirname($baseUrl))) {
  380. // directory portion of $baseUrl matches
  381. $finalBaseUrl = rtrim(dirname($baseUrl), '/');
  382. } else if (!strpos($requestUri, basename($baseUrl))) {
  383. // no match whatsoever; set it blank
  384. $finalBaseUrl = '';
  385. } else if ((strlen($requestUri) >= strlen($baseUrl))
  386. && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
  387. {
  388. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  389. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  390. // from PATH_INFO or QUERY_STRING
  391. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  392. }
  393. return ($this->_baseUrl = (NULL === $finalBaseUrl) ? rtrim($baseUrl, '/') : $finalBaseUrl);
  394. }
  395. /**
  396. * ????pathinfo
  397. *
  398. * @access public
  399. * @return string
  400. */
  401. public function getPathInfo()
  402. {
  403. /** ???? */
  404. if (NULL !== $this->_pathInfo) {
  405. return $this->_pathInfo;
  406. }
  407. //??Zend Framework?pahtinfo???, ??????
  408. $pathInfo = NULL;
  409. $requestUri = $this->getRequestUri();
  410. $finalBaseUrl = $this->getBaseUrl();
  411. // Remove the query string from REQUEST_URI
  412. if ($pos = strpos($requestUri, '?')) {
  413. $requestUri = substr($requestUri, 0, $pos);
  414. }
  415. if ((NULL !== $finalBaseUrl)
  416. && (false === ($pathInfo = substr($requestUri, strlen($finalBaseUrl)))))
  417. {
  418. // If substr() returns false then PATH_INFO is set to an empty string
  419. $pathInfo = '/';
  420. } elseif (NULL === $finalBaseUrl) {
  421. $pathInfo = $requestUri;
  422. }
  423. if (empty($pathInfo)) {
  424. $pathInfo = '/';
  425. }
  426. // fix issue 456
  427. return ($this->_pathInfo = '/' . ltrim(urldecode($pathInfo), '/'));
  428. }
  429. /**
  430. * ?????
  431. *
  432. * @access public
  433. * @return void
  434. */
  435. public function getReferer()
  436. {
  437. if (false === $this->_referer) {
  438. $this->_referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL;
  439. }
  440. return $this->_referer;
  441. }
  442. /**
  443. * ?????????POST??
  444. *
  445. * @access public
  446. * @return boolean
  447. */
  448. public function isPost()
  449. {
  450. return 'POST' == $this->getMethod();
  451. }
  452. /**
  453. * ?????????GET??
  454. *
  455. * @access public
  456. * @return boolean
  457. */
  458. public function isGet()
  459. {
  460. return 'GET' == $this->getMethod();
  461. }
  462. /**
  463. * ???????
  464. *
  465. * @static
  466. * @access public
  467. * @return boolean
  468. */
  469. public function isUpload()
  470. {
  471. return !empty($_FILES);
  472. }
  473. /**
  474. * ?????ajax
  475. *
  476. * @access public
  477. * @return boolean
  478. */
  479. public function isAjax()
  480. {
  481. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH'];
  482. }
  483. /**
  484. * ?????flash
  485. *
  486. * @access public
  487. * @return boolean
  488. */
  489. public function isFlash()
  490. {
  491. return 'Shockwave Flash' == $_SERVER['USER_AGENT'];
  492. }
  493. /**
  494. * ???????
  495. *
  496. * @access public
  497. * @return boolean
  498. */
  499. public function isSecure()
  500. {
  501. return NULL === $this->_isSecure ? ($this->_isSecure =
  502. (isset($_SERVER['HTTPS']) && 'on' == $_SERVER['HTTPS']) || 443 == $_SERVER['SERVER_PORT']) : $this->_isSecure;
  503. }
  504. /**
  505. * ????????????
  506. *
  507. * @param string $url
  508. * @static
  509. * @access public
  510. * @return boolean
  511. */
  512. public function checkReferer($url)
  513. {
  514. $referer = $this->getReferer();
  515. if (empty($referer)) {
  516. return false;
  517. }
  518. $refererParts = parse_url($referer);
  519. $urlParts = parse_url($url);
  520. if (false === $refererParts) {
  521. return false;
  522. }
  523. if ($refererParts['host'] != $urlParts['host']) {
  524. return false;
  525. }
  526. return true;
  527. }
  528. /**
  529. * ?????????
  530. *
  531. * @param mixed $query ???????
  532. * @access public
  533. * @return void
  534. */
  535. public function is($query)
  536. {
  537. $validated = false;
  538. /** ??? */
  539. if (is_string($query)) {
  540. parse_str($query, $params);
  541. } else if (is_array($query)) {
  542. $params = $query;
  543. }
  544. /** ??? */
  545. if ($params) {
  546. $validated = true;
  547. foreach ($params as $key => $val) {
  548. $validated = empty($val) ? ($val != $this->get($key)) : ($val == $this->get($key));
  549. if (!$validated) {
  550. break;
  551. }
  552. }
  553. }
  554. return $validated;
  555. }
  556. }