blob: 886433f37240f0e3ddacf7b25901aa8bdf4ccde8 [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Router Class
admine334c472006-10-21 19:44:22 +000020 *
adminb0dd10f2006-08-25 17:25:49 +000021 * 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 *
admine334c472006-10-21 19:44:22 +000047 * Runs the route mapping function.
adminb0dd10f2006-08-25 17:25:49 +000048 */
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 *
adminbd6bee72006-10-21 19:39:00 +000061 * This function determines what should be served based on the URI request,
adminb0dd10f2006-08-25 17:25:49 +000062 * 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 == '/')
admine334c472006-10-21 19:44:22 +000097 {
admin17a890d2006-09-27 20:42:42 +000098 $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
admine334c472006-10-21 19:44:22 +0000246 * starts at 1 rather then 0. Doing so makes it simpler to
admin99bccd62006-09-21 17:05:40 +0000247 * 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
admin90931312006-10-20 00:59:17 +0000295 // can be unreliable in some environments
admin813d0ac2006-10-01 20:36:39 +0000296 if (is_array($_GET) AND count($_GET) == 1)
297 {
admin7acd5812006-10-23 21:37:22 +0000298 // Note: Due to a bug in current() that affects some versions
299 // of PHP we can not pass function call directly into it
300 $keys = array_keys($_GET);
301 return current($keys);
admin813d0ac2006-10-01 20:36:39 +0000302 }
303
304 // Is there a PATH_INFO variable?
305 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
admin90931312006-10-20 00:59:17 +0000306 $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
307 if ($path != '' AND $path != "/".SELF)
308 {
309 return $path;
310 }
311
312 // No PATH_INFO?... What about QUERY_STRING?
313 $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
314 if ($path != '')
315 {
316 return $path;
317 }
admin813d0ac2006-10-01 20:36:39 +0000318
admin90931312006-10-20 00:59:17 +0000319 // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
320 $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
321 if ($path != '' AND $path != "/".SELF)
admin592cdcb2006-09-22 18:45:42 +0000322 {
admin90931312006-10-20 00:59:17 +0000323 return $path;
admin592cdcb2006-09-22 18:45:42 +0000324 }
admin90931312006-10-20 00:59:17 +0000325
326 // We've exhausted all our options...
327 return '';
admin592cdcb2006-09-22 18:45:42 +0000328 }
329 else
330 {
331 $uri = strtoupper($this->config->item('uri_protocol'));
332
333 if ($uri == 'REQUEST_URI')
334 {
335 return $this->_parse_request_uri();
336 }
337
admin813d0ac2006-10-01 20:36:39 +0000338 return (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
admin592cdcb2006-09-22 18:45:42 +0000339 }
340 }
admin592cdcb2006-09-22 18:45:42 +0000341
342 // --------------------------------------------------------------------
343
344 /**
345 * Parse the REQUEST_URI
346 *
347 * Due to the way REQUEST_URI works it usually contains path info
admine334c472006-10-21 19:44:22 +0000348 * that makes it unusable as URI data. We'll trim off the unnecessary
admin592cdcb2006-09-22 18:45:42 +0000349 * data, hopefully arriving at a valid URI that we can use.
350 *
351 * @access private
352 * @return string
353 */
354 function _parse_request_uri()
355 {
admin4c1ab6c2006-10-11 21:48:33 +0000356 if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
admindbd8aec2006-09-22 19:20:09 +0000357 {
358 return '';
359 }
360
admin4c1ab6c2006-10-11 21:48:33 +0000361 $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
362
admin90931312006-10-20 00:59:17 +0000363 if ($request_uri == '' OR $request_uri == SELF)
admin4c1ab6c2006-10-11 21:48:33 +0000364 {
365 return '';
366 }
admin592cdcb2006-09-22 18:45:42 +0000367
admin4c1ab6c2006-10-11 21:48:33 +0000368 $fc_path = FCPATH;
admin592cdcb2006-09-22 18:45:42 +0000369 if (strpos($request_uri, '?') !== FALSE)
370 {
371 $fc_path .= '?';
372 }
373
admin4c1ab6c2006-10-11 21:48:33 +0000374 $parsed_uri = explode("/", $request_uri);
375
admin592cdcb2006-09-22 18:45:42 +0000376 $i = 0;
377 foreach(explode("/", $fc_path) as $segment)
378 {
admin4c1ab6c2006-10-11 21:48:33 +0000379 if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i])
admin592cdcb2006-09-22 18:45:42 +0000380 {
381 $i++;
382 }
383 }
384
385 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
386
387 if ($parsed_uri != '')
388 {
389 $parsed_uri = '/'.$parsed_uri;
390 }
391
392 return $parsed_uri;
393 }
admin592cdcb2006-09-22 18:45:42 +0000394
395 // --------------------------------------------------------------------
396
397 /**
adminb0dd10f2006-08-25 17:25:49 +0000398 * Filter segments for malicious characters
399 *
400 * @access private
401 * @param string
402 * @return string
403 */
404 function _filter_uri($str)
405 {
admin1082bdd2006-08-27 19:32:02 +0000406 if ($this->config->item('permitted_uri_chars') != '')
407 {
408 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
admine334c472006-10-21 19:44:22 +0000409 {
admin1082bdd2006-08-27 19:32:02 +0000410 exit('The URI you submitted has disallowed characters: '.$str);
411 }
412 }
413 return $str;
adminb0dd10f2006-08-25 17:25:49 +0000414 }
adminb0dd10f2006-08-25 17:25:49 +0000415
416 // --------------------------------------------------------------------
417
418 /**
adminb0dd10f2006-08-25 17:25:49 +0000419 * Parse Routes
420 *
421 * This function matches any routes that may exist in
admine334c472006-10-21 19:44:22 +0000422 * the config/routes.php file against the URI to
adminb0dd10f2006-08-25 17:25:49 +0000423 * determine if the class/method need to be remapped.
424 *
425 * @access private
426 * @return void
427 */
428 function _parse_routes()
429 {
admine07fbb32006-08-26 17:11:01 +0000430 // Do we even have any custom routing to deal with?
431 if (count($this->routes) == 0)
432 {
433 $this->_compile_segments($this->segments);
434 return;
435 }
436
adminb0dd10f2006-08-25 17:25:49 +0000437 // Turn the segment array into a URI string
438 $uri = implode('/', $this->segments);
439 $num = count($this->segments);
440
441 // Is there a literal match? If so we're done
442 if (isset($this->routes[$uri]))
443 {
admin45c872b2006-08-26 04:51:38 +0000444 $this->_compile_segments(explode('/', $this->routes[$uri]));
adminb0dd10f2006-08-25 17:25:49 +0000445 return;
446 }
admine07fbb32006-08-26 17:11:01 +0000447
adminbd6bee72006-10-21 19:39:00 +0000448 // Loop through the route array looking for wild-cards
admind4e95072006-08-26 01:15:06 +0000449 foreach (array_slice($this->routes, 1) as $key => $val)
adminb071bb52006-08-26 19:28:37 +0000450 {
adminbd6bee72006-10-21 19:39:00 +0000451 // Convert wild-cards to RegEx
admind4e95072006-08-26 01:15:06 +0000452 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
453
admin45c872b2006-08-26 04:51:38 +0000454 // Does the RegEx match?
admin71430b42006-09-15 20:29:25 +0000455 if (preg_match('#^'.$key.'$#', $uri))
admind4e95072006-08-26 01:15:06 +0000456 {
admin45c872b2006-08-26 04:51:38 +0000457 // Do we have a back-reference?
admind4e95072006-08-26 01:15:06 +0000458 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
459 {
admin71430b42006-09-15 20:29:25 +0000460 $val = preg_replace('#^'.$key.'$#', $val, $uri);
admind4e95072006-08-26 01:15:06 +0000461 }
462
admin45c872b2006-08-26 04:51:38 +0000463 $this->_compile_segments(explode('/', $val));
464 return;
adminb0dd10f2006-08-25 17:25:49 +0000465 }
admine07fbb32006-08-26 17:11:01 +0000466 }
467
admine334c472006-10-21 19:44:22 +0000468 // If we got this far it means we didn't encounter a
admine07fbb32006-08-26 17:11:01 +0000469 // matching route so we'll set the site default route
470 $this->_compile_segments($this->segments);
adminb0dd10f2006-08-25 17:25:49 +0000471 }
admin45c872b2006-08-26 04:51:38 +0000472
473 // --------------------------------------------------------------------
474
475 /**
476 * Set the class name
477 *
478 * @access public
479 * @param string
480 * @return void
481 */
482 function set_class($class)
483 {
484 $this->class = $class;
485 }
admin45c872b2006-08-26 04:51:38 +0000486
487 // --------------------------------------------------------------------
488
489 /**
490 * Fetch the current class
491 *
492 * @access public
493 * @return string
494 */
495 function fetch_class()
496 {
497 return $this->class;
498 }
admin45c872b2006-08-26 04:51:38 +0000499
500 // --------------------------------------------------------------------
501
502 /**
503 * Set the method name
504 *
505 * @access public
506 * @param string
507 * @return void
508 */
509 function set_method($method)
510 {
511 $this->method = $method;
512 }
admin45c872b2006-08-26 04:51:38 +0000513
514 // --------------------------------------------------------------------
515
516 /**
517 * Fetch the current method
518 *
519 * @access public
520 * @return string
521 */
522 function fetch_method()
523 {
admin08f60202006-10-03 05:28:00 +0000524 if ($this->method == $this->fetch_class())
525 {
526 return 'index';
527 }
528
admin45c872b2006-08-26 04:51:38 +0000529 return $this->method;
530 }
admin45c872b2006-08-26 04:51:38 +0000531
532 // --------------------------------------------------------------------
533
534 /**
535 * Set the directory name
536 *
537 * @access public
538 * @param string
539 * @return void
540 */
541 function set_directory($dir)
542 {
543 $this->directory = $dir.'/';
544 }
admin45c872b2006-08-26 04:51:38 +0000545
546 // --------------------------------------------------------------------
547
548 /**
549 * Fetch the sub-directory (if any) that contains the requested controller class
550 *
551 * @access public
552 * @return string
553 */
554 function fetch_directory()
555 {
556 return $this->directory;
557 }
admin45c872b2006-08-26 04:51:38 +0000558
adminb0dd10f2006-08-25 17:25:49 +0000559}
560// END Router Class
561?>