Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. --[[
  2. Copyright (c) 2010 MTA: Paradise
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ]]
  17.  
  18. local allowRegistration = tonumber( get( 'allow_registration' ) ) == 1 and true or false
  19. local registrationErrorMessage = get( 'registration_error_message' )
  20. if registrationErrorMessage then
  21. -- fix for newlines in message
  22. registrationErrorMessage = registrationErrorMessage:gsub( "\\n", "\n" )
  23. end
  24.  
  25. addEventHandler( "onResourceStart", resourceRoot,
  26. function( )
  27. setElementData( source, "allowRegistration", allowRegistration )
  28. setElementData( source, "registrationErrorMessage", registrationErrorMessage )
  29. end
  30. )
  31.  
  32. local function trim( str )
  33. return str:gsub("^%s*(.-)%s*$", "%1")
  34. end
  35.  
  36. addEvent( getResourceName( resource ) .. ":register", true )
  37. addEventHandler( getResourceName( resource ) .. ":register", root,
  38. function( username, password )
  39. if source == client then
  40. if allowRegistration then
  41. if username and password then
  42. username = trim( username )
  43. password = trim( password )
  44.  
  45. -- client length checks are the same
  46. if #username >= 3 and #password >= 8 then
  47. -- see if that username is free at all
  48. local info = exports.sql:query_assoc_single( "SELECT COUNT(userID) AS usercount FROM wcf1_user WHERE username = '%s'", username )
  49. if not info then
  50. triggerClientEvent( source, getResourceName( resource ) .. ":registrationResult", source, 1 )
  51. elseif info.usercount == 0 then
  52. -- generate a salt (SHA1)
  53. local salt = ''
  54. local chars = { 'a', 'b', 'c', 'd', 'e', 'f', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
  55. for i = 1, 40 do
  56. salt = salt .. chars[ math.random( 1, #chars ) ]
  57. end
  58.  
  59. -- create the user
  60. if exports.sql:query_free( "INSERT INTO wcf1_user (username,salt,password) VALUES ('%s', '%s', SHA1(CONCAT('%s', SHA1(CONCAT('%s', '" .. sha1( password ) .. "')))))", username, salt, salt, salt ) then
  61. triggerClientEvent( source, getResourceName( resource ) .. ":registrationResult", source, 0 ) -- will automatically login when this is sent
  62. else
  63. triggerClientEvent( source, getResourceName( resource ) .. ":registrationResult", source, 4 )
  64. end
  65. else
  66. triggerClientEvent( source, getResourceName( resource ) .. ":registrationResult", source, 3 )
  67. end
  68. else
  69. -- shouldn't happen
  70. triggerClientEvent( source, getResourceName( resource ) .. ":registrationResult", source, 1 )
  71. end
  72. else
  73. -- can't do much without a username and password
  74. triggerClientEvent( source, getResourceName( resource ) .. ":registrationResult", source, 1 )
  75. end
  76. else
  77. triggerClientEvent( source, getResourceName( resource ) .. ":registrationResult", source, 2, registrationErrorMessage )
  78. end
  79. end
  80. end
  81. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement