/* AMXMOD script. 
* 
* This file is provided as is (no warranties). 
* Author: Belgar
* Based on Namralkeeg´s Admin Log plugin and mysql functions from 
* JustinHoMi´s Lastname plugin. Tnx guys ;).
*/ 

/* 
This plugin logs admin actions to a database

Use this query to create table: 
CREATE TABLE amx_adminlog (
  id int(11) NOT NULL auto_increment,
  admin_name varchar(32) NOT NULL default '',
  admin_wonid varchar(32) NOT NULL default '',
  admin_cmd varchar(255) NOT NULL default '',
  time timestamp(14) NOT NULL,
  player_name varchar(35) NOT NULL default '',
  player_wonid varchar(35) NOT NULL default '',
  server_name varchar(55) NOT NULL default '',
  server_ip varchar(35) NOT NULL default '',
  PRIMARY KEY  (id)
) TYPE=MyISAM;

  
IMPORTANT: This file must be above the admincmd.amx pluing in the plugins.ini file! 
*/ 

#include <amxmod> 
#include <mysql> 

new merror[32] 
new mhost[64] 
new muser[32] 
new mpass[32] 
new mdb[32] 
new mysql = 0 
new state = 0 

public sql_init(){ 

    get_cvar_string("amx_mysql_host",mhost,64) 
    get_cvar_string("amx_mysql_user",muser,32) 
    get_cvar_string("amx_mysql_pass",mpass,32) 
    get_cvar_string("amx_mysql_db",mdb,32) 

    mysql = mysql_connect(mhost,muser,mpass,mdb,merror,32) 

    if(mysql < 1){ 
        mysql_error(mysql,merror,32) 
        server_print("[AMX] MySQL error : could not connect : '%s'",merror) 
    } 
    else 
        server_print("[AMX] Adminlog SQL Plugin successfully connected.") 

    state = 1 

    return PLUGIN_CONTINUE 
}
public admin_logclient_sql(id) 
{ 
    if (state == 0) 
        sql_init() 

    new thecmd[32] 
    read_argv(0,thecmd,32) 

    new arg[32] 
    read_argv(1,arg,32) 
    new player = find_player("b",arg) 

    if(!player) 
        player = find_player("c",arg) 

    if (player) 
    { 
        if (!(get_user_flags(player)&ADMIN_IMMUNITY)) 
        { 
            new authid[16],authid2[16],name2[32],name[32],hostname[64] 
            get_user_authid(id,authid,16) 
            get_user_authid(player,authid2,16) 
            get_user_name(player,name2,32) 
            get_user_name(id,name,32)
    	    get_cvar_string("hostname",hostname,63)  

	    new query[256] 
    	    format(query,256,"INSERT into amx_adminlog (admin_name,admin_wonid,admin_cmd,player_name,player_wonid,server_name,server_ip) values ('%s','%s','%s','%s','%s','%s','')",name,authid,thecmd,name2,authid2,hostname) 

    	    mysql_query(mysql,query) 

    	    mysql_error(mysql,merror,32) 
    	    if (merror[0]){ 
        	server_print("[AMX] MYSQL error: %s",merror) 
        	return PLUGIN_HANDLED 
    	    }

        } 
    } 

    return PLUGIN_CONTINUE 

} 

public admin_logserver_sql(id) 
{ 
    if (state == 0) 
        sql_init()
    
    if ((get_user_flags(id) & ADMIN_RCON)||(get_user_flags(id) & ADMIN_CFG)||(get_user_flags(id) & ADMIN_CVAR)||(get_user_flags(id) & ADMIN_MAP)) 
    { 
	    new thecmd[32] 
	    read_argv(0,thecmd,32) 
	
	    new message[192] 
	    read_args(message,192) 
	
	    new authid[16],name[32] 
	    get_user_authid(id,authid,16) 
	    get_user_name(id,name,32) 
	
	    new hostname[64]
	    get_cvar_string("hostname",hostname,63)
	
	    new query[256] 
	    format(query,256,"INSERT into amx_adminlog (admin_name,admin_wonid,admin_cmd,server_name,server_ip) values ('%s','%s','%s: %s','%s','')",name,authid,thecmd,message,hostname) 
	
	    mysql_query(mysql,query) 
	
	    mysql_error(mysql,merror,32) 
	    if (merror[0]){ 
		server_print("[AMX] MYSQL error: %s",merror) 
		return PLUGIN_HANDLED 
	    } 
    } 

    return PLUGIN_CONTINUE 
} 

public plugin_end() 
{ 
    if (mysql > 0) 
        mysql_close(mysql) 
    return PLUGIN_CONTINUE 
} 

public plugin_init() 
{ 
    register_plugin("Admin Log SQL","0.7.3","Belgar") 
    register_clcmd("amx_kick","admin_logclient_sql",ADMIN_KICK,"Kick Log Alias") 
    register_clcmd("amx_ban","admin_logclient_sql",ADMIN_BAN,"Ban Log Alias") 
    register_clcmd("amx_slay","admin_logclient_sql",ADMIN_SLAY,"Slay Log Alias") 
    register_clcmd("amx_slay2","admin_logclient_sql",ADMIN_SLAY,"Slay2 Log Alias")
    register_clcmd("amx_vote","admin_logclient_sql",ADMIN_VOTE,"Vote Log Alias") 
    register_clcmd("amx_map","admin_logserver_sql",ADMIN_MAP,"Map Log Alias") 
    register_clcmd("amx_cvar","admin_logserver_sql",ADMIN_CVAR,"Cvar Log Alias") 
    register_clcmd("amx_cfg","admin_logserver_sql",ADMIN_CFG,"Config Log Alias") 
    register_clcmd("amx_rcon","admin_logserver_sql",ADMIN_RCON,"Rcon Log Alias")  
    return PLUGIN_CONTINUE 
}
