perf(datasource): Use try-with-resources when it's possible

This commit is contained in:
Alexandre Vanhecke 2017-05-21 15:13:40 +02:00
parent 24189cf5d4
commit a167429fbc
3 changed files with 382 additions and 485 deletions

View File

@ -50,9 +50,7 @@ public class FlatFile implements DataSource {
@Override
public synchronized boolean isAuthAvailable(String user) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -63,8 +61,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
silentClose(br);
}
return false;
}
@ -83,15 +79,11 @@ public class FlatFile implements DataSource {
if (isAuthAvailable(auth.getNickname())) {
return false;
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(source, true));
try (BufferedWriter bw = new BufferedWriter(new FileWriter(source, true))) {
bw.write(auth.getNickname() + ":" + auth.getPassword().getHash() + ":" + auth.getIp() + ":" + auth.getLastLogin() + ":" + auth.getQuitLocX() + ":" + auth.getQuitLocY() + ":" + auth.getQuitLocZ() + ":" + auth.getWorld() + ":" + auth.getEmail() + "\n");
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
silentClose(bw);
}
return true;
}
@ -109,9 +101,7 @@ public class FlatFile implements DataSource {
return false;
}
PlayerAuth newAuth = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -126,8 +116,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
silentClose(br);
}
if (newAuth != null) {
removeAuth(user);
@ -142,9 +130,7 @@ public class FlatFile implements DataSource {
return false;
}
PlayerAuth newAuth = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -160,8 +146,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
silentClose(br);
}
if (newAuth != null) {
removeAuth(auth.getNickname());
@ -176,9 +160,7 @@ public class FlatFile implements DataSource {
return false;
}
PlayerAuth newAuth = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -197,8 +179,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
silentClose(br);
}
if (newAuth != null) {
removeAuth(auth.getNickname());
@ -222,11 +202,9 @@ public class FlatFile implements DataSource {
if (!isAuthAvailable(user)) {
return false;
}
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<String> lines = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source));) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -234,25 +212,21 @@ public class FlatFile implements DataSource {
lines.add(line);
}
}
bw = new BufferedWriter(new FileWriter(source));
for (String l : lines) {
bw.write(l + "\n");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(source))) {
for (String l : lines) {
bw.write(l + "\n");
}
}
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
silentClose(br);
silentClose(bw);
}
return true;
}
@Override
public synchronized PlayerAuth getAuth(String user) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -263,8 +237,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return null;
} finally {
silentClose(br);
}
return null;
}
@ -279,9 +251,7 @@ public class FlatFile implements DataSource {
return false;
}
PlayerAuth newAuth = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -296,8 +266,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
silentClose(br);
}
if (newAuth != null) {
removeAuth(auth.getNickname());
@ -308,10 +276,8 @@ public class FlatFile implements DataSource {
@Override
public List<String> getAllAuthsByIp(String ip) {
BufferedReader br = null;
List<String> countIp = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -323,17 +289,13 @@ public class FlatFile implements DataSource {
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
return new ArrayList<>();
} finally {
silentClose(br);
}
}
@Override
public int countAuthsByEmail(String email) {
BufferedReader br = null;
int countEmail = 0;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -344,8 +306,6 @@ public class FlatFile implements DataSource {
return countEmail;
} catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage());
} finally {
silentClose(br);
}
return 0;
}
@ -374,18 +334,14 @@ public class FlatFile implements DataSource {
@Override
public int getAccountsRegistered() {
BufferedReader br = null;
int result = 0;
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
while ((br.readLine()) != null) {
result++;
}
} catch (Exception ex) {
ConsoleLogger.warning(ex.getMessage());
return result;
} finally {
silentClose(br);
}
return result;
}
@ -402,10 +358,8 @@ public class FlatFile implements DataSource {
@Override
public List<PlayerAuth> getAllAuths() {
BufferedReader br = null;
List<PlayerAuth> auths = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(source));
try (BufferedReader br = new BufferedReader(new FileReader(source))) {
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
@ -416,8 +370,6 @@ public class FlatFile implements DataSource {
}
} catch (IOException ex) {
ConsoleLogger.logException("Error while getting auths from flatfile:", ex);
} finally {
silentClose(br);
}
return auths;
}
@ -446,14 +398,4 @@ public class FlatFile implements DataSource {
}
return null;
}
private static void silentClose(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignored) {
// silent close
}
}
}
}

