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