blob: c83b7a74fbce6d01a535aa68cacaa8a4b3ce34c1 [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 Andreevde14aa52014-01-15 15:51:08 +020073 * Permitted URI chars
74 *
75 * PCRE character group allowed in URI segments
76 *
77 * @var string
78 */
79 protected $_permitted_uri_chars;
80
81 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020082 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000083 *
Andrey Andreevc123e112012-01-08 00:17:34 +020084 * Simply globalizes the $RTR object. The front
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * loads the Router class early on so it's not available
86 * normally as other classes are.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030087 *
88 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000089 */
Andrey Andreevc123e112012-01-08 00:17:34 +020090 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Derek Jones7576a3b2010-03-02 14:00:36 -060092 $this->config =& load_class('Config', 'core');
Andrey Andreevde14aa52014-01-15 15:51:08 +020093
94 if ($this->config->item('enable_query_strings') !== TRUE OR is_cli())
95 {
96 $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars');
97 }
98
Andrey Andreevc123e112012-01-08 00:17:34 +020099 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // --------------------------------------------------------------------
103
104 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200105 * Fetch URI String
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200107 * @used-by CI_Router
Andrey Andreevc123e112012-01-08 00:17:34 +0200108 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200110 public function _fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Ted Wood325e91a2013-01-07 10:52:21 -0800112 $protocol = strtoupper($this->config->item('uri_protocol'));
113
114 if ($protocol === 'AUTO')
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Phil Sturgeondda07e92011-01-31 23:26:25 +0000116 // Is the request coming from the command line?
Andrey Andreevf964b162013-11-12 17:04:55 +0200117 if (is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200119 $this->_set_uri_string($this->_parse_argv());
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000120 return;
121 }
122
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200123 // Is there a PATH_INFO variable? This should be the easiest solution.
124 if (isset($_SERVER['PATH_INFO']))
125 {
126 $this->_set_uri_string($_SERVER['PATH_INFO']);
127 return;
128 }
129
130 // Let's try REQUEST_URI then, this will work in most situations
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200131 if (($uri = $this->_parse_request_uri()) !== '')
Dan Horriganfea45ad2011-01-19 00:54:12 -0500132 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400133 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 return;
135 }
136
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200137 // No REQUEST_URI either?... What about QUERY_STRING?
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200138 if (($uri = $this->_parse_query_string()) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200140 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 return;
142 }
143
vlakoff35672462013-02-15 01:36:04 +0100144 // As a last ditch effort let's try using the $_GET array
Alex Bilbieed944a32012-06-02 11:07:47 +0100145 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400147 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 return;
149 }
150
151 // We've exhausted all our options...
152 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400153 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400155
Ted Wood325e91a2013-01-07 10:52:21 -0800156 if ($protocol === 'CLI')
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200158 $this->_set_uri_string($this->_parse_argv());
Pascal Kriete73598e32011-04-05 15:01:05 -0400159 return;
160 }
Ted Wood325e91a2013-01-07 10:52:21 -0800161 elseif (method_exists($this, ($method = '_parse_'.strtolower($protocol))))
Pascal Kriete73598e32011-04-05 15:01:05 -0400162 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200163 $this->_set_uri_string($this->$method());
Pascal Kriete73598e32011-04-05 15:01:05 -0400164 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
Ted Wood325e91a2013-01-07 10:52:21 -0800167 $uri = isset($_SERVER[$protocol]) ? $_SERVER[$protocol] : @getenv($protocol);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200168 $this->_set_uri_string($uri);
Pascal Kriete73598e32011-04-05 15:01:05 -0400169 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400170
Pascal Kriete73598e32011-04-05 15:01:05 -0400171 // --------------------------------------------------------------------
172
173 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200174 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400175 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200176 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200177 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400178 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300179 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400180 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300181 // Filter out control characters and trim slashes
182 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184
185 // --------------------------------------------------------------------
186
187 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200188 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200190 * Will parse REQUEST_URI and automatically detect the URI from it,
191 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200193 * @used-by CI_URI::_fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @return string
195 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200196 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300198 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 return '';
201 }
202
Andrey Andreevd4516e32012-10-31 14:44:38 +0200203 $uri = parse_url($_SERVER['REQUEST_URI']);
204 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200205 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
Andrey Andreevd4516e32012-10-31 14:44:38 +0200207 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
208 {
209 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
210 }
211 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
212 {
213 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
214 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200215
Dan Horriganfea45ad2011-01-19 00:54:12 -0500216 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
217 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200218 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200220 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200221 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200222 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500224 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200226 $_SERVER['QUERY_STRING'] = $query;
227 }
228
Andrey Andreevea6688b2012-10-31 21:52:11 +0200229 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400230
Andrey Andreev4b322b12012-10-26 15:37:28 +0300231 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500232 {
233 return '/';
234 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400235
Dan Horriganfea45ad2011-01-19 00:54:12 -0500236 // Do some final cleaning of the URI and return it
CJ0bf9cfa2012-12-06 17:15:49 +0800237 return $this->_remove_relative_directory($uri);
chernjieaf3bd3e2012-12-06 12:06:50 +0800238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Remove relative directory (../) and multi slashes (///)
CJ0bf9cfa2012-12-06 17:15:49 +0800244 *
245 * Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
246 *
247 * @param string $url
248 * @return string
chernjieaf3bd3e2012-12-06 12:06:50 +0800249 */
CJ0bf9cfa2012-12-06 17:15:49 +0800250 protected function _remove_relative_directory($uri)
chernjieaf3bd3e2012-12-06 12:06:50 +0800251 {
252 $uris = array();
CJ0bf9cfa2012-12-06 17:15:49 +0800253 $tok = strtok($uri, '/');
254 while ($tok !== FALSE)
chernjieaf3bd3e2012-12-06 12:06:50 +0800255 {
CJ0bf9cfa2012-12-06 17:15:49 +0800256 if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
257 {
258 $uris[] = $tok;
259 }
chernjieaf3bd3e2012-12-06 12:06:50 +0800260 $tok = strtok('/');
261 }
262 return implode('/', $uris);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264
265 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300266
Stephen2e00c242011-08-28 10:25:40 +0200267 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200268 * Parse QUERY_STRING
269 *
270 * Will parse QUERY_STRING and automatically detect the URI from it.
271 *
272 * @used-by CI_URI::_fetch_uri_string()
273 * @return string
274 */
275 protected function _parse_query_string()
276 {
277 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
278
279 if (trim($uri, '/') === '')
280 {
281 return '';
282 }
283 elseif (strncmp($uri, '/', 1) === 0)
284 {
285 $uri = explode('?', $uri, 2);
286 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200287 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200288 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200289
Andrey Andreevea6688b2012-10-31 21:52:11 +0200290 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200291
Andrey Andreevb2280ce2012-12-06 16:19:22 +0200292 return $this->_remove_relative_directory($uri);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200293 }
294
295 // --------------------------------------------------------------------
296
297 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200298 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000299 *
300 * Take each command line argument and assume it is a URI segment.
301 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000302 * @return string
303 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200304 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000305 {
306 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200307 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309
310 // --------------------------------------------------------------------
311
312 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200313 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200315 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200316 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200317 * @used-by CI_Router
318 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return string
320 */
Andrey Andreevde14aa52014-01-15 15:51:08 +0200321 public function filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreev08fef7d2014-01-15 18:37:01 +0200323 if ( ! empty($str) && ! empty($this->_permitted_uri_chars) && ! preg_match('/^['.$this->_permitted_uri_chars.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreevde14aa52014-01-15 15:51:08 +0200325 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
Andrey Andreevc123e112012-01-08 00:17:34 +0200328 // Convert programatic characters to entities and return
329 return str_replace(
Andrey Andreevde14aa52014-01-15 15:51:08 +0200330 array('$', '(', ')', '%28', '%29'), // Bad
331 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
332 $str
333 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
335
336 // --------------------------------------------------------------------
337
338 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200339 * Remove URL suffix
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200341 * Removes the suffix from the URL if needed.
Andrey Andreevc123e112012-01-08 00:17:34 +0200342 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200343 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @return void
345 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200346 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300348 $suffix = (string) $this->config->item('url_suffix');
349
vlakoff661f5882013-01-10 16:26:59 +0100350 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
vlakoff661f5882013-01-10 16:26:59 +0100352 return;
353 }
354
vlakoffd1e50fa2013-01-11 15:22:17 +0100355 $slen = strlen($suffix);
vlakoff661f5882013-01-10 16:26:59 +0100356
vlakoffd1e50fa2013-01-11 15:22:17 +0100357 if (substr($this->uri_string, -$slen) === $suffix)
vlakoff661f5882013-01-10 16:26:59 +0100358 {
vlakoffd1e50fa2013-01-11 15:22:17 +0100359 $this->uri_string = substr($this->uri_string, 0, -$slen);
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200366 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200368 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200369 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200370 * @see CI_URI::$segments
371 * @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 _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200376 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 // Filter segments for security
Andrey Andreevde14aa52014-01-15 15:51:08 +0200379 $val = trim($this->filter_uri($val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000380
Alex Bilbieed944a32012-06-02 11:07:47 +0100381 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 $this->segments[] = $val;
384 }
385 }
386 }
387
388 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
391 * Re-index Segments
392 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200393 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
394 * than 0. Doing so makes it simpler to use methods like
395 * CI_URI::segment(n) since there is a 1:1 relationship between the
396 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200398 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @return void
400 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200401 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 array_unshift($this->segments, NULL);
404 array_unshift($this->rsegments, NULL);
405 unset($this->segments[0]);
406 unset($this->rsegments[0]);
407 }
408
409 // --------------------------------------------------------------------
410
411 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200412 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200414 * @see CI_URI::$segments
415 * @param int $n Index
416 * @param mixed $no_result What to return if the segment index is not found
417 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100419 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300421 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 // --------------------------------------------------------------------
425
426 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200427 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200429 * Returns the re-routed URI segment (assuming routing rules are used)
430 * based on the index provided. If there is no routing, will return
431 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200433 * @see CI_URI::$rsegments
434 * @see CI_URI::segment()
435 * @param int $n Index
436 * @param mixed $no_result What to return if the segment index is not found
437 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100439 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300441 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443
444 // --------------------------------------------------------------------
445
446 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200447 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200449 * Generates an associative array of URI data starting at the supplied
450 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 *
452 * example.com/user/search/name/joe/location/UK/gender/male
453 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200454 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200456 * array (
457 * name => joe
458 * location => UK
459 * gender => male
460 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200462 * @param int $n Index (default: 3)
463 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @return array
465 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200466 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Barry Mienydd671972010-10-04 16:33:58 +0200468 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300470
Timothy Warren40403d22012-04-19 16:38:50 -0400471 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200474 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200476 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
477 * segment array.
478 *
479 * @see CI_URI::uri_to_assoc()
480 * @param int $n Index (default: 3)
481 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200482 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200484 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Barry Mienydd671972010-10-04 16:33:58 +0200486 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 // --------------------------------------------------------------------
490
491 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200492 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200494 * Generates a key/value pair from the URI string or re-routed URI string.
495 *
496 * @used-by CI_URI::uri_to_assoc()
497 * @used-by CI_URI::ruri_to_assoc()
498 * @param int $n Index (default: 3)
499 * @param array $default Default values
500 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 * @return array
502 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200503 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 if ( ! is_numeric($n))
506 {
507 return $default;
508 }
509
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300510 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300512 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514
Daniel Hunsaker42792232012-12-31 08:57:12 -0700515 $total_segments = "total_{$which}s";
516 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 if ($this->$total_segments() < $n)
519 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300520 return (count($default) === 0)
521 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100522 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524
525 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 $i = 0;
527 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300528 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 foreach ($segments as $seg)
530 {
531 if ($i % 2)
532 {
533 $retval[$lastval] = $seg;
534 }
535 else
536 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100537 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 $lastval = $seg;
539 }
540
541 $i++;
542 }
543
544 if (count($default) > 0)
545 {
546 foreach ($default as $val)
547 {
548 if ( ! array_key_exists($val, $retval))
549 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100550 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552 }
553 }
554
555 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300556 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300557 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 return $retval;
559 }
560
561 // --------------------------------------------------------------------
562
563 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200564 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200566 * Generates a URI string from an associative array.
567 *
568 * @param array $array Input array of key/value pairs
569 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200571 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100574 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200576 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 $temp[] = $val;
578 }
579
580 return implode('/', $temp);
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200586 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200588 * Fetches an URI segment with a slash.
589 *
590 * @param int $n Index
591 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 * @return string
593 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200594 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 return $this->_slash_segment($n, $where, 'segment');
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200602 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200604 * Fetches an URI routed segment with a slash.
605 *
606 * @param int $n Index
607 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 * @return string
609 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200610 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 return $this->_slash_segment($n, $where, 'rsegment');
613 }
614
615 // --------------------------------------------------------------------
616
617 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200618 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200620 * Fetches an URI Segment and adds a slash to it.
621 *
622 * @used-by CI_URI::slash_segment()
623 * @used-by CI_URI::slash_rsegment()
624 *
625 * @param int $n Index
626 * @param string $where Where to add the slash ('trailing' or 'leading')
627 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @return string
629 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200630 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200632 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000633
Andrey Andreevc123e112012-01-08 00:17:34 +0200634 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 $leading = '';
637 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200638 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 $trailing = '';
641 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 return $leading.$this->$which($n).$trailing;
644 }
645
646 // --------------------------------------------------------------------
647
648 /**
649 * Segment Array
650 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200651 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200653 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 return $this->segments;
656 }
657
658 // --------------------------------------------------------------------
659
660 /**
661 * Routed Segment Array
662 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200663 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200665 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
667 return $this->rsegments;
668 }
669
670 // --------------------------------------------------------------------
671
672 /**
673 * Total number of segments
674 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300675 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200677 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 return count($this->segments);
680 }
681
682 // --------------------------------------------------------------------
683
684 /**
685 * Total number of routed segments
686 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300687 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200689 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
691 return count($this->rsegments);
692 }
693
694 // --------------------------------------------------------------------
695
696 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200697 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200699 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200701 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
703 return $this->uri_string;
704 }
705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 // --------------------------------------------------------------------
707
708 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200709 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 * @return string
712 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200713 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
Andrey Andreev254735e2012-11-01 21:21:20 +0200715 global $RTR;
716
Andrey Andreeve5403412014-01-03 14:59:17 +0200717 return ltrim($RTR->directory, '/').implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 }
719
720}
Derek Allard2067d1a2008-11-13 22:59:24 +0000721
722/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300723/* Location: ./system/core/URI.php */