You are here

function authcache_builtin_cacheinc_retrieve_cache_page in Authenticated User Page Caching (Authcache) 7.2

Send cached page to browser, if found.

Return value

bool TRUE if page was delivered, FALSE otherwise

1 call to authcache_builtin_cacheinc_retrieve_cache_page()
authcache_builtin.cache.inc in modules/authcache_builtin/authcache_builtin.cache.inc

File

modules/authcache_builtin/authcache_builtin.cache.inc, line 79

Code

function authcache_builtin_cacheinc_retrieve_cache_page() {
  global $base_root;

  // Connect to database and initialize configuration if not disabled from
  // within settings.php
  if (!variable_get('authcache_builtin_cache_without_database')) {
    $phase = variable_get('authcache_builtin_cache_without_variables') ? DRUPAL_BOOTSTRAP_DATABASE : DRUPAL_BOOTSTRAP_VARIABLES;
    drupal_bootstrap($phase, FALSE);
  }

  // The following three basic exclusion rules are mirrored in
  // authcache_authcache_request_exclude() in authcache.module
  // BEGIN: basic exclusion rules.
  if (!function_exists('authcache_backend_init')) {
    return FALSE;
  }

  // Only GET and HEAD requests allowed.
  if (!($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'HEAD')) {
    return FALSE;
  }

  // Drupal core page caching for anonymous users active.
  if (variable_get('cache') || variable_get('page_cache_without_database')) {
    return FALSE;
  }

  // Not invoked using an allowed front controller.
  $frontscripts = variable_get('authcache_frontcontroller_whitelist', array(
    DRUPAL_ROOT . '/index.php',
  ));
  $frontscripts = array_map('realpath', $frontscripts);
  if (!in_array(realpath($_SERVER['SCRIPT_FILENAME']), $frontscripts)) {
    return FALSE;
  }

  // END: basic exclusion rules.
  //
  // Determine whether caching is disabled for this request.
  $nocache_get = variable_get('authcache_builtin_nocache_get', '_authcache_builtin_cacheinc_default_nocache_get');
  if (is_callable($nocache_get) && call_user_func($nocache_get)) {
    return FALSE;
  }

  // Try to retrieve a key when the session cookie is present on the request.
  if (isset($_COOKIE[session_name()])) {
    $key = _authcache_builtin_cacheinc_cache_key_get($_COOKIE[session_name()]);
  }
  else {
    $key = authcache_backend_anonymous_key();
  }

  // Initialize the backend.
  $vary_header = variable_get('authcache_builtin_vary', 'Cookie');
  if (!authcache_backend_init('authcache_builtin', $vary_header, $key)) {
    return FALSE;
  }

  // When key needs to be regenerated, return and trigger a full bootstrap.
  if ($key === FALSE) {
    return FALSE;
  }

  // Attempt to retrieve page from cache.
  $cid = $key . request_uri();
  $cache = cache_get($cid, 'cache_page');
  if (empty($cache)) {
    header('X-Drupal-Cache: MISS');
    return FALSE;
  }
  else {

    // Render cache benchmark.
    if (isset($_COOKIE['Drupal_authcache_cache_render'])) {
      setcookie('Drupal.authcache.cache_render', timer_read('page'), 0, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
    }
    header('X-Drupal-Cache: HIT');
    _authcache_builtin_cacheinc_fix_browsers();
    authcache_serve_page_from_cache($cache, $key);
    return TRUE;
  }
}