CI Frame source Pages-uri.php

Source: Internet
Author: User
Tags control characters codeigniter nginx server
CI Framework source Reading---------uri.php
 Config =& load_class (' config ', ' core '), Log_message (' Debug ', "URI class Initialized");} --------------------------------/** * Get the URI String * * @accessprivate * @returnstring */function _fetch_uri_strin G () {///below the Uri_protocol is a configuration item in the config.php,//is actually asking you which way to detect the meaning of the URI information,//The default is auto, automatic detection, that is, through a variety of ways to detect, until detected, or all the way to complete detection. if (Strtoupper ($this->config->item (' uri_protocol ')) = = ' AUTO ') {//is the request coming from the command line?//start tasting       Try various ways, mainly: command line, Request_uri, Path_info, query_string.       The following will appear multiple times $this->_set_uri_string ($STR) This method, this method is nothing else, is to $str through//filter and trim value to $this->uri_string property, here temporarily can be understood as the assignment. If the script is running in command-line mode, then the parameter is passed by $_server[' argv '). The following//$this->_parse_cli_args (); Just get the parameters that match the route we need//if you do not use the command line to execute the script, the following if can be used temporarily without the tube. At this time we found that the URI class uses the function Php_sapi_name () to test the different environment//The result of output under the Apache environment is "apache2handler";//The result of output in CGI mode is "cgi-fcgi"// If run under command-line mode, the result of the output is: "CLI" if (php_sapi_name () = = ' CLI ' or defined (' STDIN ') {$this->_set_uri_string ($this, _parse_cLi_args ()); return;} Let's try the Request_uri first, this would work on most situations//find Uriif ($uri = $this->_detect_uri ()) {//If you find ur I set $this->uri_string$this->_set_uri_string ($uri); return;} Is there a path_info variable? Note:some servers seem seems to have trouble trouble with getenv () so we'll test it with the it a ways//get PATH $_server[' path_info ' not Is every request to have a getenv (' path_info ') $path = (isset ($_server[' path_info ')) when not in use? $_server[' path_info ': @getenv (' path_info '); if (Trim ($path, '/')! = ' && $path! = "/". Self) {$this->_set_uri_string ($path); return;} No Path_info?... What's about query_string?//if you don't find $_server[' path_info ' 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 Path_info and query_string are not found we can only use $_getif (Is_array ($_get) &amp ;& count ($_get) = = 1 && triM (Key ($_get), '/')! = ') {$this->_set_uri_string (key ($_get)); return;} We have exhausted all of our options...//after we have not found the URI then we really can not find the $this->uri_string = "; return;} Here to re-write again get uri_protocol actually I think I can just get it once ... $uri = Strtoupper ($this->config->item (' Uri_protocol '));//The following is a different way to choose a different method to get the URI 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 you define the Uri_protocol is in the auto Request_uri CLI in addition to these three ways to perform the following paragraph. $path = (isset ($_server[$uri))? $_server[$uri]: @getenv ($uri); $this->_set_uri_string ($path);} --------------------------------/** * Set the URI String * * @accesspublic * @param String * @returnstring */function _ Set_uri_string ($STR) {//filter out control characters//filtering string remove_invisible_characters function 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/then empty $this->uri_string = ($str = = '/')? ": $STR;}  --------------------------------/** * Detects the URI * Find URI * This function would detect the URI automatically and fix The query string * if necessary. Essentials * If necessary, this function will automatically find the URI and fix the query string. * * @accessprivate * @returnstring */private function _detect_uri () {///If two values have one none then return (two variables are from the Web server, Encountered some special SERVER program, this is likely to be empty.) if (! isset ($_server[' Request_uri ')) OR! isset ($_server[' script_name ')) {return ';} Get Uri$uri = $_server[' Request_uri '];//if Script_name first appears in $uri position is 0 if (Strpos ($uri, $_server[' script_name ')) = = = 0 {//Remove URI and script_name the same part $uri = substr ($uri, strlen ($_server[' script_name '));} The function here is just to replace $_server[' script_name '] with//dirname ($_server[' script_name ')) ElseIf (Strpos ($uri, dirname ($_server[') Script_name ']) = = = 0) {$uri = substr ($uri, strlen (dirname ($_server[' script_name ')));} This section ensures guarantees that even on servers, that require the uri//to being in the query string (Nginx) a correct correct//U RI is found, and also fixes repair The Query_string server Var and $_get array.//This section guarantees that the URI can be correctly found even on the Nginx server, and also fixes the query_string server and the _get array. Judge $uri's first two characters?/if (strncmp ($uri, '?/', 2) = = = 0) {//Remove the first two characters $uri = substr ($uri, 2);} Use formal list to split a string $parts = Preg_split (' #\? #i ', $uri, 2); $uri = $parts [0];//] If it is possible to split two segments by the above-mentioned regular, then it is through query_string. The form of a routed access if (Isset ($parts [1])) {$_server[' query_string ') = $parts [The 1];//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 is empty, there are two cases, either through query_string. In the form of routing access,//So at this time $parts[0] is equal to the following two possible, while we//already through $parts[1] to get the information, you can return. It is either in the form of a paragraph, but the information of the segment is empty, that is, the direct access to the portal file without the passing of//any routing information, or it can be returned directly. if ($uri = = '/' | | | empty ($uri)) {return '/';} Returns the path portion of this URL. $uri = Parse_url ($uri, Php_url_path);//Do some final cleaning of the URI and return it//the URI in the//. /Replace 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/welcome/index String * * @acc Essprivate * @returnstring */private function _parse_cli_args () {//Returns the parameters passed when running in command-line mode. Since the first parameter is the current file name, we're going to get it from the second start. $args = Array_slice ($_server[' argv '), 1);//Returns a string that is spliced by the '/' string, because $this->uri_string is a string. Return $args? '/' . Implode ('/', $args): ';} --------------------------------/** * Filter segments segment for malicious malicious characters * filter illegal characters * @accessprivate * @param String * @returnstring */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 () was to maintain maintain backwards backwards//compatibility compatibility as many are Una Ware do not know of how characters in the Permitted_uri_chars would be parsed as a regex pattern//probably mean due to php5.3.0 Characters-are added to the need to be escaped. So here is the use of str_replace () to add preg_quote () to the-escape if (! Preg_match ("|^["). Str_replace (Array (' \\-', ' \-'), '-', Preg_quote ($ This->config->item (' Permitted_uri_chars '), '-'). "] +$|i ", $str)) {show_error (' The URI you submitted have disallowed characters. ', 400);}} Convert programatic characters to entities//converts the character entity $bad= Array (' $ ', ' (', ') ', '%28 ', '%29 '), $good = Array (' $ ', ' (', ') ', ' ( ', ') '; return Str_replace ($bad, $good, $str);} --------------------------------/** * Remove the suffix from the URL if needed *//Remove the URL of our custom suffix. * @accessprivate * @returnvoid */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'll * is stored in the $this->segments array. * Divide the URI into a positive segment and filter each segment at the same time, and deposit in $this->segments[] * @accessprivate * @returnvoid */function _explode_segments () {foreach (Explode ("/", Preg_replace ("|/* (. +?)" /*$| "," \\1 ", $this->uri_string)) as $val) {//Filter segments for security$val = Trim ($this->_filter_uri ($val)); ($val! = ") {$this->segments[] = $val;}}} --------------------------------/** * re-index segments re-index segment * makes the paragraph below Mark 1 start saving. This is easier to use because the segment array and the real segment have a 1:1 relationship * This function re-indexes the $this->segment array so that it * starts at 1 rather tha  N 0. Doing so makes it simpler to * use functions like $this->uri->segment (n) since there is * a 1:1 relationship relationship Bet Ween the segment array and the actual real segments. * * @accessprivate * @returnvoid */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 * gets a URI segment * This function returns the URI Segment based on t He number provided. provides * This function returns a number based on the provided URI segment * @accEsspublic * @paraminteger * @parambool * @returnstring */function segment ($n, $no _result = FALSE) {return (! Isset ($this-& gt;segments[$n]))? $no _result: $this->segments[$n];} --------------------------------/** * Fetch a URI "routed" Segment * Returns a section after determining the route * This function returns the re-routed  URI segment (assuming assumes routing rules rule is used) * Based on the number provided. If there is no routing this function returns the * same result as $this->segment () * This functions returns a routed URI segment based on the provided number (assuming that the routing rule has been Used) * If not yet with the route this function will be the same as $this->segment () * * @accesspublic * @paraminteger * @parambool * @returnstring */function rseg ment ($n, $no _result = FALSE) {return (! Isset ($this->rsegments[$n]))? $no _result: $this->rsegments[$n];}  --------------------------------/** * Generate generates a key value pair pair from the URI string * generates an array of key-value pairs based on the URI strings * * this function generates and associative associated array of URI data starting * At the supplied by ... Provide segment. For example, if the is your URI: *  *example.com/user/search/name/joe/location/uk/gender/male * * Can use this function to generate a array with this P Rototype: * * Array (*name = Joe *location = UK *gender = male *) * This function generates an associative array from the URI segment * Example: If your URI is like this * exa Mple.com/user/search/name/joe/location/uk/gender/male * Then a prototype like this will be produced * array (*name = Joe *location = UK *gender = male *) * @accesspublic * @paramintegerthe starting segment number * @paramarrayan array of default values * @retur Narray */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 * Keep up with a function is exactly the same just it flushes the segment array (take note of the third parameter) * @access p Ublic * @param integerthe starting segment number * @param Arrayan array of default values * @return Array * */function RU RI_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 * Generates an array of key values based on the URI string or the rerouted URI strings * @accessprivate * @paraminteger The starting segment number start segment * @paramarrayan array of default values * @paramstringwhich array we should use * @return Array */function _uri_to_assoc ($n = 3, $default = Array (), $which = ' segment ') {///differentiate the segment array is not rerouted. if ($which = = ' segment ') {$total _segments = ' total_segments '; $segment _array = ' Segment_array ';} else{$total _segments = ' total_rsegments '; $segment _array = ' Rsegment_array ';} $n is not a number if (! is_numeric ($n)) {return $default;} The list of cached URI segments is sufficient to exist $n this keyif (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 A S $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//buffer arrays Reuse $this->keyval[$n] = $retval; return $retval;} --------------------------------/** * Generate a URI string from an associative associative array * Generates a URI strings based on an associative array * * @ac Cesspublic * @paramarrayan Associative array of key/values * @returnarray */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 back, trailing slash * Gets a URI segment and adds a/* * @access Public * @paraminteger * @paramstring * @returnstring */function slash_segment ($n, $where = ' trailing ') {return $this->_ Slash_segment ($n, $where, ' segment ');} --------------------------------/** * Fetch a URI Segment and add a trailing slash * get a routed URI segment and add/* @accesspublic * @paraminteger * @paramstring * @returnstring */function slash_rsegment ($n, $where = ' trailing ') {return $this->_slash _segment ($n, $where, ' rsegment ');}  --------------------------------/** * Fetch a URI Segment and add a trailing slash-helper function * * @accessprivate  * @paraminteger * @paramstring * @paramstring * @returnstring */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 * Gets the segment array * @accesspublic * @returnarray */function Segment_array () {R Eturn $this->segments;} --------------------------------/** * Routed Segment array * Gets the segment array that has been routed * @accesspublic * @returnarray */function RSEGM Ent_array () {return $this->rsegments;} --------------------------------/** * Total number of segments * Gets the sum of segments * @accesspublic * @returninteger */function Total_segments () {return count ($this->segments);} --------------------------------/** * Total number of routed segments * Gets the sum of segments that have been routed * @accesspublic * @returninteger * * function total_rsegments () {return count ($this->rsegments);} --------------------------------/** * Fetch the entire URI string * * @accesspublic * @returnstring */function Uri_stri Ng () {return $this->uri_string;} --------------------------------/** * Fetch the entire re-routed URI String * * @accesspublic * @returnstring */functio n ruri_string () {return '/'. Implode ('/', $this->rsegment_array ());}} End URI class/* end of file uri.php *//* location:./system/core/uri.php */


  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.