blob: 06037209b9d317861cf42ca0429ec1e075a1cc0d [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();
admin33de9a12006-09-28 06:50:16 +000036 var $error_routes = array();
adminb0dd10f2006-08-25 17:25:49 +000037 var $class = '';
38 var $method = 'index';
admin45c872b2006-08-26 04:51:38 +000039 var $directory = '';
adminb0dd10f2006-08-25 17:25:49 +000040 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 {
admin7099a582006-10-10 17:47:59 +000051 $this->config =& load_class('Config');
adminb0dd10f2006-08-25 17:25:49 +000052 $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 {
admin17a890d2006-09-27 20:42:42 +000069 // Are query strings enabled in the config file?
70 // If so, we're done since segment based URIs are not used with query strings.
adminb0dd10f2006-08-25 17:25:49 +000071 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
72 {
73 $this->set_class($_GET[$this->config->item('controller_trigger')]);
74
75 if (isset($_GET[$this->config->item('function_trigger')]))
76 {
77 $this->set_method($_GET[$this->config->item('function_trigger')]);
78 }
79
80 return;
81 }
admin592cdcb2006-09-22 18:45:42 +000082
admin17a890d2006-09-27 20:42:42 +000083 // Load the routes.php file.
admin99bccd62006-09-21 17:05:40 +000084 @include_once(APPPATH.'config/routes'.EXT);
adminb0dd10f2006-08-25 17:25:49 +000085 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
86 unset($route);
87
admin17a890d2006-09-27 20:42:42 +000088 // Set the default controller so we can display it in the event
89 // the URI doesn't correlated to a valid controller.
admin33de9a12006-09-28 06:50:16 +000090 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
91
admin17a890d2006-09-27 20:42:42 +000092 // Fetch the complete URI string
93 $this->uri_string = $this->_get_uri_string();
94
95 // If the URI contains only a slash we'll kill it
96 if ($this->uri_string == '/')
97 {
98 $this->uri_string = '';
99 }
100
admin592cdcb2006-09-22 18:45:42 +0000101 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
admin17a890d2006-09-27 20:42:42 +0000102 if ($this->uri_string == '')
adminb0dd10f2006-08-25 17:25:49 +0000103 {
104 if ($this->default_controller === FALSE)
105 {
106 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
107 }
108
109 $this->set_class($this->default_controller);
110 $this->set_method('index');
111
112 log_message('debug', "No URI present. Default controller set.");
113 return;
114 }
admin45c872b2006-08-26 04:51:38 +0000115 unset($this->routes['default_controller']);
adminb0dd10f2006-08-25 17:25:49 +0000116
117 // Do we need to remove the suffix specified in the config file?
118 if ($this->config->item('url_suffix') != "")
119 {
120 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
121 }
admin10c3f412006-10-08 07:21:12 +0000122
adminb0dd10f2006-08-25 17:25:49 +0000123 // Explode the URI Segments. The individual segments will
admin45c872b2006-08-26 04:51:38 +0000124 // be stored in the $this->segments array.
admin45c872b2006-08-26 04:51:38 +0000125 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
126 {
127 // Filter segments for security
128 $val = trim($this->_filter_uri($val));
129
130 if ($val != '')
admine07fbb32006-08-26 17:11:01 +0000131 $this->segments[] = $val;
admin45c872b2006-08-26 04:51:38 +0000132 }
adminb0dd10f2006-08-25 17:25:49 +0000133
admine07fbb32006-08-26 17:11:01 +0000134 // Parse any custom routing that may exist
135 $this->_parse_routes();
admin45c872b2006-08-26 04:51:38 +0000136
admine07fbb32006-08-26 17:11:01 +0000137 // Re-index the segment array so that it starts with 1 rather than 0
admin99bccd62006-09-21 17:05:40 +0000138 $this->_reindex_segments();
adminb0dd10f2006-08-25 17:25:49 +0000139 }
adminb0dd10f2006-08-25 17:25:49 +0000140
141 // --------------------------------------------------------------------
142
143 /**
144 * Compile Segments
145 *
146 * This function takes an array of URI segments as
147 * input, and puts it into the $this->segments array.
148 * It also sets the current class/method
149 *
150 * @access private
151 * @param array
152 * @param bool
153 * @return void
154 */
admin45c872b2006-08-26 04:51:38 +0000155 function _compile_segments($segments = array())
156 {
157 $segments = $this->_validate_segments($segments);
adminb0dd10f2006-08-25 17:25:49 +0000158
admin45c872b2006-08-26 04:51:38 +0000159 if (count($segments) == 0)
160 {
161 return;
162 }
163
admin83b05a82006-09-25 21:06:46 +0000164 $this->set_class($segments[0]);
adminb0dd10f2006-08-25 17:25:49 +0000165
admin83b05a82006-09-25 21:06:46 +0000166 if (isset($segments[1]))
adminb0dd10f2006-08-25 17:25:49 +0000167 {
168 // A scaffolding request. No funny business with the URL
admin83b05a82006-09-25 21:06:46 +0000169 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
adminb0dd10f2006-08-25 17:25:49 +0000170 {
171 $this->scaffolding_request = TRUE;
172 unset($this->routes['scaffolding_trigger']);
173 }
174 else
175 {
176 // A standard method request
admin83b05a82006-09-25 21:06:46 +0000177 $this->set_method($segments[1]);
adminb0dd10f2006-08-25 17:25:49 +0000178 }
179 }
admin99bccd62006-09-21 17:05:40 +0000180
181 // Update our "routed" segment array to contain the segments.
182 // Note: If there is no custom routing, this array will be
183 // identical to $this->segments
184 $this->rsegments = $segments;
adminb0dd10f2006-08-25 17:25:49 +0000185 }
adminb0dd10f2006-08-25 17:25:49 +0000186
187 // --------------------------------------------------------------------
188
189 /**
admin45c872b2006-08-26 04:51:38 +0000190 * Validates the supplied segments. Attempts to determine the path to
191 * the controller.
192 *
193 * @access private
194 * @param array
195 * @return array
196 */
197 function _validate_segments($segments)
198 {
admine07fbb32006-08-26 17:11:01 +0000199 // Does the requested controller exist in the root folder?
admin83b05a82006-09-25 21:06:46 +0000200 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
admin45c872b2006-08-26 04:51:38 +0000201 {
admine07fbb32006-08-26 17:11:01 +0000202 return $segments;
admin45c872b2006-08-26 04:51:38 +0000203 }
admin1cf89aa2006-09-03 18:24:39 +0000204
admine07fbb32006-08-26 17:11:01 +0000205 // Is the controller in a sub-folder?
admin83b05a82006-09-25 21:06:46 +0000206 if (is_dir(APPPATH.'controllers/'.$segments[0]))
admin1cf89aa2006-09-03 18:24:39 +0000207 {
admine07fbb32006-08-26 17:11:01 +0000208 // Set the directory and remove it from the segment array
admin83b05a82006-09-25 21:06:46 +0000209 $this->set_directory($segments[0]);
admine07fbb32006-08-26 17:11:01 +0000210 $segments = array_slice($segments, 1);
211
admin1cf89aa2006-09-03 18:24:39 +0000212 if (count($segments) > 0)
213 {
214 // Does the requested controller exist in the sub-folder?
admin83b05a82006-09-25 21:06:46 +0000215 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
admin1cf89aa2006-09-03 18:24:39 +0000216 {
217 show_404();
218 }
219 }
220 else
admine07fbb32006-08-26 17:11:01 +0000221 {
222 $this->set_class($this->default_controller);
223 $this->set_method('index');
admin27818492006-09-05 03:31:28 +0000224
225 // Does the default controller exist in the sub-folder?
226 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
227 {
228 $this->directory = '';
229 return array();
230 }
231
admine07fbb32006-08-26 17:11:01 +0000232 }
233
234 return $segments;
235 }
236
237 // Can't find the requested controller...
238 show_404();
admin45c872b2006-08-26 04:51:38 +0000239 }
admin99bccd62006-09-21 17:05:40 +0000240
241 // --------------------------------------------------------------------
242 /**
243 * Re-index Segments
244 *
245 * This function re-indexes the $this->segment array so that it
246 * starts at 1 rather then 0. Doing so makes it simpler to
247 * use functions like $this->uri->segment(n) since there is
248 * a 1:1 relationship between the segment array and the actual segments.
249 *
250 * @access private
251 * @return void
252 */
253 function _reindex_segments()
254 {
255 // Is the routed segment array different then the main segment array?
256 $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE;
257
258 $i = 1;
259 foreach ($this->segments as $val)
260 {
261 $this->segments[$i++] = $val;
262 }
admin83b05a82006-09-25 21:06:46 +0000263 unset($this->segments[0]);
admin99bccd62006-09-21 17:05:40 +0000264
265 if ($diff == FALSE)
266 {
267 $this->rsegments = $this->segments;
268 }
269 else
270 {
271 $i = 1;
272 foreach ($this->rsegments as $val)
273 {
274 $this->rsegments[$i++] = $val;
275 }
admin83b05a82006-09-25 21:06:46 +0000276 unset($this->rsegments[0]);
admin99bccd62006-09-21 17:05:40 +0000277 }
278 }
admin45c872b2006-08-26 04:51:38 +0000279
280 // --------------------------------------------------------------------
281
282 /**
admin592cdcb2006-09-22 18:45:42 +0000283 * Get the URI String
284 *
285 * @access private
286 * @return string
287 */
288 function _get_uri_string()
289 {
290 if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
291 {
admin813d0ac2006-10-01 20:36:39 +0000292 // If the URL has a question mark then it's simplest to just
293 // build the URI string from the zero index of the $_GET array.
294 // This avoids having to deal with $_SERVER variables, which
295 // can be unreliable on some servers
296 if (is_array($_GET) AND count($_GET) == 1)
297 {
298 return current(array_keys($_GET));
299 }
300
301 // Is there a PATH_INFO variable?
302 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
303 $path_info = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
304
admin592cdcb2006-09-22 18:45:42 +0000305 if ($path_info != '' AND $path_info != "/".SELF)
306 {
307 return $path_info;
308 }
309 else
310 {
admin813d0ac2006-10-01 20:36:39 +0000311 // OK, how about REQUEST_URI?
admin592cdcb2006-09-22 18:45:42 +0000312 $req_uri = $this->_parse_request_uri();
313
314 if ($req_uri != "")
315 {
316 return $req_uri;
317 }
318 else
319 {
admin813d0ac2006-10-01 20:36:39 +0000320 // Hm... maybe the ORIG_PATH_INFO variable exists?
321 $path_info = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
admin592cdcb2006-09-22 18:45:42 +0000322 if ($path_info != '' AND $path_info != "/".SELF)
323 {
324 return $path_info;
325 }
326 else
327 {
admin813d0ac2006-10-01 20:36:39 +0000328 // At this point we've exhauseted all our options.
329 // Hopefully QUERY_STRING exists. If not, there's nothing else we can try.
330 $query_string = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
331
332 if ($query_string != '')
333 {
334 return $query_string;
335 }
336
337 return '';
admin592cdcb2006-09-22 18:45:42 +0000338 }
339 }
340 }
341 }
342 else
343 {
344 $uri = strtoupper($this->config->item('uri_protocol'));
345
346 if ($uri == 'REQUEST_URI')
347 {
348 return $this->_parse_request_uri();
349 }
350
admin813d0ac2006-10-01 20:36:39 +0000351 return (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
admin592cdcb2006-09-22 18:45:42 +0000352 }
353 }
admin592cdcb2006-09-22 18:45:42 +0000354
355 // --------------------------------------------------------------------
356
357 /**
358 * Parse the REQUEST_URI
359 *
360 * Due to the way REQUEST_URI works it usually contains path info
361 * that makes it unusable as URI data. We'll trim off the unnecessary
362 * data, hopefully arriving at a valid URI that we can use.
363 *
364 * @access private
365 * @return string
366 */
367 function _parse_request_uri()
368 {
admin4c1ab6c2006-10-11 21:48:33 +0000369 if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
admindbd8aec2006-09-22 19:20:09 +0000370 {
371 return '';
372 }
373
admin4c1ab6c2006-10-11 21:48:33 +0000374 $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
375
admin9a661812006-10-16 19:02:48 +0000376 if ($request_uri == '' OR $request_uri == $this->config->item('index_page'))
admin4c1ab6c2006-10-11 21:48:33 +0000377 {
378 return '';
379 }
admin592cdcb2006-09-22 18:45:42 +0000380
admin4c1ab6c2006-10-11 21:48:33 +0000381 $fc_path = FCPATH;
admin592cdcb2006-09-22 18:45:42 +0000382 if (strpos($request_uri, '?') !== FALSE)
383 {
384 $fc_path .= '?';
385 }
386
admin4c1ab6c2006-10-11 21:48:33 +0000387 $parsed_uri = explode("/", $request_uri);
388
admin592cdcb2006-09-22 18:45:42 +0000389 $i = 0;
390 foreach(explode("/", $fc_path) as $segment)
391 {
admin4c1ab6c2006-10-11 21:48:33 +0000392 if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i])
admin592cdcb2006-09-22 18:45:42 +0000393 {
394 $i++;
395 }
396 }
397
398 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
399
400 if ($parsed_uri != '')
401 {
402 $parsed_uri = '/'.$parsed_uri;
403 }
404
405 return $parsed_uri;
406 }
admin592cdcb2006-09-22 18:45:42 +0000407
408 // --------------------------------------------------------------------
409
410 /**
adminb0dd10f2006-08-25 17:25:49 +0000411 * Filter segments for malicious characters
412 *
413 * @access private
414 * @param string
415 * @return string
416 */
417 function _filter_uri($str)
418 {
admin1082bdd2006-08-27 19:32:02 +0000419 if ($this->config->item('permitted_uri_chars') != '')
420 {
421 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
422 {
423 exit('The URI you submitted has disallowed characters: '.$str);
424 }
425 }
426 return $str;
adminb0dd10f2006-08-25 17:25:49 +0000427 }
adminb0dd10f2006-08-25 17:25:49 +0000428
429 // --------------------------------------------------------------------
430
431 /**
adminb0dd10f2006-08-25 17:25:49 +0000432 * Parse Routes
433 *
434 * This function matches any routes that may exist in
435 * the config/routes.php file against the URI to
436 * determine if the class/method need to be remapped.
437 *
438 * @access private
439 * @return void
440 */
441 function _parse_routes()
442 {
admine07fbb32006-08-26 17:11:01 +0000443 // Do we even have any custom routing to deal with?
444 if (count($this->routes) == 0)
445 {
446 $this->_compile_segments($this->segments);
447 return;
448 }
449
adminb0dd10f2006-08-25 17:25:49 +0000450 // Turn the segment array into a URI string
451 $uri = implode('/', $this->segments);
452 $num = count($this->segments);
453
454 // Is there a literal match? If so we're done
455 if (isset($this->routes[$uri]))
456 {
admin45c872b2006-08-26 04:51:38 +0000457 $this->_compile_segments(explode('/', $this->routes[$uri]));
adminb0dd10f2006-08-25 17:25:49 +0000458 return;
459 }
admine07fbb32006-08-26 17:11:01 +0000460
adminb0dd10f2006-08-25 17:25:49 +0000461 // Loop through the route array looking for wildcards
admind4e95072006-08-26 01:15:06 +0000462 foreach (array_slice($this->routes, 1) as $key => $val)
adminb071bb52006-08-26 19:28:37 +0000463 {
admind4e95072006-08-26 01:15:06 +0000464 // Convert wildcards to RegEx
465 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
466
admin45c872b2006-08-26 04:51:38 +0000467 // Does the RegEx match?
admin71430b42006-09-15 20:29:25 +0000468 if (preg_match('#^'.$key.'$#', $uri))
admind4e95072006-08-26 01:15:06 +0000469 {
admin45c872b2006-08-26 04:51:38 +0000470 // Do we have a back-reference?
admind4e95072006-08-26 01:15:06 +0000471 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
472 {
admin71430b42006-09-15 20:29:25 +0000473 $val = preg_replace('#^'.$key.'$#', $val, $uri);
admind4e95072006-08-26 01:15:06 +0000474 }
475
admin45c872b2006-08-26 04:51:38 +0000476 $this->_compile_segments(explode('/', $val));
477 return;
adminb0dd10f2006-08-25 17:25:49 +0000478 }
admine07fbb32006-08-26 17:11:01 +0000479 }
480
481 // If we got this far it means we didn't encounter a
482 // matching route so we'll set the site default route
483 $this->_compile_segments($this->segments);
adminb0dd10f2006-08-25 17:25:49 +0000484 }
admin45c872b2006-08-26 04:51:38 +0000485
486 // --------------------------------------------------------------------
487
488 /**
489 * Set the class name
490 *
491 * @access public
492 * @param string
493 * @return void
494 */
495 function set_class($class)
496 {
497 $this->class = $class;
498 }
admin45c872b2006-08-26 04:51:38 +0000499
500 // --------------------------------------------------------------------
501
502 /**
503 * Fetch the current class
504 *
505 * @access public
506 * @return string
507 */
508 function fetch_class()
509 {
510 return $this->class;
511 }
admin45c872b2006-08-26 04:51:38 +0000512
513 // --------------------------------------------------------------------
514
515 /**
516 * Set the method name
517 *
518 * @access public
519 * @param string
520 * @return void
521 */
522 function set_method($method)
523 {
524 $this->method = $method;
525 }
admin45c872b2006-08-26 04:51:38 +0000526
527 // --------------------------------------------------------------------
528
529 /**
530 * Fetch the current method
531 *
532 * @access public
533 * @return string
534 */
535 function fetch_method()
536 {
admin08f60202006-10-03 05:28:00 +0000537 if ($this->method == $this->fetch_class())
538 {
539 return 'index';
540 }
541
admin45c872b2006-08-26 04:51:38 +0000542 return $this->method;
543 }
admin45c872b2006-08-26 04:51:38 +0000544
545 // --------------------------------------------------------------------
546
547 /**
548 * Set the directory name
549 *
550 * @access public
551 * @param string
552 * @return void
553 */
554 function set_directory($dir)
555 {
556 $this->directory = $dir.'/';
557 }
admin45c872b2006-08-26 04:51:38 +0000558
559 // --------------------------------------------------------------------
560
561 /**
562 * Fetch the sub-directory (if any) that contains the requested controller class
563 *
564 * @access public
565 * @return string
566 */
567 function fetch_directory()
568 {
569 return $this->directory;
570 }
admin45c872b2006-08-26 04:51:38 +0000571
adminb0dd10f2006-08-25 17:25:49 +0000572}
573// END Router Class
574?>