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

include_once( "../include/class.SQLFieldSet.php" );

global $DEBUG;
//$DEBUG = 1;

if (isset($HTTP_USER_AGENT) && strpos($HTTP_USER_AGENT,"IE") > 0)

$Rows = "60";

else

$Rows = "40";

include_once("auth.php");
include_once("../include/module.Database.php");



$timestamp = time();

## checken ob feldname angegeben, wenn feldwert angegeben

if ( ((!istleer($freifeldwert1)) && (istleer($freifeldname1)))
  || ((!istleer($freifeldwert2)) && (istleer($freifeldname2)))
  || ((!istleer($freifeldwert3)) && (istleer($freifeldname3)))){

  $fehlertext = ltext("kune5_error1");

  include("kunden_neu3.php");

  exit();
}

## /checken ob feldname angegeben, wenn feldwert angegeben



## falls keine kundendaten angegeben wurden soll kundendatenanzeigen=nein sein

if ((istleer($name))
 && (istleer($firma))
 && (istleer($anschrift))
 && (istleer($plzort))
 && (istleer($telefon))
 && (istleer($fax))
 && (istleer($emailadresse))
 && (istleer($kundennummer))
 && (istleer($freifeldname1))
 && (istleer($freifeldname2))
 && (istleer($freifeldname3))){

  $kundendatenanzeigen = "0";

}

## /falls keine kundendaten angegeben wurden soll kundendatenanzeigen=nein sein


$ausdb = "nein";

## daten überprüfen

## speicherplatz

if (!ereg("^(0|-?[1-9][0-9]*)$",$maxkb)) {
  $fehlertext = ltext("kune5_error2");

  include("kunden_neu2.php");

  exit();
}

## MB/GB in KB bei "max. Speicherplatz vom Anbieter" umwandeln

if (($maxkb <> -1) && ($kbtyp == 'MB'))        {
  $maxkb2 = $maxkb * 1024;
} else	{
  $maxkb2 = $maxkb;
}

## /MB/GB in KB bei "max. Speicherplatz vom Anbieter" umwandeln

if ((($maxkb2 < 1) && ($maxkb2 <> -1))|| ($maxkb2 > 2147283647) || ($maxkb2 < -1)) {
  $fehlertext = ltext("kune5_error3");

  include("kunden_neu2.php");

  exit();
}



if (($maxpop > 8388607) || ($maxpop < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxpop))) {
  $fehlertext = ltext("kune5_error4");

  include("kunden_neu2.php");

  exit();
}



if (($maxemail > 8388607) || ($maxemail < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxemail))) {
  $fehlertext = ltext("kune5_error5");

  include("kunden_neu2.php");

  exit();
}



# wenn email < pop dann fehler

if ((($maxemail < $maxpop) && ($maxpop > 0) && ($maxemail <> -1)) || (($maxpop == -1) && ($maxemail <> -1))) {
  $fehlertext = ltext("kune5_error6");

  include("kunden_neu2.php");

  exit();
}

# /wenn email < pop dann fehler



if (($maxautoresponder > 8388607) || ($maxautoresponder < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxautoresponder))) {
  $fehlertext = ltext("kune5_error7");

  include("kunden_neu2.php");

  exit();
}



# wenn email < autoresponder dann fehler

if ((($maxemail < $maxautoresponder) && ($maxautoresponder > 0) && ($maxemail <> -1)) || 
    (($maxautoresponder == -1) && ($maxemail <> -1))) {
  $fehlertext = ltext("kune5_error8");

  include("kunden_neu2.php");

  exit();
}

# /wenn email < autoresponder dann fehler


if (($maxftp > 8388607) || ($maxftp < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxftp))) {
  $fehlertext = ltext("kune5_error9");

  include("kunden_neu2.php");

  exit();
}

if (($maxtransfer < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxtransfer))) {
  $fehlertext = ltext("kune5_error10");

  include("kunden_neu2.php");

  exit();
}



## MB/GB in KB bei "max. transfer vom Anbieter" umwandeln

if ($maxtransfer <> -1) {
  $maxtransfer2 = $maxtransfer * 1024;
}else { 
  $maxtransfer2 = $maxtransfer; 
}



## /MB/GB in KB bei "max. transfer vom Anbieter" umwandeln


if ($maxtransfer2 > 2147283647) {
  $fehlertext = ltext("kune5_error11");

  include("kunden_neu2.php");

  exit();
}


if (($maxsubdomains > 8388607) || ($maxsubdomains < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxsubdomains))) {
  $fehlertext = ltext("kune5_error12");

  include("kunden_neu2.php");

  exit();

}


if (($maxmysql > 32767) || ($maxmysql < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxmysql))) {
  $fehlertext = ltext("kune5_error13");

  include("kunden_neu2.php");

  exit();
}

if (($maxcronjobs < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxcronjobs))) {
  $fehlertext = ltext("kune5_error27");

  include("kunden_neu2.php");

  exit();
}

if (($maxmaillist < -1) || (!ereg("^(0|-?[1-9][0-9]*)$",$maxmaillist)))
{
	$fehlertext = ltext("kune3_error31");
	include("kunden_neu2.php");
	exit();
}

