You are here

function boost_is_cacheable in Boost 7

Same name and namespace in other branches
  1. 5 boost.api.inc \boost_is_cacheable()
  2. 6 boost.module \boost_is_cacheable()

Determines whether a given url can be cached or not by boost.

TODO: Add in support for the menu_item

Parameters

$parts: $parts

$request_type: May be 'status' to skip some checks in order to show the status block on the admin interface (otherwise we will always mention that the page is non-cacheable, since user is logged in). Please don't rely on this parameter if you are extending boost, this is likely to change in the future. Contact us if you use it.

Return value

$parts

2 calls to boost_is_cacheable()
boost_block_view_status in ./boost.blocks.inc
@file Prints the cache status of the currently displayed page.
boost_transform_url in ./boost.module
Given a URL give back eveything we know.

File

./boost.module, line 628
Caches generated output as a static file to be served directly from the webserver.

Code

function boost_is_cacheable($parts, $request_type = 'normal') {

  // Set local variables.
  $path = $parts['path'];
  $query = $parts['query'];
  $full = $parts['url_full'];
  $normal_path = $parts['normal_path'];
  $alias = $parts['path_alias'];
  $decoded = $parts['url_decoded'];

  // Never cache
  //  the user autocomplete/login/registration/password/reset/logout pages
  //  any admin pages
  //  comment reply pages
  //  node add page
  //  openid login page
  //  URL variables that contain / or \
  //  if incoming URL contains '..' or null bytes
  //  if decoded URL contains :// outside of the host portion of the url
  //  Limit the maximum directory nesting depth of the path
  //  Do not cache if destination is set.
  if ($normal_path === 'user' || !empty($query_array['destination']) || preg_match('!^user/(autocomplete|login|register|password|reset|logout)!', $normal_path) || 0 === strpos($normal_path, 'admin') || 0 === strpos($normal_path, 'comment/reply') || 0 === strpos($normal_path, 'node/add') || 0 === strpos($normal_path, 'openid/authenticate') || strpos($query, '/') !== FALSE || strpos($query, "\\") !== FALSE || strpos($full, '..') !== FALSE || strpos($full, "\0") !== FALSE || strpos($decoded, '://') !== FALSE || count(explode('/', $path)) > 10) {
    $parts['is_cacheable'] = FALSE;
    $parts['is_cacheable_reason'] = 'Core Drupal dynamic pages';
    return $parts;
  }

  // Check for reserved characters if on windows.
  // http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
  // " * : < > |
  $chars = '"*:<>|';
  if (FALSE !== stripos(PHP_OS, 'WIN') && preg_match("/[" . $chars . "]/", $full)) {
    $parts['is_cacheable'] = FALSE;
    $parts['is_cacheable_reason'] = 'Reserved characters on MS Windows';
    return $parts;
  }

  // Match the user's cacheability settings against the path.
  // See http://api.drupal.org/api/function/block_block_list_alter/7
  $visibility = variable_get('boost_cacheability_option', BOOST_VISIBILITY_NOTLISTED);
  $pages_setting = variable_get('boost_cacheability_pages', BOOST_CACHEABILITY_PAGES);
  if ($pages_setting) {

    // Convert path string to lowercase. This allows comparison of the same path
    // with different case. Ex: /Page, /page, /PAGE.
    $pages = drupal_strtolower($pages_setting);
    if ($visibility < BOOST_VISIBILITY_PHP) {

      // Convert the alias to lowercase.
      $path = drupal_strtolower($alias);

      // Compare the lowercase internal and lowercase path alias (if any).
      $page_match = drupal_match_path($path, $pages);
      if ($path != $normal_path) {
        $page_match = $page_match || drupal_match_path($normal_path, $pages);
      }

      // When 'boost_cacheability_option' has a value of 0 (BOOST_VISIBILITY_NOTLISTED),
      // Boost will cache all pages except those listed in 'boost_cacheability_pages'.
      // When set to 1 (BOOST_VISIBILITY_LISTED), Boost will only cache those
      // pages listed in 'boost_cacheability_pages'.
      $page_match = !($visibility xor $page_match);
    }
    elseif (module_exists('php')) {
      $page_match = php_eval($pages_setting);
    }
    else {
      $page_match = FALSE;
    }
  }
  else {
    $page_match = TRUE;
  }
  $parts['is_cacheable'] = $page_match;
  if (!$page_match) {
    $parts['is_cacheable_reason'] = 'Page excluded from cache by the include/exclude paths defined by site admin.';
  }
  if (!$parts['is_cacheable']) {
    return $parts;
  }

  // Invoke hook_boost_is_cacheable($path).
  $modules = boost_module_implements('boost_is_cacheable', 'boost');
  foreach ($modules as $module) {
    $result = module_invoke($module, 'boost_is_cacheable', $parts, $request_type);
    if ($result['is_cacheable'] === FALSE) {
      if (!isset($result['is_cacheable'])) {
        $result['is_cacheable_reason'] = 'Page excluded from cache by a third-party module.';
      }
      return $result;
    }
  }
  return $result;
}