converted to unix-style eol
[www-register-wizard.git] / libraries / Router.php
index cf42516..32de58d 100644 (file)
-<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
-/**\r
- * CodeIgniter\r
- *\r
- * An open source application development framework for PHP 4.3.2 or newer\r
- *\r
- * @package            CodeIgniter\r
- * @author             ExpressionEngine Dev Team\r
- * @copyright  Copyright (c) 2008, EllisLab, Inc.\r
- * @license            http://codeigniter.com/user_guide/license.html\r
- * @link               http://codeigniter.com\r
- * @since              Version 1.0\r
- * @filesource\r
- */\r
-\r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Router Class\r
- *\r
- * Parses URIs and determines routing\r
- *\r
- * @package            CodeIgniter\r
- * @subpackage Libraries\r
- * @author             ExpressionEngine Dev Team\r
- * @category   Libraries\r
- * @link               http://codeigniter.com/user_guide/general/routing.html\r
- */\r
-class CI_Router {\r
-\r
-       var $config;    \r
-       var $routes             = array();\r
-       var $error_routes       = array();\r
-       var $class                      = '';\r
-       var $method                     = 'index';\r
-       var $directory          = '';\r
-       var $uri_protocol       = 'auto';\r
-       var $default_controller;\r
-       var $scaffolding_request = FALSE; // Must be set to FALSE\r
-       \r
-       /**\r
-        * Constructor\r
-        *\r
-        * Runs the route mapping function.\r
-        */\r
-       function CI_Router()\r
-       {\r
-               $this->config =& load_class('Config');\r
-               $this->uri =& load_class('URI');\r
-               $this->_set_routing();\r
-               log_message('debug', "Router Class Initialized");\r
-       }\r
-       \r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        * Set the route mapping\r
-        *\r
-        * This function determines what should be served based on the URI request,\r
-        * as well as any "routes" that have been set in the routing config file.\r
-        *\r
-        * @access      private\r
-        * @return      void\r
-        */\r
-       function _set_routing()\r
-       {\r
-               // Are query strings enabled in the config file?\r
-               // If so, we're done since segment based URIs are not used with query strings.\r
-               if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))\r
-               {\r
-                       $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));\r
-\r
-                       if (isset($_GET[$this->config->item('function_trigger')]))\r
-                       {\r
-                               $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));\r
-                       }\r
-                       \r
-                       return;\r
-               }\r
-               \r
-               // Load the routes.php file.\r
-               @include(APPPATH.'config/routes'.EXT);\r
-               $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;\r
-               unset($route);\r
-\r
-               // Set the default controller so we can display it in the event\r
-               // the URI doesn't correlated to a valid controller.\r
-               $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);     \r
-               \r
-               // Fetch the complete URI string\r
-               $this->uri->_fetch_uri_string();\r
-       \r
-               // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.\r
-               if ($this->uri->uri_string == '')\r
-               {\r
-                       if ($this->default_controller === FALSE)\r
-                       {\r
-                               show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");\r
-                       }\r
-\r
-                       // Turn the default route into an array.  We explode it in the event that\r
-                       // the controller is located in a subfolder\r
-                       $segments = $this->_validate_request(explode('/', $this->default_controller));\r
-\r
-                       // Set the class and method\r
-                       $this->set_class($segments[0]);\r
-                       $this->set_method('index');\r
-                       \r
-                       // Assign the segments to the URI class\r
-                       $this->uri->rsegments = $segments;\r
-                       \r
-                       // re-index the routed segments array so it starts with 1 rather than 0\r
-                       $this->uri->_reindex_segments();\r
-                       \r
-                       log_message('debug', "No URI present. Default controller set.");\r
-                       return;\r
-               }\r
-               unset($this->routes['default_controller']);\r
-               \r
-               // Do we need to remove the URL suffix?\r
-               $this->uri->_remove_url_suffix();\r
-               \r
-               // Compile the segments into an array\r
-               $this->uri->_explode_segments();\r
-               \r
-               // Parse any custom routing that may exist\r
-               $this->_parse_routes();         \r
-               \r
-               // Re-index the segment array so that it starts with 1 rather than 0\r
-               $this->uri->_reindex_segments();\r
-       }\r
-       \r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        * Set the Route\r
-        *\r
-        * This function takes an array of URI segments as\r
-        * input, and sets the current class/method\r
-        *\r
-        * @access      private\r
-        * @param       array\r
-        * @param       bool\r
-        * @return      void\r
-        */\r
-       function _set_request($segments = array())\r
-       {       \r
-               $segments = $this->_validate_request($segments);\r
-               \r
-               if (count($segments) == 0)\r
-               {\r
-                       return;\r
-               }\r
-                                               \r
-               $this->set_class($segments[0]);\r
-               \r
-               if (isset($segments[1]))\r
-               {\r
-                       // A scaffolding request. No funny business with the URL\r
-                       if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')\r
-                       {\r
-                               $this->scaffolding_request = TRUE;\r
-                               unset($this->routes['scaffolding_trigger']);\r
-                       }\r
-                       else\r
-                       {\r
-                               // A standard method request\r
-                               $this->set_method($segments[1]);\r
-                       }\r
-               }\r
-               else\r
-               {\r
-                       // This lets the "routed" segment array identify that the default\r
-                       // index method is being used.\r
-                       $segments[1] = 'index';\r
-               }\r
-               \r
-               // Update our "routed" segment array to contain the segments.\r
-               // Note: If there is no custom routing, this array will be\r
-               // identical to $this->uri->segments\r
-               $this->uri->rsegments = $segments;\r
-       }\r
-       \r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        * Validates the supplied segments.  Attempts to determine the path to\r
-        * the controller.\r
-        *\r
-        * @access      private\r
-        * @param       array\r
-        * @return      array\r
-        */     \r
-       function _validate_request($segments)\r
-       {\r
-               // Does the requested controller exist in the root folder?\r
-               if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))\r
-               {\r
-                       return $segments;\r
-               }\r
-\r
-               // Is the controller in a sub-folder?\r
-               if (is_dir(APPPATH.'controllers/'.$segments[0]))\r
-               {               \r
-                       // Set the directory and remove it from the segment array\r
-                       $this->set_directory($segments[0]);\r
-                       $segments = array_slice($segments, 1);\r
-                       \r
-                       if (count($segments) > 0)\r
-                       {\r
-                               // Does the requested controller exist in the sub-folder?\r
-                               if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))\r
-                               {\r
-                                       show_404($this->fetch_directory().$segments[0]);\r
-                               }\r
-                       }\r
-                       else\r
-                       {\r
-                               $this->set_class($this->default_controller);\r
-                               $this->set_method('index');\r
-                       \r
-                               // Does the default controller exist in the sub-folder?\r
-                               if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))\r
-                               {\r
-                                       $this->directory = '';\r
-                                       return array();\r
-                               }\r
-                       \r
-                       }\r
-\r
-                       return $segments;\r
-               }\r
-\r
-               // Can't find the requested controller...\r
-               show_404($segments[0]);\r
-       }\r
-\r
-       // --------------------------------------------------------------------\r
-\r
-       /**\r
-        *  Parse Routes\r
-        *\r
-        * This function matches any routes that may exist in\r
-        * the config/routes.php file against the URI to\r
-        * determine if the class/method need to be remapped.\r
-        *\r
-        * @access      private\r
-        * @return      void\r
-        */\r
-       function _parse_routes()\r
-       {\r
-               // Do we even have any custom routing to deal with?\r
-               // There is a default scaffolding trigger, so we'll look just for 1\r
-               if (count($this->routes) == 1)\r
-               {\r
-                       $this->_set_request($this->uri->segments);\r
-                       return;\r
-               }\r
-\r
-               // Turn the segment array into a URI string\r
-               $uri = implode('/', $this->uri->segments);\r
-\r
-               // Is there a literal match?  If so we're done\r
-               if (isset($this->routes[$uri]))\r
-               {\r
-                       $this->_set_request(explode('/', $this->routes[$uri]));         \r
-                       return;\r
-               }\r
-                               \r
-               // Loop through the route array looking for wild-cards\r
-               foreach ($this->routes as $key => $val)\r
-               {                                               \r
-                       // Convert wild-cards to RegEx\r
-                       $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));\r
-                       \r
-                       // Does the RegEx match?\r
-                       if (preg_match('#^'.$key.'$#', $uri))\r
-                       {                       \r
-                               // Do we have a back-reference?\r
-                               if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)\r
-                               {\r
-                                       $val = preg_replace('#^'.$key.'$#', $val, $uri);\r
-                               }\r
-                       \r
-                               $this->_set_request(explode('/', $val));                \r
-                               return;\r
-                       }\r
-               }\r
-\r
-               // If we got this far it means we didn't encounter a\r
-               // matching route so we'll set the site default route\r
-               $this->_set_request($this->uri->segments);\r
-       }\r
-\r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        * Set the class name\r
-        *\r
-        * @access      public\r
-        * @param       string\r
-        * @return      void\r
-        */     \r
-       function set_class($class)\r
-       {\r
-               $this->class = $class;\r
-       }\r
-       \r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        * Fetch the current class\r
-        *\r
-        * @access      public\r
-        * @return      string\r
-        */     \r
-       function fetch_class()\r
-       {\r
-               return $this->class;\r
-       }\r
-       \r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        *  Set the method name\r
-        *\r
-        * @access      public\r
-        * @param       string\r
-        * @return      void\r
-        */     \r
-       function set_method($method)\r
-       {\r
-               $this->method = $method;\r
-       }\r
-\r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        *  Fetch the current method\r
-        *\r
-        * @access      public\r
-        * @return      string\r
-        */     \r
-       function fetch_method()\r
-       {\r
-               if ($this->method == $this->fetch_class())\r
-               {\r
-                       return 'index';\r
-               }\r
-\r
-               return $this->method;\r
-       }\r
-\r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        *  Set the directory name\r
-        *\r
-        * @access      public\r
-        * @param       string\r
-        * @return      void\r
-        */     \r
-       function set_directory($dir)\r
-       {\r
-               $this->directory = $dir.'/';\r
-       }\r
-\r
-       // --------------------------------------------------------------------\r
-       \r
-       /**\r
-        *  Fetch the sub-directory (if any) that contains the requested controller class\r
-        *\r
-        * @access      public\r
-        * @return      string\r
-        */     \r
-       function fetch_directory()\r
-       {\r
-               return $this->directory;\r
-       }\r
-\r
-}\r
-// END Router Class\r
-\r
-/* End of file Router.php */\r
+<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package            CodeIgniter
+ * @author             ExpressionEngine Dev Team
+ * @copyright  Copyright (c) 2008, EllisLab, Inc.
+ * @license            http://codeigniter.com/user_guide/license.html
+ * @link               http://codeigniter.com
+ * @since              Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Router Class
+ *
+ * Parses URIs and determines routing
+ *
+ * @package            CodeIgniter
+ * @subpackage Libraries
+ * @author             ExpressionEngine Dev Team
+ * @category   Libraries
+ * @link               http://codeigniter.com/user_guide/general/routing.html
+ */
+class CI_Router {
+
+       var $config;    
+       var $routes             = array();
+       var $error_routes       = array();
+       var $class                      = '';
+       var $method                     = 'index';
+       var $directory          = '';
+       var $uri_protocol       = 'auto';
+       var $default_controller;
+       var $scaffolding_request = FALSE; // Must be set to FALSE
+       
+       /**
+        * Constructor
+        *
+        * Runs the route mapping function.
+        */
+       function CI_Router()
+       {
+               $this->config =& load_class('Config');
+               $this->uri =& load_class('URI');
+               $this->_set_routing();
+               log_message('debug', "Router Class Initialized");
+       }
+       
+       // --------------------------------------------------------------------
+       
+       /**
+        * Set the route mapping
+        *
+        * This function determines what should be served based on the URI request,
+        * as well as any "routes" that have been set in the routing config file.
+        *
+        * @access      private
+        * @return      void
+        */
+       function _set_routing()
+       {
+               // Are query strings enabled in the config file?
+               // If so, we're done since segment based URIs are not used with query strings.
+               if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
+               {
+                       $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
+
+                       if (isset($_GET[$this->config->item('function_trigger')]))
+                       {
+                               $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
+                       }
+                       
+                       return;
+               }
+               
+               // Load the routes.php file.
+               @include(APPPATH.'config/routes'.EXT);
+               $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
+               unset($route);
+
+               // Set the default controller so we can display it in the event
+               // the URI doesn't correlated to a valid controller.
+               $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);     
+               
+               // Fetch the complete URI string
+               $this->uri->_fetch_uri_string();
+       
+               // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
+               if ($this->uri->uri_string == '')
+               {
+                       if ($this->default_controller === FALSE)
+                       {
+                               show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
+                       }
+
+                       // Turn the default route into an array.  We explode it in the event that
+                       // the controller is located in a subfolder
+                       $segments = $this->_validate_request(explode('/', $this->default_controller));
+
+                       // Set the class and method
+                       $this->set_class($segments[0]);
+                       $this->set_method('index');
+                       
+                       // Assign the segments to the URI class
+                       $this->uri->rsegments = $segments;
+                       
+                       // re-index the routed segments array so it starts with 1 rather than 0
+                       $this->uri->_reindex_segments();
+                       
+                       log_message('debug', "No URI present. Default controller set.");
+                       return;
+               }
+               unset($this->routes['default_controller']);
+               
+               // Do we need to remove the URL suffix?
+               $this->uri->_remove_url_suffix();
+               
+               // Compile the segments into an array
+               $this->uri->_explode_segments();
+               
+               // Parse any custom routing that may exist
+               $this->_parse_routes();         
+               
+               // Re-index the segment array so that it starts with 1 rather than 0
+               $this->uri->_reindex_segments();
+       }
+       
+       // --------------------------------------------------------------------
+       
+       /**
+        * Set the Route
+        *
+        * This function takes an array of URI segments as
+        * input, and sets the current class/method
+        *
+        * @access      private
+        * @param       array
+        * @param       bool
+        * @return      void
+        */
+       function _set_request($segments = array())
+       {       
+               $segments = $this->_validate_request($segments);
+               
+               if (count($segments) == 0)
+               {
+                       return;
+               }
+                                               
+               $this->set_class($segments[0]);
+               
+               if (isset($segments[1]))
+               {
+                       // A scaffolding request. No funny business with the URL
+                       if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
+                       {
+                               $this->scaffolding_request = TRUE;
+                               unset($this->routes['scaffolding_trigger']);
+                       }
+                       else
+                       {
+                               // A standard method request
+                               $this->set_method($segments[1]);
+                       }
+               }
+               else
+               {
+                       // This lets the "routed" segment array identify that the default
+                       // index method is being used.
+                       $segments[1] = 'index';
+               }
+               
+               // Update our "routed" segment array to contain the segments.
+               // Note: If there is no custom routing, this array will be
+               // identical to $this->uri->segments
+               $this->uri->rsegments = $segments;
+       }
+       
+       // --------------------------------------------------------------------
+       
+       /**
+        * Validates the supplied segments.  Attempts to determine the path to
+        * the controller.
+        *
+        * @access      private
+        * @param       array
+        * @return      array
+        */     
+       function _validate_request($segments)
+       {
+               // Does the requested controller exist in the root folder?
+               if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
+               {
+                       return $segments;
+               }
+
+               // Is the controller in a sub-folder?
+               if (is_dir(APPPATH.'controllers/'.$segments[0]))
+               {               
+                       // Set the directory and remove it from the segment array
+                       $this->set_directory($segments[0]);
+                       $segments = array_slice($segments, 1);
+                       
+                       if (count($segments) > 0)
+                       {
+                               // Does the requested controller exist in the sub-folder?
+                               if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
+                               {
+                                       show_404($this->fetch_directory().$segments[0]);
+                               }
+                       }
+                       else
+                       {
+                               $this->set_class($this->default_controller);
+                               $this->set_method('index');
+                       
+                               // Does the default controller exist in the sub-folder?
+                               if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
+                               {
+                                       $this->directory = '';
+                                       return array();
+                               }
+                       
+                       }
+
+                       return $segments;
+               }
+
+               // Can't find the requested controller...
+               show_404($segments[0]);
+       }
+
+       // --------------------------------------------------------------------
+
+       /**
+        *  Parse Routes
+        *
+        * This function matches any routes that may exist in
+        * the config/routes.php file against the URI to
+        * determine if the class/method need to be remapped.
+        *
+        * @access      private
+        * @return      void
+        */
+       function _parse_routes()
+       {
+               // Do we even have any custom routing to deal with?
+               // There is a default scaffolding trigger, so we'll look just for 1
+               if (count($this->routes) == 1)
+               {
+                       $this->_set_request($this->uri->segments);
+                       return;
+               }
+
+               // Turn the segment array into a URI string
+               $uri = implode('/', $this->uri->segments);
+
+               // Is there a literal match?  If so we're done
+               if (isset($this->routes[$uri]))
+               {
+                       $this->_set_request(explode('/', $this->routes[$uri]));         
+                       return;
+               }
+                               
+               // Loop through the route array looking for wild-cards
+               foreach ($this->routes as $key => $val)
+               {                                               
+                       // Convert wild-cards to RegEx
+                       $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
+                       
+                       // Does the RegEx match?
+                       if (preg_match('#^'.$key.'$#', $uri))
+                       {                       
+                               // Do we have a back-reference?
+                               if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
+                               {
+                                       $val = preg_replace('#^'.$key.'$#', $val, $uri);
+                               }
+                       
+                               $this->_set_request(explode('/', $val));                
+                               return;
+                       }
+               }
+
+               // If we got this far it means we didn't encounter a
+               // matching route so we'll set the site default route
+               $this->_set_request($this->uri->segments);
+       }
+
+       // --------------------------------------------------------------------
+       
+       /**
+        * Set the class name
+        *
+        * @access      public
+        * @param       string
+        * @return      void
+        */     
+       function set_class($class)
+       {
+               $this->class = $class;
+       }
+       
+       // --------------------------------------------------------------------
+       
+       /**
+        * Fetch the current class
+        *
+        * @access      public
+        * @return      string
+        */     
+       function fetch_class()
+       {
+               return $this->class;
+       }
+       
+       // --------------------------------------------------------------------
+       
+       /**
+        *  Set the method name
+        *
+        * @access      public
+        * @param       string
+        * @return      void
+        */     
+       function set_method($method)
+       {
+               $this->method = $method;
+       }
+
+       // --------------------------------------------------------------------
+       
+       /**
+        *  Fetch the current method
+        *
+        * @access      public
+        * @return      string
+        */     
+       function fetch_method()
+       {
+               if ($this->method == $this->fetch_class())
+               {
+                       return 'index';
+               }
+
+               return $this->method;
+       }
+
+       // --------------------------------------------------------------------
+       
+       /**
+        *  Set the directory name
+        *
+        * @access      public
+        * @param       string
+        * @return      void
+        */     
+       function set_directory($dir)
+       {
+               $this->directory = $dir.'/';
+       }
+
+       // --------------------------------------------------------------------
+       
+       /**
+        *  Fetch the sub-directory (if any) that contains the requested controller class
+        *
+        * @access      public
+        * @return      string
+        */     
+       function fetch_directory()
+       {
+               return $this->directory;
+       }
+
+}
+// END Router Class
+
+/* End of file Router.php */
 /* Location: ./system/libraries/Router.php */
\ No newline at end of file