Please run this version which will log the steps so we can figure out why it's not working. Also, I fixed the issue where it was not deleting the temp file. This would occur when it was unable to add the new admin user.
I added comments to the above code so you can understand what is going on, since the point of this forum is to help not give.
PHP Code:
AddAdmin( const szAdminGroup[] , const szAuth[] , const szAccessFlags[] , const szPassword[] , const szAcctFlags[] , const szName[] , iTime = 0 )
{
new iFile , iTempFile , iLineLen , szBuffer[ 256 ] , szConfig[ MAX_FMT_LENGTH ] , szTmpConfig[ MAX_FMT_LENGTH ] , bool:bInGroup , bool:bDone;
get_configsdir( szConfig , charsmax( szConfig ) );
add( szConfig , charsmax( szConfig ) , "/users.ini" );
formatex( szTmpConfig , charsmax( szTmpConfig ) , "%s_tmp" , szConfig );
if ( ( iFile = fopen( szConfig , "rt" ) ) )
{
if ( ( iTempFile = fopen( szTmpConfig , "wt" ) ) )
{
while ( ( iLineLen = fgets( iFile , szBuffer , charsmax( szBuffer ) ) ) )
{
log_to_file( "AddAdmin" , "Line=[%s]" , szBuffer );
if ( !bDone && !bInGroup && equali( szBuffer , szAdminGroup , iLineLen - 1 ) )
{
log_to_file( "AddAdmin" , "In Group = true" );
bInGroup = true;
}
else if ( !bDone && bInGroup && ( szBuffer[ 0 ] == '^n' ) )
{
log_to_file( "AddAdmin" , "Adding new user" );
WriteUser( iTempFile , szAuth , szAccessFlags , szPassword , szAcctFlags , szName , iTime );
bDone = true;
continue;
}
//log_to_file( "AddAdmin" , "Writing unchanged line" );
fputs( iTempFile , szBuffer );
}
if ( bInGroup && !bDone )
{
log_to_file( "AddAdmin" , "Adding user to end of file" );
WriteUser( iTempFile , szAuth , szAccessFlags , szPassword , szAcctFlags , szName , iTime );
bDone = true;
}
fclose( iFile );
fclose( iTempFile );
if ( bDone )
{
delete_file( szConfig );
rename_file( szTmpConfig , szConfig , true );
server_cmd( "amx_reloadadmins" );
}
else
{
delete_file( szTmpConfig );
}
}
}
}
__________________