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

// Tree
// Moregroupware.org

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

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

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

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

$myvars = array("createnew","mboxname","rename","delete","createnewlink","href","desc","newwindow","account","empty_trash","is_public","imap_reload","delete_link","link_id");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}

$newid="";
$newaccount="";
$opentrash=0;
$trashid=0;
if ($imap_reload) { $force_imap_reload = true; } else { $force_imap_reload = false; }

if ($account!=0 and $rename!="" and $mboxname != "" and is_numeric($rename)) {
  $mboxname2 = str_replace(array("\\","/",".","'","\"","\$"),"",$mboxname);
  if ($email = connect_my_imap($account,$rename,true)) {
	sqimap_mailbox_select($email, "INBOX");
    $target = "";
    if (substr_count($mbox_id_name,$delimiter)>0 ) {
      $target = substr($mbox_id_name,0,strrpos($mbox_id_name,$delimiter)).$delimiter.$mboxname2;
    }
    if (!sqimap_mailbox_exists($email, $target) and $target!="") {
      sqimap_mailbox_myrename( $email, $mbox_id_name, $target );

      $sql = "update mgw_webmail2_sidebar set mboxname='".$target."' where account=".(int)$account." and mboxname='".$mbox_id_name."'";
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
      $force_imap_reload = true;
    } else {
      $errormessage = $hashmap["myfolders_folder_exists"];
      echo "<script>alert('".addslashes($errormessage)."');</script>";
    }
    sqimap_logout($email);
  }
}

if ($account==0 and $rename!="" and $mboxname != "" and is_numeric($rename) and has_write_rights_mbox($rename,$user_id)) {
    $mboxname = myquote($mboxname);
    $found = true;
    
    $sql = "select mboxparent from mgw_webmail2_mailboxes where mbox_id=".(int)$rename;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();

	if (isset($row["mboxparent"]) and $row["mboxparent"]!="") {
      $parent = $row["mboxparent"];
    } else {
	  $found = false;
	}

    $sql = "select mbox_id from mgw_webmail2_mailboxes where mboxparent=".(int)$parent." and (user_id=".(int)$user_id." or user_id=0) and mboxname=".$conn->Quote($mboxname);
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $mycount = $res->RecordCount();
    
    if ($mycount == 0 and $found) {
      $sql = "update mgw_webmail2_mailboxes set mboxname=".$conn->Quote($mboxname)." where mbox_protect=0 and mbox_id=".(int)$rename;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    } else {
      if ($mycount != 0) { $errormessage = $hashmap["myfolders_folder_exists"]; } else { $errormessage = $hashmap["myfolders_folder_cant_rename"]; }
      echo "<script>alert('".addslashes($errormessage)."');</script>";
    }
}

if ($delete_link!="" and is_numeric($delete_link)) {
  $sql = "select * from mgw_webmail2_links where (user_id=".(int)$user_id." or user_id=0) and id=".(int)$delete_link;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  $row = $res->FetchRow();

  if ($row["mbox_id"]!="" and ($row["mbox_id"]==-1 or $row["mbox_id"]==-2 or has_write_rights_mbox($row["mbox_id"],$user_id))) {
    $sql = "delete from mgw_webmail2_links where (user_id=".(int)$user_id." or user_id=0) and id=".(int)$delete_link;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

    $sql = "select * from mgw_webmail2_links where mbox_id=".(int)$row["mbox_id"];
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $messagecount = $res->RecordCount();
	
	if ($messagecount==0) {
      $sql = "update mgw_webmail2_mailboxes set haslinks='0' where mbox_id=".(int)$row["mbox_id"];
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	}
  }

}

