summaryrefslogtreecommitdiffstats
path: root/authcache.admin.inc
blob: d9093a163f824adf997b1bdf32487aa5d772301a (plain)
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
<?php

/**
 * @file
 * Admin forms and pages.
 */

/**
 * Main Authcache configuration form
 */

function authcache_admin_config($form, &$form_state) {
  drupal_set_title(t('Authcache Configuration'));

  $form['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Authcache Roles'),
    '#collapsible' => TRUE,
  );
  $form['roles']['authcache_roles'] = array(
    '#title' => t('Enable caching for specified user roles'),
    '#description' => t('If no roles are selected, Authcache page caching will not be enabled. Unchecked roles will never be cached.'),
    '#type' => 'checkboxes',
    '#options' => _authcache_get_roles(TRUE),
    '#default_value' => variable_get('authcache_roles', array()),
  );
  $form['roles']['authcache_nojsroles'] = array(
    '#title' => t('Enable caching without JavaScript'),
    '#description' => t('Unchecked roles will not be cached if JavaScript is turned off in the users browser.'),
    '#type' => 'checkboxes',
    '#options' => _authcache_get_roles(TRUE),
    '#default_value' => variable_get('authcache_nojsroles', drupal_map_assoc(array(DRUPAL_ANONYMOUS_RID))),
  );

  $form['adv'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    //'#collapsed' => (!variable_get('authcache_post', 0) && !variable_get('authcache_http200', 0) && !variable_get('authcache_noajax', 0)),
  );
  $form['adv']['noajax'] = array(
    '#type' => 'checkbox',
    '#title' => t("Don't cache Drupal Ajax requests"),
    '#default_value' => variable_get('authcache_noajax', 0),
    '#description' => t("Disable caching of Ajax requests, such as autocomplete."),
  );
  $form['adv']['http200'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only cache HTTP 200 OK pages (exclude 404, 403, etc)'),
    '#default_value' => variable_get('authcache_http200', 0),
    '#description' => t("It's recommended to cache all Drupal pages, though error response pages can be excluded if required."),
  );
  $form['adv']['su'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow caching for superuser (uid = 1)'),
    '#default_value' => variable_get('authcache_su', 0),
    '#description' => t('Specify whether pages should also be cached for the superuser.'),
  );
  $form['adv']['mimetype'] = array(
    '#type' => 'textarea',
    '#title' => t('Cacheable content-types'),
    '#description' => t('Specify which content-types can be cached.'),
    '#default_value' => variable_get('authcache_mimetype', AUTHCACHE_MIMETYPE_DEFAULT),
  );
  $form['adv']['post'] = array(
    '#type' => 'radios',
    '#title' => t('Authcache Ajax Method (GET vs POST)'),
    '#options' => array(
      0 => 'Automatic',
      1 => 'POST',
    ),
    '#default_value' => variable_get('authcache_post', 0),
    '#description' => t('Automatic will use GET for the Authcache Ajax request and POST if the request is over 2,000 characters. You can explicitly require POST if needed, though GET is faster.'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save & clear cached pages'),
  );

  //
  // Status messages
  //
  if (!variable_get('cache_backends', NULL)) {
    drupal_set_message(t('Your settings.php file must be modified to enable Authcache ($conf[\'cache_backends\']). See <a href="@url">README.txt</a>.', array('@url' => base_path() . drupal_get_path('module', 'authcache') . '/README.txt')), 'error');
  }

  if (!variable_get('page_compression', TRUE)) {
    drupal_set_message(t('Note: Page compression is not enabled!  It is strongly recommend that you <a href="./../performance">turn this setting on</a> through Drupal to allow for faster page response times.'), 'warning');
  }

  return $form;
}


/**
 * Authcache config form submit
 */
function authcache_admin_config_validate($form, &$form_state) {

}

/**
 * Authcache config form submit
 */
