CI framework source code reading --------- URI. php & lt ;? Phpif (! Defined ('basepath') exit ('nodirectscriptaccessallowed');/*** CodeIgniter ** Anopen CI framework source code reading --------- URI. php
Config = & load_class ('config', 'core'); log_message ('debug', "URI Class Initialized ");} // ------------------------------/*** Get the URI String ** @ accessprivate * @ returnstring */function _ fetch_uri_string () {// the uri_protocol below is in config. A configuration item in php, // In fact, is to ask you which method to detect the uri information, // The default is AUTO, automatic detection, that is, through various methods of detection, it is detected 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 to try the following methods: command line, REQUEST_URI, PATH_INFO, QUERY_STRING. // The $ this-> _ set_uri_string ($ str) method appears multiple times below. this method has nothing to do with it, the value of $ str is filtered and trimmed by // to the $ this-> uri_string attribute. it can be understood as a value for the moment. // If the script is run in command line mode, the parameter is passed through $ _ SERVER ['argv. The following // $ this-> _ parse_cli_args (); obtains some routing parameters that meet our needs. // If you do not use the command line to execute the script, you can skip the following if statement. // At this time, we found that the URI class uses the php_sapi_name () function to test different environments. // The output result in the apache environment is "apache2handler "; // the output result in cgi mode is "cgi-fcgi" // if it is run in command line mode, the output result 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 will work in most situations // find uriif ($ uri = $ this-> _ detect_uri ()) {// If you find uri settings $ this-> uri_string $ this-> _ set_uri _ String ($ uri); return;} // Is there a PATH_INFO variable? // Note: some servers seem seems to be troublesome to have trouble with getenv () so we'll test it two ways // get path $ _ SERVER ['path _ info'] is not available for every request. so when there is no, use getenv ('path _ INFO ') $ 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, 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 are found, we can only use $ _ GETif (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 the uri, so we really cannot find $ this-> uri_string = ''; return ;} // here, I wrote it again to get uri_protocol. actually, I think it can be obtained only once... $ Uri = strtoupper ($ this-> config-> item ('uri _ Protocol ')); // The following describes how to obtain a uri using 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 beyond the three methods of AUTO REQUEST_URI CLI, execute the following section. $ 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 // Filter string remove_invisible_characters function in common. $ str = remove_invisible_characters ($ str, FALSE) in php; // If the URI contains only a slash we'll kill it // If the string contains only one/ Clear $ this-> uri_string = ($ str = '/')? '': $ Str;} // --------------------------------/*** Detects the URI * find uri * This function will detect the URI automatically and fix the query string * if necessary. necessity * if necessary, this function will automatically search for the uri and fix the query string. ** @ Accessprivate * @ returnstring */private function _ detect_uri () {// returns if either of the two values does not exist (the two variables come from the web server, some special server programs 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) {// remove the section with the same uri and SCRIPT_NAME $ uri = substr ($ uri, strlen ($ _ SERVER ['script _ name']);} // the above function is only used to replace $ _ SERVER ['script _ name'] with // dirname ($ _ SERVER ['script _ name']). elseif (strpos ($ uri, dirname ($ _ SERVER ['script _ name']) ===0) {$ uri = substr ($ u Ri, strlen (dirname ($ _ SERVER ['script _ name']);} // This section ensures ensure that even on servers that require the URI // to be in the query string (Nginx) a correct is correct // URI is found, and also fixes repairs the QUERY_STRING server var and $ _ GET array. // This part ensures that the uri can be correctly found, even on the Nginx server, and the QUERY_STRING server and $ _ GET array are also fixed. // Determine the first two characters of $ uri, right? /If (strncmp ($ uri ,'? /', 2) = 0) {// remove the first two characters $ uri = substr ($ uri, 2 );} // use the main book to split the string $ parts = preg_split ('#\? # I ', $ uri, 2); $ uri = $ parts [0]; // if the preceding regular expression can be used to split two segments, then the query string is used? If (isset ($ parts [1]) {$ _ SERVER ['query _ string'] = $ parts [1]; // The function parses the query string to the $ _ GET variable. Parse_str ($ _ SERVER ['query _ string'], $ _ GET);} else {$ _ SERVER ['query _ string'] = ''; $ _ GET = array ();} // if it is/or null, either the query_string or? In this case, $ parts [0] is equal to the following two possibilities. at the same time, we have obtained the desired information through $ parts [1, you can return. // Either it is in the form of a segment, but the segment information is blank, that is, directly access the entry file without // any route information transmission, you can also directly return. If ($ uri = '/' | empty ($ uri) {return '/';} // return the path part of the url. $ Uri = parse_url ($ uri, PHP_URL_PATH); // Do some final cleaning of the URI and return it // Convert //.. /replace with/And return str_replace (array ('//','.. /'),'/', trim ($ uri ,'/'));} // --------------------------------/*** Parse cli arguments * parses the cli parameter * Take each command line argument and assume it is a URI segment. * If you perform this operation in the command line * php d:/wamp/www/CodeIgniter/index. php welcome index * _ parse_cli_args () returns /Welcome/index strings ** @ accessprivate * @ returnstring */private function _ parse_cli_args () {// return the parameters passed during running in command line mode. // Because the first parameter is the current file name, we need to obtain it from the second one. $ Args = array_slice ($ _ SERVER ['argv'], 1); // returns a string concatenated by a '/' string because $ this-> uri_string is a string. Return $ args? '/'. Implode ('/', $ args ):'';} // --------------------------------/*** Filter segments segment for malicious characters * Filter invalid characters * @ accessprivate * @ paramstring * @ 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 () is to maintain backwards backward // compatibility as unknown are unknown unaware unknown of how characters in the permitted_uri_chars will be parsed as a regex pattern // probably means that the php5.3.0 character-is added to escape. So here we use str_replace () to add preg_quote () to escape-if (! Preg_match ("| ^ [". str_replace (array ('\-', '\-'), '-', preg_quote ($ this-> config-> item ('permitted _ uri_chars '), '-')). "] + $ | I", $ str) {show_error ('The URI you submitted has disallowed characters. ', 400) ;}// Convert programatic characters to entities // Convert character entities $ bad = array (' $ ','(',')', '% 28',' % 29'); $ good = array ('$ ','(',')','(',')'); return str_replace ($ bad, $ good, $ str );}//-------------------------------- /*** Remove the suffix from the URL if needed * // Remove the custom suffix of the url. * @ 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 will * be stored in the $ this-> segments array. * split the uri into a forward segment and filter each segment at the same time, and store it 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); if ($ val! = '') {$ This-> segments [] = $ val ;}}} // --------------------------------/*** Re-index Segments Re-index the SEGMENT * to save the following marked 1. This method is easier to use because the segment array has a relationship with the actual 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 real segments. ** @ accessprivate * @ returnvoid */function _ reindex_segments () {array_unshift ($ this-> segments, NULL); array_uns Hift ($ 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. provide * This function returns a provided-based numeric uri segment * @ accesspublic * @ paraminteger * @ parambool * @ returnstring */function segment ($ n, $ no_result = FALSE) {return (! Isset ($ this-> segments [$ n])? $ No_result: $ this-> segments [$ n];} // --------------------------------/*** Fetch a URI "routed" Segment * returns a segment after the route is determined. * This function returns the re-routed URI Segment (assuming assumes that the 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 provided number-based uri segment (assuming that the routing rule is already in use). * If this function is not used with routing, it will be associated with $ this-> segment () is the same ** @ Accesspublic * @ paraminteger * @ parambool * @ returnstring */function rsegment ($ n, $ no_result = FALSE) {return (! Isset ($ this-> rsegments [$ n])? $ No_result: $ this-> rsegments [$ n];} // --------------------------------/*** Generate 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 associated array URI data starting * at the supplied... 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 associated array from the uri segment * example: if your uri is like this * example.com/user/search/name/joe/location/UK/gender/male *, a prototype such as * array (* name => joe * location => UK * gender => m Ale *) * @ accesspublic * @ paramintegerthe starting segment number * @ paramarrayan array of default values * @ returnarray */function uri_to_assoc ($ n = 3, $ default = array ()) {return $ this-> _ uri_to_assoc ($ n, $ default, 'segment ');} /*** the Identical thing to the above only it uses the re-routed segment array * is exactly the same as the last function, except that it flushes the route segment array (note that the third parameters) * @ access public * @ param integerthe starting segment number * @ Param arrayan 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 Re-routed uri string *@ accessprivate * @ paramintegerthe starting segment number start segment number * @ paramarrayan array Default values * @ paramstringwhich array we shocould use * @ returnarray */function _ uri_to_assoc ($ n = 3, $ default = array (), $ which = 'segment ') {// 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';} // $ n is not a number if (! Is_numeric ($ n) {return $ default;} // The cache uri segment list contains the $ n 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 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 // Cache array reuse once $ this-> keyval [$ n] = $ retval; return $ retval ;} // --------------------------------/*** Generate a URI string from an associative associated array * Generate a uri string based on an associated array ** @ accesspublic * @ 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);} // after --------------------------------/*** Fetch a URI Segment and add a trailing, trailing slash * gets a uri segment and adds a/** @ accesspublic * @ 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 the uri Segment of the route and add/* @ accesspublic * @ paraminteger * @ paramstring * @ returnstring * /function slash_rsegment ($ n, $ where = 'trailing') {return $ this-> _ slash_segment ($ n, $ where, 'rsegment ');} // Configure/*** Fetch a URI Segment and add a trailing slash-helper function ** @ accessprivate * @ paraminteger * @ 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 * obtain the Segment Array * @ accesspublic * @ returnarray */function segment_array () {return $ this-> segments ;} // --------------------------------/*** Routed Segment Array * obtain the Segment Array that has been Routed * @ accesspublic * @ returnarray */function rsegment_array () {return $ this-> rsegments ;} // ------------------------------/*** Total number of segments * obtain the Total number of segments * @ accesspublic * @ returninteger */function total_segments () {return count ($ this-> segments );} // --------------------------------/*** Total number of routed segments * obtain the Total number of segments already routed * @ accesspublic * @ returninteger */function total_rsegments () {return count ($ this-> rsegments);} // ------------------------------/*** Fetch the entire URI string ** @ accesspublic * @ returnstring */function uri_string () {return $ this-> uri_string;} // --------------------------------/*** Fetch the entire Re-routed URI string ** @ accesspublic * @ returnstring */function ruri_string () {return '/'. implode ('/', $ this-> rsegment_array () ;}// end uri Class/* End of file URI. php * // * Location :. /system/core/URI. php */