<?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-inc
// Moregroupware.org


function get_filename_from_imap2($account,$msg_no,$user_id,$mbox_id) {  
  global $conn,$username;
  
  $emailsource_mini2 = "";
  
  if (!is_numeric($mbox_id) or !is_numeric($account)) return "";
  
  if ($email = connect_my_imap($account,$mbox_id,false)) {
  	$header = my_imap_header($email,$email,"int",$msg_no);
	$emailsource_mini2 = str_replace(array(".","<",">"),"",getmessage_id($header));
    sqimap_logout($email);
  }
  return $emailsource_mini2;
}

function get_filename_from_imap($account,$msg_no,$user_id,$mbox_id,$copy_eml) {  
  global $conn,$username;
  
  $emailsource_mini2 = "";
  
  if (!is_numeric($mbox_id) or !is_numeric($account)) return "";
  
  if ($email = connect_my_imap($account,$mbox_id,true)) {
  	$header = my_imap_header($email,$email,"int",$msg_no);
	$message_id = str_replace(array(".","<",">","/"),"",getmessage_id($header));
        
    $act_name = CACHE_FOLDER."imap/eml/".$user_id."-".$message_id.".eml";
    $emailsource_mini2 = CACHE_FOLDER."imap/readmessage/".$user_id."-".$message_id."-2.php";

    if ($copy_eml) {
      copy($act_name,CACHE_FOLDER."sendmail/".session_id(). "######".$msg_no.".eml");
    }
    sqimap_logout($email);
    
  }
  return $emailsource_mini2;
}

function get_sendmail_users ($message) {
  global $conn,$contacts;
  
  $contacts[count($contacts)] = array("--------------- ".$message." ---------------","");

  $sql = "SELECT a.username,a.email,a.lastname,a.firstname FROM mgw_users a where a.email<>'' order by lastname";
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  while($row = $res->FetchRow()) {
	if (isset($row["firstname"]) and $row["firstname"]!="") {$row["firstname"] .= " ";} else {$row["firstname"] = "";}
	if (isset($_SESSION["MGW"]->settings["wm2_reverse_contacts"]) and $_SESSION["MGW"]->settings["wm2_reverse_contacts"]) {
	  $contacts[count($contacts)][0] = htmlspecialchars($row["lastname"].", ".$row["firstname"]." <".$row["email"].">");
	} else {
	$contacts[count($contacts)][0] = htmlspecialchars($row["firstname"].$row["lastname"]." <".$row["email"].">");
	}
	$contacts[count($contacts)-1][1] = htmlspecialchars($row["username"]);
  } 	
}

function get_sendmail_groups ($message) {
  global $conn,$contacts;

  $contacts[count($contacts)] = array("--------------- ".$message." ---------------","");

  $sql = "SELECT name,description FROM mgw_groups order by name";
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  while($row = $res->FetchRow()) {
	if ($row["description"]!="") {$description = " (".$row["description"].")";} else {$description="";}
	$contacts[count($contacts)][0] = htmlspecialchars($row["name"].$description);
	$contacts[count($contacts)-1][1] = htmlspecialchars("@".$row["name"]);
  }	
}

function get_sendmail_contacts ($user_id,$message) {
  global $conn,$contacts;

  if (isModuleInstalled("contact")) {
  $contacts[count($contacts)] = array("--------------- ".$message." ---------------","");

  $sql = "SELECT a.email,a.lastname,a.firstname FROM mgw_contacts a where a.email<>'' and (a.ispublic=1 or a.owner=".(int)$user_id.") order by lastname";
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  while($row = $res->FetchRow()) {
	if (isset($_SESSION["MGW"]->settings["wm2_reverse_contacts"]) and $_SESSION["MGW"]->settings["wm2_reverse_contacts"]) {
	  if (isset($row["firstname"]) and $row["firstname"]!="") {$row["firstname"] = ", ".$row["firstname"];} else {$row["firstname"] = "";}
	  $contacts[count($contacts)][0] = htmlspecialchars($row["lastname"].$row["firstname"]." <".$row["email"].">");
	} else {
	if (isset($row["firstname"]) and $row["firstname"]!="") {$row["firstname"] .= " ";} else {$row["firstname"] = "";}
	$contacts[count($contacts)][0] = htmlspecialchars($row["firstname"].$row["lastname"]." <".$row["email"].">");
	}
	$contacts[count($contacts)-1][1] = htmlspecialchars($row["email"]);
  }	
}
}

