/lib/Request.php
PHP | 630 lines | 304 code | 74 blank | 252 comment | 77 complexity | cdf8132fdf7c8edef11ea0f1d2d42458 MD5 | raw file
Possible License(s): AGPL-1.0
- <?php
- class Te_Request
- {
- /**
- * ???ip
- *
- * @var mixed
- * @access private
- */
- private $_ip;
- /**
- * ?????
- *
- * @var integer
- * @access private
- */
- private $_now;
- /**
- * _request
- *
- * @var array
- * @access private
- */
- private $_httpRequest = NULL;
- /**
- * ????
- *
- * @var string
- * @access private
- */
- private $_baseUrl = NULL;
-
- /**
- * ????
- *
- * @access private
- * @var string
- */
- private $_pathInfo = NULL;
- /**
- * ????
- *
- * @var string
- * @access private
- */
- private $_method;
- /**
- * ??????
- *
- * @var string
- * @access private
- */
- private $_requestUri;
- /**
- * ????
- *
- * @var array
- * @access private
- */
- private $_params = array();
- /**
- * json????
- *
- * @var array
- * @access private
- */
- private $_jsonParams = array();
- /**
- * ?????
- *
- * @var boolean
- * @access private
- */
- private $_commandMode = NULL;
- /**
- * ??
- *
- * @var string
- * @access private
- */
- private $_referer = false;
- /**
- * ??ssl
- *
- * @var mixed
- * @access private
- */
- private $_isSecure = NULL;
- /**
- * ??????
- *
- * @param string $key
- * @access private
- * @return string
- */
- private function getArg($key)
- {
- $value = strlen($key) > 1 ? getopt('', $key . ':') : getopt($key . ':');
- return $value[$key];
- }
-
- /**
- * ??????????
- *
- * @static
- * @access public
- * @return void
- */
- public function commandMode()
- {
- if (NULL === $this->_commandMode) {
- $this->_commandMode = isset($_SERVER['_']) || isset($_SERVER['COMMAND_MODE']);
- }
- return $this->_commandMode;
- }
- /**
- * ????????
- *
- * @static
- * @access public
- * @return void
- */
- public function getAgent()
- {
- return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
- }
- /**
- * ?????????????
- *
- * @static
- * @access public
- * @return void
- */
- public function getClientId()
- {
- return md5($this->getAgent() . "\0" . $this->getIp());
- }
- /**
- * ????????
- *
- * @param string $key ???
- * @param mixed $default
- * @access public
- * @return void
- */
- public function get($key, $default = NULL)
- {
- if (!isset($this->_params[$key])) {
- $paramKeys = explode('|', $key);
- $this->_params[$key] = $default;
-
- $request = array();
- if ($this->commandMode()) {
- $short = '';
- $long = array();
-
- foreach ($paramKeys as $paramKey) {
- if (strlen($paramKey) > 1) {
- $long[] = $paramKey . '::';
- } else {
- $short .= $paramKey . '::';
- }
- }
- $request = getopt($short, $long);
- } else {
- $request = $this->getHttpRequest();
- }
- foreach ($paramKeys as $paramKey) {
- if (isset($request[$paramKey])) {
- $this->_params[$key] = $request[$paramKey];
- } else if (false !== strpos($paramKey, ':')) {
- list($jsonParamKey, $jsonKey) = explode(':', $paramKey);
- $json = $this->getJson($jsonParamKey);
- if (isset($json[$jsonKey])) {
- $this->_params[$key] = $json[$jsonKey];
- }
- }
- }
- }
- return $this->_params[$key];
- }
- /**
- * ??????json??
- *
- * @param mixed $key
- * @access public
- * @return void
- */
- public function getJson($key)
- {
- if (!isset($this->_jsonParams[$key])) {
- $this->_jsonParams[$key] = NULL;
-
- if (!empty($_REQUEST[$key])) {
- $result = json_decode($this->get($key), true);
- if (NULL !== $result) {
- $this->_jsonParams[$key] = $result;
- }
- }
- }
- return $this->_jsonParams[$key];
- }
- /**
- * ????????
- *
- * @param mixed $key
- * @access public
- * @return array
- */
- public function getArray($key)
- {
- if (is_array($key)) {
- $result = array();
- foreach ($key as $k) {
- $result[$k] = $this->get($k);
- }
- return $result;
- } else {
- $result = $this->get($key, array());
- return is_array($result) ? $result : array($result);
- }
- }
-
- /**
- * ????????
- *
- * @static
- * @access public
- * @return array
- */
- public function getHttpRequest()
- {
- if (NULL === $this->_httpRequest) {
- $this->_httpRequest = array_merge($_POST, $_GET);
- }
- return $this->_httpRequest;
- }
- /**
- * ?????COOKIE?
- *
- * @access public
- * @param string $key ?????
- * @param string $default ?????
- * @return mixed
- */
- public function getCookie($key, $default = NULL)
- {
- return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default;
- }
- /**
- * ?????ip
- *
- * @access public
- * @return void
- */
- public function getIp()
- {
- if (empty($this->_ip)) {
- switch (true) {
- case !empty($_SERVER['HTTP_X_FORWARDED_FOR']):
- list($this->_ip) = array_map('trim', explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
- break;
- case !empty($_SERVER['HTTP_CLIENT_IP']):
- $this->_ip = $_SERVER['HTTP_CLIENT_IP'];
- break;
- case !empty($_SERVER['REMOTE_ADDR']):
- $this->_ip = $_SERVER['REMOTE_ADDR'];
- break;
- default:
- $this->_ip = '-';
- break;
- }
- }
- return $this->_ip;
- }
- /**
- * ???????
- *
- * @static
- * @access public
- * @return void
- */
- public function getNow()
- {
- if (empty($this->_now)) {
- $this->_now = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
- }
- return $this->_now;
- }
- /**
- * ????
- *
- * @access public
- * @return string
- */
- public function getMethod()
- {
- if (empty($this->_method)) {
- $this->_method = strtoupper($_SERVER['REQUEST_METHOD']);
- }
- return $this->_method;
- }
- /**
- * ??????
- *
- * @access public
- * @return string
- */
- public function getRequestUri()
- {
- if (!empty($this->_requestUri)) {
- return $this->_requestUri;
- }
- //??requestUri
- $requestUri = '/';
- if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
- $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
- } elseif (
- // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
- isset($_SERVER['IIS_WasUrlRewritten'])
- && $_SERVER['IIS_WasUrlRewritten'] == '1'
- && isset($_SERVER['UNENCODED_URL'])
- && $_SERVER['UNENCODED_URL'] != ''
- ) {
- $requestUri = $_SERVER['UNENCODED_URL'];
- } elseif (isset($_SERVER['REQUEST_URI'])) {
- $requestUri = $_SERVER['REQUEST_URI'];
- if (isset($_SERVER['HTTP_HOST']) && strstr($requestUri, $_SERVER['HTTP_HOST'])) {
- $parts = @parse_url($requestUri);
- if (false !== $parts) {
- $requestUri = (empty($parts['path']) ? '' : $parts['path'])
- . ((empty($parts['query'])) ? '' : '?' . $parts['query']);
- }
- }
- } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
- $requestUri = $_SERVER['ORIG_PATH_INFO'];
- if (!empty($_SERVER['QUERY_STRING'])) {
- $requestUri .= '?' . $_SERVER['QUERY_STRING'];
- }
- }
- return $this->_requestUri = $requestUri;
- }
- /**
- * ??????
- *
- * @static
- * @access public
- * @return string
- */
- public function getBaseUrl()
- {
- //??baseUrl
- if (NULL !== $this->_baseUrl) {
- return $this->_baseUrl;
- }
- $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : '';
- if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
- $baseUrl = $_SERVER['SCRIPT_NAME'];
- } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
- $baseUrl = $_SERVER['PHP_SELF'];
- } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
- $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
- } else {
- // Backtrack up the script_filename to find the portion matching
- // php_self
- $path = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
- $file = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
- $segs = explode('/', trim($file, '/'));
- $segs = array_reverse($segs);
- $index = 0;
- $last = count($segs);
- $baseUrl = '';
- do {
- $seg = $segs[$index];
- $baseUrl = '/' . $seg . $baseUrl;
- ++$index;
- } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
- }
- // Does the baseUrl have anything in common with the request_uri?
- $finalBaseUrl = NULL;
- $requestUri = $this->getRequestUri();
- if (0 === strpos($requestUri, $baseUrl)) {
- // full $baseUrl matches
- $finalBaseUrl = $baseUrl;
- } else if (0 === strpos($requestUri, dirname($baseUrl))) {
- // directory portion of $baseUrl matches
- $finalBaseUrl = rtrim(dirname($baseUrl), '/');
- } else if (!strpos($requestUri, basename($baseUrl))) {
- // no match whatsoever; set it blank
- $finalBaseUrl = '';
- } else if ((strlen($requestUri) >= strlen($baseUrl))
- && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
- {
- // If using mod_rewrite or ISAPI_Rewrite strip the script filename
- // out of baseUrl. $pos !== 0 makes sure it is not matching a value
- // from PATH_INFO or QUERY_STRING
- $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
- }
- return ($this->_baseUrl = (NULL === $finalBaseUrl) ? rtrim($baseUrl, '/') : $finalBaseUrl);
- }
- /**
- * ????pathinfo
- *
- * @access public
- * @return string
- */
- public function getPathInfo()
- {
- /** ???? */
- if (NULL !== $this->_pathInfo) {
- return $this->_pathInfo;
- }
- //??Zend Framework?pahtinfo???, ??????
- $pathInfo = NULL;
- $requestUri = $this->getRequestUri();
- $finalBaseUrl = $this->getBaseUrl();
- // Remove the query string from REQUEST_URI
- if ($pos = strpos($requestUri, '?')) {
- $requestUri = substr($requestUri, 0, $pos);
- }
- if ((NULL !== $finalBaseUrl)
- && (false === ($pathInfo = substr($requestUri, strlen($finalBaseUrl)))))
- {
- // If substr() returns false then PATH_INFO is set to an empty string
- $pathInfo = '/';
- } elseif (NULL === $finalBaseUrl) {
- $pathInfo = $requestUri;
- }
- if (empty($pathInfo)) {
- $pathInfo = '/';
- }
- // fix issue 456
- return ($this->_pathInfo = '/' . ltrim(urldecode($pathInfo), '/'));
- }
- /**
- * ?????
- *
- * @access public
- * @return void
- */
- public function getReferer()
- {
- if (false === $this->_referer) {
- $this->_referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL;
- }
- return $this->_referer;
- }
- /**
- * ?????????POST??
- *
- * @access public
- * @return boolean
- */
- public function isPost()
- {
- return 'POST' == $this->getMethod();
- }
- /**
- * ?????????GET??
- *
- * @access public
- * @return boolean
- */
- public function isGet()
- {
- return 'GET' == $this->getMethod();
- }
- /**
- * ???????
- *
- * @static
- * @access public
- * @return boolean
- */
- public function isUpload()
- {
- return !empty($_FILES);
- }
- /**
- * ?????ajax
- *
- * @access public
- * @return boolean
- */
- public function isAjax()
- {
- return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH'];
- }
- /**
- * ?????flash
- *
- * @access public
- * @return boolean
- */
- public function isFlash()
- {
- return 'Shockwave Flash' == $_SERVER['USER_AGENT'];
- }
- /**
- * ???????
- *
- * @access public
- * @return boolean
- */
- public function isSecure()
- {
- return NULL === $this->_isSecure ? ($this->_isSecure =
- (isset($_SERVER['HTTPS']) && 'on' == $_SERVER['HTTPS']) || 443 == $_SERVER['SERVER_PORT']) : $this->_isSecure;
- }
- /**
- * ????????????
- *
- * @param string $url
- * @static
- * @access public
- * @return boolean
- */
- public function checkReferer($url)
- {
- $referer = $this->getReferer();
- if (empty($referer)) {
- return false;
- }
- $refererParts = parse_url($referer);
- $urlParts = parse_url($url);
- if (false === $refererParts) {
- return false;
- }
- if ($refererParts['host'] != $urlParts['host']) {
- return false;
- }
- return true;
- }
- /**
- * ?????????
- *
- * @param mixed $query ???????
- * @access public
- * @return void
- */
- public function is($query)
- {
- $validated = false;
- /** ??? */
- if (is_string($query)) {
- parse_str($query, $params);
- } else if (is_array($query)) {
- $params = $query;
- }
- /** ??? */
- if ($params) {
- $validated = true;
- foreach ($params as $key => $val) {
- $validated = empty($val) ? ($val != $this->get($key)) : ($val == $this->get($key));
- if (!$validated) {
- break;
- }
- }
- }
- return $validated;
- }
- }