diff --git a/_config/extensions.yml b/_config/extensions.yml index 1276494..15487c4 100644 --- a/_config/extensions.yml +++ b/_config/extensions.yml @@ -4,6 +4,9 @@ Name: extensions CMSMain: extensions: ['MultisitesCMSMainExtension'] +Controller: + extensions: + ['MultisitesControllerExtension'] ContentController: extensions: ['MultisitesContentControllerExtension'] diff --git a/code/controllers/MultisitesFrontController.php b/code/controllers/MultisitesFrontController.php index 260ee3a..23ed748 100644 --- a/code/controllers/MultisitesFrontController.php +++ b/code/controllers/MultisitesFrontController.php @@ -15,7 +15,7 @@ public function getNestedController() { $site = Multisites::inst()->getCurrentSiteId(); if(!$site) { - return $this->getNotFoundResponse(); + return $this->httpError(404); } if(class_exists('Translatable')) Translatable::disable_locale_filter(); @@ -83,7 +83,7 @@ public function getNestedController() { return $this->response; } - return $this->getNotFoundResponse($site); + return $this->httpError(404); } if(class_exists('Translatable') && $page->Locale) { @@ -93,25 +93,4 @@ public function getNestedController() { return self::controller_for($page, $request->param('Action')); } - - /** - * Finds the current site's ErrorPage of ErrorCode 404, to redirect the user to - * if the requested page is not found - **/ - protected function getNotFoundResponse($siteId = null) { - $page = ErrorPage::get()->filter(array( - 'ErrorCode' => 404, - 'SiteID' => $siteId ?: Multisites::inst()->getDefaultSiteId() - )); - - if($page = $page->first()) { - $controller = ModelAsController::controller_for($page); - $request = new SS_HTTPRequest('GET', ''); - - return $controller->handleRequest($request, $this->model); - } else { - return new SS_HTTPResponse(null, 404); - } - } - } diff --git a/code/controllers/MultisitesRootController.php b/code/controllers/MultisitesRootController.php index dee36ae..bce57c6 100644 --- a/code/controllers/MultisitesRootController.php +++ b/code/controllers/MultisitesRootController.php @@ -12,7 +12,7 @@ public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) $this->init(); if(!$site = Multisites::inst()->getCurrentSiteId()) { - return $this->getNotFoundResponse(); + return $this->httpError(404); } $page = SiteTree::get()->filter(array( @@ -21,7 +21,7 @@ public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) )); if(!$page = $page->first()) { - return $this->getNotFoundResponse($site); + return $this->httpError(404); } $request = new SS_HTTPRequest( @@ -38,22 +38,6 @@ public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) $this->popCurrent(); return $response; } - - protected function getNotFoundResponse($siteId = null) { - $page = ErrorPage::get()->filter(array( - 'ErrorCode' => 404, - 'SiteID' => $siteId ?: Multisites::inst()->getDefaultSiteId() - )); - - if($page = $page->first()) { - $controller = ModelAsController::controller_for($page); - $request = new SS_HTTPRequest('GET', ''); - - return $controller->handleRequest($request, $this->model); - } else { - return new SS_HTTPResponse(null, 404); - } - } /** * The the (relative) homepage link. diff --git a/code/controllers/RobotsTxtController.php b/code/controllers/RobotsTxtController.php index 4dd9aef..b80e6b7 100644 --- a/code/controllers/RobotsTxtController.php +++ b/code/controllers/RobotsTxtController.php @@ -19,7 +19,7 @@ public function index() { $site = Multisites::inst()->getCurrentSiteId(); if(!$site) { - return $this->getNotFoundResponse(); + return $this->httpError(404); } $page = Site::get()->filter(array( @@ -29,7 +29,7 @@ public function index() { $page = $page->first(); if(!$page) { - return $this->getNotFoundResponse(); + return $this->httpError(404); } /* @@ -41,30 +41,11 @@ public function index() { $text = trim($page->RobotsTxt); if(empty($text)) { - return $this->getNotFoundResponse(); + return $this->httpError(404); } $this->getResponse()->addHeader('Content-Type', 'text/plain; charset="utf-8"'); return $text; } - /** - * Finds the current site's ErrorPage of ErrorCode 404, to redirect the user to - * if the requested page is not found. - **/ - protected function getNotFoundResponse($siteId = null) { - $page = ErrorPage::get()->filter(array( - 'ErrorCode' => 404, - 'SiteID' => $siteId ?: Multisites::inst()->getDefaultSiteId() - )); - - if($page = $page->first()) { - $controller = ModelAsController::controller_for($page); - $request = new SS_HTTPRequest('GET', ''); - - return $controller->handleRequest($request, $this->model); - } else { - return new SS_HTTPResponse(null, 404); - } - } } \ No newline at end of file diff --git a/code/extensions/MultisitesControllerExtension.php b/code/extensions/MultisitesControllerExtension.php new file mode 100644 index 0000000..13a8fae --- /dev/null +++ b/code/extensions/MultisitesControllerExtension.php @@ -0,0 +1,29 @@ + + */ + +class MultisitesControllerExtension extends Extension { + + /** + * Retrieve the correct error page for the current multisite instance. + * @param integer + * @param SS_HTTPRequest + * @throws SS_HTTPResponse_Exception + */ + public function onBeforeHTTPError($code, $request) { + + $errorPage = ErrorPage::get()->filter(array( + 'ErrorCode' => $code, + 'SiteID' => Multisites::inst()->getCurrentSiteId() + ))->first(); + if($errorPage) { + Requirements::clear(); + Requirements::clear_combined_files(); + $response = ModelAsController::controller_for($errorPage)->handleRequest($request, DataModel::inst()); + throw new SS_HTTPResponse_Exception($response, $code); + } + } + +} diff --git a/code/extensions/MultisitesErrorPageExtension.php b/code/extensions/MultisitesErrorPageExtension.php index 9266be9..1e9a742 100644 --- a/code/extensions/MultisitesErrorPageExtension.php +++ b/code/extensions/MultisitesErrorPageExtension.php @@ -22,10 +22,5 @@ public function alternateFilepathForErrorcode($code, $locale) { return sprintf("%s/error-%s.html", $path, implode('-', $parts)); } - - public function augmentSQL(\SQLQuery &$query) { - if (Multisites::inst()->getCurrentSiteId()) { - $query->addWhere('"SiteID" = ' . Multisites::inst()->getCurrentSiteId()); - } - } + }