CI framework source code reading---------URI.php_PHP tutorial

WBOY
Release: 2016-07-14 10:09:14
Original
889 people have browsed it

[php]  

/** 
 * CodeIgniter 
 * 
 * An open source application development framework for PHP 5.1.6 or newer 
 * 
 * @package     CodeIgniter 
 * @author      ExpressionEngine Dev Team 
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc. 
 * @license     http://codeigniter.com/user_guide/license.html 
 * @link        http://codeigniter.com 
 * @since       Version 1.0 
 * @filesource 
 */  
  
// ------------------------------------------------------------------------  
  
/** 
 * URI Class 
 * 
 * Parses 解析 URIs and determines routing 
 * 
 * @package     CodeIgniter 
 * @subpackage  Libraries 
 * @category    URI 
 * @author      ExpressionEngine Dev Team 
 * @link        http://codeigniter.com/user_guide/libraries/uri.html 
 */  
class CI_URI {  
  
    /**
* List of cached uri segments
* Cache uri segment list
* @var array
* @access public
*/  
    var $keyval         = array();  
    /**
* Current uri string
* Current uri string
* @var string
* @access public
*/  
    var $uri_string;  
    /**
* List of uri segments
* uri segment list
* @var array
* @access public
*/  
    var $segments       = array();  
    /**
* Re-indexed list of uri segments
* Starts at 1 instead of 0
* Re-index the rui segment list starting from 1
* @var array
* @access public
*/  
    var $rsegments      = array();  
  
    /** 
     * Constructor 
     * 
     * Simply globalizes the $RTR object.  The front 
     * loads the Router class early on so it's not available 
     * normally as other classes are. 
     * 
     * @access  public 
     */  
    function __construct()  
    {  
        $this->config =& load_class('Config', 'core');  
        log_message('debug', "URI Class Initialized");  
    }  
  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Get the URI String 
     * 
     * @access  private 
     * @return  string 
     */  
    function _fetch_uri_string()  
    {  
        // 下面的uri_protocol是在config.php里面的一个配置项,  
        // 其实是问你用哪种方式去检测uri的信息的意思,  
// The default is AUTO, automatic detection, that is, detection through various methods until it is detected, or all methods are detected. .
if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
{
// Is the request coming from the command line?
// Start trying various methods, mainly: command line, REQUEST_URI, PATH_INFO, QUERY_STRING.
                                                                                                                                                                                                                                                                                                   
// After the filtration and trimming value, the value of $ this-& gt; uri_string is temporarily understood as assignment.
                                       // If the script is run in command line mode, the parameters are passed through $_SERVER['argv']. Below
// $this->_parse_cli_args(); is to get some parameters related to routing that meet our needs
// If you do not use the command line to execute the script, the IF below can not be controlled for the time being.
                         // At this time we found that the URI class uses the function php_sapi_name() to test different environments
                                                                                                                                                                                                                                                                                               
// The result of the output output in CGI mode is "CGI-FCGI"
                                                                                                                                                                 
if (php_sapi_name() == 'cli' or defined('STDIN'))
                                                                 
$this->_set_uri_string($this->_parse_cli_args());
            return;                          
      }  
                                                                                                                                                                                                                                                                                                   
// Find uri
if ($uri = $this->_detect_uri())
                                                                 
// If you find the URI setting $ this- & gt; uri_string
$this->_set_uri_string($uri);
            return;                          
      }  
// Is there a PATH_INFO variable?
// Note: some servers seem to have trouble with getenv() so we'll test it two ways
// Get Path $ _Server ['PATH_INFO'] is not every request.
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
              if (trim($path, '/') != '' && $path != "/".SELF)  
                                                                 
$this->_set_uri_string($path);
            return;                          
      }  
// No PATH_INFO?... What about QUERY_STRING?
             // If $_SERVER['PATH_INFO'] is not found we use QUERY_STRING
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
          if (trim($path, '/') != '')
                                                                 
$this->_set_uri_string($path);
            return;                            
      }  
// As a last ditch effort lets try using the $_GET array
                      // If neither PATH_INFO nor QUERY_STRING is found, we can only use $_GET
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
                                                                 
$this->_set_uri_string(key($_GET));
            return;                            
      }  