function get_sendmail_companies ($user_id,$message) {
  global $conn,$contacts;

  if (isModuleInstalled("contact")) {
  $contacts[count($contacts)] = array("--------------- ".$message." ---------------","");

  $sql = "SELECT name1,email FROM mgw_companies where email<>'' and (ispublic=1 or owner=".(int)$user_id.") order by name1";
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  while($row = $res->FetchRow()) {
	$contacts[count($contacts)][0] = htmlspecialchars($row["name1"]." <".$row["email"].">");
	$contacts[count($contacts)-1][1] = htmlspecialchars($row["email"]);
  }	
}
}

function get_sendmail_contact_groups ($message) {
  global $conn,$contacts;

  if (isModuleInstalled("contact")) {
  $contacts[count($contacts)] = array("--------------- ".$message." ---------------","");

  $sql = "SELECT name_key,id FROM mgw_contacts_groups order by name_key";
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  while($row = $res->FetchRow()) {
	$contacts[count($contacts)][0] = htmlspecialchars($row["name_key"]);
	$contacts[count($contacts)-1][1] = htmlspecialchars("@".$row["name_key"]);
  }	
}
}

function make_text($seperator) {
  global $hashmap,$cache_replyto,$cache_to,$cache_from,$cache_date,$cache_cc,$cache_bcc,$cache_subject;

  $cache_to = format_sendmail_header($cache_to);
  $cache_from = format_sendmail_header($cache_from);
  $cache_date = getDateString_full($cache_date);

  $cache_cc = format_sendmail_header($cache_cc);
  $cache_bcc = format_sendmail_header($cache_bcc);
  $cache_replyto = format_sendmail_header($cache_replyto);

  $cache_subject = format_sendmail_header($cache_subject);

  if ($seperator=="") { $mytext = $hashmap["sendmail_forward_message"]; } else { $mytext = $hashmap["sendmail_reply_message"]; }
  
  $msgbody = 
      "-----".$mytext."-----\n".
      $hashmap["sendmail_from"]." ".$cache_from."\n".
	  $hashmap["sendmail_sent"]." ".$cache_date."\n".
	  $hashmap["sendmail_to"]." ".$cache_to."\n".
      ($cache_cc != "" ? $hashmap["sendmail_cc"].": ".$cache_cc."\n" : "").
      ($cache_bcc != "" ? $hashmap["sendmail_bcc"].": ".$cache_bcc."\n" : "").
	  $hashmap["sendmail_subject"]." ".$cache_subject."\n".
	  "\n";

  return $msgbody;
}
    
function get_format_sendmail_msgbody () {
  global $hashmap,$body_show;

//  $msg_body = htmlspecialchars($body_plain);
//  $msg_body = $seperator.preg_replace("/(\015\012)|(\015)|(\012)/","\n".$seperator,wordwrap($msg_body,72,"\n",true));
// echo stripslashes($body_show[0][1]);

  $msg_body = "";
  foreach ($body_show as $key=>$key2) {
    if (is_array($key2) and count($key2)>1 and ($key2[0]=="text" or $key2[0]=="html")) {
      $msg_body .= stripslashes($key2[1]);
	}
  }
  $msg_body = str_replace(array("sendmail.php?to=","norefer.php?url=","noimg.php?download=0&url=","noimg.php?download=1&url="),array("mailto:","","#","#"),$msg_body);
  $msg_body = preg_replace("'<img[^>]*?>'si","",$msg_body);
  
  return $msg_body;
}

function format_sendmail_header($str) {
  $str = trim(str_replace(array("\r","\n"),array("",""),htmlspecialchars(decode_mime_string($str))));
  return $str;
}
    
function get_accounts($user_id,$username) {
  global $conn,$settings_von;
  
  $result = false;
  $settings_von = array();
  $sql = "select mailboxname,id, replyto, fullname from mgw_webmail2_accounts where enable_smtp='checked' and user_id=".(int)$user_id;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  while($row = $res->FetchRow()) {
    if (($row["mailboxname"]!="default" or $username!="admin")) {
	  $result = true;
        $settings_von[] = array($row["id"],htmlspecialchars($row["fullname"]." <".$row["replyto"].">"),$row["replyto"]);
    }
  }
  return $result;
}

function my_in_array($str) {
  global $settings_von;
  foreach ($settings_von as $key => $key2) {
    if ($key2[2] == $str) { return true; }
  }
  return false;
}

?>
