You are here

function fast_404_validate_path_sql in Fast 404 7

Check to see if a path exists using plain SQL functions.

@global array $databases

Return value

bool

1 call to fast_404_validate_path_sql()
fast_404_path_check in ./fast_404.inc
Check a Drupal path to see if it really exists and load a fast 404 if not.

File

./fast_404.inc, line 165

Code

function fast_404_validate_path_sql() {
  global $databases;

  // Databases aren't set. They need to be set to work.
  if (!isset($databases) || !is_array($databases['default']['default'])) {

    // We can't check this URL here. Return TRUE to let Drupal continue
    // bootstrapping.
    return TRUE;
  }
  $db = $databases['default']['default'];

  // PDO supports any database driver Drupal does and more.
  switch ($db['driver']) {
    case 'mysql':
    case 'pgsql':
      $pdo = new PDO($db['driver'] . ':host=' . $db['host'] . ';port=' . $db['port'] . ';dbname=' . $db['database'], $db['username'], $db['password']);
      break;
    case 'sqlite':
      $pdo = new PDO($db['driver'] . ':' . $db['database']);
      break;
    default:

      // The db driver is not supported by core therefore we allow it through.
      return TRUE;
      break;
  }
  $query = $_GET['q'];

  // Act only when locale is enabled.
  $stmt = $pdo
    ->prepare("SELECT status FROM system WHERE name='locale'");
  $stmt
    ->execute();
  $locale = $stmt
    ->fetchObject();

  // TODO: Need to actually check if url negotiator is enabled.
  if ($locale && $locale->status == 1) {
    $lang_prefixes = array();
    $locale_stmt = $pdo
      ->prepare("SELECT prefix FROM languages WHERE enabled = 1 AND prefix != ''");
    $locale_stmt
      ->execute();
    while ($locale_item = $locale_stmt
      ->fetchObject()) {
      $lang_prefixes[] = $locale_item->prefix;
    }
    $path_parts = explode('/', $query);
    if (in_array($path_parts[0], $lang_prefixes)) {
      if (count($path_parts) == 1) {
        return TRUE;
      }
      $query = substr($query, strlen($path_parts[0]) + 1);
    }
  }

  // Check if redirect module is installed.
  $stmt = $pdo
    ->prepare("SELECT status FROM system WHERE name = 'redirect'");
  $stmt
    ->execute();
  $redirect = $stmt
    ->fetchObject();
  if ($redirect && $redirect->status == 1) {
    $redirect_stmt = $pdo
      ->prepare("SELECT rid FROM redirect WHERE :path LIKE source");
    $redirect_stmt
      ->execute(array(
      ':path' => $query,
    ));
    if ($redirect_stmt
      ->fetch()) {
      return TRUE;
    }
  }
  $stmt = $pdo
    ->prepare("SELECT path FROM menu_router WHERE path = :path OR :concat_path LIKE path");
  $stmt
    ->execute(array(
    ':path' => $query,
    ':concat_path' => $query . '/%',
  ));
  $row = $stmt
    ->fetchAll(PDO::FETCH_ASSOC);

  // fetchAll() will always return an array so we simply check if it is empty.
  if (empty($row)) {
    $stmt = $pdo
      ->prepare("SELECT pid FROM url_alias WHERE alias = :alias");
    $stmt
      ->execute(array(
      ':alias' => $query,
    ));
    $alias = $stmt
      ->fetchAll(PDO::FETCH_ASSOC);
    return !empty($alias) > 0 ? TRUE : FALSE;
  }
  else {
    return TRUE;
  }
  return TRUE;
}