Ejemplo n.º 1
0
 static function parse_url()
 {
     if (Router::$controller) {
         return;
     }
     // Work around problems with the CGI sapi by enforcing our default path
     if ($_SERVER["SCRIPT_NAME"] && "/" . Router::$current_uri == $_SERVER["SCRIPT_NAME"]) {
         Router::$controller_path = MODPATH . "gallery/controllers/albums.php";
         Router::$controller = "albums";
         Router::$method = 1;
         return;
     }
     $current_uri = html_entity_decode(Router::$current_uri, ENT_QUOTES);
     $item = ORM::factory("item")->where("relative_path_cache", $current_uri)->find();
     if (!$item->loaded) {
         // It's possible that the relative path cache for the item we're looking for is out of date,
         // so find it the hard way.
         $count = count(Router::$segments);
         foreach (ORM::factory("item")->where("name", html_entity_decode(Router::$segments[$count - 1], ENT_QUOTES))->where("level", $count + 1)->find_all() as $match) {
             if ($match->relative_path() == $current_uri) {
                 $item = $match;
             }
         }
     }
     if ($item && $item->loaded) {
         Router::$controller = "{$item->type}s";
         Router::$controller_path = MODPATH . "gallery/controllers/{$item->type}s.php";
         Router::$method = $item->id;
     }
 }
Ejemplo n.º 2
0
 private static function scan($force = FALSE)
 {
     $found = FALSE;
     if (!self::$scanned || $force) {
         foreach (self::$routes as $priority => $routes) {
             if ($found) {
                 break;
             }
             foreach ($routes as $route) {
                 if ($found) {
                     break;
                 }
                 unset($ctrl, $redirect);
                 list($pattern, $replacement, $method, $source) = $route;
                 switch ($method) {
                     case self::ROUTE_STATIC:
                         if (URI_PATH === $pattern) {
                             $ctrl = $replacement;
                         }
                         break;
                     case self::ROUTE_PCRE:
                         if (preg_match($pattern, URI_PATH)) {
                             $ctrl = preg_replace($pattern, $replacement, URI_PATH);
                         }
                         break;
                     case self::REDIRECT_STATIC:
                         if (URI_PATH === $pattern) {
                             $redirect = $replacement;
                         }
                         break;
                     case self::REDIRECT_PCRE:
                         if (preg_match($pattern, URI_PATH)) {
                             $redirect = preg_replace($pattern, $replacement, URI_PATH);
                         }
                         break;
                 }
                 if (isset($ctrl) || isset($redirect)) {
                     if (isset($ctrl) && is_readable($ctrl)) {
                         self::$pattern = $pattern;
                         self::$ctrl = $ctrl;
                         self::$method = $method;
                         self::$source = $source;
                         $found = TRUE;
                     }
                 }
             }
         }
         if (!self::$ctrl && !isset($redirect) && substr(URI_PATH, -1) !== '/') {
             $redirect = URI_PATH . '/';
             if (strlen(URI_PARAM)) {
                 $redirect .= '?' . URI_PARAM;
             }
         }
         if (isset($redirect)) {
             header('Location: ' . $redirect);
             exit;
         }
         self::$scanned = !$force;
     }
 }