// We've exhausted all our options...
// After the above efforts, we have not found URI, so we really can't find
$this->uri_string = '';
return;
} }
// I have rewritten it here to get uri_protocol. In fact, I think it is completely possible to get it only once. . .
$uri = strtoupper($this->config->item('uri_protocol'));
// The following is to choose different methods to obtain uri according to different methods
if ($uri == 'REQUEST_URI')
{
$this->_set_uri_string($this->_detect_uri());
return;
} }
elseif ($uri == 'CLI')
{
$this->_set_uri_string($this->_parse_cli_args());
return;
} }
// If the uri_protocol you define is outside the three methods of AUTO REQUEST_URI CLI, execute the following paragraph.
$path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
$this->_set_uri_string($path);
}
//------------------------------------------------ -----------------------
/** 
     * Set the URI String 
     * 
     * @access  public 
     * @param   string 
     * @return  string 
     */
function _set_uri_string($str)
{
// Filter out control characters
// Filter string remove_invisible_characters function is in common.php
$str = remove_invisible_characters($str, FALSE);
// If the URI contains only a slash we'll kill it
// If the string contains only one /, clear it
$this->uri_string = ($str == '/') ? '' : $str;
}
//------------------------------------------------ -----------------------
/**
* Detects the URI
* Search uri
* This function will detect the URI automatically and fix the query string
* if necessary. Necessities
* This function will automatically find the uri and fix the query string if necessary.
*
* @access private
* @return string
*/
private function _detect_uri()
{
// If one of the two values ​​is missing, return (the two variables come from the web server. When encountering some special server programs, this may be empty.)
if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
{
            return '';                                      
} }
// Get uri
$uri = $_SERVER['REQUEST_URI'];
// If the first occurrence of SCRIPT_NAME in $uri is 0
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
                                                                                                                                                                                                                                                                                                   
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
} }
// The effect here is the same as above, except that $_SERVER['SCRIPT_NAME'] is replaced by
// dirname($_SERVER['SCRIPT_NAME'])
elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
{
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
} }
// This section ensures that even on servers that require the URI
// to be in the query string (Nginx) a correct correct
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
// This part ensures that the uri can be found correctly even on the Nginx server, and also fixes the QUERY_STRING server and $_GET array.
// Determine whether the first two characters of $uri are the same?/
if (strncmp($uri, '?/', 2) === 0)
{
                                      // Remove the first two characters
$uri = substr($uri, 2);
} }
// Split the string using the original book
$parts = preg_split('#?#i', $uri, 2);
$uri = $parts[0];
// If two paragraphs can be divided through the above regular regularity, is it via Query_String? Route access in the form of
if (isset($parts[1]))
{
$_SERVER['QUERY_STRING'] = $parts[1];
                           // The function parses the query string into the $_GET variable.
parse_str($_SERVER['QUERY_STRING'], $_GET);
} }
else
{
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
} }
// If it is /, or empty, there are two situations, or it is through query_string? Route access in the form of
// So at this time $parts[0] is equal to the following two possibilities, and at the same time we
// If you have obtained the information you want through $parts[1], you can return.
// Or it is in the form of a segment, but the segment information is empty, that is, accessing the entry file directly without
// Any routing information passed can also be returned directly.
if ($uri == '/' || emptyempty($uri))
{
            return '/';                                            
} }
//Return the path part of this url.
$uri = parse_url($uri, PHP_URL_PATH);
// Do some final cleaning of the URI and return it
// Replace // ../ in uri with / Return
return str_replace(array('//', '../'), '/', trim($uri, '/'));
}
//------------------------------------------------ -----------------------
/**
* Parse cli arguments
* Parse cli parameters
* Take each command line argument and assume it is a URI segment.
* If you do this in the command line
* php d:/wamp/www/CodeIgniter/index.php welcome index
* _parse_cli_args() returns a string of /welcome/index
*
* @access private
* @return string
*/
private function _parse_cli_args()
{
// Returns the parameters passed when running in command line mode.
// Because the first parameter is the current file name, we want to get it starting from the second one.
$args = array_slice($_SERVER['argv'], 1);
//Return a string concatenated by '/' strings, because $this->uri_string is a string.
return $args ? '/' . implode('/', $args) : '';
}
//------------------------------------------------ -----------------------
/**
* Filter segments for malicious characters
* Filter illegal characters
* @access private
* @param string
* @return string
*/
function _filter_uri($str)
{
if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE )
{
                  // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards backwards
// compatibility compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
// It probably means because of PHP5.3.0 characters -increased as a need for righteousness. So when using str_replace() here, you need to add preg_quote() to escape -
                                                                                                                                                                         '-'))."]+$|i", $str))
                                                                 
show_error('The URI you submitted has disallowed characters.', 400);
      }  
} }
// Convert programatic characters to entities
// Convert character entity
        $bad    = array('$',        '(',        ')',        '%28',      '%29');  
        $good   = array('$',    '(',    ')',    '(',    ')');  
  
        return str_replace($bad, $good, $str);  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Remove the suffix from the URL if needed
* // Remove our custom suffix from the url.
* @access private
* @return void
*/  
    function _remove_url_suffix()  
    {  
        if  ($this->config->item('url_suffix') != "")  
        {  
            $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);  
        }  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Explode the URI Segments. The individual segments will
* be stored in the $this->segments array.
* Split the uri into positive segments and filter each segment, and store them in $this->segments[]
* @access private
* @return void
*/  
    function _explode_segments()  
    {  
        foreach (explode("/", preg_replace("|/*(.+?)/*$|", "\1", $this->uri_string)) as $val)  
        {  
            // Filter segments for security  
            $val = trim($this->_filter_uri($val));  
  
            if ($val != '')  
            {  
                $this->segments[] = $val;  
            }  
        }  
    }  
  
    // --------------------------------------------------------------------  
    /**
* Re-index Segments Re-index segment
* Makes the segments saved starting with subscript 1. This makes it easier to use because there is a 1:1 relationship between the segment array and the real segment
* This function re-indexes the $this->segment array so that it
* starts at 1 rather than 0. Doing so makes it simpler to
* use functions like $this->uri->segment(n) since there is
* a 1:1 relationship between the segment array and the actual segments.
*
* @access private
* @return void
*/  
    function _reindex_segments()  
    {  
        array_unshift($this->segments, NULL);  
        array_unshift($this->rsegments, NULL);  
        unset($this->segments[0]);  
        unset($this->rsegments[0]);  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
* Fetch a URI Segment
* Get a uri segment
* This function returns the URI segment based on the number provided. Provided
* This function returns a uri segment based on the provided number
* @access public
* @param integer
* @param bool
* @return string
*/
function segment($n, $no_result = FALSE)
{
return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
}
//------------------------------------------------ -----------------------
/**
* Fetch a URI "routed" Segment
* Return a certain section after the routing is determined
* This function returns the re-routed URI segment (assuming routing rules are used)
* based on the number provided. If there is no routing this function returns the
* same result as $this->segment()
* This function returns a routed uri segment based on the provided number (assuming routing rules have been used)
* If not routed yet, this function will be the same as $this->segment()
*
* @access public
* @param integer
* @param bool
* @return string
*/
function rsegment($n, $no_result = FALSE)
{
return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
}
//------------------------------------------------ -----------------------
/**
* Generate generates a key value pair from the URI string
* Generate an array of key-value pairs based on the uri string
*
* This function generates and associative array of URI data starting
* at the supplied by. . . Provide segment. For example, if this is your URI:
*
* example.com/user/search/name/joe/location/UK/gender/male
*
* You can use this function to generate an array with this prototype:
*
* array (
* name => joe
* location => UK
*   gender => male
* ) )
* This function generates an associative array from the uri segment
* Example: If your uri is like this
* example.com/user/search/name/joe/location/UK/gender/male
* Then a prototype like this will be generated
* array (
* name => joe
* location => UK
*   gender => male
* ) )
* @access public
* @param integer the starting segment number
* @param array an array of default values ​​
* @return array
*/
function uri_to_assoc($n = 3, $default = array())
{
return $this->_uri_to_assoc($n, $default, 'segment');
}
/**
* Identical exactly the same thing to above only it uses the re-routed segment array
* It is exactly the same as the previous function except that it flushes the routed segment array (note the third parameter)
* @access public
* @param integer the starting segment number
* @param array an array of default values ​​
* @return array
*
*/
function ruri_to_assoc($n = 3, $default = array())
{
return $this->_uri_to_assoc($n, $default, 'rsegment');
}
//------------------------------------------------ -----------------------
/**
* Generate a key value pair from the URI string or Re-routed URI string
* Generate an array of key-value pairs based on the uri string or the rerouted uri string
* @access private
* @param integer the starting segment number starting segment number
* @param array an array of default values ​​
* @param string which array we should use
* @return array
*/
function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
{
// Determine whether the segmented array is rerouted.
if ($which == 'segment')
{
$total_segments = 'total_segments';
$segment_array = 'segment_array';
} }
else
{
$total_segments = 'total_rsegments';
$segment_array = 'rsegment_array';
} }
// Is $n a number?
if ( ! is_numeric($n))
{
return $default;
} }
// There is enough key $n in the cache uri segment list
if (isset($this->keyval[$n]))
{
return $this->keyval[$n];
} }
// The total number of segments is less than $n
if ($this->$total_segments() < $n)
{
if (count($default) == 0)
                                                                 
return array();
      }  
