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

// webmail2_settings_listaccounts
// Moregroupware.org


// =================================================
//	              Formular ListAccounts
// =================================================
require_once(INCLUDEPATH . "crypt.inc.php");
require_once(INCLUDEPATH . "mSelectBox.class.php");
include(WM2_INC_DIR.'accounts_inc.php');

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

function listaccounts($conn,$smarty)
{
    global $showpwd,$hashmap;
    $MGW = $_SESSION["MGW"];
    $user_id = $MGW->userid;
    $username = $MGW->username;

	$myC = new Crypto();

    if ($username == "admin") {
      $sql = "select * from mgw_webmail2_accounts order by mailboxname!='default',user_id,mailboxname";
      $adminmode = true;
	} else {
      $sql = "select * from mgw_webmail2_accounts where user_id=".(int)$user_id." order by mailboxname";
      $adminmode = false;
    }
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

    $vars = array("nums","password","servertype","leavemessages","mailboxname","fullname","signature","signature_html","replyto","pophost",
                  "smtphost","username","smtpusername","smtpauth","usemailfunction","smtpusepopuser","smtppassword",
                  "activated","enable_ssl","enable_ssl_smtp","enable_smtp","popbeforesmtp","my_username","usermaychange","usermaychange2","imap_list_all","imap_show_numbers");
    foreach ($vars as $myvar) {
      ${"wm2_".$myvar} = array();
    }
        
    while($row = $res->FetchRow()) {
  	  $wm2_nums[] = $row["id"];

      $sql = "select username from mgw_users where id=".(int)$row["user_id"];
      if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
      $row2 = $res2->FetchRow();
	  $username2 = $row2["username"];

  	  $wm2_password[] = stripslashes($myC->decrypt($username2, $row["password"]));
  	  $wm2_smtppassword[] = stripslashes($myC->decrypt($username2, $row["smtppassword"]));

      if ($row["servertype"]=="wm2_servertype_pop3") { $wm2_servertype[] = "Pop3"; }
        else if ($row["servertype"]=="wm2_servertype_imap") { $wm2_servertype[] = "Imap"; }
        else { $wm2_servertype[] = "Imap only"; }

      $yes = $hashmap["settings_activated_yes"];
      $no = $hashmap["settings_activated_no"];

	  $row["signature"] = str_replace("\n","<br />",strip_tags($row["signature"]));
	  
      $vars = array("smtpauth","activated","enable_ssl","popbeforesmtp","leavemessages","imap_list_all","imap_show_numbers","smtpusepopuser","enable_ssl_smtp","enable_smtp","usemailfunction");
      foreach ($vars as $item) {
        ${"wm2_".$item}[] = ($row[$item] == "checked" ? $yes : $no);
      }
    
      if ($adminmode) { 
        $wm2_usermaychange2[] = ($row["usermaychange"] == "checked" ? $yes : $no);
        $wm2_my_username[] = ucfirst(get_myusername($row["user_id"])).": "; 
        $row["usermaychange"] = "checked";
      }

      $vars = array("usermaychange","mailboxname","fullname","signature","signature_html","replyto","pophost","smtphost","username","smtpusername");
      foreach ($vars as $item) {
        ${"wm2_".$item}[] = quote2($row[$item]);
      }
    }
    
    $vars = array("usermaychange","my_username","nums","servertype","leavemessages","imap_list_all","imap_show_numbers","popbeforesmtp","activated","enable_ssl","enable_ssl_smtp","enable_smtp","password","mailboxname",
                  "fullname","signature","signature_html","replyto","pophost","smtphost","usemailfunction","smtpauth","smtpusername","smtppassword","smtpusepopuser","username","usermaychange2");
    foreach ($vars as $item) {
  	  $smarty->assign("wm2_".$item, ${"wm2_".$item});
    }

    if ($username != "admin" and isset($MGW->settings["wm2_disable_create_accounts"]) and $MGW->settings["wm2_disable_create_accounts"]) {
	  $smarty->assign("disable_create_accounts", "yes");
      $smarty->assign("showpwd", "");
    } else {
	  $smarty->assign("disable_create_accounts", "no");
	  $smarty->assign("showpwd", $showpwd);
    }
    
	$smarty->assign("php_version", str_replace(".","",PHP_VERSION));
	$smarty->assign("wm2_default_account",isset($_SESSION["MGW"]->settings["wm2_default_account"])?$_SESSION["MGW"]->settings["wm2_default_account"]:"");

	$smarty->display('webmail2_settings_listaccounts.tpl');
	die;
}
?>