Firefox PHP

Integrating Phorum with my sites Users DB

Posted by daithicollins 
Integrating Phorum with my sites Users DB
February 27, 2009 06:39AM
Hi,

Im a newbie to all this, I have read all of the posts on "user_session_restore" and still cannnot make sense of this integration.
Is there a step by step guide to adding the users or checking the usernames and passwords in my DB and integrating it with the Phorums DB.

This is really bothering me and I cannot seem to even get the first step. can anyone help me out, as I said I have read the forums on this and I just cannot a start on it. Any help would be greatly appreciated.

Regards,
David
Re: Integrating Phorum with my sites Users DB
February 27, 2009 08:18AM
You could try your hand at creating a plugin for my External Authentication module. I have a base plugin there which has many comments and tips and also a developers guide which may be useful. If nothing else you can look at it and the other plugins built for it as examples of where you need to go.


Joe Curia (aka Azumandias)
Modules: l0Admin Mass Email00000000l000000Automatic Time Zones000ll.l00000Enhanced Custom Profiles0.00Google Calendar0000l.l000000Post Previews
000000000Admin Security Suite000000000000Check Modules for Upgrades0000External Authentication000000Group Auto-Email00000.00000Private Message Alerts
000000000Attachment Download Counter0000Custom Attachment Icons000ll.ll00Favorite Forums000000.00000Highlighted Search Terms0000Self-Delete Posts Option
000000000Attachment Watermarks0l00000000Custom Language Database00l.l.0Forum Lockdown00000.00000Ignore Forums0000000000000Threaded Tree View
000000000Automatic Message Pruning00.llll.00Easy Color Scheme Manager0l.l00Forum Subscriptions0000lll000Moderated User Group
Templates:lGeneric Integration000000000 0000Simple Rounded000000 00000000Tabbed Emerald
Re: Integrating Phorum with my sites Users DB
February 27, 2009 09:52AM
Hi Joe,

Thanks for the response, I think I will need to create a module.
Is there not a way that I can just check my own DB when authenticating in the Phorum? There is surely a way to do that without creating a module etc?
Sorry possibly a stupid question to you guys but this could possibly be way over my head as a developer.

David
Re: Integrating Phorum with my sites Users DB
February 27, 2009 02:49PM
There is a lot more than simple authentication going on in Phorum for the users. You have things like signatures, post counts, subscriptions to posts, new message flags, etc, all of which require Phorum to have its own user table. The best method really is to use the user_session_restore hook as this will do just what you described, authenticating the Phorum users via your external program.

If you are not able to code your own module to use this hook based on the example found by searching these forums and are not able to code your own plugin to use my External Authentication module, then you will have to wait until I have the free time to help you with this or someone else has the time to help you. Unfortunately my project list seems to be growing rapidly these days so that may be two or three months.


Joe Curia (aka Azumandias)
Modules: l0Admin Mass Email00000000l000000Automatic Time Zones000ll.l00000Enhanced Custom Profiles0.00Google Calendar0000l.l000000Post Previews
000000000Admin Security Suite000000000000Check Modules for Upgrades0000External Authentication000000Group Auto-Email00000.00000Private Message Alerts
000000000Attachment Download Counter0000Custom Attachment Icons000ll.ll00Favorite Forums000000.00000Highlighted Search Terms0000Self-Delete Posts Option
000000000Attachment Watermarks0l00000000Custom Language Database00l.l.0Forum Lockdown00000.00000Ignore Forums0000000000000Threaded Tree View
000000000Automatic Message Pruning00.llll.00Easy Color Scheme Manager0l.l00Forum Subscriptions0000lll000Moderated User Group
Templates:lGeneric Integration000000000 0000Simple Rounded000000 00000000Tabbed Emerald
Re: Integrating Phorum with my sites Users DB
February 28, 2009 09:56AM
Before you can do the integration of login, you need a way to populate the phorum user database (the cleanest way to do this is with the phorum api for adding users). You then need to keep users information in sync between the two databases (mainly email addresses, but you could also add in privacy options, groups etc.).

