You are here

function boost_init in Boost 6

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

Implementation of hook_init(). Performs page setup tasks if page not cached.

File

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

Code

function boost_init() {
  global $user, $base_path, $base_root, $base_url;

  // Force lowercase host name
  $base_root = strtolower($base_root);
  $parts = parse_url($base_url);
  $parts['host'] = strtolower($parts['host']);
  $base_url = boost_glue_url($parts);

  // Make sure this is the correct domain
  // Only works if $base_url is set in settings.php
  if (strcmp($parts['host'], strtolower($_SERVER['HTTP_HOST'])) != 0) {
    $GLOBALS['conf']['cache'] = CACHE_DISABLED;
    $GLOBALS['_boost_cache_this'] = FALSE;
    return;
  }

  // Check if Drupal is started from index.php - could cause problems with other
  // contrib modules like ad module.
  if (strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') !== FALSE) {
    $uid = isset($user->uid) ? $user->uid : 0;

    // Remove Boost cookie at logout if it still exists
    if (BOOST_AGGRESSIVE_COOKIE && isset($_COOKIE[BOOST_COOKIE]) && $uid == 0) {
      boost_set_cookie($uid, BOOST_TIME - 86400);
    }
    elseif (isset($_COOKIE[BOOST_COOKIE]) && $_COOKIE[BOOST_COOKIE] == '-1') {
      boost_set_cookie($uid, BOOST_TIME - 86400);
    }
    elseif (BOOST_AGGRESSIVE_COOKIE && !isset($_COOKIE[BOOST_COOKIE]) && $uid != 0) {
      boost_set_cookie($uid);
    }
  }

  // Disable all caches when nocache is set
  if (isset($_GET['nocache'])) {
    $GLOBALS['conf']['cache'] = CACHE_DISABLED;
    $GLOBALS['_boost_cache_this'] = FALSE;
    return;
  }

  // Make sure this is not a 404 redirect from the htaccesss file
  $path = explode($base_path, request_uri());
  array_shift($path);
  $path = implode($base_path, $path);
  $path = explode('?', $path);
  $path = array_shift($path);
  if ($path != '' && empty($_REQUEST['q']) && !stristr($path, '.php') && isset($_SERVER['REDIRECT_STATUS']) && $_SERVER['REDIRECT_STATUS'] == 404) {
    $GLOBALS['conf']['cache'] = CACHE_DISABLED;
    $GLOBALS['_boost_cache_this'] = FALSE;
    if (BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_init_404'])) {
      watchdog('boost', '404 received from server via redirect, going to send a 404. Info: !output', array(
        '!output' => boost_print_r($_SERVER, TRUE, TRUE),
      ));
    }
    boost_fast404();
  }

  //set variables
  if (empty($_REQUEST['q'])) {

    //front page
    $GLOBALS['_boost_path'] = '';
  }
  else {
    $GLOBALS['_boost_path'] = $_REQUEST['q'];
  }

  // Remove anchor tags from url.
  if (stristr($GLOBALS['_boost_path'], '#')) {
    $GLOBALS['_boost_path'] = array_shift(explode('#', $GLOBALS['_boost_path']));
  }

  // Make the proper filename for our query
  $GLOBALS['_boost_query'] = BOOST_CHAR;
  $query = array();
  foreach ($_GET as $key => $val) {
    if (BOOST_PAGER_CLEAN && $key == 'page') {
      $GLOBALS['_boost_path'] .= '/page/' . $val;
      continue;
    }
    if ($key != 'q' && $key != 'destination') {
      $query[$key] = $val;
    }
  }
  $GLOBALS['_boost_query'] .= str_replace('&', '&', urldecode(http_build_query($query)));
  if (!empty($user->uid)) {
    boost_set_cookie($user->uid);
    if (BOOST_DISABLE_CLEAN_URL) {
      $GLOBALS['conf']['clean_url'] = 0;
      db_query('TRUNCATE {cache_filter}');
      db_query('TRUNCATE {cache_menu}');
      cache_clear_all('*', 'cache_menu');
      cache_clear_all('*', 'cache_filter');
    }
  }

  // Make sure the page is/should be cached according to our current configuration
  if (strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') === FALSE || variable_get('site_offline', 0) || $_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD' || $_SERVER['SERVER_SOFTWARE'] === 'PHP CLI' || !variable_get('boost_enabled', CACHE_NORMAL) || !boost_is_cacheable($GLOBALS['_boost_path'])) {
    $GLOBALS['_boost_cache_this'] = FALSE;
    return;
  }

  // We only generate cached pages for anonymous visitors.
  if (empty($user->uid)) {
    if (variable_get('boost_enabled', CACHE_NORMAL) != CACHE_AGGRESSIVE) {
      $GLOBALS['conf']['cache'] = CACHE_DISABLED;
    }
    $GLOBALS['_boost_cache_this'] = TRUE;
    register_shutdown_function('_boost_ob_handler');
    ob_start();
  }
}