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

$loUser = new SQLString($PHP_AUTH_USER);
$id = safe_query2( "select dbext from anbieter where anbieter=?", $loUser);
$werte = db_fetch_array($id);
if ($werte["dbext"] == 0)
	{
	 $fehlertext = ltext("kumydb_error1");
	 db_close();
	 include("kunden_mysql.php");
	 exit();
	}

$loDbName = new SQLString($dbname);
$id = safe_query2( "select dbext, kunde from mysql_datenbanken where anbieter = ? and dbname = ?", $loUser, $loDbName);
if (db_num_rows($id) != 1)
	{
	 $fehlertext = ltext("kumydb_error2");
	 db_close();
	 include("kunden_mysql.php");
	 exit();
	}

$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"];
$loKunde = new SQLString($kunde);

safe_query2("UPDATE mysql_datenbanken SET dbext = $dbext WHERE anbieter = ? AND dbname = ?", $loUser, $loDbName);

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

$id = safe_query2( $sql, $loKunde);
$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( false ) ) {
	
	$defDbPrivs->check();
	$defUserPrivs->check();
	
  $dbname2 = str_replace("_", "\\_", $dbname);
  $lcKunde = $loKunde->SQL();
	$loDb = new SQLString( $dbname2 );
	$lcDb = $loDb->SQL();
	// update db
	//
  $id = userdb_query( "SELECT COUNT(*) FROM db WHERE User = $lcKunde AND Db = $lcDb ".
											"AND Host IN ('%','$toReplace')");
  $row = mysql_fetch_row($id);

  if ( $row[0] > 1 ) {
		$sql = "DELETE FROM db WHERE User = $lcKunde AND Db = $lcDb AND NOT Host = '$host'";

	} elseif( $row[0] == 1 ) {
    $sql = "UPDATE db SET Host = '$host' WHERE User = $lcKunde AND Db = $lcDb ".
			"AND Host IN ('%','$toReplace')";

  } else {
		$sql = "INSERT INTO db (host, db, user, ".$defDbPrivs->getSqlFields().
			") VALUES ( '$host', $lcDb, $lcKunde,".$defDbPrivs->getSqlValues().")";
  }
	userdb_query( $sql );

	// update user
	//
	$host = $needext? '%': $toReplace;

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

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

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

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

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

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

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

$fehlertitel = ltext("kumydb_errortitle");

db_close();
include("kunden_mysql.php");
exit();

?>