if (isset($shell)){
  if($shell==2){
    $shell=0;
    $scponly=1;
  }else{
    $scponly=0;
  }
}else{
  $shell=0;
  $scponly=0;
}
if($scponly) $scponly=2;

if (($shell == 1) && ($ftp == 0)) {
  $fehlertext = ltext("kune5_error14");
  db_close();

  include("kunden_neu2.php");

  exit();
}



## name & pw des kunden ermitteln

## mysql verbindung aufbauen

db_connect($db_host, $db_user, $db_pass);

## /mysql verbindung aufbauen

## standarddomain auslesen

$id = db_query("select standarddomain, idn, spamfilter from admin");

$werte = db_fetch_array($id);

$stddomain = $werte["standarddomain"];
$idn_support = $werte["idn"];
$spamfilter_support = $werte["spamfilter"];

## /standarddomain auslesen


## können noch kunden angelegt werden?
$loUser = new SQLString($PHP_AUTH_USER);
$id = safe_query2( "select count(kunde) as anzahlkunden from kunden where anbieter=?", $loUser);

$werte = db_fetch_array($id);

$anzahlkunden = $werte["anzahlkunden"];

$id = safe_query2( "select maxkunden, maxkundenlimit from anbieter where anbieter=?", $loUser);

$werte = db_fetch_array($id);

$maxkunden = $werte["maxkunden"];

$maxkundenlimit = $werte["maxkundenlimit"];

if (($maxkunden <> "-1") && ($anzahlkunden >= $maxkunden) && ($maxkundenlimit == 1)) {
  
  include("kunden_neu1.php");

  exit();
}

## /können noch kunden angelegt werden?



$id = safe_query2( "select * from anbieter where anbieter = ?", $loUser);
$serverkann = db_fetch_array($id);

$skmaxpop 		= $serverkann["maxpop"];
$skmaxemail 		= $serverkann["maxemail"];
$skmaxautoresponder 	= $serverkann["maxautoresponder"];
$skmaxftp 		= $serverkann["maxftp"];
$skmaxsubdomains 	= $serverkann["maxsubdomains"];
$skmaxmysql 		= $serverkann["maxmysql"];
$skmaxcronjobs 		= $serverkann["maxcronjobs"];
$httpdspezial 		= $serverkann["httpdspezial"];

$skphp = $serverkann["php"];
if ($skphp == 0) { $php = 0; }

$skftp 	= $serverkann["ftp"];
if ($skftp == 0) { $ftp = 0; $shell = 0; }

$skperl	= $serverkann["perl"];
if ($skperl == 0) { $perl = 0; $stdcgi = 0; }

$skmodpython = $serverkann["modpython"];
if ($skmodpython == 0) { $modpython = 0; }

$skssi = $serverkann["ssi"];
if ($skssi == 0) { $ssi = 0; }

$skssl = $serverkann["cssl"];
if ($skssl == 0) { $ssl = 0; }

$skfrontpage = $serverkann["frontpage"];
if ($skfrontpage == 0) { $frontpage = 0; }

$skshell =  $serverkann["shell"];
if ($skshell == 0)  $shell = 0; 

$skscponly =  $serverkann["scponly"];
if ($skscponly == 0)  $scponly = 0; 

$skstatistik 		= $serverkann["statistik"];
if ($skstatistik == 0) { $statistik = 0; }

$skpwschutz 		= $serverkann["pwschutz"];
if ($skpwschutz == 0) { $pwschutz = 0; }

$skfehlerseiten	= $serverkann["fehlerseiten"];
if ($skfehlerseiten == 0) { $fehlerseiten = 0; }

$skwebftp = $serverkann["webftp"];
if ($skwebftp == 0) { $webftp = 0; }

$skwebmail = $serverkann["webmail"];
if ($skwebmail == 0) { $webmail = 0; }

$skstdcgi = $serverkann["stdcgi"];
if ($skstdcgi == 0) { $stdcgi = 0; }

$skphpupload = $serverkann["phpupload"];
if ($skphpupload == 0) { $phpupload = 0; }

$skwap = $serverkann["wap"];
if ($skwap == 0) { $wap = 0; }

$skcoldfusion = $serverkann["coldfusion"];
if ($skcoldfusion == 0) { $coldfusion = 0; }

$skdirlist 		= $serverkann["dirlist"];
if ($skdirlist == 0) { $dirlist = 0; }

$skspamfilter 		= $serverkann["spamfilter"];
if($skspamfilter == 0 || $skmaxpop ==0 || $maxpop == 0) {
  $spamfilter = 0;
}

if($serverkann['maxmaillist'] == 0){$maxmaillist = 0;}

