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

include_once( "../include/class.SQLInteger.php" );
include_once( "../include/class.SQLString.php" );
include_once("auth.php");

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

## prüfen ob genug emails vorhanden und anbieter ermitteln
$id = safe_query( "select maxemail, anbieter from kunden where kunde = ? ", TRUE, new SQLString( $PHP_AUTH_USER ) );
$werte = db_fetch_array($id);
$maxemail = $werte["maxemail"];
$anbieter = $werte["anbieter"];

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

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

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

## looking for existing maillist 
$id = safe_query2( "SELECT maillist.name AS ml_name FROM maillist INNER JOIN domains ON (domains.id = maillist.domain_id) WHERE domains.domain = ? AND domains.kunde = ?", $domain, $PHP_AUTH_USER );
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 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 ), new SQLString( $PHP_AUTH_USER ) );
$check = db_num_rows($id);
if ($check < 1)	{ 
$fehlertext = ltext("ememan_error2");
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("ememan_error3");
 db_close();
 include("email_email.php");
 exit();
 } }
## //

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

## prüfen ob pop dem kunden gehört
$id = safe_query( "select account from pop3 where account = ?  and kunde = ? ", TRUE, new SQLString( $pop3 ), new SQLString( $PHP_AUTH_USER ) );
$check = db_num_rows($id);
if ($check < 1)	{ 
$fehlertext = ltext("ememan_error5", array($pop3));
db_close();
include("email_email.php");
exit();
		}
## //

## E-Mail anlegen

$timestamp = time();

safe_query( "INSERT INTO email (prefix, kunde, pop3, domain, anbieter, neu, pop3x) VALUES (  ? ,  ? ,  ? ,  ? ,  ? ,  1,  '' )", TRUE, new SQLString( $prefix ), new SQLString( $PHP_AUTH_USER ), new SQLString( $pop3 ), new SQLString( $domain ), new SQLString( $anbieter ) );
safe_query( "update kunden set hatalias = 1 where kunde = ? ", TRUE, new SQLString( $PHP_AUTH_USER ) );
safe_query( "update allgemein set aliases='1'", TRUE );

safe_query( "update pop3 set lastchange = ?  where account = ?  and kunde = ? ", TRUE, new SQLInteger( $timestamp ), new SQLString( $pop3 ), new SQLString( $PHP_AUTH_USER ) );

$fehlertitel = ltext("ememan_error6");
$fehlertext = ltext("ememan_error7", array("$prefix@$domain"));
db_close();
include("email_email.php");
exit();
## //
?>
