blob: 80cf0b4e95581bd5e5131298b26701d0cd997b6a [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * URI Class
31 *
32 * Parses URIs and determines routing
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/uri.html
39 */
40class CI_URI {
41
David Behler07b53422011-08-15 00:25:06 +020042 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020043 * List of cached URI segments
David Behler07b53422011-08-15 00:25:06 +020044 *
Andrey Andreevcca74272012-10-28 14:43:36 +020045 * @var array
David Behler07b53422011-08-15 00:25:06 +020046 */
Andrey Andreev30d53242014-01-16 14:41:46 +020047 public $keyval = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020050 * Current URI string
David Behler07b53422011-08-15 00:25:06 +020051 *
Andrey Andreevcca74272012-10-28 14:43:36 +020052 * @var string
David Behler07b53422011-08-15 00:25:06 +020053 */
Andrey Andreev30d53242014-01-16 14:41:46 +020054 public $uri_string = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler07b53422011-08-15 00:25:06 +020056 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020057 * List of URI segments
David Behler07b53422011-08-15 00:25:06 +020058 *
Andrey Andreevcca74272012-10-28 14:43:36 +020059 * @var array
David Behler07b53422011-08-15 00:25:06 +020060 */
Andrey Andreev30d53242014-01-16 14:41:46 +020061 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030062
David Behler07b53422011-08-15 00:25:06 +020063 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020064 * Re-indexed list of URI segments
David Behler07b53422011-08-15 00:25:06 +020065 *
Andrey Andreevcca74272012-10-28 14:43:36 +020066 * Starts at 1 instead of 0.
67 *
68 * @var array
David Behler07b53422011-08-15 00:25:06 +020069 */
Andrey Andreev30d53242014-01-16 14:41:46 +020070 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000071
72 /**
Andrey Andreevde14aa52014-01-15 15:51:08 +020073 * Permitted URI chars
74 *
75 * PCRE character group allowed in URI segments
76 *
77 * @var string
78 */
79 protected $_permitted_uri_chars;
80
81 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020082 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000083 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +030084 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000085 */
Andrey Andreevc123e112012-01-08 00:17:34 +020086 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Derek Jones7576a3b2010-03-02 14:00:36 -060088 $this->config =& load_class('Config', 'core');
Andrey Andreevde14aa52014-01-15 15:51:08 +020089
Andrey Andreev30d53242014-01-16 14:41:46 +020090 // If query strings are enabled, we don't need to parse any segments.
91 // However, they don't make sense under CLI.
92 if (is_cli() OR $this->config->item('enable_query_strings') !== TRUE)
Andrey Andreevde14aa52014-01-15 15:51:08 +020093 {
94 $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars');
Andrey Andreev30d53242014-01-16 14:41:46 +020095
96 // If it's a CLI request, ignore the configuration
97 if (is_cli() OR ($protocol = strtoupper($this->config->item('uri_protocol')) === 'CLI'))
98 {
99 $this->_set_uri_string($this->_parse_argv());
100 }
101 elseif ($protocol === 'AUTO')
102 {
103 // Is there a PATH_INFO variable? This should be the easiest solution.
104 if (isset($_SERVER['PATH_INFO']))
105 {
106 $this->_set_uri_string($_SERVER['PATH_INFO']);
107 }
108 // No PATH_INFO? Let's try REQUST_URI or QUERY_STRING then
109 elseif (($uri = $this->_parse_request_uri()) !== '' OR ($uri = $this->_parse_query_string()) !== '')
110 {
111 $this->_set_uri_string($uri);
112 }
113 // As a last ditch effor, let's try using the $_GET array
114 elseif (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
115 {
116 $this->_set_uri_string(key($_GET));
117 }
118 }
119 elseif (method_exists($this, ($method = '_parse_'.strtolower($protocol))))
120 {
121 $this->_set_uri_string($this->$method());
122 }
123 else
124 {
125 $uri = isset($_SERVER[$protocol]) ? $_SERVER[$protocol] : @getenv($protocol);
126 $this->_set_uri_string($uri);
127 }
Andrey Andreevde14aa52014-01-15 15:51:08 +0200128 }
129
Andrey Andreevc123e112012-01-08 00:17:34 +0200130 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200136 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400137 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200138 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200139 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400140 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300141 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400142 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300143 // Filter out control characters and trim slashes
144 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Andrey Andreev30d53242014-01-16 14:41:46 +0200145
146 if ($this->uri_string !== '')
147 {
148 // Remove the URL suffix, if present
149 if (($suffix = (string) $this->config->item('url_suffix')) !== '')
150 {
151 $slen = strlen($suffix);
152
153 if (substr($this->uri_string, -$slen) === $suffix)
154 {
155 $this->uri_string = substr($this->uri_string, 0, -$slen);
156 }
157 }
158
159 // Populate the segments array
160 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
161 {
162 // Filter segments for security
163 $val = trim($this->filter_uri($val));
164
165 if ($val !== '')
166 {
167 $this->segments[] = $val;
168 }
169 }
170 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172
173 // --------------------------------------------------------------------
174
175 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200176 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200178 * Will parse REQUEST_URI and automatically detect the URI from it,
179 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200181 * @used-by CI_URI::_fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * @return string
183 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200184 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300186 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
188 return '';
189 }
190
Andrey Andreevd4516e32012-10-31 14:44:38 +0200191 $uri = parse_url($_SERVER['REQUEST_URI']);
192 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200193 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
Andrey Andreevd4516e32012-10-31 14:44:38 +0200195 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
196 {
197 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
198 }
199 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
200 {
201 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
202 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200203
Dan Horriganfea45ad2011-01-19 00:54:12 -0500204 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
205 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200206 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200208 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200209 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200210 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
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 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200214 $_SERVER['QUERY_STRING'] = $query;
215 }
216
Andrey Andreevea6688b2012-10-31 21:52:11 +0200217 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400218
Andrey Andreev4b322b12012-10-26 15:37:28 +0300219 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500220 {
221 return '/';
222 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400223
Dan Horriganfea45ad2011-01-19 00:54:12 -0500224 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800225 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800226 }
227
228 // --------------------------------------------------------------------
229
230 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200231 * Parse QUERY_STRING
232 *
233 * Will parse QUERY_STRING and automatically detect the URI from it.
234 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200235 * @return string
236 */
237 protected function _parse_query_string()
238 {
239 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
240
241 if (trim($uri, '/') === '')
242 {
243 return '';
244 }
245 elseif (strncmp($uri, '/', 1) === 0)
246 {
247 $uri = explode('?', $uri, 2);
248 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200249 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200250 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200251
Andrey Andreevea6688b2012-10-31 21:52:11 +0200252 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200253
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200254 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200255 }
256
257 // --------------------------------------------------------------------
258
259 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200260 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000261 *
262 * Take each command line argument and assume it is a URI segment.
263 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000264 * @return string
265 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200266 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000267 {
268 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200269 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271
272 // --------------------------------------------------------------------
273
274 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200275 * Remove relative directory (../) and multi slashes (///)
276 *
277 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
278 *
279 * @param string $url
280 * @return string
281 */
282 protected function _remove_relative_directory($uri)
283 {
284 $uris = array();
285 $tok = strtok($uri, '/');
286 while ($tok !== FALSE)
287 {
288 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
289 {
290 $uris[] = $tok;
291 }
292 $tok = strtok('/');
293 }
294
295 return implode('/', $uris);
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200301 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200303 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200304 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200305 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 * @return string
307 */
Andrey Andreevde14aa52014-01-15 15:51:08 +0200308 public function filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200310 if ( ! empty($str) && ! empty($this->_permitted_uri_chars) && ! preg_match('/^['.$this->_permitted_uri_chars.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200312 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
314
Andrey Andreevc123e112012-01-08 00:17:34 +0200315 // Convert programatic characters to entities and return
316 return str_replace(
Andrey Andreevde14aa52014-01-15 15:51:08 +0200317 array('$', '(', ')', '%28', '%29'), // Bad
318 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
319 $str
320 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322
323 // --------------------------------------------------------------------
324
325 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200326 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200328 * @see CI_URI::$segments
329 * @param int $n Index
330 * @param mixed $no_result What to return if the segment index is not found
331 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100333 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300335 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337
338 // --------------------------------------------------------------------
339
340 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200341 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200343 * Returns the re-routed URI segment (assuming routing rules are used)
344 * based on the index provided. If there is no routing, will return
345 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200347 * @see CI_URI::$rsegments
348 * @see CI_URI::segment()
349 * @param int $n Index
350 * @param mixed $no_result What to return if the segment index is not found
351 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100353 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300355 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357
358 // --------------------------------------------------------------------
359
360 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200361 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200363 * Generates an associative array of URI data starting at the supplied
364 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 *
366 * example.com/user/search/name/joe/location/UK/gender/male
367 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200368 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200370 * array (
371 * name => joe
372 * location => UK
373 * gender => male
374 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200376 * @param int $n Index (default: 3)
377 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @return array
379 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200380 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Barry Mienydd671972010-10-04 16:33:58 +0200382 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300384
Timothy Warren40403d22012-04-19 16:38:50 -0400385 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200388 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200390 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
391 * segment array.
392 *
393 * @see CI_URI::uri_to_assoc()
394 * @param int $n Index (default: 3)
395 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200396 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200398 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Barry Mienydd671972010-10-04 16:33:58 +0200400 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402
403 // --------------------------------------------------------------------
404
405 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200406 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200408 * Generates a key/value pair from the URI string or re-routed URI string.
409 *
410 * @used-by CI_URI::uri_to_assoc()
411 * @used-by CI_URI::ruri_to_assoc()
412 * @param int $n Index (default: 3)
413 * @param array $default Default values
414 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @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
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300424 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300426 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428
Daniel Hunsaker42792232012-12-31 08:57:12 -0700429 $total_segments = "total_{$which}s";
430 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 if ($this->$total_segments() < $n)
433 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300434 return (count($default) === 0)
435 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100436 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 $i = 0;
441 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300442 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 foreach ($segments as $seg)
444 {
445 if ($i % 2)
446 {
447 $retval[$lastval] = $seg;
448 }
449 else
450 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100451 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 $lastval = $seg;
453 }
454
455 $i++;
456 }
457
458 if (count($default) > 0)
459 {
460 foreach ($default as $val)
461 {
462 if ( ! array_key_exists($val, $retval))
463 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100464 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
466 }
467 }
468
469 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300470 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300471 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 return $retval;
473 }
474
475 // --------------------------------------------------------------------
476
477 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200478 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200480 * Generates a URI string from an associative array.
481 *
482 * @param array $array Input array of key/value pairs
483 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200485 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100488 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200490 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 $temp[] = $val;
492 }
493
494 return implode('/', $temp);
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200500 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200502 * Fetches an URI segment with a slash.
503 *
504 * @param int $n Index
505 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @return string
507 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200508 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 return $this->_slash_segment($n, $where, 'segment');
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200516 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200518 * Fetches an URI routed segment with a slash.
519 *
520 * @param int $n Index
521 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @return string
523 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200524 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 return $this->_slash_segment($n, $where, 'rsegment');
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200532 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200534 * Fetches an URI Segment and adds a slash to it.
535 *
536 * @used-by CI_URI::slash_segment()
537 * @used-by CI_URI::slash_rsegment()
538 *
539 * @param int $n Index
540 * @param string $where Where to add the slash ('trailing' or 'leading')
541 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @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 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200565 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 */
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 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200577 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 */
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 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300589 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 */
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 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300601 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 */
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 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200611 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200613 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 // --------------------------------------------------------------------
621
622 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200623 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 * @return string
626 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200627 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Andrey Andreev254735e2012-11-01 21:21:20 +0200629 global $RTR;
630
Andrey Andreev30d53242014-01-16 14:41:46 +0200631 return ltrim($RTR->directory, '/').implode('/', $this->rsegments);
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 Andreev92ebfb62012-05-17 12:49:24 +0300637/* Location: ./system/core/URI.php */