Ejemplo n.º 3
0
 /**
  * Constructor.
  * Acts as mini Router that picks up at the Controller level, post-routing.
  * Searches for files in directory relative to delegating class.
  * 
  * Uses argument already set in Router as provided in URL. 
  * 
  * The URL parameter that would ordinarily be the method is now the subdirectory
  * The URL parameter that would be the first method parameter is now the controller.
  * The URL paramter that would be the second method parameter is now the method.
  * 
  * Any remaining URL parameters are passed along as method parameters for the
  * Subcontroller invoked.
  */
 public function __construct()
 {
     // Find the file in the directory relative to the original Controller.
     $subdirectory = dirname($this->file) . '/' . Router::$method;
     $file = strtolower(array_shift(Router::$arguments));
     $path = $subdirectory . '/' . $file . FILE_EXTENSION;
     $default_path = $subdirectory . '/' . Router::$method . FILE_EXTENSION;
     if (file_exists($path)) {
         Loader::load($path);
         $controller = ucfirst($file) . '_Controller';
         if (count(Router::$arguments)) {
             Router::$method = array_shift(Router::$arguments);
         } else {
             Router::$method = Registry::$config['core']['default_controller_method'];
         }
         Core::run_controller($controller);
     } elseif (file_exists($default_path)) {
         // Check for a default file of the same name as the directory, and load that with the default method.
         Loader::load($default_path);
         $controller = ucfirst(strtolower(Router::$method)) . '_Controller';
         Router::$method = Registry::$config['core']['default_controller_method'];
         Core::run_controller($controller);
     } else {
         // No matches.
         Core::error_404();
     }
     // Exit to prevent Router from continuing in Core.
     exit;
 }
Ejemplo n.º 4
0
function call_fallback_page()
{
    if (Router::$controller === NULL) {
        Router::$controller = 'fallback_page';
        Router::$method = 'find_view';
        Router::$controller_path = APPPATH . 'controllers/fallback_page.php';
    }
}
Ejemplo n.º 5
0
 /**
  * If Gallery is in maintenance mode, then force all non-admins to get routed to a "This site is
  * down for maintenance" page.
  */
 static function maintenance_mode()
 {
     $maintenance_mode = Kohana::config("core.maintenance_mode", false, false);
     if (Router::$controller != "login" && !empty($maintenance_mode) && !user::active()->admin) {
         Router::$controller = "maintenance";
         Router::$controller_path = MODPATH . "gallery/controllers/maintenance.php";
         Router::$method = "index";
     }
 }
Ejemplo n.º 6
0
 public static function parse($url)
 {
     $url = rtrim($url, '/');
     $url = explode('/', $url);
     if ($url[0]) {
         self::$name = ucfirst(strtolower($url[0]));
     }
     self::$method = $url[1];
     self::$params = array();
     if (count($url) > 2) {
         //есть параметры
         for ($i = 2; $i < count($url); $i++) {
             self::$params[] = $url[$i];
         }
     }
 }
Ejemplo n.º 7
0
 static function parse_url()
 {
     if (Router::$controller) {
         return;
     }
     $count = count(Router::$segments);
     foreach (ORM::factory("item")->where("name", Router::$segments[$count - 1])->where("level", $count + 1)->find_all() as $match) {
         if ($match->relative_path() == Router::$current_uri) {
             $item = $match;
         }
     }
     if (!empty($item)) {
         Router::$controller = "{$item->type}s";
         Router::$controller_path = APPPATH . "controllers/{$item->type}s.php";
         Router::$method = $item->id;
     }
 }
Ejemplo n.º 8
0
 public static function setMethod()
 {
     $method = strtolower($_SERVER['REQUEST_METHOD']);
     if ((SITE == 'dev' or SITE == 'stage') and isset($_GET['method'])) {
         $method = $_GET['method'];
         unset($_GET['method']);
     }
     if ($method == 'post') {
         if (self::$subresource == 'collection' or self::$subresource == 'throttles' or strpos(self::$resource, 'budget') !== false or self::$resource == 'token' and !self::$id or self::$resource == 'sim') {
             $method = 'add';
         } else {
             $method = 'set';
         }
     }
     unset($_GET['method']);
     self::$method = $method;
 }
Ejemplo n.º 9
0
 static function parse_url()
 {
     if (Router::$controller) {
         return;
     }
     // Work around problems with the CGI sapi by enforcing our default path
     if ($_SERVER["SCRIPT_NAME"] && "/" . Router::$current_uri == $_SERVER["SCRIPT_NAME"]) {
         Router::$controller_path = MODPATH . "gallery/controllers/albums.php";
         Router::$controller = "albums";
         Router::$method = 1;
         return;
     }
     $item = self::get_item_from_uri(Router::$current_uri);
     if ($item && $item->loaded) {
         Router::$controller = "{$item->type}s";
         Router::$controller_path = MODPATH . "gallery/controllers/{$item->type}s.php";
         Router::$method = $item->id;
     }
 }
