View source
<?php
define('AUTHCACHE_DEBUG_WATCHDOG_NEVER', 0);
define('AUTHCACHE_DEBUG_WATCHDOG_DEBUG_USERS', 1);
define('AUTHCACHE_DEBUG_WATCHDOG_ENABLED_ROLES', 2);
define('AUTHCACHE_DEBUG_WATCHDOG_ALWAYS', 99);
function authcache_debug_menu() {
$items['admin/config/system/authcache/debug'] = array(
'title' => 'Debug',
'description' => "Configure debug settings.",
'page callback' => 'drupal_get_form',
'page arguments' => array(
'authcache_debug_admin',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'authcache_debug.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
$items['authcache-debug/ajax/%authcache_debug_info'] = array(
'page callback' => 'authcache_debug_info_ajax_get',
'page arguments' => array(
2,
),
'access callback' => 'authcache_debug_access',
'type' => MENU_CALLBACK,
);
return $items;
}
function authcache_debug_user_login(&$edit, $account) {
$cookies['Drupal.authcache.cache_render']['present'] = FALSE;
$cookies['Drupal.authcache.aucdbg']['present'] = authcache_debug_access();
if ($cookies['Drupal.authcache.aucdbg']['present']) {
$cookies['Drupal.authcache.aucdbg']['value'] = isset($_COOKIE['Drupal_authcache_aucdbg']) ? $_COOKIE['Drupal_authcache_aucdbg'] : hexdec(mt_rand(0, 0xffff));
$cookies['Drupal.authcache.aucdbg']['secure'] = FALSE;
}
authcache_add_cookie($cookies);
}
function authcache_debug_user_logout($account) {
$cookies['Drupal.authcache.cache_render']['present'] = FALSE;
$cookies['Drupal.authcache.aucdbg']['present'] = variable_get('authcache_debug_all', FALSE);
if ($cookies['Drupal.authcache.aucdbg']['present']) {
$cookies['Drupal.authcache.aucdbg']['value'] = isset($_COOKIE['Drupal_authcache_aucdbg']) ? $_COOKIE['Drupal_authcache_aucdbg'] : hexdec(mt_rand(0, 0xffff));
$cookies['Drupal.authcache.aucdbg']['secure'] = FALSE;
}
authcache_add_cookie($cookies);
}
function authcache_debug_page_build(&$page) {
drupal_add_js(drupal_get_path('module', 'authcache_debug') . '/authcache_debug.js');
drupal_add_css(drupal_get_path('module', 'authcache_debug') . '/authcache_debug.css');
$callback = url('authcache-debug/ajax/' . _authcache_debug_cid());
drupal_add_js(array(
'authcacheDebug' => array(
'url' => $callback,
'all' => variable_get('authcache_debug_all', FALSE),
),
), 'setting');
if (authcache_page_is_cacheable()) {
if (variable_get('authcache_debug_page', FALSE)) {
drupal_set_message(t('Caching disabled by') . ' ' . l(t('Authcache Config.'), 'admin/config/system/authcache/debug'));
}
}
}
function authcache_debug_init() {
if (!authcache_debug_excluded()) {
drupal_register_shutdown_function('_authcache_debug_shutdown');
}
}
function authcache_debug_flush_caches() {
return array(
'cache_authcache_debug',
);
}
function _authcache_debug_cid() {
$cid =& drupal_static(__FUNCTION__);
if (!isset($cid)) {
$cid = 'req-' . drupal_random_key();
}
return $cid;
}
function _authcache_debug_shutdown() {
$debuginfo = module_invoke_all('authcache_debug_info');
drupal_alter('authcache_debug_info', $debuginfo);
$messages = authcache_debug_log();
if (!empty($messages)) {
$debuginfo['messages'] = $messages;
}
$cid = _authcache_debug_cid();
$lifetime = variable_get('authcache_debug_cache_lifetime', 0);
$expire = $lifetime > 0 ? REQUEST_TIME + $lifetime : CACHE_TEMPORARY;
cache_set($cid, $debuginfo, 'cache_authcache_debug', $expire);
}
function authcache_debug_authcache_debug_info() {
global $user;
$info = array(
'cacheTime' => REQUEST_TIME,
'cacheStatus' => _authcache_debug_status(),
'cacheReqId' => empty($_COOKIE['Drupal_authcache_aucdbg']) ? '' : $_COOKIE['Drupal_authcache_aucdbg'],
'pageRender' => timer_read('page'),
'Debug Users' => variable_get('authcache_debug_users', array()),
'Cache User' => $user->uid,
'Cache Backends' => implode(', ', module_implements('authcache_backend_cache_save')),
'Active Backend' => authcache_backend(),
'Authcache Key' => authcache_key(),
);
if (user_is_logged_in()) {
$info['Key props'] = authcache_key_properties();
}
return $info;
}
function authcache_debug_log($label = NULL, $message = NULL) {
$messages =& drupal_static(__FUNCTION__);
if (!isset($messages)) {
$messages = array();
}
if ($label && $message) {
$messages[] = array(
'label' => $label,
'message' => $message,
);
if (_authcache_debug_watchdog_enabled()) {
watchdog('Authcache', '@label: @message', array(
'@label' => $label,
'@message' => $message,
));
}
}
return $messages;
}
function _authcache_debug_status($status = NULL) {
$first_status =& drupal_static(__FUNCTION__);
if (!isset($first_status) && isset($status)) {
$first_status = $status;
}
return $first_status;
}
function authcache_debug_excluded() {
$excluded =& drupal_static(__FUNCTION__, NULL);
if (!isset($excluded)) {
$result = module_invoke_all('authcache_debug_exclude');
$excluded = count(array_filter($result)) > 0;
}
return $excluded;
}
function _authcache_debug_watchdog_enabled() {
global $user;
$enabled =& drupal_static(__FUNCTION__);
if (!isset($enabled)) {
if (authcache_debug_excluded()) {
$enabled = FALSE;
}
else {
switch (variable_get('authcache_debug_watchdog', AUTHCACHE_DEBUG_WATCHDOG_NEVER)) {
case AUTHCACHE_DEBUG_WATCHDOG_ALWAYS:
$enabled = TRUE;
break;
case AUTHCACHE_DEBUG_WATCHDOG_ENABLED_ROLES:
$enabled = NULL === authcache_authcache_account_exclude($user);
break;
case AUTHCACHE_DEBUG_WATCHDOG_DEBUG_USERS:
$enabled = $user->uid && in_array($user->name, variable_get('authcache_debug_users', array()));
break;
default:
$enabled = FALSE;
break;
}
}
}
return $enabled;
}
function authcache_debug_authcache_request_exclude() {
if (arg(0) === 'authcache-debug' && arg(1) === 'ajax') {
return t('Caching disabled on authcache debug ajax callback.');
}
}
function authcache_debug_authcache_debug_exclude() {
if (arg(0) === 'authcache-debug' && arg(1) === 'ajax') {
return TRUE;
}
}
function authcache_debug_authcache_excluded($reason) {
_authcache_debug_status('Page EXCLUDED');
authcache_debug_log(t('Excluded'), $reason);
}
function authcache_debug_authcache_canceled($reason) {
_authcache_debug_status('Caching CANCELED');
authcache_debug_log(t('Canceled'), $reason);
}
function authcache_debug_authcache_precluded($reason) {
global $user;
if (!empty($user->uid) || !empty($_SESSION)) {
$_SESSION['aucdbg_precluded'][] = array(
'label' => t('Precluded'),
'message' => $reason,
);
}
authcache_debug_log(t('Precluded'), $reason);
}
function authcache_debug_access($account = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
if (variable_get('authcache_debug_all', FALSE)) {
return TRUE;
}
elseif (!$account->uid) {
return FALSE;
}
else {
return in_array($account->name, variable_get('authcache_debug_users', array()));
}
}
function authcache_debug_info_load($cid) {
$cache = cache_get($cid, 'cache_authcache_debug');
return $cache ? $cache->data : FALSE;
}
function authcache_debug_info_ajax_get($data) {
if (!empty($_SESSION['aucdbg_precluded'])) {
foreach ($_SESSION['aucdbg_precluded'] as $message) {
$data['messages'][] = $message;
}
unset($_SESSION['aucdbg_precluded']);
if (empty($_SESSION)) {
unset($_SESSION);
}
if (empty($data['cacheStatus'])) {
$data['cacheStatus'] = 'Request PRECLUDED';
}
}
if (empty($data['cacheStatus'])) {
$req_id = empty($_COOKIE['Drupal_authcache_aucdbg']) ? '' : $_COOKIE['Drupal_authcache_aucdbg'];
$cache_req_id = $data['cacheReqId'];
$data['cacheStatus'] = $req_id === $cache_req_id ? 'MISS' : 'HIT';
}
drupal_add_http_header('Vary', 'Cookie');
drupal_add_http_header('Cache-Control', 'no-cache, must-revalidate, post-check=0, pre-check=0');
drupal_json_output($data);
drupal_exit();
}