blob: e96749456b200afe1ba1310bf88867d39edb12bf [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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 Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @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 Andreev0ae4e6c2015-02-18 21:14:55 +0200110 if (is_cli())
Andrey Andreev30d53242014-01-16 14:41:46 +0200111 {
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200112 $uri = $this->_parse_argv();
Andrey Andreev30d53242014-01-16 14:41:46 +0200113 }
114 else
115 {
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200116 $protocol = $this->config->item('uri_protocol');
117 empty($protocol) && $protocol = 'REQUEST_URI';
118
119 switch ($protocol)
120 {
121 case 'AUTO': // For BC purposes only
122 case 'REQUEST_URI':
123 $uri = $this->_parse_request_uri();
124 break;
125 case 'QUERY_STRING':
126 $uri = $this->_parse_query_string();
127 break;
128 case 'PATH_INFO':
129 default:
130 $uri = isset($_SERVER[$protocol])
131 ? $_SERVER[$protocol]
132 : $this->_parse_request_uri();
133 break;
134 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200135 }
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200136
137 $this->_set_uri_string($uri);
Andrey Andreevde14aa52014-01-15 15:51:08 +0200138 }
139
Andrey Andreev90726b82015-01-20 12:39:22 +0200140 log_message('info', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // --------------------------------------------------------------------
144
145 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200146 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400147 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200148 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200149 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400150 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300151 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400152 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300153 // Filter out control characters and trim slashes
154 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Andrey Andreev30d53242014-01-16 14:41:46 +0200155
156 if ($this->uri_string !== '')
157 {
158 // Remove the URL suffix, if present
159 if (($suffix = (string) $this->config->item('url_suffix')) !== '')
160 {
161 $slen = strlen($suffix);
162
163 if (substr($this->uri_string, -$slen) === $suffix)
164 {
165 $this->uri_string = substr($this->uri_string, 0, -$slen);
166 }
167 }
168
Andrey Andreeva4399052014-01-18 18:58:06 +0200169 $this->segments[0] = NULL;
Andrey Andreev30d53242014-01-16 14:41:46 +0200170 // Populate the segments array
Andrey Andreev4e4f2f52014-05-01 12:47:42 +0300171 foreach (explode('/', trim($this->uri_string, '/')) as $val)
Andrey Andreev30d53242014-01-16 14:41:46 +0200172 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200173 $val = trim($val);
Andrey Andreev30d53242014-01-16 14:41:46 +0200174 // Filter segments for security
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200175 $this->filter_uri($val);
Andrey Andreev30d53242014-01-16 14:41:46 +0200176
177 if ($val !== '')
178 {
179 $this->segments[] = $val;
180 }
181 }
Andrey Andreeva4399052014-01-18 18:58:06 +0200182
183 unset($this->segments[0]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200184 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186
187 // --------------------------------------------------------------------
188
189 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200190 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200192 * Will parse REQUEST_URI and automatically detect the URI from it,
193 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @return string
196 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200197 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300199 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 return '';
202 }
203
Andrey Andreevd4516e32012-10-31 14:44:38 +0200204 $uri = parse_url($_SERVER['REQUEST_URI']);
205 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200206 $uri = isset($uri['path']) ? $uri['path'] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000207
Andrey Andreevd4516e32012-10-31 14:44:38 +0200208 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
209 {
210 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
211 }
212 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
213 {
214 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
215 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200216
Dan Horriganfea45ad2011-01-19 00:54:12 -0500217 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
218 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200219 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200221 $query = explode('?', $query, 2);
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200222 $uri = $query[0];
Andrey Andreevd4516e32012-10-31 14:44:38 +0200223 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500225 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200227 $_SERVER['QUERY_STRING'] = $query;
228 }
229
Andrey Andreevea6688b2012-10-31 21:52:11 +0200230 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400231
Andrey Andreev4b322b12012-10-26 15:37:28 +0300232 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500233 {
234 return '/';
235 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400236
Dan Horriganfea45ad2011-01-19 00:54:12 -0500237 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800238 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800239 }
240
241 // --------------------------------------------------------------------
242
243 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200244 * Parse QUERY_STRING
245 *
246 * Will parse QUERY_STRING and automatically detect the URI from it.
247 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200248 * @return string
249 */
250 protected function _parse_query_string()
251 {
252 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
253
254 if (trim($uri, '/') === '')
255 {
256 return '';
257 }
258 elseif (strncmp($uri, '/', 1) === 0)
259 {
260 $uri = explode('?', $uri, 2);
261 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200262 $uri = $uri[0];
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200263 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200264
Andrey Andreevea6688b2012-10-31 21:52:11 +0200265 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200266
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200267 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200268 }
269
270 // --------------------------------------------------------------------
271
272 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200273 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000274 *
275 * Take each command line argument and assume it is a URI segment.
276 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000277 * @return string
278 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200279 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000280 {
281 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200282 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284
285 // --------------------------------------------------------------------
286
287 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200288 * Remove relative directory (../) and multi slashes (///)
289 *
290 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
291 *
292 * @param string $url
293 * @return string
294 */
295 protected function _remove_relative_directory($uri)
296 {
297 $uris = array();
298 $tok = strtok($uri, '/');
299 while ($tok !== FALSE)
300 {
301 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
302 {
303 $uris[] = $tok;
304 }
305 $tok = strtok('/');
306 }
307
308 return implode('/', $uris);
309 }
310
311 // --------------------------------------------------------------------
312
313 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200314 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200316 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200317 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200318 * @param string $str
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200319 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200321 public function filter_uri(&$str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200323 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 +0000324 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200325 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328
329 // --------------------------------------------------------------------
330
331 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200332 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200334 * @see CI_URI::$segments
335 * @param int $n Index
336 * @param mixed $no_result What to return if the segment index is not found
337 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100339 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300341 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
343
344 // --------------------------------------------------------------------
345
346 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200347 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200349 * Returns the re-routed URI segment (assuming routing rules are used)
350 * based on the index provided. If there is no routing, will return
351 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200353 * @see CI_URI::$rsegments
354 * @see CI_URI::segment()
355 * @param int $n Index
356 * @param mixed $no_result What to return if the segment index is not found
357 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100359 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300361 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
363
364 // --------------------------------------------------------------------
365
366 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200367 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200369 * Generates an associative array of URI data starting at the supplied
370 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
372 * example.com/user/search/name/joe/location/UK/gender/male
373 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200374 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200376 * array (
377 * name => joe
378 * location => UK
379 * gender => male
380 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200382 * @param int $n Index (default: 3)
383 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @return array
385 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200386 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Barry Mienydd671972010-10-04 16:33:58 +0200388 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300390
Timothy Warren40403d22012-04-19 16:38:50 -0400391 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200394 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200396 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
397 * segment array.
398 *
399 * @see CI_URI::uri_to_assoc()
400 * @param int $n Index (default: 3)
401 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200402 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200404 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Barry Mienydd671972010-10-04 16:33:58 +0200406 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
409 // --------------------------------------------------------------------
410
411 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200412 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200414 * Generates a key/value pair from the URI string or re-routed URI string.
415 *
416 * @used-by CI_URI::uri_to_assoc()
417 * @used-by CI_URI::ruri_to_assoc()
418 * @param int $n Index (default: 3)
419 * @param array $default Default values
420 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * @return array
422 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200423 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 if ( ! is_numeric($n))
426 {
427 return $default;
428 }
429
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300430 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300432 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
Daniel Hunsaker42792232012-12-31 08:57:12 -0700435 $total_segments = "total_{$which}s";
436 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 if ($this->$total_segments() < $n)
439 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300440 return (count($default) === 0)
441 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100442 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444
445 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 $i = 0;
447 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300448 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 foreach ($segments as $seg)
450 {
451 if ($i % 2)
452 {
453 $retval[$lastval] = $seg;
454 }
455 else
456 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100457 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 $lastval = $seg;
459 }
460
461 $i++;
462 }
463
464 if (count($default) > 0)
465 {
466 foreach ($default as $val)
467 {
468 if ( ! array_key_exists($val, $retval))
469 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100470 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472 }
473 }
474
475 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300476 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300477 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 return $retval;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200484 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200486 * Generates a URI string from an associative array.
487 *
488 * @param array $array Input array of key/value pairs
489 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200491 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100494 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200496 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 $temp[] = $val;
498 }
499
500 return implode('/', $temp);
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200506 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200508 * Fetches an URI segment with a slash.
509 *
510 * @param int $n Index
511 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @return string
513 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200514 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 return $this->_slash_segment($n, $where, 'segment');
517 }
518
519 // --------------------------------------------------------------------
520
521 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200522 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200524 * Fetches an URI routed segment with a slash.
525 *
526 * @param int $n Index
527 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @return string
529 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200530 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
532 return $this->_slash_segment($n, $where, 'rsegment');
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200538 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200540 * Fetches an URI Segment and adds a slash to it.
541 *
542 * @used-by CI_URI::slash_segment()
543 * @used-by CI_URI::slash_rsegment()
544 *
545 * @param int $n Index
546 * @param string $where Where to add the slash ('trailing' or 'leading')
547 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @return string
549 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200550 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200552 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000553
Andrey Andreevc123e112012-01-08 00:17:34 +0200554 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $leading = '';
557 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200558 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 $trailing = '';
561 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 return $leading.$this->$which($n).$trailing;
564 }
565
566 // --------------------------------------------------------------------
567
568 /**
569 * Segment Array
570 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200571 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200573 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 return $this->segments;
576 }
577
578 // --------------------------------------------------------------------
579
580 /**
581 * Routed Segment Array
582 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200583 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200585 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 return $this->rsegments;
588 }
589
590 // --------------------------------------------------------------------
591
592 /**
593 * Total number of segments
594 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300595 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200597 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 return count($this->segments);
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Total number of routed segments
606 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300607 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200609 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 return count($this->rsegments);
612 }
613
614 // --------------------------------------------------------------------
615
616 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200617 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200619 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200621 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 return $this->uri_string;
624 }
625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // --------------------------------------------------------------------
627
628 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200629 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @return string
632 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200633 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200635 return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
637
638}