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