config =& load_class('Config', 'core'); // If query strings are enabled, we don't need to parse any segments. // However, they don't make sense under CLI. if (is_cli() OR $this->config->item('enable_query_strings') !== TRUE) { $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars'); // If it's a CLI request, ignore the configuration if (is_cli() OR ($protocol = strtoupper($this->config->item('uri_protocol'))) === 'CLI') { $this->_set_uri_string($this->_parse_argv()); } elseif ($protocol === 'AUTO') { // Is there a PATH_INFO variable? This should be the easiest solution. if (isset($_SERVER['PATH_INFO'])) { $this->_set_uri_string($_SERVER['PATH_INFO']); } // No PATH_INFO? Let's try REQUST_URI or QUERY_STRING then elseif (($uri = $this->_parse_request_uri()) !== '' OR ($uri = $this->_parse_query_string()) !== '') { $this->_set_uri_string($uri); } // As a last ditch effor, let's try using the $_GET array elseif (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '') { $this->_set_uri_string(key($_GET)); } } elseif (method_exists($this, ($method = '_parse_'.strtolower($protocol)))) { $this->_set_uri_string($this->$method()); } else { $uri = isset($_SERVER[$protocol]) ? $_SERVER[$protocol] : @getenv($protocol); $this->_set_uri_string($uri); } } log_message('debug', 'URI Class Initialized'); } // -------------------------------------------------------------------- /** * Set URI String * * @param string $str * @return void */ protected function _set_uri_string($str) { // Filter out control characters and trim slashes $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/'); if ($this->uri_string !== '') { // Remove the URL suffix, if present if (($suffix = (string) $this->config->item('url_suffix')) !== '') { $slen = strlen($suffix); if (substr($this->uri_string, -$slen) === $suffix) { $this->uri_string = substr($this->uri_string, 0, -$slen); } } // Populate the 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; } } } } // -------------------------------------------------------------------- /** * Parse REQUEST_URI * * Will parse REQUEST_URI and automatically detect the URI from it, * while fixing the query string if necessary. * * @used-by CI_URI::_fetch_uri_string() * @return string */ protected function _parse_request_uri() { if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) { return ''; } $uri = parse_url($_SERVER['REQUEST_URI']); $query = isset($uri['query']) ? $uri['query'] : ''; $uri = isset($uri['path']) ? rawurldecode($uri['path']) : ''; if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) { $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME'])); } elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) { $uri = (string) 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 // URI is found, and also fixes the QUERY_STRING server var and $_GET array. if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0) { $query = explode('?', $query, 2); $uri = rawurldecode($query[0]); $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : ''; } else { $_SERVER['QUERY_STRING'] = $query; } parse_str($_SERVER['QUERY_STRING'], $_GET); if ($uri === '/' OR $uri === '') { return '/'; } // Do some final cleaning of the URI and return it return $this->_remove_relative_directory($uri); } // -------------------------------------------------------------------- /** * Parse QUERY_STRING * * Will parse QUERY_STRING and automatically detect the URI from it. * * @return string */ protected function _parse_query_string() { $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING'); if (trim($uri, '/') === '') { return ''; } elseif (strncmp($uri, '/', 1) === 0) { $uri = explode('?', $uri, 2); $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : ''; $uri = rawurldecode($uri[0]); } parse_str($_SERVER['QUERY_STRING'], $_GET); return $this->_remove_relative_directory($uri); } // -------------------------------------------------------------------- /** * Parse CLI arguments * * Take each command line argument and assume it is a URI segment. * * @return string */ protected function _parse_argv() { $args = array_slice($_SERVER['argv'], 1); return $args ? implode('/', $args) : ''; } // -------------------------------------------------------------------- /** * Remove relative directory (../) and multi slashes (///) * * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri() * * @param string $url * @return string */ protected function _remove_relative_directory($uri) { $uris = array(); $tok = strtok($uri, '/'); while ($tok !== FALSE) { if (( ! empty($tok) OR $tok === '0') && $tok !== '..') { $uris[] = $tok; } $tok = strtok('/'); } return implode('/', $uris); } // -------------------------------------------------------------------- /** * Filter URI * * Filters segments for malicious characters. * * @param string $str * @return string */ public function filter_uri($str) { if ( ! empty($str) && ! empty($this->_permitted_uri_chars) && ! preg_match('/^['.$this->_permitted_uri_chars.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $str)) { show_error('The URI you submitted has disallowed characters.', 400); } // Convert programatic characters to entities and return return str_replace( array('$', '(', ')', '%28', '%29'), // Bad array('$', '(', ')', '(', ')'), // Good $str ); } // -------------------------------------------------------------------- /** * Fetch URI Segment * * @see CI_URI::$segments * @param int $n Index * @param mixed $no_result What to return if the segment index is not found * @return mixed */ public function segment($n, $no_result = NULL) { return isset($this->segments[$n]) ? $this->segments[$n] : $no_result; } // -------------------------------------------------------------------- /** * Fetch URI "routed" Segment * * Returns the re-routed URI segment (assuming routing rules are used) * based on the index provided. If there is no routing, will return * the same result as CI_URI::segment(). * * @see CI_URI::$rsegments * @see CI_URI::segment() * @param int $n Index * @param mixed $no_result What to return if the segment index is not found * @return mixed */ public function rsegment($n, $no_result = NULL) { return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result; } // -------------------------------------------------------------------- /** * URI to assoc * * Generates an associative array of URI data starting at the supplied * segment index. For example, if this is your URI: * * example.com/user/search/name/joe/location/UK/gender/male * * You can use this method to generate an array with this prototype: * * array ( * name => joe * location => UK * gender => male * ) * * @param int $n Index (default: 3) * @param array $default Default values * @return array */ public function uri_to_assoc($n = 3, $default = array()) { return $this->_uri_to_assoc($n, $default, 'segment'); } // -------------------------------------------------------------------- /** * Routed URI to assoc * * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed * segment array. * * @see CI_URI::uri_to_assoc() * @param int $n Index (default: 3) * @param array $default Default values * @return array */ public function ruri_to_assoc($n = 3, $default = array()) { return $this->_uri_to_assoc($n, $default, 'rsegment'); } // -------------------------------------------------------------------- /** * Internal URI-to-assoc * * Generates a key/value pair from the URI string or re-routed URI string. * * @used-by CI_URI::uri_to_assoc() * @used-by CI_URI::ruri_to_assoc() * @param int $n Index (default: 3) * @param array $default Default values * @param string $which Array name ('segment' or 'rsegment') * @return array */ protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment') { if ( ! is_numeric($n)) { return $default; } if (isset($this->keyval[$which], $this->keyval[$which][$n])) { return $this->keyval[$which][$n]; } $total_segments = "total_{$which}s"; $segment_array = "{$which}_array"; if ($this->$total_segments() < $n) { return (count($default) === 0) ? array() : array_fill_keys($default, NULL); } $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] = NULL; $lastval = $seg; } $i++; } if (count($default) > 0) { foreach ($default as $val) { if ( ! array_key_exists($val, $retval)) { $retval[$val] = NULL; } } } // Cache the array for reuse isset($this->keyval[$which]) OR $this->keyval[$which] = array(); $this->keyval[$which][$n] = $retval; return $retval; } // -------------------------------------------------------------------- /** * Assoc to URI * * Generates a URI string from an associative array. * * @param array $array Input array of key/value pairs * @return string URI string */ public function assoc_to_uri($array) { $temp = array(); foreach ((array) $array as $key => $val) { $temp[] = $key; $temp[] = $val; } return implode('/', $temp); } // -------------------------------------------------------------------- /** * Slash segment * * Fetches an URI segment with a slash. * * @param int $n Index * @param string $where Where to add the slash ('trailing' or 'leading') * @return string */ public function slash_segment($n, $where = 'trailing') { return $this->_slash_segment($n, $where, 'segment'); } // -------------------------------------------------------------------- /** * Slash routed segment * * Fetches an URI routed segment with a slash. * * @param int $n Index * @param string $where Where to add the slash ('trailing' or 'leading') * @return string */ public function slash_rsegment($n, $where = 'trailing') { return $this->_slash_segment($n, $where, 'rsegment'); } // -------------------------------------------------------------------- /** * Internal Slash segment * * Fetches an URI Segment and adds a slash to it. * * @used-by CI_URI::slash_segment() * @used-by CI_URI::slash_rsegment() * * @param int $n Index * @param string $where Where to add the slash ('trailing' or 'leading') * @param string $which Array name ('segment' or 'rsegment') * @return string */ protected 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 * * @return array CI_URI::$segments */ public function segment_array() { return $this->segments; } // -------------------------------------------------------------------- /** * Routed Segment Array * * @return array CI_URI::$rsegments */ public function rsegment_array() { return $this->rsegments; } // -------------------------------------------------------------------- /** * Total number of segments * * @return int */ public function total_segments() { return count($this->segments); } // -------------------------------------------------------------------- /** * Total number of routed segments * * @return int */ public function total_rsegments() { return count($this->rsegments); } // -------------------------------------------------------------------- /** * Fetch URI string * * @return string CI_URI::$uri_string */ public function uri_string() { return $this->uri_string; } // -------------------------------------------------------------------- /** * Fetch Re-routed URI string * * @return string */ public function ruri_string() { global $RTR; return ltrim($RTR->directory, '/').implode('/', $this->rsegments); } } /* End of file URI.php */ /* Location: ./system/core/URI.php */