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