View File

@ -15,6 +15,7 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import javax.xml.transform.Result;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
@ -260,15 +261,13 @@ public class MySQL implements DataSource {
@Override
public boolean isAuthAvailable(String user) {
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
ResultSet rs = null;
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user.toLowerCase());
rs = pst.executeQuery();
return rs.next();
try (ResultSet rs = pst.executeQuery()) {
return rs.next();
}
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(rs);
}
return false;
}
@ -330,9 +329,6 @@ public class MySQL implements DataSource {
@Override
public boolean saveAuth(PlayerAuth auth) {
try (Connection con = getConnection()) {
PreparedStatement pst;
PreparedStatement pst2;
ResultSet rs;
String sql;
boolean useSalt = !col.SALT.isEmpty() || !StringUtils.isEmpty(auth.getPassword().getSalt());
@ -341,252 +337,253 @@ public class MySQL implements DataSource {
+ col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL
+ (useSalt ? "," + col.SALT : "")
+ ") VALUES (?,?,?,?,?,?" + (useSalt ? ",?" : "") + ");";
pst = con.prepareStatement(sql);
pst.setString(1, auth.getNickname());
pst.setString(2, auth.getPassword().getHash());
pst.setString(3, auth.getIp());
pst.setLong(4, auth.getLastLogin());
pst.setString(5, auth.getRealName());
pst.setString(6, auth.getEmail());
if (useSalt) {
pst.setString(7, auth.getPassword().getSalt());
try ( PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, auth.getNickname());
pst.setString(2, auth.getPassword().getHash());
pst.setString(3, auth.getIp());
pst.setLong(4, auth.getLastLogin());
pst.setString(5, auth.getRealName());
pst.setString(6, auth.getEmail());
if (useSalt) {
pst.setString(7, auth.getPassword().getSalt());
}
pst.executeUpdate();
}
pst.executeUpdate();
pst.close();
if (!columnOthers.isEmpty()) {
for (String column : columnOthers) {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + column + "=? WHERE " + col.NAME + "=?;");
pst.setString(1, auth.getRealName());
pst.setString(2, auth.getNickname());
pst.executeUpdate();
pst.close();
try (PreparedStatement pst = con.prepareStatement("UPDATE " + tableName + " SET " + column + "=? WHERE " + col.NAME + "=?;")) {
pst.setString(1, auth.getRealName());
pst.setString(2, auth.getNickname());
pst.executeUpdate();
}
}
}
if (hashAlgorithm == HashAlgorithm.IPB4){
sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
pst = con.prepareStatement(sql);
pst.setString(1, auth.getNickname());
rs = pst.executeQuery();
if (rs.next()){
// Update player group in core_members
sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".member_group_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, ipbGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Get current time without ms
long time = System.currentTimeMillis() / 1000;
// update joined date
sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".joined=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Update last_visit
sql = "UPDATE " + ipbPrefix + tableName + " SET " + tableName + ".last_visit=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, auth.getNickname());
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()){
// Update player group in core_members
sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".member_group_id=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql))
{
pst2.setInt(1, ipbGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Get current time without ms
long time = System.currentTimeMillis() / 1000;
// update joined date
sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".joined=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Update last_visit
sql = "UPDATE " + ipbPrefix + tableName + " SET " + tableName + ".last_visit=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
}
}
}
rs.close();
pst.close();
} else if (hashAlgorithm == HashAlgorithm.PHPBB) {
sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
pst = con.prepareStatement(sql);
pst.setString(1, auth.getNickname());
rs = pst.executeQuery();
if (rs.next()) {
int id = rs.getInt(col.ID);
// Insert player in phpbb_user_group
sql = "INSERT INTO " + phpBbPrefix
+ "user_group (group_id, user_id, group_leader, user_pending) VALUES (?,?,?,?);";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, phpBbGroup);
pst2.setInt(2, id);
pst2.setInt(3, 0);
pst2.setInt(4, 0);
pst2.executeUpdate();
pst2.close();
// Update username_clean in phpbb_users
sql = "UPDATE " + tableName + " SET " + tableName
+ ".username_clean=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setString(1, auth.getNickname());
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Update player group in phpbb_users
sql = "UPDATE " + tableName + " SET " + tableName
+ ".group_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, phpBbGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Get current time without ms
long time = System.currentTimeMillis() / 1000;
// Update user_regdate
sql = "UPDATE " + tableName + " SET " + tableName
+ ".user_regdate=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Update user_lastvisit
sql = "UPDATE " + tableName + " SET " + tableName
+ ".user_lastvisit=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Increment num_users
sql = "UPDATE " + phpBbPrefix
+ "config SET config_value = config_value + 1 WHERE config_name = 'num_users';";
pst2 = con.prepareStatement(sql);
pst2.executeUpdate();
pst2.close();
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, auth.getNickname());
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) {
int id = rs.getInt(col.ID);
// Insert player in phpbb_user_group
sql = "INSERT INTO " + phpBbPrefix
+ "user_group (group_id, user_id, group_leader, user_pending) VALUES (?,?,?,?);";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, phpBbGroup);
pst2.setInt(2, id);
pst2.setInt(3, 0);
pst2.setInt(4, 0);
pst2.executeUpdate();
}
// Update username_clean in phpbb_users
sql = "UPDATE " + tableName + " SET " + tableName
+ ".username_clean=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setString(1, auth.getNickname());
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Update player group in phpbb_users
sql = "UPDATE " + tableName + " SET " + tableName
+ ".group_id=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, phpBbGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Get current time without ms
long time = System.currentTimeMillis() / 1000;
// Update user_regdate
sql = "UPDATE " + tableName + " SET " + tableName
+ ".user_regdate=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Update user_lastvisit
sql = "UPDATE " + tableName + " SET " + tableName
+ ".user_lastvisit=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Increment num_users
sql = "UPDATE " + phpBbPrefix
+ "config SET config_value = config_value + 1 WHERE config_name = 'num_users';";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.executeUpdate();
}
}
}
}
rs.close();
pst.close();
} else if (hashAlgorithm == HashAlgorithm.WORDPRESS) {
// NOTE: Eclipse says pst should be closed HERE, but it's a bug, we already close it above. -sgdc3
pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;");
pst.setString(1, auth.getNickname());
rs = pst.executeQuery();
if (rs.next()) {
int id = rs.getInt(col.ID);
sql = "INSERT INTO " + wordpressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?)";
pst2 = con.prepareStatement(sql);
// First Name
pst2.setInt(1, id);
pst2.setString(2, "first_name");
pst2.setString(3, "");
pst2.addBatch();
// Last Name
pst2.setInt(1, id);
pst2.setString(2, "last_name");
pst2.setString(3, "");
pst2.addBatch();
// Nick Name
pst2.setInt(1, id);
pst2.setString(2, "nickname");
pst2.setString(3, auth.getNickname());
pst2.addBatch();
// Description
pst2.setInt(1, id);
pst2.setString(2, "description");
pst2.setString(3, "");
pst2.addBatch();
// Rich_Editing
pst2.setInt(1, id);
pst2.setString(2, "rich_editing");
pst2.setString(3, "true");
pst2.addBatch();
// Comments_Shortcuts
pst2.setInt(1, id);
pst2.setString(2, "comment_shortcuts");
pst2.setString(3, "false");
pst2.addBatch();
// admin_color
pst2.setInt(1, id);
pst2.setString(2, "admin_color");
pst2.setString(3, "fresh");
pst2.addBatch();
// use_ssl
pst2.setInt(1, id);
pst2.setString(2, "use_ssl");
pst2.setString(3, "0");
pst2.addBatch();
// show_admin_bar_front
pst2.setInt(1, id);
pst2.setString(2, "show_admin_bar_front");
pst2.setString(3, "true");
pst2.addBatch();
// wp_capabilities
pst2.setInt(1, id);
pst2.setString(2, wordpressPrefix + "capabilities");
pst2.setString(3, "a:1:{s:10:\"subscriber\";b:1;}");
pst2.addBatch();
// wp_user_level
pst2.setInt(1, id);
pst2.setString(2, wordpressPrefix + "user_level");
pst2.setString(3, "0");
pst2.addBatch();
// default_password_nag
pst2.setInt(1, id);
pst2.setString(2, "default_password_nag");
pst2.setString(3, "");
pst2.addBatch();
try (PreparedStatement pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;")) {
pst.setString(1, auth.getNickname());
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) {
int id = rs.getInt(col.ID);
sql = "INSERT INTO " + wordpressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?)";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
// First Name
pst2.setInt(1, id);
pst2.setString(2, "first_name");
pst2.setString(3, "");
pst2.addBatch();
// Last Name
pst2.setInt(1, id);
pst2.setString(2, "last_name");
pst2.setString(3, "");
pst2.addBatch();
// Nick Name
pst2.setInt(1, id);
pst2.setString(2, "nickname");
pst2.setString(3, auth.getNickname());
pst2.addBatch();
// Description
pst2.setInt(1, id);
pst2.setString(2, "description");
pst2.setString(3, "");
pst2.addBatch();
// Rich_Editing
pst2.setInt(1, id);
pst2.setString(2, "rich_editing");
pst2.setString(3, "true");
pst2.addBatch();
// Comments_Shortcuts
pst2.setInt(1, id);
pst2.setString(2, "comment_shortcuts");
pst2.setString(3, "false");
pst2.addBatch();
// admin_color
pst2.setInt(1, id);
pst2.setString(2, "admin_color");
pst2.setString(3, "fresh");
pst2.addBatch();
// use_ssl
pst2.setInt(1, id);
pst2.setString(2, "use_ssl");
pst2.setString(3, "0");
pst2.addBatch();
// show_admin_bar_front
pst2.setInt(1, id);
pst2.setString(2, "show_admin_bar_front");
pst2.setString(3, "true");
pst2.addBatch();
// wp_capabilities
pst2.setInt(1, id);
pst2.setString(2, wordpressPrefix + "capabilities");
pst2.setString(3, "a:1:{s:10:\"subscriber\";b:1;}");
pst2.addBatch();
// wp_user_level
pst2.setInt(1, id);
pst2.setString(2, wordpressPrefix + "user_level");
pst2.setString(3, "0");
pst2.addBatch();
// default_password_nag
pst2.setInt(1, id);
pst2.setString(2, "default_password_nag");
pst2.setString(3, "");
pst2.addBatch();
// Execute queries
pst2.executeBatch();
pst2.clearBatch();
pst2.close();
// Execute queries
pst2.executeBatch();
pst2.clearBatch();
}
}
}
}
rs.close();
pst.close();
} else if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
// NOTE: Eclipse says pst should be closed HERE, but it's a bug, we already close it above. -sgdc3
pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;");
pst.setString(1, auth.getNickname());
rs = pst.executeQuery();
if (rs.next()) {
int id = rs.getInt(col.ID);
// Insert player password, salt in xf_user_authenticate
sql = "INSERT INTO xf_user_authenticate (user_id, scheme_class, data) VALUES (?,?,?)";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, id);
pst2.setString(2, XfBCrypt.SCHEME_CLASS);
String serializedHash = XfBCrypt.serializeHash(auth.getPassword().getHash());
byte[] bytes = serializedHash.getBytes();
Blob blob = con.createBlob();
blob.setBytes(1, bytes);
pst2.setBlob(3, blob);
pst2.executeUpdate();
pst2.close();
// Update player group in xf_users
sql = "UPDATE " + tableName + " SET "+ tableName + ".user_group_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, xfGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Update player permission combination in xf_users
sql = "UPDATE " + tableName + " SET "+ tableName + ".permission_combination_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, xfGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
pst2.close();
// Insert player privacy combination in xf_user_privacy
sql = "INSERT INTO xf_user_privacy (user_id, allow_view_profile, allow_post_profile, allow_send_personal_conversation, allow_view_identities, allow_receive_news_feed) VALUES (?,?,?,?,?,?)";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, id);
pst2.setString(2, "everyone");
pst2.setString(3, "members");
pst2.setString(4, "members");
pst2.setString(5, "everyone");
pst2.setString(6, "everyone");
pst2.executeUpdate();
pst2.close();
// Insert player group relation in xf_user_group_relation
sql = "INSERT INTO xf_user_group_relation (user_id, user_group_id, is_primary) VALUES (?,?,?)";
pst2 = con.prepareStatement(sql);
pst2.setInt(1, id);
pst2.setInt(2, xfGroup);
pst2.setString(3, "1");
pst2.executeUpdate();
pst2.close();
try (PreparedStatement pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;")) {
pst.setString(1, auth.getNickname());
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) {
int id = rs.getInt(col.ID);
// Insert player password, salt in xf_user_authenticate
sql = "INSERT INTO xf_user_authenticate (user_id, scheme_class, data) VALUES (?,?,?)";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, id);
pst2.setString(2, XfBCrypt.SCHEME_CLASS);
String serializedHash = XfBCrypt.serializeHash(auth.getPassword().getHash());
byte[] bytes = serializedHash.getBytes();
Blob blob = con.createBlob();
blob.setBytes(1, bytes);
pst2.setBlob(3, blob);
pst2.executeUpdate();
}
// Update player group in xf_users
sql = "UPDATE " + tableName + " SET "+ tableName + ".user_group_id=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, xfGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Update player permission combination in xf_users
sql = "UPDATE " + tableName + " SET "+ tableName + ".permission_combination_id=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, xfGroup);
pst2.setString(2, auth.getNickname());
pst2.executeUpdate();
}
// Insert player privacy combination in xf_user_privacy
sql = "INSERT INTO xf_user_privacy (user_id, allow_view_profile, allow_post_profile, allow_send_personal_conversation, allow_view_identities, allow_receive_news_feed) VALUES (?,?,?,?,?,?)";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, id);
pst2.setString(2, "everyone");
pst2.setString(3, "members");
pst2.setString(4, "members");
pst2.setString(5, "everyone");
pst2.setString(6, "everyone");
pst2.executeUpdate();
}
// Insert player group relation in xf_user_group_relation
sql = "INSERT INTO xf_user_group_relation (user_id, user_group_id, is_primary) VALUES (?,?,?)";
try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, id);
pst2.setInt(2, xfGroup);
pst2.setString(3, "1");
pst2.executeUpdate();
}
}
}
}
rs.close();
pst.close();
}
return true;
} catch (SQLException ex) {
@ -605,51 +602,52 @@ public class MySQL implements DataSource {
user = user.toLowerCase();
try (Connection con = getConnection()) {
boolean useSalt = !col.SALT.isEmpty();
PreparedStatement pst;
if (useSalt) {
String sql = String.format("UPDATE %s SET %s = ?, %s = ? WHERE %s = ?;",
tableName, col.PASSWORD, col.SALT, col.NAME);
pst = con.prepareStatement(sql);
pst.setString(1, password.getHash());
pst.setString(2, password.getSalt());
pst.setString(3, user);
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, password.getHash());
pst.setString(2, password.getSalt());
pst.setString(3, user);
pst.executeUpdate();
}
} else {
String sql = String.format("UPDATE %s SET %s = ? WHERE %s = ?;",
tableName, col.PASSWORD, col.NAME);
pst = con.prepareStatement(sql);
pst.setString(1, password.getHash());
pst.setString(2, user);
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, password.getHash());
pst.setString(2, user);
pst.executeUpdate();
}
}
pst.executeUpdate();
pst.close();
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
pst = con.prepareStatement(sql);
pst.setString(1, user);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
int id = rs.getInt(col.ID);
// Insert password in the correct table
sql = "UPDATE xf_user_authenticate SET data=? WHERE " + col.ID + "=?;";
PreparedStatement pst2 = con.prepareStatement(sql);
String serializedHash = XfBCrypt.serializeHash(password.getHash());
byte[] bytes = serializedHash.getBytes();
Blob blob = con.createBlob();
blob.setBytes(1, bytes);
pst2.setBlob(1, blob);
pst2.setInt(2, id);
pst2.executeUpdate();
pst2.close();
// ...
sql = "UPDATE xf_user_authenticate SET scheme_class=? WHERE " + col.ID + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setString(1, XfBCrypt.SCHEME_CLASS);
pst2.setInt(2, id);
pst2.executeUpdate();
pst2.close();
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user);
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) {
int id = rs.getInt(col.ID);
// Insert password in the correct table
sql = "UPDATE xf_user_authenticate SET data=? WHERE " + col.ID + "=?;";
PreparedStatement pst2 = con.prepareStatement(sql);
String serializedHash = XfBCrypt.serializeHash(password.getHash());
byte[] bytes = serializedHash.getBytes();
Blob blob = con.createBlob();
blob.setBytes(1, bytes);
pst2.setBlob(1, blob);
pst2.setInt(2, id);
pst2.executeUpdate();
pst2.close();
// ...
sql = "UPDATE xf_user_authenticate SET scheme_class=? WHERE " + col.ID + "=?;";
pst2 = con.prepareStatement(sql);
pst2.setString(1, XfBCrypt.SCHEME_CLASS);
pst2.setInt(2, id);
pst2.executeUpdate();
pst2.close();
}
}
}
rs.close();
pst.close();
}
return true;
} catch (SQLException ex) {
@ -678,7 +676,6 @@ public class MySQL implements DataSource {
@Override
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
if (!includeEntriesWithLastLoginZero) {
select += " AND " + col.LAST_LOGIN + " <> 0";
@ -702,20 +699,20 @@ public class MySQL implements DataSource {
public boolean removeAuth(String user) {
user = user.toLowerCase();
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
PreparedStatement xfSelect = null;
PreparedStatement xfDelete = null;
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
xfSelect = con.prepareStatement(sql);
xfSelect.setString(1, user);
try (ResultSet rs = xfSelect.executeQuery()) {
if (rs.next()) {
int id = rs.getInt(col.ID);
sql = "DELETE FROM xf_user_authenticate WHERE " + col.ID + "=?;";
xfDelete = con.prepareStatement(sql);
xfDelete.setInt(1, id);
xfDelete.executeUpdate();
try (PreparedStatement xfSelect = con.prepareStatement(sql)) {
xfSelect.setString(1, user);
try (ResultSet rs = xfSelect.executeQuery()) {
if (rs.next()) {
int id = rs.getInt(col.ID);
sql = "DELETE FROM xf_user_authenticate WHERE " + col.ID + "=?;";
try (PreparedStatement xfDelete = con.prepareStatement(sql)) {
xfDelete.setInt(1, id);
xfDelete.executeUpdate();
}
}
}
}
}
@ -724,9 +721,6 @@ public class MySQL implements DataSource {
return true;
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(xfSelect);
close(xfDelete);
}
return false;
}
@ -925,27 +919,27 @@ public class MySQL implements DataSource {
public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>();
try (Connection con = getConnection()) {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
while (rs.next()) {
PlayerAuth pAuth = buildAuthFromResultSet(rs);
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
try (PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;")) {
int id = rs.getInt(col.ID);
pst.setInt(1, id);
ResultSet rs2 = pst.executeQuery();
if (rs2.next()) {
Blob blob = rs2.getBlob("data");
byte[] bytes = blob.getBytes(1, (int) blob.length());
pAuth.setPassword(new HashedPassword(XfBCrypt.getHashFromBlob(bytes)));
try (Statement st = con.createStatement()) {
try (ResultSet rs = st.executeQuery("SELECT * FROM " + tableName)) {
while (rs.next()) {
PlayerAuth pAuth = buildAuthFromResultSet(rs);
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
try (PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;")) {
int id = rs.getInt(col.ID);
pst.setInt(1, id);
ResultSet rs2 = pst.executeQuery();
if (rs2.next()) {
Blob blob = rs2.getBlob("data");
byte[] bytes = blob.getBytes(1, (int) blob.length());
pAuth.setPassword(new HashedPassword(XfBCrypt.getHashFromBlob(bytes)));
}
rs2.close();
}
}
rs2.close();
auths.add(pAuth);
}
}
auths.add(pAuth);
}
rs.close();
st.close();
} catch (SQLException ex) {
logSqlException(ex);
}
@ -1055,8 +1049,9 @@ public class MySQL implements DataSource {
// Rename lastlogin to lastlogin_old
String sql = String.format("ALTER TABLE %s CHANGE COLUMN %s %s BIGINT",
tableName, col.LAST_LOGIN, lastLoginOld);
PreparedStatement pst = con.prepareStatement(sql);
pst.execute();
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.execute();
}
// Create lastlogin column
sql = String.format("ALTER TABLE %s ADD COLUMN %s "

View File

@ -159,19 +159,15 @@ public class SQLite implements DataSource {
@Override
public boolean isAuthAvailable(String user) {
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = con.prepareStatement("SELECT 1 FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);");
String sql = "SELECT 1 FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user);
rs = pst.executeQuery();
return rs.next();
try (ResultSet rs = pst.executeQuery()) {
return rs.next();
}
} catch (SQLException ex) {
ConsoleLogger.warning(ex.getMessage());
return false;
} finally {
close(rs);
close(pst);
}
}
@ -197,27 +193,22 @@ public class SQLite implements DataSource {
@Override
public PlayerAuth getAuth(String user) {
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);");
String sql = "SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user);
rs = pst.executeQuery();
if (rs.next()) {
return buildAuthFromResultSet(rs);
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) {
return buildAuthFromResultSet(rs);
}
}
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(rs);
close(pst);
}
return null;
}
@Override
public boolean saveAuth(PlayerAuth auth) {
PreparedStatement pst = null;
try {
HashedPassword password = auth.getPassword();
if (col.SALT.isEmpty()) {
@ -225,33 +216,33 @@ public class SQLite implements DataSource {
ConsoleLogger.warning("Warning! Detected hashed password with separate salt but the salt column "
+ "is not set in the config!");
}
pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD +
"," + col.IP + "," + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL +
") VALUES (?,?,?,?,?,?);");
pst.setString(1, auth.getNickname());
pst.setString(2, password.getHash());
pst.setString(3, auth.getIp());
pst.setLong(4, auth.getLastLogin());
pst.setString(5, auth.getRealName());
pst.setString(6, auth.getEmail());
pst.executeUpdate();
try (PreparedStatement pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD +
"," + col.IP + "," + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL +
") VALUES (?,?,?,?,?,?);")) {
pst.setString(1, auth.getNickname());
pst.setString(2, password.getHash());
pst.setString(3, auth.getIp());
pst.setLong(4, auth.getLastLogin());
pst.setString(5, auth.getRealName());
pst.setString(6, auth.getEmail());
pst.executeUpdate();
}
} else {
pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD + ","
try (PreparedStatement pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD + ","
+ col.IP + "," + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL + "," + col.SALT
+ ") VALUES (?,?,?,?,?,?,?);");
pst.setString(1, auth.getNickname());
pst.setString(2, password.getHash());
pst.setString(3, auth.getIp());
pst.setLong(4, auth.getLastLogin());
pst.setString(5, auth.getRealName());
pst.setString(6, auth.getEmail());
pst.setString(7, password.getSalt());
pst.executeUpdate();
+ ") VALUES (?,?,?,?,?,?,?);")) {
pst.setString(1, auth.getNickname());
pst.setString(2, password.getHash());
pst.setString(3, auth.getIp());
pst.setLong(4, auth.getLastLogin());
pst.setString(5, auth.getRealName());
pst.setString(6, auth.getEmail());
pst.setString(7, password.getSalt());
pst.executeUpdate();
}
}
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
return true;
}
@ -264,13 +255,11 @@ public class SQLite implements DataSource {
@Override
public boolean updatePassword(String user, HashedPassword password) {
user = user.toLowerCase();
PreparedStatement pst = null;
try {
boolean useSalt = !col.SALT.isEmpty();
String sql = "UPDATE " + tableName + " SET " + col.PASSWORD + " = ?"
+ (useSalt ? ", " + col.SALT + " = ?" : "")
+ " WHERE " + col.NAME + " = ?";
pst = con.prepareStatement(sql);
boolean useSalt = !col.SALT.isEmpty();
String sql = "UPDATE " + tableName + " SET " + col.PASSWORD + " = ?"
+ (useSalt ? ", " + col.SALT + " = ?" : "")
+ " WHERE " + col.NAME + " = ?";
try (PreparedStatement pst = con.prepareStatement(sql)){
pst.setString(1, password.getHash());
if (useSalt) {
pst.setString(2, password.getSalt());
@ -282,17 +271,14 @@ public class SQLite implements DataSource {
return true;
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
return false;
}
@Override
public boolean updateSession(PlayerAuth auth) {
PreparedStatement pst = null;
try {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;");
String sql = "UPDATE " + tableName + " SET " + col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)){
pst.setString(1, auth.getIp());
pst.setLong(2, auth.getLastLogin());
pst.setString(3, auth.getRealName());
@ -301,8 +287,6 @@ public class SQLite implements DataSource {
return true;
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
return false;
}
@ -310,7 +294,6 @@ public class SQLite implements DataSource {
@Override
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
if (!includeEntriesWithLastLoginZero) {
select += " AND " + col.LAST_LOGIN + " <> 0";
@ -344,28 +327,24 @@ public class SQLite implements DataSource {
@Override
public boolean removeAuth(String user) {
PreparedStatement pst = null;
try {
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;");
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user.toLowerCase());
pst.executeUpdate();
return true;
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
return false;
}
@Override
public boolean updateQuitLoc(PlayerAuth auth) {
PreparedStatement pst = null;
try {
pst = con.prepareStatement("UPDATE " + tableName + " SET "
+ col.LASTLOC_X + "=?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, "
+ col.LASTLOC_WORLD + "=?, " + col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=? "
+ "WHERE " + col.NAME + "=?;");
String sql = "UPDATE " + tableName + " SET "
+ col.LASTLOC_X + "=?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, "
+ col.LASTLOC_WORLD + "=?, " + col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=? "
+ "WHERE " + col.NAME + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setDouble(1, auth.getQuitLocX());
pst.setDouble(2, auth.getQuitLocY());
pst.setDouble(3, auth.getQuitLocZ());
@ -377,8 +356,6 @@ public class SQLite implements DataSource {
return true;
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
return false;
}
@ -410,22 +387,18 @@ public class SQLite implements DataSource {
@Override
public List<String> getAllAuthsByIp(String ip) {
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countIp = new ArrayList<>();
try {
pst = con.prepareStatement("SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.IP + "=?;");
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.IP + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, ip);
rs = pst.executeQuery();
while (rs.next()) {
countIp.add(rs.getString(col.NAME));
try (ResultSet rs = pst.executeQuery()) {
while (rs.next()) {
countIp.add(rs.getString(col.NAME));
}
return countIp;
}
return countIp;
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(rs);
close(pst);
}
return new ArrayList<>();
}
@ -453,66 +426,53 @@ public class SQLite implements DataSource {
@Override
public boolean isLogged(String user) {
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=?;");
String sql = "SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user);
rs = pst.executeQuery();
if (rs.next())
return rs.getInt(col.IS_LOGGED) == 1;
try (ResultSet rs = pst.executeQuery()) {
if (rs.next())
return rs.getInt(col.IS_LOGGED) == 1;
}
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(rs);
close(pst);
}
return false;
}
@Override
public void setLogged(String user) {
PreparedStatement pst = null;
try {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;");
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setInt(1, 1);
pst.setString(2, user);
pst.executeUpdate();
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
}
@Override
public void setUnlogged(String user) {
PreparedStatement pst = null;
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;";
if (user != null)
try {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;");
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setInt(1, 0);
pst.setString(2, user);
pst.executeUpdate();
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
}
@Override
public void purgeLogged() {
PreparedStatement pst = null;
try {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.IS_LOGGED + "=?;");
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.IS_LOGGED + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setInt(1, 0);
pst.setInt(2, 1);
pst.executeUpdate();
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
}