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

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

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

	global $defUserPrivs;

#	echo "host: $host, user: $user, passwd: $passwd<br>\n";

	$loUser = new SQLString( $user );
	$loHost = new SQLString( $host );

	$sql = "SELECT count(user) FROM user WHERE user = ".$loUser->SQL()." AND host = ".$loHost->SQl();

	$id = userdb_query( $sql );
	$werte = mysql_fetch_row($id);
	if ( !$werte[0] ) {

		$loPass = new SQLString( $passwd );
	  $sql = "INSERT INTO user (password,user,host,".$defUserPrivs->getSqlFields().
			") VALUES ( Password(".$loPass->SQL()."),".$loUser->SQL()." , ".$loHost->SQL().",".$defUserPrivs->getSqlValues().")";

		userdb_query($sql);

	}
}

//-----------------------------------------------------------

if ($neupw1 != $neupw2) {
 $fehlertext = ltext("dbmypw2_error2"); 
 include("db_mysql.php"); 
 exit();
}

if (!valid_pw($neupw1)){
  $fehlertext = ltext("dbmypw2_error3");
  include("db_mysql.php"); 
  exit();
}

$sql = "SELECT count(dbext) FROM mysql_datenbanken WHERE kunde='$PHP_AUTH_USER' AND dbext=1";
$id = db_query( $sql );
$werte = mysql_fetch_row($id);
if ( $werte[0] ){
	$extHost = '%';
} else {
	$extHost = '127.0.0.1';
}

$safePasswd = addslashes( $neupw1 );

db_close();

if ( !userdb_connect( false ) ) {
  $fehlertext = "Error by connection to the MySQL server ($mysql_user_user @ $mysql_user_host) ".mysql_error()."<br>\n";
  include("db_mysql.php"); 
  exit();
}

userdb_query("UPDATE user SET Password = Password('$safePasswd') WHERE User = '$PHP_AUTH_USER'");

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

safeAddUser($mysql_host, $PHP_AUTH_USER, $safePasswd );

if ( $mysql_host != $extHost ){
  safeAddUser( $extHost, $PHP_AUTH_USER, $safePasswd );
}		
if ( $mysql_host != 'localhost' ){
  safeAddUser('localhost', $PHP_AUTH_USER, $safePasswd );
}	

userdb_query( "FLUSH PRIVILEGES" );

$fehlertitel = ltext("dbmypw2_error4");
$fehlertext = ltext("dbmypw2_error5");
userdb_close();

include("db_mysql.php");
exit();
?>
