blob: 7809e17c085c90b51d2979067a50b964032fa39f [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 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330
331 // --------------------------------------------------------------------
332
333 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200334 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200336 * @see CI_URI::$segments
337 * @param int $n Index
338 * @param mixed $no_result What to return if the segment index is not found
339 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100341 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300343 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
345
346 // --------------------------------------------------------------------
347
348 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200349 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200351 * Returns the re-routed URI segment (assuming routing rules are used)
352 * based on the index provided. If there is no routing, will return
353 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200355 * @see CI_URI::$rsegments
356 * @see CI_URI::segment()
357 * @param int $n Index
358 * @param mixed $no_result What to return if the segment index is not found
359 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100361 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300363 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 // --------------------------------------------------------------------
367
368 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200369 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200371 * Generates an associative array of URI data starting at the supplied
372 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 *
374 * example.com/user/search/name/joe/location/UK/gender/male
375 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200376 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200378 * array (
379 * name => joe
380 * location => UK
381 * gender => male
382 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200384 * @param int $n Index (default: 3)
385 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @return array
387 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200388 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Barry Mienydd671972010-10-04 16:33:58 +0200390 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300392
Timothy Warren40403d22012-04-19 16:38:50 -0400393 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200396 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200398 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
399 * segment array.
400 *
401 * @see CI_URI::uri_to_assoc()
402 * @param int $n Index (default: 3)
403 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200404 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200406 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Barry Mienydd671972010-10-04 16:33:58 +0200408 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410
411 // --------------------------------------------------------------------
412
413 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200414 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200416 * Generates a key/value pair from the URI string or re-routed URI string.
417 *
418 * @used-by CI_URI::uri_to_assoc()
419 * @used-by CI_URI::ruri_to_assoc()
420 * @param int $n Index (default: 3)
421 * @param array $default Default values
422 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @return array
424 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200425 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 if ( ! is_numeric($n))
428 {
429 return $default;
430 }
431
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300432 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300434 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
436
Daniel Hunsaker42792232012-12-31 08:57:12 -0700437 $total_segments = "total_{$which}s";
438 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 if ($this->$total_segments() < $n)
441 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300442 return (count($default) === 0)
443 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100444 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446
447 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 $i = 0;
449 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300450 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 foreach ($segments as $seg)
452 {
453 if ($i % 2)
454 {
455 $retval[$lastval] = $seg;
456 }
457 else
458 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100459 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 $lastval = $seg;
461 }
462
463 $i++;
464 }
465
466 if (count($default) > 0)
467 {
468 foreach ($default as $val)
469 {
470 if ( ! array_key_exists($val, $retval))
471 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100472 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474 }
475 }
476
477 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300478 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300479 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 return $retval;
481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200486 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200488 * Generates a URI string from an associative array.
489 *
490 * @param array $array Input array of key/value pairs
491 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200493 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
495 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100496 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200498 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 $temp[] = $val;
500 }
501
502 return implode('/', $temp);
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200508 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200510 * Fetches an URI segment with a slash.
511 *
512 * @param int $n Index
513 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 * @return string
515 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200516 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 return $this->_slash_segment($n, $where, 'segment');
519 }
520
521 // --------------------------------------------------------------------
522
523 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200524 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200526 * Fetches an URI routed segment with a slash.
527 *
528 * @param int $n Index
529 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @return string
531 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200532 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 return $this->_slash_segment($n, $where, 'rsegment');
535 }
536
537 // --------------------------------------------------------------------
538
539 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200540 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200542 * Fetches an URI Segment and adds a slash to it.
543 *
544 * @used-by CI_URI::slash_segment()
545 * @used-by CI_URI::slash_rsegment()
546 *
547 * @param int $n Index
548 * @param string $where Where to add the slash ('trailing' or 'leading')
549 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @return string
551 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200552 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200554 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000555
Andrey Andreevc123e112012-01-08 00:17:34 +0200556 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $leading = '';
559 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200560 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 $trailing = '';
563 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 return $leading.$this->$which($n).$trailing;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Segment Array
572 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200573 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200575 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
577 return $this->segments;
578 }
579
580 // --------------------------------------------------------------------
581
582 /**
583 * Routed Segment Array
584 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200585 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200587 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
589 return $this->rsegments;
590 }
591
592 // --------------------------------------------------------------------
593
594 /**
595 * Total number of segments
596 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300597 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200599 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 return count($this->segments);
602 }
603
604 // --------------------------------------------------------------------
605
606 /**
607 * Total number of routed segments
608 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300609 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200611 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 return count($this->rsegments);
614 }
615
616 // --------------------------------------------------------------------
617
618 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200619 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200621 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200623 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 return $this->uri_string;
626 }
627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // --------------------------------------------------------------------
629
630 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200631 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 * @return string
634 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200635 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200637 return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
639
640}
Derek Allard2067d1a2008-11-13 22:59:24 +0000641
642/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300643/* Location: ./system/core/URI.php */