blob: 868fd909e7c69ec7278e87a070089af6b961bf0c [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 }
80
81 // Load the routes.php file
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
86 // Set the default controller
87 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
88
89 // Fetch the URI string Depending on the server,
90 // the URI will be available in one of two globals
adminb071bb52006-08-26 19:28:37 +000091 if ($this->config->item('uri_protocol') == 'auto')
adminb0dd10f2006-08-25 17:25:49 +000092 {
adminb071bb52006-08-26 19:28:37 +000093 $path_info = getenv('PATH_INFO');
94 if ($path_info != '' AND $path_info != "/".SELF)
95 {
96 $this->uri_string = $path_info;
97 }
98 else
99 {
100 $path_info = getenv('ORIG_PATH_INFO');
101 if ($path_info != '' AND $path_info != "/".SELF)
102 {
103 $this->uri_string = $path_info;
104 }
105 else
106 {
107 $this->uri_string = getenv('QUERY_STRING');
108 }
109 }
adminb0dd10f2006-08-25 17:25:49 +0000110 }
adminb071bb52006-08-26 19:28:37 +0000111 else
112 {
113 $this->uri_string = getenv(strtoupper($this->config->item('uri_protocol')));
114 }
115
adminb0dd10f2006-08-25 17:25:49 +0000116
117 // Is there a URI string? If not, the default controller specified
118 // by the admin in the "routes" file will be shown.
119 if ($this->uri_string == '')
120 {
121 if ($this->default_controller === FALSE)
122 {
123 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
124 }
125
126 $this->set_class($this->default_controller);
127 $this->set_method('index');
128
129 log_message('debug', "No URI present. Default controller set.");
130 return;
131 }
admin45c872b2006-08-26 04:51:38 +0000132 unset($this->routes['default_controller']);
adminb0dd10f2006-08-25 17:25:49 +0000133
134 // Do we need to remove the suffix specified in the config file?
135 if ($this->config->item('url_suffix') != "")
136 {
137 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
138 }
139
140 // Explode the URI Segments. The individual segments will
admin45c872b2006-08-26 04:51:38 +0000141 // be stored in the $this->segments array.
admin45c872b2006-08-26 04:51:38 +0000142 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
143 {
144 // Filter segments for security
145 $val = trim($this->_filter_uri($val));
146
147 if ($val != '')
admine07fbb32006-08-26 17:11:01 +0000148 $this->segments[] = $val;
admin45c872b2006-08-26 04:51:38 +0000149 }
adminb0dd10f2006-08-25 17:25:49 +0000150
admine07fbb32006-08-26 17:11:01 +0000151 // Parse any custom routing that may exist
152 $this->_parse_routes();
admin45c872b2006-08-26 04:51:38 +0000153
admine07fbb32006-08-26 17:11:01 +0000154 // Re-index the segment array so that it starts with 1 rather than 0
admin99bccd62006-09-21 17:05:40 +0000155 $this->_reindex_segments();
adminb0dd10f2006-08-25 17:25:49 +0000156 }
157 // END _set_route_mapping()
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Compile Segments
163 *
164 * This function takes an array of URI segments as
165 * input, and puts it into the $this->segments array.
166 * It also sets the current class/method
167 *
168 * @access private
169 * @param array
170 * @param bool
171 * @return void
172 */
admin45c872b2006-08-26 04:51:38 +0000173 function _compile_segments($segments = array())
174 {
175 $segments = $this->_validate_segments($segments);
adminb0dd10f2006-08-25 17:25:49 +0000176
admin45c872b2006-08-26 04:51:38 +0000177 if (count($segments) == 0)
178 {
179 return;
180 }
181
admine07fbb32006-08-26 17:11:01 +0000182 $this->set_class($segments['0']);
adminb0dd10f2006-08-25 17:25:49 +0000183
admine07fbb32006-08-26 17:11:01 +0000184 if (isset($segments['1']))
adminb0dd10f2006-08-25 17:25:49 +0000185 {
186 // A scaffolding request. No funny business with the URL
admine07fbb32006-08-26 17:11:01 +0000187 if ($this->routes['scaffolding_trigger'] == $segments['1'] AND $segments['1'] != '_ci_scaffolding')
adminb0dd10f2006-08-25 17:25:49 +0000188 {
189 $this->scaffolding_request = TRUE;
190 unset($this->routes['scaffolding_trigger']);
191 }
192 else
193 {
194 // A standard method request
admine07fbb32006-08-26 17:11:01 +0000195 $this->set_method($segments['1']);
adminb0dd10f2006-08-25 17:25:49 +0000196 }
197 }
admin99bccd62006-09-21 17:05:40 +0000198
199 // Update our "routed" segment array to contain the segments.
200 // Note: If there is no custom routing, this array will be
201 // identical to $this->segments
202 $this->rsegments = $segments;
adminb0dd10f2006-08-25 17:25:49 +0000203 }
204 // END _compile_segments()
205
206 // --------------------------------------------------------------------
207
208 /**
admin45c872b2006-08-26 04:51:38 +0000209 * Validates the supplied segments. Attempts to determine the path to
210 * the controller.
211 *
212 * @access private
213 * @param array
214 * @return array
215 */
216 function _validate_segments($segments)
217 {
admine07fbb32006-08-26 17:11:01 +0000218 // Does the requested controller exist in the root folder?
219 if (file_exists(APPPATH.'controllers/'.$segments['0'].EXT))
admin45c872b2006-08-26 04:51:38 +0000220 {
admine07fbb32006-08-26 17:11:01 +0000221 return $segments;
admin45c872b2006-08-26 04:51:38 +0000222 }
admin1cf89aa2006-09-03 18:24:39 +0000223
admine07fbb32006-08-26 17:11:01 +0000224 // Is the controller in a sub-folder?
225 if (is_dir(APPPATH.'controllers/'.$segments['0']))
admin1cf89aa2006-09-03 18:24:39 +0000226 {
admine07fbb32006-08-26 17:11:01 +0000227 // Set the directory and remove it from the segment array
228 $this->set_directory($segments['0']);
229 $segments = array_slice($segments, 1);
230
admin1cf89aa2006-09-03 18:24:39 +0000231 if (count($segments) > 0)
232 {
233 // Does the requested controller exist in the sub-folder?
234 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments['0'].EXT))
235 {
236 show_404();
237 }
238 }
239 else
admine07fbb32006-08-26 17:11:01 +0000240 {
241 $this->set_class($this->default_controller);
242 $this->set_method('index');
admin27818492006-09-05 03:31:28 +0000243
244 // Does the default controller exist in the sub-folder?
245 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
246 {
247 $this->directory = '';
248 return array();
249 }
250
admine07fbb32006-08-26 17:11:01 +0000251 }
252
253 return $segments;
254 }
255
256 // Can't find the requested controller...
257 show_404();
admin45c872b2006-08-26 04:51:38 +0000258 }
259 // END _validate_segments()
admin99bccd62006-09-21 17:05:40 +0000260
261 // --------------------------------------------------------------------
262 /**
263 * Re-index Segments
264 *
265 * This function re-indexes the $this->segment array so that it
266 * starts at 1 rather then 0. Doing so makes it simpler to
267 * use functions like $this->uri->segment(n) since there is
268 * a 1:1 relationship between the segment array and the actual segments.
269 *
270 * @access private
271 * @return void
272 */
273 function _reindex_segments()
274 {
275 // Is the routed segment array different then the main segment array?
276 $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE;
277
278 $i = 1;
279 foreach ($this->segments as $val)
280 {
281 $this->segments[$i++] = $val;
282 }
283 unset($this->segments['0']);
284
285 if ($diff == FALSE)
286 {
287 $this->rsegments = $this->segments;
288 }
289 else
290 {
291 $i = 1;
292 foreach ($this->rsegments as $val)
293 {
294 $this->rsegments[$i++] = $val;
295 }
296 unset($this->rsegments['0']);
297 }
298 }
299 // END _reindex_segments()
admin45c872b2006-08-26 04:51:38 +0000300
301 // --------------------------------------------------------------------
302
303 /**
adminb0dd10f2006-08-25 17:25:49 +0000304 * Filter segments for malicious characters
305 *
306 * @access private
307 * @param string
308 * @return string
309 */
310 function _filter_uri($str)
311 {
admin1082bdd2006-08-27 19:32:02 +0000312 if ($this->config->item('permitted_uri_chars') != '')
313 {
314 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
315 {
316 exit('The URI you submitted has disallowed characters: '.$str);
317 }
318 }
319 return $str;
adminb0dd10f2006-08-25 17:25:49 +0000320 }
321 // END _filter_uri()
322
323 // --------------------------------------------------------------------
324
325 /**
adminb0dd10f2006-08-25 17:25:49 +0000326 * Parse Routes
327 *
328 * This function matches any routes that may exist in
329 * the config/routes.php file against the URI to
330 * determine if the class/method need to be remapped.
331 *
332 * @access private
333 * @return void
334 */
335 function _parse_routes()
336 {
admine07fbb32006-08-26 17:11:01 +0000337 // Do we even have any custom routing to deal with?
338 if (count($this->routes) == 0)
339 {
340 $this->_compile_segments($this->segments);
341 return;
342 }
343
adminb0dd10f2006-08-25 17:25:49 +0000344 // Turn the segment array into a URI string
345 $uri = implode('/', $this->segments);
346 $num = count($this->segments);
347
348 // Is there a literal match? If so we're done
349 if (isset($this->routes[$uri]))
350 {
admin45c872b2006-08-26 04:51:38 +0000351 $this->_compile_segments(explode('/', $this->routes[$uri]));
adminb0dd10f2006-08-25 17:25:49 +0000352 return;
353 }
admine07fbb32006-08-26 17:11:01 +0000354
adminb0dd10f2006-08-25 17:25:49 +0000355 // Loop through the route array looking for wildcards
admind4e95072006-08-26 01:15:06 +0000356 foreach (array_slice($this->routes, 1) as $key => $val)
adminb071bb52006-08-26 19:28:37 +0000357 {
admind4e95072006-08-26 01:15:06 +0000358 // Convert wildcards to RegEx
359 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
360
admin45c872b2006-08-26 04:51:38 +0000361 // Does the RegEx match?
admin71430b42006-09-15 20:29:25 +0000362 if (preg_match('#^'.$key.'$#', $uri))
admind4e95072006-08-26 01:15:06 +0000363 {
admin45c872b2006-08-26 04:51:38 +0000364 // Do we have a back-reference?
admind4e95072006-08-26 01:15:06 +0000365 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
366 {
admin71430b42006-09-15 20:29:25 +0000367 $val = preg_replace('#^'.$key.'$#', $val, $uri);
admind4e95072006-08-26 01:15:06 +0000368 }
369
admin45c872b2006-08-26 04:51:38 +0000370 $this->_compile_segments(explode('/', $val));
371 return;
adminb0dd10f2006-08-25 17:25:49 +0000372 }
admine07fbb32006-08-26 17:11:01 +0000373 }
374
375 // If we got this far it means we didn't encounter a
376 // matching route so we'll set the site default route
377 $this->_compile_segments($this->segments);
adminb0dd10f2006-08-25 17:25:49 +0000378 }
379 // END set_method()
admin45c872b2006-08-26 04:51:38 +0000380
381 // --------------------------------------------------------------------
382
383 /**
384 * Set the class name
385 *
386 * @access public
387 * @param string
388 * @return void
389 */
390 function set_class($class)
391 {
392 $this->class = $class;
393 }
394 // END set_class()
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Fetch the current class
400 *
401 * @access public
402 * @return string
403 */
404 function fetch_class()
405 {
406 return $this->class;
407 }
408 // END fetch_class()
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Set the method name
414 *
415 * @access public
416 * @param string
417 * @return void
418 */
419 function set_method($method)
420 {
421 $this->method = $method;
422 }
423 // END set_method()
424
425 // --------------------------------------------------------------------
426
427 /**
428 * Fetch the current method
429 *
430 * @access public
431 * @return string
432 */
433 function fetch_method()
434 {
435 return $this->method;
436 }
437 // END fetch_method()
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Set the directory name
443 *
444 * @access public
445 * @param string
446 * @return void
447 */
448 function set_directory($dir)
449 {
450 $this->directory = $dir.'/';
451 }
452 // END set_directory()
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Fetch the sub-directory (if any) that contains the requested controller class
458 *
459 * @access public
460 * @return string
461 */
462 function fetch_directory()
463 {
464 return $this->directory;
465 }
466 // END fetch_directory()
467
adminb0dd10f2006-08-25 17:25:49 +0000468}
469// END Router Class
470?>