• Semi-Complex tmysql question
    3 replies, posted
Does this look right? [lua] function Player:StartEncryption( args ) if self.Register then // Following is based upon mybb code. All rights belong to the respective Owner(s). // I only modifed it so it would work in lua. // Create salt local salt = generate_salt() // Create new password based on salt local saltedpw = salt_password(args[1], salt) // Generate new login key local loginkey = generate_loginkey() // Update password and login key in database local newpassword = saltedpw; local newloginkey = loginkey; // End of modifed mybb code. Again, All rights belong to the respective Owner(s). // Start of mysql query, Were changing the password. tmysql.query("UPDATE `mybb_users` SET `password` = '" .. tmysql.escape(newpassword) .. "', `loginkey` = '" .. tmysql.escape(newloginkey) .. "', `salt` = '" .. tmysql.escape(salt) .. "' WHERE `uid` = '" .. self.pureid .. "' )" , SQLHandle ) self:ChatPrint( "[MyBB] Successfully encrypted your account." ) end end [/lua]
What are those functions? Is Player the Player metatable? Is pl.Register defined?
Its for mybb system Im working on. Here's the full code. It's going to be publicly released. Player is metatabled and pl.Register is defined. [lua] require("tmysql") require("cryptopeepee") Host = "localhost" // Host(IP/Hostname) User = "root" // Username Pass = "" // Password DB = "mybb" // Databse to select DBPrefix = "mybb_" // By default it's mybb_ but some users might have all there table prefixs different Port = 3306 // Port, By default it's 3306. Group = 2 // This is the default group they will be placed into. By default 2 is registered. Must be a number! tmysql.initialize( Host, User, Pass, DB, Port, 2, 2 ) local Player = _R.Player function SQLHandle( result, _, error ) if error and error != 0 then print( "SQL ERROR: " .. error ) end end // This creates a user as if he/she was registering on the forums. function Player:RegisterUser( uid, user, pass, email, group, regdate ) if !user or !pass or !email then self:ChatPrint( "[MyBB] Failed to create new user, one of the fields were nil." ) return end if !group then group = Group or 2 end if !uid then uid = self.pureid end if !regdate then regdate = os.time() end tmysql.query("INSERT INTO `mybb_users` (`uid`, `username`, `password`, `email`, `usergroup`, `allownotices`, `hideemail`, `invisible`, `receivepms`, `showsigs`, `showavatars`, `showquickreply`, `regdate`, `regip`, `lastip`) VALUES ('" .. tmysql.escape(uid) .. "', '" .. tmysql.escape(user) .. "', '" .. tmysql.escape(pass) .. "','" .. tmysql.escape(email) .. "', '".. group .. "', '" .. 1 .. "', '" .. 0 .. "', '" .. 0 .. "', '" .. 1 .. "', '" .. 1 .. "', '" .. 1 .. "', '" .. 1 .. "', '" .. regdate .. "', '" .. newip .. "', '" .. newip .. "' )" , SQLHandle ) self:ChatPrint( "[MyBB] Successfully registered user, " .. user ) self:StartEncryption( pass ) end // We can use this for a custom admin mod. Or use this to set admins. You could have your admins be controled via the forums. function Player:GetUsersGroup( id ) if !id then self:ChatPrint( "[MyBB] Failed to get user, got a nil value in argument." ) return end tmysql.query( "SELECT `usergroup` FROM `mybb_users` WHERE `uid`= '" .. self.pureid .. "'", function ( Args ) if (Args && Args[1]) then self:ChatPrint( "[MyBB] User is a part of group number " .. Args[1] ) else self:ChatPrint( "[MyBB] Failed to get user's group." ) end end) end // We use this to see if he/she is registered. function Player:GetUsersID( id ) if !id then self:ChatPrint( "[MyBB] Failed to get user, got a nil value in argument." ) return end tmysql.query( "SELECT `uid` FROM `mybb_users` WHERE `uid`= '" .. self.pureid .. "'", function ( Args ) if id != Args[1] then self.Register = false self.ID = Args[1] // Place some code here to make them register. (Console Command, Datastream, or User Message) elseif id == Args[1] then self.Register = true self.ID = Args[1] print( "[MyBB] Successfully got user. He/She is registered already." ) end end) end hook.Add("PlayerInitialSpawn", "FirstSpawn", function( pl ) local id = pl:SteamID() local newid = string.gsub( id, "STEAM_", "") self.pureid = string.gsub( newid, ":", "") pl.Register = false pl:GetUsersID( self.pureid ) end) concommand.Add("mybb_register", function( pl, cmd, args ) if !args or !args[2] then pl:ChatPrint( "[MyBB] Failed to create new user, one of the fields were nil." ) return end if pl.Register then pl:ChatPrint( "[MyBB] Your already registered." ) return end pl:RegisterUser(self.pureid, pl:Nick(), args[1], args[2]) end) // Following is based upon mybb code. All rights belong to the respective Owner(s). // I've only modifed it so it would work in lua. local strings = { "a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z","1","2","3","4","5","6","7","8","9" } function random_str( args ) local str = "" for i=1,args[1] do str = str .. table.Random(#strings) end end function generate_salt( ) return random_str( 8 ) print("Generating salt" ) end function generate_loginkey( ) return random_str( 50 ) print("Generating loginkey" ) end function salt_password(password, salt) local newsalt = cryptopeepee.md5(salt) local newpass = cryptopeepee.md5(password) return newpass .. newsalt print("Generating salted password" ) end // End of modifed mybb code. Again, All rights belong to the respective Owner(s). function Player:StartEncryption( args ) if pl.Register then // Following is based upon mybb code. All rights belong to the respective Owner(s). // I've only modifed it so it would work in lua. // Create salt local salt = generate_salt() // Create new password based on salt local saltedpw = salt_password(args[1], salt) // Generate new login key local loginkey = generate_loginkey() // Update password and login key in database local newpassword = saltedpw local newloginkey = loginkey // End of modifed mybb code. Again, All rights belong to the respective Owner(s). // Start of mysql query, Were updating the password and adding loginkey and salt. tmysql.query("UPDATE `mybb_users` SET `password` = '" .. tmysql.escape(newpassword) .. "', `loginkey` = '" .. tmysql.escape(newloginkey) .. "', `salt` = '" .. tmysql.escape(salt) .. "' WHERE `uid` = '" .. self.pureid .. "' )" , SQLHandle ) self:ChatPrint( "[MyBB] Successfully encrypted your account." ) end end // MyBB is one of easiest, fastest, flexible forum software avaivible to users to date. Using these functions will allow you to create, update, and modify stuff on your forums with out even having players open a browser. // Coded by Clark 2011; All barrowed code belongs to respectives Owner(s). // Loginkey, salt, and salted password code snippets belong to MyBB Group / MyBB Team, Copyright 2002-2010 [/lua] [editline]10th March 2011[/editline] The question is weather Im doing the query right? I've never used UPDATE. [editline]10th March 2011[/editline] Just noticed the "full code" I've posted is from my desktop which is a bit outdated. But the original post has nothing updated. So the question still stands.
Yea it looks alright, try it and if it doesn't work let us know.
Sorry, you need to Log In to post a reply to this thread.