joomla 1.56的uri.php解釋

希望和大家分享,如果寫錯了,一定要留言呀

  1. <?php
  2. defined('JPATH_BASE'or die();
  3. //uri >= url
  4. class JURI extends JObject  //繼承jobject,jobject不難理解
  5. {
  6.     var $_uri = null; /*沒有分析過的uri*/
  7.     var $_scheme = null; /*協議*/
  8.     var $_host = null; /*主機*/
  9.     var $_port = null; /*端口*/
  10.     var $_user = null; /*用戶名*/
  11.     var $_pass = null; /*密碼*/
  12.     var $_path = null; /*路徑*/
  13.     var $_query = null; /*請求*/
  14.     var $_fragment = null; /*anchor錨*/
  15.     var $_vars = array (); /*query中的變量設置*/

  16.     function __construct($uri = null){//如果設置了uri,分析uri並設置到屬性
  17.         if ($uri !== null) {
  18.             $this->parse($uri);//分析uri
  19.         }
  20.     }

  21.     function &getInstance($uri = 'SERVER') {//取得一個實例,默認是現在的uri
  22.         static $instances = array();//避免重複運行

  23.         if (!isset ($instances[$uri])) {//避免重複
  24.             if ($uri == 'SERVER'){//如果默認
  25.                 //判斷是否爲SSL,(https)
  26.                 if (isset($_SERVER['HTTPS']) && !emptyempty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
  27.                     $https = 's://';
  28.                 } else {
  29.                     $https = '://';
  30.                 }
  31.                 //判斷IIS和apache,同時有php_self和request_uri->apache否則iis
  32.                 if (!emptyempty ($_SERVER['PHP_SELF']) && !emptyempty ($_SERVER['REQUEST_URI'])) {
  33.                     //設置完整的uri
  34.                     $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  35.                 //沒有request_uri我們就假設是IIS拉
  36.                 } else{
  37.                     $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
  38.                     if (isset($_SERVER['QUERY_STRING']) && !emptyempty($_SERVER['QUERY_STRING'])) {
  39.                         $theURI .= '?' . $_SERVER['QUERY_STRING'];
  40.                     }
  41.                 }
  42.                 //做細緻的檢查
  43.                 $theURI = urldecode($theURI);
  44.                 $theURI = str_replace('"''"',$theURI);
  45.                 $theURI = str_replace('<''<',$theURI);
  46.                 $theURI = str_replace('>''>',$theURI);
  47.                 //取消執行語句
  48.                 $theURI = preg_replace('/eval/((.*)/)/'''$theURI);
  49.                 $theURI = preg_replace('/[///"///'][//s]*javascript:(.*)[///"///']/''""'$theURI);
  50.             }else{
  51.                 //已經設置了$uri
  52.                 $theURI = $uri;
  53.             }
  54.             //創建一個新對象
  55.             $instances[$uri] = new JURI($theURI);
  56.         }
  57.         return $instances[$uri];//返回對象
  58.     }

  59.     function base($pathonly = false){
  60.         static $base;

  61.         if (!isset($base)){//如果頭一回
  62.             $config =& JFactory::getConfig(); //返回配置對象
  63.             $live_site = $config->getValue('config.live_site');//取得live_site
  64.             if(trim($live_site) != '') {//判斷是否爲空
  65.                 $uri =& JURI::getInstance($live_site);//創建實例
  66.                 //前綴設置
  67.                 $base['prefix'] = $uri->toString( array('scheme''host''port'));
  68.                 //路徑設置
  69.                 $base['path'] = rtrim($uri->toString( array('path')), '///');
  70.                 if(JPATH_BASE == JPATH_ADMINISTRATOR) {//這路徑在defines.php定義
  71.                     $base['path'] .= '/administrator';
  72.                 }
  73.             } else {
  74.                 //創建默認實例
  75.                 $uri             =& JURI::getInstance();
  76.                 //前綴設置
  77.                 $base['prefix'] = $uri->toString( array('scheme''host''port'));
  78.                 //查看接口類型interface between web server and PHP
  79.                 //如果不是CGI同時又有request_uri,那就斷定就是apache cgi
  80.                 if (strpos(php_sapi_name(), 'cgi') !== false && !emptyempty($_SERVER['REQUEST_URI'])) {
  81.                     //路徑設置
  82.                     $base['path'] =  rtrim(dirname($_SERVER['PHP_SELF']), '///');
  83.                 } else {
  84.                     //其他的
  85.                     $base['path'] =  rtrim(dirname($_SERVER['SCRIPT_NAME']), '///');
  86.                 }
  87.             }
  88.         }
  89.         //返回一個路徑或有前綴的路徑,默認有前綴
  90.         return $pathonly === false ? $base['prefix'].$base['path'].'/' : $base['path'];
  91.     }

  92.     function root($pathonly = false, $path = null){
  93.         static $root;

  94.         if(!isset($root)) {//頭一回
  95.             //使用base產生的URI來產生一個對象
  96.             $uri            =& JURI::getInstance(JURI::base());
  97.             $root['prefix'] = $uri->toString( array('scheme''host''port') );
  98.             $root['path']   = rtrim($uri->toString( array('path') ), '///');
  99.         }
  100.         if(isset($path)) {//如果設置了path
  101.             $root['path']    = $path;
  102.         }
  103.         return $pathonly === false ? $root['prefix'].$root['path'].'/' : $root['path'];
  104.     }
  105.     
  106.     function current(){
  107.         static $current;
  108.         //和上面很類似,返回現在的uri,但不是'/'結尾
  109.         if (!isset($current)){
  110.             $uri     = & JURI::getInstance();
  111.             $current = $uri->toString( array('scheme''host''port''path'));
  112.         }
  113.         return $current;
  114.     }

  115.     function parse($uri){
  116.         $retval = false;//返回值
  117.         
  118.         $this->_uri = $uri//保存原來的uri到_uri中
  119.         if ($_parts = $this->_parseURL($uri)) { //兼容的分析函數
  120.             $retval = true; //返回成功則把返回值設置爲TRUE
  121.         }
  122.         //把&替換成&
  123.         if(isset ($_parts['query']) && strpos($_parts['query'], '&')) {
  124.             $_parts['query'] = str_replace('&''&'$_parts['query']);
  125.         }
  126.         //設置所有屬性
  127.         $this->_scheme = isset ($_parts['scheme']) ? $_parts['scheme'] : null;
  128.         $this->_user = isset ($_parts['user']) ? $_parts['user'] : null;
  129.         $this->_pass = isset ($_parts['pass']) ? $_parts['pass'] : null;
  130.         $this->_host = isset ($_parts['host']) ? $_parts['host'] : null;
  131.         $this->_port = isset ($_parts['port']) ? $_parts['port'] : null;
  132.         $this->_path = isset ($_parts['path']) ? $_parts['path'] : null;
  133.         $this->_query = isset ($_parts['query'])? $_parts['query'] : null;
  134.         $this->_fragment = isset ($_parts['fragment']) ? $_parts['fragment'] : null;
  135.         if(isset ($_parts['query'])) parse_str($_parts['query'], $this->_vars);
  136.         return $retval;
  137.     }

  138.     //設置一下參數,返回一個串
  139.     function toString($parts = array('scheme''user''pass''host''port''path''query''fragment'))
  140.     {
  141.         $query = $this->getQuery(); //make sure the query is created

  142.         $uri = '';
  143.         $uri .= in_array('scheme'$parts)  ? (!emptyempty($this->_scheme) ? $this->_scheme.'://' : '') : '';
  144.         $uri .= in_array('user'$parts)    ? $this->_user : '';
  145.         $uri .= in_array('pass'$parts)    ? (!emptyempty ($this->_pass) ? ':' : '') .$this->_pass. (!emptyempty ($this->_user) ? '@' : '') : '';
  146.         $uri .= in_array('host'$parts)    ? $this->_host : '';
  147.         $uri .= in_array('port'$parts)    ? (!emptyempty ($this->_port) ? ':' : '').$this->_port : '';
  148.         $uri .= in_array('path'$parts)    ? $this->_path : '';
  149.         $uri .= in_array('query'$parts)   ? (!emptyempty ($query) ? '?'.$query : '') : '';
  150.         $uri .= in_array('fragment'$parts)? (!emptyempty ($this->_fragment) ? '#'.$this->_fragment : '') : '';

  151.         return $uri;
  152.     }

  153.     //設置一個query變量,如果本來就有返回本來那個,並設置成新的
  154.     function setVar($name$value)
  155.     {
  156.         $tmp = @$this->_vars[$name];
  157.         $this->_vars[$name] = $value;

  158.         //empty the query
  159.         $this->_query = null;

  160.         return $tmp;
  161.     }
  162.     
  163.     //返回query變量
  164.     function getVar($name = null, $default=null)
  165.     {
  166.         if(isset($this->_vars[$name])) {
  167.             return $this->_vars[$name];
  168.         }
  169.         return $default;
  170.     }
  171.     
  172.     //刪除一個query變量
  173.     function delVar($name)
  174.     {
  175.         if (in_array($namearray_keys($this->_vars)))
  176.         {
  177.             unset ($this->_vars[$name]);

  178.             //empty the query
  179.             $this->_query = null;
  180.         }
  181.     }
  182.     
  183.     //設置_vars,可接受數組和串
  184.     function setQuery($query)
  185.     {
  186.         if(!is_array($query)) {
  187.             if(strpos($query'&') !== false)
  188.             {
  189.                $query = str_replace('&','&',$query);
  190.             }
  191.             parse_str($query$this->_vars);
  192.         }

  193.         if(is_array($query)) {
  194.             $this->_vars = $query;
  195.         }

  196.         //empty the query
  197.         $this->_query = null;
  198.     }

  199.     //返回query,可以是數組也可以是串,是串的話通過buildquery建立
  200.     function getQuery($toArray = false)
  201.     {
  202.         if($toArray) {
  203.             return $this->_vars;
  204.         }

  205.         //If the query is empty build it first
  206.         if(is_null($this->_query)) {
  207.             $this->_query = $this->buildQuery($this->_vars);
  208.         }

  209.         return $this->_query;
  210.     }

  211.     //建立串,遞歸
  212.     function buildQuery ($params$akey = null)
  213.     {
  214.         if ( !is_array($params) || count($params) == 0 ) {
  215.             return false;
  216.         }

  217.         $out = array();

  218.         //reset in case we are looping
  219.         if( !isset($akey) && !count($out) )  {
  220.             unset($out);
  221.             $out = array();
  222.         }

  223.         foreach ( $params as $key => $val )
  224.         {
  225.             if ( is_array($val) ) {
  226.                 $out[] = JURI::buildQuery($val,$key);
  227.                 continue;
  228.             }

  229.             $thekey = ( !$akey ) ? $key : $akey.'['.$key.']';
  230.             $out[] = $thekey."=".urlencode($val);
  231.         }

  232.         return implode("&",$out);
  233.     }

  234. //下面比較簡單不解釋了
  235.     function getScheme() {
  236.         return $this->_scheme;
  237.     }

  238.     function setScheme($scheme) {
  239.         $this->_scheme = $scheme;
  240.     }

  241.     function getUser() {
  242.         return $this->_user;
  243.     }

  244.     function setUser($user) {
  245.         $this->_user = $user;
  246.     }

  247.     function getPass() {
  248.         return $this->_pass;
  249.     }

  250.     function setPass($pass) {
  251.         $this->_pass = $pass;
  252.     }

  253.     function getHost() {
  254.         return $this->_host;
  255.     }

  256.     function setHost($host) {
  257.         $this->_host = $host;
  258.     }

  259.     function getPort() {
  260.         return (isset ($this->_port)) ? $this->_port : null;
  261.     }

  262.     function setPort($port) {
  263.         $this->_port = $port;
  264.     }

  265.     function getPath() {
  266.         return $this->_path;
  267.     }

  268.     function setPath($path) {
  269.         $this->_path = $this->_cleanPath($path);
  270.     }

  271.     function getFragment() {
  272.         return $this->_fragment;
  273.     }

  274.     function setFragment($anchor) {
  275.         $this->_fragment = $anchor;
  276.     }

  277.     function isSSL() {
  278.         return $this->getScheme() == 'https' ? true : false;
  279.     }

  280.     /**
  281.      * Resolves //, ../ and ./ from a path and returns
  282.      * the result. Eg:
  283.      *
  284.      * /foo/bar/../boo.php  => /foo/boo.php
  285.      * /foo/bar/../../boo.php => /boo.php
  286.      * /foo/bar/.././/boo.php => /foo/boo.php
  287.      *
  288.      * @access  private
  289.      * @param   string $uri The URI path to clean
  290.      * @return  string Cleaned and resolved URI path
  291.      * @since   1.5
  292.      */
  293.     function _cleanPath($path)
  294.     {
  295.         $path = explode('/', preg_replace('#(/+)#''/'$path));

  296.         for ($i = 0; $i < count($path); $i ++) {
  297.             if ($path[$i] == '.') {
  298.                 unset ($path[$i]);
  299.                 $path = array_values($path);
  300.                 $i --;

  301.             }
  302.             elseif ($path[$i] == '..' AND ($i > 1 OR ($i == 1 AND $path[0] != ''))) {
  303.                 unset ($path[$i]);
  304.                 unset ($path[$i -1]);
  305.                 $path = array_values($path);
  306.                 $i -= 2;

  307.             }
  308.             elseif ($path[$i] == '..' AND $i == 1 AND $path[0] == '') {
  309.                 unset ($path[$i]);
  310.                 $path = array_values($path);
  311.                 $i --;

  312.             } else {
  313.                 continue;
  314.             }
  315.         }

  316.         return implode('/'$path);
  317.     }

  318.     /**
  319.      * Backwards compatibility function for parse_url function
  320.      *
  321.      * This function solves different bugs in PHP versions lower then
  322.      * 4.4, will be deprecated in future versions.
  323.      *
  324.      * @access  private
  325.      * @return  array Associative array containing the URL parts
  326.      * @since   1.5
  327.      * @see parse_url()
  328.      */
  329.     function _parseURL($uri)
  330.     {
  331.         $parts = array();
  332.         if (version_compare( phpversion(), '4.4' ) < 0)
  333.         {
  334.             $regex = "<^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(//?([^#]*))?(#(.*))?>";
  335.             $matches = array();
  336.             preg_match($regex$uri$matches, PREG_OFFSET_CAPTURE);

  337.             $authority = @$matches[4][0];
  338.             if (strpos($authority'@') !== false) {
  339.                 $authority = explode('@'$authority);
  340.                 @list($parts['user'], $parts['pass']) = explode(':'$authority[0]);
  341.                 $authority = $authority[1];
  342.             }

  343.             if (strpos($authority':') !== false) {
  344.                 $authority = explode(':'$authority);
  345.                 $parts['host'] = $authority[0];
  346.                 $parts['port'] = $authority[1];
  347.             } else {
  348.                 $parts['host'] = $authority;
  349.             }

  350.             $parts['scheme'] = @$matches[2][0];
  351.             $parts['path'] = @$matches[5][0];
  352.             $parts['query'] = @$matches[7][0];
  353.             $parts['fragment'] = @$matches[9][0];
  354.         }
  355.         else
  356.         {
  357.             $parts = @parse_url($uri);
  358.         }
  359.         return $parts;
  360.     }


  361. }


發佈了31 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章