$retval = array();
foreach ($default as $val)
                                                                 
$retval[$val] = FALSE;
      }  
return $retval;
} }
$segments = array_slice($this->$segment_array(), ($n - 1));
$i = 0;
        $lastval = '';  
        $retval  = array();  
        foreach ($segments as $seg)  
        {  
            if ($i % 2)  
            {  
                $retval[$lastval] = $seg;  
            }  
            else  
            {  
                $retval[$seg] = FALSE;  
                $lastval = $seg;  
            }  
  
            $i++;  
        }  
  
        if (count($default) > 0)  
        {  
            foreach ($default as $val)  
            {  
                if ( ! array_key_exists($val, $retval))  
                {  
                    $retval[$val] = FALSE;  
                }  
            }  
        }  
  
        // Cache the array for reuse  
        // 缓存数组一遍重用  
        $this->keyval[$n] = $retval;  
        return $retval;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Generate a URI string from an associative associative array array
* Generate a uri string based on an associative array
*
* @access public
* @param array an associative array of key/values ​​
* @return array
*/  
    function assoc_to_uri($array)  
    {  
        $temp = array();  
        foreach ((array)$array as $key => $val)  
        {  
            $temp[] = $key;  
            $temp[] = $val;  
        }  
  
        return implode('/', $temp);  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Fetch a URI Segment and add a trailing slash
* Get a uri segment and add a /
*
* @access public
* @param integer
* @param string
* @return string
*/  
    function slash_segment($n, $where = 'trailing')  
    {  
        return $this->_slash_segment($n, $where, 'segment');  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch a URI Segment and add a trailing slash 
     * 获取一个已经路由的uri段并且添加/ 
     * @access  public 
     * @param   integer 
     * @param   string 
     * @return  string 
     */  
    function slash_rsegment($n, $where = 'trailing')  
    {  
        return $this->_slash_segment($n, $where, 'rsegment');  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch a URI Segment and add a trailing slash - helper function 
     * 
     * @access  private 
     * @param   integer 
     * @param   string 
     * @param   string 
     * @return  string 
     */  
    function _slash_segment($n, $where = 'trailing', $which = 'segment')  
    {  
        $leading    = '/';  
        $trailing   = '/';  
  
        if ($where == 'trailing')  
        {  
            $leading    = '';  
        }  
        elseif ($where == 'leading')  
        {  
            $trailing   = '';  
        }  
  
        return $leading.$this->$which($n).$trailing;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Segment Array
* Get segment array
* @access public
* @return array
*/  
    function segment_array()  
    {  
        return $this->segments;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Routed Segment Array
* Get the routed segment array
* @access public
* @return array
*/  
    function rsegment_array()  
    {  
        return $this->rsegments;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Total number of segments
* Get the total number of segments
* @access public
* @return integer
*/  
    function total_segments()  
    {  
        return count($this->segments);  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Total number of routed segments
* Get the total number of routed segments
* @access public
* @return integer
*/  
    function total_rsegments()  
    {  
        return count($this->rsegments);  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch the entire URI string 
     * 
     * @access  public 
     * @return  string 
     */  
    function uri_string()  
    {  
        return $this->uri_string;  
    }  
  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch the entire Re-routed URI string 
     *  www.2cto.com
     * @access  public 
     * @return  string 
     */  
    function ruri_string()  
    {  
        return '/'.implode('/', $this->rsegment_array());  
    }  
  
}  
// END URI Class  
  
/* End of file URI.php */  
/* Location: ./system/core/URI.php */  
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477684.htmlTechArticle[php] ?php if ( ! defined(BASEPATH)) exit(No direct script access allowed); /** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @packag...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!