/** * Dispatches the frontend of a redirect node * @param integer $node Id of the node * @return null */ public function indexAction(Cms $cms, I18n $i18n, $site, $node, $locale = null) { if (!$cms->resolveNode($site, null, $node, RedirectNodeType::NAME)) { return; } if ($locale === null) { $locale = $i18n->getLocale()->getCode(); } else { $i18n->setCurrentLocale($locale); } $path = $this->request->getBasePath(true); if (!$path || $path !== $node->getRoute($locale)) { return $this->chainWebRequest(); } $url = $node->getRedirectUrl($locale); $url = $node->resolveUrl($locale, $this->request->getBaseScript(), $url); if ($url) { $this->response->setRedirect($url); return; } $node = $node->getRedirectNode($locale); if (!$node) { throw new CmsException('No redirect properties set to this node for locale "' . $locale . '".'); } $revision = $site->getRevision(); $site = $site->getId(); if (!$cms->resolveNode($site, $revision, $node)) { return; } $redirectUrl = $this->request->getBaseScript() . $node->getRoute($locale); $this->response->setRedirect($redirectUrl); }
/** * Sets a error view to the response if a status code above 399 is set * @return null */ public function handleHttpError(Event $event, I18n $i18n) { $web = $event->getArgument('web'); $request = $web->getRequest(); $response = $web->getResponse(); $route = $request->getRoute(); if (!$route) { return; } $routeId = $route->getId(); if ($routeId && substr($routeId, 0, 10) !== 'cms.front.') { // don't act on non cms pages return; } $statusCode = $response->getStatusCode(); if ($statusCode != Response::STATUS_CODE_FORBIDDEN && $statusCode != Response::STATUS_CODE_NOT_FOUND || $request->isXmlHttpRequest() && $response->getView() instanceof JsonView) { // js request or not a forbidden or not a not found status return; } $locale = $i18n->getLocale()->getCode(); // lookup site with the current base URL $baseUrl = $request->getBaseUrl(); $site = null; $defaultSite = null; $sites = $this->cms->getSites(); foreach ($sites as $site) { if ($site->getBaseUrl($locale) == $baseUrl) { break; } $defaultSite = $site; $site = null; } if (!$site) { if (!$defaultSite) { return; } $site = $defaultSite; } // get the error node $node = $site->get('error.' . $statusCode); if (!$node) { // no node setup for this error return; } // resolve the locale $locales = $site->getAvailableLocales(); if ($locales != Node::LOCALES_ALL && !isset($locales[$locale])) { $locale = array_pop($locales); $i18n->setCurrentLocale($locale); } // dispatch the error page $routeContainer = $web->getRouter()->getRouteContainer(); $route = $routeContainer->getRouteById('cms.front.' . $site->getId() . '.' . $node . '.' . $locale); if (!$route) { $route = $routeContainer->getRouteById('cms.node.frontend.locale'); $route->setArguments(array('node' => $node, 'locale' => $locale)); } else { $route->setArguments(); } $loginRequest = $web->createRequest($route->getPath()); $loginRequest->setRoute($route); $loginRequest->getHeaders()->setHeader(Header::HEADER_REFERER, $request->getUrl()); if ($request->hasSession()) { $loginRequest->setSession($request->getSession()); } $response->getMessageContainer()->removeAll(); $response->setView(null); $response->setStatusCode(Response::STATUS_CODE_OK); $response->removeHeader(Header::HEADER_CONTENT_TYPE); $response->clearRedirect(); try { $dispatcher = $web->getDispatcher(); $dispatcher->dispatch($loginRequest, $response); } catch (UnauthorizedException $exception) { if ($statusCode === Response::STATUS_CODE_FORBIDDEN) { $response->setRedirect($request->getBaseUrl()); return; } else { throw $exception; } } $response->setStatusCode($statusCode); if ($loginRequest->hasSession()) { $request->setSession($loginRequest->getSession()); } if ($statusCode === Response::STATUS_CODE_FORBIDDEN) { $response->addMessage(new Message($i18n->getTranslator()->translate('error.unauthorized'), Message::TYPE_ERROR)); } }