You are here

function boost_init in Boost 5

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

Implementation of hook_init(). Performs page setup tasks.

File

./boost.module, line 95
Provides static page caching for Drupal.

Code

function boost_init() {

  // Stop right here unless we're being called for an ordinary page request
  if (strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') === FALSE) {
    return;
  }

  // TODO: check interaction with other modules that use ob_start(); this
  // may have to be moved to an earlier stage of the page request.
  if (!variable_get('cache', CACHE_DISABLED) && BOOST_ENABLED) {

    // We only support GET requests by anonymous visitors:
    global $user;
    if (empty($user->uid) && $_SERVER['REQUEST_METHOD'] == 'GET') {

      // Make sure no query string (in addition to ?q=) was set, and that
      // the page is cacheable according to our current configuration:
      if (count($_GET) == 1 && boost_is_cacheable($_GET['q'])) {

        // In the event of errors such as drupal_not_found(), GET['q'] is
        // changed before _boost_ob_handler() is called. Apache is going to
        // look in the cache for the original path, however, so we need to
        // preserve it.
        $GLOBALS['_boost_path'] = $_GET['q'];
        ob_start('_boost_ob_handler');
      }
    }
  }

  // Executed when saving Drupal's settings:
  if (!empty($_POST['edit']) && $_GET['q'] == 'admin/settings') {

    // Forcibly disable Drupal's built-in SQL caching to prevent any conflicts of interest:
    variable_set('cache', CACHE_DISABLED);

    // TODO: handle 'offline' site maintenance settings.
    $old = variable_get('boost', '');
    if (!empty($_POST['edit']['boost'])) {

      // Ensure the cache directory exists or can be created
      file_check_directory($_POST['edit']['boost_file_path'], FILE_CREATE_DIRECTORY, 'boost_file_path');
    }
    else {
      if (!empty($old)) {

        // the cache was previously enabled
        if (boost_cache_expire_all()) {
          drupal_set_message('Static cache files deleted.');
        }
      }
    }
  }
}