$skmaxwildcards 	= $serverkann["maxwildcards"];
$skmaxwildcardslimit	= $serverkann["maxwildcardslimit"];
$skmaxatdomains		= $serverkann["maxatdomains"];
$skmaxatdomainslimit	= $serverkann["maxatdomainslimit"];
$skmaxkb 		= $serverkann["maxkb"];
$skmaxkblimit 		= $serverkann["maxkblimit"];
$skmaxpoplimit 		= $serverkann["maxpoplimit"];
$skmaxemaillimit 	= $serverkann["maxemaillimit"];
$skmaxautoresponderlimit = $serverkann["maxautoresponderlimit"];
$skmaxftplimit 		= $serverkann["maxftplimit"];
$skmaxsubdomainslimit 	= $serverkann["maxsubdomainslimit"];
$skmaxmysqllimit 	= $serverkann["maxmysqllimit"];
$skmaxcronjobslimit 	= $serverkann["maxcronjobslimit"];



## wildcard überschritten?

if ( $skmaxwildcardslimit && $skmaxwildcards != -1 && $wildcard!=0 )
{
	$id  = safe_query2( "select sum(wildcard) as wildcardvergeben from kunden where anbieter = ?", $loUser);
    $werte = db_fetch_array($id);
    $wildcardvergeben = $werte["wildcardvergeben"];
    if ( $wildcard == -1 || $wildcardvergeben+$wildcard > $skmaxwildcards )
    {
	$fehlertext = ltext("kune5_error15");
	db_close();
	include("kunden_neu2.php");
	exit();
    }
}
## //





/* -------------------- at domains -------------------- */
if ( $skmaxatdomainslimit && $skmaxatdomains != -1 && $atdomains != 0 )
{
	$id = safe_query2( "SELECT SUM(maxatdomains) AS other FROM kunden WHERE anbieter = ?", $loUser);
	$werte = db_fetch_array($id);
	if ( $atdomains==-1 || $werte["other"]+$atdomains > $skmaxatdomains )
	{
		$fehlertext = ltext ( "kune5_error24", array($skmaxatdomains-$werte["other"]) );
		db_close();
		include("kunden_neu2.php");
		exit();
	}
}
/* -------------------- end at domains -------------------- */





$id = safe_query2( "select frontpage, cssl, maxmysql from anbieter where anbieter=?", $loUser);
$werte = db_fetch_array($id);

$skfrontpage 	= $werte["frontpage"];
$skssl 		= $werte["cssl"];
$cmaxmysql 	= $werte["maxmysql"];

if ($cmaxmysql != 0) $skmysql = 1;


$kundenpw 	= mkpasswd(8);
$cryptpw 	= crypt_pw($kundenpw);

#/name & pw des kunden ermitteln



## Domain prüfen

## prüfen ob domain angegeben

//
// wws: ersetzen $domains mit $domain
//

//if (istleer($domains)){

if (is_array($domain)){
}  else {
  $lastDomain="kein";
}

if ($lastDomain){
  $fehlertext = ltext("kune5_error16");

  db_close();

  include("kunden_neu4.php");

  exit();
}

## // prüfen ob domain angegeben



//$neuedomains2 = explode("\n", strtolower($domains));

include_once('module.Informer.php');
$ga_WrongIdnDomains = array();
$domIndex = array();
while (list($key,$val)=each($domain)){
  	$val=strtolower(trim($val));

	if ($val) {
		$ga_OriginalName[$key] = $val;	// original name (may have national characters included)
		// check if international and then translate
		$ls_DomainOrig = $val;
		include_once('class.InputDecode.php');
		$ls_DomainTranslated = InputDecode::decode($ls_DomainOrig);

		if ($ls_DomainTranslated != $ls_DomainOrig && $idn_support) {
			// has national characters
			include_once('idn_functions.php');
			$ls_Domain = translate_name_to_idn($ls_DomainTranslated); 
			$ga_NationalName[$key] = $ls_DomainTranslated;	// save the national name
			
		} elseif ($ls_DomainTranslated != $ls_DomainOrig && !$idn_support) {
			// has national characters, but there are no IDN support
			$ga_WrongIdnDomains[] = $ls_DomainOrig;
		} else {
			$ls_Domain = $ls_DomainOrig;
		}
		
		// store ascii-translated name here
		$domIndex[$ls_Domain]=$key;
	}
}

$neuedomains2=array_keys($domIndex);

//
// wws:
//

## prüfen, ob anbieter noch so viele domains hinzufügen darf

$anzahlneuedomains = count($neuedomains2);


$idx = safe_query2( "select maxdomains, maxdomainslimit from anbieter where anbieter=?", $loUser);

$wertex = db_fetch_array($idx);

$maxdomains 		= $wertex["maxdomains"];
$maxdomainslimit 	= $wertex["maxdomainslimit"];


$idx = safe_query2( "select count(domain) as anzahldomains from domains where anbieter=? and richtigedomain=1", $loUser);

$wertex = db_fetch_array($idx);

$anzahldomains = $wertex["anzahldomains"];
$gesamtdomains = $anzahldomains + $anzahlneuedomains;


if (($gesamtdomains > $maxdomains) && ($maxdomainslimit == "1") && ($maxdomains <> "-1")) {
  $fehlertext = ltext("kune5_error17",array($maxdomains,$anzahldomains));
  db_close();

  include("kunden_neu4.php");

  exit();
}

## // prüfen, ob anbieter noch so viele domains hinzufügen darf



## doppelte domains aus liste löschen

/*
//for ($i = 0; $i < count($neuedomains2); $i++){
//  $schondrin = 0;
//  $checkdomain = trim($neuedomains2[$i]);
// for ($x = 0; $x < count($neuedomains); $x++){
//    if (trim($neuedomains[$x]) == $checkdomain){
//      $schondrin = 1;
//    }
//  }
//  if (($schondrin == 0) && (!istleer($checkdomain))){
//    $neuedomains[] = $checkdomain;
//  }
}
*/

$domains2 = $neuedomains2;

## // doppelte domains aus liste löschen



## prüfen ob domain schon angelegt

for ($i = 0;$i < count($domains2);$i++){
  $domainname = trim($domains2[$i]);
  if (!istleer($domainname)){
    $id = safe_query2( "select domain from domains where domain = ?", new SQLString($domainname) );
    if (db_num_rows($id) > 0){
      $domainschonda .= $domainname.", ";
    }
  }
}

$domainschonda = substr($domainschonda, 0, - 2);

if (!istleer($domainschonda)){
  $fehlertext = ltext("kune5_error18", array($domainschonda));

  db_close();

  include("kunden_neu4.php");

  exit();
}

## /prüfen, ob domains schon da



## prüfen, ob domain syntax ok

for ($i = 0;$i < count($domains2);$i++)	{
  $domainname = trim($domains2[$i]);

  if ((!ereg("\.", $domainname)) || (ereg("([^a-z0-9.-])|([.]{2})", $domainname)) || 
      (ereg("^-", $domainname)) || (ereg("^www\.", $domainname))) {
    $fehlerhaftedomains .= $domainname.", ";
  }
}

//print_r($domains2);

$fehlerhaftedomains = substr($fehlerhaftedomains, 0, (strlen($fehlerhaftedomains) - 2));


if (!istleer($fehlerhaftedomains) || sizeof($ga_WrongIdnDomains) > 0) {
	if (!istleer($fehlerhaftedomains)) {
		$fehlertext .= ltext("kune5_error19", array($fehlerhaftedomains));
	}
	if (sizeof($ga_WrongIdnDomains) > 0) {
		$fehlertext .= ltext('error_bad_domains__no_idn_support', array(implode(', ', $ga_WrongIdnDomains)));
	}

	db_close();

	include("kunden_neu4.php");

	exit();
}

## /prüfen, ob domain syntax ok
//$kundenstr.$stddomain
$dname_regexp = "/${user_prefix}[0-9]+\." . str_replace('.', '\.', $stddomain) . "/i";
$badomains = array();
foreach($domains2 as $dname){
  $dname = trim($dname);
  if(preg_match($dname_regexp, $dname))	{
    $badomains[] = $dname;
  }
}

if(count($badomains)){
  $fehlertext = ltext("kune5_error23", array(implode(", ", $badomains), $user_prefix, $stddomain));
  db_close();
  include("kunden_neu4.php");
  exit();	
}

## / Domain prüfen

$uid = getuid();

## preassigned kundenstr  value

if (($freifeldname3 == "add-user") && ((int)$freifeldwert3 > 0)) {
  #$kundenstr = $user_prefix.$freifeldwert3;
  $number = $freifeldwert3;
  $kundenstr = $user_prefix.$number; 
  $freifeldname3 = "";
  $freifeldwert3 = "";

  if (if_user_exists($kundenstr)) {
  	// check that there no existent (or not comletely removed) user with the same name
  	$kundenstr = "";
  }
}

// if there are no preassigned id, then assign new
if ($kundenstr == "") {	 
	#$kundenstr = $user_prefix.find_new_user_id();
	$number = find_new_user_id();
	$kundenstr = $user_prefix.$number;  
}
		
if (strlen($kundenstr) < 2){
  $fehlertext = ltext("kune5_error20");

  db_close();

  include("kunden_neu4.php");

  exit();
}


## ip für kunde ermitteln und anlegen


$id = safe_query2( "select standardip from anbieter where anbieter = ?", $loUser);

$werte = db_fetch_array($id);

$stdip = $werte["standardip"];

if ($ip != "standard"){
	$loIP = new SQLString($ip);
  $id = safe_query2( "select ip from ipadressen where anbieter=? and kunde='' and ip=?", $loUser, $loIP );

  if (db_num_rows($id) < 1){
    $fehlertext = ltext("kune5_error21");

    db_close();
    
    include("kunden_neu4.php");

    exit();
  } else  {
    if ($exklusiv == 1)	{
      $id = safe_query2( "select ip from kunden where ip = ?", $loIP );
      if (db_num_rows($id) > 0) {
	$fehlertext = ltext("kune5_error22");

	db_close();

	include("kunden_neu4.php");

	exit();
      }
      safe_query2( "update ipadressen set kunde = '$kundenstr', lastchange=$timestamp ".
									 "where ip = ? and anbieter=? and kunde=''", $loIP, $loUser);
    }
  }
}else {
  $ip = $stdip;
}

## /ip für kunde ermitteln und anlegen



## domain für kunde anlegen
if($DEBUG){
  echo "<br>domains2:<br>\n";
  print_r($domains2);
  echo "<br>domIndex:<br>\n";
  print_r($domIndex);
  echo "<br>ga_NationalName:<br>\n";
  print_r($ga_NationalName);
  echo "<br>ga_OriginalName:<br>\n";
  print_r($ga_OriginalName);
  echo "<br>domdns:<br>\n";
  print_r($domdns);
  echo "<br>domdnr:<br>\n";
  print_r($domdnr);
	echo "<br>\n";
}

if( file_exists("../dnr.php") ) {
	include_once("../dnr.php");
	$dnrGo = 1;
	
} else {
	$dnrGo = 0;
}

$loHttpdSPec = new SQLString($httpdspezial);

for ($i = 0;$i < count($domains2);$i++) {
  $domainname = trim($domains2[$i]);
  $ind = $domIndex[$domainname];
  $nat_name = $ga_NationalName[$ind];

  if (!istleer($domainname)) {

    $dns = $domdns[$ind]?$domdns[$ind]:0;
    $dnr = $domdnr[$ind]?$domdnr[$ind]:0;

		//		echo "domain: $domainname, dns: $dns, dnr: $dnr<br>\n";

    $sql ="insert into domains (domain, kunde, anbieter, richtigedomain, pfad, lastchange, zusatz, dns, dnr)".
			" VALUES ( ?, '$kundenstr', ?, 1, '/', $timestamp, ?, $dns, $dnr)";

		$res1 = safe_query2( $sql, new SQLString($domainname), $loUser, $loHttpdSPec);
		if($db_type=='mysql'){
			$id1  = db_insert_id($res1);
		}else{
			$id1  = db_insert_id('domains','id');
		}
		db_close($res1); 

    domain_aendern($domainname, "anlegen", $kundenstr);
    fpalias_aendern($domainname, "anlegen", $kundenstr);

    $sql = "insert into domains (domain, kunde, anbieter, richtigedomain, pfad, lastchange, zusatz, dns, dnr)".
	     " VALUES ( ?, '$kundenstr', ?, '2', '/', $timestamp, ?,0,0)";
		$res2 = safe_query2( $sql, new SQLString('www.'.$domainname), $loUser, $loHttpdSPec);
		if($db_type=='mysql'){
			$id2  = db_insert_id($res2);
		}else{
			$id2  = db_insert_id('domains','id');
		}
		db_close($res2); 

    domain_aendern("www.".$domainname, "anlegen", $kundenstr);
    fpalias_aendern("www.".$domainname, "anlegen", $kundenstr);

    if ($DEBUG) {
			echo "<pre>";
			echo "domainname = $domainname\n";
			echo "nat_name = $nat_name\n";
//	echo 
			echo "</pre>";
    }
		if ($nat_name) {
			include_once('idn_functions.php');
			save_idn_alias($id1, $nat_name, false);
			save_idn_alias($id2, "www.$nat_name", false);
		}

## dns eintrag erstellen

    if($dns == 1 || $dnr == 1 || $dnr == 2){
      $id = safe_query2( "select dnsspezial,pns,sns from anbieter where anbieter = ?", $loUser);
      $werte  = db_fetch_array($id);
      $dnstemplate  = $werte["dnsspezial"];
      $pns          = $werte["pns"];
      $sns          = $werte["sns"];
      $seriennummer = date("Ymd")."01";
      if($dnsspezial == ""){
				$id    = db_query("select dnstemplate from admin");
				$werte = db_fetch_array($id);
				$dnstemplate = $werte["dnstemplate"];
      }

                   ## domain
      $zonefile = str_replace ("##domain##", $domainname, $dnstemplate);
                   ## /domain

                   ## ns
      $zonefile = str_replace ("##ns1##", $pns, $zonefile);
      $zonefile = str_replace ("##ns2##", $sns, $zonefile);
                   ## /ns

                    ## ip
      $zonefile = str_replace ("##ip##", $ip, $zonefile);
                    ## /ip

                    ## glue record
      $pnsdom = substr ($pns,(strpos ($pns, ".")+1));
      $snsdom = substr ($sns,(strpos ($sns, ".")+1));
      if($snsdom == $domain){
				$id       = db_query("select ip_sns from admin");
				$werte    = db_fetch_array($id);
				$ip_sns   =  $werte["ip_sns"];
				$zonefile = $sns.".      86400  IN   A   ".$ip_sns."\n".$zonefile;
      }
      if($pnsdom == $domain){
				$id       = db_query("select ip_pns from admin");
				$werte    = db_fetch_array($id);
				$ip_pns   =  $werte["ip_pns"];
				$zonefile = $pns.".      86400  IN   A   ".$ip_pns."\n".$zonefile;
      }
                    ## /glue record

      safe_query2( "insert into dns (domain,zonefile,lastchange,neu,anbieter,kunde)".
									 " VALUES ( ?, ?,'$seriennummer',1, ?,'$kundenstr')",
									 new SQLString($domainname), new SQLString($zonefile), $loUser);
      db_query("update allgemein set dns=1");
    }
                 ## /dns eintrag erstellen

                 ## dnr ausführen
    if ( $dnrGo && $dnr == 1 ){
                    ## kundenvariablen:
      $dnr_c_firstname    = $firstname;
      $dnr_c_gender       = $gender;
      $dnr_c_plz          = $plz;
      $dnr_c_land         = $land;
      $dnr_c_name         = $name;
      $dnr_c_firma        = $firma;
      $dnr_c_anschrift    = $anschrift;
      $dnr_c_ort          = $ort;
      $dnr_c_telefon      = $telefon;
      $dnr_c_telefax      = $fax;
      $dnr_c_emailadresse = $emailadresse;
## domaindaten:
      $dnr_d_domain       = $domainname;
      $dnr_d_pns          = $pns;
      $dnr_d_sns          = $sns;
                    ## adminvariablen:
      
      $id = safe_query2("select gender,plz,firstname,land,name,firma,anschrift,plzort,telefon,fax,emailadresse".
												" from anbieter where anbieter = ?", $loUser);
      $werte              = db_fetch_array($id);
      $dnr_a_firstname    = $werte["firstname"];
      $dnr_a_gender       = $werte["gender"];
      $dnr_a_plz          = $werte["plz"];
      $dnr_a_land         = $werte["land"];
      $dnr_a_name         = $werte["name"];
      $dnr_a_firma        = $werte["firma"];
      $dnr_a_anschrift    = $werte["anschrift"];
      $dnr_a_ort          = $werte["plzort"];
      $dnr_a_telefon      = $werte["telefon"];
      $dnr_a_telefax      = $werte["fax"];
      $dnr_a_emailadresse = $werte["emailadresse"];
## dnr ausführen

			sendDnr();
      
    }
                 ## /dns ausführen
  }
}


##standarddomain einfügen

db_query("delete from domains where domain = '$kundenstr.$stddomain'");
safe_query2( "insert into domains (domain, kunde, anbieter, richtigedomain, pfad, lastchange, zusatz)".
						 " VALUES ('$kundenstr.$stddomain', '$kundenstr', ?, 3, '/', $timestamp, ?)", $loUser, $loHttpdSpec);

domain_aendern($kundenstr.".".$stddomain, "anlegen", $kundenstr);
fpalias_aendern($kundenstr.".".$stddomain, "anlegen", $kundenstr);

set_countdomains($kundenstr);

## /domain für kunde anlegen

if ($statistik == 1) {
  safe_query2( "insert into webalizer (kunde, loeschen, neu, anbieter) ".
							 "VALUES ('$kundenstr', 0, 1, ?)", $loUser);
  db_query("update allgemein set webalizer=1");
}

if ($perl == 1){
 safe_query2( "insert into perl (kunde, loeschen, neu, anbieter) ".
							"VALUES ('$kundenstr', 0, 1, ?)", $loUser);
  db_query("update allgemein set perl=1");
}

if ($stdcgi == 1){

  db_query("update allgemein set stdcgi=1");
}

$loFields = new SQLFieldSet ( array(  'kunde' => array( $kundenstr, 'C' ),
																			'number' => $number,
																			'uid' => $uid,
																			'passwort' => array( '', 'C' ),
																			'longpw' => array( $cryptpw, 'C' ),
																			'anbieter' => array( $PHP_AUTH_USER, 'C' ),
																			'maxpop' => $maxpop,
																			'maxkb' => $maxkb2,
																			'maxmysql' => $maxmysql,
																			'maxemail' => $maxemail,
																			'maxautoresponder' => $maxautoresponder,
																			'maxftp' => $maxftp,
																			'maxtransfer' => $maxtransfer2,
																			'maxsubdomains' => $maxsubdomains,
																			'php' => $php,
																			'ftp' => $ftp,
																			'wildcard' => $wildcard,
																			'maxatdomains' => $atdomains,
																			'frontpage' => $frontpage,
																			'perl' => $perl,
																			'modpython' => $modpython,
																			'ssi' => $ssi,
																			'shell' => $shell,
																			'scponly' => $scponly,
																			'statistik' => $statistik,
																			'statistikpasswort' => array( '', 'C' ),
																			'statistiklongpw' => array( $cryptpw, 'C' ),
																			'pwschutz' => $pwschutz,
																			'fehlerseiten' => $fehlerseiten,
																			'webftp' => $webftp,
																			'webmail' => $webmail,
																			'stdcgi' => $stdcgi,
																			'anmeldedatum' => $timestamp,
																			'name' => array( $name, 'C' ),
																			'firma' => array( $name, 'C' ),
																			'anschrift' => array( $anschrift, 'C' ),
																			'plzort' => array( $ort, 'C' ),
																			'telefon' => array( $telefon, 'C' ),
																			'fax' => array( $fax, 'C' ),
																			'gender' => array( $gender, 'C' ),
																			'firstname' => array( $firstname, 'C' ),
																			'plz' => array( $plz, 'C' ),
																			'land' => array( $land, 'C' ),
																			'emailadresse' => array( $emailadresse, 'C' ),
																			'kundennummer' => $kundennummer,
																			'freifeldname1' => array( $freifeldname1, 'C' ),
																			'freifeldwert1' => array( $freifeldwert1, 'C' ),
																			'freifeldname2' => array( $freifeldname2, 'C' ),
																			'freifeldwert2' => array( $freifeldwert2, 'C' ),
																			'freifeldname3' => array( $freifeldname3, 'C' ),
																			'freifeldwert3' => array( $freifeldwert3, 'C' ),
																			'kundendatenanzeigen' => $kundendatenanzeigen,
																			'ip' => array( $ip, 'C' ),
																			'quota' => 1,
																			'language' => array( $lang_short, 'C'),
																			'lastchange' => $timestamp,
																			'phpupload' => $phpupload,
																			'wap' =>$wap ,
																			'coldfusion' => $coldfusion,
																			'dirlist' => $dirlist,
																			'httpdspezial' => array( $httpdspezial, 'C'),
																			'maxcronjobs' => $maxcronjobs,
																			'spamfilter' => $spamfilter,
																			'maxidn' => -1,
																			'maxmaillist' => $maxmaillist 
																			)
															);

