PDA

View Full Version : [317]*Finally* Fully Working IPB Server Integration (IPB 3.1)



FinalRevolution
September 20th, 2010, 00:27
That's right, the search for IPB server integration is over! I was tooling around with Martin's vbulletin code and was able to update it to work 100% with IPB 3.1.2. Keep in mind 95% of this code is by Martin, I just updated it to work with the latest version of IPB. Now, for those of you who don't know what integration between your forums and server is, it's allowing people to log onto your server using the same username and password as the forums. This code will also prevent people from registering an account at the login screen forcing new members to register on your forums. In order for this to work you must have IPB 3.1 or higher currently installed on your host/server. Invisionfree or any other free forum software will not work for this. If you have IPB 3.1 then you obviously know your way around a MySQL database so this should be fairly simple.

NOTE: If your database is hosted in a different place other than where your server is hosted you need to make sure you are allowing external connections to the database.

NOTE: Like I said before this is originally for vBulletin so do not be alarmed if parts of the code and file names say vBulletin

Step 1:
Open server.java and add this:

public static vBulletin vb = new vBulletin("databasehost","database","username","password",vBulletin.Type.IPB);
Make sure you substitute databasehost, database, username, and password with the MySQL database details.

Step 2:
Create a new file called vBullletin.java

/**
* vBulletin class
* @author Mad Turnip
*/

import java.sql.*;

public class vBulletin implements Runnable {


private Connection connection = null;
private Statement statement = null;
private static Thread thread = null;

public enum Type {
myBB,
SMF,
IPB,
vBulletin,
phpBB,
}

private String[] tableNames = new String[6];
private void setTables(){
if(forum == Type.myBB){
tableNames = new String[] {"mybb_users","username","password","salt","usergroupid",};
} else if(forum == Type.SMF){
tableNames = new String[] {" smf_members","memberName","passwd","passwordSalt","ID_GROUP",};
} else if(forum == Type.IPB){
tableNames = new String[] {"members","members_display_name","members_pass_hash","members_pass_salt","member_group_id",};
} else if(forum == Type.vBulletin){//vbulletin
tableNames = new String[] {"user","username","password","salt","usergroupid",};
} else if(forum == Type.phpBB){//phpBB
tableNames = new String[] {"users","username","user_password","user_password","group_id",};
}
}

public vBulletin(String url,String database,String username,String password,Type t){
this.hostAddress = "jdbc:mysql://"+url+"/"+database;
this.username = username;
this.password = password;
this.forum = t;
try {
//connect();
thread = new Thread(this);
thread.start();
} catch(Exception e){
connection = null;
e.printStackTrace();
}
}

private final String hostAddress;
private final String username;
private final String password;
private final Type forum;

private void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(Exception e2){
System.out.println("Cannot find mySql Driver.");
return;
}
try {
connection = DriverManager.getConnection(hostAddress, username,password);
statement = connection.createStatement();
} catch(Exception e){
System.out.println("Connetion rejected, Wrong username or password, or ip is banned, or host is down.");
connection = null;
e.printStackTrace();
}
}
private void ping(){
try{
ResultSet results = null;
String query = "SELECT * FROM "+tableNames[0]+" WHERE "+tableNames[2]+" LIKE 'null312'";
results = statement.executeQuery(query);
} catch(Exception e){
connection = null;
connect();
e.printStackTrace();
}
}

public void run(){
boolean allowRun = true;
while(allowRun){
try {
if(connection == null) {
setTables();
connect();
} else {
ping();
}
thread.sleep(10000);
} catch(Exception e){
}
}
}
/**
* returns 2 integers, the return code and the usergroup of the player
*/

public int[] checkUser(String name,String password){
int i = 0;
int[] returnCodes = {0,0};//return code for client, group id

try{
ResultSet results = null;
String query = "SELECT * FROM "+tableNames[0]+" WHERE "+tableNames[1]+" LIKE '"+name+"'";
try {
if(statement == null)
statement = connection.createStatement();
} catch(Exception e5){
statement = null;
connection = null;
connect();
statement = connection.createStatement();
}
results = statement.executeQuery(query);
if(results.next()){
String salt = results.getString(tableNames[3]);
String pass = results.getString(tableNames[2]);
int group = results.getInt(tableNames[4]);
returnCodes[1] = group;
String pass2 = "";
if(forum == Type.myBB){
pass2 = MD5.MD5(MD5.MD5(salt)+MD5.MD5(password));
} else if(forum == Type.vBulletin){
pass2 = MD5.MD5(password);
pass2 = MD5.MD5(pass2+salt);
} else if(forum == Type.SMF){
pass2 = MD5.SHA((name.toLowerCase())+password);
} else if(forum == Type.phpBB){
pass2 = MD5.MD5(password);
} else if(forum == Type.IPB){
pass2 = MD5.MD5(MD5.MD5(salt)+MD5.MD5(password));
}
if(pass.equals(pass2)){
returnCodes[0] = 2;
return returnCodes;
} else {
returnCodes[0] = 3;
return returnCodes;
}
} else {
//no user exists
returnCodes[0] = 12;
return returnCodes;
}
} catch(Exception e){
statement = null;
returnCodes[0] = 8;
return returnCodes;
}
}
}

Step 3:
Create a new file called MD5.java

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}

public static String SHA(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}

public static String MD5(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}

}

Step 4:
Now that we are connected to the database we need to check their password and username on login
Open client.java
Find:
int loadgame = loadgame(playerName, playerPass);
Keep in mind this may be different depending on your server.

Add this underneath:


if(loadgame == 2 || loadgame == 0){
int[] returnCodes = server.vb.checkUser(playerName,playerPass);
int usergroupId = returnCodes[1];
//below is an example of using usergroups
switch(usergroupId){
case 4://admin
playerRights = 3;
break;
//case 6://gmod
//playerRights = 2;
//break;
//case 7://mod
//playerRights = 1;
//break;
//case 3://member
//playerRights = 0;
//break;
}
returnCode = returnCodes[0];
if(returnCode != 2){
disconnected = true;
} else
loadgame = 0;
}
This will make sure the username and password matches the forum credentials. Now that everything is working you can do even more with the integration. In the above code you can see:
case 4://admin
playerRights = 3;
break;
This code is saying that while the system is checking the username and password, it is also fetching that member's group ID. This can be very helpful if you want to make them an admin on your forums and then not have to manually change their playerRights.

Log into your IP.Board Admin and go to Manage User Groups
Only the registered members can see the link.

Look at the id of the group after its name. As you can see in mine, my admins group is id:4
Replace the 4 in case 4 with your admin group ID and you can even specify how many rights they get (mod = 1, admin = 2, owner = 3)
You may also want to add your mods, member group, and donator group.

Wow! That's a mouthful, but this is the simplest way I could make it. I'm not an expert, but I will do my best to answer your questions :)

Emily
September 20th, 2010, 00:29
I don't have vb, but this does look correct, nice job =)

dragonslyfe
October 29th, 2010, 04:40
does this tutorial work for project insanity, and im not understanding is this for vbulleton or ipboard?

FinalRevolution
October 31st, 2010, 19:39
This particular one is for IPB, and no I do not believe this will work on PI without heavy modification

Fly
October 31st, 2010, 22:26
Very nice tutorital, this is for IPB.

kaos
November 1st, 2010, 10:14
So is this for delta and nice work :D.