<?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');

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

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

if (isset($_GET["reset"]) and $_GET["reset"]=="true") {
  $_SESSION["webmail2_compose_array"] = array();
  $_POST = array();
  $_GET = array();
  include(WM2_INC_DIR.'sendmail_post_inc.php');
  delete_open_attachments(CACHE_FOLDER."sendmail/");
}

$myvars = array("msgbody","subject","von","to","cc","bcc","priority","replyto","inreplyto","signit","signit2","sendplain","sendplain2","notification","notification2","forward","editmail","replyto","enable_cc_bcc2","import","account","reset");
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;

if (isset($_SESSION["webmail2_compose_array"]) and is_array($_SESSION["webmail2_compose_array"]) and count ($_SESSION["webmail2_compose_array"])) {
  foreach ($_SESSION["webmail2_compose_array"] as $key=>$key2) {
    ${$key} = $key2;
  }
  $_SESSION["webmail2_compose_array"] = array();
  unset($_SESSION["webmail2_compose_array"]);
}
if ($signit2=="") $signit2 = $signit;
if ($sendplain2=="") $sendplain2 = $sendplain;
if ($notification2=="") $notification2 = $notification;

/*
  if ($showhtml2 == "true") {
    $msgbody = str_replace(array("\r","@#@#@"),array("","<p>"),htmlspecialchars(str_replace("\n","@#@#@",$msgbody)));
  } else {
	$msgbody = str_replace(array("&gt;", "&lt;","&quot;", "&amp;"), array(">","<", "\"", "&"), $msgbody);
    $msgbody = strip_tags(str_replace(array("\n","<br>","<p>","</p>"),array("","\n","\n","\n\n"),$msgbody));
  }
  $showhtml = $showhtml2;
}
*/

if (!strpos($to, "?") === false) {
  $to2 = substr($to,strpos($to, "?")+1);
  $to = substr($to,0,strpos($to, "?"));

  parse_str($to2,$to2a);

  foreach ($to2a as $key=>$to2b) {
    if (strtolower($key)=="subject") { $subject = $to2b; }
    if (strtolower($key)=="cc") { $cc = $to2b; }
    if (strtolower($key)=="bcc") { $bcc = $to2b; }
    if (strtolower($key)=="body") { $msgbody = $to2b; }
  }
}

if (!get_accounts($user_id,$username)) {
  $smarty->assign("username", ucfirst($username));
  $smarty->display("sendmail-2.tpl");
  die;
}
$account_count = count($settings_von);

$smarty->assign("wm2_default_account", isset($_SESSION["MGW"]->settings["wm2_default_account"])?$_SESSION["MGW"]->settings["wm2_default_account"]:"");
if (isset($MGW->settings["wm2_addsignature"]) and $MGW->settings["wm2_addsignature"] and $signit2=="") { $signit = "checked"; }
if (isset($MGW->settings["wm2_return_receipt"]) and $MGW->settings["wm2_return_receipt"] and $notification2=="") { $notification = "checked"; }
if (isset($MGW->settings["wm2_always_sendplain"]) and $MGW->settings["wm2_always_sendplain"] and $sendplain2=="") { $sendplain = "checked"; }

$attaches=array();
$handle=opendir(CACHE_FOLDER."sendmail/");
while ($file = readdir($handle)) {
  if (strstr($file,session_id())) {
    $attaches[] = "<font color='#1F4687'><img src='templates/default/media/attach.gif' align='absmiddle'>&nbsp;".urldecode(substr(strstr($file,"######"),6))." (".ceil(filesize(CACHE_FOLDER."sendmail/" . $file) / 1024)." K)</font><br>\n";
  }
}
closedir($handle);

$prio=array("","","","selected","","");
if ($priority!="") {
  $prio[3]="";
  $prio[$priority]="selected";
}

$contacts = array();

get_sendmail_users($hashmap["compose_users"]);
get_sendmail_groups($hashmap["compose_groups"]);
get_sendmail_contacts($user_id,$hashmap["compose_contacts"]);
get_sendmail_companies($user_id,$hashmap["compose_companies"]);
get_sendmail_contact_groups($hashmap["compose_contact_groups"]);

if ($cc!="" or $bcc!="") {
  $enable_cc_bcc = "true";
} else if ($enable_cc_bcc2=="" and isset($_SESSION["webmail2_enable_cc_bcc"])) {
  $enable_cc_bcc = $_SESSION["webmail2_enable_cc_bcc"];
} else if ($enable_cc_bcc2!="") {
  $enable_cc_bcc = $enable_cc_bcc2;
  $_SESSION["webmail2_enable_cc_bcc"] = $enable_cc_bcc2;
} else {
  $enable_cc_bcc = "no";
}

$contacts_str = '';
foreach ($contacts as $contact) {
  $contacts_str .= '<OPTION VALUE="'.strtolower($contact[1]).'">'.$contact[0];
}

$smarty->assign("custom_font",$_SESSION["MGW"]->settings["wm2_compose_font"]);
$smarty->assign("custom_font_size",$_SESSION["MGW"]->settings["wm2_compose_font_size"]);

$smarty->assign("account_count", $account_count);
$smarty->assign("username", ucfirst($username));
$smarty->assign("to", trim($to));
$smarty->assign("cc", trim($cc));
$smarty->assign("editmail", $editmail);
$smarty->assign("replyto", $replyto);
$smarty->assign("inreplyto", $inreplyto);
$smarty->assign("forward", $forward);
$smarty->assign("signit", $signit);
$smarty->assign("notification", $notification);
$smarty->assign("sendplain", $sendplain);
$smarty->assign("enable_cc_bcc", $enable_cc_bcc);
$smarty->assign("msgbody", $msgbody);
$smarty->assign("bcc", trim($bcc));
$smarty->assign("subject", $subject);
$smarty->assign("contacts", $contacts_str);
$smarty->assign("attaches", $attaches);
$smarty->assign("settings_von", $settings_von);
$smarty->assign("von", $von);
$smarty->assign("prio", $prio);

$smarty->display("sendmail.tpl");
?>