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

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

if (!(strpos($pfad, ":") === False)) {
	$fehlertext = ltext("toftne_error6");
	db_close();
	include("tools_ftp.php");
	exit();
}
	
$id = safe_query( "SELECT ftpmaxkb FROM admin", TRUE );
$werte = db_fetch_array($id);
$ftpmaxkb = $werte["ftpmaxkb"];

$sqlUser = new SQLString( $PHP_AUTH_USER );

## so viel darf noch eingerichtet werden + accountliste
$id = safe_query( "SELECT maxftp, anbieter FROM kunden WHERE kunde = ? ", TRUE, $sqlUser );
$werte = db_fetch_array($id);
$maxftp = $werte["maxftp"];
$anbieter = $werte["anbieter"];

$id = safe_query("SELECT COUNT(account) AS ftp FROM ftp WHERE kunde = ? ",TRUE, $sqlUser );
$werte = db_fetch_array($id);
$ftp = $werte["ftp"];
if (($ftp >= $maxftp) && ($maxftp <> -1))	{ 
	$fehlertext = ltext("toftne_error3");
	db_close();
	include("tools_ftp.php");
	exit();
}

$id = safe_query ( "SELECT account FROM delaccs WHERE kunde = ? AND type = 'ftp'", TRUE, $sqlUser );
$usedNumbers = array();
while ( $werte = db_fetch_array($id) ){
	$usedNumbers[] = ereg_replace($PHP_AUTH_USER.'f',"",$werte[0]);
}
$id = safe_query( "SELECT number FROM ftp 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++;
}
$newftp = $PHP_AUTH_USER."f".$number;


while ( ereg( "/$", $pfad ) )	{
	 $pfad = substr($pfad, 0, -1);
}

$pfad = ereg_replace("/+", "/", $pfad);

if ( substr($pfad, 0, 1) != "/") $pfad = "/".$pfad;
if ( $pfad == "/" ) {
	 $fehlertext = ltext("toftne_error1", array($pfad));
	 db_close();
	 include("tools_ftp.php");
	 exit();
}

$dir = "/$PHP_AUTH_USER/html".$pfad;
exec($bindir."/lsdir.pl ".$dir, $devnull, $status);

if (($status != 0) || (ereg("\.\./", $pfad)) || (ereg("\.\.$", $pfad)))
	{
	 $fehlertext = ltext("toftne_error1", array($pfad));
	 db_close();
	 include("tools_ftp.php");
	 exit();
	}

while (ereg("^/", $pfad))
	{
	 $pfad = substr($pfad, 1, (strlen($pfad) -1));
	}

## existiert der pfad schon?
$id = safe_query( "select pfad from ftp where kunde = ? ", TRUE, $sqlUser );
while($werte = db_fetch_array($id))
	{
	 $dbpfad = $werte["pfad"];
	 if ((ereg("^".$dbpfad."/", $pfad."/")) || (ereg("^".$pfad."/", $dbpfad."/")))
		{
		 if (substr($pfad, 0, 1) != "/") $pfad = "/".$pfad;
		 $fehlertext = ltext("toftne_error2", array($pfad));
		 db_close();
		 include("tools_ftp.php");
		 exit();
		}
	}
## /existiert der pfad schon?

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

safe_query( "INSERT INTO ftp (account, number, uid, passwort, longpw, ".
	"kunde, anbieter, pfad, quota, maxkb, language, lastchange)".
	" VALUES (  ? ,  ? ,  ? ,  '',  ? ,  ? ,  ? ,  ? ,  1,  ? ,  ? ,  ?  )", 
	TRUE, 
	new SQLString( $newftp ), new SQLInteger( $number ), new SQLInteger( $uid ),
	new SQLString( $newpw2 ), $sqlUser, new SQLString( $anbieter ), 
	new SQLString( $pfad ), new SQLInteger( $ftpmaxkb ), new SQLString( $lang_short ), 
	new SQLInteger( $timestamp ) );
	
safe_query( "UPDATE allgemein SET addftp=1, quota=1", TRUE );
freeuid();

$fehlertitel = ltext("toftne_errortitle");
$fehlertext = ltext("toftne_error5", array($newftp, $newpw));
db_close();
$pfad = "";
include("tools_ftp.php");
exit();

?>