if ($account==0 and $delete!="" and is_numeric($delete)) {
  $subfolder = $delete;

  if (boxexist($subfolder,0)) {
    $user_id2 = 0;
    $trashid = getboxid("publictrash",0,$user_id2);
  } else {
    $user_id2 = $user_id;
    $trashid = getboxid("trash",0,$user_id2);
  }

  if (boxexist($subfolder,$user_id2) and getboxprot($subfolder)==0 and has_write_rights_mbox($subfolder,$user_id)) {

    $sql = "select * from mgw_webmail2_mailboxes where user_id=".(int)$user_id2." order by mboxparent,mboxname";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  
    $tree = array();
    while($row = $res->FetchRow()) {
      addtotree($row["mboxparent"],$tree,$row["mbox_id"],0);
    }
    $children = array();
    getchildren($tree,$subfolder,array());

    $opentrash=1;
    if (isintrash($subfolder)) {

      $children[] = $subfolder;      
      foreach ($children as $key=>$key2) {
        if (!has_write_rights_mbox($key2,$user_id)) die("Access denied.");
      }
      foreach ($children as $key=>$key2) {

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

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

            if ($res_ch->RecordCount()==1) {
    	    unlink($row["msg_body"]);
	      }
		}
		}
		
        $sql = "delete from mgw_webmail2_messages where mbox_id=".(int)$key2;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

        $sql = "select mbox_id from mgw_webmail2_mailboxes where user_id=".(int)$user_id2." and mbox_protect=0 and mbox_id=".(int)$key2;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
        $mycount = $res->RecordCount();

  	    $sql = "delete from mgw_webmail2_mailboxes where user_id=".(int)$user_id2." and mbox_protect=0 and mbox_id=".(int)$key2;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

        if ($mycount != 0) {
          $sql = "select mbox_id from mgw_webmail2_sidebar where account=0 and mbox_id=".(int)$key2;
          if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
          $mycount = $res->RecordCount();

    	  $sql = "delete from mgw_webmail2_sidebar where account=0 and mbox_id=".(int)$key2;
          if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
          echo "<script>
            if (top.rightframe.maillist && top.rightframe.maillist.mbox_id == ".$key2.") {
              top.rightframe.maillist.location.reload();
            }
            if (top.mailfolders) {
              top.mailfolders.location.reload();
            }
            </script>
          ";
        }
      }
    } else if (has_write_rights_mbox($trashid,$user_id)) {
      foreach ($children as $key=>$key2) {
        if (!has_write_rights_mbox($key2,$user_id)) die("Access denied.");
      }

      // move to trash
	  
      $mboxname = addslashes(getboxname($subfolder));
	  $i = "";
      $i2 = "";

      while (boxexist2(addslashes($mboxname).$i2.$i,$trashid,$user_id2)) {
	    if ($i=="") {$i = 1; $i2="-";}
		$i++;
	  }
	  if ($i!="") {$i = "-".$i;}
      $mboxname = $mboxname.$i;

      $sql = "update mgw_webmail2_mailboxes set isintrash=1,isdraft=0,mboxname='".$mboxname."',mboxparent=".(int)$trashid." where mbox_id=".(int)$subfolder." and user_id=".(int)$user_id2;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

      foreach ($children as $key=>$key2) {
        $sql = "update mgw_webmail2_mailboxes set isintrash=1,isdraft=0 where mbox_id=".(int)$key2." and user_id=".(int)$user_id2;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
    }
  } else {
    echo "<script>alert('".addslashes($hashmap["myfolders_folder_cant_delete"])."');</script>";
  }
}

if ($account!=0 and $delete!="" and is_numeric($delete)) {
  $subfolder = $delete;

  if ($email = connect_my_imap($account,$subfolder,true)) {
	sqimap_mailbox_select($email, "INBOX");
    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) {
        include CACHE_FOLDER."imap/tree/".$account.".php";
        foreach ($folders as $folder) {
          if (strpos("@".$folder[7],$mbox_id_name)==1) {
            sqimap_mailbox_mydelete ($email, $folder[7]);

            $sql = "delete from mgw_webmail2_sidebar where account=".(int)$account." and mboxname='".$folder[7]."'";
            if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

            $force_imap_reload = true;
          }
        }
      } else {
        while (sqimap_mailbox_exists($email, $trash_name.$delimiter.$mboxname)) { $mboxname .="_2"; }
        sqimap_mailbox_myrename( $email, $mbox_id_name, $trash_name.$delimiter.$mboxname );
        $force_imap_reload = true;

        $sql = "update mgw_webmail2_sidebar set mboxname='".$trash_name.$delimiter.$mboxname."' where account=".(int)$account." and mboxname='".$mbox_id_name."'";
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
      }
    } else {
      echo "<script>alert('".addslashes($hashmap["myfolders_folder_cant_delete"])."');</script>";
    }
    sqimap_logout($email);

  }
}

