1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * Parses URIs and determines routing
23 * @package CodeIgniter
24 * @subpackage Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/uri.html
31 var $keyval = array();
33 var $segments = array();
34 var $rsegments = array();
39 * Simply globalizes the $RTR object. The front
40 * loads the Router class early on so it's not available
41 * normally as other classes are.
45 function __construct()
47 $this->config =& load_class('Config', 'core');
48 log_message('debug', "URI Class Initialized");
52 // --------------------------------------------------------------------
60 function _fetch_uri_string()
62 if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
64 // Is the request coming from the command line?
67 $this->_set_uri_string($this->_parse_cli_args());
71 // Let's try the REQUEST_URI first, this will work in most situations
72 if ($uri = $this->_detect_uri())
74 $this->_set_uri_string($uri);
78 // Is there a PATH_INFO variable?
79 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
80 $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
81 if (trim($path, '/') != '' && $path != "/".SELF)
83 $this->_set_uri_string($path);
87 // No PATH_INFO?... What about QUERY_STRING?
88 $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
89 if (trim($path, '/') != '')
91 $this->_set_uri_string($path);
95 // As a last ditch effort lets try using the $_GET array
96 if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
98 $this->_set_uri_string(key($_GET));
102 // We've exhausted all our options...
103 $this->uri_string = '';
107 $uri = strtoupper($this->config->item('uri_protocol'));
109 if ($uri == 'REQUEST_URI')
111 $this->_set_uri_string($this->_detect_uri());
114 elseif ($uri == 'CLI')
116 $this->_set_uri_string($this->_parse_cli_args());
120 $path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
121 $this->_set_uri_string($path);
124 // --------------------------------------------------------------------
132 function _set_uri_string($str)
134 // Filter out control characters
135 $str = remove_invisible_characters($str, FALSE);
137 // If the URI contains only a slash we'll kill it
138 $this->uri_string = ($str == '/') ? '' : $str;
141 // --------------------------------------------------------------------
146 * This function will detect the URI automatically and fix the query string
152 private function _detect_uri()
154 if ( ! isset($_SERVER['REQUEST_URI']))
159 $uri = $_SERVER['REQUEST_URI'];
160 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
162 $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
164 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
166 $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
169 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
170 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
171 if (strncmp($uri, '?/', 2) === 0)
173 $uri = substr($uri, 2);
175 $parts = preg_split('#\?#i', $uri, 2);
177 if (isset($parts[1]))
179 $_SERVER['QUERY_STRING'] = $parts[1];
180 parse_str($_SERVER['QUERY_STRING'], $_GET);
184 $_SERVER['QUERY_STRING'] = '';
188 if ($uri == '/' || empty($uri))
193 $uri = parse_url($uri, PHP_URL_PATH);
195 // Do some final cleaning of the URI and return it
196 return str_replace(array('//', '../'), '/', trim($uri, '/'));
199 // --------------------------------------------------------------------
202 * Parse cli arguments
204 * Take each command line argument and assume it is a URI segment.
209 private function _parse_cli_args()
211 $args = array_slice($_SERVER['argv'], 1);
213 return $args ? '/' . implode('/', $args) : '';
216 // --------------------------------------------------------------------
219 * Filter segments for malicious characters
225 function _filter_uri($str)
227 if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
229 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
230 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
231 if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))
233 show_error('The URI you submitted has disallowed characters.', 400);
237 // Convert programatic characters to entities
238 $bad = array('$', '(', ')', '%28', '%29');
239 $good = array('$', '(', ')', '(', ')');
241 return str_replace($bad, $good, $str);
244 // --------------------------------------------------------------------
247 * Remove the suffix from the URL if needed
252 function _remove_url_suffix()
254 if ($this->config->item('url_suffix') != "")
256 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
260 // --------------------------------------------------------------------
263 * Explode the URI Segments. The individual segments will
264 * be stored in the $this->segments array.
269 function _explode_segments()
271 foreach (explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
273 // Filter segments for security
274 $val = trim($this->_filter_uri($val));
278 $this->segments[] = $val;
283 // --------------------------------------------------------------------
287 * This function re-indexes the $this->segment array so that it
288 * starts at 1 rather than 0. Doing so makes it simpler to
289 * use functions like $this->uri->segment(n) since there is
290 * a 1:1 relationship between the segment array and the actual segments.
295 function _reindex_segments()
297 array_unshift($this->segments, NULL);
298 array_unshift($this->rsegments, NULL);
299 unset($this->segments[0]);
300 unset($this->rsegments[0]);
303 // --------------------------------------------------------------------
306 * Fetch a URI Segment
308 * This function returns the URI segment based on the number provided.
315 function segment($n, $no_result = FALSE)
317 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
320 // --------------------------------------------------------------------
323 * Fetch a URI "routed" Segment
325 * This function returns the re-routed URI segment (assuming routing rules are used)
326 * based on the number provided. If there is no routing this function returns the
327 * same result as $this->segment()
334 function rsegment($n, $no_result = FALSE)
336 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
339 // --------------------------------------------------------------------
342 * Generate a key value pair from the URI string
344 * This function generates and associative array of URI data starting
345 * at the supplied segment. For example, if this is your URI:
347 * example.com/user/search/name/joe/location/UK/gender/male
349 * You can use this function to generate an array with this prototype:
358 * @param integer the starting segment number
359 * @param array an array of default values
362 function uri_to_assoc($n = 3, $default = array())
364 return $this->_uri_to_assoc($n, $default, 'segment');
367 * Identical to above only it uses the re-routed segment array
370 function ruri_to_assoc($n = 3, $default = array())
372 return $this->_uri_to_assoc($n, $default, 'rsegment');
375 // --------------------------------------------------------------------
378 * Generate a key value pair from the URI string or Re-routed URI string
381 * @param integer the starting segment number
382 * @param array an array of default values
383 * @param string which array we should use
386 function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
388 if ($which == 'segment')
390 $total_segments = 'total_segments';
391 $segment_array = 'segment_array';
395 $total_segments = 'total_rsegments';
396 $segment_array = 'rsegment_array';
399 if ( ! is_numeric($n))
404 if (isset($this->keyval[$n]))
406 return $this->keyval[$n];
409 if ($this->$total_segments() < $n)
411 if (count($default) == 0)
417 foreach ($default as $val)
419 $retval[$val] = FALSE;
424 $segments = array_slice($this->$segment_array(), ($n - 1));
429 foreach ($segments as $seg)
433 $retval[$lastval] = $seg;
437 $retval[$seg] = FALSE;
444 if (count($default) > 0)
446 foreach ($default as $val)
448 if ( ! array_key_exists($val, $retval))
450 $retval[$val] = FALSE;
455 // Cache the array for reuse
456 $this->keyval[$n] = $retval;
460 // --------------------------------------------------------------------
463 * Generate a URI string from an associative array
467 * @param array an associative array of key/values
470 function assoc_to_uri($array)
473 foreach ((array)$array as $key => $val)
479 return implode('/', $temp);
482 // --------------------------------------------------------------------
485 * Fetch a URI Segment and add a trailing slash
492 function slash_segment($n, $where = 'trailing')
494 return $this->_slash_segment($n, $where, 'segment');
497 // --------------------------------------------------------------------
500 * Fetch a URI Segment and add a trailing slash
507 function slash_rsegment($n, $where = 'trailing')
509 return $this->_slash_segment($n, $where, 'rsegment');
512 // --------------------------------------------------------------------
515 * Fetch a URI Segment and add a trailing slash - helper function
523 function _slash_segment($n, $where = 'trailing', $which = 'segment')
528 if ($where == 'trailing')
532 elseif ($where == 'leading')
537 return $leading.$this->$which($n).$trailing;
540 // --------------------------------------------------------------------
548 function segment_array()
550 return $this->segments;
553 // --------------------------------------------------------------------
556 * Routed Segment Array
561 function rsegment_array()
563 return $this->rsegments;
566 // --------------------------------------------------------------------
569 * Total number of segments
574 function total_segments()
576 return count($this->segments);
579 // --------------------------------------------------------------------
582 * Total number of routed segments
587 function total_rsegments()
589 return count($this->rsegments);
592 // --------------------------------------------------------------------
595 * Fetch the entire URI string
600 function uri_string()
602 return $this->uri_string;
606 // --------------------------------------------------------------------
609 * Fetch the entire Re-routed URI string
614 function ruri_string()
616 return '/'.implode('/', $this->rsegment_array());
622 /* End of file URI.php */
623 /* Location: ./system/core/URI.php */