Ejemplo n.º 10
0
 static function parse_url()
 {
     if (Router::$controller) {
         return;
     }
     // Work around problems with the CGI sapi by enforcing our default path
     if ($_SERVER["SCRIPT_NAME"] && "/" . Router::$current_uri == $_SERVER["SCRIPT_NAME"]) {
         Router::$controller_path = MODPATH . "gallery/controllers/albums.php";
         Router::$controller = "albums";
         Router::$method = 1;
         return;
     }
     $item = item::find_by_relative_url(html_entity_decode(Router::$current_uri, ENT_QUOTES));
     if ($item && $item->loaded()) {
         Router::$controller = "{$item->type}s";
         Router::$controller_path = MODPATH . "gallery/controllers/{$item->type}s.php";
         Router::$method = "show";
         Router::$arguments = array($item);
     }
 }
Ejemplo n.º 11
0
 public static function operation()
 {
     $filepath = Router::controller('filepath');
     $classname = Router::controller('classname');
     $method = Router::method();
     $arguments = Router::arguments();
     //加载controller
     if (file_exists($filepath)) {
         Zotop::load($filepath);
     }
     if (class_exists($classname, false)) {
         $controller = new $classname();
         if (method_exists($controller, $method) && $method[0] != '_') {
             call_user_func_array(array($controller, $method), $arguments);
         } else {
             //Zotop::run('system.status.404');
             //当方法不存在时,默认调用类的_empty()函数,改函数默认显示一个404错误,你可以在控制器中重写此方法
             call_user_func_array(array($controller, '_empty'), $arguments);
         }
     } else {
         Zotop::run('system.status.404');
     }
 }
Ejemplo n.º 12
0
 /**
  * If the gallery is only available to registered users and the user is not logged in, present
  * the login page.
  */
 static function private_gallery()
 {
     if (identity::active_user()->guest && !access::user_can(identity::guest(), "view", item::root()) && php_sapi_name() != "cli") {
         try {
             $class = new ReflectionClass(ucfirst(Router::$controller) . '_Controller');
             $allowed = $class->getConstant("ALLOW_PRIVATE_GALLERY") === true;
         } catch (ReflectionClass $e) {
             $allowed = false;
         }
         if (!$allowed) {
             if (Router::$controller == "admin") {
                 // At this point we're in the admin theme and it doesn't have a themed login page, so
                 // we can't just swap in the login controller and have it work.  So redirect back to the
                 // root item where we'll run this code again with the site theme.
                 url::redirect(item::root()->abs_url());
             } else {
                 Session::instance()->set("continue_url", url::abs_current());
                 Router::$controller = "login";
                 Router::$controller_path = MODPATH . "gallery/controllers/login.php";
                 Router::$method = "html";
             }
         }
     }
 }
Ejemplo n.º 13
0
 public function init()
 {
     $uri = explode('?', $_SERVER['REQUEST_URI']);
     $uri = explode('/', $uri[0]);
     if (count($uri) < 3 or $uri[2] == '') {
         Router::$objectName = 'Index';
     } else {
         Router::$objectName = $uri[2];
     }
     Router::$object = RESTObject::objectFactory();
     switch ($_SERVER['REQUEST_METHOD']) {
         case 'POST':
             Router::$method = Router::POST;
             break;
         case 'PUT':
             Router::$method = Router::PUT;
             break;
         case 'DELETE':
             Router::$method = Router::DELETE;
             break;
         default:
             Router::$method = Router::GET;
     }
 }
