<?php
/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Sendmail
// Moregroupware.org

$myEnv["module"] = "webmail2";
$myEnv["stdsmenu"] = false;
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");
require_once(WM2_INC_DIR."rfc822.php");
include(WM2_INC_DIR.'sendmail_inc.php');
include(WM2_INC_DIR.'imap/date.php');

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

checkdir(CACHE_FOLDER."sendmail/",true);

$myvars = array("forward","forwardme","editmail","replyto","replyto_all","account","mbox_id","reply_string");
foreach ($myvars as $thevar) {
  ${$thevar} = htmlspecialchars(stripslashes(myquote((isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : ""))));
}

$MGW = $_SESSION["MGW"];
$user_id = $MGW->userid;
$username = $MGW->username;

$settings_von = get_accounts($user_id,$username);

if ($editmail!="") {

  if ($account==0 and has_read_rights_mail($editmail,$user_id)) {
    $sql = "select forward_draft,reply_draft,msg_cache,msg_body from mgw_webmail2_messages where message_id=".(int)$editmail;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();

    $msg_cache = $row["msg_cache"];
    if ($row["reply_draft"]!=0) { $replyto2 = $row["reply_draft"]; }
    if ($row["forward_draft"]!=0) { $forward2 = $row["forward_draft"]; }
  
    if ($editmail%10000==0) { $dir_nr = $editmail/10000; } else {$dir_nr = 0;}
    $emailsource_mini2 = CACHE_FOLDER."readmessage/".$dir_nr."/".basename($row["msg_body"])."-2.php";
    $emailsource_mini = CACHE_FOLDER."readmessage/".$dir_nr."/".basename($row["msg_body"]).".php";
  } elseif ($account != 0) {

    $message_id = get_filename_from_imap2($account,$editmail,$user_id,$mbox_id);

    if ($message_id=="") die("Access violation.");
    $emailsource_mini = CACHE_FOLDER."imap/readmessage/".$user_id."-".$message_id.".php";
    $emailsource_mini2 = CACHE_FOLDER."imap/readmessage/".$user_id."-".$message_id."-2.php";

  } else {die("Access violation.");}

  if ($emailsource_mini!="" and $emailsource_mini2!="" and file_exists($emailsource_mini) and file_exists($emailsource_mini2)) {
	include $emailsource_mini;
	include $emailsource_mini2;
  } else {die("<br>".$hashmap["sendmail_email_not_found"]."<br>".$hashmap["sendmail_number_in_db"].$msg_no."<br>".$hashmap["sendmail_filename_disabled"]);} // .$emailsource

  $cc = htmlspecial_mime($cache_cc);
  $bcc = htmlspecial_mime($cache_bcc);
  $to = htmlspecial_mime($cache_to);
  $von = htmlspecial_mime(strtolower($cache_from));
  $subject = htmlspecial_mime($cache_subject);

  if ($cache_flaggedbit=="0") {$cache_flaggedbit = 3;}
  $priority = $cache_flaggedbit;

//  $msgbody = htmlspecialchars($body_plain);
//  $msgbody = preg_replace("/(\015\012)|(\015)|(\012)/","<br>",$msgbody);

  foreach ($attachments2 as $attachment) {
    $ext = strtolower(substr($attachment[0],-strpos(strrev($attachment[0]),'.')));
	if (isset($mime_types[$ext][0]) and $mime_types[$ext][0]=="img") { $addon = "img/"; } else {$addon = "attachments/";}

    if ($account==0) {
      $act_name = CACHE_FOLDER.$addon.$dir_nr."/".$editmail."-".$msg_cache."-".$attachment[4]."-".$attachment[0];
	  if (file_exists($act_name) and !file_exists(CACHE_FOLDER."sendmail/".session_id(). "######".$attachment[0])) {
	    copy($act_name,CACHE_FOLDER."sendmail/".session_id(). "######".$attachment[0]);
	  }
    } else {
      $act_name = CACHE_FOLDER."imap/".$addon.$user_id."-".$message_id."-".$attachment[4]."-".$attachment[0];
	  if (file_exists($act_name) and !file_exists(CACHE_FOLDER."sendmail/".session_id(). "######".$attachment[0])) {
	    copy($act_name,CACHE_FOLDER."sendmail/".session_id(). "######".$attachment[0]);
	  }
    }
  }

  $_SESSION["webmail2_compose_array"] = array();
  $_SESSION["webmail2_compose_array"]["subject"] = $subject;
  $_SESSION["webmail2_compose_array"]["msgbody"] = get_format_sendmail_msgbody();
  $_SESSION["webmail2_compose_array"]["to"] = get_all_email_addresses(str_replace(array("&lt;","&gt;"),array("<",">"),$to));
  $_SESSION["webmail2_compose_array"]["cc"] = get_all_email_addresses(str_replace(array("&lt;","&gt;"),array("<",">"),$cc));
  $_SESSION["webmail2_compose_array"]["bcc"] = get_all_email_addresses(str_replace(array("&lt;","&gt;"),array("<",">"),$bcc));
  
  if ($account!=0) {$editmail=="";}
}

