/**
  * Find the controller and instantiate it
  */
 public function dispatch()
 {
     try {
         Dispatcher::get_requestUri();
         foreach ($this->routes as $key => $value) {
             if (Dispatcher::hasRoute($this->routes[$key]['url-formatter'])) {
                 $this->controller['directory'] = $this->routes[$key]['directory'];
                 $this->controller['controller'] = $this->routes[$key]['controller'];
                 $this->controller['formatter'] = $this->routes[$key]['url-formatter'];
                 break;
             }
         }
         if ($this->request_uri == null) {
             header('Location: /' . _PROJECT_NAME_ . '/lecons/show/all');
         }
         if ($this->controller == null) {
             $this->controller['directory'] = $this->routes['404-errors']['directory'];
             $this->controller['controller'] = $this->routes['404-errors']['controller'];
             $this->controller['formatter'] = $this->routes['404-errors']['url-formatter'];
         }
         if (file_exists(_CONTROLLERS_DIR_ . '/Tools.php')) {
             require_once _CONTROLLERS_DIR_ . '/Tools.php';
         } else {
             throw new Exception('Fichier "' . _CONTROLLERS_DIR_ . '/Tool.php" introuvable!');
         }
         if (file_exists(_CONTROLLERS_DIR_ . '/' . $this->controller['directory'] . $this->controller['controller'] . '.php')) {
             require_once _CONTROLLERS_DIR_ . '/' . $this->controller['directory'] . $this->controller['controller'] . '.php';
         } else {
             throw new Exception('Fichier "' . _CONTROLLERS_DIR_ . '/' . $this->controller['directory'] . $this->controller['controller'] . '.php" introuvable!');
         }
         Tools::getInstance()->createPost($_POST);
         Tools::getInstance()->createUrl($this->controller['controller'], $this->request_uri);
         $controllerInstance = new $this->controller['controller']();
         if (!$controllerInstance->checkAccess() || !$controllerInstance->viewAccess()) {
             $this->controller['directory'] = $this->routes['403-errors']['directory'];
             $this->controller['controller'] = $this->routes['403-errors']['controller'];
             $this->controller['formatter'] = $this->routes['403-errors']['url-formatter'];
             if (file_exists(_CONTROLLERS_DIR_ . '/' . $this->controller['directory'] . $this->controller['controller'] . '.php')) {
                 require_once _CONTROLLERS_DIR_ . '/' . $this->controller['directory'] . $this->controller['controller'] . '.php';
             } else {
                 throw new Exception('Fichier "' . _CONTROLLERS_DIR_ . '/' . $this->controller['directory'] . $this->controller['controller'] . '.php" introuvable!');
             }
             $controllerInstance = new $this->controller['controller']();
         }
     } catch (Exception $e) {
         throw new Exception('Une erreur est survenue durant la phase de routage: ' . $e->getMessage());
     }
 }
function main()
{
    // get the uri string from the query
    $path = $_SERVER['QUERY_STRING'];
    // Make sure special characters are decoded (support non-western glyphs like japanese)
    $path = urldecode($path);
    // START processing $_GET variables
    // If we're NOT using mod_rewrite, we check for GET variables we need to integrate
    if (!USE_MOD_REWRITE && strpos($path, '?') !== false) {
        $_GET = array();
        // empty $_GET array since we're going to rebuild it
        list($path, $get_var) = explode('?', $path);
        $exploded_get = explode('&', $get_var);
        if (count($exploded_get)) {
            foreach ($exploded_get as $get) {
                list($key, $value) = explode('=', $get);
                $_GET[$key] = $value;
            }
        }
    } else {
        if (!USE_MOD_REWRITE && (strpos($path, '&') !== false || strpos($path, '=') !== false)) {
            $path = '/';
        }
    }
    // If we're using mod_rewrite, we should have a WOLFPAGE entry.
    if (USE_MOD_REWRITE && array_key_exists('WOLFPAGE', $_GET)) {
        $path = $_GET['WOLFPAGE'];
        unset($_GET['WOLFPAGE']);
    } else {
        if (USE_MOD_REWRITE) {
            // We're using mod_rewrite but don't have a WOLFPAGE entry, assume site root.
            $path = '/';
        }
    }
    // Needed to allow for ajax calls to backend
    if (array_key_exists('WOLFAJAX', $_GET)) {
        $path = '/' . ADMIN_DIR . $_GET['WOLFAJAX'];
        unset($_GET['WOLFAJAX']);
    }
    // END processing $_GET variables
    // remove suffix page if founded
    if (URL_SUFFIX !== '' and URL_SUFFIX !== '/') {
        $path = preg_replace('#^(.*)(' . URL_SUFFIX . ')$#i', "\$1", $path);
    }
    define('CURRENT_PATH', trim($path, '/'));
    // Alias for backward compatibility, this constant should no longer be used.
    define('CURRENT_URI', CURRENT_PATH);
    if ($path != null && $path[0] != '/') {
        $path = '/' . $path;
    }
    // Check if there's a custom route defined for this URI,
    // otherwise continue and assume page was requested.
    if (Dispatcher::hasRoute($path)) {
        Observer::notify('dispatch_route_found', $path);
        Dispatcher::dispatch($path);
        exit;
    }
    foreach (Observer::getObserverList('page_requested') as $callback) {
        $path = call_user_func_array($callback, array(&$path));
    }
    // this is where 80% of the things is done
    $page = Page::findByPath($path, true);
    // if we found it, display it!
    if (is_object($page)) {
        // If a page is in preview status, only display to logged in users
        if (Page::STATUS_PREVIEW == $page->status_id) {
            AuthUser::load();
            if (!AuthUser::isLoggedIn() || !AuthUser::hasPermission('page_view')) {
                pageNotFound($path);
            }
        }
        // If page needs login, redirect to login
        if ($page->getLoginNeeded() == Page::LOGIN_REQUIRED) {
            AuthUser::load();
            if (!AuthUser::isLoggedIn()) {
                Flash::set('redirect', $page->url());
                redirect(URL_PUBLIC . (USE_MOD_REWRITE ? '' : '?/') . ADMIN_DIR . '/login');
            }
        }
        Observer::notify('page_found', $page);
        $page->_executeLayout();
    } else {
        pageNotFound($path);
    }
}