Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • BaseObjectCategory
  • BookingAdminPage
  • BookingPage
  • ErrorPage
  • ErrorPage_Controller
  • MediawebPage
  • Notifications
  • Page
  • Room
  • RoomCatalog
  • SiteConfig
  • SiteTree
  • SubsitesSelectorPage
  • SubsitesVirtualPage
  • SubsitesVirtualPage_Controller
  • VideoBankPage
  • VirtualPage
  • VirtualPage_Controller
  • VirtualProduct_Controller

Interfaces

  • HiddenClass
  1 <?php
  2 /**
  3  * ErrorPage holds the content for the page of an error response.
  4  * Renders the page on each publish action into a static HTML file
  5  * within the assets directory, after the naming convention
  6  * /assets/error-<statuscode>.html.
  7  * This enables us to show errors even if PHP experiences a recoverable error.
  8  * ErrorPages
  9  * 
 10  * @see Debug::friendlyError()
 11  * 
 12  * @package cms
 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      * Get a {@link SS_HTTPResponse} to response to a HTTP error code if an {@link ErrorPage} for that code is present.
 29      *
 30      * @param int $statusCode
 31      * @return SS_HTTPResponse
 32      */
 33     public static function response_for($statusCode) {
 34         // first attempt to dynamically generate the error page
 35         if($errorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = $statusCode")) {
 36             return ModelAsController::controller_for($errorPage)->handleRequest(new SS_HTTPRequest('GET', ''));
 37         }
 38         
 39         // then fall back on a cached version
 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      * Ensures that there is always a 404 page
 54      * by checking if there's an instance of
 55      * ErrorPage with a 404 error code. If there
 56      * is not, one is created when the DB is built.
 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      * When an error page is published, create a static HTML page with its
118      * content, so the page can be shown even when SilverStripe is not
119      * functioning correctly before publishing this page normally.
120      * @param string|int $fromStage Place to copy from. Can be either a stage name or a version number.
121      * @param string $toStage Place to copy to. Must be a stage name.
122      * @param boolean $createNewVersion Set this to true to create a new version number.  By default, the existing version number will be copied over.
123      */
124     function doPublish() {
125         parent::doPublish();
126 
127         // Run the page
128         $response = Director::test(Director::makeRelative($this->Link()));
129 
130         $errorContent = $response->getBody();
131         // Make the base tag dynamic.
132         $errorContent = preg_replace('/<base[^>]+href="' . str_replace('/','\\/', Director::absoluteBaseURL()) . '"[^>]*>/i', '<base href="$BaseURL" />', $errorContent);
133         
134         // Check we have an assets base directory, creating if it we don't
135         if(!file_exists(ASSETS_PATH)) {
136             mkdir(ASSETS_PATH, 02775);
137         }
138 
139 
140         // if the page is published in a language other than default language,
141         // write a specific language version of the HTML page
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      * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
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      * Returns an absolute filesystem path to a static error file
174      * which is generated through {@link publish()}.
175      * 
176      * @param int $statusCode A HTTP Statuscode, mostly 404 or 500
177      * @param String $locale A locale, e.g. 'de_DE' (Optional)
178      * @return String
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      * Set the path where static error files are saved through {@link publish()}.
193      * Defaults to /assets.
194      * 
195      * @param string $path
196      */
197     static function set_static_filepath($path) {
198         self::$static_filepath = $path;
199     }
200     
201     /**
202      * @return string
203      */
204     static function get_static_filepath() {
205         return self::$static_filepath;
206     }
207 }
208 
209 /**
210  * Controller for ErrorPages.
211  * @package cms
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. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0