blob: 9a545fd3abc2d58c132c8b4a4b670c3c228a0f19 [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 *
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @return string
185 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200186 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300188 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 return '';
191 }
192
Andrey Andreevd4516e32012-10-31 14:44:38 +0200193 $uri = parse_url($_SERVER['REQUEST_URI']);
194 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200195 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
Andrey Andreevd4516e32012-10-31 14:44:38 +0200197 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
198 {
199 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
200 }
201 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
202 {
203 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
204 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200205
Dan Horriganfea45ad2011-01-19 00:54:12 -0500206 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
207 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200208 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200210 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200211 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200212 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500214 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200216 $_SERVER['QUERY_STRING'] = $query;
217 }
218
Andrey Andreevea6688b2012-10-31 21:52:11 +0200219 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400220
Andrey Andreev4b322b12012-10-26 15:37:28 +0300221 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500222 {
223 return '/';
224 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400225
Dan Horriganfea45ad2011-01-19 00:54:12 -0500226 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800227 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800228 }
229
230 // --------------------------------------------------------------------
231
232 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200233 * Parse QUERY_STRING
234 *
235 * Will parse QUERY_STRING and automatically detect the URI from it.
236 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200237 * @return string
238 */
239 protected function _parse_query_string()
240 {
241 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
242
243 if (trim($uri, '/') === '')
244 {
245 return '';
246 }
247 elseif (strncmp($uri, '/', 1) === 0)
248 {
249 $uri = explode('?', $uri, 2);
250 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200251 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200252 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200253
Andrey Andreevea6688b2012-10-31 21:52:11 +0200254 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200255
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200256 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200257 }
258
259 // --------------------------------------------------------------------
260
261 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200262 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000263 *
264 * Take each command line argument and assume it is a URI segment.
265 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000266 * @return string
267 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200268 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000269 {
270 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200271 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273
274 // --------------------------------------------------------------------
275
276 /**
Andrey Andreev30d53242014-01-16 14:41:46 +0200277 * Remove relative directory (../) and multi slashes (///)
278 *
279 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
280 *
281 * @param string $url
282 * @return string
283 */
284 protected function _remove_relative_directory($uri)
285 {
286 $uris = array();
287 $tok = strtok($uri, '/');
288 while ($tok !== FALSE)
289 {
290 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
291 {
292 $uris[] = $tok;
293 }
294 $tok = strtok('/');
295 }
296
297 return implode('/', $uris);
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200303 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200305 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200306 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200307 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @return string
309 */
Andrey Andreevde14aa52014-01-15 15:51:08 +0200310 public function filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200312 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 +0000313 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200314 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316
Andrey Andreevc123e112012-01-08 00:17:34 +0200317 // Convert programatic characters to entities and return
318 return str_replace(
Andrey Andreevde14aa52014-01-15 15:51:08 +0200319 array('$', '(', ')', '%28', '%29'), // Bad
320 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
321 $str
322 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324
325 // --------------------------------------------------------------------
326
327 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200328 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200330 * @see CI_URI::$segments
331 * @param int $n Index
332 * @param mixed $no_result What to return if the segment index is not found
333 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100335 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300337 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200343 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200345 * Returns the re-routed URI segment (assuming routing rules are used)
346 * based on the index provided. If there is no routing, will return
347 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200349 * @see CI_URI::$rsegments
350 * @see CI_URI::segment()
351 * @param int $n Index
352 * @param mixed $no_result What to return if the segment index is not found
353 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100355 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300357 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359
360 // --------------------------------------------------------------------
361
362 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200363 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200365 * Generates an associative array of URI data starting at the supplied
366 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 *
368 * example.com/user/search/name/joe/location/UK/gender/male
369 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200370 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200372 * array (
373 * name => joe
374 * location => UK
375 * gender => male
376 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200378 * @param int $n Index (default: 3)
379 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @return array
381 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200382 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Barry Mienydd671972010-10-04 16:33:58 +0200384 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300386
Timothy Warren40403d22012-04-19 16:38:50 -0400387 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200390 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200392 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
393 * segment array.
394 *
395 * @see CI_URI::uri_to_assoc()
396 * @param int $n Index (default: 3)
397 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200398 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200400 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Barry Mienydd671972010-10-04 16:33:58 +0200402 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
405 // --------------------------------------------------------------------
406
407 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200408 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200410 * Generates a key/value pair from the URI string or re-routed URI string.
411 *
412 * @used-by CI_URI::uri_to_assoc()
413 * @used-by CI_URI::ruri_to_assoc()
414 * @param int $n Index (default: 3)
415 * @param array $default Default values
416 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @return array
418 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200419 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if ( ! is_numeric($n))
422 {
423 return $default;
424 }
425
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300426 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300428 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430
Daniel Hunsaker42792232012-12-31 08:57:12 -0700431 $total_segments = "total_{$which}s";
432 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 if ($this->$total_segments() < $n)
435 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300436 return (count($default) === 0)
437 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100438 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $i = 0;
443 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300444 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 foreach ($segments as $seg)
446 {
447 if ($i % 2)
448 {
449 $retval[$lastval] = $seg;
450 }
451 else
452 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100453 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 $lastval = $seg;
455 }
456
457 $i++;
458 }
459
460 if (count($default) > 0)
461 {
462 foreach ($default as $val)
463 {
464 if ( ! array_key_exists($val, $retval))
465 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100466 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468 }
469 }
470
471 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300472 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300473 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 return $retval;
475 }
476
477 // --------------------------------------------------------------------
478
479 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200480 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200482 * Generates a URI string from an associative array.
483 *
484 * @param array $array Input array of key/value pairs
485 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200487 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100490 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200492 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 $temp[] = $val;
494 }
495
496 return implode('/', $temp);
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200502 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200504 * Fetches an URI segment with a slash.
505 *
506 * @param int $n Index
507 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @return string
509 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200510 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 return $this->_slash_segment($n, $where, 'segment');
513 }
514
515 // --------------------------------------------------------------------
516
517 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200518 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200520 * Fetches an URI routed segment with a slash.
521 *
522 * @param int $n Index
523 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * @return string
525 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200526 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 return $this->_slash_segment($n, $where, 'rsegment');
529 }
530
531 // --------------------------------------------------------------------
532
533 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200534 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200536 * Fetches an URI Segment and adds a slash to it.
537 *
538 * @used-by CI_URI::slash_segment()
539 * @used-by CI_URI::slash_rsegment()
540 *
541 * @param int $n Index
542 * @param string $where Where to add the slash ('trailing' or 'leading')
543 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @return string
545 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200546 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200548 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000549
Andrey Andreevc123e112012-01-08 00:17:34 +0200550 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 $leading = '';
553 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200554 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $trailing = '';
557 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 return $leading.$this->$which($n).$trailing;
560 }
561
562 // --------------------------------------------------------------------
563
564 /**
565 * Segment Array
566 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200567 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200569 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
571 return $this->segments;
572 }
573
574 // --------------------------------------------------------------------
575
576 /**
577 * Routed Segment Array
578 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200579 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200581 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
583 return $this->rsegments;
584 }
585
586 // --------------------------------------------------------------------
587
588 /**
589 * Total number of segments
590 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300591 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200593 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 return count($this->segments);
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Total number of routed segments
602 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300603 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200605 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
607 return count($this->rsegments);
608 }
609
610 // --------------------------------------------------------------------
611
612 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200613 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200615 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200617 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 return $this->uri_string;
620 }
621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // --------------------------------------------------------------------
623
624 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200625 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @return string
628 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200629 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200631 return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
633
634}
Derek Allard2067d1a2008-11-13 22:59:24 +0000635
636/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300637/* Location: ./system/core/URI.php */