--------------------
-- Config --
--------------------
local host = "127.0.0.1"
local username = "root"
local password = ""
local database = "gmodtest"
local port = 3306
local table = "test"
local persistent = true -- Use a persistent MySQL connection?
require( "mysql" )
local db
function DoQuery( query, type )
local result, isok, err = mysql.query( db, query, type or mysql.QUERY_NUMERIC )
if not isok and err == "" then isok = true end -- False positive
if not isok then
error( tostring( err ), 2 )
return nil
end
if result then
-- print( query ) -- For debug
-- PrintTable( result )
end
return result
end
function Connect()
if db then return db end -- Still connected
db, err = mysql.connect( host, username, password, database, port )
if db == 0 then
db = nil
error( tostring( err ), 1 )
return
end
return db
end
function Disconnect( force )
if not db then return end -- Already disconnected
if persistent and not force then return end -- Don't disconnect, persistent
local succ, err = mysql.disconnect( db )
if not succ then
error( tostring( err ), 2 )
end
db = nil
end
hook.Add( "ShutDown", "G4P_SQL", function() Disconnect( true ) end ) -- Force closed on shutdown.
function Escape( str )
if not db then
Msg( "Not connected to DB.\n" )
return
end
if not str then return end
local esc, err = mysql.escape( db, str )
if not esc then
error( tostring( err ), 2 )
return nil
end
-- print( "esc=" .. esc ) -- For debug
return esc
end
-- Because we use this a lot
function Format( str )
if not str then return "NULL" end
return string.format( "%q", str )
end
function G4PSQL_Auth( ply, sid, uid )
Connect()
local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
local newip = string.Explode( ":", ply:IPAddress() )
if not results[1] then
ServerLog("No Results Found ... Creating Entry")
local result = DoQuery( "INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned, pmodel, lastip ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( ply:Nick() ) ) .. ", " ..Format( Escape( "user" ) ) .. ", " ..Format( Escape( "FALSE" ) ).. ", " ..Format( Escape( "NONESET" ) ).. ", " ..Format( Escape( newip[1] ) ).. " )" )
else
ServerLog("Results Found ... Loading Results")
ply.steamid = results[1][1]
ply.name = results[1][2]
ply.group = results[1][3]
ply.banned = results[1][4]
ply.pmodel = results[1][5]
ply.lplayed = results[1][6]
ply.lastip = results[1][7]
ServerLog("Name: " .. ply.name)
ServerLog("SteamID: " .. ply.steamid)
ServerLog("Group: " .. ply.group)
ServerLog("Banned?: " .. ply.banned)
ServerLog("Player Model: " .. ply.pmodel)
ServerLog("Last Played: " .. ply.lplayed)
ServerLog("Last Known IP: " .. ply.lastip)
if ply.name != ply:Nick() then
ServerLog( "Updating name for player: " ..ply:Nick() )
local result3 = DoQuery( "UPDATE " .. table .. " SET pname="..Format( Escape( ply:Nick() ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
end
if ply.lastip != newip[1] then
ServerLog( "Updating IP for player: " ..ply:Nick() )
local result3 = DoQuery( "UPDATE " .. table .. " SET lastip="..Format( Escape( newip[1] ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
end
local result4 = DoQuery( "UPDATE " .. table .. " SET lplayed="..Format( Escape( os.time() ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
if ply.banned == "TRUE" then
ULib.kick( ply, "BANNED! Visit our website at www.g4p.org to dispute this ban" )
else
ServerLog( "AUTHING PLAYER: " ..ply:Nick().. " in group (" ..ply.group..")." )
if ply.group != "user" then
ULib.ucl.addUser( ply:SteamID(), _, _, ply.group )
end
end
end
ply.newauth = 1
Disconnect()
end
hook.Add( "PlayerAuthed", "G4PSQL_Auth", G4PSQL_Auth )
function G4PSQL_PlayerSpawn(ply)
local sid = ply:SteamID()
if ply.newauth == 3 then
--ServerLog( "CHECKING PLAYER MODEL FOR PLAYER: " ..ply:Nick() )
local model1 = string.Explode( "/", ply:GetModel() )
local model2 = string.Explode( ".", model1[#model1] )
local playermodel = model2[1]
--ServerLog( "CURRENT PLAYER MODEL: " ..playermodel )
--ServerLog( "SAVED PLAYER MODEL: " ..ply.pmodel )
if playermodel != ply.pmodel then
ServerLog( "Updating model for player: " ..ply:Nick() )
--print("UPDATE " .. table .. " SET pname="..Format( Escape( ply:Nick() ) ) .. " WHERE sid=" ..Format( Escape( sid ) ) )
Connect()
local result3 = DoQuery( "UPDATE " .. table .. " SET pmodel="..Format( Escape( playermodel ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
Disconnect()
end
ply.newauth = 0
end
if ply.newauth == 2 then
ply.newauth = 3
end
if ply.newauth == 1 then
ply.newauth = 2
end
end
hook.Add( "PlayerSpawn", "G4PSQL_PlayerSpawn", G4PSQL_PlayerSpawn )
function G4PSQL_ChangeGroup( ply, cmd, args )
local sid = args[1]
local group = string.lower(args[2])
if !IsValid( ply ) or ply:IsAdmin() then
if #args != 2 then
if ply:IsPlayer() then
ULib.tsay(ply, "Not enough arguements!", true)
else
ServerLog("Not enough arguements!")
end
end
Connect()
local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
Disconnect()
if not results[1] then
Connect()
local result = DoQuery( "INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( "SET BY CONSOLE" ) ) .. ", " ..Format( Escape( group ) ) .. ", " ..Format( Escape( "FALSE" ) ).. " )" )
Disconnect()
if ply:IsPlayer() then
ULib.tsay(ply, "created player: (" ..sid.. ") with group (" ..group.. ")", true)
end
ServerLog("created player: (" ..sid.. ") with group (" ..group.. ")")
else
Connect()
--print( "UPDATE " .. table .. " SET group="..Format( Escape( group ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
local result = DoQuery( "UPDATE " .. table .. " SET pgroup="..Format( Escape( group ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
Disconnect()
if ply:IsPlayer() then
ULib.tsay(ply, "set player: (" ..sid.. ") to group (" ..group.. ")", true)
end
ServerLog("set player: (" ..sid.. ") to group (" ..group.. ")")
end
ULib.ucl.addUser( sid, _, _, group )
else
ULib.tsay(ply, "This command is reserved for administrators only!", true)
end
end
concommand.Add( "g4p_setgroup", G4PSQL_ChangeGroup)
function G4PSQL_Ban( ply, cmd, args )
local sid = args[1]
local reason = args[2]
local override = args[3] or "WAT?"
local asid = "CONSOLE"
if ply:IsPlayer() then
asid = ply:SteamID()
end
if ULib.getUser(sid) then
sid = ULib.getUser(sid):SteamID()
end
if !IsValid( ply ) or ply:IsAdmin() or ply:IsUserGroup("moderator") then
if !reason then
ServerLog("Not enough arguements! g4p_gban <SteamID or Connected Player Name> <Reason> Both arguements need to be in quotes!")
else
Connect()
local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
Disconnect()
if not results[1] then
if override == "1" then
Connect()
local noreply = DoQuery( "INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned, adminid, reason, bantime ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( "SET BY CONSOLE" ) ) .. ", " ..Format( Escape( "user" ) ) .. ", " ..Format( Escape( "TRUE" ) ).. ", " ..Format( Escape( asid ) ).. ", " ..Format( Escape( reason ) ).. ", " ..Format( Escape( os.time() ) ).. " )" )
Disconnect()
if ply:IsPlayer() then
ULib.tsay(ply, "Player [SET BY CONSOLE] was added to the global ban list for [" ..reason.. "] (Override)", true)
end
ServerLog("Player [SET BY CONSOLE] was added to the global ban list for [" ..reason.. "] (Override)")
ULib.tsay(_, "Player [SET BY CONSOLE] was added to the global ban list for [" ..reason.. "] (Override)", true)
else
if ply:IsPlayer() then
ULib.tsay(ply, "Player with SteamID (" ..sid..") does not exist! To override this add \"OR\" to the end of the concommand.", true)
end
ServerLog("Player with SteamID (" ..sid..") does not exist! To override this add 1 to the end of the concommand.")
end
else
Connect()
local result = DoQuery( "UPDATE " .. table .. " SET banned=" .. Format( Escape( "TRUE" ) ) .. ", adminid=" .. Format( Escape( asid ) ) .. ", reason=" ..Format( Escape( reason ) ).. ", bantime=" .. Format( Escape( os.time() ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
Disconnect()
if ply:IsPlayer() then
ULib.tsay(ply, "Player [" ..results[1][2].. "] was added to the global ban list for [" ..reason.. "]", true)
end
ServerLog("Player [" ..results[1][2].. "] was added to the global ban list for [" ..reason.. "]")
ULib.tsay(_, "Player [" ..results[1][2].. "] was added to the global ban list for [" ..reason.. "]", true)
for _, v in pairs( player.GetAll() ) do
if v:SteamID() == sid then
ULib.kick( v, "BANNED for [" .. reason.. "]! Visit our website at www.g4p.org to dispute this ban." )
end
end
end
end
else
ULib.tsay(ply, "This command is reserved for administrators only!", true)
end
end
concommand.Add( "gban", G4PSQL_Ban)
function G4PSQL_UnBan( ply, cmd, args )
local sid = args[1]
if !IsValid( ply ) or ply:IsAdmin() or ply:IsUserGroup("moderator") then
if #args != 1 then
if ply:IsPlayer() then
ULib.tsay(ply, "Not enough arguements!", true)
else
ServerLog("Not enough arguements!")
end
end
Connect()
local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
Disconnect()
if not results[1] then
if ply:IsPlayer() then
ULib.tsay(ply, "Player with SteamID (" ..sid..") does not exist!", true)
ServerLog("Player with SteamID (" ..sid..") does not exist!")
else
ServerLog("Player with SteamID (" ..sid..") does not exist!")
end
else
Connect()
local result = DoQuery( "UPDATE " .. table .. " SET banned="..Format( Escape( "FALSE" ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
Disconnect()
if ply:IsPlayer() then
ULib.tsay(ply, "Unbanned player [" ..results[1][2].. "]", true)
end
ServerLog("Unbanned player [" ..results[1][2].. "]")
ULib.tsay(_, "Unbanned player [" ..results[1][2].. "]", true)
end
else
ULib.tsay(ply, "This command is reserved for administrators only!", true)
end
end
concommand.Add( "gunban", G4PSQL_UnBan)