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

include_once("auth.php");
include("email_functions.php");
include("email_config.php");

/*
echo '<pre>';
print_r($_POST);
exit;
*/

/*
if (!$content) exit;

if ($charSet[$attachmentNumber]) $contentType[$attachmentNumber] .= ";\r\n\tcharset=\"$charSet[$attachmentNumber]\"";
if (isset($name[$attachmentNumber])) $contentType[$attachmentNumber] .= ";\r\n\tname=\"$name[$attachmentNumber]\"";

if ($contentTransferEncoding[$attachmentNumber] == "quoted-printable") $content[$attachmentNumber] = quoted_printable_decode($content[$attachmentNumber]);
if ($contentTransferEncoding[$attachmentNumber] == "base64") $content[$attachmentNumber] = base64_decode($content[$attachmentNumber]);

$contentDisposition = ($inLine[$attachmentNumber] == "true") ? "inline" : "attachment";
if (isset($fileName[$attachmentNumber])) $contentDisposition .= "; filename=$fileName[$attachmentNumber]";

header("Content-Type: $contentType[$attachmentNumber]");
header("Content-Transfer-Encoding: 8bit");
header("Content-Disposition: $contentDisposition");
echo $content[$attachmentNumber];
*/

if($id && e_connect())
{
	$header = imap_headerinfo($mbox, $id);
	$mail_uniq = md5($header->subject . $header->date);
	if($mail_uniq != $uniq)
	{
		imap_close($mbox);
		die("error");
	}
	
	$structure = imap_fetchstructure($mbox, $id);
	
	$ha = headerArray($structure->parts[$part]);
	
	if($ha['parameters']['charset'])
	{
		$ha['type']	.= ";\r\n\tcharset=\"" . $ha['parameters']['charset'] . "\"";
	}
	if($ha['parameters']['name'])
	{
		$ha['type']	.= ";\r\n\tname=\"" . $ha['parameters']['name'] . "\"";
	}
	
	$body = imap_fetchbody($mbox, $id, ($part+1));
	
	if($ha['encoding'] == "quoted-printable")
	{
		$body = quoted_printable_decode($body);
	}
	if($ha['encoding'] == "base64")
	{
		$body = base64_decode($body);
	}
	$contentDisposition = $inline ? "inline" : "attachment";
	if($ha['dparameters']['filename'])
	{
		$contentDisposition .= '; filename="' . $ha['dparameters']['filename'].'"';

	}	elseif ( $ha['parameters']['name'] ) {
		$contentDisposition .= '; filename="' . $ha['parameters']['name'].'"';
		
	}	elseif ( strlen($filename) ) {
		$contentDisposition .= '; filename="'.$filename.'"';
	}
	

	//echo "<pre>"; print_r($ha);
	//echo $contentDisposition;
	header("Pragma: cache");
	header("Content-Type: " . $ha['type']);
	header("Content-Transfer-Encoding: 8bit");
	header("Content-Disposition: $contentDisposition");
	echo $body;
	
	imap_close($mbox);
}

?>
