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
32
* List of cached uri segments
37
var $keyval = array();
46
* List of uri segments
51
var $segments = array();
53
* Re-indexed list of uri segments
54
* Starts at 1 instead of 0
59
var $rsegments = array();
64
* Simply globalizes the $RTR object. The front
65
* loads the Router class early on so it's not available
66
* normally as other classes are.
70
function __construct()
72
$this->config =& load_class('Config', 'core');
73
log_message('debug', "URI Class Initialized");
77
// --------------------------------------------------------------------
85
function _fetch_uri_string()
87
if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
89
// Is the request coming from the command line?
90
if (php_sapi_name() == 'cli' or defined('STDIN'))
92
$this->_set_uri_string($this->_parse_cli_args());
96
// Let's try the REQUEST_URI first, this will work in most situations
97
if ($uri = $this->_detect_uri())
99
$this->_set_uri_string($uri);
103
// Is there a PATH_INFO variable?
104
// Note: some servers seem to have trouble with getenv() so we'll test it two ways
105
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
106
if (trim($path, '/') != '' && $path != "/".SELF)
108
$this->_set_uri_string($path);
112
// No PATH_INFO?... What about QUERY_STRING?
113
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
114
if (trim($path, '/') != '')
116
$this->_set_uri_string($path);
120
// As a last ditch effort lets try using the $_GET array
121
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
123
$this->_set_uri_string(key($_GET));
127
// We've exhausted all our options...
128
$this->uri_string = '';
132
$uri = strtoupper($this->config->item('uri_protocol'));
134
if ($uri == 'REQUEST_URI')
136
$this->_set_uri_string($this->_detect_uri());
139
elseif ($uri == 'CLI')
141
$this->_set_uri_string($this->_parse_cli_args());
145
$path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
146
$this->_set_uri_string($path);
149
// --------------------------------------------------------------------
158
function _set_uri_string($str)
160
// Filter out control characters
161
$str = remove_invisible_characters($str, FALSE);
163
// If the URI contains only a slash we'll kill it
164
$this->uri_string = ($str == '/') ? '' : $str;
167
// --------------------------------------------------------------------
172
* This function will detect the URI automatically and fix the query string
178
private function _detect_uri()
180
if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
185
$uri = $_SERVER['REQUEST_URI'];
186
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
188
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
190
elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
192
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
195
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
196
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
197
if (strncmp($uri, '?/', 2) === 0)
199
$uri = substr($uri, 2);
201
$parts = preg_split('#\?#i', $uri, 2);
203
if (isset($parts[1]))
205
$_SERVER['QUERY_STRING'] = $parts[1];
206
parse_str($_SERVER['QUERY_STRING'], $_GET);
210
$_SERVER['QUERY_STRING'] = '';
214
if ($uri == '/' || empty($uri))
219
$uri = parse_url($uri, PHP_URL_PATH);
221
// Do some final cleaning of the URI and return it
222
return str_replace(array('//', '../'), '/', trim($uri, '/'));
225
// --------------------------------------------------------------------
228
* Parse cli arguments
230
* Take each command line argument and assume it is a URI segment.
235
private function _parse_cli_args()
237
$args = array_slice($_SERVER['argv'], 1);
239
return $args ? '/' . implode('/', $args) : '';
242
// --------------------------------------------------------------------
245
* Filter segments for malicious characters
251
function _filter_uri($str)
253
if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
255
// preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
256
// compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
257
if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))
259
show_error('The URI you submitted has disallowed characters.', 400);
263
// Convert programatic characters to entities
264
$bad = array('$', '(', ')', '%28', '%29');
265
$good = array('$', '(', ')', '(', ')');
267
return str_replace($bad, $good, $str);
270
// --------------------------------------------------------------------
273
* Remove the suffix from the URL if needed
278
function _remove_url_suffix()
280
if ($this->config->item('url_suffix') != "")
282
$this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
286
// --------------------------------------------------------------------
289
* Explode the URI Segments. The individual segments will
290
* be stored in the $this->segments array.
295
function _explode_segments()
297
foreach (explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
299
// Filter segments for security
300
$val = trim($this->_filter_uri($val));
304
$this->segments[] = $val;
309
// --------------------------------------------------------------------
313
* This function re-indexes the $this->segment array so that it
314
* starts at 1 rather than 0. Doing so makes it simpler to
315
* use functions like $this->uri->segment(n) since there is
316
* a 1:1 relationship between the segment array and the actual segments.
321
function _reindex_segments()
323
array_unshift($this->segments, NULL);
324
array_unshift($this->rsegments, NULL);
325
unset($this->segments[0]);
326
unset($this->rsegments[0]);
329
// --------------------------------------------------------------------
332
* Fetch a URI Segment
334
* This function returns the URI segment based on the number provided.
341
function segment($n, $no_result = FALSE)
343
return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
346
// --------------------------------------------------------------------
349
* Fetch a URI "routed" Segment
351
* This function returns the re-routed URI segment (assuming routing rules are used)
352
* based on the number provided. If there is no routing this function returns the
353
* same result as $this->segment()
360
function rsegment($n, $no_result = FALSE)
362
return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
365
// --------------------------------------------------------------------
368
* Generate a key value pair from the URI string
370
* This function generates and associative array of URI data starting
371
* at the supplied segment. For example, if this is your URI:
373
* example.com/user/search/name/joe/location/UK/gender/male
375
* You can use this function to generate an array with this prototype:
384
* @param integer the starting segment number
385
* @param array an array of default values
388
function uri_to_assoc($n = 3, $default = array())
390
return $this->_uri_to_assoc($n, $default, 'segment');
393
* Identical to above only it uses the re-routed segment array
396
* @param integer the starting segment number
397
* @param array an array of default values
401
function ruri_to_assoc($n = 3, $default = array())
403
return $this->_uri_to_assoc($n, $default, 'rsegment');
406
// --------------------------------------------------------------------
409
* Generate a key value pair from the URI string or Re-routed URI string
412
* @param integer the starting segment number
413
* @param array an array of default values
414
* @param string which array we should use
417
function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
419
if ($which == 'segment')
421
$total_segments = 'total_segments';
422
$segment_array = 'segment_array';
426
$total_segments = 'total_rsegments';
427
$segment_array = 'rsegment_array';
430
if ( ! is_numeric($n))
435
if (isset($this->keyval[$n]))
437
return $this->keyval[$n];
440
if ($this->$total_segments() < $n)
442
if (count($default) == 0)
448
foreach ($default as $val)
450
$retval[$val] = FALSE;
455
$segments = array_slice($this->$segment_array(), ($n - 1));
460
foreach ($segments as $seg)
464
$retval[$lastval] = $seg;
468
$retval[$seg] = FALSE;
475
if (count($default) > 0)
477
foreach ($default as $val)
479
if ( ! array_key_exists($val, $retval))
481
$retval[$val] = FALSE;
486
// Cache the array for reuse
487
$this->keyval[$n] = $retval;
491
// --------------------------------------------------------------------
494
* Generate a URI string from an associative array
498
* @param array an associative array of key/values
501
function assoc_to_uri($array)
504
foreach ((array)$array as $key => $val)
510
return implode('/', $temp);
513
// --------------------------------------------------------------------
516
* Fetch a URI Segment and add a trailing slash
523
function slash_segment($n, $where = 'trailing')
525
return $this->_slash_segment($n, $where, 'segment');
528
// --------------------------------------------------------------------
531
* Fetch a URI Segment and add a trailing slash
538
function slash_rsegment($n, $where = 'trailing')
540
return $this->_slash_segment($n, $where, 'rsegment');
543
// --------------------------------------------------------------------
546
* Fetch a URI Segment and add a trailing slash - helper function
554
function _slash_segment($n, $where = 'trailing', $which = 'segment')
559
if ($where == 'trailing')
563
elseif ($where == 'leading')
568
return $leading.$this->$which($n).$trailing;
571
// --------------------------------------------------------------------
579
function segment_array()
581
return $this->segments;
584
// --------------------------------------------------------------------
587
* Routed Segment Array
592
function rsegment_array()
594
return $this->rsegments;
597
// --------------------------------------------------------------------
600
* Total number of segments
605
function total_segments()
607
return count($this->segments);
610
// --------------------------------------------------------------------
613
* Total number of routed segments
618
function total_rsegments()
620
return count($this->rsegments);
623
// --------------------------------------------------------------------
626
* Fetch the entire URI string
631
function uri_string()
633
return $this->uri_string;
637
// --------------------------------------------------------------------
640
* Fetch the entire Re-routed URI string
645
function ruri_string()
647
return '/'.implode('/', $this->rsegment_array());
653
/* End of file URI.php */
654
/* Location: ./system/core/URI.php */
b'\\ No newline at end of file'