You are here

function _authcache_is_cacheable in Authenticated User Page Caching (Authcache) 6

Same name and namespace in other branches
  1. 7 authcache.helpers.inc \_authcache_is_cacheable()

Should the current page be cached?

1 call to _authcache_is_cacheable()
authcache_init in ./authcache.module
Implements hook_init().

File

./authcache.helpers.inc, line 12
Helper functions for the Authcache module (no Drupal hooks here).

Code

function _authcache_is_cacheable() {
  global $user, $authcache_debug_info, $is_ajax_authcache;

  // Do not cache...
  if (!empty($_POST) || $user->uid == 1 || $user->uid && !isset($_COOKIE['has_js']) || isset($_COOKIE['nocache']) || $is_ajax_authcache || isset($_SESSION['messages']) || drupal_set_message() != NULL || ($ar = explode('?', basename(request_uri()))) && substr(array_shift($ar), -4) == '.php' || strpos(variable_get('cache_inc', ''), 'authcache') === FALSE || variable_get('authcache_noajax', FALSE) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    return FALSE;
  }

  // Check if caching is enabled for user's role
  if (!_authcache_is_account_cacheable($user)) {
    return FALSE;
  }
  $is_cached = FALSE;
  $alias = drupal_get_path_alias($_GET['q']);
  $path = drupal_get_normal_path($_GET['q']);

  // Normalize path
  // Now check page caching settings, defined by the site admin
  $pagecaching = variable_get('authcache_pagecaching', array(
    array(
      'option' => 0,
      'pages' => AUTHCACHE_NOCACHE_DEFAULT,
      'roles' => array(
        DRUPAL_ANONYMOUS_RID,
      ),
    ),
  ));
  foreach ($pagecaching as $page_rules) {

    // Do caching page roles apply to current user?
    $keys = array_keys($user->roles);

    // Match up authenticated roles correctly in case "authenticated user" is unchecked
    if (in_array(DRUPAL_AUTHENTICATED_RID, $keys) && !in_array(DRUPAL_AUTHENTICATED_RID, $page_rules['roles'])) {
      unset($keys[array_search(DRUPAL_AUTHENTICATED_RID, $keys)]);
    }
    $extra_roles = array_diff($keys, $page_rules['roles']);
    if (empty($extra_roles)) {
      switch ($page_rules['option']) {
        case '0':

        // Cache every page except the listed pages.
        case '1':

          // Cache only the listed pages.
          $regexp = '/^(' . preg_replace(array(
            '/(\\r\\n?|\\n)/',
            '/\\\\\\*/',
            '/(^|\\|)\\\\<front\\\\>($|\\|)/',
          ), array(
            '|',
            '.*',
            '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
          ), preg_quote($page_rules['pages'], '/')) . ')$/';
          $is_cached = !($page_rules['option'] xor preg_match($regexp, $alias));
          break;
        case '2':

          // Cache pages for which the following PHP code returns TRUE
          $result = drupal_eval($page_rules['pages']);
          $is_cached = !empty($result);
          break;
        default:
          break;
      }
    }
  }
  return $is_cached;
}