Ejemplo n.º 14
0
 /**
  * Determines the appropriate controller, method, and arguments 
  * from the current URI request.
  * Where necessary, defaults will be employed.
  * Values are stored in local static members for use by the core.
  */
 public static function current()
 {
     $current = $_SERVER['PHP_SELF'];
     // Are we being run from command line?
     if (PHP_SAPI === 'cli') {
         // $argv and $argc are disabled by default in some configurations.
         $argc = isset($argc) ? $argc : $_SERVER['argc'];
         if ($argc > 0) {
             $args = isset($argv) ? $argv : $_SERVER['argv'];
             // Merge all cli arguments as if they were in a uri from web context.
             $current = implode('/', $args);
         } else {
             $current = $_SERVER['PHP_SELF'];
         }
     }
     // Remove dot paths
     $current = preg_replace('#\\.[\\s./]*/#', '', $current);
     // Remove leading slash
     $current = ltrim($current, '/');
     // Reduce multiple slashes
     $current = preg_replace('#//+#', '/', $current);
     // Remove front controller from URI
     $current = ltrim($current, Registry::$config['core']['front_controller'] . FILE_EXTENSION);
     // Remove any remaining leading/trailing slashes
     $current = trim($current, '/');
     $parts = array();
     if (strlen($current) > 0) {
         $parts = explode('/', $current);
     }
     /**
      * The controller is expected to be the first part.
      * If not supplied, we'll assume the default controller
      * defined in config.
      * 
      * The method, if supplied, is expected to be the second part.
      * If not supplied, we'll assume that the default method defined
      * in config is the intended method.
      * 
      * Any remaining parts are presumed to be arguments to the 
      * method and will be treated as such.
      */
     if (count($parts)) {
         self::$controller = array_shift($parts);
     } else {
         self::$controller = Registry::$config['core']['default_controller'];
     }
     if (count($parts)) {
         self::$method = array_shift($parts);
     } else {
         self::$method = Registry::$config['core']['default_controller_method'];
     }
     if (count($parts)) {
         self::$arguments = $parts;
     }
 }
Ejemplo n.º 15
0
 /**
  * Cleans any supplied suffixes out of the URI
  *
  * @return  void
  * @author  Sam Clark
  * @access  protected
  */
 public function process_request()
 {
     // Launch a Restful preprocessing event
     Event::run('restful.pre_processing', $this);
     if ($this->config['caching']) {
         // Load the cache name
         $cache_name = $this->get_cache_name();
         if ($cached = $this->cache->get($cache_name)) {
             Router::$method = $cached['method'];
             Router::$arguments = $cached['args'];
             self::$mime = $cached['mime'];
             self::$http_method = $cached['http_method'];
             Event::run('restful.post_processing', $this);
             return;
         }
     }
     // First check the Routers own detection
     if (Router::$url_suffix !== '') {
         $ext = ltrim(Router::$url_suffix, '.');
         if (array_key_exists($ext, $this->config['supported_mimes'])) {
             self::$mime = array('extension' => $ext, 'type' => Kohana::config('mimes.' . $ext . '.0'));
         }
     } else {
         // If there are Router arguments, the extension will be here
         if (Router::$arguments) {
             // Remove any supplied extension applied to the last argument before controller processing
             Router::$arguments[] = preg_replace_callback($this->config['supported_mimes'], array($this, 'replace_mime'), array_pop(Router::$arguments), 1);
         } else {
             // Clean the router method
             Router::$method = preg_replace_callback($this->config['supported_mimes'], array($this, 'replace_mime'), Router::$method, 1);
         }
     }
     // Try and detect the HTTP method
     self::$http_method = $this->detect_http_method();
     // Cache the process if required
     if ($this->config['caching']) {
         $this->cache->set($cache_name, array('method' => Router::$method, 'args' => Router::$arguments, 'mime' => self::$mime, 'http_method' => self::$http_method), array('mime'), $this->config['caching'] === TRUE ? NULL : $this->config['caching']);
     }
     // If auto send header is set to true, send the content-type header
     if ($this->config['auto_send_header']) {
         header('Content-Type: ' . self::$mime['type']);
     }
     // Launch a post processing event
     Event::run('restful.post_processing', $this);
     return;
 }
