<?
########## 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");
include_once("class.MysqlPermissions.php"); 

$defDbPrivs = new MysqlDbPermissions( false, $mysql_user_version );
$defUserPrivs = new MysqlUserPermissions( false, $mysql_user_version );

global $DEBUG;
#$DEBUG=1;

/* -------------------- functions -------------------- */
function safeAddDb( $host, $dblang2, $user ){

	global $defDbPrivs;

	$laParams = array( 'host' => new SQLString( $host ),
										 'db' => new SQLString( $dblang2 ),
										 'user' => new SQLString( $user ) ); 
	$laWhere = array();
	foreach( $laParams as $lcField => $loVal ){
		$laWhere[] = $lcField.' = '.$loVal->SQL();
	}
	$where = join( ' AND ', $laWhere );
	$id = userdb_query( "SELECT count(db) FROM db WHERE $where" );
	$werte = mysql_fetch_row($id);

	if ( $werte[0] ) {
		$sql = "UPDATE db SET ".$defDbPrivs->getSqlUpdates().' WHERE '.$where;

	} else {
		$sql = "INSERT INTO db (host, db, user,".$defDbPrivs->getSqlFields().
			") VALUES (".$laParams['host']->SQL().",".$laParams['db']->SQL().",".$laParams['user']->SQL().",".
			$defDbPrivs->getSqlValues().")";
	}

	userdb_query( $sql );
}

function addDB ( $host, $dblang2, $user, $dblang ){
  safeAddDb( $host, $dblang2, $user );
  if ( $host != '127.0.0.1' ){
  	safeAddDb( '127.0.0.1', $dblang2, $user );
  }		
  if ( $host != 'localhost' ){
  	safeAddDb( 'localhost', $dblang2, $user );
  }		
  userdb_query ( "CREATE DATABASE $dblang" );
}

function safeAddUser($host,$user,$passwd){

	global $defUserPrivs;

	$laParams = array( 'host' => new SQLString( $host ),
										 'user' => new SQLString( $user ) ); 
	$laWhere = array();
	foreach( $laParams as $lcField => $loVal ){
		$laWhere[] = $lcField.' = '.$loVal->SQL();
	}
	$where = join( ' AND ', $laWhere );

	$id = userdb_query("SELECT count(user) FROM user WHERE $where");

	$werte = mysql_fetch_row($id);
	$laParams['password'] = new SQLString($passwd);

	if ( $werte[0] ) {
		$sql =  "UPDATE user SET password = PASSWORD(".$laParams['password']->SQL()."),". 
			$defUserPrivs->getSqlUpdates()." WHERE $where";
		
	} else {
		$sql = "INSERT INTO user (password,user,host,".$defUserPrivs->getSqlFields().
			") VALUES ( PASSWORD(".$laParams['password']->SQL()."), ".$laParams['user']->SQL()." , ".
			$laParams['host']->SQL()." ,".$defUserPrivs->getSqlValues().")";
	}
	userdb_query( $sql );
}

/* -------------------- end functions -------------------- */

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

$id = safe_query( "select maxmysql, anbieter from kunden where kunde = ? ", TRUE, new SQLString( $PHP_AUTH_USER ) );
$werte = db_fetch_array($id);
$maxmysql = $werte["maxmysql"];
$anbieter = $werte["anbieter"];

$id = safe_query( "select count(datenbank) as mysql from mysql_datenbanken where kunde = ? ", TRUE, new SQLString( $PHP_AUTH_USER ) );
$werte = db_fetch_array($id);
$mysql = $werte["mysql"];

if (($mysql >= $maxmysql) && ($maxmysql != -1))
	{ 
	 $fehlertext = ltext("dbmyan_error1");
	 db_close();
	 include("db_mysql.php");
	 exit();
	}

$id = safe_query( "select datenbank from mysql_datenbanken where kunde = ? ", TRUE, new SQLString( $PHP_AUTH_USER ) );
while ($daten = db_fetch_array($id))
	{
	 $dbstring .= "[".$daten["datenbank"]."]";
	}

$i = $mysql + 5;
while ($i > 0)
	{
	 $newtmpmysql = $i;
	 if (! ereg("\[".$newtmpmysql."\]", $dbstring))
		{ 
		 $newmysql = $newtmpmysql;
		}
	 $i--;
	}

$id = db_query("select * from mysql_datenbanken where datenbank = $newmysql and kunde = '$PHP_AUTH_USER'");
if (db_num_rows($id) != 0)
	{
	 $fehlertext = ltext("dbmyan_error2");
	 db_close();
	 include("db_mysql.php");
	 exit();
	}

$dblang = "usr_".$PHP_AUTH_USER."_".$newmysql;
safe_query( "INSERT INTO mysql_datenbanken (kunde, datenbank, anbieter, dbname) VALUES (  ? ,  ? ,  ? ,  ?  )", TRUE, new SQLString( $PHP_AUTH_USER ), new SQLInteger( $newmysql ), new SQLString( $anbieter ), new SQLString( $dblang ) );
db_close();

if (!isset($mysql_host)) $mysql_host = "localhost";

if ( $mysql_user_port ) {
	$server = $mysql_user_host.':'.$mysql_user_port;
} else {
	$server = $mysql_user_host;
}

if ( userdb_connect() ) {
	
	$defDbPrivs->check();
	$defUserPrivs->check();
	
	if ($mysql < 1){
		$newpw = mkpasswd(8);
		safeAddUser($mysql_host,$PHP_AUTH_USER,$newpw);
		if ( $mysql_host != '127.0.0.1' ){
			safeAddUser('127.0.0.1',$PHP_AUTH_USER,$newpw);
		}		
		if ( $mysql_host != 'localhost' ){
			safeAddUser('localhost',$PHP_AUTH_USER,$newpw);
		}	
		$fehlertitel = ltext("dbmyan_errortitle");
		$fehlertext = ltext("dbmyan_error4", array($dblang, $PHP_AUTH_USER, $mysql_user_host, $newpw));
	} else {
		$fehlertitel = ltext("dbmyan_errortitle");
		$fehlertext = ltext("dbmyan_error3", array($dblang, $PHP_AUTH_USER, $mysql_user_host));
	}	

	$dblang2 = str_replace("_", "\\_", $dblang);
	addDB ( $mysql_host, $dblang2, $PHP_AUTH_USER, $dblang );
	userdb_query("FLUSH PRIVILEGES");

	userdb_close();
}

include("db_mysql.php");

exit();
?>
