blob: 2f6cade3471f69549f6f52beee27460ec906f56c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevc123e112012-01-08 00:17:34 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevc123e112012-01-08 00:17:34 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
Andrey Andreev92ebfb62012-05-17 12:49:24 +030025 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * URI Class
31 *
32 * Parses URIs and determines routing
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category URI
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/uri.html
39 */
40class CI_URI {
41
David Behler07b53422011-08-15 00:25:06 +020042 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020043 * List of cached URI segments
David Behler07b53422011-08-15 00:25:06 +020044 *
Andrey Andreevcca74272012-10-28 14:43:36 +020045 * @var array
David Behler07b53422011-08-15 00:25:06 +020046 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040047 public $keyval = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020050 * Current URI string
David Behler07b53422011-08-15 00:25:06 +020051 *
Andrey Andreevcca74272012-10-28 14:43:36 +020052 * @var string
David Behler07b53422011-08-15 00:25:06 +020053 */
Andrey Andreevc123e112012-01-08 00:17:34 +020054 public $uri_string;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler07b53422011-08-15 00:25:06 +020056 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020057 * List of URI segments
David Behler07b53422011-08-15 00:25:06 +020058 *
Andrey Andreevcca74272012-10-28 14:43:36 +020059 * @var array
David Behler07b53422011-08-15 00:25:06 +020060 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 public $segments = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030062
David Behler07b53422011-08-15 00:25:06 +020063 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020064 * Re-indexed list of URI segments
David Behler07b53422011-08-15 00:25:06 +020065 *
Andrey Andreevcca74272012-10-28 14:43:36 +020066 * Starts at 1 instead of 0.
67 *
68 * @var array
David Behler07b53422011-08-15 00:25:06 +020069 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040070 public $rsegments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000071
72 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020073 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000074 *
Andrey Andreevc123e112012-01-08 00:17:34 +020075 * Simply globalizes the $RTR object. The front
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * loads the Router class early on so it's not available
77 * normally as other classes are.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030078 *
79 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000080 */
Andrey Andreevc123e112012-01-08 00:17:34 +020081 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Derek Jones7576a3b2010-03-02 14:00:36 -060083 $this->config =& load_class('Config', 'core');
Andrey Andreevc123e112012-01-08 00:17:34 +020084 log_message('debug', 'URI Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
86
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
88
89 /**
Andrey Andreevcca74272012-10-28 14:43:36 +020090 * Fetch URI String
Derek Allard2067d1a2008-11-13 22:59:24 +000091 *
Andrey Andreevcca74272012-10-28 14:43:36 +020092 * @used-by CI_Router
Andrey Andreevc123e112012-01-08 00:17:34 +020093 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000094 */
Andrey Andreevc123e112012-01-08 00:17:34 +020095 public function _fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Andrey Andreevc123e112012-01-08 00:17:34 +020097 if (strtoupper($this->config->item('uri_protocol')) === 'AUTO')
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Phil Sturgeondda07e92011-01-31 23:26:25 +000099 // Is the request coming from the command line?
Stephen2e00c242011-08-28 10:25:40 +0200100 if ($this->_is_cli_request())
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200102 $this->_set_uri_string($this->_parse_argv());
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000103 return;
104 }
105
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200106 // Is there a PATH_INFO variable? This should be the easiest solution.
107 if (isset($_SERVER['PATH_INFO']))
108 {
109 $this->_set_uri_string($_SERVER['PATH_INFO']);
110 return;
111 }
112
113 // Let's try REQUEST_URI then, this will work in most situations
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200114 if (($uri = $this->_parse_request_uri()) !== '')
Dan Horriganfea45ad2011-01-19 00:54:12 -0500115 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400116 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return;
118 }
119
Andrey Andreev3b72eb52012-11-01 00:45:26 +0200120 // No REQUEST_URI either?... What about QUERY_STRING?
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200121 if (($uri = $this->_parse_query_string()) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200123 $this->_set_uri_string($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 return;
125 }
126
Dan Horrigan65d603e2010-12-15 08:38:30 -0500127 // As a last ditch effort lets try using the $_GET array
Alex Bilbieed944a32012-06-02 11:07:47 +0100128 if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Pascal Kriete73598e32011-04-05 15:01:05 -0400130 $this->_set_uri_string(key($_GET));
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 return;
132 }
133
134 // We've exhausted all our options...
135 $this->uri_string = '';
Pascal Kriete73598e32011-04-05 15:01:05 -0400136 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Pascal Kriete73598e32011-04-05 15:01:05 -0400138
139 $uri = strtoupper($this->config->item('uri_protocol'));
140
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200141 if ($uri === 'CLI')
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200143 $this->_set_uri_string($this->_parse_argv());
Pascal Kriete73598e32011-04-05 15:01:05 -0400144 return;
145 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200146 elseif (method_exists($this, ($method = '_parse_'.strtolower($uri))))
Pascal Kriete73598e32011-04-05 15:01:05 -0400147 {
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200148 $this->_set_uri_string($this->$method());
Pascal Kriete73598e32011-04-05 15:01:05 -0400149 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200152 $uri = isset($_SERVER[$uri]) ? $_SERVER[$uri] : @getenv($uri);
153 $this->_set_uri_string($uri);
Pascal Kriete73598e32011-04-05 15:01:05 -0400154 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400155
Pascal Kriete73598e32011-04-05 15:01:05 -0400156 // --------------------------------------------------------------------
157
158 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200159 * Set URI String
Pascal Kriete73598e32011-04-05 15:01:05 -0400160 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200161 * @param string $str
Andrey Andreevc123e112012-01-08 00:17:34 +0200162 * @return void
Pascal Kriete73598e32011-04-05 15:01:05 -0400163 */
Andrey Andreevd4619342012-06-14 02:27:25 +0300164 protected function _set_uri_string($str)
Pascal Kriete73598e32011-04-05 15:01:05 -0400165 {
Andrey Andreevf5f898f2012-10-23 02:13:29 +0300166 // Filter out control characters and trim slashes
167 $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // --------------------------------------------------------------------
171
172 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200173 * Parse REQUEST_URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200175 * Will parse REQUEST_URI and automatically detect the URI from it,
176 * while fixing the query string if necessary.
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 *
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200178 * @used-by CI_URI::_fetch_uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @return string
180 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200181 protected function _parse_request_uri()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300183 if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
185 return '';
186 }
187
Andrey Andreevd4516e32012-10-31 14:44:38 +0200188 $uri = parse_url($_SERVER['REQUEST_URI']);
189 $query = isset($uri['query']) ? $uri['query'] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200190 $uri = isset($uri['path']) ? rawurldecode($uri['path']) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
Andrey Andreevd4516e32012-10-31 14:44:38 +0200192 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
193 {
194 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
195 }
196 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
197 {
198 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
199 }
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200200
Dan Horriganfea45ad2011-01-19 00:54:12 -0500201 // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
202 // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200203 if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200205 $query = explode('?', $query, 2);
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200206 $uri = rawurldecode($query[0]);
Andrey Andreevd4516e32012-10-31 14:44:38 +0200207 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Dan Horriganfea45ad2011-01-19 00:54:12 -0500209 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreevd4516e32012-10-31 14:44:38 +0200211 $_SERVER['QUERY_STRING'] = $query;
212 }
213
Andrey Andreevea6688b2012-10-31 21:52:11 +0200214 parse_str($_SERVER['QUERY_STRING'], $_GET);
Eric Barnes26eebdd2011-04-17 23:45:41 -0400215
Andrey Andreev4b322b12012-10-26 15:37:28 +0300216 if ($uri === '/' OR $uri === '')
ericbarnes@ericbarnes.locale58199b2011-02-02 22:40:36 -0500217 {
218 return '/';
219 }
Eric Barnes26eebdd2011-04-17 23:45:41 -0400220
Dan Horriganfea45ad2011-01-19 00:54:12 -0500221 // Do some final cleaning of the URI and return it
222 return str_replace(array('//', '../'), '/', trim($uri, '/'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224
225 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300226
Stephen2e00c242011-08-28 10:25:40 +0200227 /**
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200228 * Parse QUERY_STRING
229 *
230 * Will parse QUERY_STRING and automatically detect the URI from it.
231 *
232 * @used-by CI_URI::_fetch_uri_string()
233 * @return string
234 */
235 protected function _parse_query_string()
236 {
237 $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
238
239 if (trim($uri, '/') === '')
240 {
241 return '';
242 }
243 elseif (strncmp($uri, '/', 1) === 0)
244 {
245 $uri = explode('?', $uri, 2);
246 $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200247 $uri = rawurldecode($uri[0]);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200248 }
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200249
Andrey Andreevea6688b2012-10-31 21:52:11 +0200250 parse_str($_SERVER['QUERY_STRING'], $_GET);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200251
252 return str_replace(array('//', '../'), '/', trim($uri, '/'));
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200258 * Is CLI Request?
Stephen2e00c242011-08-28 10:25:40 +0200259 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200260 * Duplicate of method from the Input class to test to see if
261 * a request was made from the command line.
Stephen2e00c242011-08-28 10:25:40 +0200262 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200263 * @see CI_Input::is_cli_request()
264 * @used-by CI_URI::_fetch_uri_string()
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300265 * @return bool
Stephen2e00c242011-08-28 10:25:40 +0200266 */
267 protected function _is_cli_request()
268 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300269 return (php_sapi_name() === 'cli') OR defined('STDIN');
Stephen2e00c242011-08-28 10:25:40 +0200270 }
271
Stephen2e00c242011-08-28 10:25:40 +0200272 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000273
274 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200275 * Parse CLI arguments
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000276 *
277 * Take each command line argument and assume it is a URI segment.
278 *
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000279 * @return string
280 */
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200281 protected function _parse_argv()
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000282 {
283 $args = array_slice($_SERVER['argv'], 1);
Andrey Andreevf2b19fe2012-10-31 16:16:24 +0200284 return $args ? implode('/', $args) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200290 * Filter URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200292 * Filters segments for malicious characters.
Andrey Andreevc123e112012-01-08 00:17:34 +0200293 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200294 * @used-by CI_Router
295 * @param string $str
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @return string
297 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200298 public function _filter_uri($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Alex Bilbie04d43fe2012-06-02 17:59:11 +0100300 if ($str !== '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000302 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
303 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
Andrey Andreev9dd2dbb2012-10-31 17:54:56 +0200304 if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '|')).']+$|i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Derek Jones817163a2009-07-11 17:05:58 +0000306 show_error('The URI you submitted has disallowed characters.', 400);
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308 }
309
Andrey Andreevc123e112012-01-08 00:17:34 +0200310 // Convert programatic characters to entities and return
311 return str_replace(
312 array('$', '(', ')', '%28', '%29'), // Bad
313 array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;'), // Good
314 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316
317 // --------------------------------------------------------------------
318
319 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200320 * Remove URL suffix
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200322 * Removes the suffix from the URL if needed.
Andrey Andreevc123e112012-01-08 00:17:34 +0200323 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200324 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @return void
326 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200327 public function _remove_url_suffix()
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300329 $suffix = (string) $this->config->item('url_suffix');
330
331 if ($suffix !== '' && ($offset = strrpos($this->uri_string, $suffix)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
Andrey Andreev95d78cf2012-06-16 19:54:33 +0300333 $this->uri_string = substr_replace($this->uri_string, '', $offset, strlen($suffix));
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
335 }
336
337 // --------------------------------------------------------------------
338
339 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200340 * Explode URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200342 * The individual segments will be stored in the $this->segments array.
Andrey Andreevc123e112012-01-08 00:17:34 +0200343 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200344 * @see CI_URI::$segments
345 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @return void
347 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200348 public function _explode_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200350 foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 // Filter segments for security
353 $val = trim($this->_filter_uri($val));
354
Alex Bilbieed944a32012-06-02 11:07:47 +0100355 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 $this->segments[] = $val;
358 }
359 }
360 }
361
362 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 /**
365 * Re-index Segments
366 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200367 * Re-indexes the CI_URI::$segment array so that it starts at 1 rather
368 * than 0. Doing so makes it simpler to use methods like
369 * CI_URI::segment(n) since there is a 1:1 relationship between the
370 * segment array and the actual segments.
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200372 * @used-by CI_Router
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @return void
374 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200375 public function _reindex_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 array_unshift($this->segments, NULL);
378 array_unshift($this->rsegments, NULL);
379 unset($this->segments[0]);
380 unset($this->rsegments[0]);
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200386 * Fetch URI Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200388 * @see CI_URI::$segments
389 * @param int $n Index
390 * @param mixed $no_result What to return if the segment index is not found
391 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100393 public function segment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300395 return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397
398 // --------------------------------------------------------------------
399
400 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200401 * Fetch URI "routed" Segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200403 * Returns the re-routed URI segment (assuming routing rules are used)
404 * based on the index provided. If there is no routing, will return
405 * the same result as CI_URI::segment().
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200407 * @see CI_URI::$rsegments
408 * @see CI_URI::segment()
409 * @param int $n Index
410 * @param mixed $no_result What to return if the segment index is not found
411 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100413 public function rsegment($n, $no_result = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300415 return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200421 * URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200423 * Generates an associative array of URI data starting at the supplied
424 * segment index. For example, if this is your URI:
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 *
426 * example.com/user/search/name/joe/location/UK/gender/male
427 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200428 * You can use this method to generate an array with this prototype:
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200430 * array (
431 * name => joe
432 * location => UK
433 * gender => male
434 * )
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200436 * @param int $n Index (default: 3)
437 * @param array $default Default values
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @return array
439 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200440 public function uri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Barry Mienydd671972010-10-04 16:33:58 +0200442 return $this->_uri_to_assoc($n, $default, 'segment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300444
Timothy Warren40403d22012-04-19 16:38:50 -0400445 // --------------------------------------------------------------------
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200448 * Routed URI to assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200450 * Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
451 * segment array.
452 *
453 * @see CI_URI::uri_to_assoc()
454 * @param int $n Index (default: 3)
455 * @param array $default Default values
David Behler07b53422011-08-15 00:25:06 +0200456 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200458 public function ruri_to_assoc($n = 3, $default = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Barry Mienydd671972010-10-04 16:33:58 +0200460 return $this->_uri_to_assoc($n, $default, 'rsegment');
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200466 * Internal URI-to-assoc
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200468 * Generates a key/value pair from the URI string or re-routed URI string.
469 *
470 * @used-by CI_URI::uri_to_assoc()
471 * @used-by CI_URI::ruri_to_assoc()
472 * @param int $n Index (default: 3)
473 * @param array $default Default values
474 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * @return array
476 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200477 protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 if ( ! is_numeric($n))
480 {
481 return $default;
482 }
483
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300484 in_array($which, array('segment', 'rsegment'), TRUE) OR $which = 'segment';
485
486 if (isset($this->keyval[$which], $this->keyval[$which][$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300488 return $this->keyval[$which][$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490
Andrey Andreevc123e112012-01-08 00:17:34 +0200491 if ($which === 'segment')
492 {
493 $total_segments = 'total_segments';
494 $segment_array = 'segment_array';
495 }
496 else
497 {
498 $total_segments = 'total_rsegments';
499 $segment_array = 'rsegment_array';
500 }
501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 if ($this->$total_segments() < $n)
503 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300504 return (count($default) === 0)
505 ? array()
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100506 : array_fill_keys($default, NULL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508
509 $segments = array_slice($this->$segment_array(), ($n - 1));
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 $i = 0;
511 $lastval = '';
vkeranov2b6b4302012-10-27 18:12:24 +0300512 $retval = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 foreach ($segments as $seg)
514 {
515 if ($i % 2)
516 {
517 $retval[$lastval] = $seg;
518 }
519 else
520 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100521 $retval[$seg] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 $lastval = $seg;
523 }
524
525 $i++;
526 }
527
528 if (count($default) > 0)
529 {
530 foreach ($default as $val)
531 {
532 if ( ! array_key_exists($val, $retval))
533 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100534 $retval[$val] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536 }
537 }
538
539 // Cache the array for reuse
Andrey Andreev90930422012-10-24 23:53:12 +0300540 isset($this->keyval[$which]) OR $this->keyval[$which] = array();
Andrey Andreev4a7cc762012-10-24 23:52:05 +0300541 $this->keyval[$which][$n] = $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 return $retval;
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200548 * Assoc to URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200550 * Generates a URI string from an associative array.
551 *
552 * @param array $array Input array of key/value pairs
553 * @return string URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200555 public function assoc_to_uri($array)
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
557 $temp = array();
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100558 foreach ((array) $array as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Andrey Andreeva798fdb2012-01-08 00:20:49 +0200560 $temp[] = $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 $temp[] = $val;
562 }
563
564 return implode('/', $temp);
565 }
566
567 // --------------------------------------------------------------------
568
569 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200570 * Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200572 * Fetches an URI segment with a slash.
573 *
574 * @param int $n Index
575 * @param string $where Where to add the slash ('trailing' or 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * @return string
577 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200578 public function slash_segment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 return $this->_slash_segment($n, $where, 'segment');
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200586 * Slash routed segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200588 * Fetches an URI routed 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_rsegment($n, $where = 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 return $this->_slash_segment($n, $where, 'rsegment');
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200602 * Internal Slash segment
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200604 * Fetches an URI Segment and adds a slash to it.
605 *
606 * @used-by CI_URI::slash_segment()
607 * @used-by CI_URI::slash_rsegment()
608 *
609 * @param int $n Index
610 * @param string $where Where to add the slash ('trailing' or 'leading')
611 * @param string $which Array name ('segment' or 'rsegment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 * @return string
613 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200614 protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200616 $leading = $trailing = '/';
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000617
Andrey Andreevc123e112012-01-08 00:17:34 +0200618 if ($where === 'trailing')
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 $leading = '';
621 }
Andrey Andreevc123e112012-01-08 00:17:34 +0200622 elseif ($where === 'leading')
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 $trailing = '';
625 }
Phil Sturgeon48c718c2010-12-30 23:40:02 +0000626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 return $leading.$this->$which($n).$trailing;
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Segment Array
634 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200635 * @return array CI_URI::$segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200637 public function segment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 return $this->segments;
640 }
641
642 // --------------------------------------------------------------------
643
644 /**
645 * Routed Segment Array
646 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200647 * @return array CI_URI::$rsegments
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200649 public function rsegment_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
651 return $this->rsegments;
652 }
653
654 // --------------------------------------------------------------------
655
656 /**
657 * Total number of segments
658 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300659 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200661 public function total_segments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
663 return count($this->segments);
664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Total number of routed segments
670 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300671 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200673 public function total_rsegments()
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
675 return count($this->rsegments);
676 }
677
678 // --------------------------------------------------------------------
679
680 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200681 * Fetch URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 *
Andrey Andreevcca74272012-10-28 14:43:36 +0200683 * @return string CI_URI::$uri_string
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200685 public function uri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 return $this->uri_string;
688 }
689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // --------------------------------------------------------------------
691
692 /**
Andrey Andreevcca74272012-10-28 14:43:36 +0200693 * Fetch Re-routed URI string
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 * @return string
696 */
Andrey Andreevc123e112012-01-08 00:17:34 +0200697 public function ruri_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100699 return implode('/', $this->rsegment_array());
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
701
702}
Derek Allard2067d1a2008-11-13 22:59:24 +0000703
704/* End of file URI.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300705/* Location: ./system/core/URI.php */