blob: c990be7b47968b5905d5029e3537b8eb8afe9212 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevc123e112012-01-08 00:17:34 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevc123e112012-01-08 00:17:34 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
Andrey Andreev92ebfb62012-05-17 12:49:24 +030025 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * URI Class
31 *
32 * Parses URIs and determines routing
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/uri.html
39 */
40class CI_URI {
41
David Behler07b53422011-08-15 00:25:06 +020042 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020043 * List of cached URI segments
David Behler07b53422011-08-15 00:25:06 +020044 *
Andrey Andreevcca74272012-10-28 14:43:36 +020045 * @var array
David Behler07b53422011-08-15 00:25:06 +020046 */
Andrey Andreev30d53242014-01-16 14:41:46 +020047 public $keyval = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020050 * Current URI string
David Behler07b53422011-08-15 00:25:06 +020051 *
Andrey Andreevcca74272012-10-28 14:43:36 +020052 * @var string
David Behler07b53422011-08-15 00:25:06 +020053 */
Andrey Andreev30d53242014-01-16 14:41:46 +020054 public $uri_string = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler07b53422011-08-15 00:25:06 +020056 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020057 * List of URI segments
David Behler07b53422011-08-15 00:25:06 +020058 *
vlakoffe1960502014-05-01 08:53:34 +020059 * Starts at 1 instead of 0.
60 *
Andrey Andreevcca74272012-10-28 14:43:36 +020061 * @var array
David Behler07b53422011-08-15 00:25:06 +020062 */
Andrey Andreev30d53242014-01-16 14:41:46 +020063 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030064
David Behler07b53422011-08-15 00:25:06 +020065 /**
vlakoffe1960502014-05-01 08:53:34 +020066 * List of routed URI segments
David Behler07b53422011-08-15 00:25:06 +020067 *
Andrey Andreevcca74272012-10-28 14:43:36 +020068 * Starts at 1 instead of 0.
69 *
70 * @var array
David Behler07b53422011-08-15 00:25:06 +020071 */
Andrey Andreev30d53242014-01-16 14:41:46 +020072 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000073
74 /**
Andrey Andreevde14aa52014-01-15 15:51:08 +020075 * Permitted URI chars
76 *
77 * PCRE character group allowed in URI segments
78 *
79 * @var string
80 */
81 protected $_permitted_uri_chars;
82
83 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020084 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000085 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +030086 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000087 */
Andrey Andreevc123e112012-01-08 00:17:34 +020088 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Derek Jones7576a3b2010-03-02 14:00:36 -060090 $this->config =& load_class('Config', 'core');
Andrey Andreevde14aa52014-01-15 15:51:08 +020091
Andrey Andreev30d53242014-01-16 14:41:46 +020092 // If query strings are enabled, we don't need to parse any segments.
93 // However, they don't make sense under CLI.
94 if (is_cli() OR $this->config->item('enable_query_strings') !== TRUE)
Andrey Andreevde14aa52014-01-15 15:51:08 +020095 {
96 $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars');
Andrey Andreev30d53242014-01-16 14:41:46 +020097
98 // If it's a CLI request, ignore the configuration
Andrey Andreeva00be042014-01-18 16:50:12 +020099 if (is_cli() OR ($protocol = strtoupper($this->config->item('uri_protocol'))) === 'CLI')
Andrey Andreev30d53242014-01-16 14:41:46 +0200100 {
101 $this->_set_uri_string($this->_parse_argv());
102 }
103 elseif ($protocol === 'AUTO')
104 {
105 // Is there a PATH_INFO variable? This should be the easiest solution.
106 if (isset($_SERVER['PATH_INFO']))
107 {
108 $this->_set_uri_string($_SERVER['PATH_INFO']);
109 }
110 // No PATH_INFO? Let's try REQUST_URI or QUERY_STRING then
111 elseif (($uri = $this->_parse_request_uri()) !== '' OR ($uri = $this->_parse_query_string()) !== '')
112 {
113 $this->_set_uri_string($uri);
114 }
115 // As a last ditch effor, let's try using the $_GET array
116 elseif (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
117 {
118 $this->_set_uri_string(key($_GET));
119 }
120 }
121 elseif (method_exists($this, ($method = '_parse_'.strtolower($protocol))))
122 {
123 $this->_set_uri_string($this->$method());
124 }
125 else
126 {
127 $uri = isset($_SERVER[$protocol]) ? $_SERVER[$protocol] : @getenv($protocol);
128 $this->_set_uri_string($uri);
129 }
Andrey Andreevde14aa52014-01-15 15:51:08 +0200130 }
131
Andrey Andreevc123e112012-01-08 00:17:34 +0200132 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
136
137 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200138 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400139 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200140 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200141 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400142 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300143 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400144 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300145 // Filter out control characters and trim slashes
146 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Andrey Andreev30d53242014-01-16 14:41:46 +0200147
148 if ($this->uri_string !== '')
149 {
150 // Remove the URL suffix, if present
151 if (($suffix = (string) $this->config->item('url_suffix')) !== '')
152 {
153 $slen = strlen($suffix);
154
155 if (substr($this->uri_string, -$slen) === $suffix)
156 {
157 $this->uri_string = substr($this->uri_string, 0, -$slen);
158 }
159 }
160
Andrey Andreeva4399052014-01-18 18:58:06 +0200161 $this->segments[0] = NULL;
Andrey Andreev30d53242014-01-16 14:41:46 +0200162 // Populate the segments array
163 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
164 {
165 // Filter segments for security
166 $val = trim($this->filter_uri($val));
167
168 if ($val !== '')
169 {
170 $this->segments[] = $val;
171 }
172 }
Andrey Andreeva4399052014-01-18 18:58:06 +0200173
174 unset($this->segments[0]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200175 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177
178 // --------------------------------------------------------------------
179
180 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200181 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200183 * Will parse REQUEST_URI and automatically detect the URI from it,
184 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @return string
187 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200188 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300190 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
192 return '';
193 }
194
Andrey Andreevd4516e32012-10-31 14:44:38 +0200195 $uri = parse_url($_SERVER['REQUEST_URI']);
196 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200197 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
Andrey Andreevd4516e32012-10-31 14:44:38 +0200199 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
200 {
201 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
202 }
203 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
204 {
205 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
206 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200207
Dan Horriganfea45ad2011-01-19 00:54:12 -0500208 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
209 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200210 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200212 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200213 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200214 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500216 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200218 $_SERVER['QUERY_STRING'] = $query;
219 }
220
Andrey Andreevea6688b2012-10-31 21:52:11 +0200221 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400222
Andrey Andreev4b322b12012-10-26 15:37:28 +0300223 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500224 {
225 return '/';
226 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400227
Dan Horriganfea45ad2011-01-19 00:54:12 -0500228 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800229 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800230 }
231
232 // --------------------------------------------------------------------
233
234 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200235 * Parse QUERY_STRING
236 *
237 * Will parse QUERY_STRING and automatically detect the URI from it.
238 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200239 * @return string
240 */
241 protected function _parse_query_string()
242 {
243 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
244
245 if (trim($uri, '/') === '')
246 {
247 return '';
248 }
249 elseif (strncmp($uri, '/', 1) === 0)
250 {
251 $uri = explode('?', $uri, 2);
252 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200253 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200254 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200255
Andrey Andreevea6688b2012-10-31 21:52:11 +0200256 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200257
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200258 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200259 }
260
261 // --------------------------------------------------------------------
262
263 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200264 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000265 *
266 * Take each command line argument and assume it is a URI segment.
267 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000268 * @return string
269 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200270 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000271 {
272 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200273 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275
276 // --------------------------------------------------------------------
277
278 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200279 * Remove relative directory (../) and multi slashes (///)
280 *
281 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
282 *
283 * @param string $url
284 * @return string
285 */
286 protected function _remove_relative_directory($uri)
287 {
288 $uris = array();
289 $tok = strtok($uri, '/');
290 while ($tok !== FALSE)
291 {
292 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
293 {
294 $uris[] = $tok;
295 }
296 $tok = strtok('/');
297 }
298
299 return implode('/', $uris);
300 }
301
302 // --------------------------------------------------------------------
303
304 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200305 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200307 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200308 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200309 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * @return string
311 */
Andrey Andreevde14aa52014-01-15 15:51:08 +0200312 public function filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200314 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 +0000315 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200316 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318
Andrey Andreevc123e112012-01-08 00:17:34 +0200319 // Convert programatic characters to entities and return
320 return str_replace(
Andrey Andreevde14aa52014-01-15 15:51:08 +0200321 array('$', '(', ')', '%28', '%29'), // Bad
322 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
323 $str
324 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326
327 // --------------------------------------------------------------------
328
329 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200330 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200332 * @see CI_URI::$segments
333 * @param int $n Index
334 * @param mixed $no_result What to return if the segment index is not found
335 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100337 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300339 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200345 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200347 * Returns the re-routed URI segment (assuming routing rules are used)
348 * based on the index provided. If there is no routing, will return
349 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200351 * @see CI_URI::$rsegments
352 * @see CI_URI::segment()
353 * @param int $n Index
354 * @param mixed $no_result What to return if the segment index is not found
355 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100357 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300359 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361
362 // --------------------------------------------------------------------
363
364 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200365 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200367 * Generates an associative array of URI data starting at the supplied
368 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 *
370 * example.com/user/search/name/joe/location/UK/gender/male
371 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200372 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200374 * array (
375 * name => joe
376 * location => UK
377 * gender => male
378 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200380 * @param int $n Index (default: 3)
381 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * @return array
383 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200384 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Barry Mienydd671972010-10-04 16:33:58 +0200386 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300388
Timothy Warren40403d22012-04-19 16:38:50 -0400389 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200392 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200394 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
395 * segment array.
396 *
397 * @see CI_URI::uri_to_assoc()
398 * @param int $n Index (default: 3)
399 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200400 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200402 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Barry Mienydd671972010-10-04 16:33:58 +0200404 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200410 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200412 * Generates a key/value pair from the URI string or re-routed URI string.
413 *
414 * @used-by CI_URI::uri_to_assoc()
415 * @used-by CI_URI::ruri_to_assoc()
416 * @param int $n Index (default: 3)
417 * @param array $default Default values
418 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @return array
420 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200421 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if ( ! is_numeric($n))
424 {
425 return $default;
426 }
427
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300428 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300430 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
Daniel Hunsaker42792232012-12-31 08:57:12 -0700433 $total_segments = "total_{$which}s";
434 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 if ($this->$total_segments() < $n)
437 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300438 return (count($default) === 0)
439 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100440 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
443 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 $i = 0;
445 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300446 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 foreach ($segments as $seg)
448 {
449 if ($i % 2)
450 {
451 $retval[$lastval] = $seg;
452 }
453 else
454 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100455 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 $lastval = $seg;
457 }
458
459 $i++;
460 }
461
462 if (count($default) > 0)
463 {
464 foreach ($default as $val)
465 {
466 if ( ! array_key_exists($val, $retval))
467 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100468 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470 }
471 }
472
473 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300474 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300475 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 return $retval;
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200482 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200484 * Generates a URI string from an associative array.
485 *
486 * @param array $array Input array of key/value pairs
487 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200489 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100492 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200494 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 $temp[] = $val;
496 }
497
498 return implode('/', $temp);
499 }
500
501 // --------------------------------------------------------------------
502
503 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200504 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200506 * Fetches an URI segment with a slash.
507 *
508 * @param int $n Index
509 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @return string
511 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200512 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 return $this->_slash_segment($n, $where, 'segment');
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200520 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200522 * Fetches an URI routed segment with a slash.
523 *
524 * @param int $n Index
525 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 * @return string
527 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200528 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
530 return $this->_slash_segment($n, $where, 'rsegment');
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200536 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200538 * Fetches an URI Segment and adds a slash to it.
539 *
540 * @used-by CI_URI::slash_segment()
541 * @used-by CI_URI::slash_rsegment()
542 *
543 * @param int $n Index
544 * @param string $where Where to add the slash ('trailing' or 'leading')
545 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 * @return string
547 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200548 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200550 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000551
Andrey Andreevc123e112012-01-08 00:17:34 +0200552 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 $leading = '';
555 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200556 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $trailing = '';
559 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 return $leading.$this->$which($n).$trailing;
562 }
563
564 // --------------------------------------------------------------------
565
566 /**
567 * Segment Array
568 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200569 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200571 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 return $this->segments;
574 }
575
576 // --------------------------------------------------------------------
577
578 /**
579 * Routed Segment Array
580 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200581 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200583 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 return $this->rsegments;
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Total number of segments
592 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300593 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200595 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 return count($this->segments);
598 }
599
600 // --------------------------------------------------------------------
601
602 /**
603 * Total number of routed segments
604 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300605 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200607 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 return count($this->rsegments);
610 }
611
612 // --------------------------------------------------------------------
613
614 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200615 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200617 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200619 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
621 return $this->uri_string;
622 }
623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // --------------------------------------------------------------------
625
626 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200627 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @return string
630 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200631 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200633 return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 }
635
636}
Derek Allard2067d1a2008-11-13 22:59:24 +0000637
638/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300639/* Location: ./system/core/URI.php */