diff --git a/config/config.php b/config/config.php index 66d9053..7011bf8 100644 --- a/config/config.php +++ b/config/config.php @@ -13,6 +13,20 @@ /* START SITE STUFF */ +/* Routing options */ +$conf['prod']['routing']['enabled'] = false; +$conf['test']['routing']['enabled'] = false; +$conf['devel']['routing']['enabled'] = true; + +$conf['prod']['routing']['defaultController'] = 'index'; +$conf['test']['routing']['defaultController'] = 'index'; +$conf['devel']['routing']['defaultController'] = 'index'; + +$conf['prod']['routing']['defaultAction'] = ''; +$conf['test']['routing']['defaultAction'] = ''; +$conf['devel']['routing']['defaultAction'] = ''; +/* End Routing options */ + /* Full path to the content */ $conf['prod']['path']['full'] = ROOT . DS; $conf['test']['path']['full'] = ROOT . DS; diff --git a/controllers/app_controller.class.php b/controllers/app_controller.class.php index 82a4f5c..7e359e4 100644 --- a/controllers/app_controller.class.php +++ b/controllers/app_controller.class.php @@ -37,4 +37,21 @@ function displayPageNotImplementedError(){ $this->log->error("Displaying 501 error. Missing Smarty template, request: " . print_r($_REQUEST, true)); APP::$smarty->display('error_pages/501.tpl'); } + + public static function factory($name) + { + /** TODO: WTF is this shit, needs to be fixed. For some reason it would not load core classes here. */ + if($name == 'App_Controller') { + $classname = $name; + require_once(APP::$conf['path']['core']['controllers'] . 'app_controller.class.php'); + } else { + $classname = ucwords($name) . '_Controller'; + require_once(APP::$conf['path']['controllers'] . strtolower($name) . '.class.php'); + } + return new $classname; + } + + public static function getActionName($name) { + return 'process' . ucwords($name); + } } \ No newline at end of file diff --git a/controllers/permission_controller.class.php b/controllers/permission_controller.class.php index a8f0825..d4ae769 100644 --- a/controllers/permission_controller.class.php +++ b/controllers/permission_controller.class.php @@ -7,7 +7,7 @@ class Permission_Controller { * Overriding standard method with custom action. */ function process(){ - if( !APP::$auth->isLoggedIn) APP::$request->jump("authorize"); + if( !APP::$auth->isLoggedIn) APP::$request->jump("login"); else $this->processWithPermission(); } diff --git a/lib/app_request.class.php b/lib/app_request.class.php index d8fa213..0fed96a 100644 --- a/lib/app_request.class.php +++ b/lib/app_request.class.php @@ -34,20 +34,55 @@ function processUrl() { $this->path_arr = explode('/', $this->url); $this->log->debug("Path exploded is: " . json_encode($this->path_arr)); } + + function processRequestWithRouting() { + $this->log->debug('Posted url: ' . $this->url); + + $router = new APP_Router($this->url); + $router->parse(); + + $controller = $router->getController(); + $action = $router->getAction(); + + APP::$controller = APP_Controller::factory($controller); + $actionName = APP_Controller::getActionName($action); + + $this->log->debug('Controller was set to: ' . get_class(APP::$controller)); + $this->log->debug('Action was set to: ' . $actionName); + + try { + if($action == '404') { + $this->log->debug('File was not found. Displaying 404 error'); + APP::$controller->displayPageNotFoundError(); + } + else { + if(method_exists(APP::$controller, $actionName)) { + $this->log->debug('Method exists. Calling ' . get_class(APP::$controller) . "->" . $actionName); + APP::$controller->$actionName(); + } else { + $this->log->debug(get_class(APP::$controller) . " does not have function $actionName, redirecting user to correct url: " . $this->path_arr[0]); + $this->jump($this->path_arr[0]); + } + } + } catch (SmartyException $e) { + $this->log->error($e); + APP::$controller->displayPageNotImplementedError(); + } + } function processRequest(){ $this->log->debug("processRequest() method has been called, starting process.."); //TODO: implement url_mapping - if(isset($this->path_arr[0])){ + if(isset($this->path_arr[0])){ // 1a. If url exists, continue processing -> OK $controllerName = strtolower($this->path_arr[0]); //check if its index - if($controllerName == ''){ + if($controllerName == ''){ // 2. If empty entry, set default controller -> OK $controllerName = 'index'; } $this->log->debug("Controller name that we will be calling is: " . $controllerName); //lets find out if we have any controller $controllerFile = APP::$conf['path']['controllers'] . $controllerName . '.class.php'; - if(file_exists($controllerFile)){ //controller exists we try to load it + if(file_exists($controllerFile)){ // 3. controller exists we try to load it $this->log->debug("Controller file exists on disk, we try to load it!"); require_once($controllerFile); $controllerClass = ucwords($controllerName) . "_Controller"; @@ -74,13 +109,13 @@ function processRequest(){ APP::$controller->displayPageNotImplementedError(); } - }else{ //controller does not exists, we display 404 error with parent controller + }else{ // 3b. controller does not exists, we display 404 error with parent controller -> OK $this->log->debug("Controller does not exist, we load default controller and display 404 error."); require_once APP::$conf['path']['core']['controllers'] . 'app_controller.class.php'; APP::$controller = new APP_Controller(); APP::$controller->displayPageNotFoundError(); } - }else{ + }else{ // 1b. If no url, then 503 -> OK $this->log->debug("Path Arr is empty.. Something must be wrong, returning 503 error"); //Something went wrong with processing url, we display 503 error. APP::$smarty->display('error_pages/503.tpl'); diff --git a/lib/app_router.class.php b/lib/app_router.class.php new file mode 100644 index 0000000..d55809c --- /dev/null +++ b/lib/app_router.class.php @@ -0,0 +1,76 @@ +log = Logger::getLogger("com.dalisra.request"); + + $this->path = $path; + $this->defaultController = APP::$conf['routing']['defaultController']; + $this->defaultAction = APP::$conf['routing']['defaultAction']; + } + + public function parse() { + if($this->path == null) + $this->path = ''; + + // TODO: Possible bug with this removing the GET parameters ...? + $chunks = explode('/', $this->path); + + if(strlen($chunks[0]) < 1) { + $this->controller = $this->defaultController; + $this->action = $this->defaultAction; + + } else { + $controllerName = array_shift($chunks); + $controllerFile = APP::$conf['path']['controllers'] . $controllerName . '.class.php'; + $actionName = array_shift($chunks); + + /** Assuming error until proven wrong ...Moahahah! */ + $this->controller = 'App_Controller'; + $this->action = '404'; + + if(file_exists($controllerFile)) { + $this->controller = $controllerName; + + if(strlen($actionName) > 0 ) $this->action = $actionName; + else $this->action = $this->defaultAction; + + } else { + $urlmapper = new App_UrlMapper(); + $route = $urlmapper->getMappedRoute($this->path); + + if (count($route) > 0) { + $controllerFile = APP::$conf['path']['controllers'] . $route['controller'] . '.class.php'; + + if (file_exists($controllerFile)) { + $this->controller = $route['controller']; + $this->action = $route['action']; + } + } + } + } + } + + public function getController() + { + return $this->controller; + } + + public function getAction() + { + return $this->action; + } +} \ No newline at end of file diff --git a/lib/app_urlmapper.class.php b/lib/app_urlmapper.class.php new file mode 100644 index 0000000..4a5234c --- /dev/null +++ b/lib/app_urlmapper.class.php @@ -0,0 +1,34 @@ + '/^login/', + 'controller' => 'authorize', + 'action' => 'login' + ), + array( + 'url' => '/^logout/', + 'controller' => 'authorize', + 'action' => 'logout' + ) + ); + + public function getMappedRoute($url) { + $mappedRoute = []; + + foreach($this->routes as $route) { + if(preg_match($route['url'], $url)) { + $mappedRoute = $route; + } + } + + return $mappedRoute; + } +} \ No newline at end of file