For some of the things that need doing see:
[www.phorum.org]



Edited 1 time(s). Last edit at 02/28/2009 10:03AM by DavidVB.
Re: Integrating Phorum with my sites Users DB
March 05, 2009 07:31PM
Hi, I'm also a total noob at this and I've been banging my head over the same thing, but I think I've worked it out now.

So, step-by-step, here's what you need to do.

1. Firstly you need to make sure that the users in the phorum_users table match your main system (or at the very least, the username and user_id).
I did this by just adding the following code to my script which registers a new user

Language: PHP
// Add the new user to the Phorum users table   $curcwd = getcwd(); $dir = $curcwd . "/Phorum"; chdir( $dir );   define("PHORUM", 1); global $PHORUM;   //Phorum Includes   include_once("./include/db/config.php"); include_once("./include/db/mysql.php"); include_once("./include/api/base.php"); include_once("./include/api/user.php");   // Get the new MB user_id from the INSERT $Query = "SELECT user_id FROM members WHERE Username = ';$sUsername';"; $rsUserID = mysql_query($Query, $conn_Mozziebite) or die(mysql_error()); $row_rsUserID = mysql_fetch_assoc($rsUserID);   $Phorum_user_id = $row_rsUserID[';user_id';]; $Phorum_Username = $_POST[';username';]; $Phorum_Password = $_POST[';pass';]; $Phorum_Email = $_POST[';email';];   $user = array( "user_id" => $Phorum_user_id, "username" => $Phorum_Username, "password" => $Phorum_Password, "email" => $Phorum_Email, "admin" => 0, "active" => PHORUM_USER_ACTIVE );   phorum_api_user_save($user, PHORUM_FLAG_RAW_PASSWORD);   chdir($curcwd);




2. Make a mod function which handles the user_session_restore hook. Mine is below, just change the bits in the
comments at the top where it says "MySite" or "Me" to something appropriate to you. Leave the rest of the comment section as it is - this is the bit that tells Phorum what hooks are called. Copy this code into a module, call it "mod_something.php"and save in the /Phorum/mods/ directory.

Language: PHP
<?php   /* phorum module info title: MySite External Authentication For Phorum category: admin category: integration category: user_management author: Me: This module allows authentication to Phorum from MySite external application hook: user_session_restore|mod_phorum_User_Session_Restore */   if(!defined("PHORUM")) return;   session_start();   function mod_phorum_User_Session_Restore($data){   // This function restores the Phorum session, inherited from the site SESSION.   // We do not want to run this when we';re in the admin or upgrade interface. if (defined(';PHORUM_ADMIN';) || defined(';PHORUM5_CONVERSION';)) return $data;   global $PHORUM_CONNECTOR;   // Retrieve the user_id of the current user from the PHP session data. The user_id is really the // only thing that needs to be remembered for a Phorum session, because all other data for the user is stored // in the database. If no user id was set in the session, then use FALSE to flag this to Phorum.   $phorum_user_id = empty($_SESSION[';user_id';])? FALSE : $_SESSION[';user_id';];   // Populate the user_session_restore hook   $data[PHORUM_SESSION_LONG_TERM] = $phorum_user_id; $data[PHORUM_SESSION_SHORT_TERM] = $phorum_user_id;   // Check the admin status of user, and set flags accordingly   if ($phorum_user_id) { // Find admin status by looking at Phorum "Admin" flag // check admin flag $user_data = phorum_api_user_get($phorum_user_id); if ($user_data && ($user_data[';admin';] > 0)) { // Admins are "1" // user is admin $data[PHORUM_SESSION_ADMIN] = $phorum_user_id; } else { // User is not admin $data[PHORUM_SESSION_ADMIN] = FALSE; } }   return $data;   }   ?>


3. Upload this module to your server. This "installs" it.
4. Go into Phorum/Admin.php. Go into Modules on the left. You should see the title of your module (the one from the comment section at the top) somewhere on the page. Change from "off" to "on". Submit the changes. Your module is now part of Phorum and the mod_phorum_User_Session_Restore() function will be called automatically when Phorum hits its user_session_restore hook.
5. Make sure the $_SESSION('user_id') value gets set in your main site when a valid logon takes place.
6. Thats it. Now when you navigate to Phorum from within your site, the user session should get passed into Phorum.



Edited 6 time(s). Last edit at 03/06/2009 08:50AM by RickM.
Re: Integrating Phorum with my sites Users DB
June 01, 2009 09:21PM
Thanks for this code. I've been attempting to integrate it with my user registration page, but keep getting this error:

Fatal error: The Phorum MySQL database layer is unable to find the extension file ./include/db/mysql/mysqli.php on the system. Check if all Phorum files are uploaded and if you did specify the correct "mysql_php_extension" in the file include/db/config.php (valid options are "mysql" and "mysqli").

Could you help me with any ideas on fixing this?
Re: Integrating Phorum with my sites Users DB
June 01, 2009 10:55PM
1) Check your server to make sure the mysqli.php file exists in the proper subdirectory.
2) Check to make sure you are changing to the phorum directory before you run the includes.

