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

#
#
#
$DEBUG=0;
#
#
#

if ($DEBUG){
  echo "email_neu.php<br>\n";
}

include_once("auth.php");
include_once( "../include/class.SQLString.php" );
include_once( "../include/class.SQLInteger.php" );
include("email_functions.php");
include("email_config.php");
include("email_header.php");

$partscount =0;
if($forwardid && e_connect()){
  $structure = imap_fetchstructure($mbox, $forwardid);
  $structarr = headerArray($structure);
  $partscount = sizeof($structure->parts);
}
if($sendmail) {
?>
<table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<tr><td>
<?
  /* This ContentTypes should not be encoded with base64 - use the given encode-Type e.g. "8bit" */
  $encodeType["text/plain"] = "8bit";
  $encodeType["text/html"] = "8bit";
  $encodeType["message/rfc822"] = "8bit";
  $encodeType["message/external-body"] = "8bit";
  $encodeType["message/http"] = "8bit";
  $encodeType["message/news"] = "8bit";
  $encodeType["message/partial"] = "8bit";

  if($from != "" && $to != "") {
    $to = str_replace ("\\", "", $to);
    $to = str_replace (";", ",", $to);
    $cc = str_replace ("\\", "", $cc);
    $cc = str_replace (";", ",", $cc);
    $bcc = str_replace ("\\", "", $bcc);
    $bcc = str_replace (";", ",", $bcc);
    if($signatur) {
	  $query_sign = safe_query( "SELECT * FROM $cnf_db_sign WHERE alias = ? AND pop3 = ?", TRUE, new SQLString($from), new SQLString( $PHP_AUTH_USER ) );
      $sign = db_fetch_array($query_sign);
      $content = $content."\n".$sign['signatur'];
    }

#
# To exclude superfluous blank lines
#
	$mail_content='';
	$lines=explode("\n",$content);
	foreach ($lines as $line){
	  $mail_content.=wordwrap(rtrim($line),72,"\n")."\n";
	}  

	if(preg_match("/\n\n$/",$mail_content)){ // there are last blank lines
	   $lines=explode("\n",$mail_content);
	   
	   while(!($line = array_pop($lines)) && (count($lines)>0)){
	    /* pop blank lines*/
	   }
	   array_push($lines,$line);
	   $mail_content=implode("\n",$lines);
	}
	
#
# end to exclude superfluous blank lines
#
	
    $mail_header = "From: $from";
    switch($priority) {
      case "1":
        $mail_header .= "\nX-Priority: 1\nImportance: High\nX-MSMail-Priority: High";
        break;
      case "3":
        $mail_header .= "\nX-Priority: 3\nImportance: Normal\nX-MSMail-Priority: Normal";
        break;
      case "5":
        $mail_header .= "\nX-Priority: 5\nImportance: Low\nX-MSMail-Priority: Low";
        break;
    }
	if($priority){
	  $mail_header .= "\nX-MimeOLE: Produced by Confixx WebMail".
	  "\nX-Mailer: Confixx WebMail (like SquirrelMail)";
	  /*  need for spamassassin */
	}
    $mail_header .= "\nCc: $cc";
    $mail_header .= "\nBcc: $bcc";

    if($DEBUG){
      echo "attachment_name: $attachment_name<br>\n";
    }
    $mail_attach='';
    $boundary = strtoupper(md5(uniqid(time())));
#
# forward attachment
#
    for($i=0; $i<=$partscount; $i++){
      if ($attachs[$i]){
	if ($DEBUG){
	  echo "$i: ".$attachs[$i]."<br>\n";
	}
	$part=$structure->parts[$i];
	if (is_object($part)){
	  $ha = headerArray($part);
	  $fn = '';
	  if($ha["dparameters"]["filename"]){
	    $fn = $ha["dparameters"]["filename"];
	  }	
	  if($ha["parameters"]["name"]){
	    $fn = $ha["parameters"]["name"];
	  }
	  if(!$fn){
	    $fn = $lang_text["wbm_unknown"];	
	  }
	  if ($fn==$attachs[$i]){
	    if ($body = imap_fetchbody($mbox, $forwardid, ($i+1))){
	      $hdrs = "\n--$boundary";
	      $hdrs .= "\nContent-Type: ".$ha["type"]."; name=\"$fn\"";
	      $hdrs .= "\nContent-Transfer-Encoding: ".$ha["encoding"];
	      $hdrs .= "\nContent-Disposition: attachment; filename=\"$fn\"";
	      $hdrs .= "\n\n";
	      if ($DEBUG){
		print_r($ha);
		echo "<br>\n$i:<br>\n<pre>$hrds</pre><br>\n";
	      }
	      $mail_attach.=$hdrs;
	      $mail_attach.=$body;
	    }
	  }
	}
      }
    }

    if($attachment_name != "") {
      if ($fh=fopen($attachment,"r")){
	$attach_content = fread($fh,filesize($attachment));
	fclose($fh);
      }
      if($encodeType["$attachment_type"]) {
        $encode = $encodeType["$attachment_type"];
      } else {
        $encode = "base64";
        $attach_content = chunk_split(base64_encode($attach_content));
      }
      $mail_attach .= "\n--$boundary";
      $mail_attach .= "\nContent-Type: $attachment_type; name=\"$attachment_name\"";
      $mail_attach .= "\nContent-Transfer-Encoding: $encode";
      $mail_attach .= "\nContent-Disposition: attachment; filename=\"$attachment_name\"";
      $mail_attach .= "\n\n$attach_content";
    }

    if(!$mail_attach){
      $mail_header .= "\nContent-Type: text/plain";
      $mail_header .= "\nContent-Transfer-Encoding: 8bit";
      $mail_header .= "\n\n$mail_content";
    } else {
      $mail_header .= "\nMIME-Version: 1.0";
      $mail_header .= "\nContent-Type: multipart/mixed; boundary=$boundary";
      $mail_header .= "\n\nThis is a multi-part message in MIME format  --  Dies ist eine mehrteilige Nachricht im MIME-Format";
      $mail_header .= "\n--$boundary";
      $mail_header .= "\nContent-Type: text/plain";
      $mail_header .= "\nContent-Transfer-Encoding: 8bit";
      $mail_header .= "\n\n$mail_content\n\n";
      /* Attachment */
      $mail_header .=$mail_attach;
      $mail_header .= "\n--$boundary--";
    }
    /* Send email */
    $sendmail_ok = $lang_text["wbm_sendmail_ok"];
    $sendmail_ok = str_replace("%subject%","&quot;$subject&quot;",$sendmail_ok);
    $to2 = str_replace("<","&#139;",$to);
    $to2 = str_replace(">","&#155;",$to2);
    $sendmail_ok = str_replace("%to%","$to2",$sendmail_ok);

    if ($DEBUG){
      echo "message:<br>\n<pre>$mail_header</pre><br>\n";
    }
#
# send mail
#
    if(@mail($to,$subject,"",$mail_header,"-f$from")) {
      echo $sendmail_ok;
    } else{
      echo "<b>".$lang_text["wbm_sendmail_error"]."</b>";
    }

    echo "<hr noshade size=1>";
    echo str_replace("\n","<br>",(htmlspecialchars($mail_content)));

  } else { 
    echo $lang_text["wbm_sendmail_norecipient"]; 
  }
?>
</td></tr>
<form name="continue_form" method="post" action="email_postfach.php">
<tr><td align="right"><? echo std_button(ltext("wbm_buttonnext"), 'javascript:document.forms[0].submit()'); ?></td></tr>
</form>
</table>
<?
} else {
  $query_from = safe_query( "SELECT * FROM $cnf_db_email WHERE pop3 = ?", TRUE, new SQLString($cnf_PHP_AUTH_USER) );
  $from_email_exists = 0;
  while($from = db_fetch_array($query_from)) {
    $from_email_exists = 1;
    if ($from['prefix'] == "*") {
    $from_email = "catchall@".$from['domain'];
    } else { 
    $from_email = $from['prefix']."@".$from['domain']; }
	$query_deffrom = safe_query( "SELECT email FROM $cnf_db_deffrom WHERE pop3 = ? AND email = ?", TRUE,
			new SQLString($cnf_PHP_AUTH_USER), new SQLString($from_email) );
    $deffrom = db_fetch_array($query_deffrom);
    if($deffrom['email'] == $from_email) { $deffrom_select = " selected"; } else { $deffrom_select = ""; }
    $from_list .= "<option value=\"$from_email\"$deffrom_select>$from_email</option>\n";
  }
  if($ab_id) {
    $query_abadress = safe_query( "SELECT name, email FROM $cnf_db_ab WHERE ident = ? AND pop3 = ? ", TRUE, new SQLInteger($ab_id), new SQLString($cnf_PHP_AUTH_USER) );
    $res_abadress = db_fetch_array($query_abadress);
    $writeTo = $abadress = "&quot;".$res_abadress['name']."&quot; &lt;".$res_abadress['email']."&gt;";
  } elseif ($copy_to) {
  	$writeTo = htmlspecialchars($copy_to);
  }
if (!$from_email_exists) {
  echo ltext(wmail_email);
} else {
?>
<form name="mail_form" method="post" enctype="multipart/form-data" action="email_neu.php">
<input type="hidden" name="sendmail" value="1">
<?
if ($forwardid){ 
  echo "<input type=\"hidden\" name=\"forwardid\" value=\"$forwardid\">\n"; 
}
?>

<table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<tr>
<td valign="top"><? echo $lang_text["wbm_sender"] ?></td>
<td>
<select name="from"><? echo $from_list ?></select><br>
(<a class="data" href="javascript:setStdEmail()"><? echo $lang_text["wbm_deffrom_set"] ?></a>)</td>
</td>
</tr><tr>
<td><? echo $lang_text["wbm_recipient"] ?></td>
<td><input type="text" size="40" name="to" value="<? echo $writeTo ?>"> <a class="data" href="javascript:showAdBook('to')"><? echo $lang_text["wbm_getad"] ?></a></td>
</tr><tr>
<td><? echo $lang_text["wbm_cc"] ?></td>
<td><input type="text" size="40" name="cc" value=""> <a class="data" href="javascript:showAdBook('cc')"><? echo $lang_text["wbm_getad"] ?></a></td>
</tr><tr>
<td><? echo $lang_text["wbm_bcc"] ?></td>
<td><input type="text" size="40" name="bcc" value=""> <a class="data" href="javascript:showAdBook('bcc')"><? echo $lang_text["wbm_getad"] ?></a></td>
</tr><tr>
<td><? echo $lang_text["wbm_subject"] ?></td>
<td><input type="text" size="40" name="subject" value="<? echo $copy_subject ?>"></td>
</tr><tr>
<td><? echo $lang_text["wbm_text"] ?></td>
<td><textarea name="content" cols="58" rows="10" wrap=physical><? echo $copy_body ?></textarea></td>
</tr><tr>
<td><? echo $lang_text["wbm_sign_add"] ?></td>
<td>
<table width="100%"><tr>
<td><input type=checkbox name=signatur value=1 checked> (<a class="data" href="javascript:showSignatur()"><? echo $lang_text['wbm_sign_showedit'] ?></a>)</td>
<? if($copy_mail) { ?>
<td align="right"><? echo std_button(ltext("wmb_zitat"), 'javascript:document.mail_form.content.value += document.mail_form.copy_body.value;void(0)'); ?></td>
<? } ?>
</tr></table>
</td>
</tr><tr>
<td><? echo $lang_text["wbm_prio"] ?></td>
<td>
<select name="priority">
<option value="3"><? echo $lang_text["wbm_prio_normal"] ?></option>
<option value="1"><? echo $lang_text["wbm_prio_high"] ?></option>
<option value="5"><? echo $lang_text["wbm_prio_low"] ?></option>
</select>
</td>
</tr><tr>
<td><?php echo $lang_text["wbm_attachment"]; ?> </td>
<td>
<table>						    
<?
#
# forwarding of attachments
#
if( ($partscount > 1) || 
      ($structure->type == TYPEMULTIPART && $structarr["parameters"]["boundary"]) ){
  for($i=1; $i<=$partscount; $i+=2){
    for($j=$i; $j<=$i+1; $j++){
      $ha = headerArray($structure->parts[$j]);
      if($ha){
	$fn = '';
	if($ha["dparameters"]["filename"]){
	  $fn = $ha["dparameters"]["filename"];
	}	
	if($ha["parameters"]["name"]){
	  $fn = $ha["parameters"]["name"];
	}
	if(!$fn){
	  $fn = $lang_text["wbm_unknown"];	
	}
	echo "<tr><td><input type=\"checkbox\" name=\"attachs[$j]\" value=\"$fn\" checked> $fn </td>";
	echo "<td>(".sizeFormat($ha['bytes']).")</td></tr>\n";
      }
    }
  }	
}
?>
<tr><td><input type="file" size="20" name="attachment"></td><td></td><tr>
</table>
</td>
</tr><tr>
<td>&nbsp;</td>
<td><? echo std_button(ltext("wmb_send"), 'javascript:document.forms[0].submit()'); ?></tr><tr>
</table>
<? if($copy_mail) { ?>
<input type="hidden" name="copy_body" value="<? echo $copy_body; ?>">
</form>
<? } ?>
</form>
<? } ?>

<?
}

include("email_footer.php");
?>
