You are here

function authcache_init in Authenticated User Page Caching (Authcache) 6

Same name and namespace in other branches
  1. 7.2 authcache.module \authcache_init()
  2. 7 authcache.module \authcache_init()

Implements hook_init().

File

./authcache.module, line 120
Authenticated User Page Caching (and anonymous users, too!)

Code

function authcache_init() {
  global $user, $is_page_authcache;

  // Pressflow compatibility (since Pressflow doesn't set this cookie)
  if (($sysblock = system_block('list')) && $sysblock[0]['info'] == t('Powered by Pressflow')) {
    drupal_add_js("document.cookie = 'has_js=1; path=/';", 'inline', 'header');
  }

  // Can this page request be cached?
  if (_authcache_is_cacheable()) {
    global $conf;
    $is_page_authcache = TRUE;

    // Don't allow format_date() to use the user's local timezone
    $conf['configurable_timezones'] = FALSE;

    // Authcache core JS
    drupal_add_js(drupal_get_path('module', 'authcache') . '/authcache.js', 'module', 'header');

    // Add JS for debug mode?
    if (variable_get('authcache_debug_all', FALSE) || $user->uid && ($debug_users = variable_get('authcache_debug_users', array()))) {
      drupal_add_js(drupal_get_path('module', 'authcache') . '/authcache.debug.js', 'module', 'header');

      // Also see authcache_authcache_info() for user debug settings
    }

    // Initialize Authcache after all other JS is loaded
    drupal_add_js('jQuery(function() { Authcache.init(); });', 'inline', 'header');

    // Force Ajax to use POST method instead of GET
    if (variable_get('authcache_post', FALSE)) {
      drupal_add_js(array(
        'Authcache' => array(
          'post' => 1,
        ),
      ), 'setting', 'header');
    }

    // Forcibly disable Drupal's built-in SQL caching
    // (No need to cache page twice for anonymous users)
    if (!$user->uid && variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
      variable_set('cache', CACHE_DISABLED);
    }

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

  // Remove debug cookies
  if (isset($_COOKIE['authcache_debug']) && !variable_get('authcache_debug_all', FALSE)) {
    if (!$user->uid || !in_array($user->name, variable_get('authcache_debug_users', array()))) {
      setcookie('authcache_debug', "", time() - 84000);

      // Delete JS cookie
      setcookie('authcache_debug', "", time() - 84000, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
      setcookie('nocache', 1, 0, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
      setcookie('nocache_temp', 1, 0, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
    }
  }
}