Frustration with Integration of Phorum

Posted by saniko 
Frustration with Integration of Phorum
June 22, 2008 11:18PM
I apologize but I've scanned every message and read through the developer docs. I'm fairly good with PHP as well.
I'm struggling with getting the user_session_restore hook and phorum_api_user_save function working!
Can I combine them both into one module? I have an info.txt file that calls out the hook:user_session_restore|ext

I've enabled the module but I'm not sure if it is even being called. I've already created the user in the phorum_users table prior to reading about the phorum_api_user_save function.

<?php
if (! defined("PHORUM")) return;
function ext($sessions)
{
require_once('Connections/connUser.php');
mysql_select_db($database_connUser, $connUser);
$query_rsUser = sprintf("SELECT user_name FROM tbl_roster WHERE email_address=%s AND game_id=%s", GetSQLValueString($_SESSION['MM_Username'], "text"),GetSQLValueString($_SESSION['MM_CurrentGame'], "int"));
$rsUser = mysql_query($query_rsUser, $connUser) or die(mysql_error());
$row_rsUser = mysql_fetch_assoc($rsUser);
$user_name = $row_rsUser['user_name'];

mysql_select_db($database_connUser, $connUser);
$query_rsGame = sprintf("SELECT game_admin FROM tbl_game WHERE game_id=%s", GetSQLValueString($_SESSION['MM_CurrentGame'], "int"));
$rsGame = mysql_query($query_rsGame, $connUser) or die(mysql_error());
$row_rsGame = mysql_fetch_assoc($rsGame);
$game_admin_email = $row_rsGame['game_admin'];
if ($game_admin_email == $_SESSION['MM_Username'])) {
			$admin = 1;} else {$admin = 0;} 
mysql_select_db($database_connUser, $connUser);
$query_rsPhorum = sprintf("SELECT user_id FROM phorum_users WHERE username=%s", GetSQLValueString($user_name, "text"));
$rsPhorum = mysql_query($query_rsPhorum, $connUser) or die(mysql_error());
$row_rsPhorum = mysql_fetch_assoc($rsPhorum);
$_SESSION['userfid'] = $row_rsPhorum['user_id'];
$user = array(
     "user_id"   => $_SESSION['userfid'],
     "username"  => $user_name,
     "email"     => $_SESSION['MM_Username'],
     "admin"     => $admin,
     "active"    => PHORUM_USER_ACTIVE
 );
phorum_api_user_save($user);
    // Override the session handling for front end forum sessions.
    // Retrieve a session from a standard PHP session by first
	// starting a PHP session if that was not done yet...
    if (!session_id()) session_start();

    // ...and then retrieving 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['userfid'])
                    ? FALSE : $_SESSION['userfid'];
	
    // The other two need to be updated. If the main system does
    // not use the concept of one long and one short term cookie
    // (named "tight security" by Phorum), then simply assign
    // the user_id to both PHORUM_SESSION_LONG_TERM and
    // PHORUM_SESSION_SHORT_TERM.
    $sessions[PHORUM_SESSION_SHORT_TERM] = $phorum_user_id;
    $sessions[PHORUM_SESSION_LONG_TERM] = $phorum_user_id;
	$sessions[PHORUM_SESSION_ADMIN] = $admin;
    return $sessions;
mysql_free_result($rsUser);
mysql_free_result($rsGame);
mysql_free_result($rsPhorum);
}
?>



Edited 1 time(s). Last edit at 06/23/2008 08:36AM by Maurice Makaay.
Re: Frustration with Integration of Phorum
June 23, 2008 01:24AM
I looked at your code. The general idea seems okay. Some things that I noticed:

  • Why all the repeating calls to mysql_select_db? One at the start is enough. Also make sure that you use a second mysql_select_db call that sets the Phorum database as the active database (after you are done with your db queries).
  • You are using $_SESSION at the start of your function, only you startup PHP session support after that code. Phorum does not need sessions, your code does. Therefore move the if (!session_id()) session_start(); call to the start of your function to fill up the $_SESSION variable.
  • Is $_SESSION['userfid'] really the user id of your own authenticated user or did you just copy that block of code from one of the examples?
  • Calling mysql_free_result after the return call is kind of useless.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce



