You are here

function fast_404_ext_check in Fast 404 6

Same name and namespace in other branches
  1. 7 fast_404.inc \fast_404_ext_check()
1 call to fast_404_ext_check()
fast_404_boot in ./fast_404.module
@desc Run the 404 check on boot. This function gets called during the DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE stage of drupal_bootstrap. This means that if there is already cached content for the current URL, it will be delivered before this hook is reached.

File

./fast_404.inc, line 3

Code

function fast_404_ext_check() {

  // Work out which $_SERVER variable to work from.  We'll ignore calls to the
  // homepage, to avoid unnecessary processing.
  if (!empty($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '/' && $_SERVER['QUERY_STRING'] != '/index.php') {
    $server_var = 'QUERY_STRING';
  }
  elseif (!empty($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != '/index.php') {
    $server_var = 'REQUEST_URI';
  }
  else {
    return TRUE;
  }

  // Check to see if the URL is an imagecache URL. Those are handled via Drupal
  if (strpos($_SERVER[$server_var], 'imagecache')) {

    // Check to see if we will allow anon users to access this url
    if (variable_get('fast_404_allow_anon_imagecache', TRUE)) {

      // If anonymous can access then anyone can and we'll just pass on by
      return TRUE;
    }
    else {

      // This is if you would like to only let your authenticated users genterate the
      // imagecache variations. Useful for sites like magazines where content
      // editors will view all the content and pre-generate the variations
      // as a part of their process.
      $found_session = FALSE;

      // At this stage of the game we don't know if the user is logged in via
      // regular function calls. Simply look for a session cookie. If we find
      // one we'll assume they're logged in
      foreach ($_COOKIE as $k => $v) {
        if (stristr($k, 'SESS')) {
          $found_session = TRUE;
          break;
        }
      }

      // Found a session. We're going to assume they're logged in
      if ($found_session) {
        return TRUE;
      }
    }
  }

  // Check to see if the URL is an advagg URL. Those are handled via Drupal
  if (strpos($_SERVER[$server_var], '/advagg_')) {

    // We're allowing anyone to hit non-existing advagg URLs (default behavior)
    return TRUE;
  }

  // If we are using URL whitelisting then determine if the current URL is whitelisted
  // before running the extension check
  if (variable_get('fast_404_url_whitelisting', FALSE)) {
    $allowed = variable_get('fast_404_whitelist', array());
    if (in_array($_SERVER[$server_var], $allowed)) {
      return TRUE;

      // URL is whitelisted. Assumed good.
    }
  }

  // Load up the blacklisted extensions
  $exts = variable_get('fast_404_exts', '/\\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp|)$/i');

  // Determine if URL is in blacklisted extensions
  if ($exts && preg_match($exts, $_SERVER[$server_var], $m)) {
    fast_404_error_return();
  }
  define('FAST_404_EXT_CHECKED', TRUE);
}