/**
  * Create default Utility Page setup
  * Ensures that there is always a 503 Utility page by checking if there's an
  * instance of ErrorPage with a 503 error code. If there is not,
  * one is created when the DB is built.
  */
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     // Skip creation of default records
     if (!self::config()->create_default_pages) {
         return;
     }
     //Only create a UtilityPage on dev/build if one does not already exist.
     if (!UtilityPage::get()->exists()) {
         $page = UtilityPage::create(array('Title' => _t('MaintenanceMode.TITLE', 'Undergoing Scheduled Maintenance'), 'URLSegment' => _t('MaintenanceMode.URLSEGMENT', 'offline'), 'MenuTitle' => _t('MaintenanceMode.MENUTITLE', 'Utility Page'), 'Content' => _t('MaintenanceMode.CONTENT', '<h1>We&rsquo;ll be back soon!</h1>' . '<p>Sorry for the inconvenience but ' . 'our site is currently down for scheduled maintenance. ' . 'If you need to you can always <a href="mailto:#">contact us</a>, ' . 'otherwise we&rsquo;ll be back online shortly!</p>' . '<p>&mdash; The Team</p>'), 'ParentID' => 0, 'Status' => 'Published'));
         $page->write();
         $page->publish('Stage', 'Live');
         DB::alteration_message('Utility Page created', 'created');
     }
 }
 /**
  * @return SS_HTTPResponse
  */
 public function onBeforeInit()
 {
     $config = SiteConfig::current_site_config();
     // If Maintenance Mode is Off, skip processing
     if (!$config->MaintenanceMode) {
         return;
     }
     // Check if the visitor is Admin OR if they have an allowed IP.
     if (Permission::check('VIEW_SITE_MAINTENANCE_MODE') || Permission::check('ADMIN') || $this->hasAllowedIP()) {
         return;
     }
     // Are we already on the UtilityPage? If so, skip processing.
     if ($this->owner instanceof UtilityPage_Controller) {
         return;
     }
     // Fetch our utility page instance now.
     /**
      * @var Page $utilityPage
      */
     $utilityPage = UtilityPage::get()->first();
     if (!$utilityPage) {
         return;
     }
     // We need a utility page before we can do anything.
     // Are we configured to prevent redirection to the UtilityPage URL?
     if ($utilityPage->config()->DisableRedirect) {
         // Process the request internally to ensure that the URL is maintained
         // (instead of redirecting to the maintenance page's URL) and skip any further processing.
         $controller = ModelAsController::controller_for($utilityPage);
         $response = $controller->handleRequest(new SS_HTTPRequest('GET', ''), DataModel::inst());
         HTTP::add_cache_headers($response);
         $response->output();
         die;
     }
     // Default: Skip any further processing and immediately respond with a redirect to the UtilityPage.
     $response = new SS_HTTPResponse();
     $response->redirect($utilityPage->AbsoluteLink(), 302);
     HTTP::add_cache_headers($response);
     $response->output();
     die;
 }
 /**
  * @throws SS_HTTPResponse_Exception
  * @return SS_HTTPResponse
  */
 public function onBeforeInit()
 {
     if (Permission::check("ADMIN")) {
         return;
     }
     $config = SiteConfig::current_site_config();
     if (!$config->MaintenanceMode) {
         return;
     }
     // Fetch our utility page instance now.
     /** @var Page $utilityPage */
     $utilityPage = UtilityPage::get()->first();
     if (!$utilityPage) {
         return;
     }
     // We need a utility page before we can do anything.
     // See if we're still configured to redirect...
     if (!$utilityPage->config()->DisableRedirect) {
         //If this is not the utility page, do a temporary (302) redirect to it
         if ($this->owner->dataRecord->ClassName != "UtilityPage") {
             return $this->owner->redirect($utilityPage->AbsoluteLink(), 302);
         }
     }
     // No need to execute more than once.
     if ($this->owner instanceof UtilityPage_Controller) {
         return;
     }
     // Additional failsafe, just in case (for some reason) the current controller isn't descended from UtilityPage_Controller.
     if (static::$runOnce) {
         return;
     }
     static::$runOnce = true;
     // Process the request internally to ensure the URL is maintained (instead of redirecting to our maintenance page's URL).
     $controller = ModelAsController::controller_for($utilityPage);
     $response = $controller->handleRequest(new SS_HTTPRequest("GET", "/"), new DataModel());
     throw new SS_HTTPResponse_Exception($response, $response->getStatusCode());
 }
 /**
  * Create default Utility Page setup
  * Ensures that there is always a 503 Utility page by checking if there's an
  * instance of ErrorPage with a 503 error code. If there is not,
  * one is created when the DB is built.
  */
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     // Skip creation of default records
     if (!self::config()->create_default_pages) {
         return;
     }
     // Ensure that an assets path exists before we do any error page creation
     if (!file_exists(ASSETS_PATH)) {
         mkdir(ASSETS_PATH);
     }
     $code = self::$defaults['ErrorCode'];
     $pagePath = self::get_filepath_for_errorcode($code);
     $page = UtilityPage::get()->first();
     $pageExists = $page && $page->exists();
     //Only create a UtilityPage on dev/build if one does not already exist.
     if (!$pageExists || !file_exists($pagePath)) {
         if (!$pageExists) {
             $page = UtilityPage::create(array('Title' => _t('MaintenanceMode.TITLE', 'Undergoing Scheduled Maintenance'), 'URLSegment' => _t('MaintenanceMode.URLSEGMENT', 'offline'), 'MenuTitle' => _t('MaintenanceMode.MENUTITLE', 'Utility Page'), 'Content' => _t('MaintenanceMode.CONTENT', '<h1>We&rsquo;ll be back soon!</h1>' . '<p>Sorry for the inconvenience but ' . 'our site is currently down for scheduled maintenance. ' . 'If you need to you can always <a href="mailto:#">contact us</a>, ' . 'otherwise we&rsquo;ll be back online shortly!</p>' . '<p>&mdash; The Team</p>'), 'ParentID' => 0, 'Status' => 'Published'));
             $page->write();
             $page->publish('Stage', 'Live');
         }
         // Ensure a static error page is created from latest Utility Page content
         $response = Director::test(Director::makeRelative($page->Link()));
         $written = null;
         if ($fh = fopen($pagePath, 'w')) {
             $written = fwrite($fh, $response->getBody());
             fclose($fh);
         }
         if ($written) {
             DB::alteration_message(sprintf('%s error Utility Page created', $code), 'created');
         } else {
             DB::alteration_message(sprintf('%s error Utility page could not be created at %s. Please check permissions', $code, $pagePath), 'error');
         }
     }
 }