<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

#
#
#
$DEBUG=0;
#
#
#
include_once("auth.php");
include("email_functions.php");
include("email_config.php");
include("email_header.php");

$PHP_SELF = 'email_message.php';

if($id && e_connect()):
  $header = imap_headerinfo($mbox, $id);
$mail_uniq = md5($header->subject . $header->date);
$date =  dateFormat(strtotime($header->date),"datetime");
$subject = $header->subject ? decode_subject($header->subject) : "[".$lang_text["wbm_nosubject"]."]";
?>
<table width="430" class="tblbgcolor" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<tr><td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><b><? echo htmlspecialchars($subject); ?></b></td></tr>
<tr>
<td>

<? /* START - HEADERS */ ?>
<table class="tblbgcolor" width="100%" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<tr>
<td width="5%"><b><? echo $lang_text["wbm_sender"] ?></b></td>
<td><? echo htmlspecialchars(mimeHeaderDecode($header->fromaddress)); ?> (<a class="data" href="email_adlist.php?editID=new&from_email=<? echo urlencode(htmlspecialchars(mimeHeaderDecode($header->fromaddress))) ?>">&raquo; <? echo $lang_text['wbm_ad_adbook'] ?></a>)</td>
</tr><tr>
<td width="5%"><b><? echo $lang_text["wbm_to"] ?></b></td>
<td><? echo htmlspecialchars(mimeHeaderDecode($header->toaddress)); ?></td>
</tr>
<? if(isset($header->ccaddress)): ?>
<tr>
<td width="5%"><b><? echo $lang_text["wbm_cc"] ?></b></td>
<td><? echo htmlspecialchars(mimeHeaderDecode($header->ccaddress)); ?></td>
</tr>
<? endif; ?>
<tr>
<td width="5%"><b><? echo $lang_text["wbm_date"] ?></b></td>
<td><? echo $date; ?></td>
</tr>
</table>
<? /* END - HEADERS */ ?>

<? /* START - MENUE */ ?>
<table width="100%" class="tblbgcolor" border="1" cellpadding="0" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<tr>
<td align="center" width="33%"><? echo std_button(ltext("wbm_email_reply"), 'javascript:submitReplyForm()'); ?></td>
<td align="center" width="33%"><? echo std_button(ltext("wbm_email_fwd"), 'javascript:submitForwardForm()'); ?></td>
<td align="center" width="33%"><? echo std_button(ltext("wbm_email_del"), "javascript:deleteOneMessage($id)"); ?></td>
</tr>
<tr>
<td align="center"><? echo std_button(ltext("wbm_email_viewsource"), "javascript:location.href='email_viewsource.php?id=$id'"); ?></td>
<td align="center"><? echo std_button(ltext("wbm_email_download"), "javascript:location.href='email_attach.php?id=$id&part=0&uniq=$mail_uniq&inline=0&filename=email.txt'"); ?></td>
<td align="center"><? echo std_button(ltext("wbm_email_print"), "javascript:self.print()"); ?></td>
</tr>
</table>
<? /* END - MENUE */ ?>


<?
$structure = imap_fetchstructure($mbox, $id);
$structarr = headerArray($structure);
$partscount = sizeof($structure->parts);
$attachNumber=0;	
if( ($partscount > 1) || ($structure->type == TYPEMULTIPART && $structarr["parameters"]["boundary"]) )
{
?>
	<table class="tblbgcolor" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<?
  for($i=1; $i<=$partscount; $i+=2){
		echo "<tr>\n";
    for($j=$i; $j<=$i+1; $j++){
      $ha = headerArray($structure->parts[$j]);
      if($ha){
				$body = imap_fetchbody($mbox, $id, ($j+1));
				correctType( $ha, $body);
				
				if ( $ha['type'] != "text/html" || $ha['disposition'] == 'attachment') {
					$fn = '';
					if($ha["dparameters"]["filename"]){
						$fn = $ha["dparameters"]["filename"];
					}
					if($ha["parameters"]["name"]){
						$fn = $ha["parameters"]["name"];
					}
					if(!$fn){
						$fn = $lang_text["wbm_unknown"];	
					}
					$inline = showInLine($ha["type"]) ? 1 : 0;
					$attachNumber++;
?>
<td>
  <a class="data"<? if($inline): ?> target="_blank"<? endif; ?> 
href="email_attach.php?id=<? echo $id; ?>&part=<? echo $j; ?>&uniq=<? echo $mail_uniq; ?>&inline=<? echo $inline; ?>">
<img src="<?=$strSkinName?>/webmail_icons/attach.gif" border="0" align="absmiddle"> 
<? 
	echo $fn; 
?>
  </a> (<? echo sizeFormat($ha['bytes']); ?>)</td>
<?
			  }
      }
    }
?>
	</tr>
<?
  }
?>
	</table>
<?
}
/*
echo "<pre>" ; print_r($structure);
$z = imap_fetchbody($mbox, $id, $a);
print_r($z);
if(count($structure->parts)){
  foreach($structure->parts as $part){
    print_r(headerArray($part));
  }
}else{
  print_r(headerArray($structure));
}
echo "</pre>";
*/
?>
	<table class="tblbgcolor" width="100%" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<?

