blob: 93105b1fdb04503a187aa2c7e084a13346429d51 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * URI Class
32 *
33 * Parses URIs and determines routing
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/uri.html
40 */
41class CI_URI {
42
David Behler07b53422011-08-15 00:25:06 +020043 /**
44 * List of cached uri segments
45 *
46 * @var array
David Behler07b53422011-08-15 00:25:06 +020047 */
Andrey Andreevc123e112012-01-08 00:17:34 +020048 public $keyval = array();
David Behler07b53422011-08-15 00:25:06 +020049 /**
50 * Current uri string
51 *
52 * @var string
David Behler07b53422011-08-15 00:25:06 +020053 */
Andrey Andreevc123e112012-01-08 00:17:34 +020054 public $uri_string;
David Behler07b53422011-08-15 00:25:06 +020055 /**
56 * List of uri segments
57 *
58 * @var array
David Behler07b53422011-08-15 00:25:06 +020059 */
Andrey Andreevc123e112012-01-08 00:17:34 +020060 public $segments = array();
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 */
Andrey Andreevc123e112012-01-08 00:17:34 +020067 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?
Andrey Andreevc123e112012-01-08 00:17:34 +020096 if (php_sapi_name() === 'cli' OR defined('STDIN'))
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
Andrey Andreevc123e112012-01-08 00:17:34 +0200189 if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500191 $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
192 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200193 elseif (strpos($_SERVER['REQUEST_URI'], dirname($_SERVER['SCRIPT_NAME'])) === 0)
Dan Horriganfea45ad2011-01-19 00:54:12 -0500194 {
195 $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
Dan Horriganfea45ad2011-01-19 00:54:12 -0500198 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
199 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
200 if (strncmp($uri, '?/', 2) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500202 $uri = substr($uri, 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500204 $parts = preg_split('#\?#i', $uri, 2);
205 $uri = $parts[0];
206 if (isset($parts[1]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500208 $_SERVER['QUERY_STRING'] = $parts[1];
Dan Horrigan65d603e2010-12-15 08:38:30 -0500209 parse_str($_SERVER['QUERY_STRING'], $_GET);
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500211 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500213 $_SERVER['QUERY_STRING'] = '';
214 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400216
Andrey Andreevc123e112012-01-08 00:17:34 +0200217 if ($uri == '/' OR empty($uri))
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500218 {
219 return '/';
220 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400221
Dan Horriganfea45ad2011-01-19 00:54:12 -0500222 $uri = parse_url($uri, PHP_URL_PATH);
Derek Allard2067d1a2008-11-13 22:59:24 +0000223
Dan Horriganfea45ad2011-01-19 00:54:12 -0500224 // Do some final cleaning of the URI and return it
225 return str_replace(array('//', '../'), '/', trim($uri, '/'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227
228 // --------------------------------------------------------------------
229
230 /**
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000231 * Parse cli arguments
232 *
233 * Take each command line argument and assume it is a URI segment.
234 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000235 * @return string
236 */
Paulf7345e42011-08-27 06:51:16 +1200237 protected function _parse_cli_args()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000238 {
239 $args = array_slice($_SERVER['argv'], 1);
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000240 return $args ? '/' . implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Filter segments for malicious characters
247 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200248 * Called by CI_Router
249 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * @param string
251 * @return string
252 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200253 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
256 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000257 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
258 // 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 +0200259 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Derek Jones817163a2009-07-11 17:05:58 +0000261 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263 }
264
Andrey Andreevc123e112012-01-08 00:17:34 +0200265 // Convert programatic characters to entities and return
266 return str_replace(
267 array('$', '(', ')', '%28', '%29'), // Bad
268 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
269 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Remove the suffix from the URL if needed
276 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200277 * Called by CI_Router
278 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * @return void
280 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200281 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200283 if ($this->config->item('url_suffix') != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200285 $this->uri_string = preg_replace('|'.preg_quote($this->config->item('url_suffix')).'$|', '', $this->uri_string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Explode the URI Segments. The individual segments will
293 * be stored in the $this->segments array.
294 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200295 * Called by CI_Router
296 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * @return void
298 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200299 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200301 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 // Filter segments for security
304 $val = trim($this->_filter_uri($val));
305
306 if ($val != '')
307 {
308 $this->segments[] = $val;
309 }
310 }
311 }
312
313 // --------------------------------------------------------------------
314 /**
315 * Re-index Segments
316 *
317 * This function re-indexes the $this->segment array so that it
Andrey Andreevc123e112012-01-08 00:17:34 +0200318 * starts at 1 rather than 0. Doing so makes it simpler to
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * use functions like $this->uri->segment(n) since there is
320 * a 1:1 relationship between the segment array and the actual segments.
321 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200322 * Called by CI_Router
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @return void
325 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200326 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 array_unshift($this->segments, NULL);
329 array_unshift($this->rsegments, NULL);
330 unset($this->segments[0]);
331 unset($this->rsegments[0]);
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Fetch a URI Segment
338 *
339 * This function returns the URI segment based on the number provided.
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @param integer
342 * @param bool
343 * @return string
344 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200345 public function segment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Fetch a URI "routed" Segment
354 *
355 * This function returns the re-routed URI segment (assuming routing rules are used)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500356 * based on the number provided. If there is no routing this function returns the
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * same result as $this->segment()
358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @param integer
360 * @param bool
361 * @return string
362 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200363 public function rsegment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
365 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Generate a key value pair from the URI string
372 *
373 * This function generates and associative array of URI data starting
374 * at the supplied segment. For example, if this is your URI:
375 *
376 * example.com/user/search/name/joe/location/UK/gender/male
377 *
378 * You can use this function to generate an array with this prototype:
379 *
380 * array (
381 * name => joe
382 * location => UK
383 * gender => male
384 * )
385 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @param integer the starting segment number
387 * @param array an array of default values
388 * @return array
389 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200390 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Barry Mienydd671972010-10-04 16:33:58 +0200392 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394 /**
395 * Identical to above only it uses the re-routed segment array
396 *
David Behler07b53422011-08-15 00:25:06 +0200397 * @param integer the starting segment number
398 * @param array an array of default values
399 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200401 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Barry Mienydd671972010-10-04 16:33:58 +0200403 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Generate a key value pair from the URI string or Re-routed URI string
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param integer the starting segment number
412 * @param array an array of default values
413 * @param string which array we should use
414 * @return array
415 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200416 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 if ( ! is_numeric($n))
419 {
420 return $default;
421 }
422
423 if (isset($this->keyval[$n]))
424 {
425 return $this->keyval[$n];
426 }
427
Andrey Andreevc123e112012-01-08 00:17:34 +0200428 if ($which === 'segment')
429 {
430 $total_segments = 'total_segments';
431 $segment_array = 'segment_array';
432 }
433 else
434 {
435 $total_segments = 'total_rsegments';
436 $segment_array = 'rsegment_array';
437 }
438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 if ($this->$total_segments() < $n)
440 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200441 if (count($default) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 return array();
444 }
445
Andrey Andreevc123e112012-01-08 00:17:34 +0200446 return function_exists('array_fill_keys')
447 ? array_fill_keys($default, FALSE)
448 : array_combine($default, array_fill(0, count($default), FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450
451 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 $i = 0;
453 $lastval = '';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500454 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 foreach ($segments as $seg)
456 {
457 if ($i % 2)
458 {
459 $retval[$lastval] = $seg;
460 }
461 else
462 {
463 $retval[$seg] = FALSE;
464 $lastval = $seg;
465 }
466
467 $i++;
468 }
469
470 if (count($default) > 0)
471 {
472 foreach ($default as $val)
473 {
474 if ( ! array_key_exists($val, $retval))
475 {
476 $retval[$val] = FALSE;
477 }
478 }
479 }
480
481 // Cache the array for reuse
482 $this->keyval[$n] = $retval;
483 return $retval;
484 }
485
486 // --------------------------------------------------------------------
487
488 /**
489 * Generate a URI string from an associative array
490 *
491 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 * @param array an associative array of key/values
493 * @return array
494 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200495 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
497 $temp = array();
498 foreach ((array)$array as $key => $val)
499 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200500 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 $temp[] = $val;
502 }
503
504 return implode('/', $temp);
505 }
506
507 // --------------------------------------------------------------------
508
509 /**
510 * Fetch a URI Segment and add a trailing slash
511 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @param integer
513 * @param string
514 * @return string
515 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200516 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 return $this->_slash_segment($n, $where, 'segment');
519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Fetch a URI Segment and add a trailing slash
525 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 * @param integer
527 * @param string
528 * @return string
529 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200530 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
532 return $this->_slash_segment($n, $where, 'rsegment');
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
538 * Fetch a URI Segment and add a trailing slash - helper function
539 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @param integer
541 * @param string
542 * @param string
543 * @return string
544 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200545 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200547 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000548
Andrey Andreevc123e112012-01-08 00:17:34 +0200549 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 $leading = '';
552 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200553 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 $trailing = '';
556 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 return $leading.$this->$which($n).$trailing;
559 }
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Segment Array
565 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @return array
567 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200568 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
570 return $this->segments;
571 }
572
573 // --------------------------------------------------------------------
574
575 /**
576 * Routed Segment Array
577 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * @return array
579 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200580 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 return $this->rsegments;
583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Total number of segments
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @return integer
591 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200592 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 return count($this->segments);
595 }
596
597 // --------------------------------------------------------------------
598
599 /**
600 * Total number of routed segments
601 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 * @return integer
603 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200604 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 return count($this->rsegments);
607 }
608
609 // --------------------------------------------------------------------
610
611 /**
612 * Fetch the entire URI string
613 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @return string
615 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200616 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 return $this->uri_string;
619 }
620
621
622 // --------------------------------------------------------------------
623
624 /**
625 * Fetch the entire Re-routed URI string
626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @return string
628 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200629 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Eric Barnesffdc3922011-01-12 09:05:20 -0500631 return '/'.implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
633
634}
Derek Allard2067d1a2008-11-13 22:59:24 +0000635
636/* End of file URI.php */
Andrey Andreevc123e112012-01-08 00:17:34 +0200637/* Location: ./system/core/URI.php */