phpFreeChat and phpBB3: Better Group Identifying

When comparing all of the free or open-source software available for providing chat on a website that I have seen so far, I have personally found phpFreeChat to be one of the more impressive options. Though a new version is in the works (2.x), the first branch (1.x) is the most stable and contains the most features as of this moment.

Of those features:

  • the ability for anyone to create rooms (if enabled)
  • op status for administrating, as well as the ability to identify chatters by their credentials from various other site software (such as phpBB)
  • an assortment of theme options that are included, as well as the ability to customize with your own
  • private messaging

I don’t need to explain a great deal about phpFreeChat. You can learn almost anything you need to know about it from its official website and forums. However, what I did not find there and thought useful is a better way to identify chatters for granting op status based on their phpBB group(s). I typically use phpBB as a board in most scenarios.

Here is a copy of the basic chat setup from the phpFreeChat website for integrating with phpBB and using its user information for chatters:

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}
?>
<?php

require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Quick chat";
$params["nick"] = $user->data['username_clean']; 
$params['firstisadmin'] = false;
if ($user->data['group_id'] == 4 OR $user->data['group_id'] == 5) // Admins and Moderators
{
$params["isadmin"] = true; // Do what you want cause a pirate is free, you are a pirate 😉
}

$params["serverid"] = md5(__FILE__); // calculate a unique id for this chat
$params["debug"] = false;
$chat = new phpFreeChat( $params );

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Chicken Talk Chat Room</title>  
 </head>
 <body>
<div class="content">
  <?php $chat->printChat(); ?>
</div>

</body></html>

I have highlighted the lines that grant op status based on the user’s board group.

The method I now use was found by searching the phpBB boards for a way to determine whether a user is in a particular group. You may think this is what is already done in the phpFreeChat example of using phpBB to identify chatters to grant op status above, but what is actually being done there is the chat script is determining if one of the groups being checked is the user’s primary group. Because group assignment is an important way to identify a board user’s main role, it is not uncommon for users to be in multiple groups so that they can access multiple functions. For example, a particular user may be a board administrator, but his primary group is support – because that is his main function on the board. In this case, in order for the user to get op status in the chat, you would have to have the check be looking for the support group, because that is the group ID that will be seen here since it is the user’s primary group. Even though the admin group is being checked, only one group ID is being passed for the user in this case, and it is the user’s primary group only.

For the method that I prefer, follow these steps:

  1. Require the includes/functions_user.php file.
    Find: 

    require($phpbb_root_path . 'common.' . $phpEx);

    and add the following after it:

    require($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  2. Use the group_memberships() function to identify an array of all of the user’s assigned groups.
    Find: 

    if ($user->data['group_id'] == 4 OR $user->data['group_id'] == 5) // Admins and Moderators
    {
    $params["isadmin"] = true; // Do what you want cause a pirate is free, you are a pirate 😉
    }

    and replace it with the following:

    if (group_memberships(array(4,5),$user->data['user_id'],true))
    {
       $params["isadmin"] = true; // Do what you want cause a pirate is free, you are a pirate 😉
    }

As that example is now, it is still only checking for the Administrator and Global moderator groups. However, you can add a group ID to the array within the parentheses and it will check for that group as well. Unlike the original method, this check will pass so long as the user is within one of the listed groups – not requiring the group to be the user’s primary group.

Also, if you wish to not require chatters to be registered and logged in on your board, you can do the following to allow guest chatting:

  1. Find:
    if ($user->data['user_id'] == ANONYMOUS)
    {
    	login_box('', $user->lang['LOGIN']);
    }

    and replace it with the following:

    $RequireLogin = false; // if true, will require chatter to be logged in on the phpBB board 
    if ($user->data['user_id'] == ANONYMOUS)
    {
       if($RequireLogin)
       {
          login_box('', $user->lang['LOGIN']);
       }
    }

    Changing $RequireLogin to true will allow you to quickly revert back to requiring registeration and login without having to replace code again.

  2. Find:
    $params["nick"] = $user->data['username_clean'];

    and replace it with the following:

    if ($user->data['user_id'] != ANONYMOUS) $params["nick"] = $user->data['username_clean']; // phpBB nick used

    If this isn’t done and the $RequireLogin variable is set to false, the guest will be given the nick ANONYMOUS. Otherwise, they are either prompted for a nick upon loading the chat or given a randomly generated guest nick – depending on your phpFreeChat configuration for that.