blob: e66cb6dc59b8b76e8a04870ab3e24742c764783a [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
114 $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
Andrey Andreevc123e112012-01-08 00:17:34 +0200115 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 Andreevc123e112012-01-08 00:17:34 +0200122 $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 if (trim($path, '/') != '')
124 {
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
Andrey Andreevc123e112012-01-08 00:17:34 +0200130 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 Andreevc123e112012-01-08 00:17:34 +0200166 public function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400167 {
168 // Filter out control characters
169 $str = remove_invisible_characters($str, FALSE);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // If the URI contains only a slash we'll kill it
Andrey Andreevc123e112012-01-08 00:17:34 +0200172 $this->uri_string = ($str === '/') ? '' : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174
175 // --------------------------------------------------------------------
176
177 /**
Dan Horriganfea45ad2011-01-19 00:54:12 -0500178 * Detects the URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 *
Dan Horriganfea45ad2011-01-19 00:54:12 -0500180 * This function will detect the URI automatically and fix the query string
181 * if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * @return string
184 */
Paulf7345e42011-08-27 06:51:16 +1200185 protected function _detect_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300187 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 return '';
190 }
191
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100192 $uri = $_SERVER['REQUEST_URI'];
193 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500195 $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
196 }
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100197 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
Dan Horriganfea45ad2011-01-19 00:54:12 -0500198 {
199 $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
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.
204 if (strncmp($uri, '?/', 2) === 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 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500208 $parts = preg_split('#\?#i', $uri, 2);
209 $uri = $parts[0];
210 if (isset($parts[1]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500212 $_SERVER['QUERY_STRING'] = $parts[1];
Dan Horrigan65d603e2010-12-15 08:38:30 -0500213 parse_str($_SERVER['QUERY_STRING'], $_GET);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500215 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Dan Horriganfea45ad2011-01-19 00:54:12 -0500217 $_SERVER['QUERY_STRING'] = '';
218 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400220
Andrey Andreevc123e112012-01-08 00:17:34 +0200221 if ($uri == '/' OR empty($uri))
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500222 {
223 return '/';
224 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400225
Dan Horriganfea45ad2011-01-19 00:54:12 -0500226 $uri = parse_url($uri, PHP_URL_PATH);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
Dan Horriganfea45ad2011-01-19 00:54:12 -0500228 // Do some final cleaning of the URI and return it
229 return str_replace(array('//', '../'), '/', trim($uri, '/'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231
232 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300233
Stephen2e00c242011-08-28 10:25:40 +0200234 /**
235 * Is cli Request?
236 *
237 * Duplicate of function from the Input class to test to see if a request was made from the command line
238 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300239 * @return bool
Stephen2e00c242011-08-28 10:25:40 +0200240 */
241 protected function _is_cli_request()
242 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300243 return (php_sapi_name() === 'cli') OR defined('STDIN');
Stephen2e00c242011-08-28 10:25:40 +0200244 }
245
Stephen2e00c242011-08-28 10:25:40 +0200246 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000247
248 /**
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000249 * Parse cli arguments
250 *
251 * Take each command line argument and assume it is a URI segment.
252 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000253 * @return string
254 */
Paulf7345e42011-08-27 06:51:16 +1200255 protected function _parse_cli_args()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000256 {
257 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300258 return $args ? '/'.implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Filter segments for malicious characters
265 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200266 * Called by CI_Router
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string
269 * @return string
270 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200271 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
274 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000275 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
276 // 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 +0200277 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Derek Jones817163a2009-07-11 17:05:58 +0000279 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281 }
282
Andrey Andreevc123e112012-01-08 00:17:34 +0200283 // Convert programatic characters to entities and return
284 return str_replace(
285 array('$', '(', ')', '%28', '%29'), // Bad
286 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
287 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Remove the suffix from the URL if needed
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 _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200301 if ($this->config->item('url_suffix') != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200303 $this->uri_string = preg_replace('|'.preg_quote($this->config->item('url_suffix')).'$|', '', $this->uri_string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Explode the URI Segments. The individual segments will
311 * be stored in the $this->segments array.
312 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200313 * Called by CI_Router
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @return void
316 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200317 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200319 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 // Filter segments for security
322 $val = trim($this->_filter_uri($val));
323
324 if ($val != '')
325 {
326 $this->segments[] = $val;
327 }
328 }
329 }
330
331 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 /**
334 * Re-index Segments
335 *
336 * This function re-indexes the $this->segment array so that it
Andrey Andreevc123e112012-01-08 00:17:34 +0200337 * starts at 1 rather than 0. Doing so makes it simpler to
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * use functions like $this->uri->segment(n) since there is
339 * a 1:1 relationship between the segment array and the actual segments.
340 *
Andrey Andreevc123e112012-01-08 00:17:34 +0200341 * Called by CI_Router
342 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @return void
344 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200345 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 array_unshift($this->segments, NULL);
348 array_unshift($this->rsegments, NULL);
349 unset($this->segments[0]);
350 unset($this->rsegments[0]);
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * Fetch a URI Segment
357 *
358 * This function returns the URI segment based on the number provided.
359 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300360 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @param bool
362 * @return string
363 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200364 public function segment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300366 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Fetch a URI "routed" Segment
373 *
374 * This function returns the re-routed URI segment (assuming routing rules are used)
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300375 * based on the number provided. If there is no routing this function returns the
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * same result as $this->segment()
377 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300378 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * @param bool
380 * @return string
381 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200382 public function rsegment($n, $no_result = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300384 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * Generate a key value pair from the URI string
391 *
392 * This function generates and associative array of URI data starting
393 * at the supplied segment. For example, if this is your URI:
394 *
395 * example.com/user/search/name/joe/location/UK/gender/male
396 *
397 * You can use this function to generate an array with this prototype:
398 *
399 * array (
400 * name => joe
401 * location => UK
402 * gender => male
403 * )
404 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300405 * @param int the starting segment number
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @param array an array of default values
407 * @return array
408 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200409 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Barry Mienydd671972010-10-04 16:33:58 +0200411 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300413
Timothy Warren40403d22012-04-19 16:38:50 -0400414 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 /**
417 * Identical to above only it uses the re-routed segment array
418 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300419 * @param int the starting segment number
David Behler07b53422011-08-15 00:25:06 +0200420 * @param array an array of default values
421 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200423 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Barry Mienydd671972010-10-04 16:33:58 +0200425 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427
428 // --------------------------------------------------------------------
429
430 /**
431 * Generate a key value pair from the URI string or Re-routed URI string
432 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300433 * @param int the starting segment number
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @param array an array of default values
435 * @param string which array we should use
436 * @return array
437 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200438 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 if ( ! is_numeric($n))
441 {
442 return $default;
443 }
444
445 if (isset($this->keyval[$n]))
446 {
447 return $this->keyval[$n];
448 }
449
Andrey Andreevc123e112012-01-08 00:17:34 +0200450 if ($which === 'segment')
451 {
452 $total_segments = 'total_segments';
453 $segment_array = 'segment_array';
454 }
455 else
456 {
457 $total_segments = 'total_rsegments';
458 $segment_array = 'rsegment_array';
459 }
460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 if ($this->$total_segments() < $n)
462 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300463 return (count($default) === 0)
464 ? array()
465 : array_fill_keys($default, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467
468 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 $i = 0;
470 $lastval = '';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500471 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 foreach ($segments as $seg)
473 {
474 if ($i % 2)
475 {
476 $retval[$lastval] = $seg;
477 }
478 else
479 {
480 $retval[$seg] = FALSE;
481 $lastval = $seg;
482 }
483
484 $i++;
485 }
486
487 if (count($default) > 0)
488 {
489 foreach ($default as $val)
490 {
491 if ( ! array_key_exists($val, $retval))
492 {
493 $retval[$val] = FALSE;
494 }
495 }
496 }
497
498 // Cache the array for reuse
499 $this->keyval[$n] = $retval;
500 return $retval;
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Generate a URI string from an associative array
507 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @param array an associative array of key/values
509 * @return array
510 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200511 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 $temp = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300514 foreach ( (array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200516 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 $temp[] = $val;
518 }
519
520 return implode('/', $temp);
521 }
522
523 // --------------------------------------------------------------------
524
525 /**
526 * Fetch a URI Segment and add a trailing slash
527 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300528 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param string
530 * @return string
531 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200532 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 return $this->_slash_segment($n, $where, 'segment');
535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Fetch a URI Segment and add a trailing slash
541 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300542 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @param string
544 * @return string
545 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200546 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
548 return $this->_slash_segment($n, $where, 'rsegment');
549 }
550
551 // --------------------------------------------------------------------
552
553 /**
554 * Fetch a URI Segment and add a trailing slash - helper function
555 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300556 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @param string
558 * @param string
559 * @return string
560 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200561 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200563 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000564
Andrey Andreevc123e112012-01-08 00:17:34 +0200565 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 $leading = '';
568 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200569 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 $trailing = '';
572 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 return $leading.$this->$which($n).$trailing;
575 }
576
577 // --------------------------------------------------------------------
578
579 /**
580 * Segment Array
581 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @return array
583 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200584 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 return $this->segments;
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
592 * Routed Segment Array
593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @return array
595 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200596 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 return $this->rsegments;
599 }
600
601 // --------------------------------------------------------------------
602
603 /**
604 * Total number of segments
605 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300606 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200608 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 return count($this->segments);
611 }
612
613 // --------------------------------------------------------------------
614
615 /**
616 * Total number of routed segments
617 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300618 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200620 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
622 return count($this->rsegments);
623 }
624
625 // --------------------------------------------------------------------
626
627 /**
628 * Fetch the entire URI string
629 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * @return string
631 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200632 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
634 return $this->uri_string;
635 }
636
637
638 // --------------------------------------------------------------------
639
640 /**
641 * Fetch the entire Re-routed URI string
642 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * @return string
644 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200645 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100647 return '/'.implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 }
649
650}
Derek Allard2067d1a2008-11-13 22:59:24 +0000651
652/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300653/* Location: ./system/core/URI.php */