From 30b40153739419d50c64e2dce338166a5c58ab58 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 20 Jul 2007 00:01:13 +0000 Subject: --- system/codeigniter/CodeIgniter.php | 4 +- system/libraries/Output.php | 4 +- system/libraries/Router.php | 247 ++++--------------------------------- system/libraries/URI.php | 244 ++++++++++++++++++++++++++++++++++-- system/libraries/Xmlrpc.php | 1 - 5 files changed, 264 insertions(+), 236 deletions(-) diff --git a/system/codeigniter/CodeIgniter.php b/system/codeigniter/CodeIgniter.php index 1cd66a187..160d86c8e 100644 --- a/system/codeigniter/CodeIgniter.php +++ b/system/codeigniter/CodeIgniter.php @@ -77,6 +77,7 @@ $EXT->_call_hook('pre_system'); */ $CFG =& load_class('Config'); +$URI =& load_class('URI'); $RTR =& load_class('Router'); $OUT =& load_class('Output'); @@ -101,7 +102,6 @@ if ($EXT->_call_hook('cache_override') === FALSE) */ $IN =& load_class('Input'); -$URI =& load_class('URI'); $LANG =& load_class('Language'); /* @@ -213,7 +213,7 @@ else // Call the requested method. // Any URI segments present (besides the class/function) will be passed to the method for convenience - call_user_func_array(array(&$CI, $method), array_slice($RTR->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3))); + call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3))); } } diff --git a/system/libraries/Output.php b/system/libraries/Output.php index 756d4b4cc..53003f180 100644 --- a/system/libraries/Output.php +++ b/system/libraries/Output.php @@ -301,7 +301,7 @@ class CI_Output { function _display_cache(&$CFG, &$RTR) { $CFG =& load_class('Config'); - $RTR =& load_class('Router'); + $URI =& load_class('URI'); $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path'); @@ -313,7 +313,7 @@ class CI_Output { // Build the file path. The file name is an MD5 hash of the full URI $uri = $CFG->item('base_url'). $CFG->item('index_page'). - $RTR->uri_string; + $URI->uri_string; $filepath = $cache_path.md5($uri); diff --git a/system/libraries/Router.php b/system/libraries/Router.php index 6af6ad380..6fca45227 100644 --- a/system/libraries/Router.php +++ b/system/libraries/Router.php @@ -28,10 +28,7 @@ */ class CI_Router { - var $config; - var $uri_string = ''; - var $segments = array(); - var $rsegments = array(); + var $config; var $routes = array(); var $error_routes = array(); var $class = ''; @@ -49,7 +46,8 @@ class CI_Router { function CI_Router() { $this->config =& load_class('Config'); - $this->_set_route_mapping(); + $this->uri =& load_class('URI'); + $this->_set_routing(); log_message('debug', "Router Class Initialized"); } @@ -64,17 +62,17 @@ class CI_Router { * @access private * @return void */ - function _set_route_mapping() + function _set_routing() { // Are query strings enabled in the config file? // If so, we're done since segment based URIs are not used with query strings. if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')])) { - $this->set_class(trim($this->_filter_uri($_GET[$this->config->item('controller_trigger')]))); + $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')]))); if (isset($_GET[$this->config->item('function_trigger')])) { - $this->set_method(trim($this->_filter_uri($_GET[$this->config->item('function_trigger')]))); + $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')]))); } return; @@ -90,16 +88,10 @@ class CI_Router { $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']); // Fetch the complete URI string - $this->uri_string = $this->_get_uri_string(); - - // If the URI contains only a slash we'll kill it - if ($this->uri_string == '/') - { - $this->uri_string = ''; - } + $this->uri->_fetch_uri_string(); // Is there a URI string? If not, the default controller specified in the "routes" file will be shown. - if ($this->uri_string == '') + if ($this->uri->uri_string == '') { if ($this->default_controller === FALSE) { @@ -114,47 +106,35 @@ class CI_Router { } unset($this->routes['default_controller']); - // Do we need to remove the suffix specified in the config file? - if ($this->config->item('url_suffix') != "") - { - $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string); - } + // Do we need to remove the URL suffix? + $this->uri->_remove_url_suffix(); - // Explode the URI Segments. The individual segments will - // be stored in the $this->segments array. - 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; - } + // Compile the segments into an array + $this->uri->_explode_segments(); // Parse any custom routing that may exist $this->_parse_routes(); // Re-index the segment array so that it starts with 1 rather than 0 - $this->_reindex_segments(); + $this->uri->_reindex_segments(); } // -------------------------------------------------------------------- /** - * Compile Segments + * Set the Route * * This function takes an array of URI segments as - * input, and puts it into the $this->segments array. - * It also sets the current class/method + * input, and sets the current class/method * * @access private * @param array * @param bool * @return void */ - function _compile_segments($segments = array()) + function _set_request($segments = array()) { - $segments = $this->_validate_segments($segments); + $segments = $this->_validate_request($segments); if (count($segments) == 0) { @@ -180,8 +160,8 @@ class CI_Router { // Update our "routed" segment array to contain the segments. // Note: If there is no custom routing, this array will be - // identical to $this->segments - $this->rsegments = $segments; + // identical to $this->uri->segments + $this->uri->rsegments = $segments; } // -------------------------------------------------------------------- @@ -194,7 +174,7 @@ class CI_Router { * @param array * @return array */ - function _validate_segments($segments) + function _validate_request($segments) { // Does the requested controller exist in the root folder? if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) @@ -237,182 +217,7 @@ class CI_Router { // Can't find the requested controller... show_404(); } - - // -------------------------------------------------------------------- - /** - * Re-index Segments - * - * This function re-indexes the $this->segment array so that it - * starts at 1 rather then 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() - { - // Is the routed segment array different then the main segment array? - $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE; - - $i = 1; - foreach ($this->segments as $val) - { - $this->segments[$i++] = $val; - } - unset($this->segments[0]); - if ($diff == FALSE) - { - $this->rsegments = $this->segments; - } - else - { - $i = 1; - foreach ($this->rsegments as $val) - { - $this->rsegments[$i++] = $val; - } - unset($this->rsegments[0]); - } - } - - // -------------------------------------------------------------------- - - /** - * Get the URI String - * - * @access private - * @return string - */ - function _get_uri_string() - { - if (strtoupper($this->config->item('uri_protocol')) == 'AUTO') - { - // If the URL has a question mark then it's simplest to just - // build the URI string from the zero index of the $_GET array. - // This avoids having to deal with $_SERVER variables, which - // can be unreliable in some environments - if (is_array($_GET) AND count($_GET) == 1) - { - // Note: Due to a bug in current() that affects some versions - // of PHP we can not pass function call directly into it - $keys = array_keys($_GET); - return current($keys); - } - - // Is there a PATH_INFO variable? - // Note: some servers seem to have trouble with getenv() so we'll test it two ways - $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO'); - if ($path != '' AND $path != "/".SELF) - { - return $path; - } - - // No PATH_INFO?... What about QUERY_STRING? - $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING'); - if ($path != '') - { - return $path; - } - - // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists? - $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO'); - if ($path != '' AND $path != "/".SELF) - { - return $path; - } - - // We've exhausted all our options... - return ''; - } - else - { - $uri = strtoupper($this->config->item('uri_protocol')); - - if ($uri == 'REQUEST_URI') - { - return $this->_parse_request_uri(); - } - - return (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri); - } - } - - // -------------------------------------------------------------------- - - /** - * Parse the REQUEST_URI - * - * Due to the way REQUEST_URI works it usually contains path info - * that makes it unusable as URI data. We'll trim off the unnecessary - * data, hopefully arriving at a valid URI that we can use. - * - * @access private - * @return string - */ - function _parse_request_uri() - { - if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '') - { - return ''; - } - - $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI'])); - - if ($request_uri == '' OR $request_uri == SELF) - { - return ''; - } - - $fc_path = FCPATH; - if (strpos($request_uri, '?') !== FALSE) - { - $fc_path .= '?'; - } - - $parsed_uri = explode("/", $request_uri); - - $i = 0; - foreach(explode("/", $fc_path) as $segment) - { - if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i]) - { - $i++; - } - } - - $parsed_uri = implode("/", array_slice($parsed_uri, $i)); - - if ($parsed_uri != '') - { - $parsed_uri = '/'.$parsed_uri; - } - - return $parsed_uri; - } - - // -------------------------------------------------------------------- - - /** - * Filter segments for malicious characters - * - * @access private - * @param string - * @return string - */ - function _filter_uri($str) - { - if ($this->config->item('permitted_uri_chars') != '') - { - if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str)) - { - exit('The URI you submitted has disallowed characters.'); - } - } - return $str; - } - // -------------------------------------------------------------------- /** @@ -431,18 +236,18 @@ class CI_Router { // There is a default scaffolding trigger, so we'll look just for 1 if (count($this->routes) == 1) { - $this->_compile_segments($this->segments); + $this->_set_request($this->uri->segments); return; } // Turn the segment array into a URI string - $uri = implode('/', $this->segments); - $num = count($this->segments); + $uri = implode('/', $this->uri->segments); + $num = count($this->uri->segments); // Is there a literal match? If so we're done if (isset($this->routes[$uri])) { - $this->_compile_segments(explode('/', $this->routes[$uri])); + $this->_set_request(explode('/', $this->routes[$uri])); return; } @@ -461,14 +266,14 @@ class CI_Router { $val = preg_replace('#^'.$key.'$#', $val, $uri); } - $this->_compile_segments(explode('/', $val)); + $this->_set_request(explode('/', $val)); return; } } // If we got this far it means we didn't encounter a // matching route so we'll set the site default route - $this->_compile_segments($this->segments); + $this->_set_request($this->uri->segments); } // -------------------------------------------------------------------- diff --git a/system/libraries/URI.php b/system/libraries/URI.php index 5cedd8e0f..2777e2f18 100644 --- a/system/libraries/URI.php +++ b/system/libraries/URI.php @@ -28,8 +28,10 @@ */ class CI_URI { - var $router; var $keyval = array(); + var $uri_string; + var $segments = array(); + var $rsegments = array(); /** * Constructor @@ -42,10 +44,229 @@ class CI_URI { */ function CI_URI() { - $this->router =& load_class('Router'); + $this->config =& load_class('Config'); log_message('debug', "URI Class Initialized"); } + + // -------------------------------------------------------------------- + + /** + * Get the URI String + * + * @access private + * @return string + */ + function _fetch_uri_string() + { + if (strtoupper($this->config->item('uri_protocol')) == 'AUTO') + { + // If the URL has a question mark then it's simplest to just + // build the URI string from the zero index of the $_GET array. + // This avoids having to deal with $_SERVER variables, which + // can be unreliable in some environments + if (is_array($_GET) AND count($_GET) == 1) + { + // Note: Due to a bug in current() that affects some versions + // of PHP we can not pass function call directly into it + $keys = array_keys($_GET); + $this->uri_string = current($keys); + } + + // Is there a PATH_INFO variable? + // Note: some servers seem to have trouble with getenv() so we'll test it two ways + $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO'); + if ($path != '' AND $path != "/".SELF) + { + $this->uri_string = $path; + } + + // No PATH_INFO?... What about QUERY_STRING? + $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING'); + if ($path != '') + { + $this->uri_string = $path; + } + + // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists? + $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO'); + if ($path != '' AND $path != "/".SELF) + { + $this->uri_string = $path; + } + + // We've exhausted all our options... + $this->uri_string = ''; + } + else + { + $uri = strtoupper($this->config->item('uri_protocol')); + + if ($uri == 'REQUEST_URI') + { + $this->uri_string = $this->_parse_request_uri(); + } + + $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri); + } + + // If the URI contains only a slash we'll kill it + if ($this->uri_string == '/') + { + $this->uri_string = ''; + } + } + + // -------------------------------------------------------------------- + + /** + * Parse the REQUEST_URI + * + * Due to the way REQUEST_URI works it usually contains path info + * that makes it unusable as URI data. We'll trim off the unnecessary + * data, hopefully arriving at a valid URI that we can use. + * + * @access private + * @return string + */ + function _parse_request_uri() + { + if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '') + { + return ''; + } + + $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI'])); + + if ($request_uri == '' OR $request_uri == SELF) + { + return ''; + } + + $fc_path = FCPATH; + if (strpos($request_uri, '?') !== FALSE) + { + $fc_path .= '?'; + } + + $parsed_uri = explode("/", $request_uri); + + $i = 0; + foreach(explode("/", $fc_path) as $segment) + { + if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i]) + { + $i++; + } + } + + $parsed_uri = implode("/", array_slice($parsed_uri, $i)); + + if ($parsed_uri != '') + { + $parsed_uri = '/'.$parsed_uri; + } + + return $parsed_uri; + } + + // -------------------------------------------------------------------- + + /** + * Filter segments for malicious characters + * + * @access private + * @param string + * @return string + */ + function _filter_uri($str) + { + if ($this->config->item('permitted_uri_chars') != '') + { + if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str)) + { + exit('The URI you submitted has disallowed characters.'); + } + } + return $str; + } + + // -------------------------------------------------------------------- + + /** + * Remove the suffix from the URL if needed + * + * @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. + * + * @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 + * + * This function re-indexes the $this->segment array so that it + * starts at 1 rather then 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() + { + // Is the routed segment array different then the main segment array? + $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE; + + $i = 1; + foreach ($this->segments as $val) + { + $this->segments[$i++] = $val; + } + unset($this->segments[0]); + + if ($diff == FALSE) + { + $this->rsegments = $this->segments; + } + else + { + $i = 1; + foreach ($this->rsegments as $val) + { + $this->rsegments[$i++] = $val; + } + unset($this->rsegments[0]); + } + } + // -------------------------------------------------------------------- /** @@ -60,7 +281,7 @@ class CI_URI { */ function segment($n, $no_result = FALSE) { - return ( ! isset($this->router->segments[$n])) ? $no_result : $this->router->segments[$n]; + return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n]; } // -------------------------------------------------------------------- @@ -79,7 +300,7 @@ class CI_URI { */ function rsegment($n, $no_result = FALSE) { - return ( ! isset($this->router->rsegments[$n])) ? $no_result : $this->router->rsegments[$n]; + return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n]; } // -------------------------------------------------------------------- @@ -203,6 +424,8 @@ class CI_URI { return $retval; } + // -------------------------------------------------------------------- + /** * Generate a URI string from an associative array * @@ -210,7 +433,8 @@ class CI_URI { * @access public * @param array an associative array of key/values * @return array - */ function assoc_to_uri($array) + */ + function assoc_to_uri($array) { $temp = array(); foreach ((array)$array as $key => $val) @@ -293,7 +517,7 @@ class CI_URI { */ function segment_array() { - return $this->router->segments; + return $this->segments; } // -------------------------------------------------------------------- @@ -306,7 +530,7 @@ class CI_URI { */ function rsegment_array() { - return $this->router->rsegments; + return $this->rsegments; } // -------------------------------------------------------------------- @@ -319,7 +543,7 @@ class CI_URI { */ function total_segments() { - return count($this->router->segments); + return count($this->segments); } // -------------------------------------------------------------------- @@ -332,7 +556,7 @@ class CI_URI { */ function total_rsegments() { - return count($this->router->rsegments); + return count($this->rsegments); } // -------------------------------------------------------------------- @@ -345,7 +569,7 @@ class CI_URI { */ function uri_string() { - return $this->router->uri_string; + return $this->uri_string; } diff --git a/system/libraries/Xmlrpc.php b/system/libraries/Xmlrpc.php index 49747e481..fc6208e94 100644 --- a/system/libraries/Xmlrpc.php +++ b/system/libraries/Xmlrpc.php @@ -70,7 +70,6 @@ class CI_Xmlrpc { function CI_Xmlrpc ($config = array()) { - $this->xmlrpcName = $this->xmlrpcName; $this->xmlrpc_backslash = chr(92).chr(92); -- cgit v1.2.3-24-g4f1b