<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

include_once("auth.php");

include_once("../include/module.Database.php");
include_once("../include/class.Cond.php");
include_once("../include/class.Data.php");
include_once("../include/class.Page.php");
include_once("../events.inc.php");
include_once("../include/event_functions.php");

if ($loeschen <> "ja")
        {
         $fehlertext = ltext("kuaelo2_error1",array($kunde));
         $fehlertitel = ltext("kuaelo2_errortitle");
         include("kunden_aendern.php");
         exit();

  } else {

## mysql verbindung aufbauen
db_connect($db_host, $db_user, $db_pass);
## /mysql verbindung aufbauen

 $loUser = new SQLString($PHP_AUTH_USER);
 $id = safe_query2( "SELECT frontpage, cssl, maxmysql FROM anbieter WHERE anbieter=?", $loUser);
$werte = db_fetch_array($id);
$skfrontpage = $werte["frontpage"];
$skssl = $werte["cssl"];
$maxmysql = $werte["maxmysql"];
 $loKunde = new SQLString($kunde);
if ($maxmysql != 0) $skmysql = 1;

## überprüfen, ob kunde existiert und dem anbieter gehört
 
 $id = safe_query2( "SELECT kunde FROM kunden WHERE kunde=? and anbieter=?",$loKunde,  $loUser);
$zeilen = db_num_rows($id);
if ($zeilen <> 1)
 {
  $fehlertext = ltext("kuaelo2_error1");
  db_close();
  include("kunden_aendern.php");
  exit();
 }
## /überprüfen, ob kunde existiert und dem anbieter gehört

 $id = safe_query2( "SELECT account FROM delaccs WHERE account = ?", $loKunde);
if (db_num_rows($id) == 0)        {
	safe_query("INSERT INTO delaccs (account, type, kunde, anbieter) VALUES (?, 'kunde', ?, ?)", $loKunde, $loKunde, $loUser);
}

## delete pop3 accounts
 $id = safe_query2( "SELECT account FROM pop3 WHERE kunde=? AND anbieter=?", $loKunde, $loUser);
while ($werte = db_fetch_array($id)){
	$loDelpop = new SQLString($werte["account"]);
  $id2 = safe_query2( "SELECT account FROM delaccs WHERE account = ?", $loDelpop );
  if (db_num_rows($id2) == 0){
	  safe_query2( "INSERT INTO delaccs (account, type) VALUES (?, 'pop3')", $loDelpop);
  }
  
	safe_query2( "DELETE FROM webmail_ab WHERE pop3 = ?", $loDelpop);
	safe_query2( "DELETE FROM webmail_session WHERE pop3 = ?", $loDelpop);
	safe_query2( "DELETE FROM webmail_sign WHERE pop3 = ?", $loDelpop);
	safe_query2( "DELETE FROM webmail_def_from WHERE pop3 = ?", $loDelpop);
	safe_query2( "DELETE FROM spampref WHERE username = ?", $loDelpop);
}
 safe_query2( "DELETE FROM pop3 WHERE kunde=?", $loKunde);
## end delete pop3 accounts

## delete ftp accounts
 $id = safe_query2( "SELECT account, pfad FROM ftp WHERE kunde=? AND anbieter=?", $loKunde, $loUser);
while ($werte = db_fetch_array($id)){
	$loDelftp = new SQLString($werte["account"]);
	$loPfad = new SQLString( $werte['pfad'] );
  $id2 = safe_query2( "SELECT account FROM delaccs WHERE account = ?", $loDelftp );
  if (db_num_rows($id2) == 0){
	  safe_query2( "INSERT INTO delaccs (account, type, kunde, pfad ) VALUES (?, 'ftp', ?, ?)",
								 $loDelftp, $loKunde, $loPfad );
  }
}
 safe_query2( "DELETE FROM ftp WHERE kunde=?", $loKunde );
## end delete ftp accounts

 safe_query2( "DELETE FROM email WHERE kunde=? and neu = 1", $loKunde );
 $idE = safe_query2( "SELECT * FROM email WHERE kunde = ?", $loKunde );
while ($werteE = db_fetch_array($idE))
        {
					$loAccount = new SQLString( $werteE["prefix"] );
					$loPfad = new SQLSTring( $werteE["domain"] );
					safe_query2( "INSERT INTO delaccs (account, type, kunde, pfad) VALUES ( ?, 'alias', ?, ? )", 
											 $loAccount,$loKunde,$loPfad );
        }
 safe_query2( "DELETE FROM email WHERE kunde=?", $loKunde);

## domains löschen
 $domainid = safe_query2( "SELECT domain, id FROM domains WHERE kunde=?", $loKunde );
$domains_arr = array();
while ($domainwerte = db_fetch_array($domainid))
{
	$domains_arr[] = $domainwerte['id'];
	$deldomain = $domainwerte["domain"];
	domain_aendern($deldomain, "loeschen", $kunde);
	fpalias_aendern($deldomain, "loeschen", $kunde);
}
if(count($domains_arr))
{
	$ids = array();
	$query = db_query ( "SELECT id FROM maillist WHERE domain_id IN (" . implode(',', $domains_arr) . ") AND neu<>3" );
	while ( $werte = db_fetch_array($query) )
		$ids[] = $werte["id"];
	MailListPage::deleteLists ($ids);
	db_query("DELETE FROM atdomains WHERE id_from IN (" . implode( ",", $domains_arr ) . ")");
}

// delete idn aliases if any
 $res_dom_id = safe_query( "SELECT id FROM domains WHERE kunde=?", $loKunde );
while($row_dom_id = db_fetch_array($res_dom_id)) {
	$domain_id = $row_dom_id['id'];
	db_query("delete from idn_aliases where domain_id='$domain_id'");
}
db_close($res_dom_id);

 safe_query2("DELETE FROM kunden WHERE kunde=? AND anbieter=?", $loKunde, $loUser);
 safe_query2("UPDATE cronjobs SET cronfile='', neu=1 WHERE kunde=?", $loKunde);
 safe_query2("DELETE FROM domains WHERE kunde=?", $loKunde);
 safe_query2("UPDATE cssl SET loeschen = 1 WHERE kunde=?", $loKunde);
 safe_query2("DELETE FROM autoresponder WHERE kunde=?", $loKunde);
db_query("UPDATE allgemein SET deluser=1, delpop=1, delftp=1, httpd=1, aliases=1");
db_query("UPDATE allgemein SET dns=1");
safe_query2("UPDATE ipadressen SET kunde='' WHERE kunde=? AND anbieter=?", $loKunde, $loUser);
safe_query2("UPDATE transfer SET geloescht=1 WHERE kunde=?", $loKunde);
safe_query2("DELETE FROM pwschutz WHERE kunde=?", $loKunde);
safe_query2("DELETE FROM users WHERE kunde=?", $loKunde);
safe_query2("DELETE FROM htaccess WHERE kunde=?", $loKunde);
safe_query2("DELETE FROM fehlermeldungen WHERE kunde=?", $loKunde);
safe_query2("UPDATE ereignisse SET geloescht=1 WHERE kunde=?", $loKunde);
safe_query2("UPDATE frontpage SET loeschen=1, neu=0 WHERE kunde=?", $loKunde);
db_query("UPDATE allgemein SET frontpagedel=1");
safe_query2("DELETE FROM perl WHERE kunde=?", $loKunde);
safe_query2("DELETE FROM webalizer WHERE kunde=?", $loKunde);
db_query("UPDATE allgemein SET frontpage=1, cssl=1");
$delid = safe_query2("select dbname from mysql_datenbanken WHERE kunde = ? AND anbieter = ?", $loKunde, $loUser);
safe_query2("DELETE FROM mysql_datenbanken WHERE kunde = ? AND anbieter = ?", $loKunde, $loUser);

## dns löschen
safe_query2("UPDATE dns SET neu=2 WHERE kunde=? AND anbieter=?", $loKunde, $loUser);


if ( userdb_connect( false ) ) {

	$lcKunde = $loKunde->SQL();
	userdb_query( "DELETE FROM db WHERE User = $lcKunde" );
	userdb_query( "DELETE FROM user WHERE User = $lcKunde");

	while ($werte = db_fetch_array( $delid ) ) {
		$lcDb = addslashes($werte['dbname']);
		userdb_query( "DROP DATABASE ".$lcDb );
	}
	userdb_query( "FLUSH PRIVILEGES" );
	userdb_close();
}

$fehlertext = ltext("kuaelo2_error3",array($kunde));
$fehlertitel = ltext("kuaelo2_errortitle");

db_close();

include("kunden_aendern.php");

}
?>
