blob: 13682cbeec75f0b0b66301b9cb21550f43985b22 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 *
Andrey Andreevcca74272012-10-28 14:43:36 +020059 * @var array
David Behler07b53422011-08-15 00:25:06 +020060 */
Andrey Andreev30d53242014-01-16 14:41:46 +020061 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030062
David Behler07b53422011-08-15 00:25:06 +020063 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020064 * Re-indexed list of URI segments
David Behler07b53422011-08-15 00:25:06 +020065 *
Andrey Andreevcca74272012-10-28 14:43:36 +020066 * Starts at 1 instead of 0.
67 *
68 * @var array
David Behler07b53422011-08-15 00:25:06 +020069 */
Andrey Andreev30d53242014-01-16 14:41:46 +020070 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000071
72 /**
Andrey Andreevde14aa52014-01-15 15:51:08 +020073 * Permitted URI chars
74 *
75 * PCRE character group allowed in URI segments
76 *
77 * @var string
78 */
79 protected $_permitted_uri_chars;
80
81 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020082 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000083 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +030084 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000085 */
Andrey Andreevc123e112012-01-08 00:17:34 +020086 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Derek Jones7576a3b2010-03-02 14:00:36 -060088 $this->config =& load_class('Config', 'core');
Andrey Andreevde14aa52014-01-15 15:51:08 +020089
Andrey Andreev30d53242014-01-16 14:41:46 +020090 // If query strings are enabled, we don't need to parse any segments.
91 // However, they don't make sense under CLI.
92 if (is_cli() OR $this->config->item('enable_query_strings') !== TRUE)
Andrey Andreevde14aa52014-01-15 15:51:08 +020093 {
94 $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars');
Andrey Andreev30d53242014-01-16 14:41:46 +020095
96 // If it's a CLI request, ignore the configuration
Andrey Andreeva00be042014-01-18 16:50:12 +020097 if (is_cli() OR ($protocol = strtoupper($this->config->item('uri_protocol'))) === 'CLI')
Andrey Andreev30d53242014-01-16 14:41:46 +020098 {
99 $this->_set_uri_string($this->_parse_argv());
100 }
101 elseif ($protocol === 'AUTO')
102 {
103 // Is there a PATH_INFO variable? This should be the easiest solution.
104 if (isset($_SERVER['PATH_INFO']))
105 {
106 $this->_set_uri_string($_SERVER['PATH_INFO']);
107 }
108 // No PATH_INFO? Let's try REQUST_URI or QUERY_STRING then
109 elseif (($uri = $this->_parse_request_uri()) !== '' OR ($uri = $this->_parse_query_string()) !== '')
110 {
111 $this->_set_uri_string($uri);
112 }
113 // As a last ditch effor, let's try using the $_GET array
114 elseif (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
115 {
116 $this->_set_uri_string(key($_GET));
117 }
118 }
119 elseif (method_exists($this, ($method = '_parse_'.strtolower($protocol))))
120 {
121 $this->_set_uri_string($this->$method());
122 }
123 else
124 {
125 $uri = isset($_SERVER[$protocol]) ? $_SERVER[$protocol] : @getenv($protocol);
126 $this->_set_uri_string($uri);
127 }
Andrey Andreevde14aa52014-01-15 15:51:08 +0200128 }
129
Andrey Andreevc123e112012-01-08 00:17:34 +0200130 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200136 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400137 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200138 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200139 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400140 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300141 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400142 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300143 // Filter out control characters and trim slashes
144 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Andrey Andreev30d53242014-01-16 14:41:46 +0200145
146 if ($this->uri_string !== '')
147 {
148 // Remove the URL suffix, if present
149 if (($suffix = (string) $this->config->item('url_suffix')) !== '')
150 {
151 $slen = strlen($suffix);
152
153 if (substr($this->uri_string, -$slen) === $suffix)
154 {
155 $this->uri_string = substr($this->uri_string, 0, -$slen);
156 }
157 }
158
Andrey Andreeva4399052014-01-18 18:58:06 +0200159 $this->segments[0] = NULL;
Andrey Andreev30d53242014-01-16 14:41:46 +0200160 // Populate the segments array
161 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
162 {
163 // Filter segments for security
164 $val = trim($this->filter_uri($val));
165
166 if ($val !== '')
167 {
168 $this->segments[] = $val;
169 }
170 }
Andrey Andreeva4399052014-01-18 18:58:06 +0200171
172 unset($this->segments[0]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200173 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 // --------------------------------------------------------------------
177
178 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200179 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200181 * Will parse REQUEST_URI and automatically detect the URI from it,
182 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200184 * @used-by CI_URI::_fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @return string
186 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200187 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300189 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
191 return '';
192 }
193
Andrey Andreevd4516e32012-10-31 14:44:38 +0200194 $uri = parse_url($_SERVER['REQUEST_URI']);
195 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200196 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000197
Andrey Andreevd4516e32012-10-31 14:44:38 +0200198 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
199 {
200 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
201 }
202 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
203 {
204 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
205 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200206
Dan Horriganfea45ad2011-01-19 00:54:12 -0500207 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
208 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200209 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200211 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200212 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200213 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500215 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200217 $_SERVER['QUERY_STRING'] = $query;
218 }
219
Andrey Andreevea6688b2012-10-31 21:52:11 +0200220 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400221
Andrey Andreev4b322b12012-10-26 15:37:28 +0300222 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500223 {
224 return '/';
225 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400226
Dan Horriganfea45ad2011-01-19 00:54:12 -0500227 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800228 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800229 }
230
231 // --------------------------------------------------------------------
232
233 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200234 * Parse QUERY_STRING
235 *
236 * Will parse QUERY_STRING and automatically detect the URI from it.
237 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200238 * @return string
239 */
240 protected function _parse_query_string()
241 {
242 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
243
244 if (trim($uri, '/') === '')
245 {
246 return '';
247 }
248 elseif (strncmp($uri, '/', 1) === 0)
249 {
250 $uri = explode('?', $uri, 2);
251 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200252 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200253 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200254
Andrey Andreevea6688b2012-10-31 21:52:11 +0200255 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200256
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200257 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200258 }
259
260 // --------------------------------------------------------------------
261
262 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200263 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000264 *
265 * Take each command line argument and assume it is a URI segment.
266 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000267 * @return string
268 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200269 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000270 {
271 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200272 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274
275 // --------------------------------------------------------------------
276
277 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200278 * Remove relative directory (../) and multi slashes (///)
279 *
280 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
281 *
282 * @param string $url
283 * @return string
284 */
285 protected function _remove_relative_directory($uri)
286 {
287 $uris = array();
288 $tok = strtok($uri, '/');
289 while ($tok !== FALSE)
290 {
291 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
292 {
293 $uris[] = $tok;
294 }
295 $tok = strtok('/');
296 }
297
298 return implode('/', $uris);
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200304 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200306 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200307 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200308 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @return string
310 */
Andrey Andreevde14aa52014-01-15 15:51:08 +0200311 public function filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200313 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 +0000314 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200315 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
Andrey Andreevc123e112012-01-08 00:17:34 +0200318 // Convert programatic characters to entities and return
319 return str_replace(
Andrey Andreevde14aa52014-01-15 15:51:08 +0200320 array('$', '(', ')', '%28', '%29'), // Bad
321 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
322 $str
323 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
325
326 // --------------------------------------------------------------------
327
328 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200329 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200331 * @see CI_URI::$segments
332 * @param int $n Index
333 * @param mixed $no_result What to return if the segment index is not found
334 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100336 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300338 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
340
341 // --------------------------------------------------------------------
342
343 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200344 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200346 * Returns the re-routed URI segment (assuming routing rules are used)
347 * based on the index provided. If there is no routing, will return
348 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200350 * @see CI_URI::$rsegments
351 * @see CI_URI::segment()
352 * @param int $n Index
353 * @param mixed $no_result What to return if the segment index is not found
354 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100356 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300358 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
361 // --------------------------------------------------------------------
362
363 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200364 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200366 * Generates an associative array of URI data starting at the supplied
367 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 *
369 * example.com/user/search/name/joe/location/UK/gender/male
370 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200371 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200373 * array (
374 * name => joe
375 * location => UK
376 * gender => male
377 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200379 * @param int $n Index (default: 3)
380 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @return array
382 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200383 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Barry Mienydd671972010-10-04 16:33:58 +0200385 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300387
Timothy Warren40403d22012-04-19 16:38:50 -0400388 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200391 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200393 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
394 * segment array.
395 *
396 * @see CI_URI::uri_to_assoc()
397 * @param int $n Index (default: 3)
398 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200399 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200401 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Barry Mienydd671972010-10-04 16:33:58 +0200403 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 // --------------------------------------------------------------------
407
408 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200409 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200411 * Generates a key/value pair from the URI string or re-routed URI string.
412 *
413 * @used-by CI_URI::uri_to_assoc()
414 * @used-by CI_URI::ruri_to_assoc()
415 * @param int $n Index (default: 3)
416 * @param array $default Default values
417 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 * @return array
419 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200420 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 if ( ! is_numeric($n))
423 {
424 return $default;
425 }
426
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300427 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300429 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
Daniel Hunsaker42792232012-12-31 08:57:12 -0700432 $total_segments = "total_{$which}s";
433 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if ($this->$total_segments() < $n)
436 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300437 return (count($default) === 0)
438 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100439 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 $i = 0;
444 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300445 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 foreach ($segments as $seg)
447 {
448 if ($i % 2)
449 {
450 $retval[$lastval] = $seg;
451 }
452 else
453 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100454 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 $lastval = $seg;
456 }
457
458 $i++;
459 }
460
461 if (count($default) > 0)
462 {
463 foreach ($default as $val)
464 {
465 if ( ! array_key_exists($val, $retval))
466 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100467 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 }
470 }
471
472 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300473 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300474 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 return $retval;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200481 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200483 * Generates a URI string from an associative array.
484 *
485 * @param array $array Input array of key/value pairs
486 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200488 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
490 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100491 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200493 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 $temp[] = $val;
495 }
496
497 return implode('/', $temp);
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200503 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200505 * Fetches an URI segment with a slash.
506 *
507 * @param int $n Index
508 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * @return string
510 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200511 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 return $this->_slash_segment($n, $where, 'segment');
514 }
515
516 // --------------------------------------------------------------------
517
518 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200519 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200521 * Fetches an URI routed segment with a slash.
522 *
523 * @param int $n Index
524 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * @return string
526 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200527 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
529 return $this->_slash_segment($n, $where, 'rsegment');
530 }
531
532 // --------------------------------------------------------------------
533
534 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200535 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200537 * Fetches an URI Segment and adds a slash to it.
538 *
539 * @used-by CI_URI::slash_segment()
540 * @used-by CI_URI::slash_rsegment()
541 *
542 * @param int $n Index
543 * @param string $where Where to add the slash ('trailing' or 'leading')
544 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @return string
546 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200547 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200549 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000550
Andrey Andreevc123e112012-01-08 00:17:34 +0200551 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 $leading = '';
554 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200555 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 $trailing = '';
558 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 return $leading.$this->$which($n).$trailing;
561 }
562
563 // --------------------------------------------------------------------
564
565 /**
566 * Segment Array
567 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200568 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200570 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
572 return $this->segments;
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Routed Segment Array
579 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200580 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200582 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 return $this->rsegments;
585 }
586
587 // --------------------------------------------------------------------
588
589 /**
590 * Total number of segments
591 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300592 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200594 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 return count($this->segments);
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Total number of routed segments
603 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300604 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200606 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
608 return count($this->rsegments);
609 }
610
611 // --------------------------------------------------------------------
612
613 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200614 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200616 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200618 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 return $this->uri_string;
621 }
622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // --------------------------------------------------------------------
624
625 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200626 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @return string
629 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200630 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Andrey Andreev254735e2012-11-01 21:21:20 +0200632 global $RTR;
633
Andrey Andreev30d53242014-01-16 14:41:46 +0200634 return ltrim($RTR->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
636
637}
Derek Allard2067d1a2008-11-13 22:59:24 +0000638
639/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300640/* Location: ./system/core/URI.php */