You are here

function cdn_boot in CDN 7.2

Implements hook_boot().

File

./cdn.module, line 551

Code

function cdn_boot() {

  // Bail if the status is "disabled" (i.e. don't when "enabled" or "testing").
  if (variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED) === CDN_DISABLED) {
    return;
  }

  // Inspired by common.inc/_drupal_bootstrap_full().
  require_once DRUPAL_ROOT . '/includes/common.inc';
  require_once DRUPAL_ROOT . '/' . variable_get('path_inc', 'includes/path.inc');
  require_once DRUPAL_ROOT . '/includes/unicode.inc';

  // Prevent the CDN from returning content pages. We only want the CDN to
  // return static files like images, CSS files, JavaScript files, etc. By
  // default it will return anything. Since those static files aren't served by
  // Drupal.
  $redirect_url = FALSE;

  // Since SEO redirection is performed in hook_boot(), modules implementing a
  // custom SEO redirect callback need to implement hook_boot() as well or the
  // function won't be available yet.
  $seo_redirect_callback = variable_get(CDN_SEO_REDIRECT_CALLBACK_VARIABLE, CDN_SEO_REDIRECT_CALLBACK_DEFAULT);
  if (function_exists($seo_redirect_callback)) {
    $redirect_url = $seo_redirect_callback(current_path());
  }
  if ($redirect_url !== FALSE) {

    // A 301 is SEO friendly, as it tells the search engine what the canonical
    // URL is for this content.
    header('HTTP/1.0 301 Moved Permanently');

    // @see http://googlewebmastercentral.blogspot.com/2011/06/supporting-relcanonical-http-headers.html
    header('Link: <' . $redirect_url . '>; rel="canonical"');
    header('Location: ' . $redirect_url);

    // Informative header to simplify debugging.
    header('Drupal-CDN-Redirect: duplicate content prevention');

    // To ensure this redirect occurs immediately we don't use drupal_exit().
    exit;
  }
}