blob: 705575a0ceeb6d5b4d91671b753fc833c7a97555 [file] [log] [blame]
Andrey Andreevc123e112012-01-08 00:17:34 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevc123e112012-01-08 00:17:34 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevc123e112012-01-08 00:17:34 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000025 */
26
Derek Allard2067d1a2008-11-13 22:59:24 +000027/**
28 * URI Class
29 *
30 * Parses URIs and determines routing
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/uri.html
37 */
38class CI_URI {
39
David Behler07b53422011-08-15 00:25:06 +020040 /**
41 * List of cached uri segments
42 *
43 * @var array
David Behler07b53422011-08-15 00:25:06 +020044 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040045 public $keyval = array();
Timothy Warren40403d22012-04-19 16:38:50 -040046
David Behler07b53422011-08-15 00:25:06 +020047 /**
48 * Current uri string
49 *
50 * @var string
David Behler07b53422011-08-15 00:25:06 +020051 */
Andrey Andreevc123e112012-01-08 00:17:34 +020052 public $uri_string;
Timothy Warren40403d22012-04-19 16:38:50 -040053
David Behler07b53422011-08-15 00:25:06 +020054 /**
55 * List of uri segments
56 *
57 * @var array
David Behler07b53422011-08-15 00:25:06 +020058 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040059 public $segments = array();
Timothy Warren40403d22012-04-19 16:38:50 -040060
David Behler07b53422011-08-15 00:25:06 +020061 /**
62 * Re-indexed list of uri segments
63 * Starts at 1 instead of 0
64 *
65 * @var array
David Behler07b53422011-08-15 00:25:06 +020066 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040067 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000068
69 /**
70 * Constructor
71 *
Andrey Andreevc123e112012-01-08 00:17:34 +020072 * Simply globalizes the $RTR object. The front
Derek Allard2067d1a2008-11-13 22:59:24 +000073 * loads the Router class early on so it's not available
74 * normally as other classes are.
Derek Allard2067d1a2008-11-13 22:59:24 +000075 */
Andrey Andreevc123e112012-01-08 00:17:34 +020076 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000077 {
Derek Jones7576a3b2010-03-02 14:00:36 -060078 $this->config =& load_class('Config', 'core');
Andrey Andreevc123e112012-01-08 00:17:34 +020079 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000080 }
81
Derek Allard2067d1a2008-11-13 22:59:24 +000082 // --------------------------------------------------------------------
83
84 /**
85 * Get the URI String
86 *
Andrey Andreevc123e112012-01-08 00:17:34 +020087 * Called by CI_Router
88 *
89 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000090 */
Andrey Andreevc123e112012-01-08 00:17:34 +020091 public function _fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Andrey Andreevc123e112012-01-08 00:17:34 +020093 if (strtoupper($this->config->item('uri_protocol')) === 'AUTO')
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Phil Sturgeondda07e92011-01-31 23:26:25 +000095 // Is the request coming from the command line?
Stephen2e00c242011-08-28 10:25:40 +020096 if ($this->_is_cli_request())
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Pascal Kriete73598e32011-04-05 15:01:05 -040098 $this->_set_uri_string($this->_parse_cli_args());
Phil Sturgeon48c718c2010-12-30 23:40:02 +000099 return;
100 }
101
Dan Horriganfea45ad2011-01-19 00:54:12 -0500102 // Let's try the REQUEST_URI first, this will work in most situations
103 if ($uri = $this->_detect_uri())
104 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400105 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 return;
107 }
108
109 // Is there a PATH_INFO variable?
110 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
111 $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
Andrey Andreevc123e112012-01-08 00:17:34 +0200112 if (trim($path, '/') != '' && $path !== '/'.SELF)
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400114 $this->_set_uri_string($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return;
116 }
117
118 // No PATH_INFO?... What about QUERY_STRING?
Andrey Andreevc123e112012-01-08 00:17:34 +0200119 $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 if (trim($path, '/') != '')
121 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400122 $this->_set_uri_string($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 return;
124 }
125
Dan Horrigan65d603e2010-12-15 08:38:30 -0500126 // As a last ditch effort lets try using the $_GET array
Andrey Andreevc123e112012-01-08 00:17:34 +0200127 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400129 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return;
131 }
132
133 // We've exhausted all our options...
134 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400135 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400137
138 $uri = strtoupper($this->config->item('uri_protocol'));
139
Andrey Andreevc123e112012-01-08 00:17:34 +0200140 if ($uri === 'REQUEST_URI')
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400142 $this->_set_uri_string($this->_detect_uri());
143 return;
144 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200145 elseif ($uri === 'CLI')
Pascal Kriete73598e32011-04-05 15:01:05 -0400146 {
147 $this->_set_uri_string($this->_parse_cli_args());
148 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
Pascal Kriete73598e32011-04-05 15:01:05 -0400151 $path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
152 $this->_set_uri_string($path);
153 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400154
Pascal Kriete73598e32011-04-05 15:01:05 -0400155 // --------------------------------------------------------------------
156
157 /**
158 * Set the URI String
159 *
David Behler07b53422011-08-15 00:25:06 +0200160 * @param string
Andrey Andreevc123e112012-01-08 00:17:34 +0200161 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400162 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200163 public function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400164 {
165 // Filter out control characters
166 $str = remove_invisible_characters($str, FALSE);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // If the URI contains only a slash we'll kill it
Andrey Andreevc123e112012-01-08 00:17:34 +0200169 $this->uri_string = ($str === '/') ? '' : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
Dan Horriganfea45ad2011-01-19 00:54:12 -0500175 * Detects the URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 *
Dan Horriganfea45ad2011-01-19 00:54:12 -0500177 * This function will detect the URI automatically and fix the query string
178 * if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @return string
181 */
Paulf7345e42011-08-27 06:51:16 +1200182 protected function _detect_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Eric Barnes26eebdd2011-04-17 23:45:41 -0400184 if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 return '';
187 }
188
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100189 $uri = $_SERVER['REQUEST_URI'];
190 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500192 $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
193 }
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100194 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
Dan Horriganfea45ad2011-01-19 00:54:12 -0500195 {
196 $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
Dan Horriganfea45ad2011-01-19 00:54:12 -0500199 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
200 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
201 if (strncmp($uri, '?/', 2) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500203 $uri = substr($uri, 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500205 $parts = preg_split('#\?#i', $uri, 2);
206 $uri = $parts[0];
207 if (isset($parts[1]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500209 $_SERVER['QUERY_STRING'] = $parts[1];
Dan Horrigan65d603e2010-12-15 08:38:30 -0500210 parse_str($_SERVER['QUERY_STRING'], $_GET);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500212 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500214 $_SERVER['QUERY_STRING'] = '';
215 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400217
Andrey Andreevc123e112012-01-08 00:17:34 +0200218 if ($uri == '/' OR empty($uri))
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500219 {
220 return '/';
221 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400222
Dan Horriganfea45ad2011-01-19 00:54:12 -0500223 $uri = parse_url($uri, PHP_URL_PATH);
Derek Allard2067d1a2008-11-13 22:59:24 +0000224
Dan Horriganfea45ad2011-01-19 00:54:12 -0500225 // Do some final cleaning of the URI and return it
226 return str_replace(array('//', '../'), '/', trim($uri, '/'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228
229 // --------------------------------------------------------------------
Stephen2e00c242011-08-28 10:25:40 +0200230
231 /**
232 * Is cli Request?
233 *
234 * Duplicate of function from the Input class to test to see if a request was made from the command line
235 *
236 * @return boolean
237 */
238 protected function _is_cli_request()
239 {
240 return (php_sapi_name() == 'cli') OR defined('STDIN');
241 }
242
243
244 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000245
246 /**
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000247 * Parse cli arguments
248 *
249 * Take each command line argument and assume it is a URI segment.
250 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000251 * @return string
252 */
Paulf7345e42011-08-27 06:51:16 +1200253 protected function _parse_cli_args()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000254 {
255 $args = array_slice($_SERVER['argv'], 1);
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000256 return $args ? '/' . implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Filter segments for malicious characters
263 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200264 * Called by CI_Router
265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param string
267 * @return string
268 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200269 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
272 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000273 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
274 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
Andrey Andreevc123e112012-01-08 00:17:34 +0200275 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Derek Jones817163a2009-07-11 17:05:58 +0000277 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279 }
280
Andrey Andreevc123e112012-01-08 00:17:34 +0200281 // Convert programatic characters to entities and return
282 return str_replace(
283 array('$', '(', ')', '%28', '%29'), // Bad
284 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
285 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * Remove the suffix from the URL if needed
292 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200293 * Called by CI_Router
294 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @return void
296 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200297 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200299 if ($this->config->item('url_suffix') != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200301 $this->uri_string = preg_replace('|'.preg_quote($this->config->item('url_suffix')).'$|', '', $this->uri_string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Explode the URI Segments. The individual segments will
309 * be stored in the $this->segments array.
310 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200311 * Called by CI_Router
312 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * @return void
314 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200315 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200317 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 // Filter segments for security
320 $val = trim($this->_filter_uri($val));
321
322 if ($val != '')
323 {
324 $this->segments[] = $val;
325 }
326 }
327 }
328
329 // --------------------------------------------------------------------
Timothy Warren40403d22012-04-19 16:38:50 -0400330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 /**
332 * Re-index Segments
333 *
334 * This function re-indexes the $this->segment array so that it
Andrey Andreevc123e112012-01-08 00:17:34 +0200335 * starts at 1 rather than 0. Doing so makes it simpler to
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * use functions like $this->uri->segment(n) since there is
337 * a 1:1 relationship between the segment array and the actual segments.
338 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200339 * Called by CI_Router
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @return void
342 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200343 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 array_unshift($this->segments, NULL);
346 array_unshift($this->rsegments, NULL);
347 unset($this->segments[0]);
348 unset($this->rsegments[0]);
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Fetch a URI Segment
355 *
356 * This function returns the URI segment based on the number provided.
357 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * @param integer
359 * @param bool
360 * @return string
361 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200362 public function segment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Fetch a URI "routed" Segment
371 *
372 * This function returns the re-routed URI segment (assuming routing rules are used)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500373 * based on the number provided. If there is no routing this function returns the
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * same result as $this->segment()
375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @param integer
377 * @param bool
378 * @return string
379 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200380 public function rsegment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Generate a key value pair from the URI string
389 *
390 * This function generates and associative array of URI data starting
391 * at the supplied segment. For example, if this is your URI:
392 *
393 * example.com/user/search/name/joe/location/UK/gender/male
394 *
395 * You can use this function to generate an array with this prototype:
396 *
397 * array (
398 * name => joe
399 * location => UK
400 * gender => male
401 * )
402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @param integer the starting segment number
404 * @param array an array of default values
405 * @return array
406 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200407 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Barry Mienydd671972010-10-04 16:33:58 +0200409 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Timothy Warren40403d22012-04-19 16:38:50 -0400411
412 // --------------------------------------------------------------------
413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 /**
415 * Identical to above only it uses the re-routed segment array
416 *
David Behler07b53422011-08-15 00:25:06 +0200417 * @param integer the starting segment number
418 * @param array an array of default values
419 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200421 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Barry Mienydd671972010-10-04 16:33:58 +0200423 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
425
426 // --------------------------------------------------------------------
427
428 /**
429 * Generate a key value pair from the URI string or Re-routed URI string
430 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @param integer the starting segment number
432 * @param array an array of default values
433 * @param string which array we should use
434 * @return array
435 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200436 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 if ( ! is_numeric($n))
439 {
440 return $default;
441 }
442
443 if (isset($this->keyval[$n]))
444 {
445 return $this->keyval[$n];
446 }
447
Andrey Andreevc123e112012-01-08 00:17:34 +0200448 if ($which === 'segment')
449 {
450 $total_segments = 'total_segments';
451 $segment_array = 'segment_array';
452 }
453 else
454 {
455 $total_segments = 'total_rsegments';
456 $segment_array = 'rsegment_array';
457 }
458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 if ($this->$total_segments() < $n)
460 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200461 if (count($default) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 return array();
464 }
465
Andrey Andreev1ae65162012-03-10 16:11:34 +0200466 return array_fill_keys($default, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468
469 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 $i = 0;
471 $lastval = '';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500472 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 foreach ($segments as $seg)
474 {
475 if ($i % 2)
476 {
477 $retval[$lastval] = $seg;
478 }
479 else
480 {
481 $retval[$seg] = FALSE;
482 $lastval = $seg;
483 }
484
485 $i++;
486 }
487
488 if (count($default) > 0)
489 {
490 foreach ($default as $val)
491 {
492 if ( ! array_key_exists($val, $retval))
493 {
494 $retval[$val] = FALSE;
495 }
496 }
497 }
498
499 // Cache the array for reuse
500 $this->keyval[$n] = $retval;
501 return $retval;
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Generate a URI string from an associative array
508 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * @param array an associative array of key/values
510 * @return array
511 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200512 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 $temp = array();
515 foreach ((array)$array as $key => $val)
516 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200517 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 $temp[] = $val;
519 }
520
521 return implode('/', $temp);
522 }
523
524 // --------------------------------------------------------------------
525
526 /**
527 * Fetch a URI Segment and add a trailing slash
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param integer
530 * @param string
531 * @return string
532 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200533 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
535 return $this->_slash_segment($n, $where, 'segment');
536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Fetch a URI Segment and add a trailing slash
542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @param integer
544 * @param string
545 * @return string
546 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200547 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
549 return $this->_slash_segment($n, $where, 'rsegment');
550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Fetch a URI Segment and add a trailing slash - helper function
556 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @param integer
558 * @param string
559 * @param string
560 * @return string
561 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200562 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200564 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000565
Andrey Andreevc123e112012-01-08 00:17:34 +0200566 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 $leading = '';
569 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200570 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 $trailing = '';
573 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 return $leading.$this->$which($n).$trailing;
576 }
577
578 // --------------------------------------------------------------------
579
580 /**
581 * Segment Array
582 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @return array
584 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200585 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 return $this->segments;
588 }
589
590 // --------------------------------------------------------------------
591
592 /**
593 * Routed Segment Array
594 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 * @return array
596 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200597 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 return $this->rsegments;
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Total number of segments
606 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 * @return integer
608 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200609 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 return count($this->segments);
612 }
613
614 // --------------------------------------------------------------------
615
616 /**
617 * Total number of routed segments
618 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @return integer
620 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200621 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 return count($this->rsegments);
624 }
625
626 // --------------------------------------------------------------------
627
628 /**
629 * Fetch the entire URI string
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @return string
632 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200633 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 return $this->uri_string;
636 }
637
638
639 // --------------------------------------------------------------------
640
641 /**
642 * Fetch the entire Re-routed URI string
643 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @return string
645 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200646 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
Phil Sturgeoncdfbd522012-04-26 22:13:31 +0100648 return implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
650
651}
Derek Allard2067d1a2008-11-13 22:59:24 +0000652
653/* End of file URI.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400654/* Location: ./system/core/URI.php */