if ($replyto_all!="") {$replyto = $replyto_all;}

if ($forward!="") {

  $emailsource_mini2 = "";
  if ($account == 0 and has_read_rights_mail($forward,$user_id)) {

    $sql = "select msg_body from mgw_webmail2_messages where message_id=".(int)$forward;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();

    if (file_exists($row["msg_body"])) {
      if (!file_exists(CACHE_FOLDER."sendmail/".session_id(). "######".$forward.".eml")) 
	    { copy($row["msg_body"],CACHE_FOLDER."sendmail/".session_id(). "######".$forward.".eml"); }
    } else {die("<br>".$hashmap["sendmail_email_not_found"]."<br>".$hashmap["sendmail_number_in_db"].$forward."<br>".$hashmap["sendmail_filename_disabled"]);} // .$emailsource

    if ($forward%10000==0) { $dir_nr = $forward/10000; } else {$dir_nr = 0;}
    $emailsource_mini2 = CACHE_FOLDER."readmessage/".$dir_nr."/".basename($row["msg_body"])."-2.php";
  } elseif ($account != 0) {

    $emailsource_mini2 = get_filename_from_imap($account,$forward,$user_id,$mbox_id,true);
    if ($emailsource_mini2=="") die("Access violation.");

  } else {die("Access violation.");}

  if ($emailsource_mini2 != "" and file_exists($emailsource_mini2)) {
	include $emailsource_mini2;
  } else {die("<br>".$hashmap["sendmail_email_not_found"]."<br>".$hashmap["sendmail_number_in_db"].$msg_no."<br>".$hashmap["sendmail_filename_disabled"]);} // .$emailsource
  
  $cache_subject = format_sendmail_header($cache_subject);
  if (strpos(strtolower($cache_subject),"fw") === false) {$subject = "FW: ".$cache_subject;} else {$subject = $cache_subject;}

  $_SESSION["webmail2_compose_array"] = array();
  $_SESSION["webmail2_compose_array"]["subject"] = $subject;
}

if ($forwardme!="") {

  $emailsource_mini2 = "";
  if ($account == 0 and has_read_rights_mail($forwardme,$user_id)) {
    $sql = "select msg_body from mgw_webmail2_messages where message_id=".(int)$forwardme;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();

    if ($forwardme%10000==0) { $dir_nr = $forwardme/10000; } else {$dir_nr = 0;}
    $emailsource_mini2 = CACHE_FOLDER."readmessage/".$dir_nr."/".basename($row["msg_body"])."-2.php";
  } elseif ($account != 0) {

    $emailsource_mini2 = get_filename_from_imap($account,$forwardme,$user_id,$mbox_id,false);
    if ($emailsource_mini2=="") die("Access violation.");

  } else {die("Access violation.");}

  if ($emailsource_mini2 != "" and file_exists($emailsource_mini2)) {
	include $emailsource_mini2;
  } else {die("<br>".$hashmap["sendmail_email_not_found"]."<br>".$hashmap["sendmail_number_in_db"].$msg_no."<br>".$hashmap["sendmail_filename_disabled"]);} // .$emailsource
  
  $msgbody = "<br><br><br><style>BLOCKQUOTE { BORDER-LEFT:#1F4687 1px solid; padding-left:20px; margin-left: 0px; }</style><signature />". 
  			 str_replace("\n","<br>",make_text("")).get_format_sendmail_msgbody();
      
  if (strpos(strtolower($cache_subject),"fw") === false) {$subject = "FW: ".$cache_subject;} else {$subject = $cache_subject;}

  $cache_subject = format_sendmail_header($cache_subject);

  $_SESSION["webmail2_compose_array"] = array();
  $_SESSION["webmail2_compose_array"]["subject"] = $subject;
  $_SESSION["webmail2_compose_array"]["msgbody"] = $msgbody;
}


