blob: bb292c224b91a10c34f453aab82de76f2e6bc359 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevc123e112012-01-08 00:17:34 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevc123e112012-01-08 00:17:34 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * URI Class
31 *
32 * Parses URIs and determines routing
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/uri.html
39 */
40class CI_URI {
41
David Behler07b53422011-08-15 00:25:06 +020042 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020043 * List of cached URI segments
David Behler07b53422011-08-15 00:25:06 +020044 *
Andrey Andreevcca74272012-10-28 14:43:36 +020045 * @var array
David Behler07b53422011-08-15 00:25:06 +020046 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040047 public $keyval = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020050 * Current URI string
David Behler07b53422011-08-15 00:25:06 +020051 *
Andrey Andreevcca74272012-10-28 14:43:36 +020052 * @var string
David Behler07b53422011-08-15 00:25:06 +020053 */
Andrey Andreevc123e112012-01-08 00:17:34 +020054 public $uri_string;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler07b53422011-08-15 00:25:06 +020056 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020057 * List of URI segments
David Behler07b53422011-08-15 00:25:06 +020058 *
Andrey Andreevcca74272012-10-28 14:43:36 +020059 * @var array
David Behler07b53422011-08-15 00:25:06 +020060 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030062
David Behler07b53422011-08-15 00:25:06 +020063 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020064 * Re-indexed list of URI segments
David Behler07b53422011-08-15 00:25:06 +020065 *
Andrey Andreevcca74272012-10-28 14:43:36 +020066 * Starts at 1 instead of 0.
67 *
68 * @var array
David Behler07b53422011-08-15 00:25:06 +020069 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040070 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000071
72 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020073 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000074 *
Andrey Andreevc123e112012-01-08 00:17:34 +020075 * Simply globalizes the $RTR object. The front
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * loads the Router class early on so it's not available
77 * normally as other classes are.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030078 *
79 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000080 */
Andrey Andreevc123e112012-01-08 00:17:34 +020081 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Derek Jones7576a3b2010-03-02 14:00:36 -060083 $this->config =& load_class('Config', 'core');
Andrey Andreevc123e112012-01-08 00:17:34 +020084 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
86
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
88
89 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020090 * Fetch URI String
Derek Allard2067d1a2008-11-13 22:59:24 +000091 *
Andrey Andreevcca74272012-10-28 14:43:36 +020092 * @used-by CI_Router
Andrey Andreevc123e112012-01-08 00:17:34 +020093 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000094 */
Andrey Andreevc123e112012-01-08 00:17:34 +020095 public function _fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Andrey Andreevc123e112012-01-08 00:17:34 +020097 if (strtoupper($this->config->item('uri_protocol')) === 'AUTO')
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Phil Sturgeondda07e92011-01-31 23:26:25 +000099 // Is the request coming from the command line?
Stephen2e00c242011-08-28 10:25:40 +0200100 if ($this->_is_cli_request())
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200102 $this->_set_uri_string($this->_parse_argv());
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000103 return;
104 }
105
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200106 // Is there a PATH_INFO variable? This should be the easiest solution.
107 if (isset($_SERVER['PATH_INFO']))
108 {
109 $this->_set_uri_string($_SERVER['PATH_INFO']);
110 return;
111 }
112
113 // Let's try REQUEST_URI then, this will work in most situations
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200114 if (($uri = $this->_parse_request_uri()) !== '')
Dan Horriganfea45ad2011-01-19 00:54:12 -0500115 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400116 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return;
118 }
119
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200120 // No REQUEST_URI either?... What about QUERY_STRING?
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200121 if (($uri = $this->_parse_query_string()) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200123 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 return;
125 }
126
Dan Horrigan65d603e2010-12-15 08:38:30 -0500127 // As a last ditch effort lets try using the $_GET array
Alex Bilbieed944a32012-06-02 11:07:47 +0100128 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400130 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 return;
132 }
133
134 // We've exhausted all our options...
135 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400136 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400138
139 $uri = strtoupper($this->config->item('uri_protocol'));
140
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200141 if ($uri === 'CLI')
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200143 $this->_set_uri_string($this->_parse_argv());
Pascal Kriete73598e32011-04-05 15:01:05 -0400144 return;
145 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200146 elseif (method_exists($this, ($method = '_parse_'.strtolower($uri))))
Pascal Kriete73598e32011-04-05 15:01:05 -0400147 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200148 $this->_set_uri_string($this->$method());
Pascal Kriete73598e32011-04-05 15:01:05 -0400149 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200152 $uri = isset($_SERVER[$uri]) ? $_SERVER[$uri] : @getenv($uri);
153 $this->_set_uri_string($uri);
Pascal Kriete73598e32011-04-05 15:01:05 -0400154 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400155
Pascal Kriete73598e32011-04-05 15:01:05 -0400156 // --------------------------------------------------------------------
157
158 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200159 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400160 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200161 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200162 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400163 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300164 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400165 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300166 // Filter out control characters and trim slashes
167 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // --------------------------------------------------------------------
171
172 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200173 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200175 * Will parse REQUEST_URI and automatically detect the URI from it,
176 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200178 * @used-by CI_URI::_fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @return string
180 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200181 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300183 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
185 return '';
186 }
187
Andrey Andreevd4516e32012-10-31 14:44:38 +0200188 $uri = parse_url($_SERVER['REQUEST_URI']);
189 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200190 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
Andrey Andreevd4516e32012-10-31 14:44:38 +0200192 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
193 {
194 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
195 }
196 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
197 {
198 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
199 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200200
Dan Horriganfea45ad2011-01-19 00:54:12 -0500201 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
202 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200203 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200205 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200206 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200207 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500209 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200211 $_SERVER['QUERY_STRING'] = $query;
212 }
213
Andrey Andreevea6688b2012-10-31 21:52:11 +0200214 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400215
Andrey Andreev4b322b12012-10-26 15:37:28 +0300216 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500217 {
218 return '/';
219 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400220
Dan Horriganfea45ad2011-01-19 00:54:12 -0500221 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800222 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Remove relative directory (../) and multi slashes (///)
CJ0bf9cfa2012-12-06 17:15:49 +0800229 *
230 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
231 *
232 * @param string $url
233 * @return string
chernjieaf3bd3e2012-12-06 12:06:50 +0800234 */
CJ0bf9cfa2012-12-06 17:15:49 +0800235 protected function _remove_relative_directory($uri)
chernjieaf3bd3e2012-12-06 12:06:50 +0800236 {
237 $uris = array();
CJ0bf9cfa2012-12-06 17:15:49 +0800238 $tok = strtok($uri, '/');
239 while ($tok !== FALSE)
chernjieaf3bd3e2012-12-06 12:06:50 +0800240 {
CJ0bf9cfa2012-12-06 17:15:49 +0800241 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
242 {
243 $uris[] = $tok;
244 }
chernjieaf3bd3e2012-12-06 12:06:50 +0800245 $tok = strtok('/');
246 }
247 return implode('/', $uris);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
250 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300251
Stephen2e00c242011-08-28 10:25:40 +0200252 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200253 * Parse QUERY_STRING
254 *
255 * Will parse QUERY_STRING and automatically detect the URI from it.
256 *
257 * @used-by CI_URI::_fetch_uri_string()
258 * @return string
259 */
260 protected function _parse_query_string()
261 {
262 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
263
264 if (trim($uri, '/') === '')
265 {
266 return '';
267 }
268 elseif (strncmp($uri, '/', 1) === 0)
269 {
270 $uri = explode('?', $uri, 2);
271 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200272 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200273 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200274
Andrey Andreevea6688b2012-10-31 21:52:11 +0200275 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200276
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200277 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200278 }
279
280 // --------------------------------------------------------------------
281
282 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200283 * Is CLI Request?
Stephen2e00c242011-08-28 10:25:40 +0200284 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200285 * Duplicate of method from the Input class to test to see if
286 * a request was made from the command line.
Stephen2e00c242011-08-28 10:25:40 +0200287 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200288 * @see CI_Input::is_cli_request()
289 * @used-by CI_URI::_fetch_uri_string()
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300290 * @return bool
Stephen2e00c242011-08-28 10:25:40 +0200291 */
292 protected function _is_cli_request()
293 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300294 return (php_sapi_name() === 'cli') OR defined('STDIN');
Stephen2e00c242011-08-28 10:25:40 +0200295 }
296
Stephen2e00c242011-08-28 10:25:40 +0200297 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000298
299 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200300 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000301 *
302 * Take each command line argument and assume it is a URI segment.
303 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000304 * @return string
305 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200306 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000307 {
308 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200309 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311
312 // --------------------------------------------------------------------
313
314 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200315 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200317 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200318 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200319 * @used-by CI_Router
320 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * @return string
322 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200323 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Alex Bilbie04d43fe2012-06-02 17:59:11 +0100325 if ($str !== '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000327 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
328 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
Andrey Andreevc3751f82012-11-02 16:50:00 +0200329 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Derek Jones817163a2009-07-11 17:05:58 +0000331 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
333 }
334
Andrey Andreevc123e112012-01-08 00:17:34 +0200335 // Convert programatic characters to entities and return
336 return str_replace(
337 array('$', '(', ')', '%28', '%29'), // Bad
338 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
339 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200345 * Remove URL suffix
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200347 * Removes the suffix from the URL if needed.
Andrey Andreevc123e112012-01-08 00:17:34 +0200348 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200349 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return void
351 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200352 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300354 $suffix = (string) $this->config->item('url_suffix');
355
356 if ($suffix !== '' && ($offset = strrpos($this->uri_string, $suffix)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300358 $this->uri_string = substr_replace($this->uri_string, '', $offset, strlen($suffix));
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200365 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200367 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200368 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200369 * @see CI_URI::$segments
370 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @return void
372 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200373 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200375 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 // Filter segments for security
378 $val = trim($this->_filter_uri($val));
379
Alex Bilbieed944a32012-06-02 11:07:47 +0100380 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 $this->segments[] = $val;
383 }
384 }
385 }
386
387 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
390 * Re-index Segments
391 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200392 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
393 * than 0. Doing so makes it simpler to use methods like
394 * CI_URI::segment(n) since there is a 1:1 relationship between the
395 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200397 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @return void
399 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200400 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 array_unshift($this->segments, NULL);
403 array_unshift($this->rsegments, NULL);
404 unset($this->segments[0]);
405 unset($this->rsegments[0]);
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200411 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200413 * @see CI_URI::$segments
414 * @param int $n Index
415 * @param mixed $no_result What to return if the segment index is not found
416 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100418 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300420 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200426 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200428 * Returns the re-routed URI segment (assuming routing rules are used)
429 * based on the index provided. If there is no routing, will return
430 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200432 * @see CI_URI::$rsegments
433 * @see CI_URI::segment()
434 * @param int $n Index
435 * @param mixed $no_result What to return if the segment index is not found
436 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100438 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300440 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
443 // --------------------------------------------------------------------
444
445 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200446 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200448 * Generates an associative array of URI data starting at the supplied
449 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 *
451 * example.com/user/search/name/joe/location/UK/gender/male
452 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200453 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200455 * array (
456 * name => joe
457 * location => UK
458 * gender => male
459 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200461 * @param int $n Index (default: 3)
462 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @return array
464 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200465 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Barry Mienydd671972010-10-04 16:33:58 +0200467 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300469
Timothy Warren40403d22012-04-19 16:38:50 -0400470 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200473 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200475 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
476 * segment array.
477 *
478 * @see CI_URI::uri_to_assoc()
479 * @param int $n Index (default: 3)
480 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200481 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200483 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Barry Mienydd671972010-10-04 16:33:58 +0200485 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 // --------------------------------------------------------------------
489
490 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200491 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200493 * Generates a key/value pair from the URI string or re-routed URI string.
494 *
495 * @used-by CI_URI::uri_to_assoc()
496 * @used-by CI_URI::ruri_to_assoc()
497 * @param int $n Index (default: 3)
498 * @param array $default Default values
499 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @return array
501 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200502 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 if ( ! is_numeric($n))
505 {
506 return $default;
507 }
508
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300509 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300511 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
Daniel Hunsaker42792232012-12-31 08:57:12 -0700514 $total_segments = "total_{$which}s";
515 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 if ($this->$total_segments() < $n)
518 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300519 return (count($default) === 0)
520 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100521 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
523
524 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 $i = 0;
526 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300527 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 foreach ($segments as $seg)
529 {
530 if ($i % 2)
531 {
532 $retval[$lastval] = $seg;
533 }
534 else
535 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100536 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 $lastval = $seg;
538 }
539
540 $i++;
541 }
542
543 if (count($default) > 0)
544 {
545 foreach ($default as $val)
546 {
547 if ( ! array_key_exists($val, $retval))
548 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100549 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
551 }
552 }
553
554 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300555 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300556 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 return $retval;
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200563 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200565 * Generates a URI string from an associative array.
566 *
567 * @param array $array Input array of key/value pairs
568 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200570 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
572 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100573 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200575 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 $temp[] = $val;
577 }
578
579 return implode('/', $temp);
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200585 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200587 * Fetches an URI 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_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 return $this->_slash_segment($n, $where, 'segment');
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200601 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200603 * Fetches an URI routed segment with a slash.
604 *
605 * @param int $n Index
606 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 * @return string
608 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200609 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 return $this->_slash_segment($n, $where, 'rsegment');
612 }
613
614 // --------------------------------------------------------------------
615
616 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200617 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200619 * Fetches an URI Segment and adds a slash to it.
620 *
621 * @used-by CI_URI::slash_segment()
622 * @used-by CI_URI::slash_rsegment()
623 *
624 * @param int $n Index
625 * @param string $where Where to add the slash ('trailing' or 'leading')
626 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @return string
628 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200629 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200631 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000632
Andrey Andreevc123e112012-01-08 00:17:34 +0200633 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 $leading = '';
636 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200637 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 $trailing = '';
640 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 return $leading.$this->$which($n).$trailing;
643 }
644
645 // --------------------------------------------------------------------
646
647 /**
648 * Segment Array
649 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200650 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200652 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
654 return $this->segments;
655 }
656
657 // --------------------------------------------------------------------
658
659 /**
660 * Routed Segment Array
661 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200662 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200664 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
666 return $this->rsegments;
667 }
668
669 // --------------------------------------------------------------------
670
671 /**
672 * Total number of segments
673 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300674 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200676 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
678 return count($this->segments);
679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Total number of routed segments
685 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300686 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200688 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
690 return count($this->rsegments);
691 }
692
693 // --------------------------------------------------------------------
694
695 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200696 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200698 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200700 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
702 return $this->uri_string;
703 }
704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // --------------------------------------------------------------------
706
707 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200708 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 * @return string
711 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200712 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
Andrey Andreev254735e2012-11-01 21:21:20 +0200714 global $RTR;
715
716 if (($dir = $RTR->fetch_directory()) === '/')
717 {
718 $dir = '';
719 }
720
721 return $dir.implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 }
723
724}
Derek Allard2067d1a2008-11-13 22:59:24 +0000725
726/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300727/* Location: ./system/core/URI.php */