--------------------
-- 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 .. "'" )
Disconnect()
if not results[1] then
ServerLog("No Results Found ... Creating Entry")
--print("INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( ply:Nick() ) ) .. ", " ..Format( Escape( "user" ) ) .. ", " ..Format( Escape( "FALSE" ) ).. " )")
local result = DoQuery( "INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( ply:Nick() ) ) .. ", " ..Format( Escape( "user" ) ) .. ", " ..Format( Escape( "FALSE" ) ).. " )" )
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]
ServerLog("Name: " .. ply.name)
ServerLog("SteamID: " .. ply.steamid)
ServerLog("Group: " .. ply.group)
ServerLog("Banned?: " .. ply.banned)
if ply.name != ply:Nick() then
ServerLog( "Updating name for player: " ..ply:Nick() )
--print("UPDATE " .. table .. " SET pname="..Format( Escape( ply:Nick() ) ) .. " WHERE sid=" ..Format( Escape( sid ) ) )
local result3 = DoQuery( "UPDATE " .. table .. " SET pname="..Format( Escape( ply:Nick() ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
end
ServerLog( "AUTHING PLAYER: " ..ply:Nick().. " in group (" ..ply.group..")." )
ULib.ucl.addUser( ply:SteamID(), _, _, ply.group )
if ply.banned == "TRUE" then
ULib.kick( ply, "BANNED! Visit our website at WEBSITE HERE to dispute this ban." )
end
end
end
hook.Add( "PlayerAuthed", "G4PSQL_Auth", G4PSQL_Auth )
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 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 then
ULib.tsay(ply, "created player: (" ..sid.. ") with group (" ..group.. ")", true)
ServerLog("created player: (" ..sid.. ") with group (" ..group.. ")")
else
ServerLog("created player: (" ..sid.. ") with group (" ..group.. ")")
end
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 then
ULib.tsay(ply, "set player: (" ..sid.. ") to group (" ..group.. ")", true)
ServerLog("set player: (" ..sid.. ") to group (" ..group.. ")")
else
ServerLog("set player: (" ..sid.. ") to group (" ..group.. ")")
end
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]
if !IsValid( ply ) or ply:IsAdmin() or ply:IsUserGroup("moderator") then
if #args != 1 then
if ply 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 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( "TRUE" ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
Disconnect()
if ply then
ULib.tsay(ply, "Banned player with SteamID: (" ..sid.. ")", true)
ServerLog("Banned player with SteamID: (" ..sid.. ")")
else
ServerLog("Banned player with SteamID: (" ..sid.. ")")
end
ULib.tsay(_, "Player with SteamID (" ..sid.. ") was globally banned!", true)
for _, v in pairs( player.GetAll() ) do
if v:SteamID() == sid then
ULib.kick( v, "BANNED! Visit our website at WEBSITE HERE to dispute this ban." )
end
end
end
else
ULib.tsay(ply, "This command is reserved for administrators only!", true)
end
end
concommand.Add( "g4p_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 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 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 then
ULib.tsay(ply, "Unbanned player with SteamID: (" ..sid.. ")", true)
ServerLog("Unbanned player with SteamID: (" ..sid.. ")")
else
ServerLog("Unbanned player with SteamID: (" ..sid.. ")")
end
ULib.tsay(_, "Player with SteamID (" ..sid.. ") was globally unbanned!", true)
end
else
ULib.tsay(ply, "This command is reserved for administrators only!", true)
end
end
concommand.Add( "g4p_gunban", G4PSQL_UnBan)