blob: 27e3c27cc02ce5915cb602e7a3e06aba4e28f44a [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 {
admin33de9a12006-09-28 06:50:16 +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 {
admindbd8aec2006-09-22 19:20:09 +0000369 if (($request_uri = getenv('REQUEST_URI')) == '')
370 {
371 return '';
372 }
373
admin592cdcb2006-09-22 18:45:42 +0000374 $fc_path = FCPATH;
375
376 if (strpos($request_uri, '?') !== FALSE)
377 {
378 $fc_path .= '?';
379 }
380
381 $parsed_uri = explode("/", preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $request_uri)));
382
383 $i = 0;
384 foreach(explode("/", $fc_path) as $segment)
385 {
386 if ($segment == $parsed_uri[$i])
387 {
388 $i++;
389 }
390 }
391
392 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
393
394 if ($parsed_uri != '')
395 {
396 $parsed_uri = '/'.$parsed_uri;
397 }
398
399 return $parsed_uri;
400 }
admin592cdcb2006-09-22 18:45:42 +0000401
402 // --------------------------------------------------------------------
403
404 /**
adminb0dd10f2006-08-25 17:25:49 +0000405 * Filter segments for malicious characters
406 *
407 * @access private
408 * @param string
409 * @return string
410 */
411 function _filter_uri($str)
412 {
admin1082bdd2006-08-27 19:32:02 +0000413 if ($this->config->item('permitted_uri_chars') != '')
414 {
415 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
416 {
417 exit('The URI you submitted has disallowed characters: '.$str);
418 }
419 }
420 return $str;
adminb0dd10f2006-08-25 17:25:49 +0000421 }
adminb0dd10f2006-08-25 17:25:49 +0000422
423 // --------------------------------------------------------------------
424
425 /**
adminb0dd10f2006-08-25 17:25:49 +0000426 * Parse Routes
427 *
428 * This function matches any routes that may exist in
429 * the config/routes.php file against the URI to
430 * determine if the class/method need to be remapped.
431 *
432 * @access private
433 * @return void
434 */
435 function _parse_routes()
436 {
admine07fbb32006-08-26 17:11:01 +0000437 // Do we even have any custom routing to deal with?
438 if (count($this->routes) == 0)
439 {
440 $this->_compile_segments($this->segments);
441 return;
442 }
443
adminb0dd10f2006-08-25 17:25:49 +0000444 // Turn the segment array into a URI string
445 $uri = implode('/', $this->segments);
446 $num = count($this->segments);
447
448 // Is there a literal match? If so we're done
449 if (isset($this->routes[$uri]))
450 {
admin45c872b2006-08-26 04:51:38 +0000451 $this->_compile_segments(explode('/', $this->routes[$uri]));
adminb0dd10f2006-08-25 17:25:49 +0000452 return;
453 }
admine07fbb32006-08-26 17:11:01 +0000454
adminb0dd10f2006-08-25 17:25:49 +0000455 // Loop through the route array looking for wildcards
admind4e95072006-08-26 01:15:06 +0000456 foreach (array_slice($this->routes, 1) as $key => $val)
adminb071bb52006-08-26 19:28:37 +0000457 {
admind4e95072006-08-26 01:15:06 +0000458 // Convert wildcards to RegEx
459 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
460
admin45c872b2006-08-26 04:51:38 +0000461 // Does the RegEx match?
admin71430b42006-09-15 20:29:25 +0000462 if (preg_match('#^'.$key.'$#', $uri))
admind4e95072006-08-26 01:15:06 +0000463 {
admin45c872b2006-08-26 04:51:38 +0000464 // Do we have a back-reference?
admind4e95072006-08-26 01:15:06 +0000465 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
466 {
admin71430b42006-09-15 20:29:25 +0000467 $val = preg_replace('#^'.$key.'$#', $val, $uri);
admind4e95072006-08-26 01:15:06 +0000468 }
469
admin45c872b2006-08-26 04:51:38 +0000470 $this->_compile_segments(explode('/', $val));
471 return;
adminb0dd10f2006-08-25 17:25:49 +0000472 }
admine07fbb32006-08-26 17:11:01 +0000473 }
474
475 // If we got this far it means we didn't encounter a
476 // matching route so we'll set the site default route
477 $this->_compile_segments($this->segments);
adminb0dd10f2006-08-25 17:25:49 +0000478 }
admin45c872b2006-08-26 04:51:38 +0000479
480 // --------------------------------------------------------------------
481
482 /**
483 * Set the class name
484 *
485 * @access public
486 * @param string
487 * @return void
488 */
489 function set_class($class)
490 {
491 $this->class = $class;
492 }
admin45c872b2006-08-26 04:51:38 +0000493
494 // --------------------------------------------------------------------
495
496 /**
497 * Fetch the current class
498 *
499 * @access public
500 * @return string
501 */
502 function fetch_class()
503 {
504 return $this->class;
505 }
admin45c872b2006-08-26 04:51:38 +0000506
507 // --------------------------------------------------------------------
508
509 /**
510 * Set the method name
511 *
512 * @access public
513 * @param string
514 * @return void
515 */
516 function set_method($method)
517 {
518 $this->method = $method;
519 }
admin45c872b2006-08-26 04:51:38 +0000520
521 // --------------------------------------------------------------------
522
523 /**
524 * Fetch the current method
525 *
526 * @access public
527 * @return string
528 */
529 function fetch_method()
530 {
admin08f60202006-10-03 05:28:00 +0000531 if ($this->method == $this->fetch_class())
532 {
533 return 'index';
534 }
535
admin45c872b2006-08-26 04:51:38 +0000536 return $this->method;
537 }
admin45c872b2006-08-26 04:51:38 +0000538
539 // --------------------------------------------------------------------
540
541 /**
542 * Set the directory name
543 *
544 * @access public
545 * @param string
546 * @return void
547 */
548 function set_directory($dir)
549 {
550 $this->directory = $dir.'/';
551 }
admin45c872b2006-08-26 04:51:38 +0000552
553 // --------------------------------------------------------------------
554
555 /**
556 * Fetch the sub-directory (if any) that contains the requested controller class
557 *
558 * @access public
559 * @return string
560 */
561 function fetch_directory()
562 {
563 return $this->directory;
564 }
admin45c872b2006-08-26 04:51:38 +0000565
adminb0dd10f2006-08-25 17:25:49 +0000566}
567// END Router Class
568?>