blob: 7909101691da6799fea363eb29f2a3b45b92f251 [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 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200176 $val = trim($val);
Andrey Andreev30d53242014-01-16 14:41:46 +0200177 // Filter segments for security
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200178 $this->filter_uri($val);
Andrey Andreev30d53242014-01-16 14:41:46 +0200179
180 if ($val !== '')
181 {
182 $this->segments[] = $val;
183 }
184 }
Andrey Andreeva4399052014-01-18 18:58:06 +0200185
186 unset($this->segments[0]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200187 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
190 // --------------------------------------------------------------------
191
192 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200193 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200195 * Will parse REQUEST_URI and automatically detect the URI from it,
196 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * @return string
199 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200200 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300202 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 return '';
205 }
206
Andrey Andreevd4516e32012-10-31 14:44:38 +0200207 $uri = parse_url($_SERVER['REQUEST_URI']);
208 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200209 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
Andrey Andreevd4516e32012-10-31 14:44:38 +0200211 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
212 {
213 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
214 }
215 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
216 {
217 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
218 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200219
Dan Horriganfea45ad2011-01-19 00:54:12 -0500220 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
221 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200222 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200224 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200225 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200226 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500228 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200230 $_SERVER['QUERY_STRING'] = $query;
231 }
232
Andrey Andreevea6688b2012-10-31 21:52:11 +0200233 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400234
Andrey Andreev4b322b12012-10-26 15:37:28 +0300235 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500236 {
237 return '/';
238 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400239
Dan Horriganfea45ad2011-01-19 00:54:12 -0500240 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800241 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200247 * Parse QUERY_STRING
248 *
249 * Will parse QUERY_STRING and automatically detect the URI from it.
250 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200251 * @return string
252 */
253 protected function _parse_query_string()
254 {
255 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
256
257 if (trim($uri, '/') === '')
258 {
259 return '';
260 }
261 elseif (strncmp($uri, '/', 1) === 0)
262 {
263 $uri = explode('?', $uri, 2);
264 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200265 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200266 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200267
Andrey Andreevea6688b2012-10-31 21:52:11 +0200268 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200269
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200270 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200271 }
272
273 // --------------------------------------------------------------------
274
275 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200276 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000277 *
278 * Take each command line argument and assume it is a URI segment.
279 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000280 * @return string
281 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200282 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000283 {
284 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200285 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // --------------------------------------------------------------------
289
290 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200291 * Remove relative directory (../) and multi slashes (///)
292 *
293 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
294 *
295 * @param string $url
296 * @return string
297 */
298 protected function _remove_relative_directory($uri)
299 {
300 $uris = array();
301 $tok = strtok($uri, '/');
302 while ($tok !== FALSE)
303 {
304 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
305 {
306 $uris[] = $tok;
307 }
308 $tok = strtok('/');
309 }
310
311 return implode('/', $uris);
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200317 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200319 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200320 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200321 * @param string $str
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200322 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 */
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200324 public function filter_uri(&$str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200326 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 +0000327 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200328 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
332 // --------------------------------------------------------------------
333
334 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200335 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200337 * @see CI_URI::$segments
338 * @param int $n Index
339 * @param mixed $no_result What to return if the segment index is not found
340 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100342 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300344 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
347 // --------------------------------------------------------------------
348
349 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200350 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200352 * Returns the re-routed URI segment (assuming routing rules are used)
353 * based on the index provided. If there is no routing, will return
354 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200356 * @see CI_URI::$rsegments
357 * @see CI_URI::segment()
358 * @param int $n Index
359 * @param mixed $no_result What to return if the segment index is not found
360 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100362 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300364 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
367 // --------------------------------------------------------------------
368
369 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200370 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200372 * Generates an associative array of URI data starting at the supplied
373 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 *
375 * example.com/user/search/name/joe/location/UK/gender/male
376 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200377 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200379 * array (
380 * name => joe
381 * location => UK
382 * gender => male
383 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200385 * @param int $n Index (default: 3)
386 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @return array
388 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200389 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Barry Mienydd671972010-10-04 16:33:58 +0200391 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300393
Timothy Warren40403d22012-04-19 16:38:50 -0400394 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200397 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200399 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
400 * segment array.
401 *
402 * @see CI_URI::uri_to_assoc()
403 * @param int $n Index (default: 3)
404 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200405 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200407 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Barry Mienydd671972010-10-04 16:33:58 +0200409 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
411
412 // --------------------------------------------------------------------
413
414 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200415 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200417 * Generates a key/value pair from the URI string or re-routed URI string.
418 *
419 * @used-by CI_URI::uri_to_assoc()
420 * @used-by CI_URI::ruri_to_assoc()
421 * @param int $n Index (default: 3)
422 * @param array $default Default values
423 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return array
425 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200426 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 if ( ! is_numeric($n))
429 {
430 return $default;
431 }
432
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300433 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300435 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437
Daniel Hunsaker42792232012-12-31 08:57:12 -0700438 $total_segments = "total_{$which}s";
439 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 if ($this->$total_segments() < $n)
442 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300443 return (count($default) === 0)
444 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100445 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 $i = 0;
450 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300451 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 foreach ($segments as $seg)
453 {
454 if ($i % 2)
455 {
456 $retval[$lastval] = $seg;
457 }
458 else
459 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100460 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 $lastval = $seg;
462 }
463
464 $i++;
465 }
466
467 if (count($default) > 0)
468 {
469 foreach ($default as $val)
470 {
471 if ( ! array_key_exists($val, $retval))
472 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100473 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475 }
476 }
477
478 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300479 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300480 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 return $retval;
482 }
483
484 // --------------------------------------------------------------------
485
486 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200487 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200489 * Generates a URI string from an associative array.
490 *
491 * @param array $array Input array of key/value pairs
492 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200494 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100497 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200499 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 $temp[] = $val;
501 }
502
503 return implode('/', $temp);
504 }
505
506 // --------------------------------------------------------------------
507
508 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200509 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200511 * Fetches an URI segment with a slash.
512 *
513 * @param int $n Index
514 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @return string
516 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200517 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 return $this->_slash_segment($n, $where, 'segment');
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200525 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200527 * Fetches an URI routed segment with a slash.
528 *
529 * @param int $n Index
530 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @return string
532 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200533 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
535 return $this->_slash_segment($n, $where, 'rsegment');
536 }
537
538 // --------------------------------------------------------------------
539
540 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200541 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200543 * Fetches an URI Segment and adds a slash to it.
544 *
545 * @used-by CI_URI::slash_segment()
546 * @used-by CI_URI::slash_rsegment()
547 *
548 * @param int $n Index
549 * @param string $where Where to add the slash ('trailing' or 'leading')
550 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * @return string
552 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200553 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200555 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000556
Andrey Andreevc123e112012-01-08 00:17:34 +0200557 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 $leading = '';
560 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200561 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 $trailing = '';
564 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 return $leading.$this->$which($n).$trailing;
567 }
568
569 // --------------------------------------------------------------------
570
571 /**
572 * Segment Array
573 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200574 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200576 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 return $this->segments;
579 }
580
581 // --------------------------------------------------------------------
582
583 /**
584 * Routed Segment Array
585 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200586 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200588 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 return $this->rsegments;
591 }
592
593 // --------------------------------------------------------------------
594
595 /**
596 * Total number of segments
597 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300598 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200600 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 return count($this->segments);
603 }
604
605 // --------------------------------------------------------------------
606
607 /**
608 * Total number of routed segments
609 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300610 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200612 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 return count($this->rsegments);
615 }
616
617 // --------------------------------------------------------------------
618
619 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200620 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200622 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200624 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 return $this->uri_string;
627 }
628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
630
631 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200632 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @return string
635 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200636 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200638 return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
640
641}
Derek Allard2067d1a2008-11-13 22:59:24 +0000642
643/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300644/* Location: ./system/core/URI.php */