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

// Filters
// Moregroupware.org

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

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

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$myvars = array("action","id","edit_id","spam_flag","match_str","account_id","match_type","copy_to_folder","target_folder","copy_to_user","target_user","change_flag_to","target_flag","mark_as_read","mark_as_flagged","delete_message");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}

if ($action=="delete") {
  $sql = "DELETE FROM mgw_webmail2_filters WHERE user_id=".(int)$_SESSION["MGW"]->userid." and id=".(int)$id;
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
}

if ($action=="edit" and $id!="" and is_numeric($id)) {
  $sql = "SELECT * FROM mgw_webmail2_filters where id=".(int)$id;
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  $row = $res->FetchRow();
  
  if (isset($row["id"])) {
	$smarty->assign("data",$row);
    $smarty->assign("edit_id",$id);
  }
}

if ($action=="change" and ($spam_flag=="checked" or $match_str!="") and $match_type!="" and $account_id!="") {
  if (($copy_to_folder!="" and $target_folder!="") or 
      ($copy_to_user!="" and $target_user!="") or 
	  ($change_flag_to!="" and $target_flag!="") or 
	  ($mark_as_read!="") or ($mark_as_flagged!="") or ($delete_message!="")
	 ) {
	  if ($edit_id=="") {
        $sql = "INSERT INTO mgw_webmail2_filters (id , user_id , match_str , match_type , account_id , copy_to_folder , target_folder , copy_to_user , target_user , mark_as_read , mark_as_flagged , change_flag_to , target_flag , delete_message , spam_flag )
	      VALUES (".(int)mgw_genID('mgw__seq_cals',2).",".(int)$_SESSION["MGW"]->userid.",".$conn->Quote($match_str).",".$conn->Quote($match_type).",".(int)$account_id.",".$conn->Quote($copy_to_folder).",".(int)$target_folder.",".$conn->Quote($copy_to_user).",".(int)$target_user.",".$conn->Quote($mark_as_read).",".$conn->Quote($mark_as_flagged).",".$conn->Quote($change_flag_to).",".(int)$target_flag.",".$conn->Quote($delete_message).",".$conn->Quote($spam_flag).")";
        if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	  } elseif (is_numeric($edit_id)) {
	    $sql = "UPDATE mgw_webmail2_filters set match_str=".$conn->Quote($match_str).", match_type=".$conn->Quote($match_type).",account_id=".(int)$account_id.",copy_to_folder=".$conn->Quote($copy_to_folder).",target_folder=".(int)$target_folder.",copy_to_user=".$conn->Quote($copy_to_user).",target_user=".(int)$target_user.",mark_as_read=".$conn->Quote($mark_as_read).",mark_as_flagged=".$conn->Quote($mark_as_flagged).",change_flag_to=".$conn->Quote($change_flag_to).",target_flag=".(int)$target_flag.",delete_message=".$conn->Quote($delete_message).",spam_flag=".$conn->Quote($spam_flag)." WHERE id=".(int)$edit_id;
        if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	  }
  }
}


$users = "";
$users2 = "";
$sql = "select username,id from mgw_users where id!=".(int)$_SESSION["MGW"]->userid." order by username";
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
while ($row = $res->FetchRow()) {
  $users[] = array($row["id"],$row["username"]);
  $users2[$row["id"]] = array($row["id"],$row["username"]);
}
$smarty->assign("users",$users);

$accounts = "";
$accounts2 = "";
$sql = "select mailboxname,id,user_id from mgw_webmail2_accounts WHERE servertype!='wm2_servertype_imap_only' order by mailboxname";
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
while ($row = $res->FetchRow()) {
  if ($row["mailboxname"]!="default" or $_SESSION["MGW"]->username!="admin") {
    if ($row["user_id"]==$_SESSION["MGW"]->userid or $_SESSION["MGW"]->username=="admin") {
    $accounts[] = array($row["id"],$row["mailboxname"]);
    $accounts2[$row["id"]] = array($row["id"],$row["mailboxname"]);
  }
}
}
$smarty->assign("accounts",$accounts);


$messagecount = 0;
$tree = array();
$folders = array();
$sql = "select * from mgw_webmail2_mailboxes where user_id=".(int)$_SESSION["MGW"]->userid." or user_id=0 order by user_id=0,mboxname!='inbox',mboxparent,mbox_protect desc,mboxname";
get_folders2($sql);
calc_ebene($tree,0);  
$folders2 = array();
treesort($tree);

$smarty->assign("folders",$folders2);

$flags = array("",$hashmap["webmail2_maillist_important"],$hashmap["webmail2_maillist_work"],$hashmap["webmail2_maillist_personal"],$hashmap["webmail2_maillist_todo"],$hashmap["webmail2_maillist_later"]);

$items = array();
$sql = "SELECT * FROM mgw_webmail2_filters where user_id=".(int)$_SESSION["MGW"]->userid." order by id";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
while ($row = $res->FetchRow()) {
  $item = array();
  $item["actions"] = array();
  
  if (isset($row["id"])) {
	$item["id"] = $row["id"];
	
	if (!isset($accounts2[$row["account_id"]]) and $row["account_id"]!=-1) $accounts2[$row["account_id"]][1] = "";

    $item["account_name"] = isset($accounts2[$row["account_id"]])?$accounts2[$row["account_id"]][1]:$hashmap["filter_all_accounts"];
	$item["match"] = ($row["spam_flag"]=="checked")?$hashmap["filter_use_spam_flag"]:str_replace(array("header_subject","header_from","header_to"),array($hashmap["maillist_subject"],$hashmap["maillist_from"],$hashmap["maillist_to"]),$row["match_type"]).": ".$row["match_str"];

	if ($row["copy_to_folder"]=="checked") {
	  $item["actions"][] = $hashmap["filter_copy_to_folder"].": ".(isset($folders[$row["target_folder"]])?$folders[$row["target_folder"]][5]:$hashmap["webmail2_top_none"]);
	}
	
	if ($row["copy_to_user"]=="checked") {
	  $item["actions"][] = $hashmap["filter_copy_to_user"].": ".(isset($users2[$row["target_user"]])?$users2[$row["target_user"]][1]:$hashmap["webmail2_top_none"]);
	}

	if ($row["change_flag_to"]=="checked") {
	  $item["actions"][] = $hashmap["filter_change_flag_to"].": ".(isset($flags[$row["target_flag"]])?$flags[$row["target_flag"]]:$hashmap["webmail2_top_none"]);
	}
	
	if ($row["mark_as_read"]=="checked") {
	  $item["actions"][] = $hashmap["filter_mark_as_read"];
	}
	if ($row["mark_as_flagged"]=="checked") {
	  $item["actions"][] = $hashmap["filter_mark_as_flagged"];
	}
	if ($row["delete_message"]=="checked") {
	  $item["actions"][] = $hashmap["filter_delete_message_from_inbox"];
	}
	
    $items[] = $item;
  }
}
$smarty->assign("items",$items);



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