forked from drupalprojects/hosting_varnish
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhosting_varnish.module
304 lines (263 loc) · 9.85 KB
/
hosting_varnish.module
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
<?php
/**
* @file
* Integrates hosted sites with Varnish cache.
*/
/**
* Implements hook_perm().
*/
function hosting_varnish_perm() {
return array('purge varnish cache', 'configure default varnish settings');
}
/**
* Implements hook_menu().
*/
function hosting_varnish_menu() {
$items = array();
$items['node/%node/hosting-varnish'] = array(
'title' => 'Varnish cache',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_varnish_node_purge_cache', 1),
'access callback' => 'hosting_varnish_check_valid_site',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
$items['node/%node/hosting-varnish-platform'] = array(
'title' => 'Varnish settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_varnish_platform_settings', 1),
'access callback' => 'hosting_varnish_check_valid_platform',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
$items['admin/hosting/varnish'] = array(
'title' => 'Varnish integration',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_varnish_settings_form'),
'access arguments' => array('administer varnish'),
'type' => MENU_LOCAL_TASK,
'file' => 'hosting_varnish.admin.inc',
);
return $items;
}
/**
* Checks if node is site and on live server (and user has purge permission).
*/
function hosting_varnish_check_valid_site($site) {
if (!hosting_feature('varnish')) {
return FALSE;
}
if (!user_access('purge varnish cache')) {
return FALSE;
}
if ($site->type != 'site') {
return FALSE;
}
$web_servers = array();
if (hosting_feature('server')) {
$web_servers = variable_get('hosting_varnish_web_servers', array());
}
if (empty($web_servers) || ($platform = node_load($site->platform)) && in_array($platform->web_server, $web_servers)) {
return TRUE;
}
return FALSE;
}
function hosting_varnish_check_valid_platform($platform) {
if (!hosting_feature('varnish')) {
return FALSE;
}
if (!user_access('configure default varnish settings')) {
return FALSE;
}
// Only support Drupal 7
if (substr($platform->release->version_code, 0, 1) != '7') {
return FALSE;
}
if ($platform->type != 'platform') {
return FALSE;
}
$web_servers = array();
if (hosting_feature('server')) {
$web_servers = variable_get('hosting_varnish_web_servers', array());
}
if (empty($web_servers) || in_array($platform->web_server, $web_servers)) {
return TRUE;
}
return FALSE;
}
function hosting_varnish_platform_settings($form_state, $platform) {
$form = array();
$nid = $platform->nid;
$form['caching'] = array(
'#type' => 'fieldset',
'#title' => t('Caching'),
);
$active = variable_get('hosting_varnish_active' . '_' . $nid, FALSE);
$form['hosting_varnish_active' . '_' . $nid] = array(
'#type' => 'checkbox',
'#title' => t('Activate automatic settings for sites on this platform'),
'#description' => t('Other settings will be ignored if this is disabled.'),
'#default_value' => $active,
'#weight' => -100,
);
$cache = variable_get('hosting_varnish_cache' . '_' . $nid, TRUE);
$form['caching']['hosting_varnish_cache' . '_' . $nid] = array(
'#type' => 'checkbox',
'#title' => t('Cache pages for anonymous users'),
'#default_value' => $cache,
'#weight' => -2,
);
$block_cache = variable_get('hosting_varnish_block_cache' . '_' . $nid, FALSE);
$form['caching']['hosting_varnish_block_cache' . '_' . $nid] = array(
'#type' => 'checkbox',
'#title' => t('Cache blocks'),
'#default_value' => $block_cache,
);
$period = drupal_map_assoc(array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400), 'format_interval');
$period[0] = '<' . t('none') . '>';
$form['caching']['hosting_varnish_cache_lifetime' . '_' . $nid] = array(
'#type' => 'select',
'#title' => t('Minimum cache lifetime'),
'#default_value' => variable_get('hosting_varnish_cache_lifetime' . '_' . $nid, 0),
'#options' => $period,
'#description' => t('Cached pages will not be re-created until at least this much time has elapsed.')
);
$form['caching']['hosting_varnish_page_cache_maximum_age' . '_' . $nid] = array(
'#type' => 'select',
'#title' => t('Expiration of cached pages'),
'#default_value' => variable_get('hosting_varnish_page_cache_maximum_age' . '_' . $nid, 0),
'#options' => $period,
'#description' => t('The maximum time an external cache can use an old version of a page.')
);
$form['bandwidth_optimization' . '_' . $nid] = array(
'#type' => 'fieldset',
'#title' => t('Bandwidth optimization'),
'#description' => t('External resources can be optimized automatically, which can reduce both the size and number of requests made to your website.'),
);
$form['bandwidth_optimization']['hosting_varnish_page_compression' . '_' . $nid] = array(
'#type' => 'checkbox',
'#title' => t('Compress cached pages.'),
'#default_value' => variable_get('hosting_varnish_page_compression' . '_' . $nid, TRUE),
'#prefix' => '<div id="page-compression-wrapper">',
'#suffix' => '</div>',
);
$form['bandwidth_optimization']['hosting_varnish_preprocess_css' . '_' . $nid] = array(
'#type' => 'checkbox',
'#title' => t('Aggregate and compress CSS files.'),
'#default_value' => variable_get('hosting_varnish_preprocess_css' . '_' . $nid, 0),
);
$form['bandwidth_optimization']['hosting_varnish_preprocess_js' . '_' . $nid] = array(
'#type' => 'checkbox',
'#title' => t('Aggregate JavaScript files.'),
'#default_value' => variable_get('hosting_varnish_preprocess_js' . '_' . $nid, 0),
);
return system_settings_form($form);
}
/**
* Purges cache for a site.
*/
function hosting_varnish_node_purge_cache($form_state, $site) {
$form = array();
if (!hosting_varnish_ready()) {
$form['error']['message'] = array(
'#value' => '<p><em>' . t('One or more Varnish servers are down.') . '</em></p>',
);
if (user_access('administer varnish')) {
$form['error']['varnish_status'] = array(
'#value' => theme('varnish_status', varnish_get_status(), floatval(variable_get('varnish_version', 2.1))),
);
$form['error']['varnish_link'] = array(
'#value' => t('Configure !settings_link.', array('!settings_link' => l(t('Varnish settings'), 'admin/settings/varnish'))),
);
}
}
else {
$names = array($site->title);
if (hosting_feature('alias')) {
$names = array_merge($names, hosting_alias_get_aliases($site));
}
$form['hostname'] = array(
'#type' => 'checkboxes',
'#title' => t('Host names'),
'#options' => array_combine($names, $names),
'#required' => TRUE,
'#description' => t('Select the host names for this site to purge the Varnish cache for.'),
);
$form['url'] = array(
'#type' => 'textfield',
'#title' => t('URL path'),
'#default_value' => '/',
'#description' => t('Purge cache matching this URL. If the path is empty, all cache for the select host names will be purged.'),
);
$form['regexp'] = array(
'#type' => 'fieldset',
'#title' => t('Regular expression'),
);
$form['regexp']['url_regexp'] = array(
'#type' => 'checkbox',
'#title' => t('Regular expression'),
'#default_value' => FALSE,
'#description' => t('Use the specified URL path as a regular expression pattern. %nb', array('%nb' => t('N.B. Remember to use caret (^) and dollar sign ($) characters to signify start and end of string to avoid unnecessarily matching strings containing the pattern.'))),
);
$form['regexp']['url_regexp_ci'] = array(
'#type' => 'checkbox',
'#title' => t('Case-insensitive'),
'#default_value' => FALSE,
'#description' => t('Whether to match case insensitive characters using the pattern.'),
);
$confirm_description = t('Purging the cache for the specified host names and path will cause the next pageview to trigger a Drupal page request. Are you sure you understand and still wish to continue?');
return confirm_form($form, t('Purge Varnish cache'), 'node/' . $site->nid . '/hosting-varnish', $confirm_description, t('Purge'));
}
return $form;
}
/**
* Checks various configuration and determines whether Varnish can correctly respond.
*/
function hosting_varnish_ready() {
$status = varnish_get_status();
return !empty($status) && !in_array(VARNISH_SERVER_STATUS_DOWN, varnish_get_status(), TRUE);
}
/**
* Validates purge form.
*/
function hosting_varnish_node_purge_cache_validate($form, &$form_state) {
// Validate regular expression.
if (!empty($form_state['values']['url_regexp']) && $form_state['values']['url'] !== '') {
$pattern = $form_state['values']['url'];
$pattern_suffix = empty($form_state['values']['url_regexp_ci']) ? '' : 'i';
// Check pattern for the "`" character.
$pattern = str_replace('`', '\`', $pattern);
// Test pattern.
$full_pattern = "`$pattern`$pattern_suffix";
if (FALSE === @preg_match($full_pattern, '')) {
form_set_error('url', t('The specified regular expression pattern is invalid.'));
}
}
}
/**
* Purges cache.
*/
function hosting_varnish_node_purge_cache_submit($form, &$form_state) {
$hostname = $form_state['values']['hostname'];
$url = $form_state['values']['url'];
$url_regexp = $form_state['values']['url_regexp'];
$url_regexp_ci = $form_state['values']['url_regexp_ci'];
// Construct pattern.
$pattern = '';
if ($url === '') {
$pattern = '.?';
}
elseif ($url_regexp) {
$pattern = ($url_regexp_ci ? '(?i)' : '') . $url;
}
else {
$pattern = '^' . preg_quote($url) . '$';
}
// Purge cache.
foreach (array_filter($hostname) as $name) {
varnish_purge($name, $pattern);
}
drupal_set_message(t('The specified Varnish cache has been successfully purged.'));
}