Integrating forums with your own application and user base can be a real pain in the ass. I’ve done this in the past with Vbulletin and phpBB, but it’s really not that much fun. Both involved some degree of hacking of their core code which means problems when trying to upgrade (you have to remember every tiny thing you did to make it work). As both products improve and get more advanced you’d think they’d have built nice apis to make this job easier. Apparently not yet…
I’ve been pissing about with my own pet project ( http://www.bandresources.net ), and decided I’d like a forum too. Obviously I’d need single sign on. Was even considering building my own forum app from scratch so I would have to deal with the usual headaches, but then I discovered Phorum . OK, it’s been around since 1998, but it always seemed kind of fugly and basic compared to phpBB and VBulletin when I’d checked it out in the past. Turns out it is actually awesome – lean and fast, and it has a fricken API so that it’s really bloody easy to integrate with other apps!
Read on to find out how to integrate in a rediculously short time.
As I mentioned Phorum has it’s own API with hooks into all the important areas of functionality. To get external authentication up and running all I had to do was a few simple things.
-
Install phorum!
-
Alter my login script to sync the phorum_users table whenever a user logs in successfully. Don’t need to sync all fields, but make sure the record contains at least the user_id, username, displayname, email. Also the “active” field has to be set to “1″ or this won’t work.
-
Create a simple forum module that overrides phorum’s own check to see if you’re logged in. My code is below. How you get the user_id will depend on your own application. Drop this php file in the mods directory
-
Login to phorum admin panel. Go to Global Settings > Modules, and your module should be somewhere on that page. Turn it on!
After you have done this trying logging into your main site, then come back to phorum. If all is well it will show you are logged in! After that you just need to edit the templates of whatever theme you are using and change login/registration links to point to your corresponding pages on the main site.
/*
require_version: 5.2.2
category: user_features
hook: user_authenticate|userauth_login
hook: user_session_restore|userauth_session_restore
*/
function userauth_login ($data) {
$db = new DB;
$sql = "SELECT * FROM users WHERE username = '" . $data['username'] . "' AND (password = '" . md5($data['password']) . "' OR password = '" . $data['password'] . "')";
$res = $db->query($sql);
if ($db->numRows($res) == 1){
$row = $db->fetchAssoc($res);
if ($row['banned'] == "N" AND $row['email_verified'] == "Y"){
$active = 1;
}
else {
$active = 0;
}
$sync_sql = "INSERT INTO phorum_users (user_id, username, password, email, active)
VALUES ('" . $row['user_id'] . "','" . $row['username'] . "','" . $row['password'] . "','" . $row['email'] . "', '$active')
ON DUPLICATE KEY UPDATE date_last_active = NOW(), active='$active'";
$sync_res = $db->update($sync_sql);
$data['user_id'] = $row['user_id'];
include_once SITE_PATH . "/classes/class.sessions.php";
$ses_class = new Session();
session_set_cookie_params(0);
session_set_save_handler (array(&$ses_class, '_open'),
array(&$ses_class, '_close'),
array(&$ses_class, '_read'),
array(&$ses_class, '_write'),
array(&$ses_class, '_destroy'),
array(&$ses_class, '_gc'));
// */
session_start();
$user_id = $_SESSION['user_id'];
}
else {
$data['user_id'] = false;
}
return $data;
}
function userauth_session_restore($sessions)
{
// do some magic code that coughs up the user_id to use in Phorum,
// based on your main site's session system. So probably something
// like looking up the cookie, finding your main user and then turning
// that one into the Phorum user_id (possible the same id as your
// main system's user_id if you implemented the synchronization of
// the users like that).
//I found out how to do this here: http://www.phorum.org/phorum5/read.php?28,125993
//echo "HI";
include_once SITE_PATH . "/classes/class.sessions.php";
$ses_class = new Session();
session_set_cookie_params(0);
session_set_save_handler (array(&$ses_class, '_open'),
array(&$ses_class, '_close'),
array(&$ses_class, '_read'),
array(&$ses_class, '_write'),
array(&$ses_class, '_destroy'),
array(&$ses_class, '_gc'));
// */
session_start();
$user_id = $_SESSION['user_id'];
//print_r($_SESSION);
//echo $user_id;
// If no user is logged in. Let's asume that the magic code would return
// NULL in that case here. Note that if it would return FALSE, you could even
// skip the whole if/then here and directly assign $user_id to the $sessions
// array elements.
if ($user_id === NULL) {
$sessions[PHORUM_SESSION_LONG_TERM] = FALSE;
$sessions[PHORUM_SESSION_SHORT_TERM] = FALSE;
}
// A user is logged in.
else {
//echo "Logged in!";
$sessions[PHORUM_SESSION_LONG_TERM] = $user_id;
$sessions[PHORUM_SESSION_SHORT_TERM] = $user_id;
}
//print_r($sessions);
return $sessions;
}
Extensive developer and API docs are available at http:/www.phorum.org/docs/html/
Phorum rawks.