You are here

function boost_exit in Boost 7

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

Implements hook_exit().

File

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

Code

function boost_exit($destination = NULL) {
  global $_boost;

  // Bypass caching on redirects (issues #1176534 and #1957532).
  if (!empty($destination)) {
    $_boost['is_cacheable'] = FALSE;
  }

  // Bail out of caching.
  if (!isset($_boost['cache_this'])) {
    if (!isset($_boost['is_cacheable'])) {
      return;
    }
    elseif (!$_boost['is_cacheable']) {
      return;
    }
  }
  if (isset($_boost['cache_this']) && $_boost['cache_this'] == FALSE) {
    return;
  }
  elseif (!isset($_boost['is_cacheable']) || !$_boost['is_cacheable']) {
    return;
  }
  elseif ($_boost['menu_item']['status'] != 200) {
    return;
  }
  elseif (!drupal_page_is_cacheable()) {
    $_boost['is_cacheable'] = FALSE;
    return;
  }

  // Get the data to cache.
  $data = ob_get_contents();

  // Get header info.
  $_boost['header_info'] = boost_get_header_info();
  $_boost['matched_header_info'] = boost_match_header_attributes($_boost['header_info']);
  if ($_boost['matched_header_info']['enabled'] === FALSE) {
    return;
  }

  // Add note to bottom of content if possible.
  if ($_boost['matched_header_info']['comment_start'] && $_boost['matched_header_info']['comment_end']) {
    $expire = $_boost['matched_header_info']['lifetime_max'];
    $cached_at = date('Y-m-d H:i:s', REQUEST_TIME);
    $expires_at = date('Y-m-d H:i:s', REQUEST_TIME + $expire);
    $note = "\n" . $_boost['matched_header_info']['comment_start'] . 'Page cached by Boost @ ' . $cached_at . ', expires @ ' . $expires_at . ', lifetime ' . format_interval($expire) . $_boost['matched_header_info']['comment_end'];
    $data .= $note;
  }

  // Write data to a file.
  if ($_boost['filename']) {

    // Attach extension to filename.
    $_boost['filename'] .= '.' . $_boost['matched_header_info']['extension'];

    // Write to file.
    $_boost['filename'] = str_ireplace('%2F', '/', $_boost['filename']);

    // If the filename includes URL encoded characters create an extra copy of
    // the file with the characters decoded.
    if (rawurldecode($_boost['filename']) != $_boost['filename']) {
      boost_write_file(rawurldecode($_boost['filename']), $data);
    }
    boost_write_file($_boost['filename'], $data);

    // Gzip support.
    if (BOOST_GZIP && $_boost['matched_header_info']['gzip']) {

      // #1416214 https://drupal.org/node/1416214#comment-7225650
      // boost_write_file($_boost['filename'] . '.gz', gzencode($data, 9));
    }
  }
}