* @version 1.0.1 (July 21, 2004) * @package AutoIndex */ class Logging { /** * @var string Filename of the log to write to */ private $filename; /** * @param string $filename The name of the log file */ public function __construct($filename) { $this -> filename = $filename; } /** * Writes data to the log file. * * @param string $extra Any additional data to add in the last column of the entry */ public function add_entry($extra = '') { if (LOG_FILE) { $h = @fopen($this -> filename, 'ab'); if ($h === false) { throw new ExceptionDisplay('Could not open log file for writing.' . ' Make sure PHP has write permission to this file.'); } global $dir, $ip, $host; $referrer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'N/A'); fwrite($h, date(DATE_FORMAT) . "\t" . date('H:i:s') . "\t$ip\t$host\t$referrer\t$dir\t$extra\n"); fclose($h); } } /** * @param int $max_num_to_display */ public function display($max_num_to_display) { if (!@is_file($this -> filename)) { throw new ExceptionDisplay('There are no entries in the log file.'); } $file_array = @file($this -> filename); if ($file_array === false) { throw new ExceptionDisplay('Could not open log file for reading.'); } $count_log = count($file_array); $num = (($max_num_to_display == 0) ? $count_log : min($max_num_to_display, $count_log)); $out = "
Viewing $num (of $count_log) entries.
\n" . '| # | Date | Time | ' . 'IP address | Hostname | ' . 'Referrer | Directory | ' . 'File downloaded or other info |
|---|---|---|---|---|---|---|---|
| ' . ($i + 1) . ' | '; $parts = explode("\t", rtrim($file_array[$count_log-$i-1], "\r\n"), 7); if (count($parts) !== 7) { throw new ExceptionDisplay('Incorrect format for log file on line ' . ($i + 1)); } for ($j = 0; $j < 7; $j++) { $cell = Url::html_output($parts[$j]); if ($j === 4 && $cell != 'N/A') { $cell = "$cell"; } $out .= '' . (($cell == '') ? ' | ' : "$cell"); } $out .= "