blob: 6692d07a678608f1fa0bdd675da36d8f0a22a24e [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 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400117 $this->_set_uri_string($path);
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'] : '';
191 $uri = isset($uri['path']) ? $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);
207 $uri = $query[0];
208 $_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 Andreevf2b19fe2012-10-31 16:16:24 +0200215 $this->_reset_query_string();
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] : '';
248 $uri = $uri[0];
249 }
250 $this->_reset_query_string();
251
252 return str_replace(array('//', '../'), '/', trim($uri, '/'));
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Reset QUERY_STRING
259 *
260 * Re-processes QUERY_STRING to and fetches the real GET values from it.
261 * Useful for cases where we got the URI path from it's query string.
262 *
263 * @used-by CI_URI::_parse_request_uri()
264 * @used-by CI_URI::_parse_query_string()
265 * @return void
266 */
267 protected function _reset_query_string()
268 {
269 if ($_SERVER['QUERY_STRING'] === '')
270 {
271 $_GET = array();
272 }
273 else
274 {
275 parse_str($_SERVER['QUERY_STRING']);
276 }
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200282 * Is CLI Request?
Stephen2e00c242011-08-28 10:25:40 +0200283 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200284 * Duplicate of method from the Input class to test to see if
285 * a request was made from the command line.
Stephen2e00c242011-08-28 10:25:40 +0200286 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200287 * @see CI_Input::is_cli_request()
288 * @used-by CI_URI::_fetch_uri_string()
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300289 * @return bool
Stephen2e00c242011-08-28 10:25:40 +0200290 */
291 protected function _is_cli_request()
292 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300293 return (php_sapi_name() === 'cli') OR defined('STDIN');
Stephen2e00c242011-08-28 10:25:40 +0200294 }
295
Stephen2e00c242011-08-28 10:25:40 +0200296 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000297
298 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200299 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000300 *
301 * Take each command line argument and assume it is a URI segment.
302 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000303 * @return string
304 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200305 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000306 {
307 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200308 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310
311 // --------------------------------------------------------------------
312
313 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200314 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200316 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200317 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200318 * @used-by CI_Router
319 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @return string
321 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200322 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Alex Bilbie04d43fe2012-06-02 17:59:11 +0100324 if ($str !== '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000326 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
327 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
Andrey Andreev58ae9712012-06-15 23:44:48 +0300328 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', urldecode($str)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Derek Jones817163a2009-07-11 17:05:58 +0000330 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
332 }
333
Andrey Andreevc123e112012-01-08 00:17:34 +0200334 // Convert programatic characters to entities and return
335 return str_replace(
336 array('$', '(', ')', '%28', '%29'), // Bad
337 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
338 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
340
341 // --------------------------------------------------------------------
342
343 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200344 * Remove URL suffix
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200346 * Removes the suffix from the URL if needed.
Andrey Andreevc123e112012-01-08 00:17:34 +0200347 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200348 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @return void
350 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200351 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300353 $suffix = (string) $this->config->item('url_suffix');
354
355 if ($suffix !== '' && ($offset = strrpos($this->uri_string, $suffix)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300357 $this->uri_string = substr_replace($this->uri_string, '', $offset, strlen($suffix));
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200364 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200366 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200367 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200368 * @see CI_URI::$segments
369 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @return void
371 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200372 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200374 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 // Filter segments for security
377 $val = trim($this->_filter_uri($val));
378
Alex Bilbieed944a32012-06-02 11:07:47 +0100379 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $this->segments[] = $val;
382 }
383 }
384 }
385
386 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 /**
389 * Re-index Segments
390 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200391 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
392 * than 0. Doing so makes it simpler to use methods like
393 * CI_URI::segment(n) since there is a 1:1 relationship between the
394 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200396 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @return void
398 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200399 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 array_unshift($this->segments, NULL);
402 array_unshift($this->rsegments, NULL);
403 unset($this->segments[0]);
404 unset($this->rsegments[0]);
405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200410 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200412 * @see CI_URI::$segments
413 * @param int $n Index
414 * @param mixed $no_result What to return if the segment index is not found
415 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100417 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300419 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200425 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200427 * Returns the re-routed URI segment (assuming routing rules are used)
428 * based on the index provided. If there is no routing, will return
429 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200431 * @see CI_URI::$rsegments
432 * @see CI_URI::segment()
433 * @param int $n Index
434 * @param mixed $no_result What to return if the segment index is not found
435 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100437 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300439 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200445 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200447 * Generates an associative array of URI data starting at the supplied
448 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 *
450 * example.com/user/search/name/joe/location/UK/gender/male
451 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200452 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200454 * array (
455 * name => joe
456 * location => UK
457 * gender => male
458 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200460 * @param int $n Index (default: 3)
461 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * @return array
463 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200464 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Barry Mienydd671972010-10-04 16:33:58 +0200466 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300468
Timothy Warren40403d22012-04-19 16:38:50 -0400469 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200472 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200474 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
475 * segment array.
476 *
477 * @see CI_URI::uri_to_assoc()
478 * @param int $n Index (default: 3)
479 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200480 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200482 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Barry Mienydd671972010-10-04 16:33:58 +0200484 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
486
487 // --------------------------------------------------------------------
488
489 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200490 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200492 * Generates a key/value pair from the URI string or re-routed URI string.
493 *
494 * @used-by CI_URI::uri_to_assoc()
495 * @used-by CI_URI::ruri_to_assoc()
496 * @param int $n Index (default: 3)
497 * @param array $default Default values
498 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @return array
500 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200501 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if ( ! is_numeric($n))
504 {
505 return $default;
506 }
507
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300508 in_array($which, array('segment', 'rsegment'), TRUE) OR $which = 'segment';
509
510 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300512 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514
Andrey Andreevc123e112012-01-08 00:17:34 +0200515 if ($which === 'segment')
516 {
517 $total_segments = 'total_segments';
518 $segment_array = 'segment_array';
519 }
520 else
521 {
522 $total_segments = 'total_rsegments';
523 $segment_array = 'rsegment_array';
524 }
525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 if ($this->$total_segments() < $n)
527 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300528 return (count($default) === 0)
529 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100530 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532
533 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 $i = 0;
535 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300536 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 foreach ($segments as $seg)
538 {
539 if ($i % 2)
540 {
541 $retval[$lastval] = $seg;
542 }
543 else
544 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100545 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 $lastval = $seg;
547 }
548
549 $i++;
550 }
551
552 if (count($default) > 0)
553 {
554 foreach ($default as $val)
555 {
556 if ( ! array_key_exists($val, $retval))
557 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100558 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
560 }
561 }
562
563 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300564 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300565 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 return $retval;
567 }
568
569 // --------------------------------------------------------------------
570
571 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200572 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200574 * Generates a URI string from an associative array.
575 *
576 * @param array $array Input array of key/value pairs
577 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200579 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100582 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200584 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 $temp[] = $val;
586 }
587
588 return implode('/', $temp);
589 }
590
591 // --------------------------------------------------------------------
592
593 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200594 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200596 * Fetches an URI segment with a slash.
597 *
598 * @param int $n Index
599 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @return string
601 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200602 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 return $this->_slash_segment($n, $where, 'segment');
605 }
606
607 // --------------------------------------------------------------------
608
609 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200610 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200612 * Fetches an URI routed segment with a slash.
613 *
614 * @param int $n Index
615 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 * @return string
617 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200618 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 return $this->_slash_segment($n, $where, 'rsegment');
621 }
622
623 // --------------------------------------------------------------------
624
625 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200626 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200628 * Fetches an URI Segment and adds a slash to it.
629 *
630 * @used-by CI_URI::slash_segment()
631 * @used-by CI_URI::slash_rsegment()
632 *
633 * @param int $n Index
634 * @param string $where Where to add the slash ('trailing' or 'leading')
635 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @return string
637 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200638 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200640 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000641
Andrey Andreevc123e112012-01-08 00:17:34 +0200642 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 $leading = '';
645 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200646 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 $trailing = '';
649 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 return $leading.$this->$which($n).$trailing;
652 }
653
654 // --------------------------------------------------------------------
655
656 /**
657 * Segment Array
658 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200659 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200661 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
663 return $this->segments;
664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Routed Segment Array
670 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200671 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200673 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
675 return $this->rsegments;
676 }
677
678 // --------------------------------------------------------------------
679
680 /**
681 * Total number of segments
682 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300683 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200685 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 return count($this->segments);
688 }
689
690 // --------------------------------------------------------------------
691
692 /**
693 * Total number of routed segments
694 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300695 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200697 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
699 return count($this->rsegments);
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200705 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200707 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200709 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 return $this->uri_string;
712 }
713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // --------------------------------------------------------------------
715
716 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200717 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @return string
720 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200721 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100723 return implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 }
725
726}
Derek Allard2067d1a2008-11-13 22:59:24 +0000727
728/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300729/* Location: ./system/core/URI.php */