You are here

public function Fast404::extensionCheck in Fast 404 8.2

Same name and namespace in other branches
  1. 8 src/Fast404.php \Drupal\fast404\Fast404::extensionCheck()

Extension check.

A strategy for handling Fast 404 settings.

File

src/Fast404.php, line 58

Class

Fast404
Fast404: A value object for manager Fast 404 logic.

Namespace

Drupal\fast404

Code

public function extensionCheck() {

  // Get the path from the request.
  $path = $this->request
    ->getPathInfo();

  // Ignore calls to the homepage, to avoid unnecessary processing.
  if (!isset($path) || $path == '/') {
    return;
  }

  // Check to see if the URL is that of an image derivative.
  // If this file does not already exist, it will be handled via Drupal.
  if (strpos($path, 'styles/')) {

    // Check to see if we will allow anon users to access this page.
    if (!Settings::get('fast404_allow_anon_imagecache', TRUE)) {
      $cookies = $this->request->cookies
        ->all();

      // 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.
      if (isset($cookies) && is_array($cookies)) {
        foreach ($cookies as $cookie) {
          if (stristr($cookie, 'SESS')) {
            return;
          }
        }
      }
    }
    else {
      return;
    }
  }

  // If we are using URL whitelisting then determine if the current URL is
  // whitelisted before running the extension check.
  // Check for exact URL matches and assume it's fine if we get one.
  if (Settings::get('fast404_url_whitelisting', FALSE)) {
    $trimmed_path = ltrim($path, '/');
    $allowed = Settings::get('fast404_whitelist', []);
    if (in_array($trimmed_path, $allowed)) {

      // URL is whitelisted. Assumed good.
      return TRUE;
    }
  }

  // Check for whitelisted strings in the URL.
  $string_whitelist = Settings::get('fast404_string_whitelisting', FALSE);
  if (is_array($string_whitelist)) {
    foreach ($string_whitelist as $str) {
      if (strstr($path, $str) !== FALSE) {
        return;
      }
    }
  }
  $extensions = Settings::get('fast404_exts', '/^(?!\\/robots)^(?!\\/system\\/files).*\\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/i');

  // Determine if URL contains a blacklisted extension.
  if (isset($extensions) && preg_match($extensions, $path, $m)) {
    $this->loadHtml = FALSE;
    $this
      ->blockPath();
    return;
  }
}