From 65d603e03d3befd6e4f13361c78ab454ea57ba70 Mon Sep 17 00:00:00 2001 From: Dan Horrigan Date: Wed, 15 Dec 2010 08:38:30 -0500 Subject: Added full Query String and $_GET array support. This is enabled by default. Added a seperate config option to enable/disable the $_GET array. --- system/core/URI.php | 100 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 31 deletions(-) (limited to 'system/core/URI.php') diff --git a/system/core/URI.php b/system/core/URI.php index 5a5a37cc6..f6487d3f9 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -61,13 +61,10 @@ class CI_URI { { 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) && count($_GET) == 1 && trim(key($_GET), '/') != '') + // Let's try the REQUEST_URI first, this will work in most situations + if ($uri = $this->_get_request_uri()) { - $this->uri_string = key($_GET); + $this->uri_string = $this->_parse_request_uri($uri); return; } @@ -88,12 +85,10 @@ class CI_URI { return; } - // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists? - $path = str_replace($_SERVER['SCRIPT_NAME'], '', (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO')); - if (trim($path, '/') != '' && $path != "/".SELF) + // As a last ditch effort lets try using the $_GET array + if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '') { - // remove path and script information so we have good URI data - $this->uri_string = $path; + $this->uri_string = key($_GET); return; } @@ -106,7 +101,7 @@ class CI_URI { if ($uri == 'REQUEST_URI') { - $this->uri_string = $this->_parse_request_uri(); + $this->uri_string = $this->_parse_request_uri($this->_get_request_uri()); return; } @@ -123,36 +118,68 @@ class CI_URI { // -------------------------------------------------------------------- /** - * Parse the REQUEST_URI + * Get 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. + * Retrieves the REQUEST_URI, or equivelent for IIS. * * @access private * @return string */ - function _parse_request_uri() + function _get_request_uri() { - if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '') + $uri = FALSE; + + // Let's check for standard servers first + if (isset($_SERVER['REQUEST_URI'])) { - return ''; + $uri = $_SERVER['REQUEST_URI']; + if (strpos($uri, $_SERVER['HTTP_HOST']) !== FALSE) + { + $uri = preg_replace('/^\w+:\/\/[^\/]+/','',$uri); + } } - - $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI'])); - - if ($request_uri == '' OR $request_uri == SELF) + // Now lets check for IIS + elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) { - return ''; + $uri = $_SERVER['HTTP_X_REWRITE_URL']; + } + // Last ditch effort (for older CGI servers, like IIS 5) + elseif (isset($_SERVER['ORIG_PATH_INFO'])) + { + $uri = $_SERVER['ORIG_PATH_INFO']; + if ( ! empty($_SERVER['QUERY_STRING'])) + { + $uri .= '?'.$_SERVER['QUERY_STRING']; + } } - $fc_path = FCPATH.SELF; - if (strpos($request_uri, '?') !== FALSE) + return $uri; + } + + // -------------------------------------------------------------------- + + /** + * Parse 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 + * @param string + * @return string + */ + function _parse_request_uri($uri) + { + // Some server's require URL's like index.php?/whatever If that is the case, + // then we need to add that to our parsing. + $fc_path = ltrim(FCPATH.SELF, '/'); + if (strpos($uri, SELF.'?') !== FALSE) { $fc_path .= '?'; } - $parsed_uri = explode("/", $request_uri); + $parsed_uri = explode('/', ltrim($uri, '/')); $i = 0; foreach(explode("/", $fc_path) as $segment) @@ -163,14 +190,25 @@ class CI_URI { } } - $parsed_uri = implode("/", array_slice($parsed_uri, $i)); + $uri = implode("/", array_slice($parsed_uri, $i)); + + // Let's take off any query string and re-assign $_SERVER['QUERY_STRING'] and $_GET. + // This is only needed on some servers. However, we are forced to use it to accomodate + // them. + if (($qs_pos = strpos($uri, '?')) !== FALSE) + { + $_SERVER['QUERY_STRING'] = substr($uri, $qs_pos + 1); + parse_str($_SERVER['QUERY_STRING'], $_GET); + $uri = substr($uri, 0, $qs_pos); + } - if ($parsed_uri != '') + // If it is just a / or index.php then just empty it. + if ($uri == '/' || $uri == SELF) { - $parsed_uri = '/'.$parsed_uri; + $uri = ''; } - return $parsed_uri; + return $uri; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 48c718c4cbe4419411623b709f898d3d9a7d7aef Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Thu, 30 Dec 2010 23:40:02 +0000 Subject: Added support for calling controllers, methods and passing parameters via command line, either automatically or specifically with $config['uri_protocol'] = 'CLI'; --- system/core/URI.php | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'system/core/URI.php') diff --git a/system/core/URI.php b/system/core/URI.php index 047e3c9bc..479a225fb 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -68,6 +68,13 @@ class CI_URI { return; } + // Arguments exist, it must be a command line request + if ( ! empty($_SERVER['argv'])) + { + $this->uri_string = $this->_parse_cli_args(); + return; + } + // 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'); @@ -104,6 +111,11 @@ class CI_URI { $this->uri_string = $this->_parse_request_uri($this->_get_request_uri()); return; } + elseif ($uri == 'CLI') + { + $this->uri_string = $this->_parse_cli_args(); + return; + } $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri); } @@ -144,7 +156,7 @@ class CI_URI { { $uri = $_SERVER['HTTP_X_REWRITE_URL']; } - + // Last ditch effort (for older CGI servers, like IIS 5) elseif (isset($_SERVER['ORIG_PATH_INFO'])) { @@ -171,7 +183,7 @@ class CI_URI { * @param string * @return string */ - function _parse_request_uri($uri) + private function _parse_request_uri($uri) { // Some server's require URL's like index.php?/whatever If that is the case, // then we need to add that to our parsing. @@ -215,6 +227,23 @@ class CI_URI { // -------------------------------------------------------------------- + /** + * Parse cli arguments + * + * Take each command line argument and assume it is a URI segment. + * + * @access private + * @return string + */ + private function _parse_cli_args() + { + $args = array_slice($_SERVER['argv'], 1); + + return $args ? '/' . implode('/', $args) : ''; + } + + // -------------------------------------------------------------------- + /** * Filter segments for malicious characters * @@ -524,7 +553,7 @@ class CI_URI { { $leading = '/'; $trailing = '/'; - + if ($where == 'trailing') { $leading = ''; @@ -533,7 +562,7 @@ class CI_URI { { $trailing = ''; } - + return $leading.$this->$which($n).$trailing; } -- cgit v1.2.3-24-g4f1b From ffdc392e2777ff8326b3fca6087d71ae96816e1b Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Wed, 12 Jan 2011 09:05:20 -0500 Subject: Removed trailing slash from $this->uri->ruri_string(). Fixes #292 --- system/core/URI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/core/URI.php') diff --git a/system/core/URI.php b/system/core/URI.php index 479a225fb..769dacd09 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -642,7 +642,7 @@ class CI_URI { */ function ruri_string() { - return '/'.implode('/', $this->rsegment_array()).'/'; + return '/'.implode('/', $this->rsegment_array()); } } -- cgit v1.2.3-24-g4f1b From fea45ad97f9f32738ed7ccc25bdb0405b6f6572f Mon Sep 17 00:00:00 2001 From: Dan Horrigan Date: Wed, 19 Jan 2011 00:54:12 -0500 Subject: Updated the auto URI detection so that it works in more scenarios. Fixes #3 --- system/core/URI.php | 110 +++++++++++++++------------------------------------- 1 file changed, 32 insertions(+), 78 deletions(-) (limited to 'system/core/URI.php') diff --git a/system/core/URI.php b/system/core/URI.php index 769dacd09..9d28d89cd 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -61,17 +61,17 @@ class CI_URI { { if (strtoupper($this->config->item('uri_protocol')) == 'AUTO') { - // Let's try the REQUEST_URI first, this will work in most situations - if ($uri = $this->_get_request_uri()) + // Arguments exist, it must be a command line request + if ( ! empty($_SERVER['argv'])) { - $this->uri_string = $this->_parse_request_uri($uri); + $this->uri_string = $this->_parse_cli_args(); return; } - // Arguments exist, it must be a command line request - if ( ! empty($_SERVER['argv'])) + // Let's try the REQUEST_URI first, this will work in most situations + if ($uri = $this->_detect_uri()) { - $this->uri_string = $this->_parse_cli_args(); + $this->uri_string = $uri; return; } @@ -108,7 +108,7 @@ class CI_URI { if ($uri == 'REQUEST_URI') { - $this->uri_string = $this->_parse_request_uri($this->_get_request_uri()); + $this->uri_string = $this->_detect_uri(); return; } elseif ($uri == 'CLI') @@ -130,99 +130,53 @@ class CI_URI { // -------------------------------------------------------------------- /** - * Get REQUEST_URI + * Detects the URI * - * Retrieves the REQUEST_URI, or equivelent for IIS. + * This function will detect the URI automatically and fix the query string + * if necessary. * * @access private * @return string */ - function _get_request_uri() + private function _detect_uri() { - $uri = FALSE; - - // Let's check for standard servers first - if (isset($_SERVER['REQUEST_URI'])) + if ( ! isset($_SERVER['REQUEST_URI'])) { - $uri = $_SERVER['REQUEST_URI']; - if (strpos($uri, $_SERVER['SERVER_NAME']) !== FALSE) - { - $uri = preg_replace('/^\w+:\/\/[^\/]+/', '', $uri); - } + return ''; } - // Now lets check for IIS - elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) + $uri = $_SERVER['REQUEST_URI']; + if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) { - $uri = $_SERVER['HTTP_X_REWRITE_URL']; + $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); } - - // Last ditch effort (for older CGI servers, like IIS 5) - elseif (isset($_SERVER['ORIG_PATH_INFO'])) + elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) { - $uri = $_SERVER['ORIG_PATH_INFO']; - if ( ! empty($_SERVER['QUERY_STRING'])) - { - $uri .= '?' . $_SERVER['QUERY_STRING']; - } + $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); } - return $uri; - } - - // -------------------------------------------------------------------- - - /** - * Parse 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 - * @param string - * @return string - */ - private function _parse_request_uri($uri) - { - // Some server's require URL's like index.php?/whatever If that is the case, - // then we need to add that to our parsing. - $fc_path = ltrim(FCPATH . SELF, '/'); - if (strpos($uri, SELF . '?') !== FALSE) - { - $fc_path .= '?'; - } - - $parsed_uri = explode('/', ltrim($uri, '/')); - - $i = 0; - foreach (explode("/", $fc_path) as $segment) + // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct + // URI is found, and also fixes the QUERY_STRING server var and $_GET array. + if (strncmp($uri, '?/', 2) === 0) { - if (isset($parsed_uri[$i]) && $segment == $parsed_uri[$i]) - { - $i++; - } + $uri = substr($uri, 2); } - - $uri = implode("/", array_slice($parsed_uri, $i)); - - // Let's take off any query string and re-assign $_SERVER['QUERY_STRING'] and $_GET. - // This is only needed on some servers. However, we are forced to use it to accomodate - // them. - if (($qs_pos = strpos($uri, '?')) !== FALSE) + $parts = preg_split('#\?#i', $uri, 2); + $uri = $parts[0]; + if (isset($parts[1])) { - $_SERVER['QUERY_STRING'] = substr($uri, $qs_pos + 1); + $_SERVER['QUERY_STRING'] = $parts[1]; parse_str($_SERVER['QUERY_STRING'], $_GET); - $uri = substr($uri, 0, $qs_pos); } - - // If it is just a / or index.php then just empty it. - if ($uri == '/' OR $uri == SELF) + else { - $uri = ''; + $_SERVER['QUERY_STRING'] = ''; + $_GET = array(); } + $uri = parse_url($uri, PHP_URL_PATH); - return $uri; + // Do some final cleaning of the URI and return it + return str_replace(array('//', '../'), '/', trim($uri, '/')); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From dda07e9efe683248c042307147b6573e104777ad Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Mon, 31 Jan 2011 23:26:25 +0000 Subject: Some servers would trick URI into thinking it was being run in CLI mode, which broke routing. --- system/core/URI.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/core/URI.php') diff --git a/system/core/URI.php b/system/core/URI.php index 999015949..1b479e92a 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -61,8 +61,8 @@ class CI_URI { { if (strtoupper($this->config->item('uri_protocol')) == 'AUTO') { - // Arguments exist, it must be a command line request - if ( ! empty($_SERVER['argv'])) + // Is the request coming from the command line? + if (defined('STDIN')) { $this->uri_string = $this->_parse_cli_args(); return; -- cgit v1.2.3-24-g4f1b From e58199ba6de5622a062536ba03c43700b70716ac Mon Sep 17 00:00:00 2001 From: "ericbarnes@ericbarnes.local" Date: Wed, 2 Feb 2011 22:40:36 -0500 Subject: Fixes #27. When the default controller was used, the _detect_uri() method was returning an incorrect URI, which caused a 404 when a query string was used and no controller specified. via Dan Horrigan --- system/core/URI.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'system/core/URI.php') diff --git a/system/core/URI.php b/system/core/URI.php index 1b479e92a..c43cde005 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -173,6 +173,12 @@ class CI_URI { $_SERVER['QUERY_STRING'] = ''; $_GET = array(); } + + if ($uri == '/' || empty($uri)) + { + return '/'; + } + $uri = parse_url($uri, PHP_URL_PATH); // Do some final cleaning of the URI and return it -- cgit v1.2.3-24-g4f1b