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