You are here

public function Fast404::pathCheck in Fast 404 8

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

Path check.

Since the path check is a lot more aggressive in its blocking we should actually check that the user wants it to be done.

File

src/Fast404.php, line 132

Class

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

Namespace

Drupal\fast404

Code

public function pathCheck() {
  if (!Settings::get('fast404_path_check', FALSE)) {
    return;
  }

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

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

  // If we have a database connection we can use it, otherwise we might be
  // initialising it. We remove '/' from the list of possible patterns as it
  // exists in the router by default. This means that the query would match
  // any path (/%) which is undesirable.
  $sql = "SELECT pattern_outline FROM {router} WHERE :path LIKE CONCAT(pattern_outline, '%') AND pattern_outline != '/'";
  $result = Database::getConnection()
    ->query($sql, [
    ':path' => $path,
  ])
    ->fetchField();
  if ($result) {
    return;
  }

  // Check the URL alias table for anything that's not a standard Drupal path.
  // Remove any trailing slash found in the request path.
  $path_noslash = rtrim($path, '/');
  $sql = "SELECT pid FROM {url_alias} WHERE alias = :alias";
  $result = Database::getConnection()
    ->query($sql, [
    ':alias' => $path_noslash,
  ])
    ->fetchField();
  if ($result) {
    return;
  }

  // If we get to here it means nothing has matched the request so we assume
  // it's a bad path and block it.
  $this
    ->blockPath();
}