-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.driver.php
391 lines (309 loc) · 12.2 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
class Extension_Page_HTTP_Caching extends Extension {
const FULL_NAME = 'Page HTTP Caching';
const NAME = 'page_http_caching';
const TBL_NAME = 'tbl_page_http_caching';
public function install() {
Symphony::Database()->query(
'CREATE TABLE `' . self::TBL_NAME . '` (
`page_id` INT(11) unsigned NOT NULL,
`caching` VARCHAR(7) NOT NULL,
`intermediary` VARCHAR(7) NOT NULL,
`max_age` INT unsigned default NULL,
PRIMARY KEY (`page_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'
);
Symphony::Configuration()->setArray(array(
self::NAME => array(
'default_caching' => 'off',
'default_intermediary' => 'no',
'default_max_age' => '60'
)
));
Symphony::Configuration()->write();
}
public function uninstall() {
Symphony::Configuration()->remove(self::NAME);
Symphony::Configuration()->write();
Symphony::Database()->query('DROP TABLE `' . self::TBL_NAME . '`');
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
/*array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'validatePreferences'
),*/
array(
'page' => '/blueprints/pages/',
'delegate' => 'AppendPageContent',
'callback' => 'appendPageSettings'
),
array(
'page' => '/blueprints/pages/',
'delegate' => 'PagePostCreate',
'callback' => 'savePageSettings'
),
array(
'page' => '/blueprints/pages/',
'delegate' => 'PagePostEdit',
'callback' => 'savePageSettings'
),
array(
'page' => '/blueprints/pages/',
'delegate' => 'PagePostDelete',
'callback' => 'deletePageSettings'
),
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'addCSS'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendPreRenderHeaders',
'callback' => 'updateHeaders'
)
);
}
public function appendPreferences($context) {
$config = Symphony::Configuration()->get(self::NAME);
// setting checked arrays in order to preselect radio buttons as appropriate
$attr_checked = array('checked' => 'checked');
${checked_caching_.$config['default_caching']} = $attr_checked;
${checked_intermediary_.$config['default_intermediary']} = $attr_checked;
// form elements
$group = new XMLElement('fieldset', null, array('class' => 'settings ' . self::NAME));
$group->appendChild(new XMLElement('legend', __(self::FULL_NAME)));
$group->appendChild(new XMLElement('p', __('Default settings.'), array('class' => 'help')));
$columns = new XMLElement('div', null, array('class' => 'two columns'));
$column = new XMLElement('div', null, array('class' => 'column'));
// default HTTP cache header
$fieldset = new XMLElement('fieldset', null, array('class' => 'inline-options'));
$fieldset->appendChild(new XMLElement('legend', __('HTTP caching')));
$input = Widget::Input('settings['.self::NAME.'][default_caching]', 'off', 'radio', $checked_caching_off);
$label = Widget::Label(null, $input);
$label->setValue(__('Off'), false);
$fieldset->appendChild($label);
$input = Widget::Input('settings['.self::NAME.'][default_caching]', 'on', 'radio', $checked_caching_on);
$label = Widget::Label(null, $input);
$label->setValue(__('On'), false);
$fieldset->appendChild($label);
$column->appendChild($fieldset);
// default intermediary
$fieldset = new XMLElement('fieldset', null, array('class' => 'inline-options'));
$fieldset->appendChild(new XMLElement('legend', __('Allow caching proxies to cache')));
$input = Widget::Input('settings['.self::NAME.'][default_intermediary]', 'no', 'radio', $checked_intermediary_no);
$label = Widget::Label(null, $input);
$label->setValue(__('No'), false);
$fieldset->appendChild($label);
$input = Widget::Input('settings['.self::NAME.'][default_intermediary]', 'yes', 'radio', $checked_intermediary_yes);
$label = Widget::Label(null, $input);
$label->setValue(__('Yes'), false);
$fieldset->appendChild($label);
$column->appendChild($fieldset);
$columns->appendChild($column);
$column = new XMLElement('div', null, array('class' => 'column'));
// default max-age
$input = Widget::Input(
'settings['.self::NAME.'][default_max_age]',
$config['default_max_age'],
'text',
array(
'id' => 'page-http-caching-max-age'
)
);
$label = Widget::Label(__('Maximum age (seconds)'), $input, 'seconds');
$column->appendChild($label);
$columns->appendChild($column);
$group->appendChild($columns);
$context['wrapper']->appendChild($group);
}
public function appendPageSettings($context) {
$page_id = $context['fields']['id'];
$page_settings = $this->getPageSettings($page_id);
// setting checked arrays in order to preselect radio buttons as appropriate
$attr_checked = array('checked' => 'checked');
if (empty($page_settings['caching'])) {
$checked_caching_default = $attr_checked;
} else {
${checked_caching_.$page_settings['caching']} = $attr_checked;
}
if (empty($page_settings['intermediary'])) {
$checked_intermediary_default = $attr_checked;
} else {
${checked_intermediary_.$page_settings['intermediary']} = $attr_checked;
}
// form elements
$group = new XMLElement('fieldset', null, array('class' => 'settings ' . self::NAME));
$group->appendChild(new XMLElement('legend', __(self::FULL_NAME)));
$columns = new XMLElement('div', null, array('class' => 'two columns'));
$column = new XMLElement('div', null, array('class' => 'column'));
// HTTP cache header
$fieldset = new XMLElement('fieldset', null, array('class' => 'inline-options'));
$fieldset->appendChild(new XMLElement('legend', __('HTTP caching')));
$input = Widget::Input(''.self::NAME.'[caching]', 'default', 'radio', $checked_caching_default);
$label = Widget::Label(null, $input);
$label->setValue(__('Default'), false);
$fieldset->appendChild($label);
$input = Widget::Input(''.self::NAME.'[caching]', 'off', 'radio', $checked_caching_off);
$label = Widget::Label(null, $input);
$label->setValue(__('Off'), false);
$fieldset->appendChild($label);
$input = Widget::Input(''.self::NAME.'[caching]', 'on', 'radio', $checked_caching_on);
$label = Widget::Label(null, $input);
$label->setValue(__('On'), false);
$fieldset->appendChild($label);
$column->appendChild($fieldset);
// intermediary
$fieldset = new XMLElement('fieldset', null, array('class' => 'inline-options'));
$fieldset->appendChild(new XMLElement('legend', __('Allow caching proxies to cache')));
$input = Widget::Input(''.self::NAME.'[intermediary]', 'default', 'radio', $checked_intermediary_default);
$label = Widget::Label(null, $input);
$label->setValue(__('Default'), false);
$fieldset->appendChild($label);
$input = Widget::Input(''.self::NAME.'[intermediary]', 'no', 'radio', $checked_intermediary_no);
$label = Widget::Label(null, $input);
$label->setValue(__('No'), false);
$fieldset->appendChild($label);
$input = Widget::Input(''.self::NAME.'[intermediary]', 'yes', 'radio', $checked_intermediary_yes);
$label = Widget::Label(null, $input);
$label->setValue(__('Yes'), false);
$fieldset->appendChild($label);
$column->appendChild($fieldset);
$columns->appendChild($column);
$column = new XMLElement('div', null, array('class' => 'column'));
// max-age
$input = Widget::Input(
self::NAME.'[max_age]',
$page_settings['max_age'],
'text',
array(
'id' => 'page-http-caching-max-age',
'placeholder' => __('Leave empty to use default')
)
);
$label = Widget::Label(__('Maximum age (seconds)'), $input);
$column->appendChild($label);
$columns->appendChild($column);
$group->appendChild($columns);
$context['form']->appendChild($group);
}
private function validatePreferences($context) {
// @TODO: validate preferences function
/*
$preferences = $context['settings'][self::NAME];
if ( // does this do what's intended?
!is_null($preferences['default_caching']) &&
!is_null($preferences['intermediary']) &&
!is_null($preferences['max_age'])
){
return false; // return specific error message?
}
if (
($preferences['default_caching'] != 'off') &&
($preferences['default_caching'] != 'on')
){
return false; // return specific error message?
}
if (
($preferences['default_intermediary'] != 'no') &&
($preferences['default_intermediary'] != 'yes')
){
return false; // return specific error message?
}
if (!ctype_digit($preferences['default_max_age'])){
return false; // return specific error message?
}
return true;
*/
}
public function savePageSettings($context) {
$settings_in = $_POST[self::NAME];
$settings_out = array(
'page_id' => $context['page_id'],
'caching' => $settings_in['caching'],
'intermediary' => $settings_in['intermediary'],
'max_age' => $settings_in['max_age']
);
Symphony::Database()->insert($settings_out, self::TBL_NAME, true);
}
public function deletePageSettings($context) {
foreach ($context['page_ids'] as $page_id) {
Symphony::Database()->delete(self::TBL_NAME, sprintf("`page_id` = %d", $page_id));
}
}
private function getPageSettings($page_id) {
$result = Symphony::Database()->fetch(
sprintf(
'SELECT * FROM ' . self::TBL_NAME . ' WHERE `page_id` = %d',
$page_id
)
);
return $result[0];
}
public function addCSS($context) {
Administration::instance()->Page->addStylesheetToHead(URL.'/extensions/'.self::NAME.'/assets/style.css');
}
public function updateHeaders() {
// Don’t override/set HTTP caching headers if Symphony user is logged in
if (Frontend::instance()->isLoggedIn()) {
return false;
}
$page_params = Frontend::instance()->Page()->Params();
// search for page settings
//$page_data = Frontend::Page()->pageData();
//$page_settings = $this->getPageSettings($page_data['id']);
$page_settings = $this->getPageSettings($page_params['current-page-id']);
$config = Symphony::Configuration()->get(self::NAME);
// @TODO: validate preferences
// if page HTTP caching is not desired
if (($page_settings['caching'] != 'on' && $config['default_caching'] != 'on') ||
($page_settings['caching'] == 'off')) {
return false;
}
// page HTTP caching is desired
$page_http_caching = array();
if (!empty($page_settings['max_age'])) {
$page_http_caching['max_age'] = $page_settings['max_age'];
} elseif (!empty($config['default_max_age'])) {
$page_http_caching['max_age'] = $config['default_max_age'];
} else {
return false;
}
if ($page_settings['intermediary'] == 'no') {
$page_http_caching['intermediary'] = 'private';
} elseif ($page_settings['intermediary'] == 'yes') {
$page_http_caching['intermediary'] = 'public';
} elseif ($config['default_intermediary'] == 'yes') {
$page_http_caching['intermediary'] = 'public';
} else {
$page_http_caching['intermediary'] = 'private';
}
// override/remove unwanted headers
if (version_compare($page_params['symphony-version'], '2.5.0', '<')) {
// Set the unwanted header values to be blank.
Frontend::Page()->addHeaderToPage('Expires', '');
Frontend::Page()->addHeaderToPage('Last-Modified', '');
Frontend::Page()->addHeaderToPage('Pragma', '');
} else {
// Completely remove headers with new removeHeaderFromPage method.
// Even though Symphony 2.3.2 added the removeHeaderFromPage method, it wasn’t effective
// due to PHP’s session_cache_limiter defaults. Symphony 2.5.0 solves this with
// session_cache_limiter('').
Frontend::Page()->removeHeaderFromPage('Expires');
Frontend::Page()->removeHeaderFromPage('Last-Modified');
Frontend::Page()->removeHeaderFromPage('Pragma');
}
// add HTTP cache header
Frontend::Page()->addHeaderToPage(
'Cache-Control',
$page_http_caching['intermediary'] . ', max-age=' . $page_http_caching['max_age']
);
}
}