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

// Properties
// Moregroupware.org

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

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

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$myvars = array("mbox_id");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}

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

$folderfound = false;
$full_path = "";
$folder_name = "";

$mbox_id2 = $mbox_id;

if ($mbox_id == -1) {
  $mbox_id = 0;
  $mbox_id2 = -1;
}

if ($mbox_id == -2) {
  $mbox_id = 0;
  $mbox_id2 = 0;
  $user_id2 = 0;
}

if (!is_numeric($mbox_id) or $mbox_id < 0) {
  $mbox_id=getboxid("inbox",0,$user_id);
}

if ($mbox_id != 0 and !has_read_rights_mbox($mbox_id,$user_id)) { die("Access denied."); }

$full_date = $hashmap["properties_unknown"];
$createdby = $hashmap["properties_unknown"];  

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

if ($res->RecordCount()!=0) {
  $folderfound = true;
  $row = $res->FetchRow();
  $user_id2 = $row["user_id"];
  if ($row["mbox_protect"]) { $row["mboxname"] = mboxname_replace($row["mboxname"]); }
  $folder_name = $row["mboxname"];

  $users = getallusers2();
  if (isset($users[$row["createdby"]])) {
    $createdby = ucfirst($users[$row["createdby"]]);
  }
  
  if ($row["created"] != 0) {
    $full_date = getDateString($row["created"]);
  }
} 
  
  $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__));
	  
  $fathers = array();
  $children = array();
  $tree = array();

  while($row = $res->FetchRow()) {
    if ($folderfound) {
      addtotree($row["mboxparent"],$tree,$row["mbox_id"],0);
    } else {
      $children[] = $row["mbox_id"];
    }
  }

  if ($folderfound) {
    getchildren($tree,$mbox_id);
    getpath($tree,$mbox_id,array());
  }

  $full_count = 0;
  $full_size = 0;
  $full_links = 0;
  $full_folders = count($children);
  
  $children2 = $children;
  $children[] = $mbox_id;
  $children2[] = $mbox_id2;
  
  foreach ($children as $key=>$child) {
    $sql = "select message_id from mgw_webmail2_messages where mbox_id=".(int)$child;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $full_count += $res->RecordCount();

    $sql = "select header_size from mgw_webmail2_messages where mbox_id=".(int)$child;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

    while($row = $res->FetchRow()) {
      $full_size += $row["header_size"];
    }

    $sql = "select id from mgw_webmail2_links where user_id=".(int)$user_id2." and mbox_id=".(int)$children2[$key];
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
//    if ($res->RecordCount() != 0) echo $child."@";
    $full_links += $res->RecordCount();
  }

  foreach ($fathers as $father) {
    $sql = "select mbox_protect,mboxname from mgw_webmail2_mailboxes where mbox_id=".(int)$father;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();
    
    if ($row["mbox_protect"]) { $row["mboxname"] = mboxname_replace($row["mboxname"]); }
    $full_path .= $row["mboxname"] . " / ";
  }


  
$full_size = format_filesize($full_size);

// $disk_size = format_filesize(disk_total_space("."));
// $disk_free_size = format_filesize(disk_free_space("."));


if ($folder_name != "") {
  $full_path .= "<b>".$folder_name."</b>";
}
if ($user_id2 == 0) { 
  $folder_root = $hashmap["publicfolders_folders"];
  if ($full_path != "") { $folder_root .= " / "; } else {
    $folder_root = "<b>".$folder_root."</b>";
  }
} else {
  $folder_root = ucfirst($username);
  if ($full_path != "") { $folder_root .= " / "; } else {
    $folder_root = "<b>".$folder_root."</b>";
  }
}

$full_path = $folder_root.$full_path;

// $smarty->assign("disk_free_size", str_replace(" ","</b> ",$disk_free_size));
// $smarty->assign("disk_size", str_replace(" ","</b> ",$disk_size));

$smarty->assign("createdby", $createdby);
$smarty->assign("full_date", $full_date);
$smarty->assign("full_mails", $full_count);
$smarty->assign("full_links", $full_links);
$smarty->assign("full_folders", $full_folders);
$smarty->assign("full_size", str_replace(" ","</b> ",$full_size));
$smarty->assign("full_path", $full_path);

$smarty->assign("username", ucfirst($username));
$smarty->assign("mbox_id", $mbox_id);
$smarty->display("properties.tpl");
?>
