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. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Router Class |
| 20 | * |
| 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(); |
| 36 | var $class = ''; |
| 37 | var $method = 'index'; |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 38 | var $directory = ''; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 39 | var $uri_protocol = 'auto'; |
| 40 | var $default_controller; |
| 41 | var $scaffolding_request = FALSE; // Must be set to FALSE |
| 42 | |
| 43 | /** |
| 44 | * Constructor |
| 45 | * |
| 46 | * Runs the route mapping function. |
| 47 | */ |
| 48 | function CI_Router() |
| 49 | { |
| 50 | $this->config =& _load_class('CI_Config'); |
| 51 | $this->_set_route_mapping(); |
| 52 | log_message('debug', "Router Class Initialized"); |
| 53 | } |
| 54 | |
| 55 | // -------------------------------------------------------------------- |
| 56 | |
| 57 | /** |
| 58 | * Set the route mapping |
| 59 | * |
| 60 | * This function determies what should be served based on the URI request, |
| 61 | * as well as any "routes" that have been set in the routing config file. |
| 62 | * |
| 63 | * @access private |
| 64 | * @return void |
| 65 | */ |
| 66 | function _set_route_mapping() |
| 67 | { |
| 68 | // Are query strings enabled? If so we're done... |
| 69 | if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')])) |
| 70 | { |
| 71 | $this->set_class($_GET[$this->config->item('controller_trigger')]); |
| 72 | |
| 73 | if (isset($_GET[$this->config->item('function_trigger')])) |
| 74 | { |
| 75 | $this->set_method($_GET[$this->config->item('function_trigger')]); |
| 76 | } |
| 77 | |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Load the routes.php file |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 82 | @include_once(APPPATH.'config/routes'.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 83 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; |
| 84 | unset($route); |
| 85 | |
| 86 | // Set the default controller |
| 87 | $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']); |
| 88 | |
| 89 | // Fetch the URI string Depending on the server, |
| 90 | // the URI will be available in one of two globals |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 91 | if ($this->config->item('uri_protocol') == 'auto') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 92 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 93 | $path_info = getenv('PATH_INFO'); |
| 94 | if ($path_info != '' AND $path_info != "/".SELF) |
| 95 | { |
| 96 | $this->uri_string = $path_info; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | $path_info = getenv('ORIG_PATH_INFO'); |
| 101 | if ($path_info != '' AND $path_info != "/".SELF) |
| 102 | { |
| 103 | $this->uri_string = $path_info; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | $this->uri_string = getenv('QUERY_STRING'); |
| 108 | } |
| 109 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 110 | } |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 111 | else |
| 112 | { |
| 113 | $this->uri_string = getenv(strtoupper($this->config->item('uri_protocol'))); |
| 114 | } |
| 115 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 116 | |
| 117 | // Is there a URI string? If not, the default controller specified |
| 118 | // by the admin in the "routes" file will be shown. |
| 119 | if ($this->uri_string == '') |
| 120 | { |
| 121 | if ($this->default_controller === FALSE) |
| 122 | { |
| 123 | show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file."); |
| 124 | } |
| 125 | |
| 126 | $this->set_class($this->default_controller); |
| 127 | $this->set_method('index'); |
| 128 | |
| 129 | log_message('debug', "No URI present. Default controller set."); |
| 130 | return; |
| 131 | } |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 132 | unset($this->routes['default_controller']); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 133 | |
| 134 | // Do we need to remove the suffix specified in the config file? |
| 135 | if ($this->config->item('url_suffix') != "") |
| 136 | { |
| 137 | $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string); |
| 138 | } |
| 139 | |
| 140 | // Explode the URI Segments. The individual segments will |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 141 | // be stored in the $this->segments array. |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 142 | foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val) |
| 143 | { |
| 144 | // Filter segments for security |
| 145 | $val = trim($this->_filter_uri($val)); |
| 146 | |
| 147 | if ($val != '') |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 148 | $this->segments[] = $val; |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 149 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 150 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 151 | // Parse any custom routing that may exist |
| 152 | $this->_parse_routes(); |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 153 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 154 | // 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] | 155 | $this->_reindex_segments(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 156 | } |
| 157 | // END _set_route_mapping() |
| 158 | |
| 159 | // -------------------------------------------------------------------- |
| 160 | |
| 161 | /** |
| 162 | * Compile Segments |
| 163 | * |
| 164 | * This function takes an array of URI segments as |
| 165 | * input, and puts it into the $this->segments array. |
| 166 | * It also sets the current class/method |
| 167 | * |
| 168 | * @access private |
| 169 | * @param array |
| 170 | * @param bool |
| 171 | * @return void |
| 172 | */ |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 173 | function _compile_segments($segments = array()) |
| 174 | { |
| 175 | $segments = $this->_validate_segments($segments); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 176 | |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 177 | if (count($segments) == 0) |
| 178 | { |
| 179 | return; |
| 180 | } |
| 181 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 182 | $this->set_class($segments['0']); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 183 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 184 | if (isset($segments['1'])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 185 | { |
| 186 | // A scaffolding request. No funny business with the URL |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 187 | if ($this->routes['scaffolding_trigger'] == $segments['1'] AND $segments['1'] != '_ci_scaffolding') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 188 | { |
| 189 | $this->scaffolding_request = TRUE; |
| 190 | unset($this->routes['scaffolding_trigger']); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | // A standard method request |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 195 | $this->set_method($segments['1']); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 198 | |
| 199 | // Update our "routed" segment array to contain the segments. |
| 200 | // Note: If there is no custom routing, this array will be |
| 201 | // identical to $this->segments |
| 202 | $this->rsegments = $segments; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 203 | } |
| 204 | // END _compile_segments() |
| 205 | |
| 206 | // -------------------------------------------------------------------- |
| 207 | |
| 208 | /** |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 209 | * Validates the supplied segments. Attempts to determine the path to |
| 210 | * the controller. |
| 211 | * |
| 212 | * @access private |
| 213 | * @param array |
| 214 | * @return array |
| 215 | */ |
| 216 | function _validate_segments($segments) |
| 217 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 218 | // Does the requested controller exist in the root folder? |
| 219 | if (file_exists(APPPATH.'controllers/'.$segments['0'].EXT)) |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 220 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 221 | return $segments; |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 222 | } |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 223 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 224 | // Is the controller in a sub-folder? |
| 225 | if (is_dir(APPPATH.'controllers/'.$segments['0'])) |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 226 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 227 | // Set the directory and remove it from the segment array |
| 228 | $this->set_directory($segments['0']); |
| 229 | $segments = array_slice($segments, 1); |
| 230 | |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 231 | if (count($segments) > 0) |
| 232 | { |
| 233 | // Does the requested controller exist in the sub-folder? |
| 234 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments['0'].EXT)) |
| 235 | { |
| 236 | show_404(); |
| 237 | } |
| 238 | } |
| 239 | else |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 240 | { |
| 241 | $this->set_class($this->default_controller); |
| 242 | $this->set_method('index'); |
admin | 2781849 | 2006-09-05 03:31:28 +0000 | [diff] [blame] | 243 | |
| 244 | // Does the default controller exist in the sub-folder? |
| 245 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) |
| 246 | { |
| 247 | $this->directory = ''; |
| 248 | return array(); |
| 249 | } |
| 250 | |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return $segments; |
| 254 | } |
| 255 | |
| 256 | // Can't find the requested controller... |
| 257 | show_404(); |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 258 | } |
| 259 | // END _validate_segments() |
admin | 99bccd6 | 2006-09-21 17:05:40 +0000 | [diff] [blame] | 260 | |
| 261 | // -------------------------------------------------------------------- |
| 262 | /** |
| 263 | * Re-index Segments |
| 264 | * |
| 265 | * This function re-indexes the $this->segment array so that it |
| 266 | * starts at 1 rather then 0. Doing so makes it simpler to |
| 267 | * use functions like $this->uri->segment(n) since there is |
| 268 | * a 1:1 relationship between the segment array and the actual segments. |
| 269 | * |
| 270 | * @access private |
| 271 | * @return void |
| 272 | */ |
| 273 | function _reindex_segments() |
| 274 | { |
| 275 | // Is the routed segment array different then the main segment array? |
| 276 | $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE; |
| 277 | |
| 278 | $i = 1; |
| 279 | foreach ($this->segments as $val) |
| 280 | { |
| 281 | $this->segments[$i++] = $val; |
| 282 | } |
| 283 | unset($this->segments['0']); |
| 284 | |
| 285 | if ($diff == FALSE) |
| 286 | { |
| 287 | $this->rsegments = $this->segments; |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | $i = 1; |
| 292 | foreach ($this->rsegments as $val) |
| 293 | { |
| 294 | $this->rsegments[$i++] = $val; |
| 295 | } |
| 296 | unset($this->rsegments['0']); |
| 297 | } |
| 298 | } |
| 299 | // END _reindex_segments() |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 300 | |
| 301 | // -------------------------------------------------------------------- |
| 302 | |
| 303 | /** |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 304 | * Filter segments for malicious characters |
| 305 | * |
| 306 | * @access private |
| 307 | * @param string |
| 308 | * @return string |
| 309 | */ |
| 310 | function _filter_uri($str) |
| 311 | { |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 312 | if ($this->config->item('permitted_uri_chars') != '') |
| 313 | { |
| 314 | if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str)) |
| 315 | { |
| 316 | exit('The URI you submitted has disallowed characters: '.$str); |
| 317 | } |
| 318 | } |
| 319 | return $str; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 320 | } |
| 321 | // END _filter_uri() |
| 322 | |
| 323 | // -------------------------------------------------------------------- |
| 324 | |
| 325 | /** |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 326 | * Parse Routes |
| 327 | * |
| 328 | * This function matches any routes that may exist in |
| 329 | * the config/routes.php file against the URI to |
| 330 | * determine if the class/method need to be remapped. |
| 331 | * |
| 332 | * @access private |
| 333 | * @return void |
| 334 | */ |
| 335 | function _parse_routes() |
| 336 | { |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 337 | // Do we even have any custom routing to deal with? |
| 338 | if (count($this->routes) == 0) |
| 339 | { |
| 340 | $this->_compile_segments($this->segments); |
| 341 | return; |
| 342 | } |
| 343 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 344 | // Turn the segment array into a URI string |
| 345 | $uri = implode('/', $this->segments); |
| 346 | $num = count($this->segments); |
| 347 | |
| 348 | // Is there a literal match? If so we're done |
| 349 | if (isset($this->routes[$uri])) |
| 350 | { |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 351 | $this->_compile_segments(explode('/', $this->routes[$uri])); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 354 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 355 | // Loop through the route array looking for wildcards |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 356 | foreach (array_slice($this->routes, 1) as $key => $val) |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 357 | { |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 358 | // Convert wildcards to RegEx |
| 359 | $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); |
| 360 | |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 361 | // Does the RegEx match? |
admin | 71430b4 | 2006-09-15 20:29:25 +0000 | [diff] [blame] | 362 | if (preg_match('#^'.$key.'$#', $uri)) |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 363 | { |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 364 | // Do we have a back-reference? |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 365 | if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) |
| 366 | { |
admin | 71430b4 | 2006-09-15 20:29:25 +0000 | [diff] [blame] | 367 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
admin | d4e9507 | 2006-08-26 01:15:06 +0000 | [diff] [blame] | 368 | } |
| 369 | |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 370 | $this->_compile_segments(explode('/', $val)); |
| 371 | return; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 372 | } |
admin | e07fbb3 | 2006-08-26 17:11:01 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // If we got this far it means we didn't encounter a |
| 376 | // matching route so we'll set the site default route |
| 377 | $this->_compile_segments($this->segments); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 378 | } |
| 379 | // END set_method() |
admin | 45c872b | 2006-08-26 04:51:38 +0000 | [diff] [blame] | 380 | |
| 381 | // -------------------------------------------------------------------- |
| 382 | |
| 383 | /** |
| 384 | * Set the class name |
| 385 | * |
| 386 | * @access public |
| 387 | * @param string |
| 388 | * @return void |
| 389 | */ |
| 390 | function set_class($class) |
| 391 | { |
| 392 | $this->class = $class; |
| 393 | } |
| 394 | // END set_class() |
| 395 | |
| 396 | // -------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
| 399 | * Fetch the current class |
| 400 | * |
| 401 | * @access public |
| 402 | * @return string |
| 403 | */ |
| 404 | function fetch_class() |
| 405 | { |
| 406 | return $this->class; |
| 407 | } |
| 408 | // END fetch_class() |
| 409 | |
| 410 | // -------------------------------------------------------------------- |
| 411 | |
| 412 | /** |
| 413 | * Set the method name |
| 414 | * |
| 415 | * @access public |
| 416 | * @param string |
| 417 | * @return void |
| 418 | */ |
| 419 | function set_method($method) |
| 420 | { |
| 421 | $this->method = $method; |
| 422 | } |
| 423 | // END set_method() |
| 424 | |
| 425 | // -------------------------------------------------------------------- |
| 426 | |
| 427 | /** |
| 428 | * Fetch the current method |
| 429 | * |
| 430 | * @access public |
| 431 | * @return string |
| 432 | */ |
| 433 | function fetch_method() |
| 434 | { |
| 435 | return $this->method; |
| 436 | } |
| 437 | // END fetch_method() |
| 438 | |
| 439 | // -------------------------------------------------------------------- |
| 440 | |
| 441 | /** |
| 442 | * Set the directory name |
| 443 | * |
| 444 | * @access public |
| 445 | * @param string |
| 446 | * @return void |
| 447 | */ |
| 448 | function set_directory($dir) |
| 449 | { |
| 450 | $this->directory = $dir.'/'; |
| 451 | } |
| 452 | // END set_directory() |
| 453 | |
| 454 | // -------------------------------------------------------------------- |
| 455 | |
| 456 | /** |
| 457 | * Fetch the sub-directory (if any) that contains the requested controller class |
| 458 | * |
| 459 | * @access public |
| 460 | * @return string |
| 461 | */ |
| 462 | function fetch_directory() |
| 463 | { |
| 464 | return $this->directory; |
| 465 | } |
| 466 | // END fetch_directory() |
| 467 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 468 | } |
| 469 | // END Router Class |
| 470 | ?> |