<?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
*/

// Myfolders_modify
// Moregroupware.org

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

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$myvars = array("todo","mbox_id","chk","folderto","msg_begin","account","accountto","value");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}

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

if (is_array($chk) and count($chk)>0) {
foreach ($chk as $key=>$key2) {
  if ($key2=="" or !is_numeric($key2)) {unset($chk[$key]);}
}
}

if (is_array($chk) and count($chk)>0) {
  if ($todo=="change_flags" and is_numeric($value)) {
    if ($account==0 and $value>=0) {
  	  foreach ($chk as $message_id) {
        $sql = "update mgw_webmail2_messages set labelbit=".$conn->Quote($value)." where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
	}
	
	if ($account!=0 and $value>=0 and ($email = connect_my_imap($account,$mbox_id,true))) {
  	  foreach ($chk as $message_id) {
		$flags = return_flags($email,$message_id);
		if ($value==0) {
          sqimap_messages_store_flag ($email, $message_id, $message_id, "-FLAGS (\$Label1 \$Label2 \$Label3 \$Label4 \$Label5)");
		} else if ($value==1) {
          sqimap_messages_store_flag ($email, $message_id, $message_id, "-FLAGS (\$Label2 \$Label3 \$Label4 \$Label5)");
          sqimap_messages_store_flag ($email, $message_id, $message_id, "+FLAGS (\$Label1)");
		} else if ($value==2) {
          sqimap_messages_store_flag ($email, $message_id, $message_id, "-FLAGS (\$Label1 \$Label3 \$Label4 \$Label5)");
          sqimap_messages_store_flag ($email, $message_id, $message_id, "+FLAGS (\$Label2)");
		} else if ($value==3) {
          sqimap_messages_store_flag ($email, $message_id, $message_id, "-FLAGS (\$Label1 \$Label2 \$Label4 \$Label5)");
          sqimap_messages_store_flag ($email, $message_id, $message_id, "+FLAGS (\$Label3)");
		} else if ($value==4) {
          sqimap_messages_store_flag ($email, $message_id, $message_id, "-FLAGS (\$Label1 \$Label2 \$Label3 \$Label5)");
          sqimap_messages_store_flag ($email, $message_id, $message_id, "+FLAGS (\$Label4)");
		} else if ($value==5) {
          sqimap_messages_store_flag ($email, $message_id, $message_id, "-FLAGS (\$Label1 \$Label2 \$Label3 \$Label4)");
          sqimap_messages_store_flag ($email, $message_id, $message_id, "+FLAGS (\$Label5)");
		}
	  }
      sqimap_logout($email);
	}

    if ($account==0 and $value==-1) {
  	  foreach ($chk as $message_id) {
        $sql = "select readbit from mgw_webmail2_messages where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	    $row = $res->FetchRow();
	  
	    $sql = "update mgw_webmail2_messages set readbit=".((int) !$row["readbit"])." where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
	    if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
	}
	if ($account!=0 and $value==-1 and ($email = connect_my_imap($account,$mbox_id,true))) {
  	  foreach ($chk as $message_id) {
		$flags = return_flags($email,$message_id);
		if ($flags["seen"]) {
          sqimap_messages_add_flag ($email, $message_id, $message_id, "\\Seen");
		} else {
          sqimap_messages_remove_flag ($email, $message_id, $message_id, "\\Seen");
        }
	  }
      sqimap_logout($email);
	}

    if ($account==0 and $value==-2) {
  	  foreach ($chk as $message_id) {
        $sql = "select flaggedbit from mgw_webmail2_messages where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	    $row = $res->FetchRow();

	    $sql = "update mgw_webmail2_messages set flaggedbit=".(($row["flaggedbit"]==1 or $row["flaggedbit"]==2)?3:1)." where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
	    if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
	}
	if ($account!=0 and $value==-2 and ($email = connect_my_imap($account,$mbox_id,true))) {
  	  foreach ($chk as $message_id) {
		$flags = return_flags($email,$message_id);
		if ($flags["flagged"]) {
          sqimap_messages_remove_flag ($email, $message_id, $message_id, "\\Flagged");
		} else {
          sqimap_messages_add_flag ($email, $message_id, $message_id, "\\Flagged");
        }
	  }
      sqimap_logout($email);
	}

  }

  if ($account==0 and $accountto==0 and $todo=="move" and $folderto!=$mbox_id and has_write_rights_mbox($mbox_id,$user_id) and has_write_rights_mbox($folderto,$user_id)) {
	foreach ($chk as $message_id) {
      $sql = "update mgw_webmail2_messages set mbox_id=".(int)$folderto." where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	}
  } elseif ($account==0 and $accountto==0 and $todo=="copy" and has_read_rights_mbox($mbox_id,$user_id) and has_write_rights_mbox($folderto,$user_id)) {
	foreach ($chk as $message_id) {
      $sql = "select * from mgw_webmail2_messages where message_id=".(int)$message_id." and mbox_id=".(int)$mbox_id;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  $row = $res->FetchRow();

      $msg_id = mgw_genID('mgw__seq_messages');
	  $sql = "insert into mgw_webmail2_messages 
	  (cal_accepted,notified,msg_cache,message_id,account_name,uid,type,mbox_id,header_from,header_to,header_cc,header_bcc,header_replyto,header_date,header_subject,header_size,header_inreplyto,msg_body,recentbit,readbit,replybit,draftbit,flaggedbit,forwardbit,attachbit,replied,replied2,forwarded,labelbit) values 
	  ('".$row["cal_accepted"]."','".$row["notified"]."','".ereg_replace("/","X",md5(mt_rand(1,6000)))."',".$msg_id.",'".addslashes($row["account_name"])."','".addslashes($row["uid"])."','".addslashes($row["type"])."',".$folderto.",'".addslashes($row["header_from"])."','".addslashes($row["header_to"])."','".addslashes($row["header_cc"])."','".addslashes($row["header_bcc"])."','".addslashes($row["header_replyto"])."','".addslashes($row["header_date"])."','".addslashes($row["header_subject"])."','".addslashes($row["header_size"])."','".addslashes($row["header_inreplyto"])."','".$row["msg_body"]."','".$row["recentbit"]."','".$row["readbit"]."','".$row["replybit"]."','".$row["draftbit"]."','".$row["flaggedbit"]."','".$row["forwardbit"]."','".$row["attachbit"]."','".$row["replied"]."','".$row["replied2"]."','".$row["forwarded"]."','".$row["labelbit"]."')";

      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	}
  } elseif ($account==0 and $todo=="delete") {

	if (isintrash($mbox_id) and has_write_rights_mbox($mbox_id,$user_id)) {

	  foreach ($chk as $message_id) {

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

		if ($sourcefile!="" and file_exists($sourcefile)) {
          $sql = "select message_id from mgw_webmail2_messages where msg_body='".$sourcefile."'";
          if(!$res_ch = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

          if ($res_ch->RecordCount()==1) {
	      if (file_exists($sourcefile) and dirname($sourcefile)!="docu") {
    	    unlink($sourcefile);
	      }
		}
		}
        $sql = "delete from mgw_webmail2_messages where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
    } elseif (has_write_rights_mbox($mbox_id,$user_id)) {
	  if (boxexist($mbox_id,0)) {$folderto = getboxid("publictrash",0,0);}
	  elseif (boxexist($mbox_id,$user_id)) {$folderto = getboxid("trash",0,$user_id);}

      if (has_write_rights_mbox($folderto,$user_id)) {
	    foreach ($chk as $message_id) {
          $sql = "update mgw_webmail2_messages set mbox_id=".(int)$folderto." where mbox_id=".(int)$mbox_id." and message_id=".(int)$message_id;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
	  $folderto_nr = $folderto;
	}}
  } elseif ($account!=0 and $todo=="delete") {
    if ($email = connect_my_imap($account,$mbox_id,false)) {
      if (sqimap_mailbox_exists($email, $mbox_id_name) and $trash_name!="") {
        if (strpos("@".strtolower($mbox_id_name),"inbox".$delimiter."trash")==1 or strpos("@".strtolower($mbox_id_name),"trash")==1) {
	      foreach ($chk as $message_id) {
            sqimap_messages_add_flag ($email, $message_id, $message_id, "\\Deleted");
	      }
        } else {
	      foreach ($chk as $message_id) {
            sqimap_messages_copy ($email, $message_id, $message_id, $trash_name);
            sqimap_messages_add_flag ($email, $message_id, $message_id, "\\Deleted");
	      }
        }
        sqimap_mailbox_expunge ($email, $mbox_id_name, false);
      } else {
	    die("Could not find Trash / Inbox.Trash !");
	  }
      sqimap_logout($email);
    }
  } elseif ($account!=0 and $accountto==$account and $todo=="move") {
    if ($email = connect_my_imap($account,$mbox_id,false)) {

      include CACHE_FOLDER."imap/tree/".$accountto.".php";
	  $found = false;
	  foreach ($folders as $folder) {
    	if ($folder[1] == $folderto) {
		  $found = true;
		  $folderto = $folder[7];
		  break;
		}
	  }

      if ($found and $mbox_id_name!=$folderto and sqimap_mailbox_exists($email, $mbox_id_name) and sqimap_mailbox_exists($email, $folderto)) {
	    foreach ($chk as $message_id) {
          sqimap_messages_copy ($email, $message_id, $message_id, $folderto);
          sqimap_messages_add_flag ($email, $message_id, $message_id, "\\Deleted");
	    }
        sqimap_mailbox_expunge ($email, $mbox_id_name, false);
      }
      sqimap_logout($email);
    }
  } elseif ($account!=0 and $accountto==$account and $todo=="copy") {
    if ($email = connect_my_imap($account,$mbox_id,false)) {

      include CACHE_FOLDER."imap/tree/".$accountto.".php";
	  $found = false;
	  foreach ($folders as $folder) {
    	if ($folder[1] == $folderto) {
		  $found = true;
		  $folderto = $folder[7];
		  break;
		}
	  }
      if ($found and sqimap_mailbox_exists($email, $mbox_id_name) and sqimap_mailbox_exists($email, $folderto)) {
	    foreach ($chk as $message_id) {
          sqimap_messages_copy ($email, $message_id, $message_id, $folderto);
	    }
      }
      sqimap_logout($email);
    }
  }

  Header("Location: maillist.php?reload=".$folderto_nr."&account=".$account."&msg_begin=".$msg_begin."&mbox_id=".$mbox_id."&".SID);
} else {Header("Location: maillist.php?".SID);}

?>
