Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.66 KB | None | 0 0
  1. --[[
  2.    
  3.     ULibSQL - by es5sujo (STEAM_0:0:38411376, spam_me@josu.se)
  4.    
  5.     A MySQL data provider for ULib (ver. 2.52+)
  6.     Mostly based on the ULib file 'server/ucl.lua'
  7.    
  8.     Feel free to edit, develop and redistribute as long as you keep it for free and keep this comment
  9.    
  10. ]]
  11.  
  12. if SERVER then
  13.     ulibsql = {}
  14.    
  15.     -- Should ulibsql be activated?
  16.     ulibsql.status = true
  17.    
  18.     -- Activate Transfer-mode? (Used for moving data into the DB the first time - then turn off)
  19.     ulibsql.transfer = false
  20.    
  21.     -- MySQL database settings
  22.     ulibsql.hostname = 'db2.myarena.ru'
  23.     ulibsql.username = 'dubr_Dubrovski'
  24.     ulibsql.password = '84954319917SQL'
  25.     ulibsql.database = 'dubr_premiumgaming'
  26.     ulibsql.port        = 3306
  27.    
  28.     -- Logging?
  29.     ulibsql.log = true
  30.    
  31.     --------------------------------------------------------
  32.     --                                                    --
  33.     -- Don't edit below unless you know what you're doing --
  34.     --                                                    --
  35.     --------------------------------------------------------
  36.    
  37.     local ucl = ULib.ucl
  38.     MsgN('[ULib-SQL] Module loaded!')
  39.    
  40.     require('mysqloo')
  41.    
  42.     local db = mysqloo.connect(ulibsql.hostname, ulibsql.username, ulibsql.password, ulibsql.database, ulibsql.port)
  43.    
  44.     function db:onConnected()
  45.         logPrint('[ULib-SQL] Connected!')
  46.         reloadGroups()
  47.         reloadUsers()
  48.     end
  49.    
  50.     function db:onConnectionFailed(err)
  51.         logPrint('[ULib-SQL] Connection failed: ' .. err)
  52.     end
  53.    
  54.     db:connect()
  55.    
  56.     if ulibsql.status or ulibsql.transfer then
  57.         function ucl.saveGroups()
  58.             local qs = [[INSERT INTO groups (`name`, `definition`) VALUES ('%s', '%s') ON DUPLICATE KEY UPDATE `definition` = VALUES(`definition`)]]
  59.            
  60.             for _, groupInfo in pairs(ucl.groups) do
  61.                 table.sort(groupInfo.allow)
  62.                
  63.                 local group_name = _
  64.                 local group_data = ULib.makeKeyValues(groupInfo)
  65.                
  66.                 local q = db:query(string.format(qs, group_name, group_data))
  67.                
  68.                 function q:onSuccess(data)
  69.                    
  70.                 end
  71.                
  72.                 function q:onError(err)
  73.                     logPrint('[Ulib-SQL] Query error: ' .. err)
  74.                 end
  75.                
  76.                 q:start()
  77.             end
  78.             logPrint('[Ulib-SQL] Saved groups!')
  79.             reloadGroups()
  80.         end
  81.        
  82.         function ucl.saveUsers()
  83.             local qs = [[INSERT INTO player_permissions (`steamid`, `content`, `group`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `content` = VALUES(`content`), `group` = VALUES(`group`)]]
  84.        
  85.             for _, userInfo in pairs(ucl.users) do
  86.                 table.sort(userInfo.allow)
  87.                 table.sort(userInfo.deny)
  88.                
  89.                 local player_id = _
  90.                 local player_data = ULib.makeKeyValues(userInfo)
  91.                 local player_group = userInfo.group
  92.                
  93.                 local q = db:query(string.format(qs, db:escape(player_id), db:escape(player_data), db:escape(player_group)))
  94.                
  95.                 function q:onSuccess(data)
  96.                    
  97.                 end
  98.                
  99.                 function q:onError(err)
  100.                     logPrint('[Ulib-SQL] Query error: ' .. err)
  101.                 end
  102.                
  103.                 q:start()
  104.             end
  105.             logPrint('[Ulib-SQL] Saved users!')
  106.             reloadUsers()
  107.         end
  108.        
  109.         concommand.Add('libsql_save_groups', ucl.saveGroups)
  110.         concommand.Add('libsql_save_users', ucl.saveUsers)
  111.     end
  112.  
  113.     if not ulibsql.transfer then
  114.         function reloadGroups()
  115.             local needsBackup = false
  116.             local err
  117.             local qs = "SELECT `name`, `definition` FROM groups"
  118.             local q = db:query(qs)
  119.            
  120.             ucl.groups = {}
  121.            
  122.             function q:onSuccess(data)
  123.                 if #data > 0 then
  124.                     for i = 1, #data do
  125.                         ucl.groups[data[i].name] = ULib.parseKeyValues(data[i].definition)
  126.                     end
  127.                     logPrint('[ULib-SQL] Loaded groups!')
  128.                 else
  129.                     logPrint('[ULib-SQL] Could not get groups from database...')
  130.                 end
  131.                
  132.                 if not ucl.groups or not ucl.groups[ ULib.ACCESS_ALL ] then
  133.                     needsBackup = true
  134.                     -- Totally messed up! Clear it.
  135.                     local f = "addons/ulib/" .. ULib.UCL_GROUPS
  136.                     if not ULib.fileExists( f ) then
  137.                         Msg( "ULIB PANIC: groups.txt is corrupted and I can't find the default groups.txt file!!\n" )
  138.                     else
  139.                         local err2
  140.                         ucl.groups, err2 = ULib.parseKeyValues( ULib.removeCommentHeader( ULib.fileRead( f ), "/" ) )
  141.                         if not ucl.groups or not ucl.groups[ ULib.ACCESS_ALL ] then
  142.                             Msg( "ULIB PANIC: default groups.txt is corrupt!\n" )
  143.                             err = err2
  144.                         end
  145.                     end
  146.                     if ULib.fileExists( ULib.UCL_REGISTERED ) then
  147.                         ULib.fileDelete( ULib.UCL_REGISTERED ) -- Since we're regnerating we'll need to remove this
  148.                     end
  149.                     accessStrings = {}
  150.  
  151.                 else
  152.                     -- Check to make sure it passes a basic validity test
  153.                     ucl.groups[ ULib.ACCESS_ALL ].inherit_from = nil -- Ensure this is the case
  154.                     for groupName, groupInfo in pairs( ucl.groups ) do
  155.                         if type( groupName ) ~= "string" then
  156.                             needsBackup = true
  157.                             ucl.groups[ groupName ] = nil
  158.                         else
  159.  
  160.                             if type( groupInfo ) ~= "table" then
  161.                                 needsBackup = true
  162.                                 groupInfo = {}
  163.                                 ucl.groups[ groupName ] = groupInfo
  164.                             end
  165.  
  166.                             if type( groupInfo.allow ) ~= "table" then
  167.                                 needsBackup = true
  168.                                 groupInfo.allow = {}
  169.                             end
  170.  
  171.                             local inherit_from = groupInfo.inherit_from
  172.                             if inherit_from and inherit_from ~= "" and not ucl.groups[ groupInfo.inherit_from ] then
  173.                                 needsBackup = true
  174.                                 groupInfo.inherit_from = nil
  175.                             end
  176.  
  177.                             -- Check for cycles
  178.                             local group = ucl.groupInheritsFrom( groupName )
  179.                             while group do
  180.                                 if group == groupName then
  181.                                     needsBackup = true
  182.                                     groupInfo.inherit_from = nil
  183.                                 end
  184.                                 group = ucl.groupInheritsFrom( group )
  185.                             end
  186.  
  187.                             if groupName ~= ULib.ACCESS_ALL and not groupInfo.inherit_from or groupInfo.inherit_from == "" then
  188.                                 groupInfo.inherit_from = ULib.ACCESS_ALL -- Clean :)
  189.                             end
  190.  
  191.                             -- Lower case'ify
  192.                             for k, v in pairs( groupInfo.allow ) do
  193.                                 if type( k ) == "string" and k:lower() ~= k then
  194.                                     groupInfo.allow[ k:lower() ] = v
  195.                                     groupInfo.allow[ k ] = nil
  196.                                 else
  197.                                     groupInfo.allow[ k ] = v
  198.                                 end
  199.                             end
  200.                         end
  201.                     end
  202.                 end
  203.  
  204.                 if needsBackup then
  205.                     Msg( "Groups file was not formatted correctly. Attempting to fix and backing up original\n" )
  206.                     if err then
  207.                         Msg( "Error while reading groups file was: " .. err .. "\n" )
  208.                     end
  209.                     Msg( "Original file was backed up to " .. ULib.backupFile( ULib.UCL_GROUPS ) .. "\n" )
  210.                     ucl.saveGroups()
  211.                 end
  212.             end
  213.            
  214.             function q:onError(err)
  215.                 logPrint('[Ulib-SQL] Query error: ' .. err)
  216.             end
  217.            
  218.             q:start()
  219.            
  220.         end
  221.  
  222.         function reloadUsers()
  223.             local qs = "SELECT `steamid`, `content` FROM player_permissions"
  224.             local q = db:query(qs)
  225.            
  226.             ucl.users = {}
  227.            
  228.             function q:onSuccess(data)
  229.                 if #data > 0 then
  230.                     for i = 1, #data do
  231.                         ucl.users[data[i].steamid] = ULib.parseKeyValues(data[i].content)
  232.                     end
  233.                     logPrint('[ULib-SQL] Loaded users!')
  234.                 else
  235.                     logPrint('[ULib-SQL] Could not get users from database...')
  236.                 end
  237.                 -- Check to make sure it passes a basic validity test
  238.                 if not ucl.users then
  239.                     needsBackup = true
  240.                     -- Totally messed up! Clear it.
  241.                     local f = "addons/ulib/" .. ULib.UCL_USERS
  242.                     if not ULib.fileExists( f ) then
  243.                         Msg( "ULIB PANIC: users.txt is corrupted and I can't find the default users.txt file!!\n" )
  244.                     else
  245.                         local err2
  246.                         ucl.users, err2 = ULib.parseKeyValues( ULib.removeCommentHeader( ULib.fileRead( f ), "/" ) )
  247.                         if not ucl.users then
  248.                             Msg( "ULIB PANIC: default users.txt is corrupt!\n" )
  249.                             err = err2
  250.                         end
  251.                     end
  252.                     if ULib.fileExists( ULib.UCL_REGISTERED ) then
  253.                         ULib.fileDelete( ULib.UCL_REGISTERED ) -- Since we're regnerating we'll need to remove this
  254.                     end
  255.                     accessStrings = {}
  256.  
  257.                 else
  258.                     for id, userInfo in pairs( ucl.users ) do
  259.                         if type( id ) ~= "string" then
  260.                             needsBackup = true
  261.                             ucl.users[ id ] = nil
  262.                         else
  263.  
  264.                             if type( userInfo ) ~= "table" then
  265.                                 needsBackup = true
  266.                                 userInfo = {}
  267.                                 ucl.users[ id ] = userInfo
  268.                             end
  269.  
  270.                             if type( userInfo.allow ) ~= "table" then
  271.                                 needsBackup = true
  272.                                 userInfo.allow = {}
  273.                             end
  274.  
  275.                             if type( userInfo.deny ) ~= "table" then
  276.                                 needsBackup = true
  277.                                 userInfo.deny = {}
  278.                             end
  279.  
  280.                             if userInfo.group and type( userInfo.group ) ~= "string" then
  281.                                 needsBackup = true
  282.                                 userInfo.group = nil
  283.                             end
  284.  
  285.                             if userInfo.name and type( userInfo.name ) ~= "string" then
  286.                                 needsBackup = true
  287.                                 userInfo.name = nil
  288.                             end
  289.  
  290.                             if userInfo.group == "" then userInfo.group = nil end -- Clean :)
  291.  
  292.                             -- Lower case'ify
  293.                             for k, v in pairs( userInfo.allow ) do
  294.                                 if type( k ) == "string" and k:lower() ~= k then
  295.                                     userInfo.allow[ k:lower() ] = v
  296.                                     userInfo.allow[ k ] = nil
  297.                                 else
  298.                                     userInfo.allow[ k ] = v
  299.                                 end
  300.                             end
  301.  
  302.                             for k, v in ipairs( userInfo.deny ) do
  303.                                 if type( k ) == "string" and type( v ) == "string" then -- This isn't allowed here
  304.                                     table.insert( userInfo.deny, k )
  305.                                     userInfo.deny[ k ] = nil
  306.                                 else
  307.                                     userInfo.deny[ k ] = v
  308.                                 end
  309.                             end
  310.                         end
  311.                     end
  312.                 end
  313.  
  314.                 if needsBackup then
  315.                     Msg( "Users file was not formatted correctly. Attempting to fix and backing up original\n" )
  316.                     if err then
  317.                         Msg( "Error while reading groups file was: " .. err .. "\n" )
  318.                     end
  319.                     Msg( "Original file was backed up to " .. ULib.backupFile( ULib.UCL_USERS ) .. "\n" )
  320.                     ucl.saveUsers()
  321.                 end
  322.             end
  323.            
  324.             function q:onError(err)
  325.                 logPrint('[Ulib-SQL] Query error: ' .. err)
  326.             end
  327.            
  328.             q:start()
  329.         end
  330.        
  331.         concommand.Add('libsql_reload_groups', reloadGroups)
  332.         concommand.Add('libsql_reload_users', reloadUsers)
  333.     end
  334.    
  335.     function logPrint(message)
  336.         if ulibsql.log then
  337.             MsgN(message)
  338.         end
  339.     end
  340. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement