blob: 1817374b71f1d0cf85551dc20ce978b18772c783 [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevc123e112012-01-08 00:17:34 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevc123e112012-01-08 00:17:34 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Andrey Andreev92ebfb62012-05-17 12:49:24 +030036 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * URI Class
42 *
43 * Parses URIs and determines routing
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/uri.html
50 */
51class CI_URI {
52
David Behler07b53422011-08-15 00:25:06 +020053 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020054 * List of cached URI segments
David Behler07b53422011-08-15 00:25:06 +020055 *
Andrey Andreevcca74272012-10-28 14:43:36 +020056 * @var array
David Behler07b53422011-08-15 00:25:06 +020057 */
Andrey Andreev30d53242014-01-16 14:41:46 +020058 public $keyval = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030059
David Behler07b53422011-08-15 00:25:06 +020060 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020061 * Current URI string
David Behler07b53422011-08-15 00:25:06 +020062 *
Andrey Andreevcca74272012-10-28 14:43:36 +020063 * @var string
David Behler07b53422011-08-15 00:25:06 +020064 */
Andrey Andreev30d53242014-01-16 14:41:46 +020065 public $uri_string = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030066
David Behler07b53422011-08-15 00:25:06 +020067 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020068 * List of URI segments
David Behler07b53422011-08-15 00:25:06 +020069 *
vlakoffe1960502014-05-01 08:53:34 +020070 * Starts at 1 instead of 0.
71 *
Andrey Andreevcca74272012-10-28 14:43:36 +020072 * @var array
David Behler07b53422011-08-15 00:25:06 +020073 */
Andrey Andreev30d53242014-01-16 14:41:46 +020074 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030075
David Behler07b53422011-08-15 00:25:06 +020076 /**
vlakoffe1960502014-05-01 08:53:34 +020077 * List of routed URI segments
David Behler07b53422011-08-15 00:25:06 +020078 *
Andrey Andreevcca74272012-10-28 14:43:36 +020079 * Starts at 1 instead of 0.
80 *
81 * @var array
David Behler07b53422011-08-15 00:25:06 +020082 */
Andrey Andreev30d53242014-01-16 14:41:46 +020083 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000084
85 /**
Andrey Andreevde14aa52014-01-15 15:51:08 +020086 * Permitted URI chars
87 *
88 * PCRE character group allowed in URI segments
89 *
90 * @var string
91 */
92 protected $_permitted_uri_chars;
93
94 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020095 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000096 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +030097 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000098 */
Andrey Andreevc123e112012-01-08 00:17:34 +020099 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Derek Jones7576a3b2010-03-02 14:00:36 -0600101 $this->config =& load_class('Config', 'core');
Andrey Andreevde14aa52014-01-15 15:51:08 +0200102
Andrey Andreev30d53242014-01-16 14:41:46 +0200103 // If query strings are enabled, we don't need to parse any segments.
104 // However, they don't make sense under CLI.
105 if (is_cli() OR $this->config->item('enable_query_strings') !== TRUE)
Andrey Andreevde14aa52014-01-15 15:51:08 +0200106 {
107 $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars');
Andrey Andreev30d53242014-01-16 14:41:46 +0200108
109 // If it's a CLI request, ignore the configuration
Andrey Andreeva00be042014-01-18 16:50:12 +0200110 if (is_cli() OR ($protocol = strtoupper($this->config->item('uri_protocol'))) === 'CLI')
Andrey Andreev30d53242014-01-16 14:41:46 +0200111 {
112 $this->_set_uri_string($this->_parse_argv());
113 }
114 elseif ($protocol === 'AUTO')
115 {
116 // Is there a PATH_INFO variable? This should be the easiest solution.
117 if (isset($_SERVER['PATH_INFO']))
118 {
119 $this->_set_uri_string($_SERVER['PATH_INFO']);
120 }
121 // No PATH_INFO? Let's try REQUST_URI or QUERY_STRING then
122 elseif (($uri = $this->_parse_request_uri()) !== '' OR ($uri = $this->_parse_query_string()) !== '')
123 {
124 $this->_set_uri_string($uri);
125 }
126 // As a last ditch effor, let's try using the $_GET array
127 elseif (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
128 {
129 $this->_set_uri_string(key($_GET));
130 }
131 }
132 elseif (method_exists($this, ($method = '_parse_'.strtolower($protocol))))
133 {
134 $this->_set_uri_string($this->$method());
135 }
136 else
137 {
138 $uri = isset($_SERVER[$protocol]) ? $_SERVER[$protocol] : @getenv($protocol);
139 $this->_set_uri_string($uri);
140 }
Andrey Andreevde14aa52014-01-15 15:51:08 +0200141 }
142
Andrey Andreevc123e112012-01-08 00:17:34 +0200143 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
147
148 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200149 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400150 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200151 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200152 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400153 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300154 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400155 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300156 // Filter out control characters and trim slashes
157 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Andrey Andreev30d53242014-01-16 14:41:46 +0200158
159 if ($this->uri_string !== '')
160 {
161 // Remove the URL suffix, if present
162 if (($suffix = (string) $this->config->item('url_suffix')) !== '')
163 {
164 $slen = strlen($suffix);
165
166 if (substr($this->uri_string, -$slen) === $suffix)
167 {
168 $this->uri_string = substr($this->uri_string, 0, -$slen);
169 }
170 }
171
Andrey Andreeva4399052014-01-18 18:58:06 +0200172 $this->segments[0] = NULL;
Andrey Andreev30d53242014-01-16 14:41:46 +0200173 // Populate the segments array
Andrey Andreev4e4f2f52014-05-01 12:47:42 +0300174 foreach (explode('/', trim($this->uri_string, '/')) as $val)
Andrey Andreev30d53242014-01-16 14:41:46 +0200175 {
176 // Filter segments for security
177 $val = trim($this->filter_uri($val));
178
179 if ($val !== '')
180 {
181 $this->segments[] = $val;
182 }
183 }
Andrey Andreeva4399052014-01-18 18:58:06 +0200184
185 unset($this->segments[0]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200186 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
189 // --------------------------------------------------------------------
190
191 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200192 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200194 * Will parse REQUEST_URI and automatically detect the URI from it,
195 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @return string
198 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200199 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300201 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 return '';
204 }
205
Andrey Andreevd4516e32012-10-31 14:44:38 +0200206 $uri = parse_url($_SERVER['REQUEST_URI']);
207 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200208 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
Andrey Andreevd4516e32012-10-31 14:44:38 +0200210 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
211 {
212 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
213 }
214 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
215 {
216 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
217 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200218
Dan Horriganfea45ad2011-01-19 00:54:12 -0500219 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
220 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200221 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200223 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200224 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200225 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500227 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200229 $_SERVER['QUERY_STRING'] = $query;
230 }
231
Andrey Andreevea6688b2012-10-31 21:52:11 +0200232 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400233
Andrey Andreev4b322b12012-10-26 15:37:28 +0300234 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500235 {
236 return '/';
237 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400238
Dan Horriganfea45ad2011-01-19 00:54:12 -0500239 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800240 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800241 }
242
243 // --------------------------------------------------------------------
244
245 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200246 * Parse QUERY_STRING
247 *
248 * Will parse QUERY_STRING and automatically detect the URI from it.
249 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200250 * @return string
251 */
252 protected function _parse_query_string()
253 {
254 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
255
256 if (trim($uri, '/') === '')
257 {
258 return '';
259 }
260 elseif (strncmp($uri, '/', 1) === 0)
261 {
262 $uri = explode('?', $uri, 2);
263 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200264 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200265 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200266
Andrey Andreevea6688b2012-10-31 21:52:11 +0200267 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200268
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200269 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200270 }
271
272 // --------------------------------------------------------------------
273
274 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200275 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000276 *
277 * Take each command line argument and assume it is a URI segment.
278 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000279 * @return string
280 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200281 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000282 {
283 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200284 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200290 * Remove relative directory (../) and multi slashes (///)
291 *
292 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
293 *
294 * @param string $url
295 * @return string
296 */
297 protected function _remove_relative_directory($uri)
298 {
299 $uris = array();
300 $tok = strtok($uri, '/');
301 while ($tok !== FALSE)
302 {
303 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
304 {
305 $uris[] = $tok;
306 }
307 $tok = strtok('/');
308 }
309
310 return implode('/', $uris);
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200316 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200318 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200319 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200320 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * @return string
322 */
Andrey Andreevde14aa52014-01-15 15:51:08 +0200323 public function filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200325 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 +0000326 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200327 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329
Andrey Andreevc123e112012-01-08 00:17:34 +0200330 // Convert programatic characters to entities and return
331 return str_replace(
Andrey Andreevde14aa52014-01-15 15:51:08 +0200332 array('$', '(', ')', '%28', '%29'), // Bad
333 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
334 $str
335 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337
338 // --------------------------------------------------------------------
339
340 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200341 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200343 * @see CI_URI::$segments
344 * @param int $n Index
345 * @param mixed $no_result What to return if the segment index is not found
346 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100348 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300350 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
352
353 // --------------------------------------------------------------------
354
355 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200356 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200358 * Returns the re-routed URI segment (assuming routing rules are used)
359 * based on the index provided. If there is no routing, will return
360 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200362 * @see CI_URI::$rsegments
363 * @see CI_URI::segment()
364 * @param int $n Index
365 * @param mixed $no_result What to return if the segment index is not found
366 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100368 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300370 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
373 // --------------------------------------------------------------------
374
375 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200376 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200378 * Generates an associative array of URI data starting at the supplied
379 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 *
381 * example.com/user/search/name/joe/location/UK/gender/male
382 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200383 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200385 * array (
386 * name => joe
387 * location => UK
388 * gender => male
389 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200391 * @param int $n Index (default: 3)
392 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * @return array
394 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200395 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Barry Mienydd671972010-10-04 16:33:58 +0200397 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300399
Timothy Warren40403d22012-04-19 16:38:50 -0400400 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200403 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200405 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
406 * segment array.
407 *
408 * @see CI_URI::uri_to_assoc()
409 * @param int $n Index (default: 3)
410 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200411 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200413 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Barry Mienydd671972010-10-04 16:33:58 +0200415 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200421 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200423 * Generates a key/value pair from the URI string or re-routed URI string.
424 *
425 * @used-by CI_URI::uri_to_assoc()
426 * @used-by CI_URI::ruri_to_assoc()
427 * @param int $n Index (default: 3)
428 * @param array $default Default values
429 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return array
431 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200432 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 if ( ! is_numeric($n))
435 {
436 return $default;
437 }
438
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300439 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300441 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443
Daniel Hunsaker42792232012-12-31 08:57:12 -0700444 $total_segments = "total_{$which}s";
445 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 if ($this->$total_segments() < $n)
448 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300449 return (count($default) === 0)
450 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100451 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
453
454 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 $i = 0;
456 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300457 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 foreach ($segments as $seg)
459 {
460 if ($i % 2)
461 {
462 $retval[$lastval] = $seg;
463 }
464 else
465 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100466 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 $lastval = $seg;
468 }
469
470 $i++;
471 }
472
473 if (count($default) > 0)
474 {
475 foreach ($default as $val)
476 {
477 if ( ! array_key_exists($val, $retval))
478 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100479 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
481 }
482 }
483
484 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300485 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300486 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 return $retval;
488 }
489
490 // --------------------------------------------------------------------
491
492 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200493 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200495 * Generates a URI string from an associative array.
496 *
497 * @param array $array Input array of key/value pairs
498 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200500 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100503 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200505 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 $temp[] = $val;
507 }
508
509 return implode('/', $temp);
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200515 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200517 * Fetches an URI segment with a slash.
518 *
519 * @param int $n Index
520 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 * @return string
522 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200523 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 return $this->_slash_segment($n, $where, 'segment');
526 }
527
528 // --------------------------------------------------------------------
529
530 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200531 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200533 * Fetches an URI routed segment with a slash.
534 *
535 * @param int $n Index
536 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * @return string
538 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200539 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
541 return $this->_slash_segment($n, $where, 'rsegment');
542 }
543
544 // --------------------------------------------------------------------
545
546 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200547 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200549 * Fetches an URI Segment and adds a slash to it.
550 *
551 * @used-by CI_URI::slash_segment()
552 * @used-by CI_URI::slash_rsegment()
553 *
554 * @param int $n Index
555 * @param string $where Where to add the slash ('trailing' or 'leading')
556 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @return string
558 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200559 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200561 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000562
Andrey Andreevc123e112012-01-08 00:17:34 +0200563 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 $leading = '';
566 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200567 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 $trailing = '';
570 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 return $leading.$this->$which($n).$trailing;
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Segment Array
579 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200580 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200582 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 return $this->segments;
585 }
586
587 // --------------------------------------------------------------------
588
589 /**
590 * Routed Segment Array
591 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200592 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200594 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 return $this->rsegments;
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Total number of segments
603 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300604 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200606 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
608 return count($this->segments);
609 }
610
611 // --------------------------------------------------------------------
612
613 /**
614 * Total number of routed segments
615 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300616 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200618 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 return count($this->rsegments);
621 }
622
623 // --------------------------------------------------------------------
624
625 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200626 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200628 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200630 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
632 return $this->uri_string;
633 }
634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // --------------------------------------------------------------------
636
637 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200638 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @return string
641 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200642 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200644 return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 }
646
647}
Derek Allard2067d1a2008-11-13 22:59:24 +0000648
649/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300650/* Location: ./system/core/URI.php */