Ejemplo n.º 16
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;
 }
Ejemplo n.º 17
0
 /**
  * Invokes method $method (with arguments $arguments) of the wrapped Controller, optionally replacing
  * $_GET and $_POST contents before making the call.
  * @param $method The name of the method to invoke.
  * @param $arguments The arguments to pass.
  * @param $get The array that should replace $_GET.
  * @param $post The array that should replace $_POST.
  * @param $capture If true (default) any output generated by the method is captured and returned,
  *        otherwise the method's return value itself is returned.
  * @return The called method's output or its return value, depending on $capture.
  */
 public function method($method, $arguments = null, $get = null, $post = null, $capture = true)
 {
     // If method does not exist, call the "__call" magic method
     // This can be made faster by catching an exception in the switch statement below
     if (!method_exists($this->controller, $method)) {
         $arguments = array($method, $arguments);
         $method = '__call';
     }
     // Change Router state to reflect call
     //Router::$current_route = '';
     //Router::$current_uri = '';
     //Router::$complete_uri = '';
     Router::$controller = $this->router_controller;
     Router::$method = $method;
     if ($arguments === null) {
         Router::$arguments = array();
     } else {
         Router::$arguments = $arguments;
     }
     // If get or post parameters are passed, alter $_GET, $_POST and Router::$query_string accordingly
     // NOTE: Should we alter $_SERVER['QUERY_STRING'] too?
     if ($get !== null) {
         $old_get = $_GET;
         $_GET = $get;
         Router::$query_string = '?' . http_build_query($get);
     }
     if ($post !== null) {
         $old_post = $_POST;
         $_POST = $post;
     }
     // Start output capture
     if ($capture) {
         ob_implicit_flush(0);
         ob_start();
     }
     if (is_string($arguments)) {
         $arguments = array($arguments);
     }
     // Invoke method
     switch (count($arguments)) {
         case 1:
             $result = $this->controller->{$method}($arguments[0]);
             break;
         case 2:
             $result = $this->controller->{$method}($arguments[0], $arguments[1]);
             break;
         case 3:
             $result = $this->controller->{$method}($arguments[0], $arguments[1], $arguments[2]);
             break;
         case 4:
             $result = $this->controller->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
             break;
         default:
             // Resort to using call_user_func_array for many arguments (slower)
             $result = call_user_func_array(array($this->controller, $method), $arguments);
             break;
     }
     // Run system.post_controller
     Event::run('dispatch.post_controller');
     // Stop output capture
     if ($capture) {
         $result = ob_get_contents();
         ob_end_clean();
     }
     // Revert $_GET and $_POST changes
     if ($get !== null) {
         $_GET = $old_get;
     }
     if ($post !== null) {
         $_POST = $old_post;
     }
     // Revert Router state
     //Router::$current_route = $this->router_state_backup['current_route'];
     //Router::$current_uri = $this->router_state_backup['current_uri'];
     //Router::$complete_uri = $this->router_state_backup['complete_uri'];
     Router::$query_string = $this->router_state_backup['query_string'];
     Router::$controller = $this->router_state_backup['controller'];
     Router::$method = $this->router_state_backup['method'];
     Router::$arguments = $this->router_state_backup['arguments'];
     return $result;
 }
