Advertisement
Guest User

Untitled

a guest
May 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. package ecstasy.util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7.  
  8. import ecstasy.rs2.model.Client;
  9.  
  10.  
  11. /**
  12. * MySQL Class
  13. *
  14. * @author 'Ecstasy?!
  15. */
  16.  
  17. public class MysqlManager {
  18. /**
  19. * Variables
  20. */
  21. public int userGroup;
  22. public int unreadMessages;
  23.  
  24. /**
  25. * Instances
  26. */
  27. public static Connection connection;
  28. public static Client client;
  29. public MysqlManager(Client client) {
  30. this.client = client;
  31. }
  32.  
  33. /**
  34. * Constant variables
  35. */
  36. public static final String URL = "jdbc:mysql://localhost";
  37. public static final String DATABASE = "forum";
  38. public static final String USER = "root";
  39. public static final String PASSWORD = "";
  40.  
  41. public static final int ADMIN = 6;
  42. public static final int MOD = 7;
  43. public static final int REGULAR_PLAYER = 2;
  44. public static final int GLOBAL_MOD = 5;
  45. public static final int BANNED_PLAYER = 8;
  46.  
  47. /**
  48. * @return the database
  49. */
  50. public static String getDatabase() {
  51. return DATABASE;
  52. }
  53.  
  54. /**
  55. * @return the URL of the database
  56. */
  57. public static String getUrl() {
  58. return URL;
  59. }
  60.  
  61. /**
  62. * @return the username to connect to the database with
  63. */
  64. public static String getUser() {
  65. return USER;
  66. }
  67.  
  68. /**
  69. * @return the password to connect to the database with
  70. */
  71. public static String getPassword() {
  72. return PASSWORD;
  73. }
  74.  
  75. /**
  76. * This basically puts everything together, just to keep it organized.
  77. *
  78. * @return the entire url to connect to the database
  79. */
  80. public static String URL() {
  81. return getUrl() + "/" + getDatabase();
  82. }
  83.  
  84. /**
  85. * Creates a connection to the database.
  86. */
  87. public static void createConnection() {
  88. try {
  89. Class.forName("com.mysql.jdbc.Driver").newInstance();
  90. connection = DriverManager.getConnection(URL(), getUser(),
  91. getPassword());
  92. Misc.println_debug("Sucessfully connceted to the database.");
  93. } catch (Exception exception) {
  94. exception.printStackTrace();
  95. }
  96. }
  97.  
  98. /**
  99. * @param name
  100. * the players username
  101. * @param password
  102. * the players password.
  103. * @return true or false, if the name & password is registered on the
  104. * forums.
  105. */
  106. public boolean loadAccount(String name, String password) {
  107. try {
  108. Statement statement = connection.createStatement();
  109. ResultSet group = statement
  110. .executeQuery("SELECT * FROM user WHERE username = '"
  111. + name + "'");
  112. while (group.next()) {
  113. String pass = group.getString("password");
  114. String salt = group.getString("salt");
  115. userGroup = group.getInt("usergroupid");
  116. unreadMessages = group.getInt("pmunread");
  117. String encryptedPassword = passHash(password, salt);
  118. if (pass.equalsIgnoreCase(encryptedPassword)) {
  119. statement.close();
  120. return true;
  121. } else {
  122. statement.close();
  123. return false;
  124. }
  125. }
  126. } catch (Exception e) {
  127. return false;
  128. }
  129. return false;
  130. }
  131.  
  132. /**
  133. * @param name
  134. * The players username This method will load all additional
  135. * info, other than the username and password.
  136. * @Author Ecstasy
  137. */
  138. public void loadInfo(String name) {
  139. try {
  140. Statement statement = connection.createStatement();
  141. ResultSet group = statement
  142. .executeQuery("SELECT * FROM user WHERE username = '"
  143. + name + "'");
  144. while (group.next()) {
  145. userGroup = group.getInt("usergroupid");
  146. unreadMessages = group.getInt("pmunread");
  147. }
  148. } catch (Exception e) {
  149. System.out.println("Oh fuck");
  150. }
  151. }
  152.  
  153. /**
  154. * @param in
  155. * the password before it's encrypted
  156. * @param salt
  157. * the encryption of the password (In this case, the vBulletin
  158. * encryption
  159. * @return the encrypted password
  160. */
  161. public String passHash(String in, String salt) {
  162. String a = new MD5(in).compute();
  163. String b = new MD5(a + salt).compute();
  164. return b;
  165. }
  166.  
  167. public void loadPlayerRights() {
  168. switch(userGroup) {
  169. case ADMIN:
  170. client.playerRights = 3;
  171. return;
  172. case MOD:
  173. client.playerRights = 1;
  174. return;
  175. case REGULAR_PLAYER:
  176. client.playerRights = 0;
  177. return;
  178. case GLOBAL_MOD:
  179. client.playerRights = 1;
  180. return;
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement