/** * Run the application * * @throws InvalidTokenException * @throws HttpNotFoundException * @throws BadResponseTypeException */ public function run() { $router = Service::get('router'); try { $request = new Request(); $security = Service::get('security'); if ($request->isPost() && !$security->validateToken()) { throw new InvalidTokenException(); } $route = $router->parseRoute(); if (!empty($route)) { $security->clearToken(); $response = $this->getResponse($route['controller'], $route['action'], isset($route['params']) ? $route['params'] : array()); } else { throw new HttpNotFoundException('Route Not Found'); } if (!$response instanceof Response) { throw new BadResponseTypeException(); } } catch (HttpNotFoundException $e) { $response = $this->renderError($e); } catch (BadResponseTypeException $e) { $response = $this->renderError($e); } catch (InvalidTokenException $e) { $response = $this->renderError($e); } catch (NotAuthException $e) { Service::get('session')->returnUrl = $router->getCurrentRoute()['pattern']; $response = new ResponseRedirect($router->generateRoute($this->config['security']['login_route'])); } catch (Exception $e) { $response = $this->renderError($e); } $response->send(); }
/** * Validation token * * @throws Exception */ public function verifyCsrfToken() { $request = new Request(); if ($request->isPost() && $request->post('csrfToken')) { if ($request->post('csrfToken') !== Service::get('session')->csrfToken) { throw new CustomException('Invalid token'); } } }
/** * Renderer and return all content to Response Class * * @return string $resalt */ public function renderContent() { //get all controller input data extract($this->data); //include controller relust in content $include = function ($controllerName, $actionName, $data = array()) { $reflectionMethod = new \ReflectionMethod($controllerName, $actionName . 'Action'); $response = $reflectionMethod->invokeArgs(new $controllerName(), $data); echo '<h3>Include</h3>'; echo '<p>'; $response->getContent(); echo '</p>'; }; //generate CSRF token to hidden form element $generateToken = function () { $csrfToken = Service::get('security')->generateCsrfToken(); echo '<input type="hidden" value="' . $csrfToken . '" name="csrfToken">'; }; //get current route information $getRoute = function ($name) { $routes = Service::get('routes'); return $routes[$name]['pattern']; }; $route = Service::get('route'); $request = new Request(); if ($request->isPost() && empty($post)) { $post = new \stdClass(); $post->title = $request->post('title'); $post->content = $request->post('content'); } if (Session::get('auth')) { $user = Service::get('security')->getUser(); } if (isset(Service::get('session')->flash)) { $flush = array('info' => array(Service::get('session')->flash)); unset(Service::get('session')->flash); } else { $flush = array(); } //Render template ob_start(); include $this->templateUrl; $content = ob_get_contents(); ob_end_clean(); //Render main layout ob_start(); include $this->layoutUrl; $this->renderDevMode(); $result = ob_get_contents(); ob_end_clean(); return $result; }
public function editAction($id, Request $request) { $id = (int) $id; $errors = []; if ($request->isPost()) { try { $date = new \DateTime(); $id = Post::where(['id' => $id])->update(['title' => $request->post('title'), 'content' => $request->post('content'), 'date' => $date->format('Y-m-d H:i:s')]); return $this->redirect($this->generateRoute('home'), 'The data has been updated successfully'); } catch (DatabaseException $e) { $errors[] = $e->getMessage(); } } return $this->render('edit.html', ['post' => Post::find($id), 'action' => $this->generateRoute('edit_post', ['id' => $id]), 'errors' => $errors ?? null]); }
public function addAction() { $dirty_request = new Request(null, false); if ($dirty_request->isPost()) { try { $post = new Post(); $date = new \DateTime(); $post->title = $dirty_request->post('title'); $post->content = trim($dirty_request->post('content')); $post->date = $date->format('Y-m-d H:i:s'); $post->users_id = Service::get('session')->get('authenticated')->id; $validator = new Validator($post); if ($validator->isValid()) { $post->save(); return $this->redirect($this->generateRoute('home'), 'The data has been saved successfully'); } else { $error = $validator->getErrors(); } } catch (DatabaseException $e) { $error = $e->getMessage(); } } return $this->render('add.html', array('action' => $this->generateRoute('add_post'), 'errors' => isset($error) ? $error : null)); }