function authcache_admin_config_submit($form, &$form_state) {
  global $user;

  // Roles
  $cache_roles = drupal_map_assoc(array_filter($form_state['values']['authcache_roles']));
  $nojs_roles = drupal_map_assoc(array_filter($form_state['values']['authcache_nojsroles']));

  // Define/update page caching settings if needed
  $pagecaching = variable_get('authcache_pagecaching', FALSE);

  // Not defined, first config submit
  if (!$pagecaching) {
    variable_set('authcache_pagecaching', array(array(
        'option' => 0,
        'pages' => AUTHCACHE_NOCACHE_DEFAULT,
        'noadmin' => 1,
        'roles' => $cache_roles,
      )));
  }
  // User may have added new role; apply new roles to first page ruleset, just to be safe
  elseif ($cache_roles != variable_get('authcache_roles', $cache_roles)) {
    $pagecaching[0]['roles'] = $cache_roles;
    variable_set('authcache_pagecaching', $pagecaching);
  }

  // Forcibly disable Drupal's built-in SQL caching (no need to cache page twice for anonymous users!)
  if (in_array(DRUPAL_ANONYMOUS_RID, $cache_roles) && variable_get('cache', 0) != 0) {
    variable_set('cache', 0);
    drupal_set_message(t("Drupal's built-in page caching for anonymous users has been disabled to avoid redundant caching."));
  }

  // Devel query logging
  // variable_set('dev_query', $form_state['values']['dev_query']);


  // Delete variable if not in use
  foreach (array('post', 'http200', 'su', 'noajax', 'mimetype') as $key) {
    if ($value = $form_state['values'][$key]) {
      variable_set("authcache_{$key}", $value);
    }
    else {
      variable_del("authcache_{$key}");
    }
  }

  variable_set('authcache_roles', $cache_roles);
  variable_set('authcache_nojsroles', $nojs_roles);

  drupal_set_message(t('Your Authcache settings have been saved.'));

  cache_clear_all('*', 'cache_page', TRUE);
  drupal_set_message(t('Cached pages cleared.'));
}


/**
 * Add new page caching rule to form (part of ajax)
 */
function _authcache_pagecache_form($details) {
  $form['#tree'] = TRUE;
  $delta = $details['delta'];

  $roles = user_roles();
  $roles[DRUPAL_AUTHENTICATED_RID] .= ' ' . t('(without additional roles)');

  // Cacheability settings
  $options = array(t('Cache every page except the listed pages.'), t('Cache only the listed pages.'));
  $description = t("To delete this ruleset, leave the textbox empty.") . ' ' . t("Enter one page per line as Drupal paths. 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>'));
  if (user_access('use PHP for settings')) {
    $options[] = t('Cache pages for which the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
    $description .= t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can severely break your Drupal site.', array('%php' => '<?php ?>'));
  }
  $form['fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Caching Ruleset # %delta', array('%delta' => $delta + 1)),
    '#collapsible' => TRUE,
  );
  $form['fieldset']['option'] = array(
    '#type' => 'radios',
    '#title' => t('Cache specified pages'),
    '#options' => $options,
    '#default_value' => $details['option'],
  );

  $form['fieldset']['pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages'),
    '#description' => $description,
    '#default_value' => $details['pages'],
  );
  $form['fieldset']['noadmin'] = array(
    '#type' => 'checkbox',
    '#title' => t('Exclude Admin pages'),
    '#description' => t('Do not cache administrative pages.'),
    '#default_value' => $details['noadmin'],
  );
  $form['fieldset']['roles'] = array(
    '#title' => 'Apply to these roles',
    '#type' => 'checkboxes',
    '#options' => _authcache_get_roles(),
    '#default_value' => (is_array($details['roles'])) ? $details['roles'] : array(),
  );

  return $form;

}

/**
 * Page caching rules form
 */
