/** * @param Router $router */ protected function validateToken(Router $router) { $request = $router->getRequest(); $httpMethod = $request->getHttpMethod(); if ($httpMethod !== 'GET' && $router->getCurrentRoute()->mustBeCsrfProtected()) { $csrfToken = $this->getCsrfFromRequest($request); if (!$this->verifyToken($csrfToken)) { throw new HttpException("Given X-CSRF-TOKEN does not match!", Response::HTTP_UNPROCESSABLE_ENTITY); } } else { $this->generateToken(); } }
/** * Base init */ private function baseInit() { // Add Page Title and Page Name (and other Variables defined in Router) to global $globalVariables = $this->router->getCurrentRoute()->getData(); $view = $this->application->getView(); DataCollection::each($globalVariables, function ($key, $value) use($view) { $view->set($key, $value); }); $config = $this->application->getConfig(); $cache = $this->application->getCache(); $request = $this->request; if ($config->get('app.metaAndTitleFromFile', false)) { $metaFilePath = $config->get('app.metaFile'); $metaPath = $this->appPath . DS . ltrim($metaFilePath, "/"); if (is_readable($metaPath)) { $metaContent = (include $metaPath); $metas = isset($metaContent[$request->getPath()]) ? $metaContent[$request->getPath()] : ''; } else { trigger_error(htmlentities("{$config->get('app.mataFile')} file not found or is not readable"), E_USER_WARNING); } if (!empty($metas)) { if (isset($metas['pageTitle'])) { $view->set('pageTitle', $metas['pageTitle']); unset($metas['pageTitle']); } $view->set('metas', $metas); } } $view->set('domainName', $config->get('app.websiteUrl', '')); $view->set('domain', $config->get('app.domain', '')); if ($cache->exists('csrf-token')) { $view->set('csrfToken', $cache->get('csrf-token')); } elseif (isset($_SESSION['csrf-token'])) { $view->set('csrfToken', $_SESSION['csrf-token']); } }
/** * Caches the current Route * * @param Router $router */ public function cacheRoute(Router $router) { $route = $router->getCurrentRoute(); if (!is_null($route) && !$this->request->isAjax() && $route->isCacheable()) { $this->getCache()->put($this->getCacheKey('route'), $route, $this->getConfig()->get('ttl')); } }