-
Notifications
You must be signed in to change notification settings - Fork 3
/
hosting_varnish.module
186 lines (165 loc) · 5.72 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
<?php
/**
* @file
* Integrates hosted sites with Varnish cache.
*/
/**
* Implements hook_perm().
*/
function hosting_varnish_perm() {
return array('purge varnish cache');
}
/**
* 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,
);
if (hosting_feature('varnish')) {
$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;
}
/**
* Purges cache for a site.
*/
function hosting_varnish_node_purge_cache($form, $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.'));
}