blob: 40eaaeb6bcfcb9414a093712fd80752937ab3805 [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
Andrey Andreev92ebfb62012-05-17 12:49:24 +030025 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000026 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * URI Class
30 *
31 * Parses URIs and determines routing
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/uri.html
38 */
39class CI_URI {
40
David Behler07b53422011-08-15 00:25:06 +020041 /**
42 * List of cached uri segments
43 *
44 * @var array
David Behler07b53422011-08-15 00:25:06 +020045 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040046 public $keyval = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030047
David Behler07b53422011-08-15 00:25:06 +020048 /**
49 * Current uri string
50 *
51 * @var string
David Behler07b53422011-08-15 00:25:06 +020052 */
Andrey Andreevc123e112012-01-08 00:17:34 +020053 public $uri_string;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030054
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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040060 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061
David Behler07b53422011-08-15 00:25:06 +020062 /**
63 * Re-indexed list of uri segments
64 * Starts at 1 instead of 0
65 *
66 * @var array
David Behler07b53422011-08-15 00:25:06 +020067 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040068 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000069
70 /**
71 * Constructor
72 *
Andrey Andreevc123e112012-01-08 00:17:34 +020073 * Simply globalizes the $RTR object. The front
Derek Allard2067d1a2008-11-13 22:59:24 +000074 * loads the Router class early on so it's not available
75 * normally as other classes are.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030076 *
77 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000078 */
Andrey Andreevc123e112012-01-08 00:17:34 +020079 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
Derek Jones7576a3b2010-03-02 14:00:36 -060081 $this->config =& load_class('Config', 'core');
Andrey Andreevc123e112012-01-08 00:17:34 +020082 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000083 }
84
Derek Allard2067d1a2008-11-13 22:59:24 +000085 // --------------------------------------------------------------------
86
87 /**
88 * Get the URI String
89 *
Andrey Andreevc123e112012-01-08 00:17:34 +020090 * Called by CI_Router
91 *
92 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000093 */
Andrey Andreevc123e112012-01-08 00:17:34 +020094 public function _fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
Andrey Andreevc123e112012-01-08 00:17:34 +020096 if (strtoupper($this->config->item('uri_protocol')) === 'AUTO')
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Phil Sturgeondda07e92011-01-31 23:26:25 +000098 // Is the request coming from the command line?
Stephen2e00c242011-08-28 10:25:40 +020099 if ($this->_is_cli_request())
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400101 $this->_set_uri_string($this->_parse_cli_args());
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000102 return;
103 }
104
Dan Horriganfea45ad2011-01-19 00:54:12 -0500105 // Let's try the REQUEST_URI first, this will work in most situations
106 if ($uri = $this->_detect_uri())
107 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400108 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 return;
110 }
111
112 // Is there a PATH_INFO variable?
113 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300114 $path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
Alex Bilbieed944a32012-06-02 11:07:47 +0100115 if (trim($path, '/') !== '' && $path !== '/'.SELF)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400117 $this->_set_uri_string($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 return;
119 }
120
121 // No PATH_INFO?... What about QUERY_STRING?
Andrey Andreevd4619342012-06-14 02:27:25 +0300122 $path = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
Alex Bilbieed944a32012-06-02 11:07:47 +0100123 if (trim($path, '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400125 $this->_set_uri_string($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 return;
127 }
128
Dan Horrigan65d603e2010-12-15 08:38:30 -0500129 // As a last ditch effort lets try using the $_GET array
Alex Bilbieed944a32012-06-02 11:07:47 +0100130 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400132 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 return;
134 }
135
136 // We've exhausted all our options...
137 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400138 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400140
141 $uri = strtoupper($this->config->item('uri_protocol'));
142
Andrey Andreevc123e112012-01-08 00:17:34 +0200143 if ($uri === 'REQUEST_URI')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400145 $this->_set_uri_string($this->_detect_uri());
146 return;
147 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200148 elseif ($uri === 'CLI')
Pascal Kriete73598e32011-04-05 15:01:05 -0400149 {
150 $this->_set_uri_string($this->_parse_cli_args());
151 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300154 $path = isset($_SERVER[$uri]) ? $_SERVER[$uri] : @getenv($uri);
Pascal Kriete73598e32011-04-05 15:01:05 -0400155 $this->_set_uri_string($path);
156 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400157
Pascal Kriete73598e32011-04-05 15:01:05 -0400158 // --------------------------------------------------------------------
159
160 /**
161 * Set the URI String
162 *
David Behler07b53422011-08-15 00:25:06 +0200163 * @param string
Andrey Andreevc123e112012-01-08 00:17:34 +0200164 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400165 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300166 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400167 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300168 // Filter out control characters and trim slashes
169 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
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 *
Andrey Andreevd4619342012-06-14 02:27:25 +0300177 * This function will detect the URI automatically
178 * and fix the query string 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 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300184 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 return '';
187 }
188
Andrey Andreeva8262ba2012-06-14 03:16:44 +0300189 if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreeva8262ba2012-06-14 03:16:44 +0300191 $uri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
Dan Horriganfea45ad2011-01-19 00:54:12 -0500192 }
Andrey Andreeva8262ba2012-06-14 03:16:44 +0300193 elseif (strpos($_SERVER['REQUEST_URI'], dirname($_SERVER['SCRIPT_NAME'])) === 0)
Dan Horriganfea45ad2011-01-19 00:54:12 -0500194 {
Andrey Andreevfb859792012-06-14 03:32:19 +0300195 $uri = substr($_SERVER['REQUEST_URI'], strlen(dirname($_SERVER['SCRIPT_NAME'])));
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
Andrey Andreevd4619342012-06-14 02:27:25 +0300197 else
198 {
199 $uri = $_SERVER['REQUEST_URI'];
200 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
Dan Horriganfea45ad2011-01-19 00:54:12 -0500202 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
203 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevd4619342012-06-14 02:27:25 +0300204 if (strpos($uri, '?/') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500206 $uri = substr($uri, 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
Andrey Andreevd4619342012-06-14 02:27:25 +0300208
209 $parts = explode('?', $uri, 2);
Dan Horriganfea45ad2011-01-19 00:54:12 -0500210 $uri = $parts[0];
211 if (isset($parts[1]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500213 $_SERVER['QUERY_STRING'] = $parts[1];
Dan Horrigan65d603e2010-12-15 08:38:30 -0500214 parse_str($_SERVER['QUERY_STRING'], $_GET);
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500216 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500218 $_SERVER['QUERY_STRING'] = '';
219 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400221
Alex Bilbieed944a32012-06-02 11:07:47 +0100222 if ($uri === '/' OR empty($uri))
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500223 {
224 return '/';
225 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400226
Andrey Andreevd4619342012-06-14 02:27:25 +0300227 $uri = parse_url('pseudo://hostname/'.$uri, PHP_URL_PATH);
Derek Allard2067d1a2008-11-13 22:59:24 +0000228
Dan Horriganfea45ad2011-01-19 00:54:12 -0500229 // Do some final cleaning of the URI and return it
230 return str_replace(array('//', '../'), '/', trim($uri, '/'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232
233 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300234
Stephen2e00c242011-08-28 10:25:40 +0200235 /**
236 * Is cli Request?
237 *
238 * Duplicate of function from the Input class to test to see if a request was made from the command line
239 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300240 * @return bool
Stephen2e00c242011-08-28 10:25:40 +0200241 */
242 protected function _is_cli_request()
243 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300244 return (php_sapi_name() === 'cli') OR defined('STDIN');
Stephen2e00c242011-08-28 10:25:40 +0200245 }
246
Stephen2e00c242011-08-28 10:25:40 +0200247 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
249 /**
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000250 * Parse cli arguments
251 *
252 * Take each command line argument and assume it is a URI segment.
253 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000254 * @return string
255 */
Paulf7345e42011-08-27 06:51:16 +1200256 protected function _parse_cli_args()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000257 {
258 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300259 return $args ? '/'.implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Filter segments for malicious characters
266 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200267 * Called by CI_Router
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param string
270 * @return string
271 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200272 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Alex Bilbie04d43fe2012-06-02 17:59:11 +0100274 if ($str !== '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000276 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
277 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
Andrey Andreev58ae9712012-06-15 23:44:48 +0300278 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', urldecode($str)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Derek Jones817163a2009-07-11 17:05:58 +0000280 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282 }
283
Andrey Andreevc123e112012-01-08 00:17:34 +0200284 // Convert programatic characters to entities and return
285 return str_replace(
286 array('$', '(', ')', '%28', '%29'), // Bad
287 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
288 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Remove the suffix from the URL if needed
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 _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300302 $suffix = (string) $this->config->item('url_suffix');
303
304 if ($suffix !== '' && ($offset = strrpos($this->uri_string, $suffix)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300306 $this->uri_string = substr_replace($this->uri_string, '', $offset, strlen($suffix));
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Explode the URI Segments. The individual segments will
314 * be stored in the $this->segments array.
315 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200316 * Called by CI_Router
317 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @return void
319 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200320 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200322 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 // Filter segments for security
325 $val = trim($this->_filter_uri($val));
326
Alex Bilbieed944a32012-06-02 11:07:47 +0100327 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 $this->segments[] = $val;
330 }
331 }
332 }
333
334 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 /**
337 * Re-index Segments
338 *
339 * This function re-indexes the $this->segment array so that it
Andrey Andreevc123e112012-01-08 00:17:34 +0200340 * starts at 1 rather than 0. Doing so makes it simpler to
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * use functions like $this->uri->segment(n) since there is
342 * a 1:1 relationship between the segment array and the actual segments.
343 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200344 * Called by CI_Router
345 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @return void
347 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200348 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 array_unshift($this->segments, NULL);
351 array_unshift($this->rsegments, NULL);
352 unset($this->segments[0]);
353 unset($this->rsegments[0]);
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Fetch a URI Segment
360 *
361 * This function returns the URI segment based on the number provided.
362 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300363 * @param int
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100364 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @return string
366 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100367 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300369 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Fetch a URI "routed" Segment
376 *
377 * This function returns the re-routed URI segment (assuming routing rules are used)
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300378 * based on the number provided. If there is no routing this function returns the
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * same result as $this->segment()
380 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300381 * @param int
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100382 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * @return string
384 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100385 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300387 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Generate a key value pair from the URI string
394 *
395 * This function generates and associative array of URI data starting
396 * at the supplied segment. For example, if this is your URI:
397 *
398 * example.com/user/search/name/joe/location/UK/gender/male
399 *
400 * You can use this function to generate an array with this prototype:
401 *
402 * array (
403 * name => joe
404 * location => UK
405 * gender => male
406 * )
407 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300408 * @param int the starting segment number
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @param array an array of default values
410 * @return array
411 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200412 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Barry Mienydd671972010-10-04 16:33:58 +0200414 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300416
Timothy Warren40403d22012-04-19 16:38:50 -0400417 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 /**
420 * Identical to above only it uses the re-routed segment array
421 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300422 * @param int the starting segment number
David Behler07b53422011-08-15 00:25:06 +0200423 * @param array an array of default values
424 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200426 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Barry Mienydd671972010-10-04 16:33:58 +0200428 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Generate a key value pair from the URI string or Re-routed URI string
435 *
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300436 * @param int $n = 3 the starting segment number
437 * @param array $default = array() an array of default values
438 * @param string $which = 'segment' which array we should use
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @return array
440 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200441 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 if ( ! is_numeric($n))
444 {
445 return $default;
446 }
447
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300448 in_array($which, array('segment', 'rsegment'), TRUE) OR $which = 'segment';
449
450 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300452 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454
Andrey Andreevc123e112012-01-08 00:17:34 +0200455 if ($which === 'segment')
456 {
457 $total_segments = 'total_segments';
458 $segment_array = 'segment_array';
459 }
460 else
461 {
462 $total_segments = 'total_rsegments';
463 $segment_array = 'rsegment_array';
464 }
465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 if ($this->$total_segments() < $n)
467 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300468 return (count($default) === 0)
469 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100470 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472
473 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 $i = 0;
475 $lastval = '';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500476 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 foreach ($segments as $seg)
478 {
479 if ($i % 2)
480 {
481 $retval[$lastval] = $seg;
482 }
483 else
484 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100485 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 $lastval = $seg;
487 }
488
489 $i++;
490 }
491
492 if (count($default) > 0)
493 {
494 foreach ($default as $val)
495 {
496 if ( ! array_key_exists($val, $retval))
497 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100498 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500 }
501 }
502
503 // Cache the array for reuse
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300504 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 return $retval;
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Generate a URI string from an associative array
512 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @param array an associative array of key/values
514 * @return array
515 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200516 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100519 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200521 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 $temp[] = $val;
523 }
524
525 return implode('/', $temp);
526 }
527
528 // --------------------------------------------------------------------
529
530 /**
531 * Fetch a URI Segment and add a trailing slash
532 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300533 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * @param string
535 * @return string
536 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200537 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 return $this->_slash_segment($n, $where, 'segment');
540 }
541
542 // --------------------------------------------------------------------
543
544 /**
545 * Fetch a URI Segment and add a trailing slash
546 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300547 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @param string
549 * @return string
550 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200551 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
553 return $this->_slash_segment($n, $where, 'rsegment');
554 }
555
556 // --------------------------------------------------------------------
557
558 /**
559 * Fetch a URI Segment and add a trailing slash - helper function
560 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300561 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @param string
563 * @param string
564 * @return string
565 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200566 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200568 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000569
Andrey Andreevc123e112012-01-08 00:17:34 +0200570 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 $leading = '';
573 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200574 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 $trailing = '';
577 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 return $leading.$this->$which($n).$trailing;
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Segment Array
586 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @return array
588 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200589 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 return $this->segments;
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
597 * Routed Segment Array
598 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 * @return array
600 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200601 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
603 return $this->rsegments;
604 }
605
606 // --------------------------------------------------------------------
607
608 /**
609 * Total number of segments
610 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300611 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200613 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 return count($this->segments);
616 }
617
618 // --------------------------------------------------------------------
619
620 /**
621 * Total number of routed segments
622 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300623 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200625 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
627 return count($this->rsegments);
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Fetch the entire URI string
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @return string
636 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200637 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 return $this->uri_string;
640 }
641
642
643 // --------------------------------------------------------------------
644
645 /**
646 * Fetch the entire Re-routed URI string
647 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 * @return string
649 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200650 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100652 return implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
654
655}
Derek Allard2067d1a2008-11-13 22:59:24 +0000656
657/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300658/* Location: ./system/core/URI.php */