<?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_editaccount
// Moregroupware.org


// =================================================
//	              Formular Mail-Editaccount
// =================================================
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 formatforform_assign ($myarray,$smarty) {
  foreach ($myarray as $key=>$item) {
	$smarty->assign($key, htmlspecialchars(stripslashes(myquote($item))));
  }
}

function editaccount($conn,$smarty,$account)
{
	$MGW = $_SESSION["MGW"];
    $user_id = $MGW->userid;
    $username = $MGW->username;

    if ($username == "admin") {
	  $sql = "select * from mgw_webmail2_accounts where id=".(int)$_GET["account"];
    } else {
	  $sql = "select * from mgw_webmail2_accounts where id=".(int)$_GET["account"]." and user_id=".(int)$user_id;
    }
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();

    $sql = "select username from mgw_users where id=".(int)$row["user_id"];
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row2 = $res->FetchRow();
	$username2 = $row2["username"];
	
	$myC = new Crypto();
  	$wm2_password = $myC->decrypt($username2, $row["password"]);
  	$wm2_smtppassword = $myC->decrypt($username2, $row["smtppassword"]);

	if ($row["servertype"]=="wm2_servertype_pop3") {
	  $smarty->assign("wm2_servertype_pop3","selected");
	  $smarty->assign("wm2_servertype_imap","");
	  $smarty->assign("wm2_servertype_imap_only","");
	} else if ($row["servertype"]=="wm2_servertype_imap") {
	  $smarty->assign("wm2_servertype_pop3","");
	  $smarty->assign("wm2_servertype_imap","selected");
	  $smarty->assign("wm2_servertype_imap_only","");
	} else {
	  $smarty->assign("wm2_servertype_pop3","");
	  $smarty->assign("wm2_servertype_imap","");
	  $smarty->assign("wm2_servertype_imap_only","selected");
	}

	$smarty->assign("wm2_users", getallusers());
	$smarty->assign("wm2_user", $username);
	$smarty->assign("wm2_the_user_id", $row["user_id"]);
    
    $vars = array(
      "wm2_password"        => $wm2_password,
      "wm2_mailboxname"     => $row["mailboxname"],
      "wm2_fullname"        => $row["fullname"],
      "wm2_signature"        => $row["signature"],
      "wm2_signature_html"        => $row["signature_html"],
      "wm2_imap_list_all" => $row["imap_list_all"],
      "wm2_imap_show_numbers" => $row["imap_show_numbers"],
      "wm2_replyto"         => $row["replyto"],
      "wm2_pophost"         => $row["pophost"],
      "wm2_usemailfunction" => $row["usemailfunction"],
      "wm2_smtphost"        => $row["smtphost"],
      "wm2_popbeforesmtp"   => $row["popbeforesmtp"],
      "wm2_smtpauth"        => $row["smtpauth"],
      "wm2_smtpusername"    => $row["smtpusername"],
      "wm2_smtppassword"    => $wm2_smtppassword,
      "wm2_smtpusepopuser"  => $row["smtpusepopuser"],
      "wm2_username"        => $row["username"],
      "wm2_leavemessages"   => $row["leavemessages"],
      "wm2_activated"       => $row["activated"],
      "wm2_usermaychange"   => $row["usermaychange"],
      "wm2_enable_ssl"      => $row["enable_ssl"],
      "wm2_enable_ssl_smtp" => $row["enable_ssl_smtp"],
      "wm2_enable_smtp" 	=> $row["enable_smtp"],
    );
    formatforform_assign($vars,&$smarty);

	$smarty->assign("php_version", str_replace(".","",PHP_VERSION));

	$smarty->assign('action',"editaccountsave");
	$smarty->assign('account',$_GET["account"]);
	$smarty->assign("suname","editaccountsave");
	$smarty->assign("suclass","submit");

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

function editaccountsave()
{
	global $conn;
	$MGW = $_SESSION["MGW"];
    $user_id = $MGW->userid;
    $username = $MGW->username;
	
	$myvars = array("wm2_usermaychange","wm2_popbeforesmtp","wm2_enable_ssl","wm2_enable_ssl_smtp","wm2_enable_smtp","wm2_usemailfunction","wm2_smtpauth","wm2_smtpusername","wm2_smtpusepopuser","wm2_fullname","wm2_signature","wm2_signature_html","wm2_replyto","wm2_servertype","wm2_pophost","wm2_smtphost","wm2_username","wm2_leavemessages","account","wm2_activated","wm2_password","wm2_smtppassword","the_user_id","wm2_imap_list_all","wm2_imap_show_numbers");
	foreach ($myvars as $thevar) {
	  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
	}

    $vars = array("usermaychange","fullname","signature","signature_html","replyto","pophost","smtphost","username","smtpusername","imap_list_all","imap_show_numbers");
    foreach ($vars as $item) {
      ${"wm2_".$item} = myquote(${"wm2_".$item});
    }

	$sql = "select user_id from mgw_webmail2_accounts where id=".(int)$account;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();
	$the_user_id = $row["user_id"];
	
    if ($username == "admin") {
      $sql = "select username from mgw_users where id=".(int)$the_user_id;
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
      $row = $res->FetchRow();
	  $username2 = $row["username"];
	} else {
	  $username2 = $username;
	}
	
	$myC	= new Crypto();
	$wm2_password	= $myC->encrypt($username2, $wm2_password);
	$wm2_smtppassword	= $myC->encrypt($username2, $wm2_smtppassword);

	$wm2_signature_html = clean_html_signature($wm2_signature_html);	
    if ($username == "admin") {
      $sql = "update mgw_webmail2_accounts set imap_show_numbers='".$wm2_imap_show_numbers."',imap_list_all='".$wm2_imap_list_all."',usermaychange='".$wm2_usermaychange."',enable_ssl='".$wm2_enable_ssl."',enable_ssl_smtp='".$wm2_enable_ssl_smtp."',enable_smtp='".$wm2_enable_smtp."',popbeforesmtp='".$wm2_popbeforesmtp."',usemailfunction='".$wm2_usemailfunction."',smtpauth='".$wm2_smtpauth."',smtpusername='".$wm2_smtpusername."',smtppassword=".$conn->Quote($wm2_smtppassword).",smtpusepopuser='".$wm2_smtpusepopuser."',password=".$conn->Quote($wm2_password).",fullname='".$wm2_fullname."',signature='".$wm2_signature."',signature_html='".$wm2_signature_html."',replyto='".$wm2_replyto."',servertype='".$wm2_servertype."',pophost='".$wm2_pophost."',smtphost='".$wm2_smtphost."',username='".$wm2_username."',leavemessages='".$wm2_leavemessages."',activated='".$wm2_activated."' where id=".(int)$account;
    } else {
      $wm2_usermaychange="checked";
      $sql = "update mgw_webmail2_accounts set imap_show_numbers='".$wm2_imap_show_numbers."',imap_list_all='".$wm2_imap_list_all."',usermaychange='checked',enable_ssl='".$wm2_enable_ssl."',enable_ssl_smtp='".$wm2_enable_ssl_smtp."',enable_smtp='".$wm2_enable_smtp."',popbeforesmtp='".$wm2_popbeforesmtp."',usemailfunction='".$wm2_usemailfunction."',smtpauth='".$wm2_smtpauth."',smtpusername='".$wm2_smtpusername."',smtppassword=".$conn->Quote($wm2_smtppassword).",smtpusepopuser='".$wm2_smtpusepopuser."',password=".$conn->Quote($wm2_password).",fullname='".$wm2_fullname."',signature='".$wm2_signature."',signature_html='".$wm2_signature_html."',replyto='".$wm2_replyto."',servertype='".$wm2_servertype."',pophost='".$wm2_pophost."',smtphost='".$wm2_smtphost."',username='".$wm2_username."',leavemessages='".$wm2_leavemessages."',activated='".$wm2_activated."' where id=".(int)$account." and user_id=".(int)$user_id;
    }
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
}

function deleteaccount($conn,$smarty,$account)
{
	$MGW = $_SESSION["MGW"];
    $user_id = $MGW->userid;
    $username = $MGW->username;

    if ($username == "admin") {
	    $sql = "delete from mgw_webmail2_accounts where mailboxname!='default' and id=".(int)$_GET["account"];
    } else {
	    $sql = "delete from mgw_webmail2_accounts where mailboxname!='default' and id=".(int)$_GET["account"]." and user_id=".(int)$user_id;
    }
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
}
?>