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