Skip to content

Commit

Permalink
BUGFIX #6055 ErrorPage should always create static error page files w…
Browse files Browse the repository at this point in the history
…hen dev/build is called if they don't exist (from r111842)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112927 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sam Minnee committed Oct 19, 2010
1 parent 9bed2ca commit 5e0b978
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions core/model/ErrorPage.php
Expand Up @@ -61,27 +61,59 @@ function requireDefaultRecords() {
parent::requireDefaultRecords(); parent::requireDefaultRecords();


$pageNotFoundErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '404'"); $pageNotFoundErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '404'");
if(!($pageNotFoundErrorPage && $pageNotFoundErrorPage->exists())) { $pageNotFoundErrorPageExists = ($pageNotFoundErrorPage && $pageNotFoundErrorPage->exists()) ? true : false;
$pageNotFoundErrorPage = new ErrorPage(); $pageNotFoundErrorPagePath = self::get_filepath_for_errorcode(404);
$pageNotFoundErrorPage->ErrorCode = 404; if(!($pageNotFoundErrorPageExists && file_exists($pageNotFoundErrorPagePath))) {
$pageNotFoundErrorPage->Title = _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'); if(!$pageNotFoundErrorPageExists) {
$pageNotFoundErrorPage->Content = _t('ErrorPage.DEFAULTERRORPAGECONTENT', '<p>Sorry, it seems you were trying to access a page that doesn\'t exist.</p><p>Please check the spelling of the URL you were trying to access and try again.</p>'); $pageNotFoundErrorPage = new ErrorPage();
$pageNotFoundErrorPage->write(); $pageNotFoundErrorPage->ErrorCode = 404;
$pageNotFoundErrorPage->publish('Stage', 'Live'); $pageNotFoundErrorPage->Title = _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found');

$pageNotFoundErrorPage->Content = _t('ErrorPage.DEFAULTERRORPAGECONTENT', '<p>Sorry, it seems you were trying to access a page that doesn\'t exist.</p><p>Please check the spelling of the URL you were trying to access and try again.</p>');
DB::alteration_message('404 page created', 'created'); $pageNotFoundErrorPage->Status = 'New page';
$pageNotFoundErrorPage->write();
$pageNotFoundErrorPage->publish('Stage', 'Live');
}

// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($pageNotFoundErrorPage->Link()));
if($fh = fopen($pageNotFoundErrorPagePath, 'w')) {
$written = fwrite($fh, $response->getBody());
fclose($fh);
}

if($written) {
DB::alteration_message('404 error page created', 'created');
} else {
DB::alteration_message(sprintf('404 error page could not be created at %s. Please check permissions', $pageNotFoundErrorPagePath), 'error');
}
} }


$serverErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '500'"); $serverErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '500'");
if(!($serverErrorPage && $serverErrorPage->exists())) { $serverErrorPageExists = ($serverErrorPage && $serverErrorPage->exists()) ? true : false;
$serverErrorPage = new ErrorPage(); $serverErrorPagePath = self::get_filepath_for_errorcode(500);
$serverErrorPage->ErrorCode = 500; if(!($serverErrorPageExists && file_exists($serverErrorPagePath))) {
$serverErrorPage->Title = _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'); if(!$serverErrorPageExists) {
$serverErrorPage->Content = _t('ErrorPage.DEFAULTSERVERERRORPAGECONTENT', '<p>Sorry, there was a problem with handling your request.</p>'); $serverErrorPage = new ErrorPage();
$serverErrorPage->write(); $serverErrorPage->ErrorCode = 500;
$serverErrorPage->publish('Stage', 'Live'); $serverErrorPage->Title = _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error');

$serverErrorPage->Content = _t('ErrorPage.DEFAULTSERVERERRORPAGECONTENT', '<p>Sorry, there was a problem with handling your request.</p>');
DB::alteration_message('500 page created', 'created'); $serverErrorPage->Status = 'New page';
$serverErrorPage->write();
$serverErrorPage->publish('Stage', 'Live');
}

// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($serverErrorPage->Link()));
if($fh = fopen($serverErrorPagePath, 'w')) {
$written = fwrite($fh, $response->getBody());
fclose($fh);
}

if($written) {
DB::alteration_message('500 error page created', 'created');
} else {
DB::alteration_message(sprintf('500 error page could not be created at %s. Please check permissions', $serverErrorPagePath), 'error');
}
} }
} }


Expand Down

0 comments on commit 5e0b978

Please sign in to comment.