View source
<?php
function fast_404_ext_check() {
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;
}
if (strpos($_SERVER[$server_var], 'imagecache')) {
if (variable_get('fast_404_allow_anon_imagecache', TRUE)) {
return TRUE;
}
else {
$found_session = FALSE;
foreach ($_COOKIE as $k => $v) {
if (stristr($k, 'SESS')) {
$found_session = TRUE;
break;
}
}
if ($found_session) {
return TRUE;
}
}
}
if (strpos($_SERVER[$server_var], '/advagg_')) {
return TRUE;
}
if (variable_get('fast_404_url_whitelisting', FALSE)) {
$allowed = variable_get('fast_404_whitelist', array());
if (in_array($_SERVER[$server_var], $allowed)) {
return TRUE;
}
}
$exts = variable_get('fast_404_exts', '/\\.(txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp|)$/i');
if ($exts && preg_match($exts, $_SERVER[$server_var], $m)) {
fast_404_error_return();
}
define('FAST_404_EXT_CHECKED', TRUE);
}
function fast_404_path_check() {
$valid = TRUE;
if (variable_get('fast_404_path_check', FALSE) && !empty($_GET['q'])) {
if (function_exists('db_query')) {
$valid = fast_404_validate_path_drupal();
}
else {
$valid = fast_404_validate_path_mysql();
}
}
if (!$valid) {
fast_404_error_return();
}
define('FAST_404_PATH_CHECKED', TRUE);
}
function fast_404_validate_path_drupal() {
if (db_result(db_query("SELECT name FROM {system} WHERE type = 'module' AND status = 1 AND name = 'path_redirect'"))) {
if (db_result(db_query("SELECT rid FROM {path_redirect} WHERE '%s' LIKE source", $_GET['q']))) {
return TRUE;
}
}
$sql = "SELECT path FROM {menu_router} WHERE '%s' LIKE path OR '%s' LIKE CONCAT(path,'%')";
$res = db_result(db_query($sql, $_GET['q'], $_GET['q']));
if ($res) {
return TRUE;
}
else {
$sql = "SELECT pid FROM {url_alias} WHERE '%s' LIKE dst OR '%s' LIKE CONCAT(dst,'%')";
$res = db_result(db_query($sql, $_GET['q'], $_GET['q']));
return $res == 0 ? FALSE : TRUE;
}
return FALSE;
}
function fast_404_validate_path_mysql() {
global $db_url, $db_prefix;
$db_conn = !is_array($db_url) ? $db_url : (isset($db_url['default']) ? $db_url['default'] : NULL);
if (!$db_conn) {
return TRUE;
}
$sql = "SELECT path FROM menu_router WHERE '%s' LIKE CONCAT(path,'%')";
$sql2 = "SELECT pid FROM url_alias WHERE '%s' LIKE CONCAT(dst,'%')";
$sql3 = "SELECT rid FROM path_redirect WHERE '%s' LIKE source";
$url = parse_url($db_conn);
$url['user'] = urldecode($url['user']);
$url['pass'] = isset($url['pass']) ? urldecode($url['pass']) : '';
$url['host'] = urldecode($url['host']);
$url['path'] = str_replace('/', '', urldecode($url['path']));
if (strstr($db_conn, 'mysqli')) {
$conn = mysqli_connect($url['host'], $url['user'], $url['pass'], $url['path'], $url['port']);
if (mysqli_fetch_row(mysqli_query($conn, "SELECT name FROM system WHERE type = 'module' AND status = 1 AND name = 'path_redirect'"))) {
$sql3 = str_replace('%s', mysqli_real_escape_string($conn, $_GET['q']), $sql3);
if (mysqli_fetch_row(mysqli_query($conn, $sql3))) {
return TRUE;
}
}
$sql = str_replace('%s', mysqli_real_escape_string($conn, $_GET['q']), $sql);
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_row($res);
if (!is_array($row)) {
$sql2 = str_replace('%s', mysqli_real_escape_string($conn, $_GET['q']), $sql2);
$res = mysqli_query($conn, $sql2);
$row = mysqli_fetch_row($res);
return is_array($row) > 0 ? TRUE : FALSE;
}
else {
return TRUE;
}
}
elseif (strstr($db_conn, 'mysql')) {
if (isset($url['port'])) {
$url['host'] = $url['host'] . ':' . $url['port'];
}
$conn = mysql_connect($url['host'], $url['user'], $url['pass']);
mysql_select_db($url['path'], $conn);
if (mysql_fetch_row(mysql_query("SELECT name FROM system WHERE type = 'module' AND status = 1 AND name = 'path_redirect'", $conn))) {
$sql3 = str_replace('%s', mysql_escape_string($_GET['q']), $sql3);
if (mysql_fetch_row(mysql_query($sql3))) {
return TRUE;
}
}
$sql = str_replace('%s', mysql_escape_string($_GET['q']), $sql);
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if (!is_array($row)) {
$sql2 = str_replace('%s', mysql_escape_string($_GET['q']), $sql2);
$res = mysql_query($sql2);
$row = mysql_fetch_array($res);
return is_array($row) > 0 ? TRUE : FALSE;
}
else {
return TRUE;
}
}
else {
return TRUE;
}
return TRUE;
}
function fast_404_error_return() {
header('HTTP/1.0 404 Not Found');
$fast_404_html = variable_get('fast_404_html', '<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;
}