admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Router Class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 20 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 | * Parses URIs and determines routing |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @author Rick Ellis |
| 26 | * @category Libraries |
| 27 | * @link http://www.codeigniter.com/user_guide/general/routing.html |
| 28 | */ |
| 29 | class CI_Router { |
| 30 | |
| 31 | var $config; |
| 32 | var $uri_string = ''; |
| 33 | var $segments = array(); |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 34 | var $rsegments = array(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 35 | var $routes = array(); |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 36 | var $error_routes = array(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 37 | var $class = ''; |
| 38 | var $method = 'index'; |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 39 | var $directory = ''; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 40 | var $uri_protocol = 'auto'; |
| 41 | var $default_controller; |
| 42 | var $scaffolding_request = FALSE; // Must be set to FALSE |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 47 | * Runs the route mapping function. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | */ |
| 49 | function CI_Router() |
| 50 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 51 | $this->config =& load_class('Config'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 52 | $this->_set_route_mapping(); |
| 53 | log_message('debug', "Router Class Initialized"); |
| 54 | } |
| 55 | |
| 56 | // -------------------------------------------------------------------- |
| 57 | |
| 58 | /** |
| 59 | * Set the route mapping |
| 60 | * |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 61 | * This function determines what should be served based on the URI request, |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 62 | * as well as any "routes" that have been set in the routing config file. |
| 63 | * |
| 64 | * @access private |
| 65 | * @return void |
| 66 | */ |
| 67 | function _set_route_mapping() |
| 68 | { |
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 69 | // Are query strings enabled in the config file? |
| 70 | // If so, we're done since segment based URIs are not used with query strings. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 71 | if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')])) |
| 72 | { |
| 73 | $this->set_class($_GET[$this->config->item('controller_trigger')]); |
| 74 | |
| 75 | if (isset($_GET[$this->config->item('function_trigger')])) |
| 76 | { |
| 77 | $this->set_method($_GET[$this->config->item('function_trigger')]); |
| 78 | } |
| 79 | |
| 80 | return; |
| 81 | } |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 82 | |
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 83 | // Load the routes.php file. |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 84 | @include(APPPATH.'config/routes'.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 85 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; |
| 86 | unset($route); |
| 87 | |
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 88 | // Set the default controller so we can display it in the event |
| 89 | // the URI doesn't correlated to a valid controller. |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 90 | $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']); |
| 91 | |
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 92 | // Fetch the complete URI string |
| 93 | $this->uri_string = $this->_get_uri_string(); |
| 94 | |
| 95 | // If the URI contains only a slash we'll kill it |
| 96 | if ($this->uri_string == '/') |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 97 | { |
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 98 | $this->uri_string = ''; |
| 99 | } |
| 100 | |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 101 | // Is there a URI string? If not, the default controller specified in the "routes" file will be shown. |
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 102 | if ($this->uri_string == '') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 103 | { |
| 104 | if ($this->default_controller === FALSE) |
| 105 | { |
| 106 | show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file."); |
| 107 | } |
| 108 | |
| 109 | $this->set_class($this->default_controller); |
| 110 | $this->set_method('index'); |
| 111 | |
| 112 | log_message('debug', "No URI present. Default controller set."); |
| 113 | return; |
| 114 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 115 | unset($this->routes['default_controller']); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 116 | |
| 117 | // Do we need to remove the suffix specified in the config file? |
| 118 | if ($this->config->item('url_suffix') != "") |
| 119 | { |
| 120 | $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string); |
| 121 | } |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 122 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 123 | // Explode the URI Segments. The individual segments will |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 124 | // be stored in the $this->segments array. |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 125 | foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val) |
| 126 | { |
| 127 | // Filter segments for security |
| 128 | $val = trim($this->_filter_uri($val)); |
| 129 | |
| 130 | if ($val != '') |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 131 | $this->segments[] = $val; |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 132 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 133 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 134 | // Parse any custom routing that may exist |
| 135 | $this->_parse_routes(); |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 136 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 137 | // Re-index the segment array so that it starts with 1 rather than 0 |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 138 | $this->_reindex_segments(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 139 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Compile Segments |
| 145 | * |
| 146 | * This function takes an array of URI segments as |
| 147 | * input, and puts it into the $this->segments array. |
| 148 | * It also sets the current class/method |
| 149 | * |
| 150 | * @access private |
| 151 | * @param array |
| 152 | * @param bool |
| 153 | * @return void |
| 154 | */ |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 155 | function _compile_segments($segments = array()) |
| 156 | { |
| 157 | $segments = $this->_validate_segments($segments); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 158 | |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 159 | if (count($segments) == 0) |
| 160 | { |
| 161 | return; |
| 162 | } |
| 163 | |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 164 | $this->set_class($segments[0]); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 165 | |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 166 | if (isset($segments[1])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 167 | { |
| 168 | // A scaffolding request. No funny business with the URL |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 169 | if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 170 | { |
| 171 | $this->scaffolding_request = TRUE; |
| 172 | unset($this->routes['scaffolding_trigger']); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | // A standard method request |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 177 | $this->set_method($segments[1]); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 180 | |
| 181 | // Update our "routed" segment array to contain the segments. |
| 182 | // Note: If there is no custom routing, this array will be |
| 183 | // identical to $this->segments |
| 184 | $this->rsegments = $segments; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 185 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 186 | |
| 187 | // -------------------------------------------------------------------- |
| 188 | |
| 189 | /** |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 190 | * Validates the supplied segments. Attempts to determine the path to |
| 191 | * the controller. |
| 192 | * |
| 193 | * @access private |
| 194 | * @param array |
| 195 | * @return array |
| 196 | */ |
| 197 | function _validate_segments($segments) |
| 198 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 199 | // Does the requested controller exist in the root folder? |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 200 | if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 201 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 202 | return $segments; |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 203 | } |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 204 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 205 | // Is the controller in a sub-folder? |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 206 | if (is_dir(APPPATH.'controllers/'.$segments[0])) |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 207 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 208 | // Set the directory and remove it from the segment array |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 209 | $this->set_directory($segments[0]); |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 210 | $segments = array_slice($segments, 1); |
| 211 | |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 212 | if (count($segments) > 0) |
| 213 | { |
| 214 | // Does the requested controller exist in the sub-folder? |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 215 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 216 | { |
| 217 | show_404(); |
| 218 | } |
| 219 | } |
| 220 | else |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 221 | { |
| 222 | $this->set_class($this->default_controller); |
| 223 | $this->set_method('index'); |
admin | 2781849 | 2006-09-05 03:31:28 +0000 | [diff] [blame] | 224 | |
| 225 | // Does the default controller exist in the sub-folder? |
| 226 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) |
| 227 | { |
| 228 | $this->directory = ''; |
| 229 | return array(); |
| 230 | } |
| 231 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return $segments; |
| 235 | } |
| 236 | |
| 237 | // Can't find the requested controller... |
| 238 | show_404(); |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 239 | } |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | /** |
| 243 | * Re-index Segments |
| 244 | * |
| 245 | * This function re-indexes the $this->segment array so that it |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 246 | * starts at 1 rather then 0. Doing so makes it simpler to |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 247 | * use functions like $this->uri->segment(n) since there is |
| 248 | * a 1:1 relationship between the segment array and the actual segments. |
| 249 | * |
| 250 | * @access private |
| 251 | * @return void |
| 252 | */ |
| 253 | function _reindex_segments() |
| 254 | { |
| 255 | // Is the routed segment array different then the main segment array? |
| 256 | $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE; |
| 257 | |
| 258 | $i = 1; |
| 259 | foreach ($this->segments as $val) |
| 260 | { |
| 261 | $this->segments[$i++] = $val; |
| 262 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 263 | unset($this->segments[0]); |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 264 | |
| 265 | if ($diff == FALSE) |
| 266 | { |
| 267 | $this->rsegments = $this->segments; |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | $i = 1; |
| 272 | foreach ($this->rsegments as $val) |
| 273 | { |
| 274 | $this->rsegments[$i++] = $val; |
| 275 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 276 | unset($this->rsegments[0]); |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 283 | * Get the URI String |
| 284 | * |
| 285 | * @access private |
| 286 | * @return string |
| 287 | */ |
| 288 | function _get_uri_string() |
| 289 | { |
| 290 | if (strtoupper($this->config->item('uri_protocol')) == 'AUTO') |
| 291 | { |
admin | 813d0ac | 2006-10-01 20:36:39 +0000 | [diff] [blame] | 292 | // If the URL has a question mark then it's simplest to just |
| 293 | // build the URI string from the zero index of the $_GET array. |
| 294 | // This avoids having to deal with $_SERVER variables, which |
admin | 9093131 | 2006-10-20 00:59:17 +0000 | [diff] [blame] | 295 | // can be unreliable in some environments |
admin | 813d0ac | 2006-10-01 20:36:39 +0000 | [diff] [blame] | 296 | if (is_array($_GET) AND count($_GET) == 1) |
| 297 | { |
admin | 7acd581 | 2006-10-23 21:37:22 +0000 | [diff] [blame] | 298 | // Note: Due to a bug in current() that affects some versions |
| 299 | // of PHP we can not pass function call directly into it |
| 300 | $keys = array_keys($_GET); |
| 301 | return current($keys); |
admin | 813d0ac | 2006-10-01 20:36:39 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Is there a PATH_INFO variable? |
| 305 | // Note: some servers seem to have trouble with getenv() so we'll test it two ways |
admin | 9093131 | 2006-10-20 00:59:17 +0000 | [diff] [blame] | 306 | $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO'); |
| 307 | if ($path != '' AND $path != "/".SELF) |
| 308 | { |
| 309 | return $path; |
| 310 | } |
| 311 | |
| 312 | // No PATH_INFO?... What about QUERY_STRING? |
| 313 | $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING'); |
| 314 | if ($path != '') |
| 315 | { |
| 316 | return $path; |
| 317 | } |
admin | 813d0ac | 2006-10-01 20:36:39 +0000 | [diff] [blame] | 318 | |
admin | 9093131 | 2006-10-20 00:59:17 +0000 | [diff] [blame] | 319 | // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists? |
| 320 | $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO'); |
| 321 | if ($path != '' AND $path != "/".SELF) |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 322 | { |
admin | 9093131 | 2006-10-20 00:59:17 +0000 | [diff] [blame] | 323 | return $path; |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 324 | } |
admin | 9093131 | 2006-10-20 00:59:17 +0000 | [diff] [blame] | 325 | |
| 326 | // We've exhausted all our options... |
| 327 | return ''; |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 328 | } |
| 329 | else |
| 330 | { |
| 331 | $uri = strtoupper($this->config->item('uri_protocol')); |
| 332 | |
| 333 | if ($uri == 'REQUEST_URI') |
| 334 | { |
| 335 | return $this->_parse_request_uri(); |
| 336 | } |
| 337 | |
admin | 813d0ac | 2006-10-01 20:36:39 +0000 | [diff] [blame] | 338 | return (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri); |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
| 345 | * Parse the REQUEST_URI |
| 346 | * |
| 347 | * Due to the way REQUEST_URI works it usually contains path info |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 348 | * that makes it unusable as URI data. We'll trim off the unnecessary |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 349 | * data, hopefully arriving at a valid URI that we can use. |
| 350 | * |
| 351 | * @access private |
| 352 | * @return string |
| 353 | */ |
| 354 | function _parse_request_uri() |
| 355 | { |
admin | 4c1ab6c | 2006-10-11 21:48:33 +0000 | [diff] [blame] | 356 | if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '') |
admin | dbd8aec | 2006-09-22 19:20:09 +0000 | [diff] [blame] | 357 | { |
| 358 | return ''; |
| 359 | } |
| 360 | |
admin | 4c1ab6c | 2006-10-11 21:48:33 +0000 | [diff] [blame] | 361 | $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI'])); |
| 362 | |
admin | 9093131 | 2006-10-20 00:59:17 +0000 | [diff] [blame] | 363 | if ($request_uri == '' OR $request_uri == SELF) |
admin | 4c1ab6c | 2006-10-11 21:48:33 +0000 | [diff] [blame] | 364 | { |
| 365 | return ''; |
| 366 | } |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 367 | |
admin | 4c1ab6c | 2006-10-11 21:48:33 +0000 | [diff] [blame] | 368 | $fc_path = FCPATH; |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 369 | if (strpos($request_uri, '?') !== FALSE) |
| 370 | { |
| 371 | $fc_path .= '?'; |
| 372 | } |
| 373 | |
admin | 4c1ab6c | 2006-10-11 21:48:33 +0000 | [diff] [blame] | 374 | $parsed_uri = explode("/", $request_uri); |
| 375 | |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 376 | $i = 0; |
| 377 | foreach(explode("/", $fc_path) as $segment) |
| 378 | { |
admin | 4c1ab6c | 2006-10-11 21:48:33 +0000 | [diff] [blame] | 379 | if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i]) |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 380 | { |
| 381 | $i++; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | $parsed_uri = implode("/", array_slice($parsed_uri, $i)); |
| 386 | |
| 387 | if ($parsed_uri != '') |
| 388 | { |
| 389 | $parsed_uri = '/'.$parsed_uri; |
| 390 | } |
| 391 | |
| 392 | return $parsed_uri; |
| 393 | } |
admin | 592cdcb | 2006-09-22 18:45:42 +0000 | [diff] [blame] | 394 | |
| 395 | // -------------------------------------------------------------------- |
| 396 | |
| 397 | /** |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 398 | * Filter segments for malicious characters |
| 399 | * |
| 400 | * @access private |
| 401 | * @param string |
| 402 | * @return string |
| 403 | */ |
| 404 | function _filter_uri($str) |
| 405 | { |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 406 | if ($this->config->item('permitted_uri_chars') != '') |
| 407 | { |
| 408 | if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str)) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 409 | { |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 410 | exit('The URI you submitted has disallowed characters: '.$str); |
| 411 | } |
| 412 | } |
| 413 | return $str; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 414 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 415 | |
| 416 | // -------------------------------------------------------------------- |
| 417 | |
| 418 | /** |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 419 | * Parse Routes |
| 420 | * |
| 421 | * This function matches any routes that may exist in |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 422 | * the config/routes.php file against the URI to |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 423 | * determine if the class/method need to be remapped. |
| 424 | * |
| 425 | * @access private |
| 426 | * @return void |
| 427 | */ |
| 428 | function _parse_routes() |
| 429 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 430 | // Do we even have any custom routing to deal with? |
| 431 | if (count($this->routes) == 0) |
| 432 | { |
| 433 | $this->_compile_segments($this->segments); |
| 434 | return; |
| 435 | } |
| 436 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 437 | // Turn the segment array into a URI string |
| 438 | $uri = implode('/', $this->segments); |
| 439 | $num = count($this->segments); |
| 440 | |
| 441 | // Is there a literal match? If so we're done |
| 442 | if (isset($this->routes[$uri])) |
| 443 | { |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 444 | $this->_compile_segments(explode('/', $this->routes[$uri])); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 445 | return; |
| 446 | } |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 447 | |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 448 | // Loop through the route array looking for wild-cards |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 449 | foreach (array_slice($this->routes, 1) as $key => $val) |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 450 | { |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 451 | // Convert wild-cards to RegEx |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 452 | $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); |
| 453 | |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 454 | // Does the RegEx match? |
admin | 71430b4 | 2006-09-15 20:29:25 +0000 | [diff] [blame] | 455 | if (preg_match('#^'.$key.'$#', $uri)) |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 456 | { |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 457 | // Do we have a back-reference? |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 458 | if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) |
| 459 | { |
admin | 71430b4 | 2006-09-15 20:29:25 +0000 | [diff] [blame] | 460 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 461 | } |
| 462 | |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 463 | $this->_compile_segments(explode('/', $val)); |
| 464 | return; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 465 | } |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 466 | } |
| 467 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 468 | // If we got this far it means we didn't encounter a |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 469 | // matching route so we'll set the site default route |
| 470 | $this->_compile_segments($this->segments); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 471 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 472 | |
| 473 | // -------------------------------------------------------------------- |
| 474 | |
| 475 | /** |
| 476 | * Set the class name |
| 477 | * |
| 478 | * @access public |
| 479 | * @param string |
| 480 | * @return void |
| 481 | */ |
| 482 | function set_class($class) |
| 483 | { |
| 484 | $this->class = $class; |
| 485 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 486 | |
| 487 | // -------------------------------------------------------------------- |
| 488 | |
| 489 | /** |
| 490 | * Fetch the current class |
| 491 | * |
| 492 | * @access public |
| 493 | * @return string |
| 494 | */ |
| 495 | function fetch_class() |
| 496 | { |
| 497 | return $this->class; |
| 498 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 499 | |
| 500 | // -------------------------------------------------------------------- |
| 501 | |
| 502 | /** |
| 503 | * Set the method name |
| 504 | * |
| 505 | * @access public |
| 506 | * @param string |
| 507 | * @return void |
| 508 | */ |
| 509 | function set_method($method) |
| 510 | { |
| 511 | $this->method = $method; |
| 512 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 513 | |
| 514 | // -------------------------------------------------------------------- |
| 515 | |
| 516 | /** |
| 517 | * Fetch the current method |
| 518 | * |
| 519 | * @access public |
| 520 | * @return string |
| 521 | */ |
| 522 | function fetch_method() |
| 523 | { |
admin | 08f6020 | 2006-10-03 05:28:00 +0000 | [diff] [blame] | 524 | if ($this->method == $this->fetch_class()) |
| 525 | { |
| 526 | return 'index'; |
| 527 | } |
| 528 | |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 529 | return $this->method; |
| 530 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 531 | |
| 532 | // -------------------------------------------------------------------- |
| 533 | |
| 534 | /** |
| 535 | * Set the directory name |
| 536 | * |
| 537 | * @access public |
| 538 | * @param string |
| 539 | * @return void |
| 540 | */ |
| 541 | function set_directory($dir) |
| 542 | { |
| 543 | $this->directory = $dir.'/'; |
| 544 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 545 | |
| 546 | // -------------------------------------------------------------------- |
| 547 | |
| 548 | /** |
| 549 | * Fetch the sub-directory (if any) that contains the requested controller class |
| 550 | * |
| 551 | * @access public |
| 552 | * @return string |
| 553 | */ |
| 554 | function fetch_directory() |
| 555 | { |
| 556 | return $this->directory; |
| 557 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 558 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 559 | } |
| 560 | // END Router Class |
| 561 | ?> |