blob: 5e4c80a00270e0e1d72a34bc80c7374db1c9b59a [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 {
Ted Wood325e91a2013-01-07 10:52:21 -080097 $protocol = strtoupper($this->config->item('uri_protocol'));
98
99 if ($protocol === 'AUTO')
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Phil Sturgeondda07e92011-01-31 23:26:25 +0000101 // Is the request coming from the command line?
Andrey Andreevf964b162013-11-12 17:04:55 +0200102 if (is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200104 $this->_set_uri_string($this->_parse_argv());
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000105 return;
106 }
107
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200108 // Is there a PATH_INFO variable? This should be the easiest solution.
109 if (isset($_SERVER['PATH_INFO']))
110 {
111 $this->_set_uri_string($_SERVER['PATH_INFO']);
112 return;
113 }
114
115 // Let's try REQUEST_URI then, this will work in most situations
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200116 if (($uri = $this->_parse_request_uri()) !== '')
Dan Horriganfea45ad2011-01-19 00:54:12 -0500117 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400118 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 return;
120 }
121
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200122 // No REQUEST_URI either?... What about QUERY_STRING?
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200123 if (($uri = $this->_parse_query_string()) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200125 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 return;
127 }
128
vlakoff35672462013-02-15 01:36:04 +0100129 // As a last ditch effort let's try using the $_GET array
Alex Bilbieed944a32012-06-02 11:07:47 +0100130 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400132 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 return;
134 }
135
136 // We've exhausted all our options...
137 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400138 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400140
Ted Wood325e91a2013-01-07 10:52:21 -0800141 if ($protocol === '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 }
Ted Wood325e91a2013-01-07 10:52:21 -0800146 elseif (method_exists($this, ($method = '_parse_'.strtolower($protocol))))
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
Ted Wood325e91a2013-01-07 10:52:21 -0800152 $uri = isset($_SERVER[$protocol]) ? $_SERVER[$protocol] : @getenv($protocol);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200153 $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 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000284 *
285 * Take each command line argument and assume it is a URI segment.
286 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000287 * @return string
288 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200289 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000290 {
291 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200292 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
295 // --------------------------------------------------------------------
296
297 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200298 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200300 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200301 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200302 * @used-by CI_Router
303 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * @return string
305 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200306 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Alex Bilbie04d43fe2012-06-02 17:59:11 +0100308 if ($str !== '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000310 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
311 // 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 +0200312 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Derek Jones817163a2009-07-11 17:05:58 +0000314 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316 }
317
Andrey Andreevc123e112012-01-08 00:17:34 +0200318 // Convert programatic characters to entities and return
319 return str_replace(
320 array('$', '(', ')', '%28', '%29'), // Bad
321 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
322 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324
325 // --------------------------------------------------------------------
326
327 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200328 * Remove URL suffix
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200330 * Removes the suffix from the URL if needed.
Andrey Andreevc123e112012-01-08 00:17:34 +0200331 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200332 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @return void
334 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200335 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300337 $suffix = (string) $this->config->item('url_suffix');
338
vlakoff661f5882013-01-10 16:26:59 +0100339 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
vlakoff661f5882013-01-10 16:26:59 +0100341 return;
342 }
343
vlakoffd1e50fa2013-01-11 15:22:17 +0100344 $slen = strlen($suffix);
vlakoff661f5882013-01-10 16:26:59 +0100345
vlakoffd1e50fa2013-01-11 15:22:17 +0100346 if (substr($this->uri_string, -$slen) === $suffix)
vlakoff661f5882013-01-10 16:26:59 +0100347 {
vlakoffd1e50fa2013-01-11 15:22:17 +0100348 $this->uri_string = substr($this->uri_string, 0, -$slen);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200355 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200357 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200358 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200359 * @see CI_URI::$segments
360 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @return void
362 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200363 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200365 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 // Filter segments for security
368 $val = trim($this->_filter_uri($val));
369
Alex Bilbieed944a32012-06-02 11:07:47 +0100370 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 $this->segments[] = $val;
373 }
374 }
375 }
376
377 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 /**
380 * Re-index Segments
381 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200382 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
383 * than 0. Doing so makes it simpler to use methods like
384 * CI_URI::segment(n) since there is a 1:1 relationship between the
385 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200387 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @return void
389 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200390 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 array_unshift($this->segments, NULL);
393 array_unshift($this->rsegments, NULL);
394 unset($this->segments[0]);
395 unset($this->rsegments[0]);
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200401 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200403 * @see CI_URI::$segments
404 * @param int $n Index
405 * @param mixed $no_result What to return if the segment index is not found
406 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100408 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300410 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
412
413 // --------------------------------------------------------------------
414
415 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200416 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200418 * Returns the re-routed URI segment (assuming routing rules are used)
419 * based on the index provided. If there is no routing, will return
420 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200422 * @see CI_URI::$rsegments
423 * @see CI_URI::segment()
424 * @param int $n Index
425 * @param mixed $no_result What to return if the segment index is not found
426 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100428 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300430 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // --------------------------------------------------------------------
434
435 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200436 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200438 * Generates an associative array of URI data starting at the supplied
439 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 *
441 * example.com/user/search/name/joe/location/UK/gender/male
442 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200443 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200445 * array (
446 * name => joe
447 * location => UK
448 * gender => male
449 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200451 * @param int $n Index (default: 3)
452 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @return array
454 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200455 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Barry Mienydd671972010-10-04 16:33:58 +0200457 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300459
Timothy Warren40403d22012-04-19 16:38:50 -0400460 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200463 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200465 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
466 * segment array.
467 *
468 * @see CI_URI::uri_to_assoc()
469 * @param int $n Index (default: 3)
470 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200471 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200473 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Barry Mienydd671972010-10-04 16:33:58 +0200475 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477
478 // --------------------------------------------------------------------
479
480 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200481 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200483 * Generates a key/value pair from the URI string or re-routed URI string.
484 *
485 * @used-by CI_URI::uri_to_assoc()
486 * @used-by CI_URI::ruri_to_assoc()
487 * @param int $n Index (default: 3)
488 * @param array $default Default values
489 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @return array
491 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200492 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 if ( ! is_numeric($n))
495 {
496 return $default;
497 }
498
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300499 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300501 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
503
Daniel Hunsaker42792232012-12-31 08:57:12 -0700504 $total_segments = "total_{$which}s";
505 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 if ($this->$total_segments() < $n)
508 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300509 return (count($default) === 0)
510 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100511 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
514 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 $i = 0;
516 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300517 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 foreach ($segments as $seg)
519 {
520 if ($i % 2)
521 {
522 $retval[$lastval] = $seg;
523 }
524 else
525 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100526 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 $lastval = $seg;
528 }
529
530 $i++;
531 }
532
533 if (count($default) > 0)
534 {
535 foreach ($default as $val)
536 {
537 if ( ! array_key_exists($val, $retval))
538 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100539 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541 }
542 }
543
544 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300545 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300546 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 return $retval;
548 }
549
550 // --------------------------------------------------------------------
551
552 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200553 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200555 * Generates a URI string from an associative array.
556 *
557 * @param array $array Input array of key/value pairs
558 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200560 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100563 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200565 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 $temp[] = $val;
567 }
568
569 return implode('/', $temp);
570 }
571
572 // --------------------------------------------------------------------
573
574 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200575 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200577 * Fetches an URI segment with a slash.
578 *
579 * @param int $n Index
580 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @return string
582 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200583 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 return $this->_slash_segment($n, $where, 'segment');
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200591 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200593 * Fetches an URI routed segment with a slash.
594 *
595 * @param int $n Index
596 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 * @return string
598 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200599 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 return $this->_slash_segment($n, $where, 'rsegment');
602 }
603
604 // --------------------------------------------------------------------
605
606 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200607 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200609 * Fetches an URI Segment and adds a slash to it.
610 *
611 * @used-by CI_URI::slash_segment()
612 * @used-by CI_URI::slash_rsegment()
613 *
614 * @param int $n Index
615 * @param string $where Where to add the slash ('trailing' or 'leading')
616 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 * @return string
618 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200619 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200621 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000622
Andrey Andreevc123e112012-01-08 00:17:34 +0200623 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 $leading = '';
626 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200627 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 $trailing = '';
630 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 return $leading.$this->$which($n).$trailing;
633 }
634
635 // --------------------------------------------------------------------
636
637 /**
638 * Segment Array
639 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200640 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200642 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 return $this->segments;
645 }
646
647 // --------------------------------------------------------------------
648
649 /**
650 * Routed Segment Array
651 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200652 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200654 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 return $this->rsegments;
657 }
658
659 // --------------------------------------------------------------------
660
661 /**
662 * Total number of segments
663 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300664 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200666 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
668 return count($this->segments);
669 }
670
671 // --------------------------------------------------------------------
672
673 /**
674 * Total number of routed segments
675 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300676 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200678 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
680 return count($this->rsegments);
681 }
682
683 // --------------------------------------------------------------------
684
685 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200686 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200688 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200690 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 return $this->uri_string;
693 }
694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 // --------------------------------------------------------------------
696
697 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200698 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 * @return string
701 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200702 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Andrey Andreev254735e2012-11-01 21:21:20 +0200704 global $RTR;
705
Andrey Andreeve5403412014-01-03 14:59:17 +0200706 return ltrim($RTR->directory, '/').implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
708
709}
Derek Allard2067d1a2008-11-13 22:59:24 +0000710
711/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300712/* Location: ./system/core/URI.php */