Advertisement
Jnk1296

Database Controller

Sep 6th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 41.05 KB | None | 0 0
  1. /*
  2.  * Copyright © 2014 Jacob Keep (Jnk1296). All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  *  * Redistributions of source code must retain the above copyright notice,
  8.  *   this list of conditions and the following disclaimer.
  9.  *
  10.  *  * Redistributions in binary form must reproduce the above copyright notice,
  11.  *   this list of conditions and the following disclaimer in the documentation
  12.  *   and/or other materials provided with the distribution.
  13.  *
  14.  *  * Neither the name of JuNK Software nor the names of its contributors may
  15.  *   be used to endorse or promote products derived from this software without
  16.  *   specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. package net.risenphoenix.ipcheck.database;
  32.  
  33. import net.risenphoenix.commons.Plugin;
  34. import net.risenphoenix.commons.database.DatabaseManager;
  35. import net.risenphoenix.commons.database.QueryFilter;
  36. import net.risenphoenix.commons.database.StatementObject;
  37. import net.risenphoenix.ipcheck.objects.IPObject;
  38. import net.risenphoenix.ipcheck.objects.UserObject;
  39.  
  40. import java.sql.ResultSet;
  41. import java.sql.SQLException;
  42. import java.util.ArrayList;
  43.  
  44. public class DatabaseController extends DatabaseManager {
  45.  
  46.     // SQ-Lite Initializer
  47.     public DatabaseController(final Plugin plugin) {
  48.         super(plugin, "ip-check");
  49.  
  50.         // Enable Debugging to allow us to view the dynamic SQL queries
  51.         //this.enableDebug(true);
  52.         this.dropTables(); // Attempt Table Drop
  53.         this.initializeSQLiteTables();
  54.     }
  55.  
  56.     // MySQL Initializer
  57.     public DatabaseController(final Plugin plugin, String hostname, int port,
  58.                               String database, String username, String pwd
  59.                               /* int poolSize */) {
  60.         super(plugin, hostname, port, database, username, pwd/*, poolSize */);
  61.  
  62.  
  63.         // Enable Debugging to allow us to view the dynamic SQL queries
  64.         //this.enableDebug(true);
  65.         this.dropTables(); // Attempt Table Drop
  66.         this.initializeMySQLTables(); // Initialize Tables
  67.     }
  68.  
  69.     private void dropTables() {
  70.         if (!getPlugin().getConfigurationManager().getBoolean("dbGenerated")) {
  71.             // SQL Strings
  72.             String SQL_0 = "DROP TABLE IF EXISTS ipcheck_log;";
  73.             String SQL_1 = "DROP TABLE IF EXISTS ipcheck_user;";
  74.             String SQL_2 = "DROP TABLE IF EXISTS ipcheck_ip;";
  75.  
  76.             // Execute SQL
  77.             executeStatement(new StatementObject(getPlugin(), SQL_0));
  78.             executeStatement(new StatementObject(getPlugin(), SQL_1));
  79.             executeStatement(new StatementObject(getPlugin(), SQL_2));
  80.  
  81.             // Save Configuration Option
  82.             getPlugin().getConfigurationManager()
  83.                     .setConfigurationOption("dbGenerated", true);
  84.         }
  85.     }
  86.  
  87.     // Initialize Tables for SQ-Lite
  88.     public void initializeSQLiteTables() {
  89.         String TABLE_IPC_LOG = "CREATE TABLE IF NOT EXISTS ipcheck_log ( " +
  90.                 "ip TEXT," +
  91.                 "username TEXT," +
  92.                 "timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
  93.                 "PRIMARY KEY(username,ip));";
  94.  
  95.         String TABLE_IPC_USER = "CREATE TABLE IF NOT EXISTS ipcheck_user ( " +
  96.                 "username TEXT," +
  97.                 "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP," +
  98.                 "banmessage TEXT," +
  99.                 "banned INTEGER DEFAULT 0," +
  100.                 "exempted INTEGER DEFAULT 0," +
  101.                 "rejoinexempt INTEGER DEFAULT 0," +
  102.                 "protected INTEGER DEFAULT 0," +
  103.                 "PRIMARY KEY(username));";
  104.  
  105.         String TABLE_IPC_IP = "CREATE TABLE IF NOT EXISTS ipcheck_ip ( " +
  106.                 "ip TEXT," +
  107.                 "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP," +
  108.                 "banned INTEGER DEFAULT 0," +
  109.                 "exempted INTEGER DEFAULT 0," +
  110.                 "rejoinexempt INTEGER DEFAULT 0," +
  111.                 "PRIMARY KEY(ip));";
  112.  
  113.         this.executeStatement(new StatementObject(this.getPlugin(),
  114.                 TABLE_IPC_IP));
  115.         this.executeStatement(new StatementObject(this.getPlugin(),
  116.                 TABLE_IPC_LOG));
  117.         this.executeStatement(new StatementObject(this.getPlugin(),
  118.                 TABLE_IPC_USER));
  119.  
  120.         executeColumnUpdate();
  121.     }
  122.  
  123.     // Initialize Tables for MySQL
  124.     public void initializeMySQLTables() {
  125.         String TABLE_IPC_LOG = "CREATE TABLE IF NOT EXISTS ipcheck_log ( " +
  126.                 "ip varchar(15) NOT NULL," +
  127.                 "username varchar(255) NOT NULL," +
  128.                 "timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
  129.                 "PRIMARY KEY (ip,username)" +
  130.                 ");";
  131.  
  132.         String TABLE_IPC_USER = "CREATE TABLE IF NOT EXISTS ipcheck_user ( " +
  133.                 "username varchar(255) NOT NULL," +
  134.                 "timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
  135.                 "banmessage varchar(255)," +
  136.                 "banned bit(1) NOT NULL DEFAULT b'0'," +
  137.                 "exempted bit(1) NOT NULL DEFAULT b'0'," +
  138.                 "rejoinexempt bit(1) NOT NULL DEFAULT b'0'," +
  139.                 "protected bit(1) NOT NULL DEFAULT b'0'," +
  140.                 "PRIMARY KEY (username)" +
  141.                 ");";
  142.  
  143.         String TABLE_IPC_IP = "CREATE TABLE IF NOT EXISTS ipcheck_ip ( " +
  144.                 "ip varchar(15) NOT NULL," +
  145.                 "timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
  146.                 "banned bit(1) NOT NULL DEFAULT b'0'," +
  147.                 "exempted bit(1) NOT NULL DEFAULT b'0'," +
  148.                 "rejoinexempt bit(1) NOT NULL DEFAULT b'0'," +
  149.                 "PRIMARY KEY (ip)" +
  150.                 ");";
  151.  
  152.         this.executeStatement(new StatementObject(this.getPlugin(),
  153.                 TABLE_IPC_IP));
  154.         this.executeStatement(new StatementObject(this.getPlugin(),
  155.                 TABLE_IPC_LOG));
  156.         this.executeStatement(new StatementObject(this.getPlugin(),
  157.                 TABLE_IPC_USER));
  158.  
  159.         executeColumnUpdate();
  160.     }
  161.  
  162.     public final void log(String player, String ip) {
  163.         this.addIP(ip);
  164.         this.addPlayer(player);
  165.  
  166.         String SQL = "replace into ipcheck_log (ip,username) VALUES (?, ?)";
  167.         this.executeStatement(new StatementObject(this.getPlugin(),
  168.                 SQL, new Object[]{ip, player.toLowerCase()}));
  169.     }
  170.  
  171.     public final void addIP(String ip) {
  172.         String SQL = "insert " + ((this.getPlugin().getConfigurationManager()
  173.                 .getBoolean("use-mysql")) ? "" : "or ") + "ignore into " +
  174.                 "ipcheck_ip (ip) values (?)";
  175.  
  176.         this.executeStatement(new StatementObject(this.getPlugin(),
  177.                 SQL, new Object[]{ip}));
  178.     }
  179.  
  180.     public final void addPlayer(String player) {
  181.         String SQL = "insert " + ((this.getPlugin().getConfigurationManager()
  182.                 .getBoolean("use-mysql")) ? "" : "or ") + "ignore into " +
  183.                 "ipcheck_user (username) values (?)";
  184.  
  185.         this.executeStatement(new StatementObject(this.getPlugin(),
  186.                 SQL, new Object[]{player.toLowerCase()}));
  187.     }
  188.  
  189.     /* Player Methods */
  190.  
  191.     public final void purgePlayer(String player) {
  192.         String STMT_1 = "delete from ipcheck_user where lower(username) = ?";
  193.         String STMT_2 = "delete from ipcheck_log where lower(username) = ?";
  194.  
  195.         this.executeStatement(new StatementObject(this.getPlugin(),
  196.                 STMT_1, new Object[]{player.toLowerCase()}));
  197.  
  198.         this.executeStatement(new StatementObject(this.getPlugin(),
  199.                 STMT_2, new Object[]{player.toLowerCase()}));
  200.     }
  201.  
  202.     // Exemption Methods
  203.  
  204.     public final void exemptPlayer(String player) {
  205.         String SQL = "update ipcheck_user set exempted=1 where " +
  206.                 "lower(username) = ?";
  207.  
  208.         this.executeStatement(new StatementObject(this.getPlugin(),
  209.                 SQL, new Object[]{player.toLowerCase()}));
  210.     }
  211.  
  212.     public final void unexemptPlayer(String player) {
  213.         String SQL = "update ipcheck_user set exempted=0 where " +
  214.                 "lower(username) = ?";
  215.  
  216.         this.executeStatement(new StatementObject(this.getPlugin(),
  217.                 SQL, new Object[]{player.toLowerCase()}));
  218.     }
  219.  
  220.     public final boolean isExemptPlayer(String player) {
  221.         String SQL = "select exempted from ipcheck_user where " +
  222.                 "lower(username) = ?";
  223.  
  224.         QueryFilter filter = new QueryFilter() {
  225.             @Override
  226.             public Object onExecute(ResultSet res) {
  227.                 boolean isExempt = false;
  228.  
  229.                 try {
  230.                     while (res.next()) {
  231.                         int exempt = Integer.parseInt(res.getString(1));
  232.                         isExempt = (exempt == 1);
  233.                     }
  234.                 } catch (SQLException e) {
  235.                     e.printStackTrace();
  236.                 }
  237.  
  238.                 return isExempt;
  239.             }
  240.         };
  241.  
  242.         return (Boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  243.                 SQL, new Object[]{player.toLowerCase()}), filter);
  244.     }
  245.  
  246.     public ArrayList<String> getPlayerExemptList() {
  247.         String SQL = "select username from ipcheck_user where exempted=1";
  248.  
  249.         QueryFilter filter = new QueryFilter() {
  250.             @Override
  251.             public Object onExecute(ResultSet res) {
  252.                 ArrayList<String> users = new ArrayList<String>();
  253.  
  254.                 try {
  255.                     while (res.next()) {
  256.                         users.add(res.getString(1));
  257.                     }
  258.                 } catch (SQLException e) {
  259.                     e.printStackTrace();
  260.                 }
  261.  
  262.                 return users;
  263.             }
  264.         };
  265.  
  266.         return (ArrayList<String>) this.executeQuery(new StatementObject(
  267.                 this.getPlugin(), SQL), filter);
  268.     }
  269.  
  270.     // Ban Methods
  271.  
  272.     public final void banPlayer(String player, String message) {
  273.         String SQL = "update ipcheck_user set banned=1, banmessage = ? where " +
  274.                 "lower(username) = ?";
  275.  
  276.         this.executeStatement(new StatementObject(this.getPlugin(),
  277.                 SQL, new Object[]{message, player.toLowerCase()}));
  278.     }
  279.  
  280.     public final void batchBanPlayers(String list, String msg, Boolean ban) {
  281.         String SQL = "update ipcheck_user set banned = ?, banmessage = ? " +
  282.                 "where lower(username) = '" + list.toLowerCase();
  283.  
  284.         int bit = (ban) ? 1 : 0;
  285.  
  286.         this.executeStatement(new StatementObject(this.getPlugin(),
  287.                 SQL, new Object[]{bit, msg}));
  288.     }
  289.  
  290.     public final void unbanPlayer(String player) {
  291.         String SQL = "update ipcheck_user set banned = 0 where " +
  292.                 "lower(username) = ?";
  293.  
  294.         this.executeStatement(new StatementObject(this.getPlugin(),
  295.                 SQL, new Object[]{player.toLowerCase()}));
  296.     }
  297.  
  298.     public final boolean isBannedPlayer(String player) {
  299.         String SQL = "select banned from ipcheck_user where " +
  300.                 "lower(username) = ?";
  301.  
  302.         QueryFilter filter = new QueryFilter() {
  303.             @Override
  304.             public Object onExecute(ResultSet res) {
  305.                 boolean isBanned = false;
  306.  
  307.                 try {
  308.                     while (res.next()) {
  309.                         int banned = Integer.parseInt(res.getString(1));
  310.                         isBanned = (banned == 1);
  311.                         break;
  312.                     }
  313.                 } catch (SQLException e) {
  314.                     e.printStackTrace();
  315.                 }
  316.  
  317.                 return isBanned;
  318.             }
  319.         };
  320.  
  321.         return (Boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  322.                 SQL, new Object[]{player.toLowerCase()}), filter);
  323.     }
  324.  
  325.     public final boolean isValidPlayer(String player) {
  326.         String SQL = "select username from ipcheck_user where " +
  327.                 "lower(username) = ?";
  328.  
  329.         QueryFilter filter = new QueryFilter() {
  330.             @Override
  331.             public Object onExecute(ResultSet res) {
  332.                 try {
  333.                     return (res.next());
  334.                 } catch (SQLException e) {
  335.                     e.printStackTrace();
  336.                 }
  337.  
  338.                 return false;
  339.             }
  340.         };
  341.  
  342.         return (Boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  343.                 SQL, new Object[]{player.toLowerCase()}), filter);
  344.     }
  345.  
  346.     public final String getBanMessage(String player) {
  347.         String SQL = "select banmessage from ipcheck_user where " +
  348.                 "lower(username) = ?";
  349.  
  350.         QueryFilter filter = new QueryFilter() {
  351.             @Override
  352.             public Object onExecute(ResultSet res) {
  353.                 String message = null;
  354.  
  355.                 try {
  356.                     while (res.next()) {
  357.                         message = res.getString("banmessage");
  358.                         break;
  359.                     }
  360.                 } catch (SQLException e) {
  361.                     e.printStackTrace();
  362.                 }
  363.  
  364.                 return message;
  365.             }
  366.         };
  367.  
  368.         return (String) this.executeQuery(new StatementObject(this.getPlugin(),
  369.                 SQL, new Object[]{player.toLowerCase()}), filter);
  370.     }
  371.  
  372.     /* IP Methods */
  373.  
  374.     public final void purgeIP(String ip) {
  375.         String STMT_1 = "delete from ipcheck_ip where ip = ?";
  376.         String STMT_2 = "delete from ipcheck_log where ip = ?";
  377.  
  378.         this.executeStatement(new StatementObject(this.getPlugin(),
  379.                 STMT_1, new Object[]{ip}));
  380.  
  381.         this.executeStatement(new StatementObject(this.getPlugin(),
  382.                 STMT_2, new Object[]{ip}));
  383.     }
  384.  
  385.     public final void exemptIP(String ip) {
  386.         String SQL = "update ipcheck_ip set exempted = 1 where ip = ?";
  387.  
  388.         this.executeStatement(new StatementObject(this.getPlugin(),
  389.                 SQL, new Object[]{ip}));
  390.     }
  391.  
  392.     public final void unexemptIP(String ip) {
  393.         String SQL = "update ipcheck_ip set exempted = 0 where ip = ?";
  394.  
  395.         this.executeStatement(new StatementObject(this.getPlugin(),
  396.                 SQL, new Object[]{ip}));
  397.     }
  398.  
  399.     public final boolean isExemptIP(String ip) {
  400.         String SQL = "select exempted from ipcheck_ip where ip = ?";
  401.  
  402.         QueryFilter filter = new QueryFilter() {
  403.             @Override
  404.             public Object onExecute(ResultSet res) {
  405.                 boolean isExempt = false;
  406.  
  407.                 try {
  408.                     while (res.next()) {
  409.                         int exempt = Integer.parseInt(res.getString(1));
  410.                         isExempt = (exempt == 1);
  411.                     }
  412.                 } catch (SQLException e) {
  413.                     e.printStackTrace();
  414.                 }
  415.  
  416.                 return isExempt;
  417.             }
  418.         };
  419.  
  420.         return (Boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  421.                 SQL, new Object[]{ip}), filter);
  422.     }
  423.  
  424.     public ArrayList<String> getIPExemptList() {
  425.         String SQL = "select ip from ipcheck_ip where exempted=1";
  426.  
  427.         QueryFilter filter = new QueryFilter() {
  428.             @Override
  429.             public Object onExecute(ResultSet res) {
  430.                 ArrayList<String> ips = new ArrayList<String>();
  431.  
  432.                 try {
  433.                     while (res.next()) {
  434.                         ips.add(res.getString(1));
  435.                     }
  436.                 } catch (SQLException e) {
  437.                     e.printStackTrace();
  438.                 }
  439.  
  440.                 return ips;
  441.             }
  442.         };
  443.  
  444.         return (ArrayList<String>) this.executeQuery(new StatementObject(
  445.                 this.getPlugin(), SQL), filter);
  446.     }
  447.  
  448.     public final void banIP(String ip) {
  449.         String SQL = "update ipcheck_ip set banned = 1 where ip = ?";
  450.  
  451.         this.executeStatement(new StatementObject(this.getPlugin(),
  452.                 SQL, new Object[]{ip}));
  453.     }
  454.  
  455.     public final void batchBanIPs(String list, boolean banning) {
  456.         String SQL = "update ipcheck_ip set banned = ? where ip = '"
  457.                 + list.toLowerCase();
  458.  
  459.         int bit = (banning) ? 1 : 0;
  460.  
  461.         this.executeStatement(new StatementObject(this.getPlugin(),
  462.                 SQL, new Object[]{bit}));
  463.     }
  464.  
  465.     public final void unbanIP(String ip) {
  466.         String SQL = "update ipcheck_ip set banned = 0 where ip = ?";
  467.  
  468.         this.executeStatement(new StatementObject(this.getPlugin(),
  469.                 SQL, new Object[]{ip}));
  470.     }
  471.  
  472.     public final boolean isBannedIP(String ip) {
  473.         String SQL = "select banned from ipcheck_ip where ip = ?";
  474.  
  475.         QueryFilter filter = new QueryFilter() {
  476.             @Override
  477.             public Object onExecute(ResultSet res) {
  478.                 boolean isBanned = false;
  479.  
  480.                 try {
  481.                     while (res.next()) {
  482.                         int banned = Integer.parseInt(res.getString(1));
  483.                         isBanned = (banned == 1);
  484.                         break;
  485.                     }
  486.                 } catch (SQLException e) {
  487.                     e.printStackTrace();
  488.                 }
  489.  
  490.                 return isBanned;
  491.             }
  492.         };
  493.  
  494.         return (Boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  495.                 SQL, new Object[]{ip}), filter);
  496.     }
  497.  
  498.     /* Other Methods */
  499.  
  500.     public final IPObject getIPObject(String ip) {
  501.         String SQL = "select username from ipcheck_log where ip = ?";
  502.  
  503.         QueryFilter filter = new QueryFilter(new Object[]{ip, this}) {
  504.             @Override
  505.             public Object onExecute(ResultSet res) {
  506.                 DatabaseController dbC = (DatabaseController) this.getData()[1];
  507.                 ArrayList<String> users = new ArrayList<String>();
  508.                 String ip = (String) this.getData()[0];
  509.  
  510.                 try {
  511.                     while (res.next()) {
  512.                         users.add(res.getString(1));
  513.                     }
  514.                 } catch (SQLException e) {
  515.                     e.printStackTrace();
  516.                 }
  517.  
  518.                 return new IPObject(ip, users, dbC.isBannedIP(ip),
  519.                         dbC.isExemptIP(ip), dbC.isRejoinExemptIP(ip));
  520.             }
  521.         };
  522.  
  523.         return (IPObject) this.executeQuery(new StatementObject(
  524.                 this.getPlugin(), SQL, new Object[]{ip}), filter);
  525.     }
  526.  
  527.     public final UserObject getUserObject(String player) {
  528.         String SQL = "select ip from ipcheck_log where lower(username) = ?";
  529.         boolean isBanned = this.isBannedPlayer(player);
  530.         boolean isExempt = this.isExemptPlayer(player);
  531.         boolean isRejoin = this.isRejoinExemptPlayer(player);
  532.         boolean isProtec = this.isProtectedPlayer(player);
  533.  
  534.         QueryFilter filter = new QueryFilter() {
  535.             @Override
  536.             public Object onExecute(ResultSet res) {
  537.                 ArrayList<String> ips = new ArrayList<String>();
  538.  
  539.                 try {
  540.                     while (res.next()) {
  541.                         if (!ips.contains(res.getString(1))) {
  542.                             ips.add(res.getString(1));
  543.                         }
  544.                     }
  545.                 } catch (SQLException e) {
  546.                     e.printStackTrace();
  547.                 }
  548.  
  549.                 return ips;
  550.             }
  551.         };
  552.  
  553.         ArrayList<String> ips = (ArrayList<String>) this.executeQuery(
  554.                 new StatementObject(this.getPlugin(), SQL, new Object[]{
  555.                         player.toLowerCase()}), filter);
  556.  
  557.         return new UserObject(player.toLowerCase(), ips, isBanned, isExempt,
  558.                 isRejoin, isProtec);
  559.     }
  560.  
  561.     public final String getLastKnownIP(String player) {
  562.         String SQL = "select ip from ipcheck_log where lower(username) = ? " +
  563.                 "order by timestamp desc limit 1;";
  564.  
  565.         QueryFilter filter = new QueryFilter() {
  566.             @Override
  567.             public Object onExecute(ResultSet res) {
  568.                 String returning = "NO_FIND";
  569.  
  570.                 try {
  571.                     if (res.next()) returning = res.getString(1);
  572.                 } catch (SQLException e) {
  573.                     e.printStackTrace();
  574.                 }
  575.  
  576.                 return returning;
  577.             }
  578.         };
  579.  
  580.         return (String) this.executeQuery(new StatementObject(this.getPlugin(),
  581.                 SQL, new Object[]{player.toLowerCase()}), filter);
  582.     }
  583.  
  584.     public final boolean isValidIP(String ip) {
  585.         String SQL = "select ip from ipcheck_ip where ip = ?";
  586.  
  587.         QueryFilter filter = new QueryFilter() {
  588.             @Override
  589.             public Object onExecute(ResultSet res) {
  590.                 try {
  591.                     return (res.next());
  592.                 } catch (SQLException e) {
  593.                     e.printStackTrace();
  594.                 }
  595.  
  596.                 return false;
  597.             }
  598.         };
  599.  
  600.         if (ip.equals("NO_FIND")) return false;
  601.  
  602.         return (Boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  603.                 SQL, new Object[]{ip}), filter);
  604.     }
  605.  
  606.     public final String getLogTime(String player) {
  607.         String SQL = "select timestamp from ipcheck_user where " +
  608.                 "lower(username) = ?";
  609.  
  610.         QueryFilter filter = new QueryFilter() {
  611.             @Override
  612.             public Object onExecute(ResultSet res) {
  613.                 String returning = null;
  614.  
  615.                 try {
  616.                     if (res.next()) returning = res.getString(1);
  617.                 } catch (SQLException e) {
  618.                     e.printStackTrace();
  619.                 }
  620.  
  621.                 return returning;
  622.             }
  623.         };
  624.  
  625.         return (String) this.executeQuery(new StatementObject(this.getPlugin(),
  626.                 SQL, new Object[]{player.toLowerCase()}), filter);
  627.     }
  628.  
  629.     public final String getLastTime(String player) {
  630.         String SQL = "select timestamp from ipcheck_log where " +
  631.                 "lower(username) = ? order by timestamp desc limit 1;";
  632.  
  633.         QueryFilter filter = new QueryFilter() {
  634.             @Override
  635.             public Object onExecute(ResultSet res) {
  636.                 String returning = null;
  637.  
  638.                 try {
  639.                     if (res.next()) returning = res.getString(1);
  640.                 } catch (SQLException e) {
  641.                     e.printStackTrace();
  642.                 }
  643.  
  644.                 return returning;
  645.             }
  646.         };
  647.  
  648.         return (String) this.executeQuery(new StatementObject(this.getPlugin(),
  649.                 SQL, new Object[]{player.toLowerCase()}), filter);
  650.     }
  651.  
  652.     public final String getCurrentTimeStamp() {
  653.         String SQL = "select CURRENT_TIMESTAMP";
  654.  
  655.         QueryFilter filter = new QueryFilter() {
  656.             @Override
  657.             public Object onExecute(ResultSet res) {
  658.                 String date = null;
  659.  
  660.                 try {
  661.                     res.next();
  662.                     date = res.getString(1);
  663.                 } catch (SQLException e) {
  664.                     e.printStackTrace();
  665.                 }
  666.  
  667.                 return date;
  668.             }
  669.         };
  670.  
  671.         return (String) this.executeQuery(new StatementObject(this.getPlugin(),
  672.                 SQL), filter);
  673.     }
  674.  
  675.     public final ArrayList<UserObject> getPlayersByDate(String dateOne,
  676.                                                         String dateTwo) {
  677.         String SQL = "select username from ipcheck_user where timestamp >= ? " +
  678.                 "and timestamp <= ?";
  679.  
  680.         QueryFilter filter = new QueryFilter(new Object[]{this}) {
  681.             @Override
  682.             public Object onExecute(ResultSet res) {
  683.                 DatabaseController dbC = (DatabaseController) this.getData()[0];
  684.                 ArrayList<UserObject> users = new ArrayList<UserObject>();
  685.  
  686.                 try {
  687.                     while (res.next()) {
  688.                         String user = res.getString(1);
  689.                         users.add(new UserObject(user,
  690.                                 dbC.isBannedPlayer(user)));
  691.                     }
  692.                 } catch (SQLException e) {
  693.                     e.printStackTrace();
  694.                 }
  695.  
  696.                 return users;
  697.             }
  698.         };
  699.  
  700.         return (ArrayList<UserObject>) this.executeQuery(new StatementObject(
  701.                 this.getPlugin(), SQL, new Object[]{dateOne, dateTwo}), filter);
  702.     }
  703.  
  704.     public final ArrayList<UserObject> fetchAllPlayers() {
  705.         String SQL = "select * from ipcheck_user";
  706.  
  707.         QueryFilter filter = new QueryFilter(new Object[]{this}) {
  708.             @Override
  709.             public Object onExecute(ResultSet res) {
  710.                 DatabaseController dbC = (DatabaseController) this.getData()[0];
  711.                 ArrayList<UserObject> users = new ArrayList<UserObject>();
  712.  
  713.                 try {
  714.                     while (res.next()) {
  715.                         String user = res.getString(1);
  716.                         users.add(new UserObject(user,
  717.                                 dbC.isBannedPlayer(user)));
  718.                     }
  719.                 } catch (SQLException e) {
  720.                     e.printStackTrace();
  721.                 }
  722.  
  723.                 return users;
  724.             }
  725.         };
  726.  
  727.         return (ArrayList<UserObject>) this.executeQuery(new StatementObject(
  728.                 this.getPlugin(), SQL), filter);
  729.     }
  730.  
  731.     public final ArrayList<UserObject> fetchBannedPlayers() {
  732.         String SQL = "select * from ipcheck_user";
  733.  
  734.         QueryFilter filter = new QueryFilter(new Object[]{this}) {
  735.             @Override
  736.             public Object onExecute(ResultSet res) {
  737.                 DatabaseController dbC = (DatabaseController) this.getData()[0];
  738.                 ArrayList<UserObject> users = new ArrayList<>();
  739.  
  740.                 try {
  741.                     while (res.next()) {
  742.                         if (res.getString(4).equals("1")) {
  743.                             String user = res.getString(1);
  744.                             users.add(new UserObject(user,
  745.                                     dbC.isBannedPlayer(user)));
  746.                         }
  747.                     }
  748.                 } catch (SQLException e) {
  749.                     e.printStackTrace();
  750.                 }
  751.  
  752.                 return users;
  753.             }
  754.         };
  755.  
  756.         return (ArrayList<UserObject>) this.executeQuery(new StatementObject(
  757.                 this.getPlugin(), SQL), filter);
  758.     }
  759.  
  760.  
  761.     public final ArrayList<IPObject> fetchAllIPs() {
  762.         String SQL = "select * from ipcheck_ip";
  763.  
  764.         QueryFilter filter = new QueryFilter(new Object[]{this}) {
  765.             @Override
  766.             public Object onExecute(ResultSet res) {
  767.                 DatabaseController dbC = (DatabaseController) this.getData()[0];
  768.                 ArrayList<IPObject> ips = new ArrayList<IPObject>();
  769.  
  770.                 try {
  771.                     while (res.next()) {
  772.                         String ip = res.getString(1);
  773.                         ips.add(new IPObject(ip, dbC.isBannedIP(ip)));
  774.                     }
  775.                 } catch (SQLException e) {
  776.                     e.printStackTrace();
  777.                 }
  778.  
  779.                 return ips;
  780.             }
  781.         };
  782.  
  783.         return (ArrayList<IPObject>) this.executeQuery(new StatementObject(
  784.                 this.getPlugin(), SQL), filter);
  785.     }
  786.  
  787.     public final ArrayList<IPObject> fetchBannedIPs() {
  788.         String SQL = "select * from ipcheck_ip";
  789.  
  790.         QueryFilter filter = new QueryFilter(new Object[]{this}) {
  791.             @Override
  792.             public Object onExecute(ResultSet res) {
  793.                 DatabaseController dbC = (DatabaseController) this.getData()[0];
  794.                 ArrayList<IPObject> ips = new ArrayList<IPObject>();
  795.  
  796.                 try {
  797.                     while (res.next()) {
  798.                         if (res.getString(3).equals("1")) {
  799.                             String ip = res.getString(1);
  800.                             ips.add(new IPObject(ip, dbC.isBannedIP(ip)));
  801.                         }
  802.                     }
  803.                 } catch (SQLException e) {
  804.                     e.printStackTrace();
  805.                 }
  806.  
  807.                 return ips;
  808.             }
  809.         };
  810.  
  811.         return (ArrayList<IPObject>) this.executeQuery(new StatementObject(
  812.                 this.getPlugin(), SQL), filter);
  813.     }
  814.  
  815.     public final void setRejoinExemptPlayer(String player, boolean exempt) {
  816.         String SQL = "update ipcheck_user set rejoinexempt = ? " +
  817.                 "where username = ?";
  818.         int value = (exempt) ? 1 : 0;
  819.  
  820.         this.executeStatement(new StatementObject(this.getPlugin(),
  821.                 SQL, new Object[]{value, player.toLowerCase()}));
  822.     }
  823.  
  824.     public final boolean isRejoinExemptPlayer(String player) {
  825.         String SQL = "select rejoinexempt from ipcheck_user where username = ?";
  826.  
  827.         QueryFilter filter = new QueryFilter() {
  828.             @Override
  829.             public Object onExecute(ResultSet res) {
  830.                 boolean isExempt = false;
  831.  
  832.                 try {
  833.                     if (res.next()) {
  834.                         isExempt = (res.getString(1).equals("1"));
  835.                     }
  836.                 } catch (SQLException e) {
  837.                     e.printStackTrace();
  838.                 }
  839.  
  840.                 return isExempt;
  841.             }
  842.         };
  843.  
  844.         return (boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  845.                 SQL, new Object[]{player.toLowerCase()}), filter);
  846.     }
  847.  
  848.     public final void setRejoinExemptIP(String ip, boolean exempt) {
  849.         String SQL = "update ipcheck_ip set rejoinexempt = ? where ip = ?";
  850.         int value = (exempt) ? 1 : 0;
  851.  
  852.         this.executeStatement(new StatementObject(this.getPlugin(),
  853.                 SQL, new Object[]{value, ip}));
  854.     }
  855.  
  856.     public final boolean isRejoinExemptIP(String ip) {
  857.         String SQL = "select rejoinexempt from ipcheck_ip where ip = ?";
  858.  
  859.         QueryFilter filter = new QueryFilter() {
  860.             @Override
  861.             public Object onExecute(ResultSet res) {
  862.                 boolean isExempt = false;
  863.  
  864.                 try {
  865.                     if (res.next()) {
  866.                         isExempt = (res.getString(1).equals("1"));
  867.                     }
  868.                 } catch (SQLException e) {
  869.                     e.printStackTrace();
  870.                 }
  871.  
  872.                 return isExempt;
  873.             }
  874.         };
  875.  
  876.         return (boolean) this.executeQuery(new StatementObject(this.getPlugin(),
  877.                 SQL, new Object[]{ip}), filter);
  878.     }
  879.  
  880.     public final ArrayList<UserObject> fetchRejoinExemptPlayers() {
  881.         String SQL = "select username FROM ipcheck_user WHERE rejoinexempt = 1";
  882.  
  883.         QueryFilter filter = new QueryFilter(new Object[]{this}){
  884.             @Override
  885.             public Object onExecute(ResultSet res) {
  886.                 // IPO Storage
  887.                 ArrayList<UserObject> upos = new ArrayList<>();
  888.  
  889.                 // Fetch DatabaseController from Data
  890.                 DatabaseController dbc = (DatabaseController) getData()[0];
  891.  
  892.                 // Fetch UPOs and append to storage
  893.                 try {
  894.                     while (res.next())
  895.                         upos.add(dbc.getUserObject(res.getString(1)));
  896.                 } catch (SQLException e) {
  897.                     e.printStackTrace();
  898.                 }
  899.  
  900.                 return upos;
  901.             }
  902.         };
  903.  
  904.         // Fetch Return Value
  905.         return (ArrayList<UserObject>) executeQuery(new StatementObject(
  906.                 this.getPlugin(), SQL), filter);
  907.     }
  908.  
  909.     public final ArrayList<IPObject> fetchRejoinExemptIPs() {
  910.         String SQL = "select ip FROM ipcheck_ip WHERE rejoinexempt = 1";
  911.  
  912.         QueryFilter filter = new QueryFilter(new Object[]{this}){
  913.             @Override
  914.             public Object onExecute(ResultSet res) {
  915.                 // IPO Storage
  916.                 ArrayList<IPObject> ipos = new ArrayList<>();
  917.  
  918.                 // Fetch DatabaseController from Data
  919.                 DatabaseController dbc = (DatabaseController) getData()[0];
  920.  
  921.                 // Fetch IPOs and append to storage
  922.                 try {
  923.                     while (res.next())
  924.                         ipos.add(dbc.getIPObject(res.getString(1)));
  925.                 } catch (SQLException e) {
  926.                     e.printStackTrace();
  927.                 }
  928.  
  929.                 return ipos;
  930.             }
  931.         };
  932.  
  933.         // Fetch Return Value
  934.         return (ArrayList<IPObject>) executeQuery(new StatementObject(
  935.                 this.getPlugin(), SQL), filter);
  936.     }
  937.  
  938.     public final void protectPlayer(String player) {
  939.         String SQL = "update ipcheck_user set protected=1 where " +
  940.                 "lower(username) = ?";
  941.  
  942.         executeStatement(new StatementObject(this.getPlugin(), SQL,
  943.                 new Object[]{player.toLowerCase()}));
  944.     }
  945.  
  946.     public final void unprotectPlayer(String player) {
  947.         String SQL = "update ipcheck_user set protected=0 where " +
  948.                 "lower(username) = ?";
  949.  
  950.         executeStatement(new StatementObject(this.getPlugin(), SQL,
  951.                 new Object[]{player.toLowerCase()}));
  952.     }
  953.  
  954.     public final boolean isProtectedPlayer(String player) {
  955.         String SQL = "select protected FROM ipcheck_user WHERE " +
  956.                 "lower(username) = ?";
  957.  
  958.         QueryFilter filter = new QueryFilter() {
  959.             @Override
  960.             public Object onExecute(ResultSet res) {
  961.                 boolean result = false;
  962.  
  963.                 try {
  964.                     if (res.next()) result = res.getString(1).equals("1");
  965.                 } catch (SQLException e) {
  966.                     e.printStackTrace();
  967.                 }
  968.  
  969.                 return result;
  970.             }
  971.         };
  972.  
  973.         return (boolean) executeQuery(new StatementObject(this.getPlugin(), SQL,
  974.                 new Object[]{player.toLowerCase()}), filter);
  975.     }
  976.  
  977.     private void executeColumnUpdate() {
  978.         // Check for Missing Columns (Required for those upgrading from pre-2.0)
  979.         boolean hasRejoinPlayer = true;
  980.         boolean hasRejoinIP = true;
  981.         boolean hasProtectedPlayer = true;
  982.  
  983.         // SQLite
  984.         if (getDatabaseType() == DatabaseType.SQLITE) {
  985.             String SQL_P = "PRAGMA table_info(ipcheck_user);";
  986.  
  987.             QueryFilter filter = new QueryFilter() {
  988.                 @Override
  989.                 public Object onExecute(ResultSet res) {
  990.                     boolean[] flags = new boolean[2];
  991.                     // 0 = Rejoin, 1 = Protection
  992.                     try {
  993.                         while (res.next()) {
  994.                             if (res.getString(2).equals("rejoinexempt"))
  995.                                 flags[0] = true;
  996.                             if (res.getString(2).equals("protected"))
  997.                                 flags[1] = true;
  998.                         }
  999.                     } catch (SQLException e) {
  1000.                         e.printStackTrace();
  1001.                     }
  1002.  
  1003.                     return flags;
  1004.                 }
  1005.             };
  1006.  
  1007.             // Fetch Boolean Array from Database
  1008.             boolean[] res = (boolean[]) this.executeQuery(
  1009.                     new StatementObject(this.getPlugin(), SQL_P), filter);
  1010.  
  1011.             hasRejoinPlayer = res[0];
  1012.             hasProtectedPlayer = res[1];
  1013.  
  1014.             String SQL_I = "PRAGMA table_info(ipcheck_ip);";
  1015.  
  1016.             QueryFilter filter_two = new QueryFilter() {
  1017.                 @Override
  1018.                 public Object onExecute(ResultSet res) {
  1019.                     boolean flag = false;
  1020.                     // 0 = Rejoin, 1 = Protection
  1021.                     try {
  1022.                         while (res.next()) {
  1023.                             if (res.getString(2).equals("rejoinexempt"))
  1024.                                 flag = true;
  1025.                         }
  1026.                     } catch (SQLException e) {
  1027.                         e.printStackTrace();
  1028.                     }
  1029.  
  1030.                     return flag;
  1031.                 }
  1032.             };
  1033.  
  1034.             // Fetch Boolean from Database
  1035.             hasRejoinIP = (boolean) this.executeQuery(
  1036.                     new StatementObject(this.getPlugin(), SQL_I), filter_two);
  1037.  
  1038.             // Execute Results
  1039.             if (!hasRejoinPlayer) {
  1040.                 String SQL_ZERO = "ALTER TABLE ipcheck_user ADD COLUMN " +
  1041.                         "rejoinexempt INTEGER DEFAULT 0";
  1042.  
  1043.                 this.executeStatement(new StatementObject(this.getPlugin(),
  1044.                         SQL_ZERO));
  1045.             }
  1046.  
  1047.             if (!hasProtectedPlayer) {
  1048.                 String SQL_ZERO = "ALTER TABLE ipcheck_user ADD COLUMN " +
  1049.                         "protected INTEGER DEFAULT 0";
  1050.  
  1051.                 this.executeStatement(new StatementObject(this.getPlugin(),
  1052.                         SQL_ZERO));
  1053.             }
  1054.  
  1055.             if (!hasRejoinIP) {
  1056.                 String SQL_ZERO = "ALTER TABLE ipcheck_ip ADD COLUMN " +
  1057.                         "rejoinexempt INTEGER DEFAULT 0";
  1058.  
  1059.                 this.executeStatement(new StatementObject(this.getPlugin(),
  1060.                         SQL_ZERO));
  1061.             }
  1062.  
  1063.  
  1064.         // MySQL
  1065.         } else {
  1066.             String SQL_P = "SHOW COLUMNS FROM " +
  1067.                     getPlugin().getConfigurationManager().getString("dbName") +
  1068.                     ".ipcheck_user";
  1069.  
  1070.             QueryFilter filter = new QueryFilter() {
  1071.                 @Override
  1072.                 public Object onExecute(ResultSet res) {
  1073.                     boolean[] flags = new boolean[2];
  1074.                     // 0 = Rejoin, 1 = Protection
  1075.                     try {
  1076.                         while (res.next()) {
  1077.                             if (res.getString(1).equals("rejoinexempt"))
  1078.                                 flags[0] = true;
  1079.                             if (res.getString(1).equals("protected"))
  1080.                                 flags[1] = true;
  1081.                         }
  1082.                     } catch (SQLException e) {
  1083.                         e.printStackTrace();
  1084.                     }
  1085.  
  1086.                     return flags;
  1087.                 }
  1088.             };
  1089.  
  1090.             // Fetch Boolean Array from Database
  1091.             boolean[] res = (boolean[]) this.executeQuery(
  1092.                     new StatementObject(this.getPlugin(), SQL_P), filter);
  1093.  
  1094.             hasRejoinPlayer = res[0];
  1095.             hasProtectedPlayer = res[1];
  1096.  
  1097.             String SQL_I = "SHOW COLUMNS FROM " +
  1098.                     getPlugin().getConfigurationManager().getString("dbName") +
  1099.                     ".ipcheck_ip";
  1100.  
  1101.             QueryFilter filter_two = new QueryFilter() {
  1102.                 @Override
  1103.                 public Object onExecute(ResultSet res) {
  1104.                     boolean flag = false;
  1105.                     // 0 = Rejoin, 1 = Protection
  1106.                     try {
  1107.                         while (res.next()) {
  1108.                             if (res.getString(1).equals("rejoinexempt"))
  1109.                                 flag = true;
  1110.                         }
  1111.                     } catch (SQLException e) {
  1112.                         e.printStackTrace();
  1113.                     }
  1114.  
  1115.                     return flag;
  1116.                 }
  1117.             };
  1118.  
  1119.             // Fetch Boolean from Database
  1120.             hasRejoinIP = (boolean) this.executeQuery(
  1121.                     new StatementObject(this.getPlugin(), SQL_I), filter_two);
  1122.  
  1123.             // Execute Results
  1124.             if (!hasRejoinPlayer) {
  1125.                 String SQL_ZERO = "ALTER TABLE ipcheck_user ADD COLUMN " +
  1126.                         "rejoinexempt bit(1) NOT NULL DEFAULT b'0'";
  1127.  
  1128.                 this.executeStatement(new StatementObject(this.getPlugin(),
  1129.                         SQL_ZERO));
  1130.             }
  1131.  
  1132.             if (!hasProtectedPlayer) {
  1133.                 String SQL_ZERO = "ALTER TABLE ipcheck_user ADD COLUMN " +
  1134.                         "protected bit(1) NOT NULL DEFAULT b'0'";
  1135.  
  1136.                 this.executeStatement(new StatementObject(this.getPlugin(),
  1137.                         SQL_ZERO));
  1138.             }
  1139.  
  1140.             if (!hasRejoinIP) {
  1141.                 String SQL_ZERO = "ALTER TABLE ipcheck_ip ADD COLUMN " +
  1142.                         "rejoinexempt bit(1) NOT NULL DEFAULT b'0'";
  1143.  
  1144.                 this.executeStatement(new StatementObject(this.getPlugin(),
  1145.                         SQL_ZERO));
  1146.             }
  1147.         }
  1148.     }
  1149.  
  1150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement