Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Router Class |
| 20 | * |
| 21 | * Parses URIs and determines routing |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @author ExpressionEngine Dev Team |
| 26 | * @category Libraries |
| 27 | * @link http://codeigniter.com/user_guide/general/routing.html |
| 28 | */ |
| 29 | class CI_Router { |
| 30 | |
| 31 | var $config; |
| 32 | var $routes = array(); |
| 33 | var $error_routes = array(); |
| 34 | var $class = ''; |
| 35 | var $method = 'index'; |
| 36 | var $directory = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | var $default_controller; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Constructor |
| 41 | * |
| 42 | * Runs the route mapping function. |
| 43 | */ |
| 44 | function CI_Router() |
| 45 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 46 | $this->config =& load_class('Config', 'core'); |
| 47 | $this->uri =& load_class('URI', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | log_message('debug', "Router Class Initialized"); |
| 49 | } |
| 50 | |
| 51 | // -------------------------------------------------------------------- |
| 52 | |
| 53 | /** |
| 54 | * Set the route mapping |
| 55 | * |
| 56 | * This function determines what should be served based on the URI request, |
| 57 | * as well as any "routes" that have been set in the routing config file. |
| 58 | * |
| 59 | * @access private |
| 60 | * @return void |
| 61 | */ |
| 62 | function _set_routing() |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 63 | { |
| 64 | // Are query strings enabled in the config file? Normally CI doesn't utilize query strings |
| 65 | // since URI segments are more search-engine friendly, but they can optionally be used. |
| 66 | // If this feature is enabled, we will gather the directory/class/method a little differently |
| 67 | $segments = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')])) |
| 69 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 70 | if (isset($_GET[$this->config->item('directory_trigger')])) |
| 71 | { |
| 72 | $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')]))); |
| 73 | $segments[] = $this->fetch_directory(); |
| 74 | } |
| 75 | |
| 76 | if (isset($_GET[$this->config->item('controller_trigger')])) |
| 77 | { |
| 78 | $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')]))); |
| 79 | $segments[] = $this->fetch_class(); |
| 80 | } |
| 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | if (isset($_GET[$this->config->item('function_trigger')])) |
| 83 | { |
| 84 | $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')]))); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 85 | $segments[] = $this->fetch_method(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Load the routes.php file. |
| 90 | @include(APPPATH.'config/routes'.EXT); |
| 91 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; |
| 92 | unset($route); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 93 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | // Set the default controller so we can display it in the event |
| 95 | // the URI doesn't correlated to a valid controller. |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 96 | $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']); |
| 97 | |
| 98 | // Were there any query string segments? If so, we'll validate them and bail out since we're done. |
| 99 | if (count($segments) > 0) |
| 100 | { |
| 101 | return $this->_validate_request($segments); |
| 102 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | |
| 104 | // Fetch the complete URI string |
| 105 | $this->uri->_fetch_uri_string(); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 106 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | // Is there a URI string? If not, the default controller specified in the "routes" file will be shown. |
| 108 | if ($this->uri->uri_string == '') |
| 109 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 110 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | |
| 113 | // Do we need to remove the URL suffix? |
| 114 | $this->uri->_remove_url_suffix(); |
| 115 | |
| 116 | // Compile the segments into an array |
| 117 | $this->uri->_explode_segments(); |
| 118 | |
| 119 | // Parse any custom routing that may exist |
| 120 | $this->_parse_routes(); |
| 121 | |
| 122 | // Re-index the segment array so that it starts with 1 rather than 0 |
| 123 | $this->uri->_reindex_segments(); |
| 124 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 125 | |
| 126 | // -------------------------------------------------------------------- |
| 127 | |
| 128 | /** |
| 129 | * Set the default controller |
| 130 | * |
| 131 | * @access private |
| 132 | * @return void |
| 133 | */ |
| 134 | function _set_default_controller() |
| 135 | { |
| 136 | if ($this->default_controller === FALSE) |
| 137 | { |
| 138 | show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file."); |
| 139 | } |
| 140 | // Is the method being specified? |
| 141 | if (strpos($this->default_controller, '/') !== FALSE) |
| 142 | { |
| 143 | $x = explode('/', $this->default_controller); |
| 144 | |
| 145 | $this->set_class($x[0]); |
| 146 | $this->set_method($x[1]); |
| 147 | $this->_set_request(array($x[0], $x[1])); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | $this->set_class($this->default_controller); |
| 152 | $this->set_method('index'); |
| 153 | $this->_set_request(array($this->default_controller, 'index')); |
| 154 | } |
| 155 | |
| 156 | // re-index the routed segments array so it starts with 1 rather than 0 |
| 157 | $this->uri->_reindex_segments(); |
| 158 | |
| 159 | log_message('debug', "No URI present. Default controller set."); |
| 160 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Set the Route |
| 166 | * |
| 167 | * This function takes an array of URI segments as |
| 168 | * input, and sets the current class/method |
| 169 | * |
| 170 | * @access private |
| 171 | * @param array |
| 172 | * @param bool |
| 173 | * @return void |
| 174 | */ |
| 175 | function _set_request($segments = array()) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 176 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | $segments = $this->_validate_request($segments); |
| 178 | |
| 179 | if (count($segments) == 0) |
| 180 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 181 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | $this->set_class($segments[0]); |
| 185 | |
| 186 | if (isset($segments[1])) |
| 187 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 188 | // A standard method request |
| 189 | $this->set_method($segments[1]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | } |
| 191 | else |
| 192 | { |
| 193 | // This lets the "routed" segment array identify that the default |
| 194 | // index method is being used. |
| 195 | $segments[1] = 'index'; |
| 196 | } |
| 197 | |
| 198 | // Update our "routed" segment array to contain the segments. |
| 199 | // Note: If there is no custom routing, this array will be |
| 200 | // identical to $this->uri->segments |
| 201 | $this->uri->rsegments = $segments; |
| 202 | } |
| 203 | |
| 204 | // -------------------------------------------------------------------- |
| 205 | |
| 206 | /** |
| 207 | * Validates the supplied segments. Attempts to determine the path to |
| 208 | * the controller. |
| 209 | * |
| 210 | * @access private |
| 211 | * @param array |
| 212 | * @return array |
| 213 | */ |
| 214 | function _validate_request($segments) |
| 215 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 216 | if (count($segments) == 0) |
| 217 | { |
| 218 | return $segments; |
| 219 | } |
| 220 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | // Does the requested controller exist in the root folder? |
| 222 | if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) |
| 223 | { |
| 224 | return $segments; |
| 225 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | // Is the controller in a sub-folder? |
| 228 | if (is_dir(APPPATH.'controllers/'.$segments[0])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 229 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | // Set the directory and remove it from the segment array |
| 231 | $this->set_directory($segments[0]); |
| 232 | $segments = array_slice($segments, 1); |
| 233 | |
| 234 | if (count($segments) > 0) |
| 235 | { |
| 236 | // Does the requested controller exist in the sub-folder? |
| 237 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) |
| 238 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 239 | show_404($this->fetch_directory().$segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | else |
| 243 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 244 | // Is the method being specified in the route? |
| 245 | if (strpos($this->default_controller, '/') !== FALSE) |
| 246 | { |
| 247 | $x = explode('/', $this->default_controller); |
| 248 | |
| 249 | $this->set_class($x[0]); |
| 250 | $this->set_method($x[1]); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | $this->set_class($this->default_controller); |
| 255 | $this->set_method('index'); |
| 256 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | |
| 258 | // Does the default controller exist in the sub-folder? |
| 259 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) |
| 260 | { |
| 261 | $this->directory = ''; |
| 262 | return array(); |
| 263 | } |
| 264 | |
| 265 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | return $segments; |
| 268 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 269 | |
| 270 | |
| 271 | // If we've gotten this far it means that the URI does not correlate to a valid |
| 272 | // controller class. We will now see if there is an override |
| 273 | if (isset($this->routes['404_override']) AND $this->routes['404_override'] != '') |
| 274 | { |
| 275 | if (strpos($this->routes['404_override'], '/') !== FALSE) |
| 276 | { |
| 277 | $x = explode('/', $this->routes['404_override']); |
| 278 | |
| 279 | $this->set_class($x[0]); |
| 280 | $this->set_method($x[1]); |
| 281 | |
| 282 | return $x; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // Nothing else to do at this point but show a 404 |
| 287 | show_404($segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | // -------------------------------------------------------------------- |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 291 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | /** |
| 293 | * Parse Routes |
| 294 | * |
| 295 | * This function matches any routes that may exist in |
| 296 | * the config/routes.php file against the URI to |
| 297 | * determine if the class/method need to be remapped. |
| 298 | * |
| 299 | * @access private |
| 300 | * @return void |
| 301 | */ |
| 302 | function _parse_routes() |
| 303 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | // Turn the segment array into a URI string |
| 305 | $uri = implode('/', $this->uri->segments); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 306 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | // Is there a literal match? If so we're done |
| 308 | if (isset($this->routes[$uri])) |
| 309 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 310 | return $this->_set_request(explode('/', $this->routes[$uri])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // Loop through the route array looking for wild-cards |
| 314 | foreach ($this->routes as $key => $val) |
| 315 | { |
| 316 | // Convert wild-cards to RegEx |
| 317 | $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | // Does the RegEx match? |
| 320 | if (preg_match('#^'.$key.'$#', $uri)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 321 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | // Do we have a back-reference? |
| 323 | if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) |
| 324 | { |
| 325 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
| 326 | } |
| 327 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 328 | return $this->_set_request(explode('/', $val)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | // If we got this far it means we didn't encounter a |
| 333 | // matching route so we'll set the site default route |
| 334 | $this->_set_request($this->uri->segments); |
| 335 | } |
| 336 | |
| 337 | // -------------------------------------------------------------------- |
| 338 | |
| 339 | /** |
| 340 | * Set the class name |
| 341 | * |
| 342 | * @access public |
| 343 | * @param string |
| 344 | * @return void |
| 345 | */ |
| 346 | function set_class($class) |
| 347 | { |
| 348 | $this->class = $class; |
| 349 | } |
| 350 | |
| 351 | // -------------------------------------------------------------------- |
| 352 | |
| 353 | /** |
| 354 | * Fetch the current class |
| 355 | * |
| 356 | * @access public |
| 357 | * @return string |
| 358 | */ |
| 359 | function fetch_class() |
| 360 | { |
| 361 | return $this->class; |
| 362 | } |
| 363 | |
| 364 | // -------------------------------------------------------------------- |
| 365 | |
| 366 | /** |
| 367 | * Set the method name |
| 368 | * |
| 369 | * @access public |
| 370 | * @param string |
| 371 | * @return void |
| 372 | */ |
| 373 | function set_method($method) |
| 374 | { |
| 375 | $this->method = $method; |
| 376 | } |
| 377 | |
| 378 | // -------------------------------------------------------------------- |
| 379 | |
| 380 | /** |
| 381 | * Fetch the current method |
| 382 | * |
| 383 | * @access public |
| 384 | * @return string |
| 385 | */ |
| 386 | function fetch_method() |
| 387 | { |
| 388 | if ($this->method == $this->fetch_class()) |
| 389 | { |
| 390 | return 'index'; |
| 391 | } |
| 392 | |
| 393 | return $this->method; |
| 394 | } |
| 395 | |
| 396 | // -------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
| 399 | * Set the directory name |
| 400 | * |
| 401 | * @access public |
| 402 | * @param string |
| 403 | * @return void |
| 404 | */ |
| 405 | function set_directory($dir) |
| 406 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 407 | $this->directory = trim($dir, '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // -------------------------------------------------------------------- |
| 411 | |
| 412 | /** |
| 413 | * Fetch the sub-directory (if any) that contains the requested controller class |
| 414 | * |
| 415 | * @access public |
| 416 | * @return string |
| 417 | */ |
| 418 | function fetch_directory() |
| 419 | { |
| 420 | return $this->directory; |
| 421 | } |
| 422 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame^] | 423 | // -------------------------------------------------------------------- |
| 424 | |
| 425 | /** |
| 426 | * Set the controller overrides |
| 427 | * |
| 428 | * @access public |
| 429 | * @param array |
| 430 | * @return null |
| 431 | */ |
| 432 | function _set_overrides($routing) |
| 433 | { |
| 434 | if ( ! is_array($routing)) |
| 435 | { |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | if (isset($routing['directory'])) |
| 440 | { |
| 441 | $this->set_directory($routing['directory']); |
| 442 | } |
| 443 | |
| 444 | if (isset($routing['controller']) AND $routing['controller'] != '') |
| 445 | { |
| 446 | $this->set_class($routing['controller']); |
| 447 | } |
| 448 | |
| 449 | if (isset($routing['function'])) |
| 450 | { |
| 451 | $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function']; |
| 452 | $this->set_method($routing['function']); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | } |
| 458 | // END Router Class |
| 459 | |
| 460 | /* End of file Router.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 461 | /* Location: ./system/core/Router.php */ |