blob: 3b7718fff09ef303b3180d3919bb306a430732d9 [file] [log] [blame]
Andrey Andreevc123e112012-01-08 00:17:34 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * URI Class
30 *
31 * Parses URIs and determines routing
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/uri.html
38 */
39class CI_URI {
40
David Behler07b53422011-08-15 00:25:06 +020041 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020042 * List of cached URI segments
David Behler07b53422011-08-15 00:25:06 +020043 *
Andrey Andreevcca74272012-10-28 14:43:36 +020044 * @var array
David Behler07b53422011-08-15 00:25:06 +020045 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040046 public $keyval = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030047
David Behler07b53422011-08-15 00:25:06 +020048 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020049 * Current URI string
David Behler07b53422011-08-15 00:25:06 +020050 *
Andrey Andreevcca74272012-10-28 14:43:36 +020051 * @var string
David Behler07b53422011-08-15 00:25:06 +020052 */
Andrey Andreevc123e112012-01-08 00:17:34 +020053 public $uri_string;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030054
David Behler07b53422011-08-15 00:25:06 +020055 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020056 * List of URI segments
David Behler07b53422011-08-15 00:25:06 +020057 *
Andrey Andreevcca74272012-10-28 14:43:36 +020058 * @var array
David Behler07b53422011-08-15 00:25:06 +020059 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040060 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061
David Behler07b53422011-08-15 00:25:06 +020062 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020063 * Re-indexed list of URI segments
David Behler07b53422011-08-15 00:25:06 +020064 *
Andrey Andreevcca74272012-10-28 14:43:36 +020065 * Starts at 1 instead of 0.
66 *
67 * @var array
David Behler07b53422011-08-15 00:25:06 +020068 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040069 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000070
71 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020072 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000073 *
Andrey Andreevc123e112012-01-08 00:17:34 +020074 * Simply globalizes the $RTR object. The front
Derek Allard2067d1a2008-11-13 22:59:24 +000075 * loads the Router class early on so it's not available
76 * normally as other classes are.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030077 *
78 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000079 */
Andrey Andreevc123e112012-01-08 00:17:34 +020080 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
Derek Jones7576a3b2010-03-02 14:00:36 -060082 $this->config =& load_class('Config', 'core');
Andrey Andreevc123e112012-01-08 00:17:34 +020083 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000084 }
85
Derek Allard2067d1a2008-11-13 22:59:24 +000086 // --------------------------------------------------------------------
87
88 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020089 * Fetch URI String
Derek Allard2067d1a2008-11-13 22:59:24 +000090 *
Andrey Andreevcca74272012-10-28 14:43:36 +020091 * @used-by CI_Router
Andrey Andreevc123e112012-01-08 00:17:34 +020092 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000093 */
Andrey Andreevc123e112012-01-08 00:17:34 +020094 public function _fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
Andrey Andreevc123e112012-01-08 00:17:34 +020096 if (strtoupper($this->config->item('uri_protocol')) === 'AUTO')
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Phil Sturgeondda07e92011-01-31 23:26:25 +000098 // Is the request coming from the command line?
Stephen2e00c242011-08-28 10:25:40 +020099 if ($this->_is_cli_request())
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200101 $this->_set_uri_string($this->_parse_argv());
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000102 return;
103 }
104
Dan Horriganfea45ad2011-01-19 00:54:12 -0500105 // Let's try the REQUEST_URI first, this will work in most situations
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200106 if (($uri = $this->_parse_request_uri()) !== '')
Dan Horriganfea45ad2011-01-19 00:54:12 -0500107 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400108 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 return;
110 }
111
112 // Is there a PATH_INFO variable?
113 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200114 $uri = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
115 if (trim($uri, '/') !== '' && $uri !== '/'.SELF)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Andrey Andreev704f3f52012-10-31 16:50:13 +0200117 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 return;
119 }
120
121 // No PATH_INFO?... What about QUERY_STRING?
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200122 if (($uri = $this->_parse_query_string()) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200124 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 return;
126 }
127
Dan Horrigan65d603e2010-12-15 08:38:30 -0500128 // As a last ditch effort lets try using the $_GET array
Alex Bilbieed944a32012-06-02 11:07:47 +0100129 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400131 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 return;
133 }
134
135 // We've exhausted all our options...
136 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400137 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400139
140 $uri = strtoupper($this->config->item('uri_protocol'));
141
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200142 if ($uri === 'CLI')
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200144 $this->_set_uri_string($this->_parse_argv());
Pascal Kriete73598e32011-04-05 15:01:05 -0400145 return;
146 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200147 elseif (method_exists($this, ($method = '_parse_'.strtolower($uri))))
Pascal Kriete73598e32011-04-05 15:01:05 -0400148 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200149 $this->_set_uri_string($this->$method());
Pascal Kriete73598e32011-04-05 15:01:05 -0400150 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200153 $uri = isset($_SERVER[$uri]) ? $_SERVER[$uri] : @getenv($uri);
154 $this->_set_uri_string($uri);
Pascal Kriete73598e32011-04-05 15:01:05 -0400155 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400156
Pascal Kriete73598e32011-04-05 15:01:05 -0400157 // --------------------------------------------------------------------
158
159 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200160 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400161 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200162 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200163 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400164 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300165 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400166 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300167 // Filter out control characters and trim slashes
168 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 // --------------------------------------------------------------------
172
173 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200174 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200176 * Will parse REQUEST_URI and automatically detect the URI from it,
177 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200179 * @used-by CI_URI::_fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @return string
181 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200182 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300184 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 return '';
187 }
188
Andrey Andreevd4516e32012-10-31 14:44:38 +0200189 $uri = parse_url($_SERVER['REQUEST_URI']);
190 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200191 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000192
Andrey Andreevd4516e32012-10-31 14:44:38 +0200193 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
194 {
195 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
196 }
197 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
198 {
199 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
200 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200201
Dan Horriganfea45ad2011-01-19 00:54:12 -0500202 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
203 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200204 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200206 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200207 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200208 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500210 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200212 $_SERVER['QUERY_STRING'] = $query;
213 }
214
Andrey Andreevea6688b2012-10-31 21:52:11 +0200215 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400216
Andrey Andreev4b322b12012-10-26 15:37:28 +0300217 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500218 {
219 return '/';
220 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400221
Dan Horriganfea45ad2011-01-19 00:54:12 -0500222 // Do some final cleaning of the URI and return it
223 return str_replace(array('//', '../'), '/', trim($uri, '/'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300227
Stephen2e00c242011-08-28 10:25:40 +0200228 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200229 * Parse QUERY_STRING
230 *
231 * Will parse QUERY_STRING and automatically detect the URI from it.
232 *
233 * @used-by CI_URI::_fetch_uri_string()
234 * @return string
235 */
236 protected function _parse_query_string()
237 {
238 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
239
240 if (trim($uri, '/') === '')
241 {
242 return '';
243 }
244 elseif (strncmp($uri, '/', 1) === 0)
245 {
246 $uri = explode('?', $uri, 2);
247 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200248 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200249 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200250
Andrey Andreevea6688b2012-10-31 21:52:11 +0200251 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200252
253 return str_replace(array('//', '../'), '/', trim($uri, '/'));
254 }
255
256 // --------------------------------------------------------------------
257
258 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200259 * Is CLI Request?
Stephen2e00c242011-08-28 10:25:40 +0200260 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200261 * Duplicate of method from the Input class to test to see if
262 * a request was made from the command line.
Stephen2e00c242011-08-28 10:25:40 +0200263 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200264 * @see CI_Input::is_cli_request()
265 * @used-by CI_URI::_fetch_uri_string()
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300266 * @return bool
Stephen2e00c242011-08-28 10:25:40 +0200267 */
268 protected function _is_cli_request()
269 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300270 return (php_sapi_name() === 'cli') OR defined('STDIN');
Stephen2e00c242011-08-28 10:25:40 +0200271 }
272
Stephen2e00c242011-08-28 10:25:40 +0200273 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
275 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200276 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000277 *
278 * Take each command line argument and assume it is a URI segment.
279 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000280 * @return string
281 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200282 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000283 {
284 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200285 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // --------------------------------------------------------------------
289
290 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200291 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200293 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200294 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200295 * @used-by CI_Router
296 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * @return string
298 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200299 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Alex Bilbie04d43fe2012-06-02 17:59:11 +0100301 if ($str !== '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000303 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
304 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200305 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '|')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Derek Jones817163a2009-07-11 17:05:58 +0000307 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 }
310
Andrey Andreevc123e112012-01-08 00:17:34 +0200311 // Convert programatic characters to entities and return
312 return str_replace(
313 array('$', '(', ')', '%28', '%29'), // Bad
314 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
315 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
318 // --------------------------------------------------------------------
319
320 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200321 * Remove URL suffix
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200323 * Removes the suffix from the URL if needed.
Andrey Andreevc123e112012-01-08 00:17:34 +0200324 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200325 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @return void
327 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200328 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300330 $suffix = (string) $this->config->item('url_suffix');
331
332 if ($suffix !== '' && ($offset = strrpos($this->uri_string, $suffix)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300334 $this->uri_string = substr_replace($this->uri_string, '', $offset, strlen($suffix));
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200341 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200343 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200344 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200345 * @see CI_URI::$segments
346 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * @return void
348 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200349 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200351 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 // Filter segments for security
354 $val = trim($this->_filter_uri($val));
355
Alex Bilbieed944a32012-06-02 11:07:47 +0100356 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
358 $this->segments[] = $val;
359 }
360 }
361 }
362
363 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 /**
366 * Re-index Segments
367 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200368 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
369 * than 0. Doing so makes it simpler to use methods like
370 * CI_URI::segment(n) since there is a 1:1 relationship between the
371 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200373 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * @return void
375 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200376 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 array_unshift($this->segments, NULL);
379 array_unshift($this->rsegments, NULL);
380 unset($this->segments[0]);
381 unset($this->rsegments[0]);
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200387 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200389 * @see CI_URI::$segments
390 * @param int $n Index
391 * @param mixed $no_result What to return if the segment index is not found
392 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100394 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300396 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398
399 // --------------------------------------------------------------------
400
401 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200402 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200404 * Returns the re-routed URI segment (assuming routing rules are used)
405 * based on the index provided. If there is no routing, will return
406 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200408 * @see CI_URI::$rsegments
409 * @see CI_URI::segment()
410 * @param int $n Index
411 * @param mixed $no_result What to return if the segment index is not found
412 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100414 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300416 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200422 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200424 * Generates an associative array of URI data starting at the supplied
425 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
427 * example.com/user/search/name/joe/location/UK/gender/male
428 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200429 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200431 * array (
432 * name => joe
433 * location => UK
434 * gender => male
435 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200437 * @param int $n Index (default: 3)
438 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @return array
440 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200441 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Barry Mienydd671972010-10-04 16:33:58 +0200443 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300445
Timothy Warren40403d22012-04-19 16:38:50 -0400446 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200449 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200451 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
452 * segment array.
453 *
454 * @see CI_URI::uri_to_assoc()
455 * @param int $n Index (default: 3)
456 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200457 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200459 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Barry Mienydd671972010-10-04 16:33:58 +0200461 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
463
464 // --------------------------------------------------------------------
465
466 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200467 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200469 * Generates a key/value pair from the URI string or re-routed URI string.
470 *
471 * @used-by CI_URI::uri_to_assoc()
472 * @used-by CI_URI::ruri_to_assoc()
473 * @param int $n Index (default: 3)
474 * @param array $default Default values
475 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @return array
477 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200478 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 if ( ! is_numeric($n))
481 {
482 return $default;
483 }
484
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300485 in_array($which, array('segment', 'rsegment'), TRUE) OR $which = 'segment';
486
487 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300489 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491
Andrey Andreevc123e112012-01-08 00:17:34 +0200492 if ($which === 'segment')
493 {
494 $total_segments = 'total_segments';
495 $segment_array = 'segment_array';
496 }
497 else
498 {
499 $total_segments = 'total_rsegments';
500 $segment_array = 'rsegment_array';
501 }
502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if ($this->$total_segments() < $n)
504 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300505 return (count($default) === 0)
506 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100507 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509
510 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 $i = 0;
512 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300513 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 foreach ($segments as $seg)
515 {
516 if ($i % 2)
517 {
518 $retval[$lastval] = $seg;
519 }
520 else
521 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100522 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 $lastval = $seg;
524 }
525
526 $i++;
527 }
528
529 if (count($default) > 0)
530 {
531 foreach ($default as $val)
532 {
533 if ( ! array_key_exists($val, $retval))
534 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100535 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537 }
538 }
539
540 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300541 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300542 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 return $retval;
544 }
545
546 // --------------------------------------------------------------------
547
548 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200549 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200551 * Generates a URI string from an associative array.
552 *
553 * @param array $array Input array of key/value pairs
554 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200556 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100559 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200561 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 $temp[] = $val;
563 }
564
565 return implode('/', $temp);
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200571 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200573 * Fetches an URI segment with a slash.
574 *
575 * @param int $n Index
576 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @return string
578 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200579 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 return $this->_slash_segment($n, $where, 'segment');
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200587 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200589 * Fetches an URI routed segment with a slash.
590 *
591 * @param int $n Index
592 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 * @return string
594 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200595 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 return $this->_slash_segment($n, $where, 'rsegment');
598 }
599
600 // --------------------------------------------------------------------
601
602 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200603 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200605 * Fetches an URI Segment and adds a slash to it.
606 *
607 * @used-by CI_URI::slash_segment()
608 * @used-by CI_URI::slash_rsegment()
609 *
610 * @param int $n Index
611 * @param string $where Where to add the slash ('trailing' or 'leading')
612 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @return string
614 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200615 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200617 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000618
Andrey Andreevc123e112012-01-08 00:17:34 +0200619 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 $leading = '';
622 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200623 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 $trailing = '';
626 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 return $leading.$this->$which($n).$trailing;
629 }
630
631 // --------------------------------------------------------------------
632
633 /**
634 * Segment Array
635 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200636 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200638 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
640 return $this->segments;
641 }
642
643 // --------------------------------------------------------------------
644
645 /**
646 * Routed Segment Array
647 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200648 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200650 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
652 return $this->rsegments;
653 }
654
655 // --------------------------------------------------------------------
656
657 /**
658 * Total number of segments
659 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300660 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200662 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 return count($this->segments);
665 }
666
667 // --------------------------------------------------------------------
668
669 /**
670 * Total number of routed segments
671 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300672 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200674 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
676 return count($this->rsegments);
677 }
678
679 // --------------------------------------------------------------------
680
681 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200682 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200684 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200686 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 return $this->uri_string;
689 }
690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // --------------------------------------------------------------------
692
693 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200694 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 * @return string
697 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200698 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100700 return implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 }
702
703}
Derek Allard2067d1a2008-11-13 22:59:24 +0000704
705/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300706/* Location: ./system/core/URI.php */