/Framework/Request.php
PHP | 286 lines | 138 code | 35 blank | 113 comment | 36 complexity | b40e749e5e000c4c9b6249c786fee5f0 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- /**
- * ?? - HTTP???????
- *
- * @version $Id: Request.php 278 2012-01-18 06:43:14Z linsir123 $
- * @package Core
- */
- class Request
- {
- /**
- * @var ???????(??????)
- */
- public $domain;
-
- /**
- * @var ??????
- */
- public $route;
-
- /**
- * @var ???????
- */
- public $urlModel;
-
- /**
- * @var ????????
- */
- public $appRoutes;
-
- /**
- * @var ????????
- */
- public $queryParts = array();
-
- /**
- * ????
- */
- public function __construct()
- {
- $uri = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/'));
- $phpSelf = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
- if (empty($phpSelf) || strpos($uri, $phpSelf) === false)
- $phpSelf = '';
-
- $this->domain = htmlspecialchars(preg_replace("/\/+(api|archiver|wap)?\/*$/i", '', $phpSelf) . '/');
- $this->appRoutes = App::O('main.routes');
- $this->urlModel = App::O('main.urlMode');
- }
-
- /**
- * ????????
- */
- private function _initRoute()
- {
- if ($this->urlModel == URL_PHPINFO && isset($_SERVER['PATH_INFO']))
- $this->route = substr($_SERVER['PATH_INFO'], 1);
- elseif ($this->urlModel == URL_REWRITE && isset($_GET['url']))
- $this->route = $_GET['url'];
- }
-
- /**
- * ??????
- * // ?????????
- * array(
- * 'pattern' => '/^list-(\d+).html$/i',
- * 'route' => 'site/list',
- * 'keys' => array('cid')
- * )
- *
- */
- private function _initAppRoutes()
- {
- if (is_array($this->appRoutes) && $this->appRoutes) {
- $keys = array('pattern', 'route', 'keys');
- foreach ($this->appRoutes as $i => $tmp) {
- if ( ! is_array($tmp))
- unset($this->appRoutes[$i]);
- }
- } else
- $this->appRoutes = array();
- }
-
- /**
- * ??????
- * // ???????
- * // ?????
- */
- private function _parse()
- {
- foreach ($this->appRoutes as $r) {
- if (preg_match($r['pattern'], $this->route, $matches)) {
- array_shift($matches);
- $this->route = $r['route'];
- foreach ($r['keys'] as $i => $key)
- $_GET[$key] = $matches[$i];
-
- break;
- }
- }
-
- ///
- if ($this->route) {
- if ($this->queryParts = explode('/', $this->route)) {
- $subApps = App::O('main.subApps');
- if (is_array($subApps) && in_array($this->queryParts[0], $subApps))
- App::$__subApp = array_shift($this->queryParts);
- }
- }
- }
-
- /**
- * ???
- *
- * @param string $route ????
- */
- public function init($route = '')
- {
- if (empty($route))
- $this->_initRoute();
- else
- $this->route = $route;
-
- ///
- $this->_initAppRoutes();
- $this->_parse();
- }
-
- /**
- * ????????
- *
- * @return array
- */
- public function getParts()
- {
- return $this->queryParts;
- }
-
- /**
- * ????????
- *
- * @param string $route ????
- * @param array $params ????
- * @param bool $full ???????????
- * @return string
- */
- public function create($route = '', $params = array(), $full = false)
- {
- $url = $route;
- if ($this->urlModel != URL_REWRITE)
- $url = basename($_SERVER['SCRIPT_NAME']).'/'.$url;
-
- if (is_array($params)) {
- foreach ($params as $key => $val)
- $url .= '/'.rawurlencode($key).'/'.rawurlencode($val);
- }
-
- ///
- $domain = $this->domain;
- if ($full)
- $domain = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$domain;
-
- return $domain.$url;
- }
-
-
- /// ??????????? ///
-
-
- /**
- * ?????IP??
- *
- * @return string
- * @static
- */
- static function ip()
- {
- if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
- $onlineip = getenv('HTTP_CLIENT_IP');
- elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
- $onlineip = getenv('HTTP_X_FORWARDED_FOR');
- elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
- $onlineip = getenv('REMOTE_ADDR');
- elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
- $onlineip = $_SERVER['REMOTE_ADDR'];
-
- preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
- $onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
-
- return $onlineip;
- }
-
- /**
- * ??HTTP??
- * // ??POST??
- *
- * @return bool
- * @static
- */
- static function isPost()
- {
- return strtoupper($_SERVER['REQUEST_METHOD']) == 'POST';
- }
-
- /**
- * ??HTTP??
- * // ????XMLHttp???
- *
- * @return bool
- * @static
- */
- static function isAjax()
- {
- $tmp = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) : '';
- return $tmp == 'XMLHTTPREQUEST';
- }
-
- /**
- * ??HTTP??
- * // ????????
- *
- * @return bool
- * @static
- */
- static function isRobot()
- {
- static $kwSpiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
- static $kwBrowsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');
-
- ///
- $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (strpos($useragent, 'http://') === false && self::_astrpos($useragent, $kwBrowsers))
- return false;
-
- if (self::_astrpos($useragent, $kwSpiders))
- return true;
-
- return false;
- }
-
- /**
- * ??HTTP??
- * // ????????
- *
- * @return bool
- * @static
- */
- static function isMobile()
- {
- static $mobileBrowsers = array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
- 'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
- 'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
- 'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
- 'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
- 'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
- 'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
-
- ///
- $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (self::_astrpos($useragent, $mobileBrowsers))
- return true;
-
- return false;
- }
-
- /**
- * ???????????????????????
- *
- * @param string $string ???????
- * @param array $array ?????
- * @return bool
- * @static
- */
- static function _astrpos($string, $array)
- {
- if (empty($string))
- return false;
-
- ///
- foreach((array)$array as $v) {
- if (strpos($string, $v) !== false)
- return true;
- }
-
- return false;
- }
- }