blob: bc086d22367662371a7f701a48494fa5c17eab21 [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?
Stephen2e00c242011-08-28 10:25:40 +0200102 if ($this->_is_cli_request())
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 * 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 {
Ted Wood325e91a2013-01-07 10:52:21 -0800294 return (PHP_SAPI === '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
vlakoff661f5882013-01-10 16:26:59 +0100356 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
vlakoff661f5882013-01-10 16:26:59 +0100358 return;
359 }
360
vlakoffd1e50fa2013-01-11 15:22:17 +0100361 $slen = strlen($suffix);
vlakoff661f5882013-01-10 16:26:59 +0100362
vlakoffd1e50fa2013-01-11 15:22:17 +0100363 if (substr($this->uri_string, -$slen) === $suffix)
vlakoff661f5882013-01-10 16:26:59 +0100364 {
vlakoffd1e50fa2013-01-11 15:22:17 +0100365 $this->uri_string = substr($this->uri_string, 0, -$slen);
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200372 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200374 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200375 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200376 * @see CI_URI::$segments
377 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @return void
379 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200380 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200382 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 // Filter segments for security
385 $val = trim($this->_filter_uri($val));
386
Alex Bilbieed944a32012-06-02 11:07:47 +0100387 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 $this->segments[] = $val;
390 }
391 }
392 }
393
394 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
397 * Re-index Segments
398 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200399 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
400 * than 0. Doing so makes it simpler to use methods like
401 * CI_URI::segment(n) since there is a 1:1 relationship between the
402 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200404 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return void
406 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200407 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 array_unshift($this->segments, NULL);
410 array_unshift($this->rsegments, NULL);
411 unset($this->segments[0]);
412 unset($this->rsegments[0]);
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200418 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200420 * @see CI_URI::$segments
421 * @param int $n Index
422 * @param mixed $no_result What to return if the segment index is not found
423 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100425 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300427 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429
430 // --------------------------------------------------------------------
431
432 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200433 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200435 * Returns the re-routed URI segment (assuming routing rules are used)
436 * based on the index provided. If there is no routing, will return
437 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200439 * @see CI_URI::$rsegments
440 * @see CI_URI::segment()
441 * @param int $n Index
442 * @param mixed $no_result What to return if the segment index is not found
443 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100445 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300447 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 // --------------------------------------------------------------------
451
452 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200453 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200455 * Generates an associative array of URI data starting at the supplied
456 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 *
458 * example.com/user/search/name/joe/location/UK/gender/male
459 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200460 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200462 * array (
463 * name => joe
464 * location => UK
465 * gender => male
466 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200468 * @param int $n Index (default: 3)
469 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @return array
471 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200472 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Barry Mienydd671972010-10-04 16:33:58 +0200474 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300476
Timothy Warren40403d22012-04-19 16:38:50 -0400477 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200480 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200482 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
483 * segment array.
484 *
485 * @see CI_URI::uri_to_assoc()
486 * @param int $n Index (default: 3)
487 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200488 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200490 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Barry Mienydd671972010-10-04 16:33:58 +0200492 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
494
495 // --------------------------------------------------------------------
496
497 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200498 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200500 * Generates a key/value pair from the URI string or re-routed URI string.
501 *
502 * @used-by CI_URI::uri_to_assoc()
503 * @used-by CI_URI::ruri_to_assoc()
504 * @param int $n Index (default: 3)
505 * @param array $default Default values
506 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 * @return array
508 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200509 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 if ( ! is_numeric($n))
512 {
513 return $default;
514 }
515
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300516 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300518 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520
Daniel Hunsaker42792232012-12-31 08:57:12 -0700521 $total_segments = "total_{$which}s";
522 $segment_array = "{$which}_array";
Andrey Andreevc123e112012-01-08 00:17:34 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 if ($this->$total_segments() < $n)
525 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300526 return (count($default) === 0)
527 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100528 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
530
531 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 $i = 0;
533 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300534 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 foreach ($segments as $seg)
536 {
537 if ($i % 2)
538 {
539 $retval[$lastval] = $seg;
540 }
541 else
542 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100543 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 $lastval = $seg;
545 }
546
547 $i++;
548 }
549
550 if (count($default) > 0)
551 {
552 foreach ($default as $val)
553 {
554 if ( ! array_key_exists($val, $retval))
555 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100556 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
558 }
559 }
560
561 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300562 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300563 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 return $retval;
565 }
566
567 // --------------------------------------------------------------------
568
569 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200570 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200572 * Generates a URI string from an associative array.
573 *
574 * @param array $array Input array of key/value pairs
575 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200577 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100580 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200582 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 $temp[] = $val;
584 }
585
586 return implode('/', $temp);
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200592 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200594 * Fetches an URI segment with a slash.
595 *
596 * @param int $n Index
597 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 * @return string
599 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200600 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 return $this->_slash_segment($n, $where, 'segment');
603 }
604
605 // --------------------------------------------------------------------
606
607 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200608 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200610 * Fetches an URI routed segment with a slash.
611 *
612 * @param int $n Index
613 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @return string
615 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200616 public function slash_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 return $this->_slash_segment($n, $where, 'rsegment');
619 }
620
621 // --------------------------------------------------------------------
622
623 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200624 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200626 * Fetches an URI Segment and adds a slash to it.
627 *
628 * @used-by CI_URI::slash_segment()
629 * @used-by CI_URI::slash_rsegment()
630 *
631 * @param int $n Index
632 * @param string $where Where to add the slash ('trailing' or 'leading')
633 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @return string
635 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200636 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200638 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000639
Andrey Andreevc123e112012-01-08 00:17:34 +0200640 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 $leading = '';
643 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200644 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 $trailing = '';
647 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 return $leading.$this->$which($n).$trailing;
650 }
651
652 // --------------------------------------------------------------------
653
654 /**
655 * Segment Array
656 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200657 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200659 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 return $this->segments;
662 }
663
664 // --------------------------------------------------------------------
665
666 /**
667 * Routed Segment Array
668 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200669 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200671 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 return $this->rsegments;
674 }
675
676 // --------------------------------------------------------------------
677
678 /**
679 * Total number of segments
680 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300681 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200683 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
685 return count($this->segments);
686 }
687
688 // --------------------------------------------------------------------
689
690 /**
691 * Total number of routed segments
692 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300693 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200695 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 {
697 return count($this->rsegments);
698 }
699
700 // --------------------------------------------------------------------
701
702 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200703 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200705 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200707 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
709 return $this->uri_string;
710 }
711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 // --------------------------------------------------------------------
713
714 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200715 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 * @return string
718 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200719 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
Andrey Andreev254735e2012-11-01 21:21:20 +0200721 global $RTR;
722
Andrey Andreev5eb1cbf2013-04-08 01:20:31 +0300723 if (($dir = $RTR->directory) === '/')
Andrey Andreev254735e2012-11-01 21:21:20 +0200724 {
725 $dir = '';
726 }
727
728 return $dir.implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
730
731}
Derek Allard2067d1a2008-11-13 22:59:24 +0000732
733/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300734/* Location: ./system/core/URI.php */