01 <?php
02     function WriteLogData($LogExt, $LogArray, $LockFname) {
03         global $_VARIABLE;
04 
05         SetLockFile($LockFname);
06 
07         $TodayStr = GetDateString();
08         $LogDataFname = "{$_VARIABLE['LogDir']}/$TodayStr.$LogExt";
09         if(!$LogDataHandle = fopen($LogDataFname, "c+")) {
10             die("fopen(ERROR): $LogDataFname");
11         }
12         flock($LogDataHandle, LOCK_EX);
13 
14         fseek($LogDataHandle, 0, SEEK_END);
15 
16         fputs($LogDataHandle, implode("\t", $LogArray) . "\n");
17 
18         flock($LogDataHandle, LOCK_UN);
19         fclose($LogDataHandle);
20 
21         ResetLockFile($LockFname);
22 
23         return;
24     }
25 
26     if(isset($_GET['code'])) {
27         $ErrorCode = $_GET['code'];
28     }
29     $RequestUri = $_SERVER['REQUEST_URI'];
30 
31     switch($ErrorCode) {
32         case 403:
33             $Title = "403 Forbidden";
34             $Heading = "Forbidden";
35             $Message = "You don't have permission to access $RequestUri on this server.";
36             break;
37         case 404:
38             $Title = "404 Not Found";
39             $Heading = "Not Found";
40             $Message = "The requested URL $RequestUri was not found on this server.";
41             break;
42     }
43 
44     $LogExt = "err";
45     if(CheckDateChange($LogExt)) {
46         DeleteLogDataFiles($LogExt);
47     }
48 
49     $RemoteIP = $_SERVER['REMOTE_ADDR'];
50     $ATime = GetTimeString();
51     $RemoteHost = $_SERVER['REMOTE_HOST'];
52     if(!$RemoteHost) {
53         $RemoteHost = gethostbyaddr($RemoteIP);
54     }
55     $Referer = $_SERVER['HTTP_REFERER'];
56 
57     $LogArray = array(
58         $RemoteIP,
59         $ATime,
60         $RemoteHost,
61         $Referer,
62         $ErrorCode,
63         $RequestUri,
64     );
65     WriteLogData($LogExt, $LogArray, "error.lock");
66 
67     list($ApacheVer) = explode(" ", $_SERVER['SERVER_SOFTWARE']);
68     $ServerName = $_SERVER['SERVER_NAME'];
69     $ServerPort = $_SERVER['SERVER_PORT'];
70 
71     print <<<EOT
72 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
73 <HTML>
74 <HEAD>
75 <TITLE>$Title</TITLE>
76 </HEAD>
77 <BODY>
78 <H1>$Heading</H1>
79 <P>$Message</P>
80 <HR>
81 <ADDRESS>$ApacheVer Server at $ServerName Port $ServerPort</ADDRESS>
82 </BODY>
83 </HTML>
84 EOT;
85 ?>