2007-07-19 12:40:28 +02:00
< ? php
/**
2008-01-08 07:37:50 +01:00
* @ package cms
2007-07-19 12:40:28 +02:00
*/
/**
* ErrorPage holds the content for the page of an error response .
2008-01-09 05:18:36 +01:00
* @ package cms
2007-07-19 12:40:28 +02:00
*/
class ErrorPage extends Page {
2007-09-15 01:21:35 +02:00
2007-07-19 12:40:28 +02:00
static $db = array (
" ErrorCode " => " Int " ,
);
2007-09-15 01:21:35 +02:00
2007-07-19 12:40:28 +02:00
static $defaults = array (
" ShowInMenus " => 0 ,
);
/**
* Ensures that there is always a 404 page .
*/
function requireDefaultRecords () {
parent :: requireDefaultRecords ();
2007-08-15 08:38:41 +02:00
2007-07-19 12:40:28 +02:00
if ( ! DataObject :: get_one ( " ErrorPage " , " ErrorCode = '404' " )) {
$errorpage = new ErrorPage ();
$errorpage -> ErrorCode = 404 ;
2008-01-10 04:28:13 +01:00
$errorpage -> Title = _t ( 'ErrorPage.DEFAULTERRORPAGETITLE' , 'Page not found' );
2007-07-19 12:40:28 +02:00
$errorpage -> URLSegment = " page-not-found " ;
$errorpage -> ShowInMenus = false ;
2008-01-10 04:28:13 +01:00
$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>' );
2007-08-20 06:23:51 +02:00
$errorpage -> Status = " New page " ;
2007-07-19 12:40:28 +02:00
$errorpage -> write ();
2007-08-20 06:23:51 +02:00
// Don't publish, as the manifest may not be built yet
// $errorpage->publish("Stage", "Live");
2007-07-19 12:40:28 +02:00
2007-08-24 05:31:14 +02:00
Database :: alteration_message ( " 404 page created " , " created " );
2007-07-19 12:40:28 +02:00
}
}
2007-09-15 01:21:35 +02:00
2007-07-19 12:40:28 +02:00
function getCMSFields () {
$fields = parent :: getCMSFields ();
$fields -> addFieldToTab (
" Root.Content.Main " ,
new DropdownField (
" ErrorCode " ,
2007-10-25 04:47:45 +02:00
_t ( 'ErrorPage.CODE' , " Error code " ),
2007-07-19 12:40:28 +02:00
array (
2008-01-10 04:28:13 +01:00
404 => _t ( 'ErrorPage.404' , '404 - Page not found' ),
500 => _t ( 'ErrorPage.500' , '500 - Server error' )
2007-07-19 12:40:28 +02:00
)
),
" Content "
);
return $fields ;
}
/**
* 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 .
* @ param string | int $fromStage Place to copy from . Can be either a stage name or a version number .
* @ param string $toStage Place to copy to . Must be a stage name .
* @ param boolean $createNewVersion Set this to true to create a new version number . By default , the existing version number will be copied over .
*/
function publish ( $fromStage , $toStage , $createNewVersion = false ) {
// Temporarily log out when producing this page
$loggedInMember = Member :: currentUser ();
Session :: clear ( " loggedInAs " );
2007-07-20 01:15:05 +02:00
$alc_enc = isset ( $_COOKIE [ 'alc_enc' ]) ? $_COOKIE [ 'alc_enc' ] : null ;
Cookie :: set ( 'alc_enc' , null );
2007-08-16 08:35:02 +02:00
$oldStage = Versioned :: current_stage ();
2007-07-19 12:40:28 +02:00
// Run the page
Requirements :: clear ();
$controller = new ErrorPage_Controller ( $this );
2007-08-29 00:27:13 +02:00
$errorContent = $controller -> run ( array () ) -> getBody ();
2007-10-28 09:14:49 +01:00
if ( ! file_exists ( " ../assets " )) {
mkdir ( " ../assets " , 02775 );
}
2007-07-19 12:40:28 +02:00
if ( $fh = fopen ( " ../assets/error- $this->ErrorCode .html " , " w " )) {
2007-10-03 02:10:49 +02:00
fwrite ( $fh , $errorContent );
2007-07-19 12:40:28 +02:00
fclose ( $fh );
}
2007-08-16 08:35:02 +02:00
// Restore the version we're currently connected to.
Versioned :: reading_stage ( $oldStage );
2007-07-19 12:40:28 +02:00
// Log back in
2007-08-16 08:35:02 +02:00
if ( $loggedInMember ) Session :: set ( " loggedInAs " , $loggedInMember -> ID );
if ( isset ( $alc_enc )) Cookie :: set ( 'alc_enc' , $alc_enc );
2007-07-19 12:40:28 +02:00
return $this -> extension_instances [ 'Versioned' ] -> publish ( $fromStage , $toStage , $createNewVersion );
}
}
/**
* Controller for ErrorPages .
2008-01-10 01:34:18 +01:00
* @ package cms
2007-07-19 12:40:28 +02:00
*/
class ErrorPage_Controller extends Page_Controller {
2007-12-02 22:33:59 +01:00
public function init () {
parent :: init ();
Director :: set_status_code ( $this -> failover -> ErrorCode ? $this -> failover -> ErrorCode : 404 );
}
2007-07-19 12:40:28 +02:00
}
2007-09-15 01:21:35 +02:00
2007-12-02 22:24:12 +01:00
?>