-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathextension.driver.php
executable file
·279 lines (222 loc) · 9.61 KB
/
extension.driver.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
Class extension_maintenance_mode extends Extension
{
public function install()
{
Symphony::Configuration()->set('enabled', 'no', 'maintenance_mode');
return Symphony::Configuration()->write();
}
public function uninstall()
{
Symphony::Configuration()->remove('maintenance_mode');
}
public function getSubscribedDelegates()
{
return array(
array('page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'),
array('page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => '__SavePreferences'),
array('page' => '/system/preferences/',
'delegate' => 'CustomActions',
'callback' => '__toggleMaintenanceMode'),
array('page' => '/backend/',
'delegate' => 'AppendPageAlert',
'callback' => '__appendAlert'),
array('page' => '/blueprints/pages/',
'delegate' => 'AppendPageContent',
'callback' => '__appendType'),
array('page' => '/frontend/',
'delegate' => 'FrontendPrePageResolve',
'callback' => '__checkForMaintenanceMode'),
array('page' => '/frontend/',
'delegate' => 'FrontendParamsResolve',
'callback' => '__addParam')
);
}
/**
* Append maintenance mode preferences
*
* @param array $context
* delegate context
*/
public function appendPreferences($context)
{
// Create preference group
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', __('Maintenance Mode')));
// Append settings
$label = Widget::Label();
$input = Widget::Input('settings[maintenance_mode][enabled]', 'yes', 'checkbox');
if (Symphony::Configuration()->get('enabled', 'maintenance_mode') === 'yes') {
$input->setAttribute('checked', 'checked');
}
$label->setValue($input->generate() . ' ' . __('Enable maintenance mode'));
$group->appendChild($label);
// Append help
$group->appendChild(new XMLElement('p', __('Maintenance mode will redirect all visitors, other than developers, to the specified maintenance page. To specify a maintenance page, give a page a type of <code>maintenance</code>'), array('class' => 'help')));
// IP White list
$label = Widget::Label(__('IP Whitelist'));
$label->appendChild(Widget::Input('settings[maintenance_mode][ip_whitelist]', General::sanitize(Symphony::Configuration()->get('ip_whitelist', 'maintenance_mode'))));
$group->appendChild($label);
// Append help
$group->appendChild(new XMLElement('p', __('Any user that has an IP listed above will be granted access. This eliminates the need to allow a user backend access. Separate each with a space.'), array('class' => 'help')));
// Useragent White list
$label = Widget::Label(__('Useragent Whitelist'));
$whitelist = json_decode(Symphony::Configuration()->get('useragent_whitelist', 'maintenance_mode'));
if (is_array($whitelist) && !empty($whitelist)) {
$useragent = implode("\r\n",$whitelist);
}
$label->appendChild(Widget::Textarea('settings[maintenance_mode][useragent_whitelist]', 5, 50, General::sanitize($useragent)));
$group->appendChild($label);
// Append help
$group->appendChild(new XMLElement('p', __('Any useragent that listed above will be granted access. This eliminates the need to allow a user backend access, useful when third party services need to access your site prior to launch. Insert in json array format eg ["useragent1","useragent2"].'), array('class' => 'help')));
// Append new preference group
$context['wrapper']->appendChild($group);
}
/**
* Save preferences
*
* @param array $context
* delegate context
*/
public function __SavePreferences($context)
{
if ($context['settings']['maintenance_mode']['useragent_whitelist']){
// Convert to a json encoded array
$context['settings']['maintenance_mode']['useragent_whitelist'] = json_encode(explode("\r\n",$context['settings']['maintenance_mode']['useragent_whitelist']));
}
if (!is_array($context['settings'])) {
// Disable maintenance mode by default
$context['settings'] = array('maintenance_mode' => array('enabled' => 'no'));
} elseif (!isset($context['settings']['maintenance_mode']['enabled'])) {
// Disable maintenance mode if it has not been set to 'yes'
$context['settings']['maintenance_mode']['enabled'] = 'no';
}
}
/**
* Toggle maintenance mode and redirect to the previous page, if specified.
*/
public function __toggleMaintenanceMode()
{
if ($_REQUEST['action'] === 'toggle-maintenance-mode') {
// Toggle mode
$value = (Symphony::Configuration()->get('enabled', 'maintenance_mode') === 'no' ? 'yes' : 'no');
Symphony::Configuration()->set('enabled', $value, 'maintenance_mode');
Symphony::Configuration()->write();
// Redirect
redirect((isset($_REQUEST['redirect']) ? SYMPHONY_URL . $_REQUEST['redirect'] : Administration::instance()->getCurrentPageURL() . '/'));
}
}
/**
* Append notice that the site is currently in maintenance mode offering a link
* to switch to live mode if no other alert is set.
*
* @param array $context
* delegate context
*/
public function __appendAlert($context)
{
$author = null;
if (is_callable(array('Symphony', 'Author'))) {
$author = Symphony::Author();
} else {
$author = Administration::instance()->Author;
}
// Site in maintenance mode
if(Symphony::Configuration()->get('enabled', 'maintenance_mode') == 'yes') {
if ($author != null && $author->isDeveloper()) {
Administration::instance()->Page->pageAlert(
__('This site is currently in maintenance mode.') . ' <a href="' . SYMPHONY_URL . '/system/preferences/?action=toggle-maintenance-mode&redirect=' . getCurrentPage() . '">' . __('Restore?') . '</a>',
Alert::NOTICE
);
} else {
Administration::instance()->Page->pageAlert(
__('This site is currently in maintenance mode.'),
Alert::NOTICE
);
}
}
}
/**
* Append type for maintenance pages to page editor.
*
* @param array $context
* delegate context
*/
public function __appendType($context)
{
// Find page types
$elements = $context['form']->getChildren();
$fieldset = $elements[0]->getChildren();
$group = $fieldset[2]->getChildren();
$div = $group[1]->getChildren();
$types = $div[2]->getChildren();
// Search for existing maintenance type
$flag = false;
foreach ($types as $type) {
if ($type->getValue() === 'maintenance') {
$flag = true;
}
}
// Append maintenance type
if ($flag === false) {
$mode = new XMLElement('li', 'maintenance');
$div[2]->appendChild($mode);
}
}
/**
* Redirect to maintenance page, if site is in maintenance and the user is not logged in
*
* @param array $context
* delegate context
*/
public function __checkForMaintenanceMode($context)
{
if (!Symphony::Engine()->isLoggedIn() && Symphony::Configuration()->get('enabled', 'maintenance_mode') === 'yes'){
// Check the IP white list
$whitelist = Symphony::Configuration()->get('ip_whitelist', 'maintenance_mode');
if (strlen(trim($whitelist)) > 0) {
$whitelist = explode(' ', $whitelist);
if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
return;
}
}
// Check if useragent is allowed
$useragent = Symphony::Configuration()->get('useragent_whitelist', 'maintenance_mode');
if (strlen(trim($useragent)) > 0) {
$useragent = json_decode($useragent);
if (in_array($_SERVER['HTTP_USER_AGENT'], $useragent)) {
return;
}
}
// Find custom maintenance page
$row = PageManager::fetchPageByType('maintenance');
if (is_array($row) && isset($row[0])) {
// There's more than a `maintenance` page
$row = $row[0];
}
$context['row'] = $row;
// Default maintenance message
if (empty($context['row'])) {
Symphony::Engine()->throwCustomError(
__('Website Offline'),
__('This site is currently in maintenance. Please check back at a later date.')
);
}
}
}
/**
* Add site mode to parameter pool
*
* @param array $context
* delegate context
*/
public function __addParam($context)
{
$context['params']['site-mode'] = (Symphony::Configuration()->get('enabled', 'maintenance_mode') === 'yes' ? 'maintenance' : 'live');
}
}