1 <?php
2 3 4 5 6 7 8 9 10 11 12 13
14 class ErrorPage extends Page {
15
16 static $db = array(
17 "ErrorCode" => "Int",
18 );
19
20 static $defaults = array(
21 "ShowInMenus" => 0,
22 "ShowInSearch" => 0
23 );
24
25 protected static $static_filepath = ASSETS_PATH;
26
27 28 29 30 31 32
33 public static function response_for($statusCode) {
34
35 if($errorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = $statusCode")) {
36 return ModelAsController::controller_for($errorPage)->handleRequest(new SS_HTTPRequest('GET', ''));
37 }
38
39
40 $cachedPath = self::get_filepath_for_errorcode($statusCode, i18n::get_locale());
41
42 if(file_exists($cachedPath)) {
43 $response = new SS_HTTPResponse();
44
45 $response->setStatusCode($statusCode);
46 $response->setBody(file_get_contents($cachedPath));
47
48 return $response;
49 }
50 }
51
52 53 54 55 56 57
58 function requireDefaultRecords() {
59 parent::requireDefaultRecords();
60
61 $errorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '404'");
62 if(!($errorPage && $errorPage->exists())) {
63 $errorpage = new ErrorPage();
64 $errorpage->ErrorCode = 404;
65 $errorpage->Title = _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found');
66 $errorpage->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>');
67 $errorpage->Status = 'New page';
68 $errorpage->write();
69 $errorpage->publish('Stage', 'Live');
70
71 DB::alteration_message('404 page created', 'created');
72 }
73 }
74
75
76 function getCMSFields() {
77 $fields = parent::getCMSFields();
78
79 $fields->addFieldToTab(
80 "Root.Content.Main",
81 new DropdownField(
82 "ErrorCode",
83 $this->fieldLabel('ErrorCode'),
84 array(
85 400 => _t('ErrorPage.400', '400 - Bad Request'),
86 401 => _t('ErrorPage.401', '401 - Unauthorized'),
87 403 => _t('ErrorPage.403', '403 - Forbidden'),
88 404 => _t('ErrorPage.404', '404 - Not Found'),
89 405 => _t('ErrorPage.405', '405 - Method Not Allowed'),
90 406 => _t('ErrorPage.406', '406 - Not Acceptable'),
91 407 => _t('ErrorPage.407', '407 - Proxy Authentication Required'),
92 408 => _t('ErrorPage.408', '408 - Request Timeout'),
93 409 => _t('ErrorPage.409', '409 - Conflict'),
94 410 => _t('ErrorPage.410', '410 - Gone'),
95 411 => _t('ErrorPage.411', '411 - Length Required'),
96 412 => _t('ErrorPage.412', '412 - Precondition Failed'),
97 413 => _t('ErrorPage.413', '413 - Request Entity Too Large'),
98 414 => _t('ErrorPage.414', '414 - Request-URI Too Long'),
99 415 => _t('ErrorPage.415', '415 - Unsupported Media Type'),
100 416 => _t('ErrorPage.416', '416 - Request Range Not Satisfiable'),
101 417 => _t('ErrorPage.417', '417 - Expectation Failed'),
102 500 => _t('ErrorPage.500', '500 - Internal Server Error'),
103 501 => _t('ErrorPage.501', '501 - Not Implemented'),
104 502 => _t('ErrorPage.502', '502 - Bad Gateway'),
105 503 => _t('ErrorPage.503', '503 - Service Unavailable'),
106 504 => _t('ErrorPage.504', '504 - Gateway Timeout'),
107 505 => _t('ErrorPage.505', '505 - HTTP Version Not Supported'),
108 )
109 ),
110 "Content"
111 );
112
113 return $fields;
114 }
115
116 117 118 119 120 121 122 123
124 function doPublish() {
125 parent::doPublish();
126
127
128 $response = Director::test(Director::makeRelative($this->Link()));
129
130 $errorContent = $response->getBody();
131
132 $errorContent = preg_replace('/<base[^>]+href="' . str_replace('/','\\/', Director::absoluteBaseURL()) . '"[^>]*>/i', '<base href="$BaseURL" />', $errorContent);
133
134
135 if(!file_exists(ASSETS_PATH)) {
136 mkdir(ASSETS_PATH, 02775);
137 }
138
139
140
141
142 $filePath = self::get_filepath_for_errorcode($this->ErrorCode, $this->Locale);
143 if($fh = fopen($filePath, "w")) {
144 fwrite($fh, $errorContent);
145 fclose($fh);
146 } else {
147 $fileErrorText = sprintf(
148 _t(
149 "ErrorPage.ERRORFILEPROBLEM",
150 "Error opening file \"%s\" for writing. Please check file permissions."
151 ),
152 $errorFile
153 );
154 FormResponse::status_message($fileErrorText, 'bad');
155 FormResponse::respond();
156 return;
157 }
158 }
159
160 161 162 163 164
165 function fieldLabels($includerelations = true) {
166 $labels = parent::fieldLabels($includerelations);
167 $labels['ErrorCode'] = _t('ErrorPage.CODE', "Error code");
168
169 return $labels;
170 }
171
172 173 174 175 176 177 178 179
180 static function get_filepath_for_errorcode($statusCode, $locale = null) {
181 if (singleton('ErrorPage')->hasMethod('alternateFilepathForErrorcode')) {
182 return singleton('ErrorPage')-> alternateFilepathForErrorcode($statusCode, $locale);
183 }
184 if(singleton('SiteTree')->hasExtension('Translatable') && $locale && $locale != Translatable::default_locale()) {
185 return self::$static_filepath . "/error-{$statusCode}-{$locale}.html";
186 } else {
187 return self::$static_filepath . "/error-{$statusCode}.html";
188 }
189 }
190
191 192 193 194 195 196
197 static function set_static_filepath($path) {
198 self::$static_filepath = $path;
199 }
200
201 202 203
204 static function get_static_filepath() {
205 return self::$static_filepath;
206 }
207 }
208
209 210 211 212
213 class ErrorPage_Controller extends Page_Controller {
214 function init() {
215 parent::init();
216
217 $action = $this->request->param('Action');
218 if(!$action || $action == 'index') {
219 Director::set_status_code($this->failover->ErrorCode ? $this->failover->ErrorCode : 404);
220 }
221
222 }
223 }
224
225
226 ?>
227
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-