1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, 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.
47 $this->config =& load_class('Config');
48 log_message('debug', "URI Class Initialized");
52 // --------------------------------------------------------------------
60 function _fetch_uri_string()
62 if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
64 // If the URL has a question mark then it's simplest to just
65 // build the URI string from the zero index of the $_GET array.
66 // This avoids having to deal with $_SERVER variables, which
67 // can be unreliable in some environments
68 if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
70 $this->uri_string = key($_GET);
74 // Is there a PATH_INFO variable?
75 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
76 $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
77 if (trim($path, '/') != '' && $path != "/".SELF)
79 $this->uri_string = $path;
83 // No PATH_INFO?... What about QUERY_STRING?
84 $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
85 if (trim($path, '/') != '')
87 $this->uri_string = $path;
91 // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
92 $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
93 if (trim($path, '/') != '' && $path != "/".SELF)
95 // remove path and script information so we have good URI data
96 $this->uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $path);
100 // We've exhausted all our options...
101 $this->uri_string = '';
105 $uri = strtoupper($this->config->item('uri_protocol'));
107 if ($uri == 'REQUEST_URI')
109 $this->uri_string = $this->_parse_request_uri();
113 $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
116 // If the URI contains only a slash we'll kill it
117 if ($this->uri_string == '/')
119 $this->uri_string = '';
123 // --------------------------------------------------------------------
126 * Parse the REQUEST_URI
128 * Due to the way REQUEST_URI works it usually contains path info
129 * that makes it unusable as URI data. We'll trim off the unnecessary
130 * data, hopefully arriving at a valid URI that we can use.
135 function _parse_request_uri()
137 if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
142 $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
144 if ($request_uri == '' OR $request_uri == SELF)
150 if (strpos($request_uri, '?') !== FALSE)
155 $parsed_uri = explode("/", $request_uri);
158 foreach(explode("/", $fc_path) as $segment)
160 if (isset($parsed_uri[$i]) && $segment == $parsed_uri[$i])
166 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
168 if ($parsed_uri != '')
170 $parsed_uri = '/'.$parsed_uri;
176 // --------------------------------------------------------------------
179 * Filter segments for malicious characters
185 function _filter_uri($str)
187 if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
189 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
191 exit('The URI you submitted has disallowed characters.');
195 // Convert programatic characters to entities
196 $bad = array('$', '(', ')', '%28', '%29');
197 $good = array('$', '(', ')', '(', ')');
199 return str_replace($bad, $good, $str);
202 // --------------------------------------------------------------------
205 * Remove the suffix from the URL if needed
210 function _remove_url_suffix()
212 if ($this->config->item('url_suffix') != "")
214 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
218 // --------------------------------------------------------------------
221 * Explode the URI Segments. The individual segments will
222 * be stored in the $this->segments array.
227 function _explode_segments()
229 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
231 // Filter segments for security
232 $val = trim($this->_filter_uri($val));
236 $this->segments[] = $val;
241 // --------------------------------------------------------------------
245 * This function re-indexes the $this->segment array so that it
246 * starts at 1 rather than 0. Doing so makes it simpler to
247 * use functions like $this->uri->segment(n) since there is
248 * a 1:1 relationship between the segment array and the actual segments.
253 function _reindex_segments()
255 array_unshift($this->segments, NULL);
256 array_unshift($this->rsegments, NULL);
257 unset($this->segments[0]);
258 unset($this->rsegments[0]);
261 // --------------------------------------------------------------------
264 * Fetch a URI Segment
266 * This function returns the URI segment based on the number provided.
273 function segment($n, $no_result = FALSE)
275 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
278 // --------------------------------------------------------------------
281 * Fetch a URI "routed" Segment
283 * This function returns the re-routed URI segment (assuming routing rules are used)
284 * based on the number provided. If there is no routing this function returns the
285 * same result as $this->segment()
292 function rsegment($n, $no_result = FALSE)
294 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
297 // --------------------------------------------------------------------
300 * Generate a key value pair from the URI string
302 * This function generates and associative array of URI data starting
303 * at the supplied segment. For example, if this is your URI:
305 * example.com/user/search/name/joe/location/UK/gender/male
307 * You can use this function to generate an array with this prototype:
316 * @param integer the starting segment number
317 * @param array an array of default values
320 function uri_to_assoc($n = 3, $default = array())
322 return $this->_uri_to_assoc($n, $default, 'segment');
325 * Identical to above only it uses the re-routed segment array
328 function ruri_to_assoc($n = 3, $default = array())
330 return $this->_uri_to_assoc($n, $default, 'rsegment');
333 // --------------------------------------------------------------------
336 * Generate a key value pair from the URI string or Re-routed URI string
339 * @param integer the starting segment number
340 * @param array an array of default values
341 * @param string which array we should use
344 function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
346 if ($which == 'segment')
348 $total_segments = 'total_segments';
349 $segment_array = 'segment_array';
353 $total_segments = 'total_rsegments';
354 $segment_array = 'rsegment_array';
357 if ( ! is_numeric($n))
362 if (isset($this->keyval[$n]))
364 return $this->keyval[$n];
367 if ($this->$total_segments() < $n)
369 if (count($default) == 0)
375 foreach ($default as $val)
377 $retval[$val] = FALSE;
382 $segments = array_slice($this->$segment_array(), ($n - 1));
387 foreach ($segments as $seg)
391 $retval[$lastval] = $seg;
395 $retval[$seg] = FALSE;
402 if (count($default) > 0)
404 foreach ($default as $val)
406 if ( ! array_key_exists($val, $retval))
408 $retval[$val] = FALSE;
413 // Cache the array for reuse
414 $this->keyval[$n] = $retval;
418 // --------------------------------------------------------------------
421 * Generate a URI string from an associative array
425 * @param array an associative array of key/values
428 function assoc_to_uri($array)
431 foreach ((array)$array as $key => $val)
437 return implode('/', $temp);
440 // --------------------------------------------------------------------
443 * Fetch a URI Segment and add a trailing slash
450 function slash_segment($n, $where = 'trailing')
452 return $this->_slash_segment($n, $where, 'segment');
455 // --------------------------------------------------------------------
458 * Fetch a URI Segment and add a trailing slash
465 function slash_rsegment($n, $where = 'trailing')
467 return $this->_slash_segment($n, $where, 'rsegment');
470 // --------------------------------------------------------------------
473 * Fetch a URI Segment and add a trailing slash - helper function
481 function _slash_segment($n, $where = 'trailing', $which = 'segment')
483 if ($where == 'trailing')
488 elseif ($where == 'leading')
498 return $leading.$this->$which($n).$trailing;
501 // --------------------------------------------------------------------
509 function segment_array()
511 return $this->segments;
514 // --------------------------------------------------------------------
517 * Routed Segment Array
522 function rsegment_array()
524 return $this->rsegments;
527 // --------------------------------------------------------------------
530 * Total number of segments
535 function total_segments()
537 return count($this->segments);
540 // --------------------------------------------------------------------
543 * Total number of routed segments
548 function total_rsegments()
550 return count($this->rsegments);
553 // --------------------------------------------------------------------
556 * Fetch the entire URI string
561 function uri_string()
563 return $this->uri_string;
567 // --------------------------------------------------------------------
570 * Fetch the entire Re-routed URI string
575 function ruri_string()
577 return '/'.implode('/', $this->rsegment_array()).'/';
583 /* End of file URI.php */
584 /* Location: ./system/libraries/URI.php */