Exemple #1
0
 /**
  * Render specified template file with data provided
  * @param $template_path Template file path (full)
  * @param array $data Data array
  * @param bool|true $wrap To be wrapped with main template if true
  * @return bool|html
  * @throws FileException if template file does not exist
  */
 public function render($template_path, $data = array(), $wrap = true)
 {
     //Launches the appropriate event
     Service::get('eventManager')->trigger('renderAction', "Render specified template file \"" . $template_path . "\" with data provided");
     $data['include'] = function ($controllerName, $actionName, $params) {
         Helper::dispatch($controllerName, $actionName, $params);
     };
     $data['getRoute'] = function ($route_name, $params = array()) {
         return Service::get('router')->buildRoute($route_name, $params);
     };
     $data['generateToken'] = function () {
         echo '<input type="hidden" name="token" value="' . Service::get('session')->get('token') . '"/>';
     };
     $data['route'] = Registry::getConfig('route');
     $data['user'] = Service::get('session')->get('user');
     if (!$wrap) {
         //Gets messages from session message container to display them in view
         $flush = Service::get('session')->get('messages') ? Service::get('session')->get('messages') : array();
         Service::get('session')->del('messages');
     }
     extract($data);
     //Checks if template file exists
     if (file_exists($template_path)) {
         ob_start();
         include $template_path;
         $content = ob_get_contents();
         ob_end_clean();
     } else {
         throw new FileException("File " . $template_path . " does not found");
     }
     if ($wrap) {
         $content = $this->renderMain($content);
     }
     return $content;
 }
 public function run()
 {
     $router = Service::get('router');
     $route = $router->parseRoute($_SERVER['REQUEST_URI']);
     try {
         //Checks if route is empty
         if (!empty($route)) {
             //Verifies user role if it needs
             if (array_key_exists('security', $route)) {
                 if (is_null($user = Service::get('session')->get('user')) || !in_array($user->role, $route['security'])) {
                     throw new SecurityException("Access is denied");
                 }
             }
             //Returns Response object
             $response = Helper::dispatch($route['controller'], $route['action'], $route['params']);
         } else {
             throw new HttpNotFoundException("Route does not found", 404);
         }
     } catch (SecurityException $e) {
         Service::get('session')->set('returnUrl', Registry::getConfig('route')['pattern']);
         $response = new ResponseRedirect(Service::get('router')->buildRoute('login'));
     } catch (HttpNotFoundException $e) {
         $response = new Response(Service::get('renderer')->render(Registry::getConfig('error_400'), array('code' => $e->getCode(), 'message' => $e->getMessage())));
     } catch (\Exception $e) {
         $response = new Response(Service::get('renderer')->render(Registry::getConfig('error_500'), array('code' => $e->getCode(), 'message' => $e->getMessage())));
     }
     $response->send();
 }
Exemple #3
0
 /**
  * Parses URL
  * Generates array of suitable route and optional parameters
  * @param $url
  * @return null | array of appropriate route and params
  */
 public function parseRoute($url)
 {
     $route_found = null;
     foreach (self::$map as $routeName => $route) {
         $pattern = $route['pattern'];
         // Checks if pattern has parameters
         if (strpos($pattern, '{')) {
             list($param_names, $pattern) = $this->prepare($route);
         }
         $pattern = '~^' . $pattern . '$~';
         if (preg_match($pattern, $url, $params)) {
             if (isset($route['_requirements']['_method']) && $route['_requirements']['_method'] === 'POST' && !Service::get('request')->isPost()) {
                 continue;
             }
             $route_found = $route;
             $route_found['params'] = array();
             $route_found['_name'] = $routeName;
             //Gets associative array of params
             if (count($params) > 1) {
                 $params = array_map('urldecode', array_slice($params, 1));
                 $params = array_combine($param_names, $params);
                 $route_found['params'] = $params;
             }
             Registry::setConfig('route', $route_found);
             break;
         }
     }
     //Launches the appropriate event
     Service::get('eventManager')->trigger('parseRoute', "Parsed URL \"" . $url . "\". Generated the array of suitable route \"" . $routeName . "\" and optional parameters");
     return $route_found;
 }
 /**
  * Displays profile if it possible otherwise redirects to login page
  * @return \Framework\Response\Response|\Framework\Response\ResponseRedirect
  */
 public function getAction()
 {
     if (Service::get('security')->isAuthenticated()) {
         return $this->render('get.html', array('posts' => Post::findByParams(array('user_id' => Service::get('session')->get('user')->id))));
     }
     //Sets the return page
     Service::get('session')->set('returnUrl', Registry::getConfig('route')['pattern']);
     return $this->redirect($this->generateRoute('login'), 'You need to login');
 }
 /**
  * Displays the edit window
  * @param $id
  * @return \Framework\Response\Response
  */
 public function start_editAction($id)
 {
     if (Service::get('security')->isAuthenticated()) {
         if (Post::find($id)->user_id === Service::get('session')->get('user')->id) {
             return $this->render('start_edit.html', array('post' => Post::find($id)));
         } else {
             return $this->redirect($this->generateRoute('profile'), '"' . Post::find($id)->title . '".' . ' It is not your article');
         }
     }
     Service::get('session')->set('returnUrl', $this->generateRoute(Registry::getConfig('route')['_name'], array('id' => $id)));
     return $this->redirect($this->generateRoute('login'), 'You need to login');
 }
Exemple #6
0
 public static function errorReporting()
 {
     //Checks the development mode. If it is 'dev' shows all messages
     if (Registry::getConfig("mode") === "dev") {
         ini_set('display_errors', 'On');
         ini_set('log_errors', 'Off');
         //Otherwise writes the lof files
     } else {
         ini_set('display_errors', 'Off');
         ini_set('error_log', __DIR__ . '/../../app/error_logs/' . 'errors_' . date("Y_m_d") . '.log');
         ini_set('log_errors', 'On');
     }
 }