<?php

class mailformatter
{

	var $bgcolor = "#000000";
	var $from = "#888822";
	var $level1header = "#44dd44";
	var $level1content = "#dddd44";
	var $resentheader = "#dddd44";
	var $resentcontent = "#888822";
	var $level2header = "#228822";
	var $level2content = "#888822";
	var $blankheader = "#888822";
	var $quote = "#228888";
	var $sig = "#882288";
	var $body = "#faebd7";
	
	var $insig = false;
	var $inhead = false;
	var $data = "";
	var $ct = "";
	
	var $linkcolor = "";

	function 
	mailformatter ($file) {
		$this->file=$file;
	}

	function 
	do_head ($line) {
		$line = ereg_replace ("^(From .*)\n$", "<font color='".$this->from."'>\\1</font>\n", $line);
		if (eregi ("^(To|From|Cc|Bcc|Date|Subject|Newsgroups):", $line)) {
			$line = ereg_replace ("^([^:[:blank:]]+:)([[:blank:]]+)(.*)\n$",
				 "<b><font color='".$this->level1header."'>\\1</font></b>\\2<font color='".$this->level1content."'>\\3</font>\n", $line);
		} else if (eregi ("^Resent-[^:]+:", $line)) {
			$line = ereg_replace ("^([^:[:blank:]]+:)([[:blank:]]+)(.*)\n$",
				 "<b><font color='".$this->resentheader."'>\\1</font></b>\\2<font color='".$this->resentcontent."'>\\3</font>\n", $line);
		} else if (eregi ("^(User-Agent|Organization|List-Id|Reply-To|In-Reply-To|Message-Id|References):", $line)) {
			$line = ereg_replace ("^([^:[:blank:]]+:)([[:blank:]]+)(.*)\n$",
				 "<b><font color='".$this->level2header."'>\\1</font></b>\\2<font color='".$this->level2content."'>\\3</font>\n", $line);
		} else if (eregi ("Content-Transfer-Encoding: quoted-printable", $line)){
			$this->quopri = true;
			$line = "";
		} else {
			$line = "";
		}
		$line = ereg_replace ("^([[:blank:]]+)(.*)\n$",
				 "\\1<font color='".$this->blankheader."'>\\2</font>\n", $line);
		return $line;
	}

	function 
	do_body ($line) {

		if ($line == "-- \n") {
			$this->insig = true;
		}
		if (eregi ("Content-Transfer-Encoding: quoted-printable", $line)){
			$this->quopri = true;
		}
		if (!$this->insig) {
			$this->linkcolor = $this->body;
			$line = ereg_replace ("^((&gt;|\|).*)\n", "<font color='".$this->quote."'>\\1</font>\n", $line);
		}
		if ($this->insig) {
			$this->linkcolor = $this->sig;
			$line = ereg_replace ("^(.*)\n", "<font color='".$this->sig."'>\\1</font>\n", $line);
		}

		if (eregi($this->quote,$line)) {
			$this->linkcolor = $this->quote;
		}
		$line = ereg_replace("(http://[^ <>]+)","<a style=\"color:".$this->linkcolor."; text-decoration: none\" href=\"\\1\">\\1</a>",$line);
		return $line;
	}

	function 
	parse () {
		$f = fopen ($this->file, 'r');
		if ($f) {
			$this->ct = "text/html";
			$this->data .= "<html><body bgcolor='".$this->bgcolor."' text='".$this->body."'><pre>\n";
			$this->inhead = true;

			while (!feof ($f)) {
				$line = htmlspecialchars (fgets ($f, 4096));
				if ($line == "\n") {
					$this->inhead = false;
				}
				if (ereg ("^From ", $line) && !$this->inhead) {
					$this->inhead = true;
					$this->insig = false;
					$this->quopri = false;
				}
				if ($this->quopri) {
					if(eregi("=\n$", $line)) {
						$line .= htmlspecialchars (fgets ($f, 4096));
					}
					$line = quoted_printable_decode($line);
				}
				if ($this->inhead) {
					$line = $this->do_head ($line);
				} else {
					$line = $this->do_body ($line);
				}
				$this->data .= $line;
			}
			$this->data .= "</pre></body></html>\n";
			fclose ($f);
		} else {
			$this->ct = "text/plain";
			$this->data = "Error opening file";
		}
	}

	function 
	dump () {
		header("Content-Type: ".$this->ct);
		echo $this->data;
	}

}

$file = $_GET['name'];

if (ereg ("^mail/[[:alnum:]-]+$", $file)) {
	$m = new mailformatter($file);
	$m->parse();
	$m->dump();
} else {
	header("Content-Type: text/plain");
	echo "Denied.";
}

?>
