/
UnderConstruction.php
130 lines (116 loc) · 4.38 KB
/
UnderConstruction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Decorator to essentially create a static under construction {@link ErrorPage} in the assets folder
* with the aid of requireDefaultRecords().
*
* @author Frank Mullenger <frankmullenger@gmail.com>
* @package underconstruction
*/
class UnderConstruction_Decorator extends DataObjectDecorator {
/**
* Create an {@link ErrorPage} for status code 503
*
* @see UnderConstruction_Extension::onBeforeInit()
* @see DataObjectDecorator::requireDefaultRecords()
* @return Void
*/
function requireDefaultRecords() {
// Ensure that an assets path exists before we do any error page creation
if(!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH);
}
$pageUnderConstructionErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '503'");
$pageUnderConstructionErrorPageExists = ($pageUnderConstructionErrorPage && $pageUnderConstructionErrorPage->exists()) ? true : false;
$pageUnderConstructionErrorPagePath = ErrorPage::get_filepath_for_errorcode(503);
if(!($pageUnderConstructionErrorPageExists && file_exists($pageUnderConstructionErrorPagePath))) {
if(!$pageUnderConstructionErrorPageExists) {
$pageUnderConstructionErrorPage = new ErrorPage();
$pageUnderConstructionErrorPage->ErrorCode = 503;
$pageUnderConstructionErrorPage->Title = _t('UnderConstruction.TITLE', 'Under Construction');
$pageUnderConstructionErrorPage->Content = _t('UnderConstruction.CONTENT', '<p>Sorry, this site is currently under construction.</p>');
$pageUnderConstructionErrorPage->Status = 'New page';
$pageUnderConstructionErrorPage->write();
$pageUnderConstructionErrorPage->publish('Stage', 'Live');
}
// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($pageUnderConstructionErrorPage->Link()));
if($fh = fopen($pageUnderConstructionErrorPagePath, 'w')) {
$written = fwrite($fh, $response->getBody());
fclose($fh);
}
if($written) {
DB::alteration_message('503 error page created', 'created');
} else {
DB::alteration_message(sprintf('503 error page could not be created at %s. Please check permissions', $pageUnderConstructionErrorPagePath), 'error');
}
}
}
}
/**
* Extension to display an {@link ErrorPage} if necessary.
*
* @author Frank Mullenger <frankmullenger@gmail.com>
* @package underconstruction
*/
class UnderConstruction_Extension extends Extension {
/**
* If current logged in member is not an admin and not trying to log in to the admin
* or run a /dev/build then display an {@link ErrorPage}.
*
* @see UnderConstruction_Decorator::requireDefaultRecords()
* @return Void
*/
public function onBeforeInit() {
$siteConfig = SiteConfig::current_site_config();
$siteUnderConstruction = $siteConfig->UnderConstruction;
if ($siteUnderConstruction) {
//Check to see if running /dev/build
$runningDevBuild = $this->owner && $this->owner->data() instanceof ErrorPage;
if (!Permission::check('ADMIN')
&& strpos($_SERVER['REQUEST_URI'], '/admin') === false
&& strpos($_SERVER['REQUEST_URI'], '/Security') === false
&& !Director::isDev()
&& !$runningDevBuild) {
Debug::friendlyError(503);
exit;
}
}
}
}
/**
* Decorator to add settings to config to make it easier to make the site live and
* turn off any 'Under Construction' pages.
*
* @author Frank Mullenger <frankmullenger@gmail.com>
* @package underconstruction
*/
class UnderConstruction_Settings extends DataObjectDecorator {
/**
* Add database field for flag to either display or hide under construction pages.
*
* @see DataObjectDecorator::extraStatics()
*/
function extraStatics() {
return array(
'db' => array(
'UnderConstruction' => 'Boolean'
)
);
}
/**
* Adding field to allow CMS users to turn off under construction pages.
*
* @see DataObjectDecorator::updateCMSFields()
*/
function updateCMSFields(FieldSet &$fields) {
$fields->addFieldToTab('Root.Access', new HeaderField(
'UnderConstructionHeading',
_t('UnderConstruction.SETTINGSHEADING', 'Is this site under construction?'),
2
));
$fields->addFieldToTab('Root.Access', new CheckboxField(
'UnderConstruction',
_t('UnderConstruction.SETTINGSCHECKBOXLABEL', ' Display an under construction page?')
));
}
}