'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 README.txt.', 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 turn this setting on 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' => ''));
if (user_access('use PHP for settings')) {
$options[] = t('Cache pages for which the following PHP code returns TRUE
(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' => ''));
}
$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' => '',
'#suffix' => '
',
);
$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' => '
',
);
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;
}
}