You are here

function _authcache_is_cacheable in Authenticated User Page Caching (Authcache) 7

Same name and namespace in other branches
  1. 6 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 25
Helper functions for the Authcache module (no Drupal hooks here).

Code

function _authcache_is_cacheable() {
  global $user, $_authcache_is_ajax, $_authcache_info, $conf;

  // Do not cache if...
  if (drupal_is_cli()) {
    $_authcache_info['no_cache_reason'] = 'Running as CLI script';
    return FALSE;
  }
  if (!empty($_POST)) {
    $_authcache_info['no_cache_reason'] = 'POST request';
    return FALSE;
  }

  // disabled this, as it's covered by the "role caching" test

  //if ($user->uid == 1) {

  //  return 'User is Superadmin (user id 1)';

  //}
  if ($user->uid && !isset($_COOKIE['has_js'])) {
    $_authcache_info['no_cache_reason'] = 'Users with JS disabled';
    return FALSE;
  }
  if (isset($_COOKIE['nocache'])) {
    $_authcache_info['no_cache_reason'] = 'Caching disabled for session (nocache cookie set)';
    return FALSE;
  }
  if ($_authcache_is_ajax) {
    $_authcache_info['no_cache_reason'] = 'Authcache Ajax request';
    return FALSE;
  }
  if (drupal_set_message()) {
    $_authcache_info['no_cache_reason'] = 'User notification pending (drupal_set_message)';
    return FALSE;
  }
  if (($ar = explode('?', basename(request_uri()))) && substr(array_shift($ar), -4) == '.php') {
    $_authcache_info['no_cache_reason'] = 'PHP files (cron.php, update.php, etc)';
    return FALSE;
  }
  if (empty($conf['cache_backends'])) {
    $_authcache_info['no_cache_reason'] = 'no cache_backends defined in settings.php - please see README.txt';
    return FALSE;
  }
  if (variable_get('authcache_noajax', FALSE) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $_authcache_info['no_cache_reason'] = 'Ajax request';
    return FALSE;
  }

  // Check if caching is enabled for user's role
  if (!_authcache_is_account_cacheable($user)) {
    $_authcache_info['no_cache_reason'] = 'Caching disabled for this users role';
    return 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,
      ),
    ),
  ));
  $i = 0;
  $is_cacheable = TRUE;
  foreach ($pagecaching as $page_rules) {

    // Do caching page roles apply to current user?
    $extra_roles = _authcache_diff_roles($user->roles, $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.
          $page_listed = drupal_match_path($alias, $page_rules['pages']);
          if (!!($page_rules['option'] xor $page_listed)) {
            $_authcache_info['no_cache_reason'] = 'Caching disabled by Page Ruleset ' . $i;
            $is_cacheable = FALSE;
          }
          break;
        case '2':

          // Cache pages for which the following PHP code returns TRUE
          if (module_exists('php')) {
            $result = php_eval($page_rules['pages']);
          }
          if (empty($result)) {
            $_authcache_info['no_cache_reason'] = 'Caching disabled by PHP Rule';
            $is_cacheable = FALSE;
          }
          break;
        default:
          break;
      }
      if (!empty($page_rules['noadmin']) && path_is_admin(current_path())) {
        $is_cacheable = FALSE;
        $_authcache_info['no_cache_reason'] = 'Not caching admin pages';
      }
    }
    $i++;
  }

  // Backport from 7.x-2.x: Let other modules prevent this request from being
  // cached.
  if ($is_cacheable) {
    $reasons = module_invoke_all('authcache_request_exclude');
    if (!empty($reasons)) {
      $_authcache_info['no_cache_reason'] = reset($reasons);
      $is_cacheable = FALSE;
    }
  }
  return $is_cacheable;
}