PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/Request.php

http://lxsphp.googlecode.com/
PHP | 286 lines | 138 code | 35 blank | 113 comment | 36 complexity | b40e749e5e000c4c9b6249c786fee5f0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * ?? - HTTP???????
  4. *
  5. * @version $Id: Request.php 278 2012-01-18 06:43:14Z linsir123 $
  6. * @package Core
  7. */
  8. class Request
  9. {
  10. /**
  11. * @var ???????(??????)
  12. */
  13. public $domain;
  14. /**
  15. * @var ??????
  16. */
  17. public $route;
  18. /**
  19. * @var ???????
  20. */
  21. public $urlModel;
  22. /**
  23. * @var ????????
  24. */
  25. public $appRoutes;
  26. /**
  27. * @var ????????
  28. */
  29. public $queryParts = array();
  30. /**
  31. * ????
  32. */
  33. public function __construct()
  34. {
  35. $uri = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/'));
  36. $phpSelf = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
  37. if (empty($phpSelf) || strpos($uri, $phpSelf) === false)
  38. $phpSelf = '';
  39. $this->domain = htmlspecialchars(preg_replace("/\/+(api|archiver|wap)?\/*$/i", '', $phpSelf) . '/');
  40. $this->appRoutes = App::O('main.routes');
  41. $this->urlModel = App::O('main.urlMode');
  42. }
  43. /**
  44. * ????????
  45. */
  46. private function _initRoute()
  47. {
  48. if ($this->urlModel == URL_PHPINFO && isset($_SERVER['PATH_INFO']))
  49. $this->route = substr($_SERVER['PATH_INFO'], 1);
  50. elseif ($this->urlModel == URL_REWRITE && isset($_GET['url']))
  51. $this->route = $_GET['url'];
  52. }
  53. /**
  54. * ??????
  55. * // ?????????
  56. * array(
  57. * 'pattern' => '/^list-(\d+).html$/i',
  58. * 'route' => 'site/list',
  59. * 'keys' => array('cid')
  60. * )
  61. *
  62. */
  63. private function _initAppRoutes()
  64. {
  65. if (is_array($this->appRoutes) && $this->appRoutes) {
  66. $keys = array('pattern', 'route', 'keys');
  67. foreach ($this->appRoutes as $i => $tmp) {
  68. if ( ! is_array($tmp))
  69. unset($this->appRoutes[$i]);
  70. }
  71. } else
  72. $this->appRoutes = array();
  73. }
  74. /**
  75. * ??????
  76. * // ???????
  77. * // ?????
  78. */
  79. private function _parse()
  80. {
  81. foreach ($this->appRoutes as $r) {
  82. if (preg_match($r['pattern'], $this->route, $matches)) {
  83. array_shift($matches);
  84. $this->route = $r['route'];
  85. foreach ($r['keys'] as $i => $key)
  86. $_GET[$key] = $matches[$i];
  87. break;
  88. }
  89. }
  90. ///
  91. if ($this->route) {
  92. if ($this->queryParts = explode('/', $this->route)) {
  93. $subApps = App::O('main.subApps');
  94. if (is_array($subApps) && in_array($this->queryParts[0], $subApps))
  95. App::$__subApp = array_shift($this->queryParts);
  96. }
  97. }
  98. }
  99. /**
  100. * ???
  101. *
  102. * @param string $route ????
  103. */
  104. public function init($route = '')
  105. {
  106. if (empty($route))
  107. $this->_initRoute();
  108. else
  109. $this->route = $route;
  110. ///
  111. $this->_initAppRoutes();
  112. $this->_parse();
  113. }
  114. /**
  115. * ????????
  116. *
  117. * @return array
  118. */
  119. public function getParts()
  120. {
  121. return $this->queryParts;
  122. }
  123. /**
  124. * ????????
  125. *
  126. * @param string $route ????
  127. * @param array $params ????
  128. * @param bool $full ???????????
  129. * @return string
  130. */
  131. public function create($route = '', $params = array(), $full = false)
  132. {
  133. $url = $route;
  134. if ($this->urlModel != URL_REWRITE)
  135. $url = basename($_SERVER['SCRIPT_NAME']).'/'.$url;
  136. if (is_array($params)) {
  137. foreach ($params as $key => $val)
  138. $url .= '/'.rawurlencode($key).'/'.rawurlencode($val);
  139. }
  140. ///
  141. $domain = $this->domain;
  142. if ($full)
  143. $domain = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$domain;
  144. return $domain.$url;
  145. }
  146. /// ??????????? ///
  147. /**
  148. * ?????IP??
  149. *
  150. * @return string
  151. * @static
  152. */
  153. static function ip()
  154. {
  155. if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
  156. $onlineip = getenv('HTTP_CLIENT_IP');
  157. elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
  158. $onlineip = getenv('HTTP_X_FORWARDED_FOR');
  159. elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
  160. $onlineip = getenv('REMOTE_ADDR');
  161. elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
  162. $onlineip = $_SERVER['REMOTE_ADDR'];
  163. preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
  164. $onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
  165. return $onlineip;
  166. }
  167. /**
  168. * ??HTTP??
  169. * // ??POST??
  170. *
  171. * @return bool
  172. * @static
  173. */
  174. static function isPost()
  175. {
  176. return strtoupper($_SERVER['REQUEST_METHOD']) == 'POST';
  177. }
  178. /**
  179. * ??HTTP??
  180. * // ????XMLHttp???
  181. *
  182. * @return bool
  183. * @static
  184. */
  185. static function isAjax()
  186. {
  187. $tmp = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) : '';
  188. return $tmp == 'XMLHTTPREQUEST';
  189. }
  190. /**
  191. * ??HTTP??
  192. * // ????????
  193. *
  194. * @return bool
  195. * @static
  196. */
  197. static function isRobot()
  198. {
  199. static $kwSpiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
  200. static $kwBrowsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');
  201. ///
  202. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  203. if (strpos($useragent, 'http://') === false && self::_astrpos($useragent, $kwBrowsers))
  204. return false;
  205. if (self::_astrpos($useragent, $kwSpiders))
  206. return true;
  207. return false;
  208. }
  209. /**
  210. * ??HTTP??
  211. * // ????????
  212. *
  213. * @return bool
  214. * @static
  215. */
  216. static function isMobile()
  217. {
  218. static $mobileBrowsers = array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
  219. 'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
  220. 'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
  221. 'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
  222. 'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
  223. 'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
  224. 'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
  225. ///
  226. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  227. if (self::_astrpos($useragent, $mobileBrowsers))
  228. return true;
  229. return false;
  230. }
  231. /**
  232. * ???????????????????????
  233. *
  234. * @param string $string ???????
  235. * @param array $array ?????
  236. * @return bool
  237. * @static
  238. */
  239. static function _astrpos($string, $array)
  240. {
  241. if (empty($string))
  242. return false;
  243. ///
  244. foreach((array)$array as $v) {
  245. if (strpos($string, $v) !== false)
  246. return true;
  247. }
  248. return false;
  249. }
  250. }