blob: 309c77630d3c8a50cb7ae0215e4f004f98d3c972 [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
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200105 // Is there a PATH_INFO variable? This should be the easiest solution.
106 if (isset($_SERVER['PATH_INFO']))
107 {
108 $this->_set_uri_string($_SERVER['PATH_INFO']);
109 return;
110 }
111
112 // Let's try REQUEST_URI then, this will work in most situations
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200113 if (($uri = $this->_parse_request_uri()) !== '')
Dan Horriganfea45ad2011-01-19 00:54:12 -0500114 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400115 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 return;
117 }
118
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200119 // No REQUEST_URI either?... What about QUERY_STRING?
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200120 if (($uri = $this->_parse_query_string()) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200122 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 return;
124 }
125
Dan Horrigan65d603e2010-12-15 08:38:30 -0500126 // As a last ditch effort lets try using the $_GET array
Alex Bilbieed944a32012-06-02 11:07:47 +0100127 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400129 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return;
131 }
132
133 // We've exhausted all our options...
134 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400135 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400137
138 $uri = strtoupper($this->config->item('uri_protocol'));
139
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200140 if ($uri === 'CLI')
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200142 $this->_set_uri_string($this->_parse_argv());
Pascal Kriete73598e32011-04-05 15:01:05 -0400143 return;
144 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200145 elseif (method_exists($this, ($method = '_parse_'.strtolower($uri))))
Pascal Kriete73598e32011-04-05 15:01:05 -0400146 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200147 $this->_set_uri_string($this->$method());
Pascal Kriete73598e32011-04-05 15:01:05 -0400148 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200151 $uri = isset($_SERVER[$uri]) ? $_SERVER[$uri] : @getenv($uri);
152 $this->_set_uri_string($uri);
Pascal Kriete73598e32011-04-05 15:01:05 -0400153 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400154
Pascal Kriete73598e32011-04-05 15:01:05 -0400155 // --------------------------------------------------------------------
156
157 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200158 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400159 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200160 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200161 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400162 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300163 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400164 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300165 // Filter out control characters and trim slashes
166 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168
169 // --------------------------------------------------------------------
170
171 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200172 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200174 * Will parse REQUEST_URI and automatically detect the URI from it,
175 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200177 * @used-by CI_URI::_fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @return string
179 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200180 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300182 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 return '';
185 }
186
Andrey Andreevd4516e32012-10-31 14:44:38 +0200187 $uri = parse_url($_SERVER['REQUEST_URI']);
188 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200189 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000190
Andrey Andreevd4516e32012-10-31 14:44:38 +0200191 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
192 {
193 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
194 }
195 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
196 {
197 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
198 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200199
Dan Horriganfea45ad2011-01-19 00:54:12 -0500200 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
201 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200202 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200204 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200205 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200206 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500208 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200210 $_SERVER['QUERY_STRING'] = $query;
211 }
212
Andrey Andreevea6688b2012-10-31 21:52:11 +0200213 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400214
Andrey Andreev4b322b12012-10-26 15:37:28 +0300215 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500216 {
217 return '/';
218 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400219
Dan Horriganfea45ad2011-01-19 00:54:12 -0500220 // Do some final cleaning of the URI and return it
221 return str_replace(array('//', '../'), '/', trim($uri, '/'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223
224 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300225
Stephen2e00c242011-08-28 10:25:40 +0200226 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200227 * Parse QUERY_STRING
228 *
229 * Will parse QUERY_STRING and automatically detect the URI from it.
230 *
231 * @used-by CI_URI::_fetch_uri_string()
232 * @return string
233 */
234 protected function _parse_query_string()
235 {
236 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
237
238 if (trim($uri, '/') === '')
239 {
240 return '';
241 }
242 elseif (strncmp($uri, '/', 1) === 0)
243 {
244 $uri = explode('?', $uri, 2);
245 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200246 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200247 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200248
Andrey Andreevea6688b2012-10-31 21:52:11 +0200249 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200250
251 return str_replace(array('//', '../'), '/', trim($uri, '/'));
252 }
253
254 // --------------------------------------------------------------------
255
256 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200257 * Is CLI Request?
Stephen2e00c242011-08-28 10:25:40 +0200258 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200259 * Duplicate of method from the Input class to test to see if
260 * a request was made from the command line.
Stephen2e00c242011-08-28 10:25:40 +0200261 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200262 * @see CI_Input::is_cli_request()
263 * @used-by CI_URI::_fetch_uri_string()
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300264 * @return bool
Stephen2e00c242011-08-28 10:25:40 +0200265 */
266 protected function _is_cli_request()
267 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300268 return (php_sapi_name() === 'cli') OR defined('STDIN');
Stephen2e00c242011-08-28 10:25:40 +0200269 }
270
Stephen2e00c242011-08-28 10:25:40 +0200271 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000272
273 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200274 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000275 *
276 * Take each command line argument and assume it is a URI segment.
277 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000278 * @return string
279 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200280 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000281 {
282 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200283 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
285
286 // --------------------------------------------------------------------
287
288 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200289 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200291 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200292 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200293 * @used-by CI_Router
294 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @return string
296 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200297 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Alex Bilbie04d43fe2012-06-02 17:59:11 +0100299 if ($str !== '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000301 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
302 // 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 +0200303 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '|')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Derek Jones817163a2009-07-11 17:05:58 +0000305 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 }
308
Andrey Andreevc123e112012-01-08 00:17:34 +0200309 // Convert programatic characters to entities and return
310 return str_replace(
311 array('$', '(', ')', '%28', '%29'), // Bad
312 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
313 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
315
316 // --------------------------------------------------------------------
317
318 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200319 * Remove URL suffix
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200321 * Removes the suffix from the URL if needed.
Andrey Andreevc123e112012-01-08 00:17:34 +0200322 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200323 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @return void
325 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200326 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300328 $suffix = (string) $this->config->item('url_suffix');
329
330 if ($suffix !== '' && ($offset = strrpos($this->uri_string, $suffix)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300332 $this->uri_string = substr_replace($this->uri_string, '', $offset, strlen($suffix));
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200339 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200341 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200342 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200343 * @see CI_URI::$segments
344 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 * @return void
346 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200347 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200349 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 // Filter segments for security
352 $val = trim($this->_filter_uri($val));
353
Alex Bilbieed944a32012-06-02 11:07:47 +0100354 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 $this->segments[] = $val;
357 }
358 }
359 }
360
361 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 /**
364 * Re-index Segments
365 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200366 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
367 * than 0. Doing so makes it simpler to use methods like
368 * CI_URI::segment(n) since there is a 1:1 relationship between the
369 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200371 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @return void
373 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200374 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 array_unshift($this->segments, NULL);
377 array_unshift($this->rsegments, NULL);
378 unset($this->segments[0]);
379 unset($this->rsegments[0]);
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200385 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200387 * @see CI_URI::$segments
388 * @param int $n Index
389 * @param mixed $no_result What to return if the segment index is not found
390 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100392 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300394 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
396
397 // --------------------------------------------------------------------
398
399 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200400 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200402 * Returns the re-routed URI segment (assuming routing rules are used)
403 * based on the index provided. If there is no routing, will return
404 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200406 * @see CI_URI::$rsegments
407 * @see CI_URI::segment()
408 * @param int $n Index
409 * @param mixed $no_result What to return if the segment index is not found
410 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100412 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300414 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 // --------------------------------------------------------------------
418
419 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200420 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200422 * Generates an associative array of URI data starting at the supplied
423 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 *
425 * example.com/user/search/name/joe/location/UK/gender/male
426 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200427 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200429 * array (
430 * name => joe
431 * location => UK
432 * gender => male
433 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200435 * @param int $n Index (default: 3)
436 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @return array
438 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200439 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Barry Mienydd671972010-10-04 16:33:58 +0200441 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300443
Timothy Warren40403d22012-04-19 16:38:50 -0400444 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200447 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200449 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
450 * segment array.
451 *
452 * @see CI_URI::uri_to_assoc()
453 * @param int $n Index (default: 3)
454 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200455 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200457 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
Barry Mienydd671972010-10-04 16:33:58 +0200459 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
461
462 // --------------------------------------------------------------------
463
464 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200465 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200467 * Generates a key/value pair from the URI string or re-routed URI string.
468 *
469 * @used-by CI_URI::uri_to_assoc()
470 * @used-by CI_URI::ruri_to_assoc()
471 * @param int $n Index (default: 3)
472 * @param array $default Default values
473 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @return array
475 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200476 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 if ( ! is_numeric($n))
479 {
480 return $default;
481 }
482
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300483 in_array($which, array('segment', 'rsegment'), TRUE) OR $which = 'segment';
484
485 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300487 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
Andrey Andreevc123e112012-01-08 00:17:34 +0200490 if ($which === 'segment')
491 {
492 $total_segments = 'total_segments';
493 $segment_array = 'segment_array';
494 }
495 else
496 {
497 $total_segments = 'total_rsegments';
498 $segment_array = 'rsegment_array';
499 }
500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 if ($this->$total_segments() < $n)
502 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300503 return (count($default) === 0)
504 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100505 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507
508 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 $i = 0;
510 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300511 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 foreach ($segments as $seg)
513 {
514 if ($i % 2)
515 {
516 $retval[$lastval] = $seg;
517 }
518 else
519 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100520 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $lastval = $seg;
522 }
523
524 $i++;
525 }
526
527 if (count($default) > 0)
528 {
529 foreach ($default as $val)
530 {
531 if ( ! array_key_exists($val, $retval))
532 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100533 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535 }
536 }
537
538 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300539 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300540 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 return $retval;
542 }
543
544 // --------------------------------------------------------------------
545
546 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200547 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200549 * Generates a URI string from an associative array.
550 *
551 * @param array $array Input array of key/value pairs
552 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200554 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
556 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100557 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200559 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 $temp[] = $val;
561 }
562
563 return implode('/', $temp);
564 }
565
566 // --------------------------------------------------------------------
567
568 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200569 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200571 * Fetches an URI segment with a slash.
572 *
573 * @param int $n Index
574 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 * @return string
576 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200577 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 return $this->_slash_segment($n, $where, 'segment');
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200585 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200587 * Fetches an URI routed segment with a slash.
588 *
589 * @param int $n Index
590 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * @return string
592 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200593 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 return $this->_slash_segment($n, $where, 'rsegment');
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200601 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200603 * Fetches an URI Segment and adds a slash to it.
604 *
605 * @used-by CI_URI::slash_segment()
606 * @used-by CI_URI::slash_rsegment()
607 *
608 * @param int $n Index
609 * @param string $where Where to add the slash ('trailing' or 'leading')
610 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 * @return string
612 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200613 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200615 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000616
Andrey Andreevc123e112012-01-08 00:17:34 +0200617 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 $leading = '';
620 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200621 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 $trailing = '';
624 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 return $leading.$this->$which($n).$trailing;
627 }
628
629 // --------------------------------------------------------------------
630
631 /**
632 * Segment Array
633 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200634 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200636 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
638 return $this->segments;
639 }
640
641 // --------------------------------------------------------------------
642
643 /**
644 * Routed Segment Array
645 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200646 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200648 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 return $this->rsegments;
651 }
652
653 // --------------------------------------------------------------------
654
655 /**
656 * Total number of segments
657 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300658 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200660 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
662 return count($this->segments);
663 }
664
665 // --------------------------------------------------------------------
666
667 /**
668 * Total number of routed segments
669 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300670 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200672 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
674 return count($this->rsegments);
675 }
676
677 // --------------------------------------------------------------------
678
679 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200680 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200682 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200684 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 return $this->uri_string;
687 }
688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // --------------------------------------------------------------------
690
691 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200692 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 * @return string
695 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200696 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100698 return implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 }
700
701}
Derek Allard2067d1a2008-11-13 22:59:24 +0000702
703/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300704/* Location: ./system/core/URI.php */