if ($account==0 and $empty_trash=="true") {

  if ($is_public) {
    $user_id2 = 0;
    $trashid = getboxid("publictrash",0,$user_id2);
  } else {
    $user_id2 = $user_id;
    $trashid = getboxid("trash",0,$user_id2);
  }
  $subfolder = $trashid;

  if (has_write_rights_mbox($subfolder,$user_id)) {

    $sql = "select * from mgw_webmail2_mailboxes where user_id=".(int)$user_id2." order by mboxparent,mboxname";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  
    $tree = array();
    while($row = $res->FetchRow()) {
      addtotree($row["mboxparent"],$tree,$row["mbox_id"],0);
    }
    $children = array();
    getchildren($tree,$subfolder,array());

    $opentrash=1;

    $children[] = $subfolder;      
    foreach ($children as $key=>$key2) {
      if (!has_write_rights_mbox($key2,$user_id)) die("Access denied.");
    }
    foreach ($children as $key=>$key2) {

      $sql = "select msg_body from mgw_webmail2_messages where mbox_id=".(int)$key2;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  while($row = $res->FetchRow()) {
		if ($row["msg_body"]!="" and file_exists($row["msg_body"]) and dirname($row["msg_body"])!="docu") {
          $sql = "select message_id from mgw_webmail2_messages where msg_body=".$conn->Quote($row["msg_body"]);
          if(!$res_ch = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

          if ($res_ch->RecordCount()==1) {
    	  unlink($row["msg_body"]);
	    }
	  }
	  }
	  
      $sql = "delete from mgw_webmail2_messages where mbox_id=".(int)$key2;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

      $sql = "select mbox_id from mgw_webmail2_mailboxes where user_id=".(int)$user_id2." and mbox_protect=0 and mbox_id=".(int)$key2;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
      $mycount = $res->RecordCount();

  	  $sql = "delete from mgw_webmail2_mailboxes where user_id=".(int)$user_id2." and mbox_protect=0 and mbox_id=".(int)$key2;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

      echo "<script>
        if (top.rightframe.maillist && top.rightframe.maillist.mbox_id == ".$key2.") {
          top.rightframe.maillist.location.reload();
        }
        </script>
      ";
      if ($mycount != 0) {
        $sql = "select mbox_id from mgw_webmail2_sidebar where account=0 and mbox_id=".(int)$key2;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
        $mycount = $res->RecordCount();

    	$sql = "delete from mgw_webmail2_sidebar where account=0 and mbox_id=".(int)$key2;
        if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
        echo "<script>
          if (top.mailfolders) {
            top.mailfolders.location.reload();
          }
          </script>
        ";
      }
    }
  }
}

if ($account==0 and $createnew!="" and is_numeric($createnew) and $mboxname != "" and ($createnew==-1 or $createnew==-2 or has_write_rights_mbox($createnew,$user_id))) {

  $isintrash = 0;
  $isdraft = 0;
  $the_user_id = $user_id;
  if ($createnew == -1) { $createnew = 0; }
  
  $mboxname = myquote($mboxname);

  $mycount = 0;
  if ($createnew == -2) {
    $createnew = 0;
    $the_user_id = 0;
  }
  if ($createnew != 0 and $createnew != -2) {
    $sql = "select user_id,isdraft,isintrash from mgw_webmail2_mailboxes where mbox_id=".(int)$createnew;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $mycount = $res->RecordCount();
    $row = $res->FetchRow();
    $isintrash = $row["isintrash"];
    if ($isintrash == "") {$isintrash=0;}
    $isdraft = $row["isdraft"];
    if ($isdraft == "") {$isdraft=0;}
    if ($row["user_id"] != "") {$the_user_id = $row["user_id"];}
  }

  if (($mycount == 1 or $createnew == 0) and !boxexist2($mboxname,$createnew,$the_user_id)) {
    $newid = mgw_genID('mgw__seq_mailboxes');
    $newaccount = 0;
    $sql = "insert into mgw_webmail2_mailboxes (createdby,created,mbox_id,user_id,mboxname,mboxparent,mbox_protect,isintrash,isdraft) values  (".(int)$the_user_id.",".time().",".(int)$newid.",".(int)$the_user_id.",'".$mboxname."',".(int)$createnew.",0,".(int)$isintrash.",".(int)$isdraft.")";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  } else {
    echo "<script>alert('".addslashes($hashmap["myfolders_folder_exists"])."');</script>";
  }
}

if ($account!=0 and $createnew!="" and is_numeric($createnew) and $mboxname != "") {

  $mboxname2 = $mboxname;
  if ($email = connect_my_imap($account,$createnew,true)) {
    $mboxname2 = str_replace(array("\\","/",".","'","\"","\$"),"",$mboxname2);

    $mailbox = $mbox_id_name.$delimiter.$mboxname2;

    if (sqimap_mailbox_exists($email, $mailbox)) {
      echo "<script>alert('".addslashes($hashmap["myfolders_folder_exists"])."');</script>";
    } elseif (sqimap_mailbox_exists($email, $mbox_id_name)) {
      sqimap_mailbox_create($email,$mailbox,"");
      $force_imap_reload = true;
    }
    sqimap_logout($email);
    
  }
}

if ($link_id=="" and $createnewlink!="" and is_numeric($createnewlink) and $href != "" and $desc != "" and ($createnewlink==-1 or $createnewlink==-2 or has_write_rights_mbox($createnewlink,$user_id))) {
  $desc = myquote($desc);
  $href = myquote($href);

  $mycount = 0;

  $sql = "select id from mgw_webmail2_links where (user_id=".(int)$user_id." or user_id=0) and mbox_id=".(int)$createnewlink." and description='".$desc."'";
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  $mycount = $res->RecordCount();

  if ($newwindow == "checked") {$newwindow = "1";} else {$newwindow = "0";}

  if ($createnewlink==-1) {
    $link_uid = $user_id;
  } elseif ($createnewlink==-2) {
    $link_uid = 0;
  } else {
    $sql = "select user_id from mgw_webmail2_mailboxes where mbox_id=".(int)$createnewlink;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();
	if (isset($row["user_id"]) and is_numeric($row["user_id"])) {
	  $link_uid = $row["user_id"];
	} else {
	  $mycount = 1;
	}
  }

  if ($mycount == 0) {
    $newid = mgw_genID('mgw__seq_links');
    $newaccount = 0;
    $sql = "insert into mgw_webmail2_links (id,href,description,newwindow,mbox_id,user_id) values  (".(int)$newid.",'".$href."','".$desc."','".$newwindow."',".(int)$createnewlink.",".(int)$link_uid.")";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

    $sql = "update mgw_webmail2_mailboxes set haslinks='1' where (user_id=".$_SESSION["MGW"]->userid." or user_id=0) and mbox_id=".(int)$createnewlink;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  } else {
    echo "<script>alert('".addslashes($hashmap["tree_link_exists"])."');</script>";
  }
}

if ($link_id!="" and is_numeric($link_id) and $href != "" and $desc != "") {
  $desc = myquote($desc);
  $href = myquote($href);

  $sql = "select mbox_id from mgw_webmail2_links where (user_id=".(int)$user_id." or user_id=0) and id=".(int)$link_id;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  $mycount2 = $res->RecordCount();
  $row = $res->FetchRow();

  if ($mycount2!=0 and ($row["mbox_id"]==-1 or $row["mbox_id"]==-2 or has_write_rights_mbox($row["mbox_id"],$user_id))) {
    if ($newwindow == "checked") {$newwindow = "1";} else {$newwindow = "0";}
  
	$sql = "update mgw_webmail2_links set href='".$href."',description='".$desc."',newwindow='".$newwindow."' where (user_id=".(int)$user_id." or user_id=0) and id=".(int)$link_id;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  }
}

$fields = array();

$messagecount = 0;
$tree = array();
$folders = array();
$folders2 = array();

$sql = "select * from mgw_webmail2_mailboxes where user_id=".(int)$user_id." order by mboxname!='inbox',mboxparent,mbox_protect desc,mboxname";
get_folders($sql,$user_id,true);

$images = array("inbox","draft","sent","trash");
foreach ($images as $image) {
  if (isset(${$image."_id"})) {
    $fields[] = 'field_0_'.${$image."_id"}.'.iconSrc = ICONPATH + "'.$image.'folderclosed.gif";';
    $fields[] = 'field_0_'.${$image."_id"}.'.iconSrcClosed = ICONPATH + "'.$image.'folderclosed.gif"';
  }
}

$sql = "select * from mgw_webmail2_accounts where mailboxname!='' and activated='checked' and servertype='wm2_servertype_imap_only' and user_id=".(int)$user_id;
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

checkdir(CACHE_FOLDER."imap/tree/",false);
while ($row = $res->FetchRow()) {
  get_imap_tree($row);
}

if (!isset($MGW->settings["wm2_disable_public_folders"]) or !$MGW->settings["wm2_disable_public_folders"]) {
  $fields[] = 'fieldpublic = insFld(foldersTree, gFld("'.$hashmap["webmail2_func_public_folders"].'", 0, -2,true,false,false,"javascript:parent.treeframe.op();",0,0,0,0))';

  $images = array("public");
  foreach ($images as $image) {
    $fields[] = 'field'.$image.'.iconSrc = ICONPATH + "'.$image.'folderclosed.gif";';
    $fields[] = 'field'.$image.'.iconSrcClosed = ICONPATH + "'.$image.'folderclosed.gif"';
  } 
}

$fields[] = 'foldersTree.iconSrc = ICONPATH + "userfolderclosed.gif";';
$fields[] = 'foldersTree.iconSrcClosed = ICONPATH + "userfolderclosed.gif"';

if (isset($MGW->settings["wm2_disable_public_folders"]) and $MGW->settings["wm2_disable_public_folders"]) {
  $fields[] = 'fieldpublic1=false;';
} else {
  $fields[] = 'fieldpublic1=true;';

  $sql = "select * from mgw_webmail2_mailboxes where user_id=0 order by mboxparent,mbox_protect desc,mboxname";
  get_folders($sql,0,true);
}

$smarty->assign("opentrash", $opentrash);
$smarty->assign("trashid", $trashid);

if ($newid != "" and $newaccount !="") {
  $smarty->assign("newid", $newaccount.",".$newid);
} else {
  $smarty->assign("newid", $newid);
}

if ($force_imap_reload) {
  $smarty->assign("reload_sidebar", "yes");
} else {
  $smarty->assign("reload_sidebar", "no");
}

if (isset($_SESSION["MGW"]->settings["wm2_openallfolders"])) {
  $smarty->assign("wm2_openallfolders", $_SESSION["MGW"]->settings["wm2_openallfolders"]);
} else {
  $smarty->assign("wm2_openallfolders", 0);
}

if (isset($_SESSION["MGW"]->settings["wm2_preserve_state"])) {
  $smarty->assign("wm2_preserve_state", $_SESSION["MGW"]->settings["wm2_preserve_state"]);
} else {
  $smarty->assign("wm2_preserve_state", 1);
}

if (isset($_SESSION["MGW"]->settings["wm2_auto_open_unread_folders"])) {
  $smarty->assign("wm2_auto_open_unread_folders", $_SESSION["MGW"]->settings["wm2_auto_open_unread_folders"]);
} else {
  $smarty->assign("wm2_auto_open_unread_folders", 1);
}

$smarty->assign("fields", $fields);
$smarty->assign("myimgpathmaster", "templates/default/media");

$smarty->assign("username", ucfirst($username));
$smarty->display("tree.tpl");

?>