function authcache_admin_pagecaching($form, $form_state, $ajax_form = array()) {
  drupal_set_title(t('Authcache Page Caching Settings'));
  drupal_add_css(drupal_get_path('module', 'authcache') . '/authcache.css');

  if (!count(variable_get('authcache_roles', array()))) {
    drupal_set_message(t('You must first select roles to cache before defining page caching setting.'), 'error');
    return $form;
  }

  $form['#cache'] = TRUE;

  // The contents of ajax_form will either come from the db or from $form_state
  if (isset($form_state['ajax_form'])) {
    $ajax_form = $form_state['ajax_form'] + (array) $ajax_form;
  }

  // Default values
  if (empty($ajax_form)) {
    $ajax_form['ajax_form'] = variable_get('authcache_pagecaching', array(array()));
  }

  $form['ajax_wrapper'] = array(
    '#tree' => FALSE,
    '#prefix' => '<div class="clear-block" id="ajax-wrapper">',
    '#suffix' => '</div>',
  );

  $form['ajax_wrapper']['ajax']['#tree'] = TRUE;


  foreach ($ajax_form['ajax_form'] as $delta => $details) {
    $details = (isset($details['fieldset'])) ? $details['fieldset'] : $details; // fieldset inserted from ajax postback
    $details['delta'] = $delta;

    $form['ajax_wrapper']['ajax'][$delta] = _authcache_pagecache_form($details);
  }

  // ajax-enabled "Add" button
  $form['authcache_add_cache'] = array(
    '#type' => 'submit',
    '#value' => t('Add new ruleset') . '...',
    '#description' => t("If the above ruleset isn't, click here to add more choices."),
    '#submit' => array('authcache_ajax_add'),
    '#ajax' => array(
      'callback' => 'authcache_ajax_add',
      'wrapper' => 'ajax-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#attributes' => array('class' => array('authcache-add')),
    '#attached' => array('css' => array(
      drupal_get_path('module', 'authcache') . '/authcache.admin.css',
    )),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save & clear cached pages'),
    '#prefix' => '<br><hr size="1"><br>',
  );

  return $form;
}

/**
 * Page caching rules form submit
 */
function authcache_admin_pagecaching_submit($form, &$form_state) {

  // Ignore ajax events
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    $pagecaching = array();

    foreach ($form_state['values']['ajax'] as $key => $ray) {
      $values = $ray['fieldset'];
      $values['roles'] = drupal_map_assoc(array_filter($values['roles']));
      if ($values['pages']) {
        $pagecaching[$key] = $values;
      }
      if (empty($values['roles'])) {
        drupal_set_message(t('Ruleset #%key is disabled since no roles are associated with it.', array('%key' => ($key + 1))), 'warning');
      }
    }

    if (!empty($pagecaching)) {
      variable_set('authcache_pagecaching', $pagecaching);
    }
    else {
      variable_set('authcache_pagecaching', array(
        array(
          'option' => 0,
          'pages' => AUTHCACHE_NOCACHE_DEFAULT,
          'noadmin' => 1,
          'roles' => variable_get('authcache_roles', array()),
        ),
      )
      );
    }

    drupal_set_message(t('Your page caching settings have been saved.'));
    cache_clear_all();
    drupal_set_message(t('Cached pages cleared.'));

  }
}


/**
 * Generic ajax menu callback (['#ajax']['path'])
 */
function authcache_ajax() {
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form['#post'] = $form_state['post'] = $_POST;
  $form_state['#redirect'] = $form['#programmed'] = FALSE;
  drupal_process_form($form_id, $form, $form_state);
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  $ajax_form = $form['ajax_wrapper']['ajax'];
  unset($ajax_form['#prefix'], $ajax_form['#suffix']); // Prevent duplicate wrappers.
  // TODO Please change this theme call to use an associative array for the $variables parameter.
  // TODO Please change this theme call to use an associative array for the $variables parameter.
  drupal_json_output(array(
    'status' => TRUE,
    'data' => theme('status_messages') . drupal_render($ajax_form),
  ));
}

/**
 * Generic ajax add
 */
function authcache_ajax_add($form, &$form_state) {
  if (isset($form_state['values']['ajax'])) {
    $ajax_form['ajax_form'] = $form_state['values']['ajax'];
  }

  $ajax_form['ajax_form'][] = array();

  unset($form_state['submit_handlers']);
  form_execute_handlers('submit', $form, $form_state);
  $form_state['ajax_form'] = $ajax_form;
  $form_state['rebuild'] = TRUE;
  return $form;
}

/**
 * Helper function, get authcache user roles
 * @param $all_roles = return all user roles
 * @return array of user roles
 */
function _authcache_get_roles($all_roles = FALSE) {
  $roles = user_roles();
  $roles[DRUPAL_AUTHENTICATED_RID] .= ' ' . t('(without additional roles)');
  if ($all_roles) {
    return $roles;
  }
  else {
    $authcache_roles = array();
    foreach (variable_get('authcache_roles', array()) as $key) {
      $authcache_roles[$key] = $roles[$key];
    }
    return $authcache_roles;
  }
}