Ejemplo n.º 18
0
 public static function init()
 {
     // find URI
     $uri = self::uri();
     // remove query string from URI
     if (($query = strpos($uri, '?')) !== FALSE) {
         // split URI on question mark
         list($uri, $query) = explode('?', $uri, 2);
         // parse the query string into $_GET if using CLI
         // warning: converts spaces and dots to underscores
         if (PHP_SAPI === 'cli') {
             parse_str($query, $_GET);
         }
     }
     // store requested URI on first run only
     if (self::$current_uri === NULL) {
         self::$current_uri = trim($uri, '/');
     }
     // matches a defined route
     $matched = FALSE;
     // match URI against route
     foreach (self::$routes as $route => $callback) {
         // trim slashes
         $route = trim($route, '/');
         $callback = trim($callback, '/');
         if (preg_match('#^' . $route . '$#u', self::$current_uri)) {
             if (strpos($callback, '$') !== FALSE) {
                 // use regex routing
                 self::$routed_uri = preg_replace('#^' . $route . '$#u', $callback, self::$current_uri);
             } else {
                 // standard routing
                 self::$routed_uri = $callback;
             }
             // valid route has been found
             $matched = TRUE;
             break;
         }
     }
     // no route matches found, use actual uri
     if (!$matched) {
         self::$routed_uri = self::$current_uri;
     }
     // use default route if requesting /
     if (empty(self::$routed_uri)) {
         self::$routed_uri = self::$routes['_default'];
     }
     // decipher controller/method
     $segments = explode('/', self::$routed_uri);
     // controller is first segment
     self::$controller = $segments[0];
     // use default method if none specified
     self::$method = isset($segments[1]) ? $segments[1] : self::$method;
     // remaining arguments
     self::$arguments = array_slice($segments, 2);
     // instatiate controller
     self::execute();
 }
Ejemplo n.º 19
0
 private function getRequestMethod()
 {
     self::$method = InputData::getMethod();
     Log::log(self::CLASSNAME, 'Method: ' . self::$method);
 }
