blob: d7740f5f3cff4d06e6def9e6d4f5edd3657b3b7e [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?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 */
29class CI_Router {
30
31 var $config;
32 var $uri_string = '';
33 var $segments = array();
admin99bccd62006-09-21 17:05:40 +000034 var $rsegments = array();
adminb0dd10f2006-08-25 17:25:49 +000035 var $routes = array();
36 var $class = '';
37 var $method = 'index';
admin45c872b2006-08-26 04:51:38 +000038 var $directory = '';
adminb0dd10f2006-08-25 17:25:49 +000039 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 }
admin592cdcb2006-09-22 18:45:42 +000080
81 // Load the routes.php file and set the default controller
admin99bccd62006-09-21 17:05:40 +000082 @include_once(APPPATH.'config/routes'.EXT);
adminb0dd10f2006-08-25 17:25:49 +000083 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
84 unset($route);
85
admin592cdcb2006-09-22 18:45:42 +000086 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
adminb0dd10f2006-08-25 17:25:49 +000087
admin592cdcb2006-09-22 18:45:42 +000088 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
admindbd8aec2006-09-22 19:20:09 +000089 if (($this->uri_string = $this->_get_uri_string()) == '')
adminb0dd10f2006-08-25 17:25:49 +000090 {
91 if ($this->default_controller === FALSE)
92 {
93 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
94 }
95
96 $this->set_class($this->default_controller);
97 $this->set_method('index');
98
99 log_message('debug', "No URI present. Default controller set.");
100 return;
101 }
admin45c872b2006-08-26 04:51:38 +0000102 unset($this->routes['default_controller']);
adminb0dd10f2006-08-25 17:25:49 +0000103
104 // Do we need to remove the suffix specified in the config file?
105 if ($this->config->item('url_suffix') != "")
106 {
107 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
108 }
109
110 // Explode the URI Segments. The individual segments will
admin45c872b2006-08-26 04:51:38 +0000111 // be stored in the $this->segments array.
admin45c872b2006-08-26 04:51:38 +0000112 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
113 {
114 // Filter segments for security
115 $val = trim($this->_filter_uri($val));
116
117 if ($val != '')
admine07fbb32006-08-26 17:11:01 +0000118 $this->segments[] = $val;
admin45c872b2006-08-26 04:51:38 +0000119 }
adminb0dd10f2006-08-25 17:25:49 +0000120
admine07fbb32006-08-26 17:11:01 +0000121 // Parse any custom routing that may exist
122 $this->_parse_routes();
admin45c872b2006-08-26 04:51:38 +0000123
admine07fbb32006-08-26 17:11:01 +0000124 // Re-index the segment array so that it starts with 1 rather than 0
admin99bccd62006-09-21 17:05:40 +0000125 $this->_reindex_segments();
adminb0dd10f2006-08-25 17:25:49 +0000126 }
127 // END _set_route_mapping()
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Compile Segments
133 *
134 * This function takes an array of URI segments as
135 * input, and puts it into the $this->segments array.
136 * It also sets the current class/method
137 *
138 * @access private
139 * @param array
140 * @param bool
141 * @return void
142 */
admin45c872b2006-08-26 04:51:38 +0000143 function _compile_segments($segments = array())
144 {
145 $segments = $this->_validate_segments($segments);
adminb0dd10f2006-08-25 17:25:49 +0000146
admin45c872b2006-08-26 04:51:38 +0000147 if (count($segments) == 0)
148 {
149 return;
150 }
151
admin83b05a82006-09-25 21:06:46 +0000152 $this->set_class($segments[0]);
adminb0dd10f2006-08-25 17:25:49 +0000153
admin83b05a82006-09-25 21:06:46 +0000154 if (isset($segments[1]))
adminb0dd10f2006-08-25 17:25:49 +0000155 {
156 // A scaffolding request. No funny business with the URL
admin83b05a82006-09-25 21:06:46 +0000157 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
adminb0dd10f2006-08-25 17:25:49 +0000158 {
159 $this->scaffolding_request = TRUE;
160 unset($this->routes['scaffolding_trigger']);
161 }
162 else
163 {
164 // A standard method request
admin83b05a82006-09-25 21:06:46 +0000165 $this->set_method($segments[1]);
adminb0dd10f2006-08-25 17:25:49 +0000166 }
167 }
admin99bccd62006-09-21 17:05:40 +0000168
169 // Update our "routed" segment array to contain the segments.
170 // Note: If there is no custom routing, this array will be
171 // identical to $this->segments
172 $this->rsegments = $segments;
adminb0dd10f2006-08-25 17:25:49 +0000173 }
174 // END _compile_segments()
175
176 // --------------------------------------------------------------------
177
178 /**
admin45c872b2006-08-26 04:51:38 +0000179 * Validates the supplied segments. Attempts to determine the path to
180 * the controller.
181 *
182 * @access private
183 * @param array
184 * @return array
185 */
186 function _validate_segments($segments)
187 {
admine07fbb32006-08-26 17:11:01 +0000188 // Does the requested controller exist in the root folder?
admin83b05a82006-09-25 21:06:46 +0000189 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
admin45c872b2006-08-26 04:51:38 +0000190 {
admine07fbb32006-08-26 17:11:01 +0000191 return $segments;
admin45c872b2006-08-26 04:51:38 +0000192 }
admin1cf89aa2006-09-03 18:24:39 +0000193
admine07fbb32006-08-26 17:11:01 +0000194 // Is the controller in a sub-folder?
admin83b05a82006-09-25 21:06:46 +0000195 if (is_dir(APPPATH.'controllers/'.$segments[0]))
admin1cf89aa2006-09-03 18:24:39 +0000196 {
admine07fbb32006-08-26 17:11:01 +0000197 // Set the directory and remove it from the segment array
admin83b05a82006-09-25 21:06:46 +0000198 $this->set_directory($segments[0]);
admine07fbb32006-08-26 17:11:01 +0000199 $segments = array_slice($segments, 1);
200
admin1cf89aa2006-09-03 18:24:39 +0000201 if (count($segments) > 0)
202 {
203 // Does the requested controller exist in the sub-folder?
admin83b05a82006-09-25 21:06:46 +0000204 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
admin1cf89aa2006-09-03 18:24:39 +0000205 {
206 show_404();
207 }
208 }
209 else
admine07fbb32006-08-26 17:11:01 +0000210 {
211 $this->set_class($this->default_controller);
212 $this->set_method('index');
admin27818492006-09-05 03:31:28 +0000213
214 // Does the default controller exist in the sub-folder?
215 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
216 {
217 $this->directory = '';
218 return array();
219 }
220
admine07fbb32006-08-26 17:11:01 +0000221 }
222
223 return $segments;
224 }
225
226 // Can't find the requested controller...
227 show_404();
admin45c872b2006-08-26 04:51:38 +0000228 }
229 // END _validate_segments()
admin99bccd62006-09-21 17:05:40 +0000230
231 // --------------------------------------------------------------------
232 /**
233 * Re-index Segments
234 *
235 * This function re-indexes the $this->segment array so that it
236 * starts at 1 rather then 0. Doing so makes it simpler to
237 * use functions like $this->uri->segment(n) since there is
238 * a 1:1 relationship between the segment array and the actual segments.
239 *
240 * @access private
241 * @return void
242 */
243 function _reindex_segments()
244 {
245 // Is the routed segment array different then the main segment array?
246 $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE;
247
248 $i = 1;
249 foreach ($this->segments as $val)
250 {
251 $this->segments[$i++] = $val;
252 }
admin83b05a82006-09-25 21:06:46 +0000253 unset($this->segments[0]);
admin99bccd62006-09-21 17:05:40 +0000254
255 if ($diff == FALSE)
256 {
257 $this->rsegments = $this->segments;
258 }
259 else
260 {
261 $i = 1;
262 foreach ($this->rsegments as $val)
263 {
264 $this->rsegments[$i++] = $val;
265 }
admin83b05a82006-09-25 21:06:46 +0000266 unset($this->rsegments[0]);
admin99bccd62006-09-21 17:05:40 +0000267 }
268 }
269 // END _reindex_segments()
admin45c872b2006-08-26 04:51:38 +0000270
271 // --------------------------------------------------------------------
272
273 /**
admin592cdcb2006-09-22 18:45:42 +0000274 * Get the URI String
275 *
276 * @access private
277 * @return string
278 */
279 function _get_uri_string()
280 {
281 if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
282 {
283 $path_info = getenv('PATH_INFO');
284 if ($path_info != '' AND $path_info != "/".SELF)
285 {
286 return $path_info;
287 }
288 else
289 {
290 $req_uri = $this->_parse_request_uri();
291
292 if ($req_uri != "")
293 {
294 return $req_uri;
295 }
296 else
297 {
298 $path_info = getenv('ORIG_PATH_INFO');
299 if ($path_info != '' AND $path_info != "/".SELF)
300 {
301 return $path_info;
302 }
303 else
304 {
305 return getenv('QUERY_STRING');
306 }
307 }
308 }
309 }
310 else
311 {
312 $uri = strtoupper($this->config->item('uri_protocol'));
313
314 if ($uri == 'REQUEST_URI')
315 {
316 return $this->_parse_request_uri();
317 }
318
319 return getenv($uri);
320 }
321 }
322 // END _get_uri_string()
323
324 // --------------------------------------------------------------------
325
326 /**
327 * Parse the REQUEST_URI
328 *
329 * Due to the way REQUEST_URI works it usually contains path info
330 * that makes it unusable as URI data. We'll trim off the unnecessary
331 * data, hopefully arriving at a valid URI that we can use.
332 *
333 * @access private
334 * @return string
335 */
336 function _parse_request_uri()
337 {
admindbd8aec2006-09-22 19:20:09 +0000338 if (($request_uri = getenv('REQUEST_URI')) == '')
339 {
340 return '';
341 }
342
admin592cdcb2006-09-22 18:45:42 +0000343 $fc_path = FCPATH;
344
345 if (strpos($request_uri, '?') !== FALSE)
346 {
347 $fc_path .= '?';
348 }
349
350 $parsed_uri = explode("/", preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $request_uri)));
351
352 $i = 0;
353 foreach(explode("/", $fc_path) as $segment)
354 {
355 if ($segment == $parsed_uri[$i])
356 {
357 $i++;
358 }
359 }
360
361 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
362
363 if ($parsed_uri != '')
364 {
365 $parsed_uri = '/'.$parsed_uri;
366 }
367
368 return $parsed_uri;
369 }
370 // END _parse_request_uri()
371
372 // --------------------------------------------------------------------
373
374 /**
adminb0dd10f2006-08-25 17:25:49 +0000375 * Filter segments for malicious characters
376 *
377 * @access private
378 * @param string
379 * @return string
380 */
381 function _filter_uri($str)
382 {
admin1082bdd2006-08-27 19:32:02 +0000383 if ($this->config->item('permitted_uri_chars') != '')
384 {
385 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
386 {
387 exit('The URI you submitted has disallowed characters: '.$str);
388 }
389 }
390 return $str;
adminb0dd10f2006-08-25 17:25:49 +0000391 }
392 // END _filter_uri()
393
394 // --------------------------------------------------------------------
395
396 /**
adminb0dd10f2006-08-25 17:25:49 +0000397 * Parse Routes
398 *
399 * This function matches any routes that may exist in
400 * the config/routes.php file against the URI to
401 * determine if the class/method need to be remapped.
402 *
403 * @access private
404 * @return void
405 */
406 function _parse_routes()
407 {
admine07fbb32006-08-26 17:11:01 +0000408 // Do we even have any custom routing to deal with?
409 if (count($this->routes) == 0)
410 {
411 $this->_compile_segments($this->segments);
412 return;
413 }
414
adminb0dd10f2006-08-25 17:25:49 +0000415 // Turn the segment array into a URI string
416 $uri = implode('/', $this->segments);
417 $num = count($this->segments);
418
419 // Is there a literal match? If so we're done
420 if (isset($this->routes[$uri]))
421 {
admin45c872b2006-08-26 04:51:38 +0000422 $this->_compile_segments(explode('/', $this->routes[$uri]));
adminb0dd10f2006-08-25 17:25:49 +0000423 return;
424 }
admine07fbb32006-08-26 17:11:01 +0000425
adminb0dd10f2006-08-25 17:25:49 +0000426 // Loop through the route array looking for wildcards
admind4e95072006-08-26 01:15:06 +0000427 foreach (array_slice($this->routes, 1) as $key => $val)
adminb071bb52006-08-26 19:28:37 +0000428 {
admind4e95072006-08-26 01:15:06 +0000429 // Convert wildcards to RegEx
430 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
431
admin45c872b2006-08-26 04:51:38 +0000432 // Does the RegEx match?
admin71430b42006-09-15 20:29:25 +0000433 if (preg_match('#^'.$key.'$#', $uri))
admind4e95072006-08-26 01:15:06 +0000434 {
admin45c872b2006-08-26 04:51:38 +0000435 // Do we have a back-reference?
admind4e95072006-08-26 01:15:06 +0000436 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
437 {
admin71430b42006-09-15 20:29:25 +0000438 $val = preg_replace('#^'.$key.'$#', $val, $uri);
admind4e95072006-08-26 01:15:06 +0000439 }
440
admin45c872b2006-08-26 04:51:38 +0000441 $this->_compile_segments(explode('/', $val));
442 return;
adminb0dd10f2006-08-25 17:25:49 +0000443 }
admine07fbb32006-08-26 17:11:01 +0000444 }
445
446 // If we got this far it means we didn't encounter a
447 // matching route so we'll set the site default route
448 $this->_compile_segments($this->segments);
adminb0dd10f2006-08-25 17:25:49 +0000449 }
450 // END set_method()
admin45c872b2006-08-26 04:51:38 +0000451
452 // --------------------------------------------------------------------
453
454 /**
455 * Set the class name
456 *
457 * @access public
458 * @param string
459 * @return void
460 */
461 function set_class($class)
462 {
463 $this->class = $class;
464 }
465 // END set_class()
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Fetch the current class
471 *
472 * @access public
473 * @return string
474 */
475 function fetch_class()
476 {
477 return $this->class;
478 }
479 // END fetch_class()
480
481 // --------------------------------------------------------------------
482
483 /**
484 * Set the method name
485 *
486 * @access public
487 * @param string
488 * @return void
489 */
490 function set_method($method)
491 {
492 $this->method = $method;
493 }
494 // END set_method()
495
496 // --------------------------------------------------------------------
497
498 /**
499 * Fetch the current method
500 *
501 * @access public
502 * @return string
503 */
504 function fetch_method()
505 {
506 return $this->method;
507 }
508 // END fetch_method()
509
510 // --------------------------------------------------------------------
511
512 /**
513 * Set the directory name
514 *
515 * @access public
516 * @param string
517 * @return void
518 */
519 function set_directory($dir)
520 {
521 $this->directory = $dir.'/';
522 }
523 // END set_directory()
524
525 // --------------------------------------------------------------------
526
527 /**
528 * Fetch the sub-directory (if any) that contains the requested controller class
529 *
530 * @access public
531 * @return string
532 */
533 function fetch_directory()
534 {
535 return $this->directory;
536 }
537 // END fetch_directory()
538
adminb0dd10f2006-08-25 17:25:49 +0000539}
540// END Router Class
541?>