From 2722215aba21eb75209a7caaca2c91ed972d0eee Mon Sep 17 00:00:00 2001 From: GhzGarage Date: Tue, 3 Aug 2021 19:22:06 -0500 Subject: [PATCH] Remove QBCore.Functions.ExecuteSql --- server/events.lua | 30 ++++--- server/functions.lua | 41 ++-------- server/player.lua | 190 ++++++++++++++++++++++--------------------- 3 files changed, 119 insertions(+), 142 deletions(-) diff --git a/server/events.lua b/server/events.lua index 7806b52..0dc9230 100644 --- a/server/events.lua +++ b/server/events.lua @@ -232,17 +232,16 @@ AddEventHandler('QBCore:ToggleDuty', function() end) Citizen.CreateThread(function() - exports.ghmattimysql:execute('SELECT * FROM permissions', function(result) - if result[1] ~= nil then - for k, v in pairs(result) do - QBCore.Config.Server.PermissionList[v.license] = { - license = v.license, - permission = v.permission, - optin = true, - } - end + local result = exports['ghmattimysql']:executeSync('SELECT * FROM permissions') + if result[1] ~= nil then + for k, v in pairs(result) do + QBCore.Config.Server.PermissionList[v.license] = { + license = v.license, + permission = v.permission, + optin = true, + } end - end) + end end) QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items) @@ -274,11 +273,10 @@ end) RegisterServerEvent('QBCore:Command:CheckOwnedVehicle') AddEventHandler('QBCore:Command:CheckOwnedVehicle', function(VehiclePlate) if VehiclePlate ~= nil then - exports.ghmattimysql:execute('SELECT * FROM player_vehicles WHERE plate=@plate', {['@plate'] = VehiclePlate}, function(result) - if result[1] ~= nil then - exports.ghmattimysql:execute('UPDATE player_vehicles SET state=@state WHERE citizenid=@citizenid', {['@state'] = 1, ['@citizenid'] = result[1].citizenid}) - TriggerEvent('qb-garages:server:RemoveVehicle', result[1].citizenid, VehiclePlate) - end - end) + local result = exports['ghmattimysql']:executeSync('SELECT * FROM player_vehicles WHERE plate=@plate', {['@plate'] = VehiclePlate}) + if result[1] ~= nil then + exports.ghmattimysql:execute('UPDATE player_vehicles SET state=@state WHERE citizenid=@citizenid', {['@state'] = 1, ['@citizenid'] = result[1].citizenid}) + TriggerEvent('qb-garages:server:RemoveVehicle', result[1].citizenid, VehiclePlate) + end end end) diff --git a/server/functions.lua b/server/functions.lua index 63f6096..996cfa5 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -1,26 +1,5 @@ QBCore.Functions = {} -QBCore.Functions.ExecuteSql = function(wait, query, cb) - local rtndata = {} - local waiting = true - exports['ghmattimysql']:execute(query, {}, function(data) - if cb ~= nil and wait == false then - cb(data) - end - rtndata = data - waiting = false - end) - if wait then - while waiting do - Citizen.Wait(5) - end - if cb ~= nil and wait == true then - cb(rtndata) - end - end - return rtndata -end - QBCore.Functions.GetEntityCoords = function(entity) local coords = GetEntityCoords(entity, false) local heading = GetEntityHeading(entity) @@ -149,16 +128,15 @@ QBCore.Functions.IsWhitelisted = function(source) local identifiers = GetPlayerIdentifiers(source) local rtn = false if (QBCore.Config.Server.whitelist) then - exports['ghmattimysql']:execute('SELECT * FROM whitelist WHERE license=@license', {['@license'] = QBCore.Functions.GetIdentifier(source, 'license')}, function(result) - local data = result[1] - if data ~= nil then - for _, id in pairs(identifiers) do - if data.license == id then - rtn = true - end + local result = exports['ghmattimysql']:executeSync('SELECT * FROM whitelist WHERE license=@license', {['@license'] = QBCore.Functions.GetIdentifier(source, 'license')}) + local data = result[1] + if data ~= nil then + for _, id in pairs(identifiers) do + if data.license == id then + rtn = true end end - end) + end else rtn = true end @@ -245,9 +223,8 @@ end QBCore.Functions.IsPlayerBanned = function (source) local retval = false local message = "" - local result = exports.ghmattimysql:executeSync('SELECT * FROM bans WHERE license=@license', {['@license'] = QBCore.Functions.GetIdentifier(source, 'license')})[1] - - if result ~= nil then + local result = exports.ghmattimysql:executeSync('SELECT * FROM bans WHERE license=@license', {['@license'] = QBCore.Functions.GetIdentifier(source, 'license')}) + if result[1] ~= nil then if os.time() < result.expire then retval = true local timeTable = os.date("*t", tonumber(result.expire)) diff --git a/server/player.lua b/server/player.lua index 34ddc4b..4228721 100644 --- a/server/player.lua +++ b/server/player.lua @@ -4,22 +4,21 @@ QBCore.Player = {} QBCore.Player.Login = function(source, citizenid, newData) if source ~= nil then if citizenid then - QBCore.Functions.ExecuteSql(true, "SELECT * FROM `players` WHERE `citizenid` = '"..citizenid.."'", function(result) - local PlayerData = result[1] - if PlayerData ~= nil then - PlayerData.money = json.decode(PlayerData.money) - PlayerData.job = json.decode(PlayerData.job) - PlayerData.position = json.decode(PlayerData.position) - PlayerData.metadata = json.decode(PlayerData.metadata) - PlayerData.charinfo = json.decode(PlayerData.charinfo) - if PlayerData.gang ~= nil then - PlayerData.gang = json.decode(PlayerData.gang) - else - PlayerData.gang = {} - end + local result = exports['ghmattimysql']:executeSync('SELECT * FROM players WHERE citizenid=@citizenid', {['@citizenid'] = citizenid}) + local PlayerData = result[1] + if PlayerData ~= nil then + PlayerData.money = json.decode(PlayerData.money) + PlayerData.job = json.decode(PlayerData.job) + PlayerData.position = json.decode(PlayerData.position) + PlayerData.metadata = json.decode(PlayerData.metadata) + PlayerData.charinfo = json.decode(PlayerData.charinfo) + if PlayerData.gang ~= nil then + PlayerData.gang = json.decode(PlayerData.gang) + else + PlayerData.gang = {} end - QBCore.Player.CheckPlayerData(source, PlayerData) - end) + end + QBCore.Player.CheckPlayerData(source, PlayerData) else QBCore.Player.CheckPlayerData(source, newData) end @@ -417,35 +416,34 @@ end QBCore.Player.Save = function(source) local PlayerData = QBCore.Players[source].PlayerData if PlayerData ~= nil then - QBCore.Functions.ExecuteSql(true, "SELECT * FROM `players` WHERE `citizenid` = '"..PlayerData.citizenid.."'", function(result) - if result[1] == nil then - exports.ghmattimysql:execute('INSERT INTO players (citizenid, cid, license, name, money, charinfo, job, gang, position, metadata) VALUES (@citizenid, @cid, @license, @name, @money, @charinfo, @job, @gang, @position, @metadata)', { - ['@citizenid'] = PlayerData.citizenid, - ['@cid'] = tonumber(PlayerData.cid), - ['@license'] = PlayerData.license, - ['@name'] = PlayerData.name, - ['@money'] = json.encode(PlayerData.money), - ['@charinfo'] = json.encode(PlayerData.charinfo), - ['@job'] = json.encode(PlayerData.job), - ['@gang'] = json.encode(PlayerData.gang), - ['@position'] = json.encode(PlayerData.position), - ['@metadata'] = json.encode(PlayerData.metadata) - }) - else - exports.ghmattimysql:execute('UPDATE players SET license=@license, name=@name, money=@money, charinfo=@charinfo, job=@job, gang=@gang, position=@position, metadata=@metadata WHERE citizenid=@citizenid', { - ['@citizenid'] = PlayerData.citizenid, - ['@license'] = PlayerData.license, - ['@name'] = PlayerData.name, - ['@money'] = json.encode(PlayerData.money), - ['@charinfo'] = json.encode(PlayerData.charinfo), - ['@job'] = json.encode(PlayerData.job), - ['@gang'] = json.encode(PlayerData.gang), - ['@position'] = json.encode(PlayerData.position), - ['@metadata'] = json.encode(PlayerData.metadata) - }) - end - QBCore.Player.SaveInventory(source) - end) + local result = exports.ghmattimysql:executeSync('SELECT * FROM players WHERE citizenid=@citizenid', {['@citizenid'] = citizenid}) + if result[1] == nil then + exports.ghmattimysql:execute('INSERT INTO players (citizenid, cid, license, name, money, charinfo, job, gang, position, metadata) VALUES (@citizenid, @cid, @license, @name, @money, @charinfo, @job, @gang, @position, @metadata)', { + ['@citizenid'] = PlayerData.citizenid, + ['@cid'] = tonumber(PlayerData.cid), + ['@license'] = PlayerData.license, + ['@name'] = PlayerData.name, + ['@money'] = json.encode(PlayerData.money), + ['@charinfo'] = json.encode(PlayerData.charinfo), + ['@job'] = json.encode(PlayerData.job), + ['@gang'] = json.encode(PlayerData.gang), + ['@position'] = json.encode(PlayerData.position), + ['@metadata'] = json.encode(PlayerData.metadata) + }) + else + exports.ghmattimysql:execute('UPDATE players SET license=@license, name=@name, money=@money, charinfo=@charinfo, job=@job, gang=@gang, position=@position, metadata=@metadata WHERE citizenid=@citizenid', { + ['@citizenid'] = PlayerData.citizenid, + ['@license'] = PlayerData.license, + ['@name'] = PlayerData.name, + ['@money'] = json.encode(PlayerData.money), + ['@charinfo'] = json.encode(PlayerData.charinfo), + ['@job'] = json.encode(PlayerData.job), + ['@gang'] = json.encode(PlayerData.gang), + ['@position'] = json.encode(PlayerData.position), + ['@metadata'] = json.encode(PlayerData.metadata) + }) + end + QBCore.Player.SaveInventory(source) QBCore.ShowSuccess(GetCurrentResourceName(), PlayerData.name .." PLAYER SAVED!") else QBCore.ShowError(GetCurrentResourceName(), "ERROR QBCORE.PLAYER.SAVE - PLAYERDATA IS EMPTY!") @@ -463,7 +461,6 @@ local playertables = { {table = "players"}, {table = "apartments"}, {table = "bank_accounts"}, - {table = "bills"}, {table = "crypto_transactions"}, {table = "phone_invoices"}, {table = "phone_messages"}, @@ -477,43 +474,49 @@ local playertables = { } QBCore.Player.DeleteCharacter = function(source, citizenid) - for k,v in pairs(playertables) do - QBCore.Functions.ExecuteSql(true, "DELETE FROM `"..v.table.."` WHERE `citizenid` = '"..citizenid.."'") + local license = QBCore.Functions.GetIdentifier(source, 'license') + local result = exports.ghmattimysql:scalarSync('SELECT license FROM players where citizenid = ?', {citizenid}) + if license == result then + for k,v in pairs(playertables) do + exports.ghmattimysql:execute('DELETE FROM '..v.table..' WHERE citizenid = ?', {citizenid}) + end + TriggerEvent("qb-log:server:CreateLog", "joinleave", "Character Deleted", "red", "**".. GetPlayerName(source) .. "** ("..QBCore.Functions.GetIdentifier(source, 'license')..") deleted **"..citizenid.."**..") + else + DropPlayer(source, 'You Have Been Kicked For Exploitation') + TriggerEvent("qb-log:server:CreateLog", "anticheat", "Anti-Cheat", "white", GetPlayerName(source).." Has Been Dropped For Character Deletion Exploit", false) end - TriggerEvent("qb-log:server:CreateLog", "joinleave", "Character Deleted", "red", "**".. GetPlayerName(source) .. "** ("..QBCore.Functions.GetIdentifier(source, 'license')..") deleted **"..citizenid.."**..") end QBCore.Player.LoadInventory = function(PlayerData) PlayerData.items = {} - QBCore.Functions.ExecuteSql(true, "SELECT * FROM `players` WHERE `citizenid` = '"..PlayerData.citizenid.."'", function(result) - if result[1] ~= nil then - if result[1].inventory ~= nil then - plyInventory = json.decode(result[1].inventory) - if next(plyInventory) ~= nil then - for _, item in pairs(plyInventory) do - if item ~= nil then - local itemInfo = QBCore.Shared.Items[item.name:lower()] - PlayerData.items[item.slot] = { - name = itemInfo["name"], - amount = item.amount, - info = item.info ~= nil and item.info or "", - label = itemInfo["label"], - description = itemInfo["description"] ~= nil and itemInfo["description"] or "", - weight = itemInfo["weight"], - type = itemInfo["type"], - unique = itemInfo["unique"], - useable = itemInfo["useable"], - image = itemInfo["image"], - shouldClose = itemInfo["shouldClose"], - slot = item.slot, - combinable = itemInfo["combinable"] - } - end + local result = exports['ghmattimysql']:executeSync('SELECT * FROM players WHERE citizenid=@citizenid', {['@citizenid'] = PlayerData.citizenid}) + if result[1] ~= nil then + if result[1].inventory ~= nil then + plyInventory = json.decode(result[1].inventory) + if next(plyInventory) ~= nil then + for _, item in pairs(plyInventory) do + if item ~= nil then + local itemInfo = QBCore.Shared.Items[item.name:lower()] + PlayerData.items[item.slot] = { + name = itemInfo["name"], + amount = item.amount, + info = item.info ~= nil and item.info or "", + label = itemInfo["label"], + description = itemInfo["description"] ~= nil and itemInfo["description"] or "", + weight = itemInfo["weight"], + type = itemInfo["type"], + unique = itemInfo["unique"], + useable = itemInfo["useable"], + image = itemInfo["image"], + shouldClose = itemInfo["shouldClose"], + slot = item.slot, + combinable = itemInfo["combinable"] + } end end end end - end) + end return PlayerData end @@ -578,11 +581,10 @@ QBCore.Player.CreateCitizenId = function() while not UniqueFound do CitizenId = tostring(QBCore.Shared.RandomStr(3) .. QBCore.Shared.RandomInt(5)):upper() - QBCore.Functions.ExecuteSql(true, "SELECT COUNT(*) as count FROM `players` WHERE `citizenid` = '"..CitizenId.."'", function(result) - if result[1].count == 0 then - UniqueFound = true - end - end) + local result = exports.ghmattimysql:executeSync('SELECT COUNT(*) as count FROM players WHERE citizenid=@citizenid', {['@citizenid'] = CitizenId}) + if result[1].count == 0 then + UniqueFound = true + end end return CitizenId end @@ -592,11 +594,11 @@ QBCore.Player.CreateFingerId = function() local FingerId = nil while not UniqueFound do FingerId = tostring(QBCore.Shared.RandomStr(2) .. QBCore.Shared.RandomInt(3) .. QBCore.Shared.RandomStr(1) .. QBCore.Shared.RandomInt(2) .. QBCore.Shared.RandomStr(3) .. QBCore.Shared.RandomInt(4)) - QBCore.Functions.ExecuteSql(true, "SELECT COUNT(*) as count FROM `players` WHERE `metadata` LIKE '%"..FingerId.."%'", function(result) - if result[1].count == 0 then - UniqueFound = true - end - end) + local query = '%'..FingerId..'%' + local result = exports.ghmattimysql:executeSync('SELECT COUNT(*) as count FROM `players` WHERE `metadata` LIKE @query', {['@query'] = query}) + if result[1].count == 0 then + UniqueFound = true + end end return FingerId end @@ -606,11 +608,11 @@ QBCore.Player.CreateWalletId = function() local WalletId = nil while not UniqueFound do WalletId = "QB-"..math.random(11111111, 99999999) - QBCore.Functions.ExecuteSql(true, "SELECT COUNT(*) as count FROM `players` WHERE `metadata` LIKE '%"..WalletId.."%'", function(result) - if result[1].count == 0 then - UniqueFound = true - end - end) + local query = '%'..WalletId..'%' + local result = exports.ghmattimysql:executeSync('SELECT COUNT(*) as count FROM players WHERE metadata LIKE @query', {['@query'] = query}) + if result[1].count == 0 then + UniqueFound = true + end end return WalletId end @@ -621,11 +623,11 @@ QBCore.Player.CreateSerialNumber = function() while not UniqueFound do SerialNumber = math.random(11111111, 99999999) - QBCore.Functions.ExecuteSql(true, "SELECT COUNT(*) as count FROM players WHERE metadata LIKE '%"..SerialNumber.."%'", function(result) - if result[1].count == 0 then - UniqueFound = true - end - end) + local query = '%'..SerialNumber..'%' + local result = exports.ghmattimysql:executeSync('SELECT COUNT(*) as count FROM players WHERE metadata LIKE @query', {['@query'] = query}) + if result[1].count == 0 then + UniqueFound = true + end end return SerialNumber end @@ -635,4 +637,4 @@ QBCore.EscapeSqli = function(str) return str:gsub( "['\"]", replacements ) -- or string.gsub( source, "['\"]", replacements ) end -PaycheckLoop() +PaycheckLoop() \ No newline at end of file