Guest User

Untitled

a guest
Jul 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. package util;
  2.  
  3. import java.util.*;
  4. import java.sql.*;
  5. import javax.sql.DataSource;
  6. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  7. import java.io.*;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import java.math.BigInteger;
  11. import java.security.MessageDigest;
  12. import java.security.NoSuchAlgorithmException;
  13. public class sql{
  14.     private boolean connect;
  15.     private Connection c;
  16.     private MysqlDataSource dataSource;
  17.    
  18.     private static String ConfigConnection = null;
  19.     private static String ConfigUsername = null;
  20.     private static String ConfigPassword = null;
  21.     private static int ConfigPort = 0;
  22.     private String hashPassword(String password) {
  23.         String hashword = null;
  24.         try {
  25.         MessageDigest md5 = MessageDigest.getInstance("MD5");
  26.         md5.update(password.getBytes());
  27.         BigInteger hash = new BigInteger(1, md5.digest());
  28.         hashword = hash.toString(16);
  29.  
  30.         } catch (NoSuchAlgorithmException nsae) {
  31.  
  32.         }
  33.         return hashword;
  34.         }
  35.  
  36.     public boolean checkLogin(String userName, String ip, String pass)
  37.     {
  38.         Pattern pattern = Pattern.compile("\\A[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1}\\.[0-9]{1,3}\\z");
  39.         Matcher matcher = pattern.matcher(ip);
  40.         if (!matcher.matches()) return false;
  41.         if (userName.contains("\"")) return false;
  42.         if (userName.contains("\'")) return false;
  43.         if (pass.contains("\"")) return false;
  44.         if (pass.contains("\'")) return false;
  45.     String passw =hashPassword(pass);
  46.     ArrayList result=null;
  47.     String Q="";
  48.     Q="SELECT password FROM hackerforum.users WHERE name = \"" + userName + "\" AND password = \"" + passw + "\" AND ip = \""+ip+"\"";
  49.             result = (ArrayList)this.process(Q);
  50.                         this.close();
  51.             if (result== null || result.size() == 0) {
  52.                     return(false);
  53.             }
  54.                         return true;
  55.     }
  56.  
  57.  
  58.     public sql(String Connection,String DB,String Username,String Password){
  59.    
  60.         if(ConfigConnection == null) {
  61.             try {
  62.                 BufferedReader BR = new BufferedReader(new FileReader("db.ini"));
  63.                 ConfigConnection = BR.readLine();
  64.                 ConfigUsername = BR.readLine();
  65.                 ConfigPassword = BR.readLine();
  66.                 ConfigPort = new Integer(BR.readLine());
  67.             } catch (Exception e) {
  68.             //  e.printStackTrace();
  69.             }
  70.            
  71.             if(ConfigConnection == null) {
  72.                 ConfigConnection = "localhost";
  73.                 ConfigUsername = "root";
  74.                 ConfigPassword = "";
  75.                 ConfigPort = 3306;
  76.             } else {
  77.                 System.out.println("Configuration file sucessfully loaded.");
  78.             }
  79.         }
  80.    
  81.         Connection = ConfigConnection;
  82.         Username = ConfigUsername;
  83.         Password = ConfigPassword;
  84.        
  85.         connect=false;
  86.  
  87.         //Connect to the database.
  88.         c=null;
  89.         dataSource = new MysqlDataSource();
  90.         dataSource.setDatabaseName(DB);
  91.         dataSource.setServerName(Connection);
  92.         dataSource.setPort(ConfigPort);
  93.         try{
  94.             c = dataSource.getConnection(Username,Password);
  95.             connect=true;
  96.         }catch(Exception e){
  97.             //Throw a loggable error here
  98.         }
  99.     }
  100.  
  101.     public sql() {
  102.        
  103.     }
  104.  
  105.     /**
  106.     ArrayList process()<br />
  107.     Returns an ArrayList of strings representing a flattend collection
  108.     of MySql matches. CMD is an SQL query.
  109.     */
  110.     public ArrayList process(String cmd){
  111.             ArrayList returnMe=null;
  112.             if(connect==false)
  113.                 return(null);
  114.  
  115.             Statement stmt=null;
  116.             try{
  117.                 stmt = this.c.createStatement();
  118.                 stmt.setEscapeProcessing(false);
  119.                 ResultSet rs = stmt.executeQuery(cmd);
  120.                 while (rs.next()){
  121.                     int i=1;
  122.                     boolean or=false;
  123.                     while(!or){
  124.                         try{
  125.                             String s=rs.getString(i);
  126.                             if(returnMe==null)
  127.                                 returnMe=new ArrayList();
  128.                             returnMe.add(s);
  129.  
  130.                             i++;
  131.                         }catch(Exception ore){
  132.                             or=true;
  133.                         }
  134.                     }
  135.                 }
  136.             }catch(Exception ee){
  137. //ee.printStackTrace();
  138.                 try{//Maybe it's an update.
  139.                     int i = stmt.executeUpdate(cmd);
  140.                 }catch(Exception ue){
  141.                     //ue.printStackTrace();
  142.                 }
  143.             }
  144.  
  145.         return(returnMe);
  146.     }
  147.    
  148.     public void close(){
  149.         try{
  150.             c.close();
  151.         }catch(Exception e){
  152.             e.printStackTrace();
  153.         }
  154.     }
  155. }
Add Comment
Please, Sign In to add comment