You are here

function _boost_ob_handler in Boost 6

Same name and namespace in other branches
  1. 5 boost.module \_boost_ob_handler()

PHP output buffering callback for static page caching.

1 string reference to '_boost_ob_handler'
boost_init in ./boost.module
Implementation of hook_init(). Performs page setup tasks if page not cached.

File

./boost.module, line 2544
Provides static file caching for Drupal text output. Pages, Feeds, ect...

Code

function _boost_ob_handler() {
  global $_boost;
  $buffer = ob_get_contents();

  // If Compressed data was given to us decompress it
  if (boost_headers_contain('gzip')) {
    $decompressed_buffer = gzinflate(substr(substr($buffer, 10), 0, -8));
  }
  else {
    $decompressed_buffer = $buffer;
  }

  // Ensure we're in the correct working directory, since some web servers (e.g. Apache) mess this up here.
  chdir(dirname($_SERVER['SCRIPT_FILENAME']));

  // Very late cache canceling
  $router_item = _boost_get_menu_router();
  if ($router_item['page_callback'] == 'search404_page' || $router_item['page_callback'] == 'fivestar_vote') {
    $GLOBALS['_boost_cache_this'] = FALSE;
  }

  // Check for PHP errors
  if ($error = _boost_page_have_error()) {
    $GLOBALS['_boost_cache_this'] = FALSE;
    if (BOOST_VERBOSE >= 3) {
      watchdog('boost', 'There are <strong>php errors</strong> on this page, preventing boost from caching. ERROR: <pre>%error</pre> !link <br /> !performance', array(
        '%error' => boost_print_r($error, TRUE),
        '!link' => l(t('Lookup Error Type'), 'http://php.net/errorfunc.constants'),
        '!performance' => l(t('Turn Off Error Checking'), 'admin/settings/performance/boost'),
      ), WATCHDOG_WARNING);
    }
  }

  // Check for drupal messages
  if (BOOST_HALT_ON_MESSAGES && isset($GLOBALS['_boost_message_count']) && $GLOBALS['_boost_message_count'] != 0) {
    $GLOBALS['_boost_cache_this'] = FALSE;
    if (BOOST_VERBOSE >= 3) {
      watchdog('boost', 'There are <strong>Drupal messages</strong> on this page, preventing boost from caching. MESSAGES: %msg <br /> !performance', array(
        '%msg' => $GLOBALS['_boost_message_count'],
        '!performance' => l(t('Turn Off Error Checking'), 'admin/settings/performance/boost'),
      ), WATCHDOG_WARNING);
    }
  }

  // Check the currently set content type and the HTTP response code. only cache
  // 'text/*' pages that were output with a 200 status number.
  if (!empty($decompressed_buffer)) {
    $status = boost_get_http_status();
    $types = boost_get_content_type();
    if (BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_ob_handler_info'])) {
      watchdog('boost', 'Debug: _boost_ob_handler() <br />HTTP Info: !status - !types <br />Path: !path <br />Content Container: !callback <br />Content Type: !type <br />ID: !id <br />Cache This: !cache.', array(
        '!status' => $status,
        '!types' => implode(', ', $types),
        '!path' => boost_file_path($GLOBALS['_boost_path']),
        '!callback' => $router_item['page_callback'],
        '!type' => $router_item['page_type'],
        '!id' => $router_item['page_id'],
        '!cache' => $GLOBALS['_boost_cache_this'] ? 'TRUE' : 'FALSE',
      ));
    }

    // Bail out if we can not cache
    if ($status != 200 || $GLOBALS['_boost_cache_this'] == FALSE) {
      return;
    }

    // Check for corret types and cache accordingly
    $types = array_pop($types);
    if (stristr($types, 'text/javascript') && BOOST_CACHE_JSON) {
      if (BOOST_ASYNCHRONOUS_OUTPUT && !headers_sent()) {
        boost_async_opp($buffer, FALSE, 'text/javascript; charset=utf-8');
      }
      boost_cache_set($GLOBALS['_boost_path'], $decompressed_buffer, BOOST_JSON_EXTENSION);
    }
    elseif ((stristr($types, 'application/rss') || stristr($types, 'text/xml') || stristr($types, 'application/rss+xml')) && BOOST_CACHE_XML) {
      if (BOOST_ASYNCHRONOUS_OUTPUT && !headers_sent()) {
        boost_async_opp($buffer, FALSE, 'text/xml; charset=utf-8');
      }
      boost_cache_set($GLOBALS['_boost_path'], $decompressed_buffer, BOOST_XML_EXTENSION);
    }
    elseif (stristr($types, 'text/html') && BOOST_CACHE_HTML) {
      if (BOOST_ASYNCHRONOUS_OUTPUT && !headers_sent()) {
        boost_async_opp($buffer, FALSE, 'text/html; charset=utf-8');
      }
      boost_cache_set($GLOBALS['_boost_path'], $decompressed_buffer, BOOST_FILE_EXTENSION);

      // html output requires special handling of the aggregated js/css files.
      boost_cache_css_js_files($decompressed_buffer);
    }
  }
}