<?
########## 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/class.SQLString.php" );

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

$prefix = strtolower($prefix);
$domain = strtolower($domain);
$pop3 = strtolower($pop3);

$sqlUser = new SQLString( $PHP_AUTH_USER );

## prüfen ob genug emails vorhanden und anbieter ermitteln
$id = safe_query( "SELECT maxemail, anbieter FROM kunden WHERE kunde = ? ", TRUE, $sqlUser);

$werte = db_fetch_array($id);
$maxemail = $werte["maxemail"];
$anbieter = $werte["anbieter"];

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

if ( ($email >= $maxemail ) && ( $maxemail != "-1" ) )	{ 
	 $fehlertext = ltext("ememan2_error1");
	 db_close();
	 include("email_email.php");
	 exit();
}

if ( (strlen($pop3) < 5) || (ereg("([^a-z0-9+/=_.@-])", $pop3)) || (!ereg("@", $pop3) ) )	{ 
	$fehlertext = ltext("ememan2_error2");
	db_close();
	include("email_email.php");
	exit();
}

## //

## prüfen ob domain dem user gehört
$id = safe_query( "SELECT domain FROM domains WHERE domain = ? AND kunde = ? AND ".
									"(richtigedomain='0' OR richtigedomain='1')", 
									TRUE, new SQLString( $domain ), $sqlUser );
$check = db_num_rows($id);
if ($check < 1)	{ 
	$fehlertext = ltext("ememan2_error3", array($domain));
	db_close();
	include("email_email.php");
	exit();
}

## //

## prefix auf syntax prüfen
if ($prefix != "*") {
	if ((!ereg("^[a-z0-9]",$prefix)) || (!ereg("[a-z0-9]$", $prefix)) || (ereg("([^a-z0-9_.-])|([.]{2})", $prefix))) {
		$fehlertext = ltext("ememan2_error4");
		db_close();
		include("email_email.php");
		exit();
	} 
}
## //

$sqlDomain = new SQLString( $domain );

## prüfen ob prefix schon eingerichtet
$id = safe_query( "select ident from email where prefix = ?  and domain = ? ", 
									TRUE, new SQLString( $prefix ), $sqlDomain );
$check = db_num_rows($id);
if ($check > 0)	{ 
	$fehlertext = ltext("ememan2_error5", array("$prefix@$domain"));
	db_close();
	include("email_email.php");
	exit();
}
## //

## looking for existing maillist 
$id = safe_query( "SELECT maillist.name AS ml_name FROM maillist ".
									"INNER JOIN domains ON (domains.id = maillist.domain_id) ".
									"WHERE domains.domain = ? AND domains.kunde = ?", 
									TRUE, $sqlDomain, $sqlUser );
while ($row = db_fetch_array($id)) {

	$ml_name = $row["ml_name"];

	if(($prefix == $ml_name) || ($prefix == 'owner-'.$ml_name) || ($prefix == 'majordomo-owner') || 
		 ($prefix == 'majordomo') ||	 ($prefix == $ml_name.'-owner') || 
		 ($prefix == $ml_name.'-approval') || ($prefix == $ml_name.'-outgoing') || 
		 ($prefix == $ml_name.'-archive') || ($prefix == $ml_name.'-request') ) {

		$fehlertext = ltext('ememan_error8', array($ml_name.'@'.$domain));
		db_close();
		include("email_email.php");
		exit();
	}
}

## prüfen ob zielemailadresse ein alias account ist

$emailteile = explode("@", $pop3);
$teil1 = $emailteile[0];
$teil2 = $emailteile[1];
$id = safe_query( "select pop3 from email where prefix = ?  and domain = ?  and kunde = ? ",
									TRUE, new SQLString( $teil1 ), new SQLString( $teil2 ), $sqlUser );
if ( ( db_num_rows($id) == 1 ) || ( $pop3 == "$prefix@$domain" ) )	{
	$fehlertext = ltext("ememan2_error6");
	db_close();
	include("email_email.php");
	exit();	 
}

## //

## E-Mail anlegen

safe_query( "INSERT INTO email (prefix, kunde, pop3, domain, anbieter, neu, pop3x) ".
						"VALUES (  ? ,  ? ,  ? ,  ? ,  ? ,  1,  '' )", 
						TRUE, new SQLString( $prefix ), $sqlUser, new SQLString( $pop3 ), 
						$sqlDomain, new SQLString( $anbieter ) );
safe_query( "UPDATE kunden SET hatalias = 1 WHERE kunde = ? ", 
						TRUE, $sqlUser );

safe_query( "UPDATE allgemein set aliases=1", TRUE );

$fehlertitel = ltext("ememan2_error7");
$fehlertext = ltext("ememan2_error8", array("$prefix@$domain"));
db_close();

include("email_email.php");

exit();
## //
?>
