You are here

authcache_debug.module in Authenticated User Page Caching (Authcache) 7.2

Debug widget for Authcache.

File

modules/authcache_debug/authcache_debug.module
View source
<?php

/**
 * @file
 * Debug widget for Authcache.
 */
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);

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Implements hook_user_login().
 */
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);
}

/**
 * Implements hook_user_logout().
 */
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);
}

/**
 * Implements hook_page_build().
 *
 * Attach authcache JavaScript if page is going to be cached.
 */
function authcache_debug_page_build(&$page) {

  // Add JS for debug mode?
  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()) {

    // Status messages prevent pages from being cached.
    if (variable_get('authcache_debug_page', FALSE)) {
      drupal_set_message(t('Caching disabled by') . ' ' . l(t('Authcache Config.'), 'admin/config/system/authcache/debug'));
    }
  }
}

/**
 * Implements hook_authcache_cache_data_alter().
 */
function authcache_debug_init() {
  if (!authcache_debug_excluded()) {
    drupal_register_shutdown_function('_authcache_debug_shutdown');
  }
}

/**
 * Implements hook_flush_caches().
 */
function authcache_debug_flush_caches() {
  return array(
    'cache_authcache_debug',
  );
}

/**
 * Return generated cid for authcache debug information.
 */
function _authcache_debug_cid() {
  $cid =& drupal_static(__FUNCTION__);
  if (!isset($cid)) {
    $cid = 'req-' . drupal_random_key();
  }
  return $cid;
}

/**
 * Shutdown function for saving debug info.
 */
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);
}

/**
 * Implements hook_authcache_debug_info().
 */
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;
}

/**
 * Log a message to the debug widget and / or watchdog.
 */
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;
}

/**
 * Set and get the caching status of the current request.
 */
function _authcache_debug_status($status = NULL) {
  $first_status =& drupal_static(__FUNCTION__);
  if (!isset($first_status) && isset($status)) {
    $first_status = $status;
  }
  return $first_status;
}

/**
 * Determines whether or not to collect debug info on the current request.
 */
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;
}

/**
 * Return true when log-to-watchdog is enabled for the logged in user.
 */
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;
}

/**
 * Implements hook_authcache_request_exclude().
 */
function authcache_debug_authcache_request_exclude() {
  if (arg(0) === 'authcache-debug' && arg(1) === 'ajax') {
    return t('Caching disabled on authcache debug ajax callback.');
  }
}

/**
 * Implements hook_authcache_debug_exclude().
 */
function authcache_debug_authcache_debug_exclude() {
  if (arg(0) === 'authcache-debug' && arg(1) === 'ajax') {
    return TRUE;
  }
}

/**
 * Implements hook_authcache_excluded().
 */
function authcache_debug_authcache_excluded($reason) {
  _authcache_debug_status('Page EXCLUDED');
  authcache_debug_log(t('Excluded'), $reason);
}

/**
 * Implements hook_authcache_canceled().
 */
function authcache_debug_authcache_canceled($reason) {
  _authcache_debug_status('Caching CANCELED');
  authcache_debug_log(t('Canceled'), $reason);
}

/**
 * Implements hook_authcache_precluded().
 */
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);
}

/**
 * Returns true if the currently logged in user has access to debug functions.
 */
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()));
  }
}

/**
 * Load authcache info for the specified cache id.
 */
function authcache_debug_info_load($cid) {
  $cache = cache_get($cid, 'cache_authcache_debug');
  return $cache ? $cache->data : FALSE;
}

/**
 * Deliver cache info to the client via Ajax/Drupal.settings.
 */
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();
}

Functions

Namesort descending Description
authcache_debug_access Returns true if the currently logged in user has access to debug functions.
authcache_debug_authcache_canceled Implements hook_authcache_canceled().
authcache_debug_authcache_debug_exclude Implements hook_authcache_debug_exclude().
authcache_debug_authcache_debug_info Implements hook_authcache_debug_info().
authcache_debug_authcache_excluded Implements hook_authcache_excluded().
authcache_debug_authcache_precluded Implements hook_authcache_precluded().
authcache_debug_authcache_request_exclude Implements hook_authcache_request_exclude().
authcache_debug_excluded Determines whether or not to collect debug info on the current request.
authcache_debug_flush_caches Implements hook_flush_caches().
authcache_debug_info_ajax_get Deliver cache info to the client via Ajax/Drupal.settings.
authcache_debug_info_load Load authcache info for the specified cache id.
authcache_debug_init Implements hook_authcache_cache_data_alter().
authcache_debug_log Log a message to the debug widget and / or watchdog.
authcache_debug_menu Implements hook_menu().
authcache_debug_page_build Implements hook_page_build().
authcache_debug_user_login Implements hook_user_login().
authcache_debug_user_logout Implements hook_user_logout().
_authcache_debug_cid Return generated cid for authcache debug information.
_authcache_debug_shutdown Shutdown function for saving debug info.
_authcache_debug_status Set and get the caching status of the current request.
_authcache_debug_watchdog_enabled Return true when log-to-watchdog is enabled for the logged in user.

Constants