Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 27 | /** |
| 28 | * URI Class |
| 29 | * |
| 30 | * Parses URIs and determines routing |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category URI |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/libraries/uri.html |
| 37 | */ |
| 38 | class CI_URI { |
| 39 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 40 | /** |
| 41 | * List of cached uri segments |
| 42 | * |
| 43 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 44 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 45 | public $keyval = array(); |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 46 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 47 | /** |
| 48 | * Current uri string |
| 49 | * |
| 50 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 51 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 52 | public $uri_string; |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 53 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 54 | /** |
| 55 | * List of uri segments |
| 56 | * |
| 57 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 58 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 59 | public $segments = array(); |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 60 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 61 | /** |
| 62 | * Re-indexed list of uri segments |
| 63 | * Starts at 1 instead of 0 |
| 64 | * |
| 65 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 66 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 67 | public $rsegments = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Constructor |
| 71 | * |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 72 | * Simply globalizes the $RTR object. The front |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | * loads the Router class early on so it's not available |
| 74 | * normally as other classes are. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 76 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | { |
Derek Jones | 7576a3b | 2010-03-02 14:00:36 -0600 | [diff] [blame] | 78 | $this->config =& load_class('Config', 'core'); |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 79 | log_message('debug', 'URI Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | // -------------------------------------------------------------------- |
| 83 | |
| 84 | /** |
| 85 | * Get the URI String |
| 86 | * |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 87 | * Called by CI_Router |
| 88 | * |
| 89 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 91 | public function _fetch_uri_string() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 93 | if (strtoupper($this->config->item('uri_protocol')) === 'AUTO') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
Phil Sturgeon | dda07e9 | 2011-01-31 23:26:25 +0000 | [diff] [blame] | 95 | // Is the request coming from the command line? |
Stephen | 2e00c24 | 2011-08-28 10:25:40 +0200 | [diff] [blame] | 96 | if ($this->_is_cli_request()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | { |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 98 | $this->_set_uri_string($this->_parse_cli_args()); |
Phil Sturgeon | 48c718c | 2010-12-30 23:40:02 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 102 | // Let's try the REQUEST_URI first, this will work in most situations |
| 103 | if ($uri = $this->_detect_uri()) |
| 104 | { |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 105 | $this->_set_uri_string($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
| 109 | // Is there a PATH_INFO variable? |
| 110 | // Note: some servers seem to have trouble with getenv() so we'll test it two ways |
| 111 | $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO'); |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 112 | if (trim($path, '/') != '' && $path !== '/'.SELF) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | { |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 114 | $this->_set_uri_string($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | // No PATH_INFO?... What about QUERY_STRING? |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 119 | $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | if (trim($path, '/') != '') |
| 121 | { |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 122 | $this->_set_uri_string($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
Dan Horrigan | 65d603e | 2010-12-15 08:38:30 -0500 | [diff] [blame] | 126 | // As a last ditch effort lets try using the $_GET array |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 127 | if (is_array($_GET) && count($_GET) === 1 && trim(key($_GET), '/') != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | { |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 129 | $this->_set_uri_string(key($_GET)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | // We've exhausted all our options... |
| 134 | $this->uri_string = ''; |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 135 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | } |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 137 | |
| 138 | $uri = strtoupper($this->config->item('uri_protocol')); |
| 139 | |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 140 | if ($uri === 'REQUEST_URI') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 142 | $this->_set_uri_string($this->_detect_uri()); |
| 143 | return; |
| 144 | } |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 145 | elseif ($uri === 'CLI') |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 146 | { |
| 147 | $this->_set_uri_string($this->_parse_cli_args()); |
| 148 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 151 | $path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri); |
| 152 | $this->_set_uri_string($path); |
| 153 | } |
Eric Barnes | 26eebdd | 2011-04-17 23:45:41 -0400 | [diff] [blame] | 154 | |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 155 | // -------------------------------------------------------------------- |
| 156 | |
| 157 | /** |
| 158 | * Set the URI String |
| 159 | * |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 160 | * @param string |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 161 | * @return void |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 162 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 163 | public function _set_uri_string($str) |
Pascal Kriete | 73598e3 | 2011-04-05 15:01:05 -0400 | [diff] [blame] | 164 | { |
| 165 | // Filter out control characters |
| 166 | $str = remove_invisible_characters($str, FALSE); |
Eric Barnes | 26eebdd | 2011-04-17 23:45:41 -0400 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | // If the URI contains only a slash we'll kill it |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 169 | $this->uri_string = ($str === '/') ? '' : $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 175 | * Detects the URI |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | * |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 177 | * This function will detect the URI automatically and fix the query string |
| 178 | * if necessary. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | * @return string |
| 181 | */ |
Paul | f7345e4 | 2011-08-27 06:51:16 +1200 | [diff] [blame] | 182 | protected function _detect_uri() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | { |
Eric Barnes | 26eebdd | 2011-04-17 23:45:41 -0400 | [diff] [blame] | 184 | if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | { |
| 186 | return ''; |
| 187 | } |
| 188 | |
Michiel Vugteveen | 0609d58 | 2012-01-08 13:26:17 +0100 | [diff] [blame] | 189 | $uri = $_SERVER['REQUEST_URI']; |
| 190 | if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 192 | $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); |
| 193 | } |
Michiel Vugteveen | 0609d58 | 2012-01-08 13:26:17 +0100 | [diff] [blame] | 194 | elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 195 | { |
| 196 | $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 199 | // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct |
| 200 | // URI is found, and also fixes the QUERY_STRING server var and $_GET array. |
| 201 | if (strncmp($uri, '?/', 2) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | { |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 203 | $uri = substr($uri, 2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | } |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 205 | $parts = preg_split('#\?#i', $uri, 2); |
| 206 | $uri = $parts[0]; |
| 207 | if (isset($parts[1])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | { |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 209 | $_SERVER['QUERY_STRING'] = $parts[1]; |
Dan Horrigan | 65d603e | 2010-12-15 08:38:30 -0500 | [diff] [blame] | 210 | parse_str($_SERVER['QUERY_STRING'], $_GET); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | } |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 212 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | { |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 214 | $_SERVER['QUERY_STRING'] = ''; |
| 215 | $_GET = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | } |
Eric Barnes | 26eebdd | 2011-04-17 23:45:41 -0400 | [diff] [blame] | 217 | |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 218 | if ($uri == '/' OR empty($uri)) |
ericbarnes@ericbarnes.local | e58199b | 2011-02-02 22:40:36 -0500 | [diff] [blame] | 219 | { |
| 220 | return '/'; |
| 221 | } |
Eric Barnes | 26eebdd | 2011-04-17 23:45:41 -0400 | [diff] [blame] | 222 | |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 223 | $uri = parse_url($uri, PHP_URL_PATH); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | |
Dan Horrigan | fea45ad | 2011-01-19 00:54:12 -0500 | [diff] [blame] | 225 | // Do some final cleaning of the URI and return it |
| 226 | return str_replace(array('//', '../'), '/', trim($uri, '/')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // -------------------------------------------------------------------- |
Stephen | 2e00c24 | 2011-08-28 10:25:40 +0200 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Is cli Request? |
| 233 | * |
| 234 | * Duplicate of function from the Input class to test to see if a request was made from the command line |
| 235 | * |
| 236 | * @return boolean |
| 237 | */ |
| 238 | protected function _is_cli_request() |
| 239 | { |
| 240 | return (php_sapi_name() == 'cli') OR defined('STDIN'); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | |
| 246 | /** |
Phil Sturgeon | 48c718c | 2010-12-30 23:40:02 +0000 | [diff] [blame] | 247 | * Parse cli arguments |
| 248 | * |
| 249 | * Take each command line argument and assume it is a URI segment. |
| 250 | * |
Phil Sturgeon | 48c718c | 2010-12-30 23:40:02 +0000 | [diff] [blame] | 251 | * @return string |
| 252 | */ |
Paul | f7345e4 | 2011-08-27 06:51:16 +1200 | [diff] [blame] | 253 | protected function _parse_cli_args() |
Phil Sturgeon | 48c718c | 2010-12-30 23:40:02 +0000 | [diff] [blame] | 254 | { |
| 255 | $args = array_slice($_SERVER['argv'], 1); |
Phil Sturgeon | 48c718c | 2010-12-30 23:40:02 +0000 | [diff] [blame] | 256 | return $args ? '/' . implode('/', $args) : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // -------------------------------------------------------------------- |
| 260 | |
| 261 | /** |
| 262 | * Filter segments for malicious characters |
| 263 | * |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 264 | * Called by CI_Router |
| 265 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | * @param string |
| 267 | * @return string |
| 268 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 269 | public function _filter_uri($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | { |
| 271 | if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE) |
| 272 | { |
Derek Jones | f0a9b33 | 2009-07-29 14:19:18 +0000 | [diff] [blame] | 273 | // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards |
| 274 | // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 275 | if ( ! preg_match('|^['.str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-')).']+$|i', $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 277 | show_error('The URI you submitted has disallowed characters.', 400); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 281 | // Convert programatic characters to entities and return |
| 282 | return str_replace( |
| 283 | array('$', '(', ')', '%28', '%29'), // Bad |
| 284 | array('$', '(', ')', '(', ')'), // Good |
| 285 | $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // -------------------------------------------------------------------- |
| 289 | |
| 290 | /** |
| 291 | * Remove the suffix from the URL if needed |
| 292 | * |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 293 | * Called by CI_Router |
| 294 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | * @return void |
| 296 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 297 | public function _remove_url_suffix() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 299 | if ($this->config->item('url_suffix') != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 301 | $this->uri_string = preg_replace('|'.preg_quote($this->config->item('url_suffix')).'$|', '', $this->uri_string); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | // -------------------------------------------------------------------- |
| 306 | |
| 307 | /** |
| 308 | * Explode the URI Segments. The individual segments will |
| 309 | * be stored in the $this->segments array. |
| 310 | * |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 311 | * Called by CI_Router |
| 312 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | * @return void |
| 314 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 315 | public function _explode_segments() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 317 | foreach (explode('/', preg_replace('|/*(.+?)/*$|', '\\1', $this->uri_string)) as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | { |
| 319 | // Filter segments for security |
| 320 | $val = trim($this->_filter_uri($val)); |
| 321 | |
| 322 | if ($val != '') |
| 323 | { |
| 324 | $this->segments[] = $val; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // -------------------------------------------------------------------- |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 330 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | /** |
| 332 | * Re-index Segments |
| 333 | * |
| 334 | * This function re-indexes the $this->segment array so that it |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 335 | * starts at 1 rather than 0. Doing so makes it simpler to |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | * use functions like $this->uri->segment(n) since there is |
| 337 | * a 1:1 relationship between the segment array and the actual segments. |
| 338 | * |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 339 | * Called by CI_Router |
| 340 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | * @return void |
| 342 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 343 | public function _reindex_segments() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | { |
| 345 | array_unshift($this->segments, NULL); |
| 346 | array_unshift($this->rsegments, NULL); |
| 347 | unset($this->segments[0]); |
| 348 | unset($this->rsegments[0]); |
| 349 | } |
| 350 | |
| 351 | // -------------------------------------------------------------------- |
| 352 | |
| 353 | /** |
| 354 | * Fetch a URI Segment |
| 355 | * |
| 356 | * This function returns the URI segment based on the number provided. |
| 357 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | * @param integer |
| 359 | * @param bool |
| 360 | * @return string |
| 361 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 362 | public function segment($n, $no_result = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | { |
| 364 | return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n]; |
| 365 | } |
| 366 | |
| 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
| 370 | * Fetch a URI "routed" Segment |
| 371 | * |
| 372 | * This function returns the re-routed URI segment (assuming routing rules are used) |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 373 | * based on the number provided. If there is no routing this function returns the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | * same result as $this->segment() |
| 375 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | * @param integer |
| 377 | * @param bool |
| 378 | * @return string |
| 379 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 380 | public function rsegment($n, $no_result = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | { |
| 382 | return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n]; |
| 383 | } |
| 384 | |
| 385 | // -------------------------------------------------------------------- |
| 386 | |
| 387 | /** |
| 388 | * Generate a key value pair from the URI string |
| 389 | * |
| 390 | * This function generates and associative array of URI data starting |
| 391 | * at the supplied segment. For example, if this is your URI: |
| 392 | * |
| 393 | * example.com/user/search/name/joe/location/UK/gender/male |
| 394 | * |
| 395 | * You can use this function to generate an array with this prototype: |
| 396 | * |
| 397 | * array ( |
| 398 | * name => joe |
| 399 | * location => UK |
| 400 | * gender => male |
| 401 | * ) |
| 402 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | * @param integer the starting segment number |
| 404 | * @param array an array of default values |
| 405 | * @return array |
| 406 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 407 | public function uri_to_assoc($n = 3, $default = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 409 | return $this->_uri_to_assoc($n, $default, 'segment'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | } |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | /** |
| 415 | * Identical to above only it uses the re-routed segment array |
| 416 | * |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 417 | * @param integer the starting segment number |
| 418 | * @param array an array of default values |
| 419 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 421 | public function ruri_to_assoc($n = 3, $default = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | return $this->_uri_to_assoc($n, $default, 'rsegment'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // -------------------------------------------------------------------- |
| 427 | |
| 428 | /** |
| 429 | * Generate a key value pair from the URI string or Re-routed URI string |
| 430 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | * @param integer the starting segment number |
| 432 | * @param array an array of default values |
| 433 | * @param string which array we should use |
| 434 | * @return array |
| 435 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 436 | protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 438 | if ( ! is_numeric($n)) |
| 439 | { |
| 440 | return $default; |
| 441 | } |
| 442 | |
| 443 | if (isset($this->keyval[$n])) |
| 444 | { |
| 445 | return $this->keyval[$n]; |
| 446 | } |
| 447 | |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 448 | if ($which === 'segment') |
| 449 | { |
| 450 | $total_segments = 'total_segments'; |
| 451 | $segment_array = 'segment_array'; |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | $total_segments = 'total_rsegments'; |
| 456 | $segment_array = 'rsegment_array'; |
| 457 | } |
| 458 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | if ($this->$total_segments() < $n) |
| 460 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 461 | if (count($default) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | { |
| 463 | return array(); |
| 464 | } |
| 465 | |
Andrey Andreev | 1ae6516 | 2012-03-10 16:11:34 +0200 | [diff] [blame] | 466 | return array_fill_keys($default, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | $segments = array_slice($this->$segment_array(), ($n - 1)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | $i = 0; |
| 471 | $lastval = ''; |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 472 | $retval = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | foreach ($segments as $seg) |
| 474 | { |
| 475 | if ($i % 2) |
| 476 | { |
| 477 | $retval[$lastval] = $seg; |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | $retval[$seg] = FALSE; |
| 482 | $lastval = $seg; |
| 483 | } |
| 484 | |
| 485 | $i++; |
| 486 | } |
| 487 | |
| 488 | if (count($default) > 0) |
| 489 | { |
| 490 | foreach ($default as $val) |
| 491 | { |
| 492 | if ( ! array_key_exists($val, $retval)) |
| 493 | { |
| 494 | $retval[$val] = FALSE; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // Cache the array for reuse |
| 500 | $this->keyval[$n] = $retval; |
| 501 | return $retval; |
| 502 | } |
| 503 | |
| 504 | // -------------------------------------------------------------------- |
| 505 | |
| 506 | /** |
| 507 | * Generate a URI string from an associative array |
| 508 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | * @param array an associative array of key/values |
| 510 | * @return array |
| 511 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 512 | public function assoc_to_uri($array) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | { |
| 514 | $temp = array(); |
| 515 | foreach ((array)$array as $key => $val) |
| 516 | { |
Andrey Andreev | a798fdb | 2012-01-08 00:20:49 +0200 | [diff] [blame] | 517 | $temp[] = $key; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | $temp[] = $val; |
| 519 | } |
| 520 | |
| 521 | return implode('/', $temp); |
| 522 | } |
| 523 | |
| 524 | // -------------------------------------------------------------------- |
| 525 | |
| 526 | /** |
| 527 | * Fetch a URI Segment and add a trailing slash |
| 528 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | * @param integer |
| 530 | * @param string |
| 531 | * @return string |
| 532 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 533 | public function slash_segment($n, $where = 'trailing') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 534 | { |
| 535 | return $this->_slash_segment($n, $where, 'segment'); |
| 536 | } |
| 537 | |
| 538 | // -------------------------------------------------------------------- |
| 539 | |
| 540 | /** |
| 541 | * Fetch a URI Segment and add a trailing slash |
| 542 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | * @param integer |
| 544 | * @param string |
| 545 | * @return string |
| 546 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 547 | public function slash_rsegment($n, $where = 'trailing') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | { |
| 549 | return $this->_slash_segment($n, $where, 'rsegment'); |
| 550 | } |
| 551 | |
| 552 | // -------------------------------------------------------------------- |
| 553 | |
| 554 | /** |
| 555 | * Fetch a URI Segment and add a trailing slash - helper function |
| 556 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | * @param integer |
| 558 | * @param string |
| 559 | * @param string |
| 560 | * @return string |
| 561 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 562 | protected function _slash_segment($n, $where = 'trailing', $which = 'segment') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 564 | $leading = $trailing = '/'; |
Phil Sturgeon | 48c718c | 2010-12-30 23:40:02 +0000 | [diff] [blame] | 565 | |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 566 | if ($where === 'trailing') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 568 | $leading = ''; |
| 569 | } |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 570 | elseif ($where === 'leading') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | $trailing = ''; |
| 573 | } |
Phil Sturgeon | 48c718c | 2010-12-30 23:40:02 +0000 | [diff] [blame] | 574 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | return $leading.$this->$which($n).$trailing; |
| 576 | } |
| 577 | |
| 578 | // -------------------------------------------------------------------- |
| 579 | |
| 580 | /** |
| 581 | * Segment Array |
| 582 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 583 | * @return array |
| 584 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 585 | public function segment_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | { |
| 587 | return $this->segments; |
| 588 | } |
| 589 | |
| 590 | // -------------------------------------------------------------------- |
| 591 | |
| 592 | /** |
| 593 | * Routed Segment Array |
| 594 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | * @return array |
| 596 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 597 | public function rsegment_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | { |
| 599 | return $this->rsegments; |
| 600 | } |
| 601 | |
| 602 | // -------------------------------------------------------------------- |
| 603 | |
| 604 | /** |
| 605 | * Total number of segments |
| 606 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | * @return integer |
| 608 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 609 | public function total_segments() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 610 | { |
| 611 | return count($this->segments); |
| 612 | } |
| 613 | |
| 614 | // -------------------------------------------------------------------- |
| 615 | |
| 616 | /** |
| 617 | * Total number of routed segments |
| 618 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 619 | * @return integer |
| 620 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 621 | public function total_rsegments() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | { |
| 623 | return count($this->rsegments); |
| 624 | } |
| 625 | |
| 626 | // -------------------------------------------------------------------- |
| 627 | |
| 628 | /** |
| 629 | * Fetch the entire URI string |
| 630 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 631 | * @return string |
| 632 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 633 | public function uri_string() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | { |
| 635 | return $this->uri_string; |
| 636 | } |
| 637 | |
| 638 | |
| 639 | // -------------------------------------------------------------------- |
| 640 | |
| 641 | /** |
| 642 | * Fetch the entire Re-routed URI string |
| 643 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | * @return string |
| 645 | */ |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 646 | public function ruri_string() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | { |
Eric Barnes | ffdc392 | 2011-01-12 09:05:20 -0500 | [diff] [blame] | 648 | return '/'.implode('/', $this->rsegment_array()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | |
| 653 | /* End of file URI.php */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 654 | /* Location: ./system/core/URI.php */ |