<?
/*
	This PHP script is licensed under the GPL
	
	Author: Terence Yim
	E-mail: chtyim@gmail.com
	Homepage: http://web-news.sourceforge.net
*/

	$header = $MIME_Message->get_main_header();
	$parts = $MIME_Message->get_all_parts();
	
	if (is_requested("art_group")) {
		$group = get_request("art_group");
	} else {
		$group = $_SESSION["newsgroup"];
	}
?>

<font face="<? echo $font_family; ?>">
<table cellpadding="5" cellspacing="0" border="0" align="left" width="100%">
	<tr>
		<td bgcolor="<? echo $primary_color; ?>" width="15%" valign="top"><font size="<? echo $font_size; ?>"><b><? echo $messages_ini["text"]["subject"]; ?></b></font></td>
		<td bgcolor="<? echo $secondary_color; ?>"><font size="<? echo $font_size; ?>"><? echo htmlescape($header["subject"]);?></td>
	</tr>
	<tr>
		<td bgcolor="<? echo $primary_color; ?>" width="15%" valign="top"><font size="<? echo $font_size; ?>"><b><? echo $messages_ini["text"]["from"]; ?></b></font></td>
		<td bgcolor="<? echo $secondary_color; ?>"><font size="<? echo $font_size; ?>">
<?
		if (is_requested("post") || $_SESSION["auth"]) {
			echo "<a href=\"mailto:".htmlescape($header["from"]["email"])."\">";
		}
		echo htmlescape($header["from"]["name"]);
		
		if (is_requested("post") || $_SESSION["auth"]) {
			echo htmlescape(" <".$header["from"]["email"].">")."</a>";
		}
?>
		</td>
	</tr>
	<tr>
		<td bgcolor="<? echo $primary_color; ?>" width="15%" valign="top"><font size="<? echo $font_size; ?>"><b><? echo $messages_ini["text"]["date"]; ?></b></font></td>
		<td bgcolor="<? echo $secondary_color; ?>"><font size="<? echo $font_size; ?>"><? echo $header["date"]; ?></td>
	</tr>
	<tr>
		<td bgcolor="<? echo $primary_color; ?>" width="15%" valign="top"><font size="<? echo $font_size; ?>"><b><? echo $messages_ini["text"]["newsgroups"]; ?></b></font></td>
		<td bgcolor="<? echo $secondary_color; ?>"><font size="<? echo $font_size; ?>"><? echo $header["newsgroups"]; ?></td>
	</tr>
<!--
	<tr>
		<td bgcolor="<? echo $primary_color; ?>" width="15%" valign="top"><font size="<? echo $font_size; ?>"><b>Content-Type</b></font></td>
		<td bgcolor="<? echo $secondary_color; ?>"><font size="<? echo $font_size; ?>"><? echo $header["content-type"]; ?></td>
	</tr>
-->
<?
	if (sizeof($parts) > 1) {	// We've got attachment
		echo "<tr>\r\n";
		echo "<td bgcolor=\"$primary_color\" width=\"15%\" valign=\"top\"><font size=\"$font_size\"><b>".$messages_ini["text"]["attachments"]."</b></font></td>\r\n";
		echo "<td bgcolor=\"$secondary_color\"><font size=\"$font_size\">\r\n";
		$attach_file = "";
		for ($i = 1;$i < sizeof($parts);$i++) {
			if (($i != 1) && (($i - 1) % 5 == 0)) {
				$attach_file .= "<br>\r\n";
			}
			if (strcmp($parts[$i]["filename"], "") != 0) {
				$attach_file .= "<a href=\"newsgroups.php?art_group=".urlencode($group)."&message_id=".$article_id."&attachment_id=".$i."\" target=\"_blank\">".$parts[$i]["filename"]."</a>,&nbsp;";
			} else {
				$attach_file .= "<a href=\"newsgroups.php?art_group=".urlencode($group)."&message_id=".$article_id."&attachment_id=".$i."\" target=\"_blank\">".$messages_ini["text"]["no_name"]." $i</a>,&nbsp;";
			}
		}
		if (strlen($attach_file) > 0) {
			$attach_file = substr($attach_file, 0, strlen($attach_file) - 7);
		}
		echo $attach_file;
		echo "</td>\r\n";
		echo "</tr>\r\n";
	}

	$count = 0;
	
	foreach ($parts as $part) {
		if (stristr($part["header"]["content-type"], "text/html")) {	// HTML
			$body = filter_html(decode_message_content($part));

			// Replace the image link for internal resources
			$content_map = $MIME_Message->get_content_map();
			$search_array = array();
			$replace_array = array();
			foreach ($content_map as $cid => $aid) {
				$cid = substr($cid, 1, strlen($cid) - 2);
				$search_array[] = "cid:".$cid;
				$replace_array[] = "newsgroups.php?art_group=".urlencode($group)."&message_id=".$article_id."&attachment_id=".$aid;
			}
	
			$body = str_replace($search_array, $replace_array, $body);
			
			echo "<tr><td colspan=\"2\"><div>".$body."</div><br></td></tr>";
		} elseif (stristr($part["header"]["content-type"], "text")) {	// Treat all other form of text as plain text
			echo "<tr><td colspan=\"2\"><font size=\"$font_size\"><br>";
			$body = decode_message_content($part);
			$body = htmlescape($body);
			$body = preg_replace(array("/\r\n/", "/(^&gt;.*)/m", "/\t/", "/  /"),
										array("<br>\r\n", "<i>$1</i>", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;&nbsp;"),
										add_html_links($body));
			echo $body."<br></td></tr>";
		} elseif (preg_match("/^image\/(gif|jpeg|pjpeg)/i", $part["header"]["content-type"])) {
			echo "<tr><td colspan=\"2\" align=\"center\">";
			echo "<hr width=\"100%\"><br>";
			echo "<img src=\"newsgroups.php?art_group=".urlencode($group)."&message_id=$article_id&attachment_id=$count\" border=\"0\">";
			echo "<br></td></tr>\r\n";

		}
		$count++;
	}	

    if ($message_node) {
?>
<tr><td colspan=2">
<table cellpadding="0" cellspacing="1" border="0" align="left" width="100%">
<tr>
    <td colspan="3" bgcolor="<? echo $primary_color;?>"><b><font size="<? echo $font_size;?>">
        <? echo $messages_ini["text"]["recent_thread"];?>
    </font></b></td>    
</tr>
<tr>
    <td><font size="<? echo ($font_size - 1);?>">&nbsp;</font></td>
</tr>
<?
        display_tree($message_node->get_children(), 0, "", FALSE, $article_id);
?>
</table>
</td></tr>
<?
    }
?>
</table>
</font>
<br><br>