View source
<?php
function fast_404_ext_check() {
if (!variable_get('fast_404_extension_check', TRUE)) {
return TRUE;
}
if (drupal_is_cli()) {
return TRUE;
}
$uri = request_uri();
$path = parse_url('http://example.com' . $uri, PHP_URL_PATH);
if ($path == '/') {
return TRUE;
}
if (strpos($path, 'styles/')) {
if (!variable_get('fast_404_allow_anon_imagecache', TRUE)) {
$found_session = FALSE;
foreach ($_COOKIE as $k => $v) {
if (stristr($k, 'SESS')) {
$found_session = TRUE;
break;
}
}
if ($found_session) {
return TRUE;
}
}
else {
return TRUE;
}
}
if (variable_get('fast_404_url_whitelisting', FALSE)) {
$allowed = variable_get('fast_404_whitelist', array());
if (in_array($path, $allowed)) {
return TRUE;
}
}
if (is_array(variable_get('fast_404_string_whitelisting', FALSE))) {
foreach (variable_get('fast_404_string_whitelisting', array()) as $str) {
if (strstr($path, $str) !== FALSE) {
return TRUE;
}
}
}
$exts = variable_get('fast_404_exts', '/^(?!robots).*\\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/i');
if ($exts && preg_match($exts, $path, $m)) {
fast_404_error_return(FALSE, variable_get('fast_404_return_gone', FALSE));
}
define('FAST_404_EXT_CHECKED', TRUE);
}
function fast_404_path_check() {
$valid = TRUE;
$query = $_GET['q'];
if (variable_get('fast_404_path_check', FALSE) && !empty($query)) {
if (function_exists('db_query')) {
$valid = fast_404_validate_path_drupal();
}
else {
$valid = fast_404_validate_path_sql();
}
}
if (!$valid) {
fast_404_error_return(TRUE, variable_get('fast_404_return_gone', FALSE));
}
define('FAST_404_PATH_CHECKED', TRUE);
}
function fast_404_validate_path_drupal() {
$query = $_GET['q'];
if (db_query("SELECT name FROM {system} WHERE type = 'module' AND status = 1 AND name = 'redirect'")
->fetchField()) {
if (db_query("SELECT rid FROM {redirect} WHERE :path = source", array(
':path' => $query,
))
->fetchField()) {
return TRUE;
}
}
if (db_query("SELECT status FROM {system} WHERE type = 'module' AND name = 'locale' AND status = 1")
->fetchField()) {
$prefixes = db_query("SELECT prefix FROM {languages} WHERE enabled = 1 AND prefix != ''");
$lang_prefixes = $prefixes
->fetchCol();
$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);
}
}
$sql = "SELECT path FROM {menu_router} WHERE :path LIKE CONCAT(path, '%')";
$res = db_query($sql, array(
':path' => $query,
))
->fetchField();
if ($res) {
return TRUE;
}
else {
$res = db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(
':alias' => $query,
))
->fetchField();
return $res == 0 ? FALSE : TRUE;
}
return FALSE;
}
function fast_404_validate_path_sql() {
global $databases;
if (!isset($databases) || !is_array($databases['default']['default'])) {
return TRUE;
}
$db = $databases['default']['default'];
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:
return TRUE;
break;
}
$query = $_GET['q'];
$stmt = $pdo
->prepare("SELECT status FROM system WHERE name='locale'");
$stmt
->execute();
$locale = $stmt
->fetchObject();
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);
}
}
$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);
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;
}
function fast_404_error_return($load_html = FALSE, $return_gone = FALSE) {
if ($return_gone) {
header((variable_get('fast_404_HTTP_status_method', 'mod_php') == 'FastCGI' ? 'Status:' : 'HTTP/1.0') . ' 410 Gone');
}
else {
header((variable_get('fast_404_HTTP_status_method', 'mod_php') == 'FastCGI' ? 'Status:' : 'HTTP/1.0') . ' 404 Not Found');
}
if (($load_html || variable_get('fast_404_HTML_error_all_paths', FALSE)) && file_exists(variable_get('fast_404_HTML_error_page', FALSE))) {
$fast_404_html = @file_get_contents(variable_get('fast_404_HTML_error_page', FALSE));
}
if (!isset($fast_404_html) || empty($fast_404_html)) {
$fast_404_html = variable_get('fast_404_html', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>');
}
print strtr($fast_404_html, array(
'@path' => check_plain(request_uri()),
));
exit;
}