Remove file execution permission.
[living-lab-site.git] / system / core / Router.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://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              ExpressionEngine Dev Team
26  * @category    Libraries
27  * @link                http://codeigniter.com/user_guide/general/routing.html
28  */
29 class CI_Router {
30
31         var $config;
32         var $routes                     = array();
33         var $error_routes       = array();
34         var $class                      = '';
35         var $method                     = 'index';
36         var $directory          = '';
37         var $default_controller;
38
39         /**
40          * Constructor
41          *
42          * Runs the route mapping function.
43          */
44         function __construct()
45         {
46                 $this->config =& load_class('Config', 'core');
47                 $this->uri =& load_class('URI', 'core');
48                 log_message('debug', "Router Class Initialized");
49         }
50
51         // --------------------------------------------------------------------
52
53         /**
54          * Set the route mapping
55          *
56          * This function determines what should be served based on the URI request,
57          * as well as any "routes" that have been set in the routing config file.
58          *
59          * @access      private
60          * @return      void
61          */
62         function _set_routing()
63         {
64                 // Are query strings enabled in the config file?  Normally CI doesn't utilize query strings
65                 // since URI segments are more search-engine friendly, but they can optionally be used.
66                 // If this feature is enabled, we will gather the directory/class/method a little differently
67                 $segments = array();
68                 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
69                 {
70                         if (isset($_GET[$this->config->item('directory_trigger')]))
71                         {
72                                 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
73                                 $segments[] = $this->fetch_directory();
74                         }
75
76                         if (isset($_GET[$this->config->item('controller_trigger')]))
77                         {
78                                 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
79                                 $segments[] = $this->fetch_class();
80                         }
81
82                         if (isset($_GET[$this->config->item('function_trigger')]))
83                         {
84                                 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
85                                 $segments[] = $this->fetch_method();
86                         }
87                 }
88
89                 // Load the routes.php file.
90                 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes'.EXT))
91                 {
92                         include(APPPATH.'config/'.ENVIRONMENT.'/routes'.EXT);
93                 }
94                 elseif (is_file(APPPATH.'config/routes'.EXT))
95                 {
96                         include(APPPATH.'config/routes'.EXT);
97                 }
98                 
99                 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
100                 unset($route);
101
102                 // Set the default controller so we can display it in the event
103                 // the URI doesn't correlated to a valid controller.
104                 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
105
106                 // Were there any query string segments?  If so, we'll validate them and bail out since we're done.
107                 if (count($segments) > 0)
108                 {
109                         return $this->_validate_request($segments);
110                 }
111
112                 // Fetch the complete URI string
113                 $this->uri->_fetch_uri_string();
114
115                 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
116                 if ($this->uri->uri_string == '')
117                 {
118                         return $this->_set_default_controller();
119                 }
120
121                 // Do we need to remove the URL suffix?
122                 $this->uri->_remove_url_suffix();
123
124                 // Compile the segments into an array
125                 $this->uri->_explode_segments();
126
127                 // Parse any custom routing that may exist
128                 $this->_parse_routes();
129
130                 // Re-index the segment array so that it starts with 1 rather than 0
131                 $this->uri->_reindex_segments();
132         }
133
134         // --------------------------------------------------------------------
135
136         /**
137          * Set the default controller
138          *
139          * @access      private
140          * @return      void
141          */
142         function _set_default_controller()
143         {
144                 if ($this->default_controller === FALSE)
145                 {
146                         show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
147                 }
148                 // Is the method being specified?
149                 if (strpos($this->default_controller, '/') !== FALSE)
150                 {
151                         $x = explode('/', $this->default_controller);
152
153                         $this->set_class($x[0]);
154                         $this->set_method($x[1]);
155                         $this->_set_request($x);
156                 }
157                 else
158                 {
159                         $this->set_class($this->default_controller);
160                         $this->set_method('index');
161                         $this->_set_request(array($this->default_controller, 'index'));
162                 }
163
164                 // re-index the routed segments array so it starts with 1 rather than 0
165                 $this->uri->_reindex_segments();
166
167                 log_message('debug', "No URI present. Default controller set.");
168         }
169
170         // --------------------------------------------------------------------
171
172         /**
173          * Set the Route
174          *
175          * This function takes an array of URI segments as
176          * input, and sets the current class/method
177          *
178          * @access      private
179          * @param       array
180          * @param       bool
181          * @return      void
182          */
183         function _set_request($segments = array())
184         {
185                 $segments = $this->_validate_request($segments);
186
187                 if (count($segments) == 0)
188                 {
189                         return $this->_set_default_controller();
190                 }
191
192                 $this->set_class($segments[0]);
193
194                 if (isset($segments[1]))
195                 {
196                         // A standard method request
197                         $this->set_method($segments[1]);
198                 }
199                 else
200                 {
201                         // This lets the "routed" segment array identify that the default
202                         // index method is being used.
203                         $segments[1] = 'index';
204                 }
205
206                 // Update our "routed" segment array to contain the segments.
207                 // Note: If there is no custom routing, this array will be
208                 // identical to $this->uri->segments
209                 $this->uri->rsegments = $segments;
210         }
211
212         // --------------------------------------------------------------------
213
214         /**
215          * Validates the supplied segments.  Attempts to determine the path to
216          * the controller.
217          *
218          * @access      private
219          * @param       array
220          * @return      array
221          */
222         function _validate_request($segments)
223         {
224                 if (count($segments) == 0)
225                 {
226                         return $segments;
227                 }
228
229                 // Does the requested controller exist in the root folder?
230                 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
231                 {
232                         return $segments;
233                 }
234
235                 // Is the controller in a sub-folder?
236                 if (is_dir(APPPATH.'controllers/'.$segments[0]))
237                 {
238                         // Set the directory and remove it from the segment array
239                         $this->set_directory($segments[0]);
240                         $segments = array_slice($segments, 1);
241
242                         if (count($segments) > 0)
243                         {
244                                 // Does the requested controller exist in the sub-folder?
245                                 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
246                                 {
247                                         show_404($this->fetch_directory().$segments[0]);
248                                 }
249                         }
250                         else
251                         {
252                                 // Is the method being specified in the route?
253                                 if (strpos($this->default_controller, '/') !== FALSE)
254                                 {
255                                         $x = explode('/', $this->default_controller);
256
257                                         $this->set_class($x[0]);
258                                         $this->set_method($x[1]);
259                                 }
260                                 else
261                                 {
262                                         $this->set_class($this->default_controller);
263                                         $this->set_method('index');
264                                 }
265
266                                 // Does the default controller exist in the sub-folder?
267                                 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
268                                 {
269                                         $this->directory = '';
270                                         return array();
271                                 }
272
273                         }
274
275                         return $segments;
276                 }
277
278
279                 // If we've gotten this far it means that the URI does not correlate to a valid
280                 // controller class.  We will now see if there is an override
281                 if ( ! empty($this->routes['404_override']))
282                 {
283                         $x = explode('/', $this->routes['404_override']);
284
285                         $this->set_class($x[0]);
286                         $this->set_method(isset($x[1]) ? $x[1] : 'index');
287
288                         return $x;
289                 }
290
291
292                 // Nothing else to do at this point but show a 404
293                 show_404($segments[0]);
294         }
295
296         // --------------------------------------------------------------------
297
298         /**
299          *  Parse Routes
300          *
301          * This function matches any routes that may exist in
302          * the config/routes.php file against the URI to
303          * determine if the class/method need to be remapped.
304          *
305          * @access      private
306          * @return      void
307          */
308         function _parse_routes()
309         {
310                 // Turn the segment array into a URI string
311                 $uri = implode('/', $this->uri->segments);
312
313                 // Is there a literal match?  If so we're done
314                 if (isset($this->routes[$uri]))
315                 {
316                         return $this->_set_request(explode('/', $this->routes[$uri]));
317                 }
318
319                 // Loop through the route array looking for wild-cards
320                 foreach ($this->routes as $key => $val)
321                 {
322                         // Convert wild-cards to RegEx
323                         $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
324
325                         // Does the RegEx match?
326                         if (preg_match('#^'.$key.'$#', $uri))
327                         {
328                                 // Do we have a back-reference?
329                                 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
330                                 {
331                                         $val = preg_replace('#^'.$key.'$#', $val, $uri);
332                                 }
333
334                                 return $this->_set_request(explode('/', $val));
335                         }
336                 }
337
338                 // If we got this far it means we didn't encounter a
339                 // matching route so we'll set the site default route
340                 $this->_set_request($this->uri->segments);
341         }
342
343         // --------------------------------------------------------------------
344
345         /**
346          * Set the class name
347          *
348          * @access      public
349          * @param       string
350          * @return      void
351          */
352         function set_class($class)
353         {
354                 $this->class = str_replace(array('/', '.'), '', $class);
355         }
356
357         // --------------------------------------------------------------------
358
359         /**
360          * Fetch the current class
361          *
362          * @access      public
363          * @return      string
364          */
365         function fetch_class()
366         {
367                 return $this->class;
368         }
369
370         // --------------------------------------------------------------------
371
372         /**
373          *  Set the method name
374          *
375          * @access      public
376          * @param       string
377          * @return      void
378          */
379         function set_method($method)
380         {
381                 $this->method = $method;
382         }
383
384         // --------------------------------------------------------------------
385
386         /**
387          *  Fetch the current method
388          *
389          * @access      public
390          * @return      string
391          */
392         function fetch_method()
393         {
394                 if ($this->method == $this->fetch_class())
395                 {
396                         return 'index';
397                 }
398
399                 return $this->method;
400         }
401
402         // --------------------------------------------------------------------
403
404         /**
405          *  Set the directory name
406          *
407          * @access      public
408          * @param       string
409          * @return      void
410          */
411         function set_directory($dir)
412         {
413                 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
414         }
415
416         // --------------------------------------------------------------------
417
418         /**
419          *  Fetch the sub-directory (if any) that contains the requested controller class
420          *
421          * @access      public
422          * @return      string
423          */
424         function fetch_directory()
425         {
426                 return $this->directory;
427         }
428
429         // --------------------------------------------------------------------
430
431         /**
432          *  Set the controller overrides
433          *
434          * @access      public
435          * @param       array
436          * @return      null
437          */
438         function _set_overrides($routing)
439         {
440                 if ( ! is_array($routing))
441                 {
442                         return;
443                 }
444
445                 if (isset($routing['directory']))
446                 {
447                         $this->set_directory($routing['directory']);
448                 }
449
450                 if (isset($routing['controller']) AND $routing['controller'] != '')
451                 {
452                         $this->set_class($routing['controller']);
453                 }
454
455                 if (isset($routing['function']))
456                 {
457                         $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
458                         $this->set_method($routing['function']);
459                 }
460         }
461
462
463 }
464 // END Router Class
465
466 /* End of file Router.php */
467 /* Location: ./system/core/Router.php */