<?php
/**
* This scpript can call directly or access as include and executed as a function.
* This script will send a standarded error 404 (Not found) an is nice for real or faked 404-errors
*
* Args:
* 1. string  $logentry  - if false (zero, null, 0 ...) nothing will be logged
*                       - if true this will be logged as a notice
*
* 2. boolean $exit      - if true php will stopped after show the error
*                       - if false php will stopped after show the error
*
* If you  have heders send this will die with a error.
* If you using a ob-buffer he will be claered. It's a good idea to use the ob-buffers if you want to fake the Error 404.
**/

error_reporting(0);

$error404calledDirect = ( $_SERVER['SCRIPT_FILENAME'] == __FILE__ );

if ( $error404calledDirect ) { error404('directly called', true); }

function error404($logentry=false, $exit=true) {
    if ( headers_sent() ) {
        trigger_error('Es kann kein Status 404 gesendet werden, weil zuvor Daten gesendet wurden.', E_USER_ERROR);
    } else {
        ob_end_clean();
        header("HTTP/1.0 404 Not Found");
?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL <?=htmlspecialchars(urldecode($_SERVER['REQUEST_URI']))?> was not found on this server.</p>
<hr>
<address><?=$_SERVER['SERVER_SOFTWARE']?> Server at <?=htmlspecialchars($_SERVER['HTTP_HOST'])?> Port <?=htmlspecialchars($_SERVER['SERVER_PORT'])?></address>
</body></html>
<?php
    }
    if ($logentry) {
        error_log($logentry);
    }
    if ( $exit ) {
        exit;
    } else {
        return true;
    }
}
