You are here

function boost_exit in Boost 6

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

Implementation of hook_exit(). Performs cleanup tasks.

For POST requests by anonymous visitors, this adds a dummy query string to any URL being redirected to using drupal_goto().

This is pretty much a hack that assumes a bit too much familiarity with what happens under the hood of the Drupal core function drupal_goto().

It's necessary, though, in order for any session messages set on form submission to actually show up on the next page if that page has been cached by Boost.

Parameters

$destination: URL that user will be sent to soon.

File

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

Code

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

  // Check for redirects.
  if (!empty($destination) && $_SERVER['REQUEST_METHOD'] != 'POST' && empty($_GET['destination'])) {

    // Make sure path functions are available.
    drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
    boost_redirect_handler($destination);
  }

  // 404, 403 dection and removal from the boost cache.
  // Only run if user is anonymous.
  if (empty($user->uid)) {
    $status = boost_get_http_status();
    if ($status == 404 || $status == 403) {

      // Bail out of caching
      $GLOBALS['_boost_cache_this'] = FALSE;

      // Get content type
      $types = boost_get_content_type();
      $types = array_pop($types);

      // Match extension
      if (stristr($types, 'text/javascript') || stristr($types, 'application/json')) {
        $extension = BOOST_JSON_EXTENSION;
      }
      elseif (stristr($types, 'application/rss') || stristr($types, 'text/xml') || stristr($types, 'application/rss+xml') || stristr($types, 'application/xml')) {
        $extension = BOOST_JSON_EXTENSION;
      }
      elseif (stristr($types, 'text/html') && BOOST_CACHE_HTML) {
        $extension = BOOST_FILE_EXTENSION;
      }

      // Get filename
      if (!empty($extension)) {
        $filename = boost_file_path($GLOBALS['_boost_path'], TRUE, $extension);

        // Remove dead item from the cache (file & db);
        if ($filename) {
          $files = array(
            array(
              'filename' => $filename,
            ),
          );
          boost_cache_kill($files, TRUE);
          boost_remove_db($files);
        }
      }
    }
  }

  // Check that hook_exit() was invoked by drupal_goto() for a POST request:
  // Check that we're dealing with an anonymous visitor. and that some
  // session messages have actually been set during this page request:
  if (!empty($destination) && $_SERVER['REQUEST_METHOD'] == 'POST' && empty($user->uid) && ($messages = drupal_set_message())) {
    $query_parts = parse_url($destination);

    // Add a nocache parameter to query. Such pages will never be cached
    $query_parts['query'] .= (empty($query_parts['query']) ? '' : '&') . 'nocache=1';

    // Rebuild the URL with the new query string.  Do not use url() since
    // destination has presumably already been run through url().
    $destination = boost_glue_url($query_parts);

    // Do what drupal_goto() would do if we were to return to it:
    if (BOOST_EXIT_IN_HOOK_EXIT) {

      // FIXME: call any remaining exit hooks since we're about to terminate?
      exit(header('Location: ' . $destination));
    }
    else {
      header('Location: ' . $destination);
    }
  }

  // Set watchdog error if headers already sent
  if (BOOST_ASYNCHRONOUS_OUTPUT && isset($GLOBALS['_boost_cache_this']) && $GLOBALS['_boost_cache_this'] && headers_sent($filename, $linenum) && !boost_headers_contain('Location: ') && BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_exit_headers'])) {
    watchdog('boost', 'boost_exit() Debug: Headers already sent in @filename on line @linenum. Asynchronous Operation will not be used.', array(
      '@filename' => $filename,
      '@linenum' => $linenum,
    ));
  }
}