Ejemplo n.º 20
0
 /**
  * Router setup routine. Automatically called during Kohana setup process.
  *
  * @return  void
  */
 public static function setup()
 {
     if (!empty($_SERVER['QUERY_STRING'])) {
         // Set the query string to the current query string
         Router::$query_string = '?' . trim($_SERVER['QUERY_STRING'], '&/');
     }
     if (Router::$routes === NULL) {
         // Load routes
         Router::$routes = Kohana::config('routes');
     }
     // Default route status
     $default_route = FALSE;
     if (Router::$current_uri === '') {
         // Make sure the default route is set
         if (!isset(Router::$routes['_default'])) {
             throw new Kohana_Exception('core.no_default_route');
         }
         // Use the default route when no segments exist
         Router::$current_uri = Router::$routes['_default'];
         // Default route is in use
         $default_route = TRUE;
     }
     // Make sure the URL is not tainted with HTML characters
     Router::$current_uri = html::specialchars(Router::$current_uri, FALSE);
     // Remove all dot-paths from the URI, they are not valid
     Router::$current_uri = preg_replace('#\\.[\\s./]*/#', '', Router::$current_uri);
     // At this point segments, rsegments, and current URI are all the same
     Router::$segments = Router::$rsegments = Router::$current_uri = trim(Router::$current_uri, '/');
     // Set the complete URI
     Router::$complete_uri = Router::$current_uri . Router::$query_string;
     // Explode the segments by slashes
     Router::$segments = ($default_route === TRUE or Router::$segments === '') ? array() : explode('/', Router::$segments);
     if ($default_route === FALSE and count(Router::$routes) > 1) {
         // Custom routing
         Router::$rsegments = Router::routed_uri(Router::$current_uri);
     }
     // The routed URI is now complete
     Router::$routed_uri = Router::$rsegments;
     // Routed segments will never be empty
     Router::$rsegments = explode('/', Router::$rsegments);
     // Prepare to find the controller
     $controller_path = '';
     $method_segment = NULL;
     // Paths to search
     $paths = Kohana::include_paths();
     foreach (Router::$rsegments as $key => $segment) {
         // Add the segment to the search path
         $controller_path .= $segment;
         $found = FALSE;
         foreach ($paths as $dir) {
             // Search within controllers only
             $dir .= 'controllers/';
             if (is_dir($dir . $controller_path) or is_file($dir . $controller_path . EXT)) {
                 // Valid path
                 $found = TRUE;
                 // The controller must be a file that exists with the search path
                 if ($c = str_replace('\\', '/', realpath($dir . $controller_path . EXT)) and is_file($c) and strpos($c, $dir) === 0) {
                     // Set controller name
                     Router::$controller = $segment;
                     // Change controller path
                     Router::$controller_path = $c;
                     // Set the method segment
                     $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(Router::$rsegments[$method_segment])) {
         // Set method
         Router::$method = Router::$rsegments[$method_segment];
         if (isset(Router::$rsegments[$method_segment + 1])) {
             // Set arguments
             Router::$arguments = array_slice(Router::$rsegments, $method_segment + 1);
         }
     }
     // Last chance to set routing before a 404 is triggered
     Event::run('system.post_routing');
     if (Router::$controller === NULL) {
         // No controller was found, so no page can be rendered
         Event::run('system.404');
     }
 }
Ejemplo n.º 21
0
 /**
  * Determines the appropriate controller, method, and arguments 
  * from the current URI request.
  * Where necessary, defaults will be employed.
  * Values are stored in local static members for use by the core.
  */
 public static function current()
 {
     $current = self::getRequestPath();
     // Are we being run from command line?
     if (PHP_SAPI === 'cli') {
         // $argv and $argc are disabled by default in some configurations.
         $argc = isset($argc) ? $argc : $_SERVER['argc'];
         if ($argc > 0) {
             $args = isset($argv) ? $argv : $_SERVER['argv'];
             // Merge all cli arguments as if they were in a uri from web context.
             $current = implode('/', $args);
         } else {
             $current = self::getRequestPath();
         }
     }
     // Remove dot paths
     $current = preg_replace('#\\.[\\s./]*/#', '', $current);
     // Remove leading slash
     $current = ltrim($current, '/');
     // Reduce multiple slashes
     $current = preg_replace('#//+#', '/', $current);
     // Remove front controller from URI if present (depends on variable used)
     $frontController = \Config::get('core.front_controller') . FILE_EXTENSION;
     if (substr($current, 0, strlen($frontController)) == $frontController) {
         $current = substr($current, strlen($frontController));
     }
     // Remove any remaining leading/trailing slashes
     $current = trim($current, '/');
     // Check for rewrites matching configuration values.
     $matched = $current;
     $rewrites = \Config::get('rewrites');
     if (!empty($rewrites)) {
         foreach ($rewrites as $match => $replace) {
             $match = trim($match, '/');
             // Exact match?
             if ($match == $current) {
                 $matched = $replace;
             } elseif (preg_match('#^' . $match . '$#u', $current)) {
                 if (strpos($replace, '$')) {
                     // Replacement requires arguments that were parsed during match.
                     $matched = preg_replace('#^' . $match . '$#u', $replace, $current);
                 } else {
                     // Found match, no parsed arguments required.
                     $matched = $replace;
                 }
             }
         }
     }
     $current = $matched;
     $parts = array();
     if (strlen($current) > 0) {
         $parts = explode('/', $current);
     }
     if (empty($parts)) {
         self::$controller = \Config::get('core.default_controller');
     } else {
         $controller = '';
         while (count($parts)) {
             $controller .= array_shift($parts);
             if (Loader::search('controller' . DIRECTORY_SEPARATOR . $controller)) {
                 self::$controller = $controller;
                 if (count($parts)) {
                     self::$method = array_shift($parts);
                 }
                 if (count($parts)) {
                     self::$arguments = $parts;
                 }
             } else {
                 $controller .= DIRECTORY_SEPARATOR;
             }
         }
     }
     if (empty(self::$method)) {
         self::$method = \Config::get('core.default_controller_method');
     }
 }