if( ($partscount > 1) || ($structure->type == TYPEMULTIPART && $structarr["parameters"]["boundary"]) ){
	

	
	$reply_body = '';
	global $linkToHtml;
	
	for ( $i=0; $i<=$partscount; $i++ ) {

		$ha = headerArray($structure->parts[$i]);

		if ( $ha['type'] == 'multipart/alternative' || $ha['type'] == 'multipart/related'){

			$body = imap_fetchbody($mbox, $id, ($i+1));
			$DEBUG=0;
			$arMulti =& parseMultipart($body,$ha['parameters']['boundary']);

			for ( $iMulti=0; $iMulti<count($arMulti); $iMulti++ ){
				
				$ha =& $arMulti[$iMulti]['content']; // get an array of parameters

				if ( $ha['type'] == 'multipart/alternative' ) {
					
					$body =& $arMulti[$iMulti]['body']; // get a body of the part
				
					$arAlter =& parseMultipart($body,$ha['boundary']);
					
					for ( $iAlter=0; $iAlter<count($arAlter); $iAlter++ ){
						$ha =& $arAlter[$iMulti]['content']; // get an array of parameters
						if ( showInLine($ha['type'], false ) ) {
							$body =& $arAlter[$iAlter]['body']; // get a body of the part
							correctType( $ha, $body ); 	
							
							show_mail( $body, $ha['type'], $ha['encoding'] );

							if( !$reply_body ){ // set the replay body if it is empty
								$reply_body = $body;	
							}
						}
					}	
			
				} else {
					if ( showInLine($ha['type'], false ) ) {
						$body =& $arMulti[$iMulti]['body']; // get a body of the part
						correctType( $ha, $body ); 	
						show_mail( $body, $ha['type'], $ha['encoding'] );
						if( !$reply_body ){ // set the replay body if it is empty
							$reply_body = $body;	
						}
					}	
				}
			}
			
		} else {
			if ( $i == 0 || showInLine($ha['type'], false ) ) {
				
				if ( $ha['disposition'] != 'attachment' ) {
				
					$body = imap_fetchbody($mbox, $id, ($i+1));
					correctType( $ha, $body);

					show_mail( $body, $ha['type'], $ha['encoding'] );
					if ( $i == 0 ) {
						$reply_body = $body;	
					}
				}
      }
    }
  }
}else{
 	$ha = headerArray($structure);
	$reply_body = imap_fetchbody($mbox, $id, 1);
  correctType( $ha, $reply_body);
  show_mail( $reply_body, $ha['type'], $ha['encoding'] );
}
?>
	</table>

</td></tr></table>
<?
	$date = explode(" ", $date);
	$time = $date[1];
	$date = $date[0];
	$rev_reply_body = strrev($reply_body);
	// cut signature from reply body
	if($sig_pos = strpos($rev_reply_body, " --\n"))
	{
		$rev_reply_body = substr($rev_reply_body, $sig_pos+3);
	}
	$reply_body = strrev($rev_reply_body);
	/* REPLY-FORM */
	$reply_zitat = str_replace("%date%",$date,$lang_text["wbm_rezitat"]);
	$reply_zitat = str_replace("%time%",$time,$reply_zitat);
	$reply_zitat = $reply_zitat."\n&gt; ".str_replace("\n","\n&gt; ",(htmlspecialchars($reply_body))."\n");
	if(trim($header->fromaddress)) {
		$reply_sender = htmlspecialchars(mimeHeaderDecode($header->fromaddress));
	}
	else {
		$reply_sender = htmlspecialchars(mimeHeaderDecode($header->reply_toaddress));
	}
	echo "<form action=\"email_neu.php\" name=replyForm method=post>\n";
	echo "<input type=hidden name=copy_mail value=\"".htmlspecialchars($date)."\">\n";
	echo "<input type=hidden name=copy_to value=\"$reply_sender\">\n";
	echo "<input type=hidden name=copy_subject value=\"Re: ".htmlspecialchars($subject)."\">\n";
	echo "<input type=hidden name=copy_body value=\"".htmlspecialchars($reply_zitat)."\">";
	echo "</form>\n";
	/* FORWARD-FORM */
	if($header->from[0]->personal) {
		$fwd_sender = htmlspecialchars($header->from[0]->personal);
	}
	else {
		$fwd_sender = htmlspecialchars($reply_sender);
	}
	$fwd_zitat = str_replace("%date%",$date,$lang_text["wbm_fwdzitat"]);
	$fwd_zitat = str_replace("%time%",$time,$fwd_zitat);
	$fwd_sender = str_replace("\n","",$fwd_sender);
	$fwd_sender = str_replace("\r","",$fwd_sender);
	$fwd_sender = str_replace("\0","",$fwd_sender);
	$fwd_zitat = str_replace("%sender%",$fwd_sender,$fwd_zitat);
	$fwd_zitat = $fwd_zitat."\n&gt; ".str_replace("\n","\n&gt; ",(htmlspecialchars($reply_body))."\n");
	echo "<form action=\"email_neu.php\" name=forwardForm method=post>\n";
	echo "<input type=hidden name=copy_mail value=\"".htmlspecialchars($date)."\">\n";
	echo "<input type=hidden name=copy_subject value=\"Fw: ".htmlspecialchars($subject)."\">\n";
	echo "<input name=copy_body type=hidden value=\"".htmlspecialchars($fwd_zitat)."\">";
if ($attachNumber){
  echo "<input type=hidden name=forwardid value=\"".htmlspecialchars($id)."\">\n";
}
	echo "</form>\n";
	imap_close($mbox);
endif;
include("email_footer.php");
?>

