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

  if (!defined('ROOTPATH')) die;

  if ($old_version < 75) {
    $schema2 = new adoSchema( $conn, true, false);
    foreach ($schema2->legacyTables as $table) {
  	  if (strpos("@".$table,"mgw_webmail_")==1) {
	    $old_table = $table;
		$new_table = str_replace("mgw_webmail_","mgw_webmail2_",$table);
		echo "Renaming table ".$old_table." to ".$new_table." ...<br>";

	    $sql = "ALTER TABLE ".$old_table." RENAME TO ".$new_table;

		if (($appconf["dbvendor"]!="mysql" and $appconf["dbvendor"]!="postgres7") or in_array($new_table,$schema2->legacyTables)) {
		  die("<br>Please update your database manually. ".$sql);
		}
	    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  }
    }
    @$schema2->Destroy();

	echo "Moving msg_body-fields to new store folder ...<br>";
    $sql = "select message_id,msg_body from mgw_webmail2_messages";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    while($row = $res->FetchRow()) {
	  if (strpos("@".$row["msg_body"],"store/")==1) {
        $sql = "update mgw_webmail2_messages set msg_body=".$conn->Quote(str_replace("store/","../../data_store/webmail2/",$row["msg_body"]))." where message_id=".(int)$row["message_id"];
        if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	  }
	}
  }

  
  if (isset($appconf["demo"]) and $appconf["demo"]) {
	require_once(INCLUDEPATH . "crypt.inc.php");
	$myC	= new Crypto();

	$host = $_SERVER["HTTP_HOST"];
	$users = array("user0","user1","user2","user3","user4","user5","user6","user7","user8","user9");
	foreach ($users as $user) {
	  $sql = "SELECT id FROM mgw_users WHERE username=".$conn->Quote($user);
	  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	  $row = $res->FetchRow();

	  if (isset($row["id"])) {
  	    $user_id = $row["id"];
	  
	    $sep = "@";
	    $smtp = "checked";
	    $type = "wm2_servertype_imap_only";

	    $sql = "SELECT mailboxname FROM mgw_webmail2_accounts WHERE mailboxname='".$user.$sep.$host."'";
	    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	    $row = $res->FetchRow();

	    if (!isset($row["mailboxname"])) {
	      $sql = "INSERT INTO `mgw_webmail2_accounts` (`mailboxname`, `folderid`, `copyfolderid`, `servertype`, `imap_list_all`, `imap_show_numbers`, `pophost`, `smtphost`, `smtpauth`, `smtpusepopuser`, `usemailfunction`, `smtpusername`, `smtppassword`, `username`, `password`, `leavemessages`, `popbeforesmtp`, `enable_ssl`, `enable_ssl_smtp`, `activated`, `fullname`, `signature`, `signature_html`, `replyto`, `usermaychange`, `enable_smtp`, `user_id`, `leaveoldmails`, `id`) VALUES (".
		    "'".$user.$sep.$host."',0,0,'".$type."','','checked','localhost','localhost','','','','','','".$user."','".$myC->encrypt($user, $user)."','', '', '', '', 'checked','".$user."_first ".$user."_last','','','".$user."@".$host."','','".$smtp."',".$user_id.", '',".mgw_genID('mgw__seq_accounts',1).")";
	      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	    }

	    $sep = "_";
	    $smtp = "";
	    $type = "wm2_servertype_imap";

	    $sql = "SELECT mailboxname FROM mgw_webmail2_accounts WHERE mailboxname='".$user.$sep.$host."'";
	    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	    $row = $res->FetchRow();

	    if (!isset($row["mailboxname"])) {
	      $sql = "INSERT INTO `mgw_webmail2_accounts` (`mailboxname`, `folderid`, `copyfolderid`, `servertype`, `imap_list_all`, `imap_show_numbers`, `pophost`, `smtphost`, `smtpauth`, `smtpusepopuser`, `usemailfunction`, `smtpusername`, `smtppassword`, `username`, `password`, `leavemessages`, `popbeforesmtp`, `enable_ssl`, `enable_ssl_smtp`, `activated`, `fullname`, `signature`, `signature_html`, `replyto`, `usermaychange`, `enable_smtp`, `user_id`, `leaveoldmails`, `id`) VALUES (".
		    "'".$user.$sep.$host."',0,0,'".$type."','','checked','localhost','localhost','','','','','','".$user."','".$myC->encrypt($user, $user)."','', '', '', '', 'checked','".$user."_first ".$user."_last','','','".$user."@".$host."','','".$smtp."',".$user_id.", '',".mgw_genID('mgw__seq_accounts',1).")";
	      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	    }
	  }
	}
  }
  
?>
