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