Example #1
0
 /**
  * Setup routine.
  * Automatically called during the core initialization process.
  * @return bool
  * @throws Exception_Exido
  * @throws Exception_Exido_404
  */
 public static function initialize()
 {
     if (self::$_routes === null) {
         // Load routes
         self::$_routes = Exido::config('route');
     }
     // Debug log
     if (Exido::$log_debug) {
         Exido::$log->add('EXIDO_DEBUG_LOG', __('Initialize routing'));
     }
     $default_route = false;
     if (self::$current_uri == Exido::config('global.core.index_file')) {
         self::$current_uri = '';
     }
     if (!isset(self::$_routes['default_method'])) {
         self::$_routes['default_method'] = self::$method;
     }
     // Remove web-root directory
     if (WEB_ROOT != '') {
         self::$web_root = trim(WEB_ROOT, '/');
         // Remove the suffix from the URL if needed
         self::$current_uri = trim(preg_replace("|^" . self::$web_root . "|", "", self::$current_uri), '/');
     }
     if (self::$current_uri == '') {
         // If default controller is not set
         if (!isset(self::$_routes['default_controller']) or self::$_routes['default_controller'] == '') {
             self::$_routes['default_controller'] = self::$default;
         }
         // Use the default route when no segments exist
         self::$current_uri = self::$_routes['default_controller'];
         // Default route is in use
         $default_route = true;
     }
     if ($default_route == false) {
         // Remove the suffix from the URL if needed
         self::$current_uri = preg_replace("|" . preg_quote(Exido::config('global.core.url_suffix')) . "\$|", "", self::$current_uri);
         // Strip arguments
         if ($argspos = strpos(self::$current_uri, '?')) {
             self::$current_uri = substr(self::$current_uri, 0, $argspos);
         }
         // Explode the segments by slashes
         foreach (explode("/", preg_replace("|/*(.+?)/*\$|", "\\1", self::$current_uri)) as $val) {
             $val = trim($val);
             // Check for allowed characters
             if (Exido::config('global.core.permitted_uri_chars') != '' and $val != '') {
                 // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
                 // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
                 if (!preg_match("|^[" . str_replace(array('\\-', '\\-'), '-', preg_quote(Exido::config('global.core.permitted_uri_chars'), '-')) . "]+\$|i", $val)) {
                     throw new Exception_Exido('The requested URL %s has a disallowed characters', array($val));
                 }
             }
             if ($val != '') {
                 self::$segments[] = self::$rsegments[] = $val;
             }
         }
         // Custom routing
         if (count(self::$_routes) > 0) {
             self::$rsegments = self::_getRouted(self::$segments);
         }
         //array_merge(self::_getRouted(self::$segments), self::$segments);
     }
     if ($default_route == true) {
         self::$rsegments[] = self::$current_uri;
     }
     // Prepare to find the controller
     $controller_path = '';
     $method_segment = null;
     $controller_name = array();
     // Paths to search
     $paths = Exido::getIncludePaths();
     foreach (self::$rsegments as $key => $segment) {
         // Add the segment to the search path
         $controller_path .= $segment;
         $found = false;
         // Set segment into controller name
         $controller_name[] = ucfirst($segment);
         foreach ($paths as $dir) {
             // Search within controllers only
             $dir .= 'controller/';
             if (is_dir($dir . $controller_path) or is_file($dir . $controller_path . '.php')) {
                 // Valid path
                 $found = true;
                 // The controller must be a file that exists with the search path
                 if ($c = realpath($dir . $controller_path . '.php') and is_file($c)) {
                     // Set the controller name
                     self::$controller = ucfirst(strtolower(EXIDO_ENVIRONMENT_NAME)) . '_Controller_' . implode('_', $controller_name);
                     self::$controller_view = $controller_path;
                     // Set the controller path
                     self::$controller_path = $c;
                     // Set the method
                     $method_segment = $key + 1;
                     // Stop searching
                     break;
                 }
             }
         }
         if ($found === false) {
             // Maximum depth has been reached, stop searching
             break;
         }
         // Add another slash
         $controller_path .= '/';
     }
     if ($method_segment !== null and isset(self::$rsegments[$method_segment])) {
         // Set method
         if (isset(self::$rsegments[$method_segment])) {
             self::$method = self::$rsegments[$method_segment];
         }
         if (isset(self::$rsegments[$method_segment])) {
             // Set arguments
             self::$arguments = array_slice(self::$rsegments, $method_segment + 1);
         }
     }
     // If controller does not found
     // We throw the 404 page
     if (self::$controller === null) {
         throw new Exception_Exido_404('Undefined controller %s.', array(self::$current_uri));
     }
     return true;
 }