Edited 1 time(s). Last edit at 06/23/2008 02:21AM by Maurice Makaay.
Re: Frustration with Integration of Phorum
June 23, 2008 11:38PM
Maurice, thanks for the advice. However, I don't even think the hook is working. It doesn't matter what I put in here... it just doesn't do anything!

<?php
if (! defined("PHORUM")) return;
function ext($sessions)
{
if (!session_id()) session_start();
require_once('Connections/connUser.php');
mysql_select_db($database_connUser, $connUser);
$query_rsUser = sprintf("SELECT user_name FROM tbl_roster WHERE email_address=%s AND game_id=%s", GetSQLValueString($_SESSION['MM_Username'], "text"),GetSQLValueString($_SESSION['MM_CurrentGame'], "int"));
$rsUser = mysql_query($query_rsUser, $connUser) or die(mysql_error());
$row_rsUser = mysql_fetch_assoc($rsUser);
$user_name = $row_rsUser['user_name'];
$query_rsGame = sprintf("SELECT game_admin FROM tbl_game WHERE game_id=%s", GetSQLValueString($_SESSION['MM_CurrentGame'], "int"));
$rsGame = mysql_query($query_rsGame, $connUser) or die(mysql_error());
$row_rsGame = mysql_fetch_assoc($rsGame);
$game_admin_email = $row_rsGame['game_admin'];
if ($game_admin_email == $_SESSION['MM_Username'])) {
$admin = 1;} else {$admin = 0;}
$query_rsPhorum = sprintf("SELECT user_id FROM phorum_users WHERE username=%s", GetSQLValueString($user_name, "text"));
$rsPhorum = mysql_query($query_rsPhorum, $connUser) or die(mysql_error());
$row_rsPhorum = mysql_fetch_assoc($rsPhorum);
$user = array(
"user_id" => $row_rsPhorum['user_id'],
"username" => $user_name,
"email" => $_SESSION['MM_Username'],
"admin" => $admin,
"active" => PHORUM_USER_ACTIVE
);
phorum_api_user_save($user);
$phorum_user_id = $row_rsPhorum['user_id'];
$sessions[PHORUM_SESSION_SHORT_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_LONG_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_ADMIN] = $admin;
return $sessions;
}
?>
Re: Frustration with Integration of Phorum
June 24, 2008 02:15AM
If the hook is not run at all (you can do a quick sure check by calling a die("OHNO!") or so at the start of your function), then:
  • your module info.txt might not have a correct hook: line
  • you might not have enabled the module in your admin.php Modules page


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: Frustration with Integration of Phorum
June 24, 2008 09:10AM
I sort of attacked the problem of external login from the other end using session_create to add a cookie to the user. The userinterface program is called by the main site program for adding/editing users and groups and for the login. This isn't a drop in solution, but shows how it can be done. The require_once(userinterface.php) is run just before these functions are called (this changes the active database to the phorum database) and then setting up the normal database is done at the beginnig of all other pages on the site.



Edited 1 time(s). Last edit at 06/24/2008 10:48AM by DavidVB.
Attachments:
open | download - userinterface.zip (2.6 KB)
Re: Frustration with Integration of Phorum
June 24, 2008 09:42AM
Maurice,

I've checked the info.txt file to ensure the hook line is correct and I've ensured that the module is activated. I've even put an echo statement in the function to see if it gets called out. Nothing :( Where in the phorum program is the hook? Is it in the user.php file?

DavidVB,
I'll take a look at your solution today.

Thanks and take care,
-saniko
Re: Frustration with Integration of Phorum
June 24, 2008 09:46AM
Please pack your module and post it here, so we can take a look at it.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: Frustration with Integration of Phorum
June 24, 2008 05:10PM
Maurice,

I've packed it into a zip file and attached it here.

Can you take a look? I think I'm very close, but it's just not hooking!

Take care,
-saniko
Attachments:
open | download - external_auth.zip (1.2 KB)
Re: Frustration with Integration of Phorum
June 24, 2008 05:13PM
You named you directory "external_auth" and your module file "auth.php". Rename either one to make them use the same name, e.g. "auth.php" -> "external_auth.php". Once they match, you will have lift off.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: Frustration with Integration of Phorum
June 24, 2008 05:56PM
Ah. I am an idiot. Thanks!
Sorry, only registered users may post in this forum.

Click here to login