Language: PHP
$curcwd = getcwd(); $dir = $curcwd . "/Phorum"; chdir( $dir );
That /Phorum exists
ie if you are in your root directory for the server, you can successfully change to Phorum directory.
Re: Integrating Phorum with my sites Users DB
June 01, 2009 11:26PM
Thank you for the help. Now the user is correctly being inserted into the "phorum_users" table.

Now I'm having trouble with the final 2 steps:
5. Make sure the $_SESSION('user_id') value gets set in your main site when a valid logon takes place.
6. Thats it. Now when you navigate to Phorum from within your site, the user session should get passed into Phorum.

I log-in to my website (see log-in code below), but the session doesn't seem to transfer when I browse to the Phorum directory. If I turn off the module (posted above by RickM) then I can go to the Phorum pages directly and log-in there. How can I keep the module enabled and only have the user log-in once?

// Query the database.
$query = "SELECT user_id, username, first_name, DATE_FORMAT(last_login, '%M %D, %Y at %l:%i %p') as last_login, last_name, access_level, email, zip, state, city, hometelephone, mailing_address, mailing_address2, number_of_logins FROM member_database WHERE (username='$u' AND password=PASSWORD('$p'))";
$result = mysql_query ($query) or die(mysql_error());
$row = mysql_fetch_array ($result, MYSQL_NUM);

if ($row) { // A match was made.

// Start the session, register the values & redirect.
session_name ('YourVisitID');
session_set_cookie_params (600, '/folder/', '[www.mywebsiteURL];);
session_start();
$_SESSION['user_id'] = $row[0];
$_SESSION['username'] = $row[1];
$_SESSION['first_name'] = $row[2];
$_SESSION['last_login'] = $row[3];
$_SESSION['last_name'] = $row[4];
$_SESSION['access_level'] = $row[5];
$_SESSION['email'] = $row[6];
$_SESSION['zip'] = $row[7];
$_SESSION['state'] = $row[8];
$_SESSION['city'] = $row[9];
$_SESSION['hometelephone'] = $row[10];
$_SESSION['mailing_address'] = $row[11];
$_SESSION['mailing_address2'] = $row[12];
$_SESSION['number_of_logins'] = $row[13];
header ("Location: ["]; . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/loggedin.php");
exit();
Re: Integrating Phorum with my sites Users DB
June 02, 2009 03:03AM
You'll have to do some debugging on that, since the basic idea of Rick's code seems okay to me. Two things that you can do:

  • Add a die("blah"); statement in the function mod_phorum_User_Session_Restore() to see if it is really called.
  • Add a print_r($_SESSION); to the function, to see if the expected session data is available when running the function.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce



Edited 1 time(s). Last edit at 06/02/2009 03:21AM by Thomas Seifert.
Sorry, only registered users may post in this forum.

Click here to login