daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest May 31st, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.stackit.dtb;
  2.  
  3. import java.math.BigInteger;
  4. import java.security.SecureRandom;
  5. import java.sql.DatabaseMetaData;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.text.DateFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.HashMap;
  15. import org.apache.commons.codec.digest.DigestUtils;
  16. import org.stackit.config.StackItConfiguration;
  17. import org.stackit.src.Language;
  18. import org.stackit.src.Logger;
  19.  
  20. public class MySQLClient implements DatabaseType {
  21.     public java.sql.Connection mysql = null;
  22.    
  23.     @Override
  24.     public Object getDatabase() {
  25.         return this.mysql;
  26.     }
  27.  
  28.     @Override
  29.     public Object Connect() {
  30.         try {
  31.             mysql = DriverManager.getConnection("jdbc:mysql://" + StackItConfiguration.getDatabaseHost() + ":" + StackItConfiguration.getDatabasePort() + "/" +
  32.                     StackItConfiguration.getDatabaseName() + "?" +
  33.                     "user=" + StackItConfiguration.getDatabaseUser() +
  34.                     "&password=" + StackItConfiguration.getDatabasePassword());
  35.            
  36.             Logger.info(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_connected")));
  37.             return mysql;
  38.         } catch(SQLException e) {
  39.             Logger.critical(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_error_accessing")));
  40.             Logger.critical(e.getMessage());
  41.             return null;
  42.         }
  43.     }
  44.    
  45.     @Override
  46.     public void CheckDatabase() {
  47.         DatabaseMetaData dbm = null;
  48.        
  49.         try {
  50.             dbm = mysql.getMetaData();
  51.         } catch (SQLException e) {
  52.             e.printStackTrace();
  53.         }
  54.        
  55.         // API Users table check
  56.         try {
  57.             ResultSet table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "api_users", null);
  58.            
  59.             if(table.next()) { // If table exist
  60.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_users"));
  61.             } else { // Table does not exist, let's create it.
  62.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_users"));
  63.                 Statement req = mysql.createStatement();
  64.                
  65.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users` ( `row` INT NOT NULL AUTO_INCREMENT , `id` INT NOT NULL , `name` TEXT NOT NULL , `pass` TEXT NOT NULL , `role` INT NOT NULL , `enabled` BOOLEAN NOT NULL , PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  66.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_users"));
  67.             }
  68.            
  69.             table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "api_tokens", null);
  70.                
  71.             if(table.next()) { // If table exist
  72.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_tokens"));
  73.             } else { // Table does not exist, let's create it.
  74.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_tokens"));
  75.                 Statement req = mysql.createStatement();
  76.  
  77.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_tokens` ( `row` INT NOT NULL AUTO_INCREMENT , `id` INT NOT NULL , `token` TEXT NOT NULL , `user` TEXT NOT NULL , `expire` TEXT NOT NULL , `enabled` BOOLEAN NOT NULL , PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  78.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_tokens"));
  79.             }
  80.            
  81.             table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "api_roles", null);
  82.                
  83.             if(table.next()) { // If table exist
  84.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_roles"));
  85.             } else { // Table does not exist, let's create it.
  86.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_roles"));
  87.                 Statement req = mysql.createStatement();
  88.                
  89.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_roles` ( `row` INT NOT NULL AUTO_INCREMENT , `id` INT NOT NULL , `permissions` TEXT NOT NULL , `enabled` BOOLEAN NOT NULL , PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  90.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_roles"));
  91.             }
  92.            
  93.             table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms", null);
  94.                
  95.             if(table.next()) { // If table exist
  96.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms"));
  97.             } else { // Table does not exist, let's create it.
  98.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms"));
  99.                 Statement req = mysql.createStatement();
  100.                
  101.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms` ( `row` INT NOT NULL AUTO_INCREMENT , `id` INT NOT NULL , `context` TEXT NOT NULL , `required_permission` TEXT NOT NULL , `enabled` BOOLEAN NOT NULL , PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  102.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms"));
  103.             }
  104.            
  105.             table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "api_logs", null);
  106.                
  107.             if(table.next()) { // If table exist
  108.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_logs"));
  109.             } else { // Table does not exist, let's create it.
  110.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_logs"));
  111.                 Statement req = mysql.createStatement();
  112.                
  113.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_logs` ( `row` INT NOT NULL AUTO_INCREMENT , `id` INT NOT NULL , `date` TEXT NOT NULL , `time` TEXT NOT NULL , `log` TEXT NOT NULL , `executor` TEXT NOT NULL , `context` TEXT NOT NULL , `remote` TEXT NOT NULL , PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  114.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "api_logs"));
  115.             }
  116.            
  117.             table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "buys_history", null);
  118.                
  119.             if(table.next()) { // If table exist
  120.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys_history"));
  121.             } else { // Table does not exist, let's create it.
  122.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys_history"));
  123.                 Statement req = mysql.createStatement();
  124.                
  125.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_history` ( `row` INT NOT NULL AUTO_INCREMENT , `id` TEXT NOT NULL , `buy` TEXT NOT NULL , `buy_sid` TEXT NOT NULL , `quantity` INT NOT NULL , `buyer` TEXT NOT NULL , `message` TEXT NOT NULL , `buy_for` TEXT NOT NULL , `pay_id` TEXT NOT NULL , `type` TEXT NOT NULL , `buy_date` TEXT NOT NULL , `buy_time` TEXT NOT NULL , `args` TEXT NOT NULL , `gift` BOOLEAN NOT NULL, `history_date` TEXT NOT NULL , `history_time` TEXT NOT NULL , PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  126.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys_history"));
  127.             }
  128.            
  129.             table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "buys", null);
  130.                
  131.             if(table.next()) { // If table exist
  132.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys"));
  133.             } else { // Table does not exist, let's create it.
  134.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys"));
  135.                 Statement req = mysql.createStatement();
  136.                
  137.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` ( `row` INT NOT NULL AUTO_INCREMENT , `id` TEXT NOT NULL , `buy` TEXT NOT NULL , `buy_sid` TEXT NOT NULL , `quantity` INT NOT NULL , `buyer` TEXT NOT NULL , `buy_for` TEXT NOT NULL , `message` TEXT NOT NULL , `pay_id` TEXT NOT NULL , `type` TEXT NOT NULL , `buy_date` TEXT NOT NULL , `buy_time` TEXT NOT NULL , `args` TEXT NOT NULL , `gift` BOOLEAN NOT NULL, PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  138.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys"));
  139.             }
  140.            
  141.             table = dbm.getTables(null, null, StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting", null);
  142.                
  143.             if(table.next()) { // If table
  144.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting"));
  145.             } else { // Table does not exist, let's create it.
  146.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_doesnt_exist")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting"));
  147.                 Statement req = mysql.createStatement();
  148.                
  149.                 req.executeUpdate("CREATE TABLE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting` ( `row` INT NOT NULL AUTO_INCREMENT , `id` INT NOT NULL , `buy_id` TEXT NOT NULL , PRIMARY KEY (`row`)) ENGINE = InnoDB;");
  150.                 if(StackItConfiguration.isLogEnabled()) Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_table_created")), "TABLE", StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting"));
  151.             }
  152.         } catch (SQLException e) {
  153.             e.printStackTrace();
  154.         }
  155.     }
  156.    
  157.     @Override
  158.     public HashMap<String, Object> GetContext(String context) {
  159.         Statement req;
  160.         HashMap<String, Object> dtb_context = new HashMap<String, Object>();
  161.        
  162.         try {
  163.             req = mysql.createStatement();
  164.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms` WHERE context='" + context.toString().replace("'", "''") + "'");
  165.  
  166.             while(rs.next()) {
  167.                 for(Integer i = 1 ; i < rs.getMetaData().getColumnCount() ; i++) {
  168.                     dtb_context.put(rs.getMetaData().getColumnName(i), rs.getString(i));
  169.                 }
  170.             }
  171.            
  172.             return dtb_context;
  173.         } catch (SQLException e) {
  174.             e.printStackTrace();
  175.             return null;
  176.         }
  177.     }
  178.    
  179.     @Override
  180.     public HashMap<String, Object> GetRole(Integer role) {
  181.         Statement req;
  182.         HashMap<String, Object> dtb_role = new HashMap<String, Object>();
  183.        
  184.         try {
  185.             req = mysql.createStatement();
  186.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_roles` WHERE id='" + role.toString().replace("'", "''") + "'");
  187.  
  188.             while(rs.next()) {
  189.                 for(Integer i = 1 ; i < rs.getMetaData().getColumnCount() ; i++) {
  190.                     dtb_role.put(rs.getMetaData().getColumnName(i), rs.getString(i));
  191.                 }
  192.             }
  193.            
  194.             return dtb_role;
  195.         } catch (SQLException e) {
  196.             e.printStackTrace();
  197.             return null;
  198.         }
  199.     }
  200.    
  201.     @Override
  202.     public HashMap<String, Object> GetToken(String token) {
  203.         Statement req;
  204.         HashMap<String, Object> dtb_token = new HashMap<String, Object>();
  205.        
  206.         try {
  207.             req = mysql.createStatement();
  208.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_tokens` WHERE token='" + token.toString().replace("'", "''") + "'");
  209.  
  210.             while(rs.next()) {
  211.                 for(Integer i = 1 ; i < rs.getMetaData().getColumnCount() ; i++) {
  212.                     dtb_token.put(rs.getMetaData().getColumnName(i), rs.getString(i));
  213.                 }
  214.             }
  215.            
  216.             return dtb_token;
  217.         } catch (SQLException e) {
  218.             e.printStackTrace();
  219.             return null;
  220.         }
  221.     }
  222.    
  223.     @Override
  224.     public HashMap<String, Object> GetUser(String user) {
  225.         Statement req;
  226.         HashMap<String, Object> dtb_user = new HashMap<String, Object>();
  227.        
  228.         try {
  229.             req = mysql.createStatement();
  230.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users` WHERE name='" + user.toString().replace("'", "''") + "'");
  231.            
  232.             while(rs.next()) {
  233.                 for(Integer i = 1 ; i < rs.getMetaData().getColumnCount() ; i++) {
  234.                     if(!rs.getMetaData().getColumnLabel(i).equalsIgnoreCase("pass")) { // Security
  235.                         dtb_user.put(rs.getMetaData().getColumnName(i), rs.getString(i));
  236.                     }
  237.                 }
  238.             }
  239.            
  240.             return dtb_user;
  241.         } catch (SQLException e) {
  242.             e.printStackTrace();
  243.             return null;
  244.         }
  245.     }
  246.    
  247.     @Override
  248.     public HashMap<String, Object> GetPurchase(String purchaseId) {
  249.         Statement req;
  250.         HashMap<String, Object> purchase = new HashMap<String, Object>();
  251.        
  252.         try {
  253.             req = mysql.createStatement();
  254.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id='" + purchaseId.toString().replace("'", "''") + "'");
  255.            
  256.             while(rs.next()) {
  257.                 for(Integer i = 1 ; i <= rs.getMetaData().getColumnCount() ; i++) {
  258.                     purchase.put(rs.getMetaData().getColumnName(i), rs.getString(i));
  259.                 }
  260.             }
  261.            
  262.             return purchase;
  263.         } catch (SQLException e) {
  264.             e.printStackTrace();
  265.             return null;
  266.         }
  267.     }
  268.    
  269.     @Override
  270.     public String AuthUser(String user, String pass) {
  271.         Statement req;
  272.        
  273.         try {
  274.             req = mysql.createStatement();
  275.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users` WHERE name='" + user.toString().replace("'", "''") + "'");
  276.             rs.next();
  277.            
  278.             if(UserExist(user)) {
  279.                 if(rs.getString("pass").equalsIgnoreCase(DigestUtils.sha1Hex(DigestUtils.md5Hex(pass)))) {
  280.                     if(rs.getBoolean("enabled") == true) {
  281.                         return "success";
  282.                     } else {
  283.                         return "account_not_enabled";
  284.                     }
  285.                 } else {
  286.                     return "invalid_password";
  287.                 }
  288.             } else {
  289.                 return "unknown_user";
  290.             }
  291.         } catch (SQLException e) {
  292.             e.printStackTrace();
  293.             return "internal_server_error";
  294.         }
  295.     }
  296.    
  297.     @Override
  298.     public String AuthToken(String token) {
  299.         Statement req;
  300.        
  301.         try {
  302.             req = mysql.createStatement();
  303.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_tokens` WHERE token='" + token.toString().replace("'", "''") + "'");
  304.             rs.next();
  305.            
  306.             if(TokenExist(token)) {
  307.                 // If time hasn't expired
  308.                 if(Calendar.getInstance().getTimeInMillis() < Long.parseLong(rs.getString("expire"))) {
  309.                     if(rs.getBoolean("enabled") == true) {
  310.                         return "success";
  311.                     } else {
  312.                         return "token_disabled";
  313.                     }
  314.                 } else {
  315.                     return "token_expired";
  316.                 }
  317.             } else {
  318.                 return "unknown_token";
  319.             }
  320.         } catch (SQLException e) {
  321.             e.printStackTrace();
  322.             return "internal_server_error";
  323.         }
  324.     }
  325.    
  326.     @Override
  327.     public void AddLog(String date, String time, String log, String executor, String context, String remoteAddress) {
  328.         try {
  329.             Statement req = mysql.createStatement();
  330.            
  331.             ResultSet id = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_logs`");
  332.             id.next();
  333.            
  334.             req.executeUpdate("INSERT INTO `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_logs` VALUES (0, " + id.getInt(1) + ", '" + date.toString().replace("'", "''") + "', '" + time.toString().replace("'", "''") + "', '" + log.toString().replace("'", "''") + "', '" + executor.toString().replace("'", "''") + "', '" + context.toString().replace("'", "''") + "', '" + remoteAddress.toString().replace("'", "''") + "')");
  335.         } catch (SQLException e) {
  336.             e.printStackTrace();
  337.         }
  338.     }
  339.    
  340.     @Override
  341.     public void AddPurchaseToWaitingList(String user, String buy_id) {
  342.         try {
  343.             Statement req = mysql.createStatement();
  344.            
  345.             ResultSet id = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_logs`");
  346.             id.next();
  347.            
  348.             req.executeUpdate("INSERT INTO `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting` VALUES (0, " + id.getInt(1) + ", '" + buy_id.toString().replace("'", "''") + "')");
  349.         } catch (SQLException e) {
  350.             e.printStackTrace();
  351.         }
  352.     }
  353.    
  354.     @Override
  355.     public Boolean HasPurchaseInWaitingList(String user) {
  356.         Statement req, req2;
  357.        
  358.         try {
  359.             req = mysql.createStatement();
  360.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting`");
  361.  
  362.             while(rs.next()) {
  363.                 req2 = mysql.createStatement();
  364.                 ResultSet rs2 = req2.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id=\'" + rs.getString("buy_id").toString().replace("'", "''") + "\'");
  365.  
  366.                 while(rs2.next()) {
  367.                     if(rs2.getString("buy_for").equalsIgnoreCase(user)) {
  368.                         return true;
  369.                     }
  370.                 }
  371.             }
  372.         } catch (SQLException e) {
  373.             e.printStackTrace();
  374.         }
  375.        
  376.         return false;
  377.     }
  378.    
  379.     @Override
  380.     public Boolean HasPurchaseMessage(String purchaseId) {
  381.         Statement req;
  382.        
  383.         try {
  384.             req = mysql.createStatement();
  385.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id='" + purchaseId.toString().replace("'", "''") + "'");
  386.             rs.next();
  387.            
  388.             if(rs.getString("message").replaceAll(" ", "") != "") {
  389.                 return true;
  390.             }
  391.         } catch (SQLException e) {
  392.             e.printStackTrace();
  393.         }
  394.        
  395.         return false;
  396.     }
  397.    
  398.     @Override
  399.     public Boolean IsAGift(String purchaseId) {
  400.         Statement req;
  401.        
  402.         try {
  403.             req = mysql.createStatement();
  404.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id='" + purchaseId.toString().replace("'", "''") + "'");
  405.             rs.next();
  406.            
  407.             if(rs.getBoolean("gift")) {
  408.                 return true;
  409.             }
  410.         } catch (SQLException e) {
  411.             e.printStackTrace();
  412.         }
  413.        
  414.         return false;
  415.     }
  416.    
  417.     @Override
  418.     public String GetPurchaseMessage(String purchaseId) {
  419.         Statement req;
  420.        
  421.         try {
  422.             req = mysql.createStatement();
  423.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id='" + purchaseId.toString().replace("'", "''") + "'");
  424.             rs.next();
  425.            
  426.             return rs.getString("message");
  427.         } catch (SQLException e) {
  428.             e.printStackTrace();
  429.             return null;
  430.         }
  431.     }
  432.    
  433.     @Override
  434.     public Boolean IsPurchaseInWaitingList(String purchaseId) {
  435.         Statement req;
  436.        
  437.         try {
  438.             req = mysql.createStatement();
  439.             ResultSet rs = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting` WHERE buy_id='" + purchaseId.toString().replace("'", "''") + "'");
  440.             rs.next();
  441.             int count = rs.getInt(1);
  442.            
  443.             if(count >= 1) {
  444.                 return true;
  445.             }
  446.         } catch (SQLException e) {
  447.             e.printStackTrace();
  448.         }
  449.        
  450.         return false;
  451.     }
  452.    
  453.     @Override
  454.     public HashMap<Integer, String> GetPurchasesInWaitingList(String user, String uuid) {
  455.         Statement req, req2;
  456.         HashMap<Integer, String> purchases = new HashMap<Integer, String>();
  457.        
  458.         try {
  459.             req = mysql.createStatement();
  460.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting`");
  461.  
  462.             while(rs.next()) {
  463.                 req2 = mysql.createStatement();
  464.                 ResultSet rs2 = req2.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id=\'" + rs.getString("buy_id").toString().replace("'", "''") + "\'");
  465.  
  466.                 while(rs2.next()) {
  467.                     if(rs2.getString("buy_for").equalsIgnoreCase(user) || rs2.getString("buy_for").equalsIgnoreCase(uuid)) {
  468.                         purchases.put(purchases.size(), rs.getString("buy_id"));
  469.                     }
  470.                 }
  471.             }
  472.            
  473.             return purchases;
  474.         } catch (SQLException e) {
  475.             e.printStackTrace();
  476.             return null;
  477.         }
  478.     }
  479.    
  480.     @Override
  481.     public HashMap<String, Object> GetPurchaseArguments(String purchaseId) {
  482.         Statement req;
  483.         HashMap<String, Object> args = new HashMap<String, Object>();
  484.        
  485.         try {
  486.             req = mysql.createStatement();
  487.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id='" + purchaseId.toString().replace("'", "''") + "'");
  488.            
  489.             if(rs.next()) {
  490.                 String argsString = rs.getString("args");
  491.                 String[] splitted1 = argsString.split("&");
  492.                
  493.                 for(Integer i = 0 ; i < splitted1.length ; i++) {
  494.                     String[] splitted2 = splitted1[i].split("=");
  495.                     args.put(splitted2[0], splitted2[1]);
  496.                 }
  497.             }
  498.            
  499.             return args;
  500.         } catch (SQLException e) {
  501.             e.printStackTrace();
  502.             return null;
  503.         }
  504.     }
  505.    
  506.     @Override
  507.     public void RemovePurchase(String purchaseId) {
  508.         Statement req, req2, req3;
  509.         HashMap<String, Object> purchase = GetPurchase(purchaseId);
  510.        
  511.         try {
  512.             req = mysql.createStatement();
  513.             req.executeUpdate("DELETE FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys` WHERE id='" + purchaseId.toString().replace("'", "''") + "'");
  514.  
  515.             req2 = mysql.createStatement();
  516.             req2.executeUpdate("DELETE FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_waiting` WHERE buy_id='" + purchaseId.toString().replace("'", "''") + "'");
  517.  
  518.             Date date = new Date();
  519.            
  520.             DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
  521.             DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
  522.            
  523.             String dateOutput = dateFormat.format(date);
  524.             String timeOutput = timeFormat.format(date);
  525.            
  526.             req3 = mysql.createStatement();
  527.             req3.executeUpdate("INSERT INTO `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "buys_history` VALUES (0, '" + purchase.get("id") + "', '" + purchase.get("buy").toString().replace("'", "''") + "', '" + purchase.get("buy_sid").toString().replace("'", "''") + "', " + purchase.get("quantity").toString().replace("'", "''") + ", '" + purchase.get("buyer").toString().replace("'", "''") + "', '" + purchase.get("message").toString().replace("'", "''") + "', '" + purchase.get("buy_for").toString().replace("'", "''") + "', '" + purchase.get("pay_id").toString().replace("'", "''") + "', '" + purchase.get("type").toString().replace("'", "''") + "', '" + purchase.get("buy_date").toString().replace("'", "''") + "', '" + purchase.get("buy_time").toString().replace("'", "''") + "', '" + purchase.get("args").toString().replace("'", "''") + "', " + purchase.get("gift").toString().replace("'", "''") + ", '" + dateOutput.toString().replace("'", "''") + "', '" + timeOutput.toString().replace("'", "''") + "')");
  528.         } catch (SQLException e) {
  529.             e.printStackTrace();
  530.         }
  531.     }
  532.    
  533.     @Override
  534.     public Boolean ContextExist(String context) {
  535.         Statement req;
  536.        
  537.         try {
  538.             req = mysql.createStatement();
  539.             ResultSet rs = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms` WHERE context='" + context.toString().replace("'", "''") + "'");
  540.             rs.next();
  541.             int count = rs.getInt(1);
  542.            
  543.             if(count >= 1) {
  544.                 return true;
  545.             } else {
  546.                 return false;
  547.             }
  548.         } catch (SQLException e) {
  549.             e.printStackTrace();
  550.             return false;
  551.         }
  552.     }
  553.    
  554.     @Override
  555.     public Boolean UserExist(String user) {
  556.         Statement req;
  557.        
  558.         try {
  559.             req = mysql.createStatement();
  560.             ResultSet rs = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users` WHERE name='" + user.toString().replace("'", "''") + "'");
  561.             rs.next();
  562.             int count = rs.getInt(1);
  563.            
  564.             if(count >= 1) {
  565.                 return true;
  566.             } else {
  567.                 return false;
  568.             }
  569.         } catch (SQLException e) {
  570.             e.printStackTrace();
  571.             return false;
  572.         }
  573.     }
  574.    
  575.     @Override
  576.     public String GenerateToken(String user) {
  577.         String token = new BigInteger(130, new SecureRandom()).toString(32);
  578.        
  579.         while(TokenExist(token)) {
  580.             token = new BigInteger(130, new SecureRandom()).toString(32);
  581.         }
  582.  
  583.         try {
  584.             Statement req = mysql.createStatement();
  585.            
  586.             ResultSet id = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_tokens`");
  587.             id.next();
  588.            
  589.             Calendar expiration = Calendar.getInstance();
  590.             expiration.add(Calendar.MINUTE, 30);
  591.            
  592.             req.executeUpdate("INSERT INTO `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_tokens` VALUES (0, " + id.getInt(1) + ", '" + token.toString().replace("'", "''") + "', '" + user.toString().replace("'", "''") + "', '" + expiration.getTimeInMillis() + "', 1)");
  593.             return token;
  594.         } catch (SQLException e) {
  595.             e.printStackTrace();
  596.             return null;
  597.         }
  598.     }
  599.    
  600.     @Override
  601.     public String GetContextRequiredPermission(String context) {
  602.         HashMap<String, Object> dtb_context = GetContext(context);
  603.        
  604.         if(dtb_context.size() > 0) {
  605.             return dtb_context.get("required_permission").toString();
  606.         }
  607.        
  608.         return null;
  609.     }
  610.    
  611.     @Override
  612.     public String GetRolePermissions(Integer role) {
  613.         HashMap<String, Object> dtb_role = GetRole(role);
  614.        
  615.         if(dtb_role.size() > 0) {
  616.             return dtb_role.get("permissions").toString();
  617.         }
  618.        
  619.         return null;
  620.     }
  621.    
  622.     @Override
  623.     public Boolean TokenExist(String token) {
  624.         Statement req;
  625.         try {
  626.             req = mysql.createStatement();
  627.             ResultSet rs = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_tokens` WHERE token='" + token.toString().replace("'", "''") + "'");
  628.             rs.next();
  629.             int count = rs.getInt(1);
  630.            
  631.             if(count >= 1) {
  632.                 return true;
  633.             } else {
  634.                 return false;
  635.             }
  636.         } catch (SQLException e) {
  637.             e.printStackTrace();
  638.             return false;
  639.         }
  640.     }
  641.    
  642.     @Override
  643.     public String GetTokenUser(String token) {
  644.         HashMap<String, Object> dtb_token = GetToken(token);
  645.        
  646.         if(dtb_token.size() > 0) {
  647.             return dtb_token.get("user").toString();
  648.         }
  649.        
  650.         return null;
  651.     }
  652.    
  653.     @Override
  654.     public String GetTokenPermissions(String token) {
  655.         HashMap<String, Object> dtb_token = Database.getActiveDatabase().GetToken(token);
  656.        
  657.         if(dtb_token.size() > 0) {
  658.             HashMap<String, Object> dtb_user = Database.getActiveDatabase().GetUser(dtb_token.get("user").toString());
  659.            
  660.             if(dtb_user.size() > 0) {
  661.                 return Database.getActiveDatabase().GetRolePermissions(Integer.parseInt(dtb_user.get("role").toString()));
  662.             }
  663.         }
  664.        
  665.         return null;
  666.     }
  667.    
  668.     @Override
  669.     public void CreateContext(String context, String permission) {
  670.         if(!ContextExist(context)) {
  671.             try {
  672.                 Statement req = mysql.createStatement();
  673.                
  674.                 ResultSet id = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms`");
  675.                 id.next();
  676.                
  677.                 req.executeUpdate("INSERT INTO `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_contexts_perms` VALUES (0, " + id.getInt(1) + ", '" + context.toString().replace("'", "''") + "', '" + permission.toString().replace("'", "''") + "', 1)");
  678.             } catch (SQLException e) {
  679.                 e.printStackTrace();
  680.             }
  681.  
  682.             Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_create_context")), "CONTEXT", context));
  683.         } else {
  684.             Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_create_context")), "CONTEXT", context));
  685.         }
  686.     }
  687.    
  688.     @Override
  689.     public String CreateUser(String user, String pass) {
  690.         if(!UserExist(user)) {
  691.             try {
  692.                 Statement req = mysql.createStatement();
  693.                
  694.                 ResultSet id = req.executeQuery("SELECT COUNT(*) FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users`");
  695.                 id.next();
  696.                
  697.                 req.executeUpdate("INSERT INTO `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users` VALUES (0, " + id.getInt(1) + ", '" + user + "', '" + DigestUtils.sha1Hex(DigestUtils.md5Hex(pass)) + "', 1, 1)");
  698.             } catch (SQLException e) {
  699.                 e.printStackTrace();
  700.                 return null;
  701.             }
  702.            
  703.             Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_user_created")), "USER", user));
  704.             return "success";
  705.         } else {
  706.             Logger.info(Language.replace(Language.process(Language.get(Language.getBukkitLanguage(), "dtb_user_already_exist")), "USER", user));
  707.             return "already_exist";
  708.         }
  709.     }
  710.    
  711.     @Override
  712.     public String DeleteUser(String user) {
  713.         if(UserExist(user)) {
  714.             Statement req;
  715.            
  716.             try {
  717.                 req = mysql.createStatement();
  718.                
  719.                 req.executeUpdate("DELETE FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users` WHERE name='" + user.toString().replace("'", "''") + "'");
  720.                 return "success";
  721.             } catch (SQLException e) {
  722.                 e.printStackTrace();
  723.                 return "null";
  724.             }
  725.         } else {
  726.             return "not_found";
  727.         }
  728.     }
  729.    
  730.     @Override
  731.     public String GetTokenExpiration(String token) {
  732.         Statement req;
  733.        
  734.         try {
  735.             req = mysql.createStatement();
  736.             ResultSet rs = req.executeQuery("SELECT * FROM `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_tokens` WHERE token='" + token.toString().replace("'", "''") + "'");
  737.             rs.next();
  738.            
  739.             return rs.getString("expire");
  740.         } catch (SQLException e) {
  741.             e.printStackTrace();
  742.             return null;
  743.         }
  744.     }
  745.    
  746.     @Override
  747.     public String GetUserPermissions(String user) {
  748.         HashMap<String, Object> dtb_user = GetUser(user);
  749.        
  750.         if(dtb_user.size() > 0) {
  751.             HashMap<String, Object> dtb_role = GetRole(Integer.parseInt(dtb_user.get("role").toString()));
  752.            
  753.             if(dtb_role.size() > 0) {
  754.                 return dtb_role.get("permissions").toString();
  755.             }
  756.         }
  757.        
  758.         return null;
  759.     }
  760.    
  761.     @Override
  762.     public void SetUserRole(String user, Integer role) {
  763.           try {
  764.               Statement req = mysql.createStatement();
  765.             req.executeUpdate("UPDATE `" + StackItConfiguration.getDatabaseName() + "`.`" + StackItConfiguration.getDatabaseTablePrefix() + "api_users` SET role=" + role + " WHERE name='" + user.toString().replace("'", "''") + "'");
  766.         } catch (SQLException e) {
  767.             e.printStackTrace();
  768.         }
  769.     }
  770.    
  771.     @Override
  772.     public void Disconnect() {
  773.         try {
  774.             if(mysql != null) {
  775.                 mysql.close();
  776.             }
  777.         } catch (SQLException e) {
  778.             e.printStackTrace();
  779.         }
  780.     }
  781.  
  782. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top