<?
########## 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" );

global $DEBUG;
#$DEBUG=1;

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

$id = safe_query( "SELECT popmaxkb FROM admin", TRUE );
$werte = db_fetch_array($id);
$popmaxkb = $werte["popmaxkb"];

$sqlUser = new SQLString( $PHP_AUTH_USER );

$id = safe_query( "SELECT maxpop, anbieter FROM kunden WHERE kunde = ? ",TRUE, $sqlUser);
$werte = db_fetch_array($id);
$maxpop = $werte["maxpop"];
$anbieter = $werte["anbieter"];

$id = safe_query( "SELECT COUNT(account) AS pop FROM pop3 WHERE kunde = ?", TRUE, $sqlUser);
$werte = db_fetch_array($id);
$pop = $werte["pop"];

if (($pop >= $maxpop) && ($maxpop <> -1))	{ 
	$fehlertext = ltext("empoan_error1");
	db_close();
	include("email_pop3.php");
	exit();
}

$id = safe_query ( "SELECT account FROM delaccs WHERE kunde = ? AND type = 'pop3'", TRUE, $sqlUser );
$usedNumbers = array();
while ( $werte = db_fetch_array($id) ){
     $usedNumbers[] = ereg_replace($PHP_AUTH_USER.'p',"",$werte[0]);
}
if($DEBUG){
	echo "deleted: <br>\n";
	print_r($deleted);	
	echo "<br>\n";
}
$id = safe_query( "SELECT number FROM pop3 WHERE kunde = ?", TRUE, $sqlUser);
while ( $werte = db_fetch_array($id) ) {
	$usedNumbers[] = $werte[0];
}

// calculate the first free number
$number = 1;
while ( in_array($number, $usedNumbers) ) {
	$number++;
}
$newpop = $PHP_AUTH_USER."p".$number;

$newpw = mkpasswd(8);
$newpw2 = crypt_pw($newpw);
$uid = getuid();

$timestamp = time();

safe_query ( "INSERT INTO pop3 ( account, number, uid, passwort, longpw, kunde, anbieter, quota, maxkb, language, kommentar, lastchange) ".
                      " VALUES ( ?,       ?,      ?,   '',       ?,      ?,     ?,        1,     ?,     ?,        ?,         ?)", TRUE, 
 new SQLString( $newpop ), new SQLInteger( $number ), new SQLInteger( $uid ),  
 new SQLString( $newpw2 ), $sqlUser, new SQLString( $anbieter ), new SQLInteger( $popmaxkb ), 
 new SQLString( $lang_short ), new SQLString( $comment ), new SQLInteger( $timestamp ) 
);

safe_query("UPDATE allgemein SET addpop=1, quota=1", TRUE);
freeuid();

$fehlertitel = ltext("empoan_error3");
$fehlertext = ltext("empoan_error4", array($newpop, $newpw));
db_close();
include("email_pop3.php");
exit();

?>
