-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastly.admin.inc
431 lines (361 loc) · 11.8 KB
/
fastly.admin.inc
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/**
* @file
* Administrative forms for Fastly module.
*/
/**
* Settings form.
*/
function fastly_setup_form($form_state) {
$api = fastly_get_api();
$service_id = variable_get('fastly_service_id', '');
$api_key = variable_get('fastly_api_key', '');
$form['fastly_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Fastly API Key'),
'#default_value' => $api_key,
'#required' => TRUE,
'#description' => t('You can find it on your account settings page. If you dont have an account, please go to <a href="/?q=admin/config/services/fastly/register">registration page</a>'),
);
if ($api_key) {
$services = array();
foreach ($api->getServices() as $service) {
$services[$service->id] = $service->name;
}
ksort($services);
$form['fastly_service_id'] = array(
'#type' => 'radios',
'#title' => t('Service'),
'#options' => $services,
'#default_value' => $service_id,
'#required' => TRUE,
'#description' => t('A Service represents the configuration for your website to be served through Fastly.'),
);
$form['actions']['new_service'] = array(
'#markup' => l(t('New service'), 'admin/config/services/fastly/new',
array('attributes' => array('class' => 'button'))),
'#weight' => 10,
);
}
$form['fastly_ttl'] = array(
'#type' => 'textfield',
'#title' => t('Default TTL'),
'#default_value' => $service_id ? $api->getSettings()->{'general.default_ttl'} : '',
'#description' => t('The default time to live for cached content in seconds.'),
);
$description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.",
array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
));
$form['fastly_non_cached'] = array(
'#type' => 'textarea',
'#title' => t('Non-cached pages'),
'#default_value' => variable_get('fastly_non_cached', ''),
'#description' => $description,
);
$form['#submit'][] = 'fastly_setup_form_submit';
return system_settings_form($form);
}
/**
* Register form.
*/
function fastly_register_form($form_state) {
if (variable_get('fastly_used_registration')) {
drupal_set_message(t('You are already registered. You can <a href="https://app.fastly.com/#password_reset">reset your password</a> if you don\'t remember it.'), 'warning');
}
else {
global $user;
$form['owner_first_name'] = array(
'#type' => 'textfield',
'#title' => t("First Name"),
'#description' => t("The customer account owner's first name. Ex: John."),
'#required' => TRUE,
);
$form['owner_last_name'] = array(
'#type' => 'textfield',
'#title' => t("Last Name"),
'#description' => t("The customer account owner's last name. Ex: Smith."),
'#required' => TRUE,
);
$form['owner_login'] = array(
'#type' => 'textfield',
'#title' => t("Email"),
'#default_value' => $user->mail,
'#description' => t("The owner's email to be used as login. Ex: [email protected]."),
'#required' => TRUE,
);
$form['owner_password'] = array(
'#type' => 'password',
'#title' => t("Password"),
'#default_value' => '',
'#required' => TRUE,
);
$form['confirm_password'] = array(
'#type' => 'password',
'#title' => t("Confirm Password"),
'#default_value' => '',
'#description' => t("The customer account owner's password. Please enter 4 or more characters. Ex: P@ssW0rd!1"),
'#required' => TRUE,
);
$form['account_name'] = array(
'#type' => 'textfield',
'#title' => t('Company Name'),
'#default_value' => variable_get('site_name', ''),
'#description' => t('The customer account name. Ex: Some Business, LLC.'),
'#required' => TRUE,
);
$form['origin_ip'] = array(
'#type' => 'textfield',
'#title' => t('Origin Server IP'),
'#default_value' => $_SERVER['SERVER_ADDR'],
'#required' => TRUE,
);
$form['port'] = array(
'#type' => 'textfield',
'#title' => t('Origin Port'),
'#default_value' => '80',
'#required' => TRUE,
);
$form['domain_name'] = array(
'#type' => 'textfield',
'#title' => t('Domain Name'),
'#default_value' => $_SERVER['HTTP_HOST'],
'#required' => TRUE,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Sign Up'),
);
$form['actions']['already_registered'] = array(
'#markup' => t('Already registered?') . l(t('Click here to enter your account information and get started!'), 'admin/config/services/fastly/config'),
);
$form['policy'] = array(
'#markup' => '<p>' . t('By clicking "Sign Up" you are agreeing to the <a target="_blank" href="https://www.fastly.com/terms">Terms of Use</a> and <a target="_blank" href="https://www.fastly.com/privacy">Privacy Policy</a>.') . '</p>',
);
return $form;
}
}
/**
* Purge form.
*/
function fastly_purge_form($form_state) {
if (variable_get('fastly_service_id', '') && variable_get('fastly_api_key', '')) {
$form['purge_url'] = array(
'#type' => 'fieldset',
'#title' => t('Purge by URL'),
'#description' => t('Paste one or more URLs to purge. Each in new line.'),
);
$form['purge_url']['urls_list'] = array(
'#type' => 'textarea',
);
$form['purge_url']['submit'] = array(
'#type' => 'button',
'#value' => t('Purge'),
'#id' => 'urls',
'#name' => 'urls',
'#executes_submit_callback' => 1,
);
$form['purge_key'] = array(
'#type' => 'fieldset',
'#title' => t('Purge by key'),
'#description' => t('Paste one or more keys to purge. Each in new line.'),
);
$form['purge_key']['keys_list'] = array(
'#type' => 'textarea',
);
$form['purge_key']['submit'] = array(
'#type' => 'button',
'#value' => t('Purge'),
'#id' => 'keys',
'#name' => 'keys',
'#executes_submit_callback' => 1,
);
$form['purge_all'] = array(
'#type' => 'fieldset',
'#title' => t('Purge all'),
'#description' => t('Purge whole service. You might not use this function too often.'),
);
$form['purge_all']['submit'] = array(
'#type' => 'button',
'#value' => t('Purge'),
'#id' => 'all',
'#name' => 'all',
'#executes_submit_callback' => 1,
);
return $form;
}
else {
drupal_set_message(t('You need to set up your API key and service ID to use this form.'), 'warning');
}
}
/**
* New service form.
*/
function fastly_new_service_form() {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Service Name'),
'#default_value' => variable_get('site_name', ''),
'#required' => TRUE,
);
$form['origin_ip'] = array(
'#type' => 'textfield',
'#title' => t('Origin Server IP'),
'#default_value' => $_SERVER['SERVER_ADDR'],
'#required' => TRUE,
);
$form['port'] = array(
'#type' => 'textfield',
'#title' => t('Origin Port'),
'#default_value' => '80',
'#required' => TRUE,
);
$form['domain_name'] = array(
'#type' => 'textfield',
'#title' => t('Domain Name'),
'#default_value' => $_SERVER['HTTP_HOST'],
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create'),
);
return $form;
}
/**
* Implements hook_form_validate().
*/
function fastly_register_form_validate($form, &$form_state) {
if ($form_state['values']['owner_password'] && strlen($form_state['values']['owner_password']) < 4) {
form_set_error('owner_password', t('The password must contain 4 or more characters.'));
}
if (!valid_email_address($form_state['values']['owner_login'])) {
form_set_error('owner_login', t('The email address is invalid.'));
}
if ($form_state['values']['owner_password'] !== $form_state['values']['confirm_password']) {
form_set_error('confirm_password', t('The passwords do not match.'));
}
fastly_help_message();
}
/**
* Implements hook_form_validate().
*/
function fastly_setup_form_validate($form, &$form_state) {
$api = fastly_get_api($form_state['values']['fastly_api_key']);
if (!$api->validate()) {
form_set_error('', t('Invalid API key.'));
}
fastly_help_message();
}
/**
* Implements hook_form_submit().
*/
function fastly_setup_form_submit($form, &$form_state) {
$api = fastly_get_api();
$api->updateSettings(array('general.default_ttl' => $form_state['values']['fastly_ttl']));
}
/**
* Implements hook_form_submit().
*/
function fastly_register_form_submit($form, &$form_state) {
$data = array(
'name' => $form_state['values']['account_name'],
'owner' => array(
'name' => $form_state['values']['owner_first_name'] . ' ' . $form_state['values']['owner_last_name'],
'login' => $form_state['values']['owner_login'],
'password' => $form_state['values']['owner_password'],
),
'domain' => $form_state['values']['domain_name'],
'port' => $form_state['values']['port'],
'address' => $form_state['values']['origin_ip'],
'ipv4' => $form_state['values']['origin_ip'],
'version' => 1,
'id' => 'syslog',
);
$api = fastly_get_api();
$account = $api->signup($data);
if (isset($account->msg)) {
drupal_set_message($account->msg, 'error');
}
else {
variable_set('fastly_used_registration', 1);
variable_set('fastly_api_key', $account->api_key);
variable_set('fastly_service_id', $account->service_id);
drupal_set_message(t('Registration successful! You will receive a confirmation email very soon. Please check your inbox and verify your account by clicking the received link.'));
drupal_goto('admin/config/services/fastly/purge');
}
}
/**
* Implements hook_form_submit().
*/
function fastly_new_service_form_submit($form, &$form_state) {
$data = array(
'name' => $form_state['values']['name'],
'domain' => $form_state['values']['domain_name'],
'port' => $form_state['values']['port'],
'address' => $form_state['values']['domain_name'],
'ipv4' => $form_state['values']['origin_ip'],
'version' => 1,
'id' => 'syslog',
);
$api = fastly_get_api();
$service = $api->createService($data);
if (isset($service->msg)) {
drupal_set_message($service->msg, 'error');
}
else {
variable_set('fastly_service_id', $service->id);
drupal_set_message(t('A new service successfuly created and set to default.'));
drupal_goto('admin/config/services/fastly');
}
}
/**
* Implements hook_form_submit().
*/
function fastly_purge_form_submit($form, &$form_state) {
$method = $form_state['triggering_element']['#name'];
$api = fastly_get_api();
switch ($method) {
case 'all':
$api->purgeAll();
break;
case 'urls':
$values = trim($form_state['values']['urls_list']);
if (empty($values)) {
$error = (bool) drupal_set_message(t('Please input the URLs to purge.'), 'error');
}
else {
foreach (explode("\n", $values) as $line) {
$api->purgePath(trim($line));
}
}
break;
case 'keys':
$values = trim($form_state['values']['keys_list']);
if (empty($values)) {
$error = (bool) drupal_set_message(t('Please input the keys to purge.'), 'error');
}
else {
foreach (explode("\n", $values) as $line) {
$api->purgeKey(trim($line));
}
}
break;
}
if (empty($error)) {
drupal_set_message(t('Cache successfuly purged.'));
}
}
/**
* A reusable snippet.
*
* Checks for error messages
* and sets up an extra message with a link to Zendesk.
*/
function fastly_help_message() {
if (drupal_get_messages('error', FALSE)) {
drupal_set_message(t('<a href="https://fastly.zendesk.com">Click here</a> if you need help.'), 'warning');
}
}