2011-03-18 04:01:06 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ErrorPage holds the content for the page of an error response.
|
|
|
|
* Renders the page on each publish action into a static HTML file
|
|
|
|
* within the assets directory, after the naming convention
|
|
|
|
* /assets/error-<statuscode>.html.
|
|
|
|
* This enables us to show errors even if PHP experiences a recoverable error.
|
|
|
|
* ErrorPages
|
2015-08-14 02:39:45 +02:00
|
|
|
*
|
2011-03-18 04:01:06 +01:00
|
|
|
* @see Debug::friendlyError()
|
2015-08-14 02:39:45 +02:00
|
|
|
*
|
2011-03-18 04:01:06 +01:00
|
|
|
* @package cms
|
|
|
|
*/
|
|
|
|
class ErrorPage extends Page {
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $db = array(
|
2011-03-18 04:01:06 +01:00
|
|
|
"ErrorCode" => "Int",
|
|
|
|
);
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $defaults = array(
|
2011-03-18 04:01:06 +01:00
|
|
|
"ShowInMenus" => 0,
|
|
|
|
"ShowInSearch" => 0
|
|
|
|
);
|
2013-01-30 13:07:11 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $allowed_children = array();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $description = 'Custom content for different error cases (e.g. "Page not found")';
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2013-04-08 09:25:31 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*/
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $static_filepath = ASSETS_PATH;
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2013-04-08 09:25:31 +02:00
|
|
|
/**
|
|
|
|
* @param $member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canAddChildren($member = null) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
2013-04-08 09:25:31 +02:00
|
|
|
* Get a {@link SS_HTTPResponse} to response to a HTTP error code if an
|
2015-08-14 02:39:45 +02:00
|
|
|
* {@link ErrorPage} for that code is present. First tries to serve it
|
2013-11-18 04:32:15 +01:00
|
|
|
* through the standard SilverStripe request method. Falls back to a static
|
|
|
|
* file generated when the user hit's save and publish in the CMS
|
2011-03-18 04:01:06 +01:00
|
|
|
*
|
|
|
|
* @param int $statusCode
|
2013-04-08 09:25:31 +02:00
|
|
|
*
|
2011-03-18 04:01:06 +01:00
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2013-04-08 09:25:31 +02:00
|
|
|
public static function response_for($statusCode) {
|
2011-03-18 04:01:06 +01:00
|
|
|
// first attempt to dynamically generate the error page
|
2013-11-18 04:32:15 +01:00
|
|
|
$errorPage = ErrorPage::get()->filter(array(
|
|
|
|
"ErrorCode" => $statusCode
|
2015-08-14 02:39:45 +02:00
|
|
|
))->first();
|
2013-11-18 04:32:15 +01:00
|
|
|
|
|
|
|
if($errorPage) {
|
2013-02-05 23:34:45 +01:00
|
|
|
Requirements::clear();
|
|
|
|
Requirements::clear_combined_files();
|
2013-04-08 09:25:31 +02:00
|
|
|
|
2013-11-18 04:32:15 +01:00
|
|
|
return ModelAsController::controller_for($errorPage)->handleRequest(
|
|
|
|
new SS_HTTPRequest('GET', ''), DataModel::inst()
|
|
|
|
);
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
// then fall back on a cached version
|
2011-03-22 09:30:10 +01:00
|
|
|
$cachedPath = self::get_filepath_for_errorcode(
|
2015-08-14 02:39:45 +02:00
|
|
|
$statusCode,
|
2011-03-22 09:30:10 +01:00
|
|
|
class_exists('Translatable') ? Translatable::get_current_locale() : null
|
|
|
|
);
|
2013-11-18 04:32:15 +01:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
if(file_exists($cachedPath)) {
|
2015-08-14 02:39:45 +02:00
|
|
|
$response = new SS_HTTPResponse();
|
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
$response->setStatusCode($statusCode);
|
2015-08-14 02:39:45 +02:00
|
|
|
$response->setBody(file_get_contents($cachedPath));
|
2013-11-18 04:32:15 +01:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
2013-01-30 13:07:11 +01:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
2013-04-08 09:25:31 +02:00
|
|
|
* Ensures that there is always a 404 page by checking if there's an
|
|
|
|
* instance of ErrorPage with a 404 and 500 error code. If there is not,
|
|
|
|
* one is created when the DB is built.
|
2011-03-18 04:01:06 +01:00
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function requireDefaultRecords() {
|
2011-03-18 04:01:06 +01:00
|
|
|
parent::requireDefaultRecords();
|
|
|
|
|
2013-03-27 12:00:55 +01:00
|
|
|
if ($this->class == 'ErrorPage' && SiteTree::config()->create_default_pages) {
|
2011-04-07 08:40:24 +02:00
|
|
|
// Ensure that an assets path exists before we do any error page creation
|
|
|
|
if(!file_exists(ASSETS_PATH)) {
|
|
|
|
mkdir(ASSETS_PATH);
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
2013-01-29 19:34:05 +01:00
|
|
|
|
2013-01-30 13:07:11 +01:00
|
|
|
$defaultPages = $this->getDefaultRecords();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2013-01-29 19:34:05 +01:00
|
|
|
foreach($defaultPages as $defaultData) {
|
|
|
|
$code = $defaultData['ErrorCode'];
|
|
|
|
$page = DataObject::get_one(
|
2015-08-14 02:39:45 +02:00
|
|
|
'ErrorPage',
|
2013-08-29 03:59:45 +02:00
|
|
|
sprintf("\"ErrorPage\".\"ErrorCode\" = '%s'", $code)
|
2013-01-29 19:34:05 +01:00
|
|
|
);
|
|
|
|
$pageExists = ($page && $page->exists());
|
|
|
|
$pagePath = self::get_filepath_for_errorcode($code);
|
|
|
|
if(!($pageExists && file_exists($pagePath))) {
|
|
|
|
if(!$pageExists) {
|
2013-01-30 13:07:11 +01:00
|
|
|
$page = new ErrorPage($defaultData);
|
2013-01-29 19:34:05 +01:00
|
|
|
$page->write();
|
|
|
|
$page->publish('Stage', 'Live');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a static error page is created from latest error page content
|
|
|
|
$response = Director::test(Director::makeRelative($page->Link()));
|
|
|
|
$written = null;
|
|
|
|
if($fh = fopen($pagePath, 'w')) {
|
|
|
|
$written = fwrite($fh, $response->getBody());
|
|
|
|
fclose($fh);
|
|
|
|
}
|
2011-03-18 04:01:06 +01:00
|
|
|
|
2013-01-29 19:34:05 +01:00
|
|
|
if($written) {
|
|
|
|
DB::alteration_message(
|
|
|
|
sprintf('%s error page created', $code),
|
|
|
|
'created'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
DB::alteration_message(
|
|
|
|
sprintf(
|
2015-08-14 02:39:45 +02:00
|
|
|
'%s error page could not be created at %s. Please check permissions',
|
2013-01-29 19:34:05 +01:00
|
|
|
$code,
|
|
|
|
$pagePath
|
2015-08-14 02:39:45 +02:00
|
|
|
),
|
2013-01-29 19:34:05 +01:00
|
|
|
'error'
|
|
|
|
);
|
|
|
|
}
|
2011-04-07 08:40:24 +02:00
|
|
|
}
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:07:11 +01:00
|
|
|
/**
|
2015-08-14 02:39:45 +02:00
|
|
|
* Returns an array of arrays, each of which defines properties for a new
|
2013-11-18 04:32:15 +01:00
|
|
|
* ErrorPage record.
|
2015-08-14 02:39:45 +02:00
|
|
|
*
|
2013-04-08 09:25:31 +02:00
|
|
|
* @return array
|
2013-01-30 13:07:11 +01:00
|
|
|
*/
|
|
|
|
protected function getDefaultRecords() {
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'ErrorCode' => 404,
|
|
|
|
'Title' => _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'),
|
|
|
|
'Content' => _t(
|
2015-08-14 02:39:45 +02:00
|
|
|
'ErrorPage.DEFAULTERRORPAGECONTENT',
|
2013-01-30 13:07:11 +01:00
|
|
|
'<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>'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'ErrorCode' => 500,
|
|
|
|
'Title' => _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'),
|
|
|
|
'Content' => _t(
|
2015-08-14 02:39:45 +02:00
|
|
|
'ErrorPage.DEFAULTSERVERERRORPAGECONTENT',
|
2013-01-30 13:07:11 +01:00
|
|
|
'<p>Sorry, there was a problem with handling your request.</p>'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2013-11-18 04:32:15 +01:00
|
|
|
|
2013-01-30 13:07:11 +01:00
|
|
|
$this->extend('getDefaultRecords', $data);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2013-04-08 09:25:31 +02:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function getCMSFields() {
|
2012-04-13 15:55:32 +02:00
|
|
|
$fields = parent::getCMSFields();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
$fields->addFieldToTab(
|
2015-08-14 02:39:45 +02:00
|
|
|
"Root.Main",
|
2011-03-18 04:01:06 +01:00
|
|
|
new DropdownField(
|
|
|
|
"ErrorCode",
|
|
|
|
$this->fieldLabel('ErrorCode'),
|
|
|
|
array(
|
|
|
|
400 => _t('ErrorPage.400', '400 - Bad Request'),
|
|
|
|
401 => _t('ErrorPage.401', '401 - Unauthorized'),
|
|
|
|
403 => _t('ErrorPage.403', '403 - Forbidden'),
|
|
|
|
404 => _t('ErrorPage.404', '404 - Not Found'),
|
|
|
|
405 => _t('ErrorPage.405', '405 - Method Not Allowed'),
|
|
|
|
406 => _t('ErrorPage.406', '406 - Not Acceptable'),
|
|
|
|
407 => _t('ErrorPage.407', '407 - Proxy Authentication Required'),
|
|
|
|
408 => _t('ErrorPage.408', '408 - Request Timeout'),
|
|
|
|
409 => _t('ErrorPage.409', '409 - Conflict'),
|
|
|
|
410 => _t('ErrorPage.410', '410 - Gone'),
|
|
|
|
411 => _t('ErrorPage.411', '411 - Length Required'),
|
|
|
|
412 => _t('ErrorPage.412', '412 - Precondition Failed'),
|
|
|
|
413 => _t('ErrorPage.413', '413 - Request Entity Too Large'),
|
|
|
|
414 => _t('ErrorPage.414', '414 - Request-URI Too Long'),
|
|
|
|
415 => _t('ErrorPage.415', '415 - Unsupported Media Type'),
|
|
|
|
416 => _t('ErrorPage.416', '416 - Request Range Not Satisfiable'),
|
|
|
|
417 => _t('ErrorPage.417', '417 - Expectation Failed'),
|
2014-05-01 06:34:30 +02:00
|
|
|
422 => _t('ErrorPage.422', '422 - Unprocessable Entity'),
|
|
|
|
429 => _t('ErrorPage.429', '429 - Too Many Requests'),
|
2011-03-18 04:01:06 +01:00
|
|
|
500 => _t('ErrorPage.500', '500 - Internal Server Error'),
|
|
|
|
501 => _t('ErrorPage.501', '501 - Not Implemented'),
|
|
|
|
502 => _t('ErrorPage.502', '502 - Bad Gateway'),
|
|
|
|
503 => _t('ErrorPage.503', '503 - Service Unavailable'),
|
|
|
|
504 => _t('ErrorPage.504', '504 - Gateway Timeout'),
|
|
|
|
505 => _t('ErrorPage.505', '505 - HTTP Version Not Supported'),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"Content"
|
|
|
|
);
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
|
|
|
* When an error page is published, create a static HTML page with its
|
|
|
|
* content, so the page can be shown even when SilverStripe is not
|
|
|
|
* functioning correctly before publishing this page normally.
|
2013-04-08 09:25:31 +02:00
|
|
|
*
|
2015-10-08 23:38:58 +02:00
|
|
|
* @return bool
|
2011-03-18 04:01:06 +01:00
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function doPublish() {
|
2015-10-08 23:38:58 +02:00
|
|
|
if (!parent::doPublish()) return false;
|
2015-04-21 10:38:45 +02:00
|
|
|
return $this->writeStaticPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write out the published version of the page to the filesystem
|
|
|
|
*
|
|
|
|
* @return mixed Either true, or an error
|
|
|
|
*/
|
|
|
|
public function writeStaticPage() {
|
2011-03-18 04:01:06 +01:00
|
|
|
// Run the page (reset the theme, it might've been disabled by LeftAndMain::init())
|
2013-04-08 09:25:31 +02:00
|
|
|
$oldEnabled = Config::inst()->get('SSViewer', 'theme_enabled');
|
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', true);
|
2011-03-18 04:01:06 +01:00
|
|
|
$response = Director::test(Director::makeRelative($this->Link()));
|
2013-04-08 09:25:31 +02:00
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', $oldEnabled);
|
2011-03-18 04:01:06 +01:00
|
|
|
$errorContent = $response->getBody();
|
2015-04-21 10:38:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
// Check we have an assets base directory, creating if it we don't
|
|
|
|
if(!file_exists(ASSETS_PATH)) {
|
|
|
|
mkdir(ASSETS_PATH, 02775);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the page is published in a language other than default language,
|
|
|
|
// write a specific language version of the HTML page
|
|
|
|
$filePath = self::get_filepath_for_errorcode($this->ErrorCode, $this->Locale);
|
2016-04-07 02:34:55 +02:00
|
|
|
if (file_put_contents($filePath, $errorContent) === false) {
|
2012-05-01 21:43:43 +02:00
|
|
|
$fileErrorText = _t(
|
2015-04-21 10:38:45 +02:00
|
|
|
'ErrorPage.ERRORFILEPROBLEM',
|
|
|
|
'Error opening file "{filename}" for writing. Please check file permissions.',
|
2015-10-08 23:38:58 +02:00
|
|
|
array('filename' => $filePath)
|
2011-03-18 04:01:06 +01:00
|
|
|
);
|
2015-10-08 23:38:58 +02:00
|
|
|
user_error($fileErrorText, E_USER_WARNING);
|
|
|
|
return false;
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
2015-05-15 01:51:23 +02:00
|
|
|
return true;
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
|
|
|
* @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
|
2015-08-14 02:39:45 +02:00
|
|
|
*
|
2013-04-08 09:25:31 +02:00
|
|
|
* @return array
|
2011-03-18 04:01:06 +01:00
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function fieldLabels($includerelations = true) {
|
2011-03-18 04:01:06 +01:00
|
|
|
$labels = parent::fieldLabels($includerelations);
|
|
|
|
$labels['ErrorCode'] = _t('ErrorPage.CODE', "Error code");
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
return $labels;
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
|
|
|
* Returns an absolute filesystem path to a static error file
|
|
|
|
* which is generated through {@link publish()}.
|
2015-08-14 02:39:45 +02:00
|
|
|
*
|
2011-03-18 04:01:06 +01:00
|
|
|
* @param int $statusCode A HTTP Statuscode, mostly 404 or 500
|
2015-09-28 11:31:31 +02:00
|
|
|
* @param string $locale A locale, e.g. 'de_DE' (Optional)
|
2013-04-08 09:25:31 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2011-03-18 04:01:06 +01:00
|
|
|
*/
|
2013-04-08 09:25:31 +02:00
|
|
|
public static function get_filepath_for_errorcode($statusCode, $locale = null) {
|
2011-03-18 04:01:06 +01:00
|
|
|
if (singleton('ErrorPage')->hasMethod('alternateFilepathForErrorcode')) {
|
|
|
|
return singleton('ErrorPage')-> alternateFilepathForErrorcode($statusCode, $locale);
|
|
|
|
}
|
2013-04-08 09:25:31 +02:00
|
|
|
|
2011-03-22 09:30:10 +01:00
|
|
|
if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable') && $locale && $locale != Translatable::default_locale()) {
|
2013-03-18 11:47:15 +01:00
|
|
|
return self::config()->static_filepath . "/error-{$statusCode}-{$locale}.html";
|
2011-03-18 04:01:06 +01:00
|
|
|
} else {
|
2013-03-18 11:47:15 +01:00
|
|
|
return self::config()->static_filepath . "/error-{$statusCode}.html";
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
|
|
|
* Set the path where static error files are saved through {@link publish()}.
|
|
|
|
* Defaults to /assets.
|
2013-03-18 11:47:15 +01:00
|
|
|
*
|
2015-06-19 02:00:42 +02:00
|
|
|
* @deprecated 4.0 Use "ErrorPage.static_file_path" instead
|
2011-03-18 04:01:06 +01:00
|
|
|
* @param string $path
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function set_static_filepath($path) {
|
2015-06-19 02:00:42 +02:00
|
|
|
Deprecation::notice('4.0', 'Use "ErrorPage.static_file_path" instead');
|
2013-03-18 11:47:15 +01:00
|
|
|
self::config()->static_filepath = $path;
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
2015-06-19 02:00:42 +02:00
|
|
|
* @deprecated 4.0 Use "ErrorPage.static_file_path" instead
|
2011-03-18 04:01:06 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function get_static_filepath() {
|
2015-06-19 02:00:42 +02:00
|
|
|
Deprecation::notice('4.0', 'Use "ErrorPage.static_file_path" instead');
|
2013-03-18 11:47:15 +01:00
|
|
|
return self::config()->static_filepath;
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for ErrorPages.
|
2013-04-08 09:25:31 +02:00
|
|
|
*
|
2011-03-18 04:01:06 +01:00
|
|
|
* @package cms
|
|
|
|
*/
|
|
|
|
class ErrorPage_Controller extends Page_Controller {
|
|
|
|
}
|
|
|
|
|