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


// =================================================
//	              Formular New-Mail-Account
// =================================================
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 newaccount($conn,$smarty,$message,$type)
{
    global $hashmap;
	if ($message) {$smarty->assign('message',$hashmap["webmail2_settings_mailboxname_exists_or_is_empty"]);} else {$smarty->assign('message',"");}
	$MGW = $_SESSION["MGW"];
    $user_id = $MGW->userid;
    $username = $MGW->username;
    $fullusername = $MGW->fullusername;

	$sql = "select * from mgw_webmail2_accounts where mailboxname='default' and user_id=1";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row = $res->FetchRow();

    if ($username != "admin") {
      $row["fullname"] = str_replace("\$fullusername",$fullusername,$row["fullname"]);
      $row["replyto"] = str_replace("\$email",get_myuseremail($user_id),$row["replyto"]);
      $row["username"] = str_replace("\$username",$username,$row["username"]);
      $row["smtpusername"] = str_replace("\$username",$username,$row["smtpusername"]);
    }
    
    
    $smarty->assign("wm2_users", getallusers());
	$smarty->assign("wm2_user", $username);
	$smarty->assign("wm2_the_user_id", $row["user_id"]);

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

	$smarty->assign("wm2_mailboxname", "");
	$smarty->assign("wm2_password", "");
	$smarty->assign("php_version", str_replace(".","",PHP_VERSION));
    
    if ($type=="imap") {$row["servertype"] = "wm2_servertype_imap_only";}
	$smarty->assign("type",$type);
    
	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('action',"newaccountsave");
	$smarty->assign("suname","newaccountsave");
	$smarty->assign("suclass","submit");

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

function newaccountsave()
{
    global $conn;
    $MGW = $_SESSION["MGW"];
    $user_id = $MGW->userid;
    $username = $MGW->username;

	$myvars = array("wm2_mailboxname","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_usermaychange","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","mailboxname","fullname","signature","signature_html","replyto","pophost","smtphost","username","smtpusername","imap_list_all","imap_show_numbers");
    foreach ($vars as $item) {
      ${"wm2_".$item} = myquote(${"wm2_".$item});
    }

    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;
	}

	// encrypt pop3password before storing in db (key = actual username)
	$myC	= new Crypto();
	$wm2_password	= $myC->encrypt($username2, $wm2_password);
	$wm2_smtppassword	= $myC->encrypt($username2, $wm2_smtppassword);

    if ($username != "admin") {
	  $sql = "select id from mgw_webmail2_accounts where mailboxname='".$wm2_mailboxname."' and user_id=".(int)$user_id;
    } else {
	  $sql = "select id from mgw_webmail2_accounts where mailboxname='".$wm2_mailboxname."' and user_id=".(int)$the_user_id;
    }
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $mycount = $res->RecordCount();

    if ($username != "admin") {
      $the_user_id = $user_id;
      $wm2_usermaychange = "checked";
    } else {
      $wm2_fullname = str_replace("\$fullusername",get_myfullusername($the_user_id),$wm2_fullname);
      $wm2_replyto = str_replace("\$email",get_myuseremail($the_user_id),$wm2_replyto);
      $wm2_username = str_replace("\$username",get_myusername($the_user_id),$wm2_username);
      $wm2_smtpusername = str_replace("\$username",get_myusername($the_user_id),$wm2_smtpusername);
    }
    
	if ($mycount==0 and $wm2_mailboxname!="") {	

   	  $wm2_signature_html = clean_html_signature($wm2_signature_html);	

      $sql = "insert into mgw_webmail2_accounts (id,imap_list_all,imap_show_numbers,enable_ssl,enable_ssl_smtp,enable_smtp,popbeforesmtp,mailboxname,fullname,signature,signature_html,replyto,servertype,pophost,smtphost,smtpauth,usemailfunction,smtpusername,smtppassword,smtpusepopuser,username,password,leavemessages,activated,usermaychange,user_id) values 
	  (".mgw_genID('mgw__seq_accounts',1).",
	  '".$wm2_imap_list_all."', '".$wm2_imap_show_numbers."',
      '".$wm2_enable_ssl."',
      '".$wm2_enable_ssl_smtp."','".$wm2_enable_smtp."','".$wm2_popbeforesmtp."',
	  '".$wm2_mailboxname."','".$wm2_fullname."',
	  '".$wm2_signature."','".$wm2_signature_html."',
	  '".$wm2_replyto."','".$wm2_servertype."',
	  '".$wm2_pophost."','".$wm2_smtphost."',
	  '".$wm2_smtpauth."','".$wm2_usemailfunction."',
	  '".$wm2_smtpusername."',
	  ".$conn->Quote($wm2_smtppassword).",'".$wm2_smtpusepopuser."',
	  '".$wm2_username."',".$conn->Quote($wm2_password).",
	  '".$wm2_leavemessages."','".$wm2_activated."',
      '".$wm2_usermaychange."',
	  ".(int)$the_user_id.")";
	  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

	  return true;
	} else {return false;}
}

?>