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

global $DEBUG;
#$DEBUG=1;

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

$id = db_query("select dbext, kunde from mysql_datenbanken where dbname = '$dbname'");
$werte = db_fetch_array($id);
$toReplace = '127.0.0.1';
if ( $werte["dbext"] == 0 ) {
	$dbext = 1;
	$host = '%';
} else 	{
	$dbext = 0;
	$host = $toReplace;
}

$kunde = $werte["kunde"];

db_query("update mysql_datenbanken set dbext = '$dbext' where dbname = '$dbname'");

$sql = "SELECT COUNT(*) FROM mysql_datenbanken WHERE kunde='$kunde' AND dbext=1";

$id = db_query($sql);
$row = db_fetch_array($id); 
$needext = $row[0];

db_close();

//==========================================================

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

if ( userdb_connect() ) {

	$defDbPrivs->check();
	$defUserPrivs->check();
	
  $dbname2 = str_replace("_", "\\_", $dbname);
  
	// update db
	//
 
  $id = userdb_query("SELECT COUNT(*) FROM db WHERE User = '$kunde' AND Db = '$dbname2' ".
										"AND Host IN ('%','$toReplace')");
  $row = mysql_fetch_row($id);

	if ( $row[0] > 1 ) {
		$sql = "DELETE FROM db WHERE User = '$kunde' AND Db = '$dbname2' AND NOT Host = '$host'";
  } elseif ( $row[0] == 1 ) {
    $sql = "UPDATE db SET Host = '$host' WHERE User = '$kunde' AND Db = '$dbname2' ".
			"AND Host IN ('%','$toReplace')";
  } else {
		$sql = "INSERT INTO db (host, db, user,".$defDbPrivs->getSqlFields().
			") VALUES ( '$host', '$dbname2', '$kunde',".$defDbPrivs->getSqlValues().")";
  }

	userdb_query( $sql );
	// update user
	//
	$host = $needext? '%': $toReplace;

	$sql = "SELECT count(*) FROM user WHERE User = '$kunde' AND Host IN ('%','$toReplace')";

	$id = userdb_query( $sql );
	$row = mysql_fetch_row( $id );

	if( $row[0] > 1 ) {
		$sql = "DELETE FROM user WHERE User = '$kunde' AND NOT Host = '$host'";
		userdb_query( $sql );

	} elseif ( $row[0] == 1 ) {
		$sql = "UPDATE user SET Host = '$host' WHERE User = '$kunde' AND Host IN ('%','$toReplace')";
		userdb_query( $sql );

	}else{
		$id = userdb_query("SELECT Password FROM user Where User = '$kunde'");
		$row = mysql_fetch_row($id);
		$passwd = $row[0];
		if ( $passwd ) {
			$sql =  "INSERT INTO user (password,user,host, ".$defUserPrivs->getSqlFields().
				") VALUES ('$passwd', '$kunde', '$host',".$defDbPrivs->getSqlValues().")";
			userdb_query( $sql );
		}
	}  
 
  userdb_query( "FLUSH PRIVILEGES" );
  userdb_close();

	$fehlertext = ltext("anmydb_error1", array($dbname));

}else{
  $fehlertext = "Error by selection of 'mysql' data base: ".mysql_error();
}  

db_close();

$fehlertitel = ltext("anmydb_errortitle");
include("anbieter_mysql.php");
exit();

?>