list( $lcFlds, $lcVals ) = $loFields->getSQLInsert();
$sql = "INSERT INTO kunden ($lcFlds) VALUES ($lcVals)";

if($DEBUG){
  echo "sql: $sql<br>\n";
}

db_query($sql);

if ( $spamfilter_support )
	db_query( "UPDATE allgemein SET spamupdate = 1" );

freeuid();

db_query( "UPDATE transfer SET geloescht = 1 WHERE kunde = '$kundenstr'");
$id = db_query( "SELECT neukundenreload FROM admin");

$werte = db_fetch_array($id);

$neukundenreload = $werte["neukundenreload"];

if ($neukundenreload == 1){
  db_query( "UPDATE allgemein SET adduser=1, httpd=1, httpdupdate=0, quota=1");
}else {
  db_query( "UPDATE allgemein SET adduser=1, httpd=1, quota=1");
}



## daten für email holen

if ($ident == "standard"){
  $id = safe_query( "SELECT * FROM anbieter WHERE anbieter = ?", $loUser);
}else {
  $id = safe_query( "SELECT * FROM angebote WHERE anbieter = ? AND ident=$ident", $loUser);
}
$werte = db_fetch_array($id);

$emailbetreff 	= $werte["emailbetreff"];
$emailtext 	= $werte["emailtext"];
$emailname 	= $werte["emailname"];
$emailadresse2 	= $werte["emailemailadresse"];
$emailbcc 	= $werte["emailbcc"];

$loUser = new SQLString($PHP_AUTH_USER);

if ((istleer($emailbetreff)) && (istleer($emailtext)) && (istleer($emailname)) && 
    (istleer($emailadresse2)) && (istleer($emailbcc))) {
  
  $id = safe_query( "select * from anbieter where anbieter = ?", $loUser );

  $werte = db_fetch_array($id);

  $emailbetreff = $werte["emailbetreff"];
  $emailtext 	= $werte["emailtext"];
  $emailname 	= $werte["emailname"];
  $emailadresse2 = $werte["emailemailadresse"];
  $emailbcc 	= $werte["emailbcc"];
}


if($DEBUG){
  echo "<br>domains2:<br>\n";
  print_r($domains2);
  echo "<br>\n";
}

$firstdomain = $domains2[0];

for ($i = 0;$i < count($domains2);$i++)	{
  $domainlist .= trim($domains2[$i])."\n";
}



## old stuff from german version

$emailbetreff = str_replace("##kunde##", $kundenstr, $emailbetreff);
$emailbetreff = str_replace("##passwort##", $kundenpw, $emailbetreff);
$emailbetreff = str_replace("##firma##", $firma, $emailbetreff);


$emailtext = str_replace("##kunde##", $kundenstr, $emailtext);
$emailtext = str_replace("##passwort##", $kundenpw, $emailtext);
$emailtext = str_replace("##firma##", $firma, $emailtext);

## // old stuff


$emailbetreff = str_replace("##user##", $kundenstr, $emailbetreff);
$emailbetreff = str_replace("##password##", $kundenpw, $emailbetreff);
$emailbetreff = str_replace("##domain##", $firstdomain, $emailbetreff);
$emailbetreff = str_replace("##name##", $name, $emailbetreff);
$emailbetreff = str_replace("##company##", $firma, $emailbetreff);
$emailbetreff = str_replace("##email##", $emailadresse, $emailbetreff);


$emailtext = str_replace("##user##", $kundenstr, $emailtext);
$emailtext = str_replace("##password##", $kundenpw, $emailtext);
$emailtext = str_replace("##domain##", $firstdomain, $emailtext);
$emailtext = str_replace("##domains##", $domainlist, $emailtext);
$emailtext = str_replace("##name##", $name, $emailtext);
$emailtext = str_replace("##company##", $firma, $emailtext);
$emailtext = str_replace("##email##", $emailadresse, $emailtext);

## //

function if_user_exists($account) 
{
	// check that there no existent (or not comletely removed) user with the same name
	// NOTE: for mysql would be better to add "LIMIT 1" (could seriously increase perfomance)
	$loAccount = new SQlString( $account );
	$id1 = safe_query2( "SELECT kunde FROM kunden WHERE kunde = ?", $loAccount);	
	$id2 = safe_query2( "SELECT account FROM delaccs WHERE account = ?", $loAccount);

	return (db_num_rows($id1) != 0 || db_num_rows($id2) != 0);
}

