blob: 4a2e87c2a2ea775ddf565ceeee8db9b74fc3eda8 [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
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
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 // --------------------------------------------------------------------
230
231 /**
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000232 * Parse cli arguments
233 *
234 * Take each command line argument and assume it is a URI segment.
235 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000236 * @return string
237 */
Paulf7345e42011-08-27 06:51:16 +1200238 protected function _parse_cli_args()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000239 {
240 $args = array_slice($_SERVER['argv'], 1);
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000241 return $args ? '/' . implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Filter segments for malicious characters
248 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200249 * Called by CI_Router
250 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 * @param string
252 * @return string
253 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200254 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
257 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000258 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
259 // 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 +0200260 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Derek Jones817163a2009-07-11 17:05:58 +0000262 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264 }
265
Andrey Andreevc123e112012-01-08 00:17:34 +0200266 // Convert programatic characters to entities and return
267 return str_replace(
268 array('$', '(', ')', '%28', '%29'), // Bad
269 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
270 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Remove the suffix from the URL if needed
277 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200278 * Called by CI_Router
279 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @return void
281 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200282 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200284 if ($this->config->item('url_suffix') != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200286 $this->uri_string = preg_replace('|'.preg_quote($this->config->item('url_suffix')).'$|', '', $this->uri_string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Explode the URI Segments. The individual segments will
294 * be stored in the $this->segments array.
295 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200296 * Called by CI_Router
297 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * @return void
299 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200300 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200302 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 // Filter segments for security
305 $val = trim($this->_filter_uri($val));
306
307 if ($val != '')
308 {
309 $this->segments[] = $val;
310 }
311 }
312 }
313
314 // --------------------------------------------------------------------
315 /**
316 * Re-index Segments
317 *
318 * This function re-indexes the $this->segment array so that it
Andrey Andreevc123e112012-01-08 00:17:34 +0200319 * starts at 1 rather than 0. Doing so makes it simpler to
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * use functions like $this->uri->segment(n) since there is
321 * a 1:1 relationship between the segment array and the actual segments.
322 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200323 * Called by CI_Router
324 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @return void
326 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200327 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 array_unshift($this->segments, NULL);
330 array_unshift($this->rsegments, NULL);
331 unset($this->segments[0]);
332 unset($this->rsegments[0]);
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * Fetch a URI Segment
339 *
340 * This function returns the URI segment based on the number provided.
341 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @param integer
343 * @param bool
344 * @return string
345 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200346 public function segment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Fetch a URI "routed" Segment
355 *
356 * This function returns the re-routed URI segment (assuming routing rules are used)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500357 * based on the number provided. If there is no routing this function returns the
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * same result as $this->segment()
359 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * @param integer
361 * @param bool
362 * @return string
363 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200364 public function rsegment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Generate a key value pair from the URI string
373 *
374 * This function generates and associative array of URI data starting
375 * at the supplied segment. For example, if this is your URI:
376 *
377 * example.com/user/search/name/joe/location/UK/gender/male
378 *
379 * You can use this function to generate an array with this prototype:
380 *
381 * array (
382 * name => joe
383 * location => UK
384 * gender => male
385 * )
386 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @param integer the starting segment number
388 * @param array an array of default values
389 * @return array
390 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200391 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Barry Mienydd671972010-10-04 16:33:58 +0200393 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
395 /**
396 * Identical to above only it uses the re-routed segment array
397 *
David Behler07b53422011-08-15 00:25:06 +0200398 * @param integer the starting segment number
399 * @param array an array of default values
400 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200402 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Barry Mienydd671972010-10-04 16:33:58 +0200404 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
410 * Generate a key value pair from the URI string or Re-routed URI string
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @param integer the starting segment number
413 * @param array an array of default values
414 * @param string which array we should use
415 * @return array
416 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200417 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 if ( ! is_numeric($n))
420 {
421 return $default;
422 }
423
424 if (isset($this->keyval[$n]))
425 {
426 return $this->keyval[$n];
427 }
428
Andrey Andreevc123e112012-01-08 00:17:34 +0200429 if ($which === 'segment')
430 {
431 $total_segments = 'total_segments';
432 $segment_array = 'segment_array';
433 }
434 else
435 {
436 $total_segments = 'total_rsegments';
437 $segment_array = 'rsegment_array';
438 }
439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 if ($this->$total_segments() < $n)
441 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200442 if (count($default) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 return array();
445 }
446
Andrey Andreev1ae65162012-03-10 16:11:34 +0200447 return array_fill_keys($default, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 $i = 0;
452 $lastval = '';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500453 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 foreach ($segments as $seg)
455 {
456 if ($i % 2)
457 {
458 $retval[$lastval] = $seg;
459 }
460 else
461 {
462 $retval[$seg] = FALSE;
463 $lastval = $seg;
464 }
465
466 $i++;
467 }
468
469 if (count($default) > 0)
470 {
471 foreach ($default as $val)
472 {
473 if ( ! array_key_exists($val, $retval))
474 {
475 $retval[$val] = FALSE;
476 }
477 }
478 }
479
480 // Cache the array for reuse
481 $this->keyval[$n] = $retval;
482 return $retval;
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * Generate a URI string from an associative array
489 *
490 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 * @param array an associative array of key/values
492 * @return array
493 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200494 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 $temp = array();
497 foreach ((array)$array as $key => $val)
498 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200499 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 $temp[] = $val;
501 }
502
503 return implode('/', $temp);
504 }
505
506 // --------------------------------------------------------------------
507
508 /**
509 * Fetch a URI Segment and add a trailing slash
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @param integer
512 * @param string
513 * @return string
514 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200515 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 return $this->_slash_segment($n, $where, 'segment');
518 }
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Fetch a URI Segment and add a trailing slash
524 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * @param integer
526 * @param string
527 * @return string
528 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200529 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
531 return $this->_slash_segment($n, $where, 'rsegment');
532 }
533
534 // --------------------------------------------------------------------
535
536 /**
537 * Fetch a URI Segment and add a trailing slash - helper function
538 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * @param integer
540 * @param string
541 * @param string
542 * @return string
543 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200544 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200546 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000547
Andrey Andreevc123e112012-01-08 00:17:34 +0200548 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 $leading = '';
551 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200552 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 $trailing = '';
555 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 return $leading.$this->$which($n).$trailing;
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Segment Array
564 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @return array
566 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200567 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 return $this->segments;
570 }
571
572 // --------------------------------------------------------------------
573
574 /**
575 * Routed Segment Array
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @return array
578 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200579 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 return $this->rsegments;
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
587 * Total number of segments
588 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @return integer
590 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200591 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 return count($this->segments);
594 }
595
596 // --------------------------------------------------------------------
597
598 /**
599 * Total number of routed segments
600 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 * @return integer
602 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200603 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 return count($this->rsegments);
606 }
607
608 // --------------------------------------------------------------------
609
610 /**
611 * Fetch the entire URI string
612 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @return string
614 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200615 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 return $this->uri_string;
618 }
619
620
621 // --------------------------------------------------------------------
622
623 /**
624 * Fetch the entire Re-routed URI string
625 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @return string
627 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200628 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
Eric Barnesffdc3922011-01-12 09:05:20 -0500630 return '/'.implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 }
632
633}
Derek Allard2067d1a2008-11-13 22:59:24 +0000634
635/* End of file URI.php */
Andrey Andreevc123e112012-01-08 00:17:34 +0200636/* Location: ./system/core/URI.php */