- <?php
- defined('JPATH_BASE') or die();
- //uri >= url
- class JURI extends JObject //繼承jobject,jobject不難理解
- {
- var $_uri = null; /*沒有分析過的uri*/
- var $_scheme = null; /*協議*/
- var $_host = null; /*主機*/
- var $_port = null; /*端口*/
- var $_user = null; /*用戶名*/
- var $_pass = null; /*密碼*/
- var $_path = null; /*路徑*/
- var $_query = null; /*請求*/
- var $_fragment = null; /*anchor錨*/
- var $_vars = array (); /*query中的變量設置*/
- function __construct($uri = null){//如果設置了uri,分析uri並設置到屬性
- if ($uri !== null) {
- $this->parse($uri);//分析uri
- }
- }
- function &getInstance($uri = 'SERVER') {//取得一個實例,默認是現在的uri
- static $instances = array();//避免重複運行
- if (!isset ($instances[$uri])) {//避免重複
- if ($uri == 'SERVER'){//如果默認
- //判斷是否爲SSL,(https)
- if (isset($_SERVER['HTTPS']) && !emptyempty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
- $https = 's://';
- } else {
- $https = '://';
- }
- //判斷IIS和apache,同時有php_self和request_uri->apache否則iis
- if (!emptyempty ($_SERVER['PHP_SELF']) && !emptyempty ($_SERVER['REQUEST_URI'])) {
- //設置完整的uri
- $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
- //沒有request_uri我們就假設是IIS拉
- } else{
- $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
- if (isset($_SERVER['QUERY_STRING']) && !emptyempty($_SERVER['QUERY_STRING'])) {
- $theURI .= '?' . $_SERVER['QUERY_STRING'];
- }
- }
- //做細緻的檢查
- $theURI = urldecode($theURI);
- $theURI = str_replace('"', '"',$theURI);
- $theURI = str_replace('<', '<',$theURI);
- $theURI = str_replace('>', '>',$theURI);
- //取消執行語句
- $theURI = preg_replace('/eval/((.*)/)/', '', $theURI);
- $theURI = preg_replace('/[///"///'][//s]*javascript:(.*)[///"///']/', '""', $theURI);
- }else{
- //已經設置了$uri
- $theURI = $uri;
- }
- //創建一個新對象
- $instances[$uri] = new JURI($theURI);
- }
- return $instances[$uri];//返回對象
- }
- function base($pathonly = false){
- static $base;
- if (!isset($base)){//如果頭一回
- $config =& JFactory::getConfig(); //返回配置對象
- $live_site = $config->getValue('config.live_site');//取得live_site
- if(trim($live_site) != '') {//判斷是否爲空
- $uri =& JURI::getInstance($live_site);//創建實例
- //前綴設置
- $base['prefix'] = $uri->toString( array('scheme', 'host', 'port'));
- //路徑設置
- $base['path'] = rtrim($uri->toString( array('path')), '///');
- if(JPATH_BASE == JPATH_ADMINISTRATOR) {//這路徑在defines.php定義
- $base['path'] .= '/administrator';
- }
- } else {
- //創建默認實例
- $uri =& JURI::getInstance();
- //前綴設置
- $base['prefix'] = $uri->toString( array('scheme', 'host', 'port'));
- //查看接口類型interface between web server and PHP
- //如果不是CGI同時又有request_uri,那就斷定就是apache cgi
- if (strpos(php_sapi_name(), 'cgi') !== false && !emptyempty($_SERVER['REQUEST_URI'])) {
- //路徑設置
- $base['path'] = rtrim(dirname($_SERVER['PHP_SELF']), '///');
- } else {
- //其他的
- $base['path'] = rtrim(dirname($_SERVER['SCRIPT_NAME']), '///');
- }
- }
- }
- //返回一個路徑或有前綴的路徑,默認有前綴
- return $pathonly === false ? $base['prefix'].$base['path'].'/' : $base['path'];
- }
- function root($pathonly = false, $path = null){
- static $root;
- if(!isset($root)) {//頭一回
- //使用base產生的URI來產生一個對象
- $uri =& JURI::getInstance(JURI::base());
- $root['prefix'] = $uri->toString( array('scheme', 'host', 'port') );
- $root['path'] = rtrim($uri->toString( array('path') ), '///');
- }
- if(isset($path)) {//如果設置了path
- $root['path'] = $path;
- }
- return $pathonly === false ? $root['prefix'].$root['path'].'/' : $root['path'];
- }
- function current(){
- static $current;
- //和上面很類似,返回現在的uri,但不是'/'結尾
- if (!isset($current)){
- $uri = & JURI::getInstance();
- $current = $uri->toString( array('scheme', 'host', 'port', 'path'));
- }
- return $current;
- }
- function parse($uri){
- $retval = false;//返回值
- $this->_uri = $uri; //保存原來的uri到_uri中
- if ($_parts = $this->_parseURL($uri)) { //兼容的分析函數
- $retval = true; //返回成功則把返回值設置爲TRUE
- }
- //把&替換成&
- if(isset ($_parts['query']) && strpos($_parts['query'], '&')) {
- $_parts['query'] = str_replace('&', '&', $_parts['query']);
- }
- //設置所有屬性
- $this->_scheme = isset ($_parts['scheme']) ? $_parts['scheme'] : null;
- $this->_user = isset ($_parts['user']) ? $_parts['user'] : null;
- $this->_pass = isset ($_parts['pass']) ? $_parts['pass'] : null;
- $this->_host = isset ($_parts['host']) ? $_parts['host'] : null;
- $this->_port = isset ($_parts['port']) ? $_parts['port'] : null;
- $this->_path = isset ($_parts['path']) ? $_parts['path'] : null;
- $this->_query = isset ($_parts['query'])? $_parts['query'] : null;
- $this->_fragment = isset ($_parts['fragment']) ? $_parts['fragment'] : null;
- if(isset ($_parts['query'])) parse_str($_parts['query'], $this->_vars);
- return $retval;
- }
- //設置一下參數,返回一個串
- function toString($parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'))
- {
- $query = $this->getQuery(); //make sure the query is created
- $uri = '';
- $uri .= in_array('scheme', $parts) ? (!emptyempty($this->_scheme) ? $this->_scheme.'://' : '') : '';
- $uri .= in_array('user', $parts) ? $this->_user : '';
- $uri .= in_array('pass', $parts) ? (!emptyempty ($this->_pass) ? ':' : '') .$this->_pass. (!emptyempty ($this->_user) ? '@' : '') : '';
- $uri .= in_array('host', $parts) ? $this->_host : '';
- $uri .= in_array('port', $parts) ? (!emptyempty ($this->_port) ? ':' : '').$this->_port : '';
- $uri .= in_array('path', $parts) ? $this->_path : '';
- $uri .= in_array('query', $parts) ? (!emptyempty ($query) ? '?'.$query : '') : '';
- $uri .= in_array('fragment', $parts)? (!emptyempty ($this->_fragment) ? '#'.$this->_fragment : '') : '';
- return $uri;
- }
- //設置一個query變量,如果本來就有返回本來那個,並設置成新的
- function setVar($name, $value)
- {
- $tmp = @$this->_vars[$name];
- $this->_vars[$name] = $value;
- //empty the query
- $this->_query = null;
- return $tmp;
- }
- //返回query變量
- function getVar($name = null, $default=null)
- {
- if(isset($this->_vars[$name])) {
- return $this->_vars[$name];
- }
- return $default;
- }
- //刪除一個query變量
- function delVar($name)
- {
- if (in_array($name, array_keys($this->_vars)))
- {
- unset ($this->_vars[$name]);
- //empty the query
- $this->_query = null;
- }
- }
- //設置_vars,可接受數組和串
- function setQuery($query)
- {
- if(!is_array($query)) {
- if(strpos($query, '&') !== false)
- {
- $query = str_replace('&','&',$query);
- }
- parse_str($query, $this->_vars);
- }
- if(is_array($query)) {
- $this->_vars = $query;
- }
- //empty the query
- $this->_query = null;
- }
- //返回query,可以是數組也可以是串,是串的話通過buildquery建立
- function getQuery($toArray = false)
- {
- if($toArray) {
- return $this->_vars;
- }
- //If the query is empty build it first
- if(is_null($this->_query)) {
- $this->_query = $this->buildQuery($this->_vars);
- }
- return $this->_query;
- }
- //建立串,遞歸
- function buildQuery ($params, $akey = null)
- {
- if ( !is_array($params) || count($params) == 0 ) {
- return false;
- }
- $out = array();
- //reset in case we are looping
- if( !isset($akey) && !count($out) ) {
- unset($out);
- $out = array();
- }
- foreach ( $params as $key => $val )
- {
- if ( is_array($val) ) {
- $out[] = JURI::buildQuery($val,$key);
- continue;
- }
- $thekey = ( !$akey ) ? $key : $akey.'['.$key.']';
- $out[] = $thekey."=".urlencode($val);
- }
- return implode("&",$out);
- }
- //下面比較簡單不解釋了
- function getScheme() {
- return $this->_scheme;
- }
- function setScheme($scheme) {
- $this->_scheme = $scheme;
- }
- function getUser() {
- return $this->_user;
- }
- function setUser($user) {
- $this->_user = $user;
- }
- function getPass() {
- return $this->_pass;
- }
- function setPass($pass) {
- $this->_pass = $pass;
- }
- function getHost() {
- return $this->_host;
- }
- function setHost($host) {
- $this->_host = $host;
- }
- function getPort() {
- return (isset ($this->_port)) ? $this->_port : null;
- }
- function setPort($port) {
- $this->_port = $port;
- }
- function getPath() {
- return $this->_path;
- }
- function setPath($path) {
- $this->_path = $this->_cleanPath($path);
- }
- function getFragment() {
- return $this->_fragment;
- }
- function setFragment($anchor) {
- $this->_fragment = $anchor;
- }
- function isSSL() {
- return $this->getScheme() == 'https' ? true : false;
- }
- /**
- * Resolves //, ../ and ./ from a path and returns
- * the result. Eg:
- *
- * /foo/bar/../boo.php => /foo/boo.php
- * /foo/bar/../../boo.php => /boo.php
- * /foo/bar/.././/boo.php => /foo/boo.php
- *
- * @access private
- * @param string $uri The URI path to clean
- * @return string Cleaned and resolved URI path
- * @since 1.5
- */
- function _cleanPath($path)
- {
- $path = explode('/', preg_replace('#(/+)#', '/', $path));
- for ($i = 0; $i < count($path); $i ++) {
- if ($path[$i] == '.') {
- unset ($path[$i]);
- $path = array_values($path);
- $i --;
- }
- elseif ($path[$i] == '..' AND ($i > 1 OR ($i == 1 AND $path[0] != ''))) {
- unset ($path[$i]);
- unset ($path[$i -1]);
- $path = array_values($path);
- $i -= 2;
- }
- elseif ($path[$i] == '..' AND $i == 1 AND $path[0] == '') {
- unset ($path[$i]);
- $path = array_values($path);
- $i --;
- } else {
- continue;
- }
- }
- return implode('/', $path);
- }
- /**
- * Backwards compatibility function for parse_url function
- *
- * This function solves different bugs in PHP versions lower then
- * 4.4, will be deprecated in future versions.
- *
- * @access private
- * @return array Associative array containing the URL parts
- * @since 1.5
- * @see parse_url()
- */
- function _parseURL($uri)
- {
- $parts = array();
- if (version_compare( phpversion(), '4.4' ) < 0)
- {
- $regex = "<^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(//?([^#]*))?(#(.*))?>";
- $matches = array();
- preg_match($regex, $uri, $matches, PREG_OFFSET_CAPTURE);
- $authority = @$matches[4][0];
- if (strpos($authority, '@') !== false) {
- $authority = explode('@', $authority);
- @list($parts['user'], $parts['pass']) = explode(':', $authority[0]);
- $authority = $authority[1];
- }
- if (strpos($authority, ':') !== false) {
- $authority = explode(':', $authority);
- $parts['host'] = $authority[0];
- $parts['port'] = $authority[1];
- } else {
- $parts['host'] = $authority;
- }
- $parts['scheme'] = @$matches[2][0];
- $parts['path'] = @$matches[5][0];
- $parts['query'] = @$matches[7][0];
- $parts['fragment'] = @$matches[9][0];
- }
- else
- {
- $parts = @parse_url($uri);
- }
- return $parts;
- }
- }
joomla 1.56的uri.php解釋
希望和大家分享,如果寫錯了,一定要留言呀
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.