function find_new_user_id() 
{
	global $user_prefix;
	
	// select which accounts are going to be deleted, but are not yet
	$id = db_query("SELECT account FROM delaccs WHERE type='kunde'");
	$used_accounts = array();
	while ( $werte = db_fetch_array($id) ) {
		$index = substr ( $werte["account"], strlen($user_prefix) );
		$used_accounts[] = $index;
	}

	// how to make search faster: 
	// we can use dihotomy to find a zone (~10 elements) where one or more values are sk

	$db_res1 = db_query( "SELECT kunde FROM kunden");	 
	while ($row = db_fetch_array($db_res1)) {	 
		$index = substr ( $row["kunde"], strlen($user_prefix) );
		$used_accounts[] = $index; 
	}	 

	sort($used_accounts);
	$num_accounts = count($used_accounts);
	$max_used_id = end($used_accounts);

	// I used '<=', because array may include '0' value	
	if ($num_accounts <= $max_used_id) {  
		// look for a missing IDs [ O(n) perfomance ]
		$free_id_found = false;
		for ($i = 0; $i < $num_accounts; ++$i) {
			if ($used_accounts[$i] > $i) {	
				// no value for this index
				$new_id = $i;
				return $new_id;
			} 
		}
		// this should never happen, but who knows ...
		// todo: write to stderr
		return $max_used_id + 1;
	} 
	// no skipped IDs in array, use next value
	return $max_used_id + 1;
}
 
?>

<HTML>

<HEAD>

<META content="text/html; charset=<? echo $lang_html_charset; ?>" http-equiv=Content-Type>

<LINK href="<?=$strSkinName?>/style.css" rel=stylesheet type=text/css>

</HEAD>



<BODY bgColor=#ffffff bottomMargin=0 leftMargin=0 topMargin=0 MARGINHEIGHT="0" MARGINWIDTH="0">

<br><? include("../fehler.inc.php"); ?>

<script>

<?
echo js_f_call_help('', 'kune5_title1');
?>

function doChange()

{

document.forms[0].submit();

}

</script>

<table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">

<tr>

    <TD valign="top" colspan=2>

      <p>

        <? echo ltext("kune5_user"); ?>

        <b>

        <? echo $kundenstr; ?>

        </b> <br>

        <? echo ltext("kune5_pw"); ?>

        <b>

        <? echo $kundenpw; ?>

        </b><br>

        </p>

    </TD>

  </TR>

      <form method="post" action="kunden_sendmail.php">

          <tr class="titel-3" >

            <td colspan="2" ><i><b><? echo ltext("kune5_cell_sendmail"); ?></td>

          </tr>

          <tr >

            <td  >

              <? echo ltext("kune5_cell_sendto"); ?>

              </td>

            <td  >

              <input type="text" name="empfaenger" value="<? echo $emailadresse; ?>">

            </td>

          </tr>

          <tr >

            <td >

              <? echo ltext("kune5_cell_subject"); ?>

              </td>

            <td  >

              <input type="text" name="emailbetreff" size="<?=$Rows?>" value="<? echo HTMLSpecialChars($emailbetreff); ?>">

            </td>

          </tr>

          <tr >

            <td >

              <? echo ltext("kune5_cell_text"); ?>

              </td>

            <td  >

              <textarea name="emailtext" cols="<?=$Rows?>" rows="8"><? echo HTMLSpecialChars($emailtext); ?></textarea>

            </td>

          </tr>

          <tr >

            <td >&nbsp;</td>

            <td  >

              <input type="text" name="absenderemail" value="<? echo $emailadresse2; ?>">



              <? echo ltext("kune5_cell_senderemail"); ?>

              </td>

          </tr>

          <tr >

            <td  height="26">&nbsp;</td>

            <td   height="26">

              <input type="text" name="absendername" value="<? echo HTMLSpecialChars($emailname); ?>">



              <? echo ltext("kune5_cell_sendername"); ?>

              </td>

          </tr>

          <tr >

            <td >&nbsp;</td>

            <td  >

              <input type="text" name="emailbcc" value="<? echo $emailbcc; ?>">



              <? echo ltext("kune5_cell_bcc"); ?>

              </td>

          </tr>

          <tr >

            <td >&nbsp;</td>

            <td  >

            

            <table cellpadding="0" cellspacing="0" border="0">

            <tr>

            <td><input type=image border=0 src="<?=$strSkinName?>/pics/buttons/bt_img_left.gif"></td>

            <td background="<?=$strSkinName?>/pics/buttons/bt_img_middle.jpg"><a href="javascript:doChange()"  class="button-1"><? echo ltext("kune5_button1"); ?></a></td>

            <td><input type=image border=0 src="<?=$strSkinName?>/pics/buttons/bt_changepass.gif"></td>

            </tr>

            </table></a>

            </td>

          </tr>

</table>

</form>

</BODY>

</HTML>

<?

db_close();

?>