if ($replyto!="") {
  $emailsource_mini2 = "";
  if ($account==0 and has_read_rights_mail($replyto,$user_id)) {
    $sql = "select * from mgw_webmail2_messages where message_id=".(int)$replyto;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();

    if ($replyto%10000==0) { $dir_nr = $replyto/10000; } else {$dir_nr = 0;}
    $emailsource_mini2 = CACHE_FOLDER."readmessage/".$dir_nr."/".basename($row["msg_body"])."-2.php";
  } elseif ($account != 0) {
    $emailsource_mini2 = get_filename_from_imap($account,$replyto,$user_id,$mbox_id,false);
    if ($emailsource_mini2=="") die("Access violation.");
  } else {die("Access violation.");}

  if ($emailsource_mini2!="" and file_exists($emailsource_mini2)) {
	include $emailsource_mini2;
  } else {die("<br>".$hashmap["sendmail_email_not_found"]."<br>".$hashmap["sendmail_number_in_db"].$replyto."<br>".$hashmap["sendmail_filename_disabled"]);} // .$emailsource

  $msgbody = "<br><br><br><style>BLOCKQUOTE { BORDER-LEFT:#1F4687 1px solid; padding-left:20px; margin-left: 0px; }</style>". 
  			 "<signature /><blockquote type=cite class=cite cite>".str_replace("\n","<br>",make_text("> ")).get_format_sendmail_msgbody()."</blockquote>";
      
  if ($cache_replyto=="") { $str = $cache_from; } else { $str = $cache_replyto; }

  if ($replyto_all != "") {
    if ($cache_to != "") { $str .= ",".$cache_to; }
    if ($cache_cc != "") { $str .= ",".$cache_cc; }
    if ($cache_bcc != "") { $str .= ",".$cache_bcc; }
  }

  $str = get_all_email_addresses(str_replace(array("&lt;","&gt;"),array("<",">"),$str));

  if ($reply_string=="") $reply_string = "Re";
  if (strpos(strtolower($cache_subject),strtolower($reply_string)) === false) {$subject = $reply_string.": ".$cache_subject;} else {$subject = $cache_subject;}

  $_SESSION["webmail2_compose_array"] = array();
  $_SESSION["webmail2_compose_array"]["subject"] = $subject;
  $_SESSION["webmail2_compose_array"]["msgbody"] = $msgbody;
  $_SESSION["webmail2_compose_array"]["to"] = $str;
  $_SESSION["webmail2_compose_array"]["inreplyto"] = $cache_uid;
}
if ($forwardme != "") $forward = $forwardme;
if (isset($replyto2) and $replyto2 != "") $replyto = $replyto2;
if (isset($forward2) and $forward2 != "") $forward = $forward2;

if (isset($_SESSION["webmail2_compose_array"]) and isset($_SESSION["webmail2_compose_array"]["msgbody"]) and $_SESSION["webmail2_compose_array"]["msgbody"]) {
  $_SESSION["webmail2_compose_array"]["msgbody"] = htmlspecialchars($_SESSION["webmail2_compose_array"]["msgbody"]);
}

Header("Location: sendmail.php?editmail=".$editmail."&forward=".$forward."&replyto=".$replyto."&".SID);
?>