blob: 5bbf9e6ca962fa864171cf905de38c243e3e7289 [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 // Get the URI string
89 $this->uri_string = $this->_get_uri_string();
90
91 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
adminb0dd10f2006-08-25 17:25:49 +000092 if ($this->uri_string == '')
93 {
94 if ($this->default_controller === FALSE)
95 {
96 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
97 }
98
99 $this->set_class($this->default_controller);
100 $this->set_method('index');
101
102 log_message('debug', "No URI present. Default controller set.");
103 return;
104 }
admin45c872b2006-08-26 04:51:38 +0000105 unset($this->routes['default_controller']);
adminb0dd10f2006-08-25 17:25:49 +0000106
107 // Do we need to remove the suffix specified in the config file?
108 if ($this->config->item('url_suffix') != "")
109 {
110 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
111 }
112
113 // Explode the URI Segments. The individual segments will
admin45c872b2006-08-26 04:51:38 +0000114 // be stored in the $this->segments array.
admin45c872b2006-08-26 04:51:38 +0000115 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
116 {
117 // Filter segments for security
118 $val = trim($this->_filter_uri($val));
119
120 if ($val != '')
admine07fbb32006-08-26 17:11:01 +0000121 $this->segments[] = $val;
admin45c872b2006-08-26 04:51:38 +0000122 }
adminb0dd10f2006-08-25 17:25:49 +0000123
admine07fbb32006-08-26 17:11:01 +0000124 // Parse any custom routing that may exist
125 $this->_parse_routes();
admin45c872b2006-08-26 04:51:38 +0000126
admine07fbb32006-08-26 17:11:01 +0000127 // Re-index the segment array so that it starts with 1 rather than 0
admin99bccd62006-09-21 17:05:40 +0000128 $this->_reindex_segments();
adminb0dd10f2006-08-25 17:25:49 +0000129 }
130 // END _set_route_mapping()
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Compile Segments
136 *
137 * This function takes an array of URI segments as
138 * input, and puts it into the $this->segments array.
139 * It also sets the current class/method
140 *
141 * @access private
142 * @param array
143 * @param bool
144 * @return void
145 */
admin45c872b2006-08-26 04:51:38 +0000146 function _compile_segments($segments = array())
147 {
148 $segments = $this->_validate_segments($segments);
adminb0dd10f2006-08-25 17:25:49 +0000149
admin45c872b2006-08-26 04:51:38 +0000150 if (count($segments) == 0)
151 {
152 return;
153 }
154
admine07fbb32006-08-26 17:11:01 +0000155 $this->set_class($segments['0']);
adminb0dd10f2006-08-25 17:25:49 +0000156
admine07fbb32006-08-26 17:11:01 +0000157 if (isset($segments['1']))
adminb0dd10f2006-08-25 17:25:49 +0000158 {
159 // A scaffolding request. No funny business with the URL
admine07fbb32006-08-26 17:11:01 +0000160 if ($this->routes['scaffolding_trigger'] == $segments['1'] AND $segments['1'] != '_ci_scaffolding')
adminb0dd10f2006-08-25 17:25:49 +0000161 {
162 $this->scaffolding_request = TRUE;
163 unset($this->routes['scaffolding_trigger']);
164 }
165 else
166 {
167 // A standard method request
admine07fbb32006-08-26 17:11:01 +0000168 $this->set_method($segments['1']);
adminb0dd10f2006-08-25 17:25:49 +0000169 }
170 }
admin99bccd62006-09-21 17:05:40 +0000171
172 // Update our "routed" segment array to contain the segments.
173 // Note: If there is no custom routing, this array will be
174 // identical to $this->segments
175 $this->rsegments = $segments;
adminb0dd10f2006-08-25 17:25:49 +0000176 }
177 // END _compile_segments()
178
179 // --------------------------------------------------------------------
180
181 /**
admin45c872b2006-08-26 04:51:38 +0000182 * Validates the supplied segments. Attempts to determine the path to
183 * the controller.
184 *
185 * @access private
186 * @param array
187 * @return array
188 */
189 function _validate_segments($segments)
190 {
admine07fbb32006-08-26 17:11:01 +0000191 // Does the requested controller exist in the root folder?
192 if (file_exists(APPPATH.'controllers/'.$segments['0'].EXT))
admin45c872b2006-08-26 04:51:38 +0000193 {
admine07fbb32006-08-26 17:11:01 +0000194 return $segments;
admin45c872b2006-08-26 04:51:38 +0000195 }
admin1cf89aa2006-09-03 18:24:39 +0000196
admine07fbb32006-08-26 17:11:01 +0000197 // Is the controller in a sub-folder?
198 if (is_dir(APPPATH.'controllers/'.$segments['0']))
admin1cf89aa2006-09-03 18:24:39 +0000199 {
admine07fbb32006-08-26 17:11:01 +0000200 // Set the directory and remove it from the segment array
201 $this->set_directory($segments['0']);
202 $segments = array_slice($segments, 1);
203
admin1cf89aa2006-09-03 18:24:39 +0000204 if (count($segments) > 0)
205 {
206 // Does the requested controller exist in the sub-folder?
207 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments['0'].EXT))
208 {
209 show_404();
210 }
211 }
212 else
admine07fbb32006-08-26 17:11:01 +0000213 {
214 $this->set_class($this->default_controller);
215 $this->set_method('index');
admin27818492006-09-05 03:31:28 +0000216
217 // Does the default controller exist in the sub-folder?
218 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
219 {
220 $this->directory = '';
221 return array();
222 }
223
admine07fbb32006-08-26 17:11:01 +0000224 }
225
226 return $segments;
227 }
228
229 // Can't find the requested controller...
230 show_404();
admin45c872b2006-08-26 04:51:38 +0000231 }
232 // END _validate_segments()
admin99bccd62006-09-21 17:05:40 +0000233
234 // --------------------------------------------------------------------
235 /**
236 * Re-index Segments
237 *
238 * This function re-indexes the $this->segment array so that it
239 * starts at 1 rather then 0. Doing so makes it simpler to
240 * use functions like $this->uri->segment(n) since there is
241 * a 1:1 relationship between the segment array and the actual segments.
242 *
243 * @access private
244 * @return void
245 */
246 function _reindex_segments()
247 {
248 // Is the routed segment array different then the main segment array?
249 $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE;
250
251 $i = 1;
252 foreach ($this->segments as $val)
253 {
254 $this->segments[$i++] = $val;
255 }
256 unset($this->segments['0']);
257
258 if ($diff == FALSE)
259 {
260 $this->rsegments = $this->segments;
261 }
262 else
263 {
264 $i = 1;
265 foreach ($this->rsegments as $val)
266 {
267 $this->rsegments[$i++] = $val;
268 }
269 unset($this->rsegments['0']);
270 }
271 }
272 // END _reindex_segments()
admin45c872b2006-08-26 04:51:38 +0000273
274 // --------------------------------------------------------------------
275
276 /**
admin592cdcb2006-09-22 18:45:42 +0000277 * Get the URI String
278 *
279 * @access private
280 * @return string
281 */
282 function _get_uri_string()
283 {
284 if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
285 {
286 $path_info = getenv('PATH_INFO');
287 if ($path_info != '' AND $path_info != "/".SELF)
288 {
289 return $path_info;
290 }
291 else
292 {
293 $req_uri = $this->_parse_request_uri();
294
295 if ($req_uri != "")
296 {
297 return $req_uri;
298 }
299 else
300 {
301 $path_info = getenv('ORIG_PATH_INFO');
302 if ($path_info != '' AND $path_info != "/".SELF)
303 {
304 return $path_info;
305 }
306 else
307 {
308 return getenv('QUERY_STRING');
309 }
310 }
311 }
312 }
313 else
314 {
315 $uri = strtoupper($this->config->item('uri_protocol'));
316
317 if ($uri == 'REQUEST_URI')
318 {
319 return $this->_parse_request_uri();
320 }
321
322 return getenv($uri);
323 }
324 }
325 // END _get_uri_string()
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Parse the REQUEST_URI
331 *
332 * Due to the way REQUEST_URI works it usually contains path info
333 * that makes it unusable as URI data. We'll trim off the unnecessary
334 * data, hopefully arriving at a valid URI that we can use.
335 *
336 * @access private
337 * @return string
338 */
339 function _parse_request_uri()
340 {
341 $request_uri = getenv('REQUEST_URI');
342 $fc_path = FCPATH;
343
344 if (strpos($request_uri, '?') !== FALSE)
345 {
346 $fc_path .= '?';
347 }
348
349 $parsed_uri = explode("/", preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $request_uri)));
350
351 $i = 0;
352 foreach(explode("/", $fc_path) as $segment)
353 {
354 if ($segment == $parsed_uri[$i])
355 {
356 $i++;
357 }
358 }
359
360 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
361
362 if ($parsed_uri != '')
363 {
364 $parsed_uri = '/'.$parsed_uri;
365 }
366
367 return $parsed_uri;
368 }
369 // END _parse_request_uri()
370
371 // --------------------------------------------------------------------
372
373 /**
adminb0dd10f2006-08-25 17:25:49 +0000374 * Filter segments for malicious characters
375 *
376 * @access private
377 * @param string
378 * @return string
379 */
380 function _filter_uri($str)
381 {
admin1082bdd2006-08-27 19:32:02 +0000382 if ($this->config->item('permitted_uri_chars') != '')
383 {
384 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
385 {
386 exit('The URI you submitted has disallowed characters: '.$str);
387 }
388 }
389 return $str;
adminb0dd10f2006-08-25 17:25:49 +0000390 }
391 // END _filter_uri()
392
393 // --------------------------------------------------------------------
394
395 /**
adminb0dd10f2006-08-25 17:25:49 +0000396 * Parse Routes
397 *
398 * This function matches any routes that may exist in
399 * the config/routes.php file against the URI to
400 * determine if the class/method need to be remapped.
401 *
402 * @access private
403 * @return void
404 */
405 function _parse_routes()
406 {
admine07fbb32006-08-26 17:11:01 +0000407 // Do we even have any custom routing to deal with?
408 if (count($this->routes) == 0)
409 {
410 $this->_compile_segments($this->segments);
411 return;
412 }
413
adminb0dd10f2006-08-25 17:25:49 +0000414 // Turn the segment array into a URI string
415 $uri = implode('/', $this->segments);
416 $num = count($this->segments);
417
418 // Is there a literal match? If so we're done
419 if (isset($this->routes[$uri]))
420 {
admin45c872b2006-08-26 04:51:38 +0000421 $this->_compile_segments(explode('/', $this->routes[$uri]));
adminb0dd10f2006-08-25 17:25:49 +0000422 return;
423 }
admine07fbb32006-08-26 17:11:01 +0000424
adminb0dd10f2006-08-25 17:25:49 +0000425 // Loop through the route array looking for wildcards
admind4e95072006-08-26 01:15:06 +0000426 foreach (array_slice($this->routes, 1) as $key => $val)
adminb071bb52006-08-26 19:28:37 +0000427 {
admind4e95072006-08-26 01:15:06 +0000428 // Convert wildcards to RegEx
429 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
430
admin45c872b2006-08-26 04:51:38 +0000431 // Does the RegEx match?
admin71430b42006-09-15 20:29:25 +0000432 if (preg_match('#^'.$key.'$#', $uri))
admind4e95072006-08-26 01:15:06 +0000433 {
admin45c872b2006-08-26 04:51:38 +0000434 // Do we have a back-reference?
admind4e95072006-08-26 01:15:06 +0000435 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
436 {
admin71430b42006-09-15 20:29:25 +0000437 $val = preg_replace('#^'.$key.'$#', $val, $uri);
admind4e95072006-08-26 01:15:06 +0000438 }
439
admin45c872b2006-08-26 04:51:38 +0000440 $this->_compile_segments(explode('/', $val));
441 return;
adminb0dd10f2006-08-25 17:25:49 +0000442 }
admine07fbb32006-08-26 17:11:01 +0000443 }
444
445 // If we got this far it means we didn't encounter a
446 // matching route so we'll set the site default route
447 $this->_compile_segments($this->segments);
adminb0dd10f2006-08-25 17:25:49 +0000448 }
449 // END set_method()
admin45c872b2006-08-26 04:51:38 +0000450
451 // --------------------------------------------------------------------
452
453 /**
454 * Set the class name
455 *
456 * @access public
457 * @param string
458 * @return void
459 */
460 function set_class($class)
461 {
462 $this->class = $class;
463 }
464 // END set_class()
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Fetch the current class
470 *
471 * @access public
472 * @return string
473 */
474 function fetch_class()
475 {
476 return $this->class;
477 }
478 // END fetch_class()
479
480 // --------------------------------------------------------------------
481
482 /**
483 * Set the method name
484 *
485 * @access public
486 * @param string
487 * @return void
488 */
489 function set_method($method)
490 {
491 $this->method = $method;
492 }
493 // END set_method()
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Fetch the current method
499 *
500 * @access public
501 * @return string
502 */
503 function fetch_method()
504 {
505 return $this->method;
506 }
507 // END fetch_method()
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Set the directory name
513 *
514 * @access public
515 * @param string
516 * @return void
517 */
518 function set_directory($dir)
519 {
520 $this->directory = $dir.'/';
521 }
522 // END set_directory()
523
524 // --------------------------------------------------------------------
525
526 /**
527 * Fetch the sub-directory (if any) that contains the requested controller class
528 *
529 * @access public
530 * @return string
531 */
532 function fetch_directory()
533 {
534 return $this->directory;
535 }
536 // END fetch_directory()
537
adminb0dd10f2006-08-25 17:25:49 +0000538}
539// END Router Class
540?>