blob: 574ade69b92774dd2056f94109dac6a5ddab0b07 [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 Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/uri.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
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 Andreevfddbde62015-07-28 00:07:21 +0300204 // parse_url() returns false if no host is present, but the path or query string
205 // contains a colon followed by a number
Andrey Andreev5ffbdce2015-07-28 12:19:10 +0300206 $uri = parse_url('http://dummy'.$_SERVER['REQUEST_URI']);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200207 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200208 $uri = isset($uri['path']) ? $uri['path'] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
Cyrille TOULETcbf3a552015-03-30 09:14:46 +0200210 if (isset($_SERVER['SCRIPT_NAME'][0]))
211 {
212 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
213 {
214 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
215 }
216 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
217 {
218 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
219 }
220 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200221
Dan Horriganfea45ad2011-01-19 00:54:12 -0500222 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
223 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200224 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200226 $query = explode('?', $query, 2);
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200227 $uri = $query[0];
Andrey Andreevd4516e32012-10-31 14:44:38 +0200228 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500230 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200232 $_SERVER['QUERY_STRING'] = $query;
233 }
234
Andrey Andreevea6688b2012-10-31 21:52:11 +0200235 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400236
Andrey Andreev4b322b12012-10-26 15:37:28 +0300237 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500238 {
239 return '/';
240 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400241
Dan Horriganfea45ad2011-01-19 00:54:12 -0500242 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800243 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800244 }
245
246 // --------------------------------------------------------------------
247
248 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200249 * Parse QUERY_STRING
250 *
251 * Will parse QUERY_STRING and automatically detect the URI from it.
252 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200253 * @return string
254 */
255 protected function _parse_query_string()
256 {
257 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
258
259 if (trim($uri, '/') === '')
260 {
261 return '';
262 }
263 elseif (strncmp($uri, '/', 1) === 0)
264 {
265 $uri = explode('?', $uri, 2);
266 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev0ae4e6c2015-02-18 21:14:55 +0200267 $uri = $uri[0];
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200268 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200269
Andrey Andreevea6688b2012-10-31 21:52:11 +0200270 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200271
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200272 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200273 }
274
275 // --------------------------------------------------------------------
276
277 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200278 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000279 *
280 * Take each command line argument and assume it is a URI segment.
281 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000282 * @return string
283 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200284 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000285 {
286 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200287 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
289
290 // --------------------------------------------------------------------
291
292 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200293 * Remove relative directory (../) and multi slashes (///)
294 *
295 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
296 *
Andrey Andreevf0f93f52015-12-07 12:12:44 +0200297 * @param string $uri
Andrey Andreev30d53242014-01-16 14:41:46 +0200298 * @return string
299 */
300 protected function _remove_relative_directory($uri)
301 {
302 $uris = array();
303 $tok = strtok($uri, '/');
304 while ($tok !== FALSE)
305 {
306 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
307 {
308 $uris[] = $tok;
309 }
310 $tok = strtok('/');
311 }
312
313 return implode('/', $uris);
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200319 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200321 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200322 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200323 * @param string $str
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200324 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 */
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200326 public function filter_uri(&$str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200328 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 +0000329 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200330 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
333
334 // --------------------------------------------------------------------
335
336 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200337 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200339 * @see CI_URI::$segments
340 * @param int $n Index
341 * @param mixed $no_result What to return if the segment index is not found
342 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100344 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300346 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348
349 // --------------------------------------------------------------------
350
351 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200352 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200354 * Returns the re-routed URI segment (assuming routing rules are used)
355 * based on the index provided. If there is no routing, will return
356 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200358 * @see CI_URI::$rsegments
359 * @see CI_URI::segment()
360 * @param int $n Index
361 * @param mixed $no_result What to return if the segment index is not found
362 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100364 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300366 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368
369 // --------------------------------------------------------------------
370
371 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200372 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200374 * Generates an associative array of URI data starting at the supplied
375 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 *
377 * example.com/user/search/name/joe/location/UK/gender/male
378 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200379 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200381 * array (
382 * name => joe
383 * location => UK
384 * gender => male
385 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200387 * @param int $n Index (default: 3)
388 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @return array
390 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200391 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Barry Mienydd671972010-10-04 16:33:58 +0200393 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300395
Timothy Warren40403d22012-04-19 16:38:50 -0400396 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200399 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200401 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
402 * segment array.
403 *
404 * @see CI_URI::uri_to_assoc()
405 * @param int $n Index (default: 3)
406 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200407 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200409 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Barry Mienydd671972010-10-04 16:33:58 +0200411 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
413
414 // --------------------------------------------------------------------
415
416 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200417 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200419 * Generates a key/value pair from the URI string or re-routed URI string.
420 *
421 * @used-by CI_URI::uri_to_assoc()
422 * @used-by CI_URI::ruri_to_assoc()
423 * @param int $n Index (default: 3)
424 * @param array $default Default values
425 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return array
427 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200428 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 if ( ! is_numeric($n))
431 {
432 return $default;
433 }
434
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300435 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300437 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
Daniel Hunsaker42792232012-12-31 08:57:12 -0700440 $total_segments = "total_{$which}s";
441 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 if ($this->$total_segments() < $n)
444 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300445 return (count($default) === 0)
446 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100447 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 $i = 0;
452 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300453 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 foreach ($segments as $seg)
455 {
456 if ($i % 2)
457 {
458 $retval[$lastval] = $seg;
459 }
460 else
461 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100462 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 $lastval = $seg;
464 }
465
466 $i++;
467 }
468
469 if (count($default) > 0)
470 {
471 foreach ($default as $val)
472 {
473 if ( ! array_key_exists($val, $retval))
474 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100475 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477 }
478 }
479
480 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300481 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300482 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 return $retval;
484 }
485
486 // --------------------------------------------------------------------
487
488 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200489 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200491 * Generates a URI string from an associative array.
492 *
493 * @param array $array Input array of key/value pairs
494 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200496 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
498 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100499 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200501 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 $temp[] = $val;
503 }
504
505 return implode('/', $temp);
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200511 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200513 * Fetches an URI segment with a slash.
514 *
515 * @param int $n Index
516 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 * @return string
518 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200519 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 return $this->_slash_segment($n, $where, 'segment');
522 }
523
524 // --------------------------------------------------------------------
525
526 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200527 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200529 * Fetches an URI routed segment with a slash.
530 *
531 * @param int $n Index
532 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @return string
534 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200535 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
537 return $this->_slash_segment($n, $where, 'rsegment');
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200543 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200545 * Fetches an URI Segment and adds a slash to it.
546 *
547 * @used-by CI_URI::slash_segment()
548 * @used-by CI_URI::slash_rsegment()
549 *
550 * @param int $n Index
551 * @param string $where Where to add the slash ('trailing' or 'leading')
552 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * @return string
554 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200555 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200557 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000558
Andrey Andreevc123e112012-01-08 00:17:34 +0200559 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 $leading = '';
562 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200563 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 $trailing = '';
566 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 return $leading.$this->$which($n).$trailing;
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Segment Array
575 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200576 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200578 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 return $this->segments;
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Routed Segment Array
587 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200588 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200590 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 return $this->rsegments;
593 }
594
595 // --------------------------------------------------------------------
596
597 /**
598 * Total number of segments
599 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300600 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200602 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 return count($this->segments);
605 }
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Total number of routed segments
611 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300612 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200614 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
616 return count($this->rsegments);
617 }
618
619 // --------------------------------------------------------------------
620
621 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200622 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200624 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200626 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 return $this->uri_string;
629 }
630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 // --------------------------------------------------------------------
632
633 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200634 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @return string
637 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200638 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200640 return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
642
643}