Location: PHPKode > scripts > Lightweight Club Calendar > lc-calendar-0.9.4/drivers/user/phpbb2.class.php
<?php
/**
* PHPBB UserDriver for LCC
*
* PHP Version 4, 5
*
* @author <hide@address.com>
* @copyright Copyright (c) 2006, Benedikt Hallinger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, 51 Franklin St, Fifth Floor,
* Boston, MA  02110-1301  USA
*/

/**
* UserDriver for PHPBB 2.x
*
* This UserDriver enables LCC to use users defined from the
* well known forum software PHPBB.
* Users that are logged in at PHPBB can use the calendar without the need
* to log in separately; a valid login in the forum is sufficient.
* The driver is known to work with 2.0.22 but should work with any 2.x.
*
* Configuration is as follows:
* <code>
* $config = array(
*      'phpbb_root'   => './some/path/phpbb/',   // Where is your phpbb installed? (absolute or relative to your script that runs LCC)
* );
* </code>
*
* The driver returns levels depending on PHPBB privilegues; for details when which levels are
* returned, refer to the documentation of the method {@link getLevel()} below.
*
* [INSTALLATION NOTES]
* Usually, this driver works out of the box if it is configured properly.
* However, the driver works only, if LCC has access to the session of PHPBB. This means, that if you use cookies
* to establish a session for PHPBB (comon case), you must configure your PHPBB to set the cookie path to a
* location LCC can access. Usually the cookie path is the installation path of PHPBB (e.g. /phpbb/), so you
* must either install LCC bebeath the PHPBB installation directory (e.g. phpbb/lcc/, not recommendet!) or
* alter the cookie path of the PHPBB cookies to a common path.
* If LCC is instaled at "somepath/lcc/" and PHPBB at "somepath/phpbb/" then the cookie path should be "somepath/".
* Usually this means to simply set the cookie path to "/" but this may imply a security problem with
* software installed beside PHPBB and lcc, so please check this.
* You can alter the PHPBB cookie path in the "Administration Panel"->"Configuration".
*/
class LCC_UserDriver_PHPBB2 extends LCC_UserDriver
{
	/**
	* Default config of the driver
	*
	* @var array
	*/
	var $config = array (
			'phpbb_root' => ''  // Where is your phpbb installed? (absolute or relative to your script that runs LCC)
		);
	
	
	/**
	* Userdata of phpbb user, set by configure()
	*
	* @access private
	* @var array|false
	*/
	var $_phpbb_user = false;
	
	/**
	* Database object of phpBB installation
	*
	* @access private
	* @var array
	*/
	var $_phpbb_dbms = false;
	
	/**
	* Return the username of the user currently logged in phpbb
	*
	* @return false|string
	*/
	function &fetchLogin()
	{
		if ($this->_phpbb_user !== false && is_array($this->_phpbb_user)
		   && $this->_phpbb_user['session_logged_in'] && strlen($this->_phpbb_user['username']) > 0) {
			return $this->_phpbb_user['username'];
		} else {
			// not logged in
			return false;
		}
	}

	/**
	* Get the group names a user is member of
	*
	* @param string $username     Name of the user in question
	* @return false|array
	*/
	function getGroups($username)
	{
		$db =& $this->_phpbb_dbms;
		
		$sql = "SELECT ".GROUPS_TABLE.".group_name FROM ".GROUPS_TABLE.", ".USER_GROUP_TABLE.", ".USERS_TABLE." WHERE
		        ".GROUPS_TABLE.".group_id          = ".USER_GROUP_TABLE.".group_id AND
		        ".GROUPS_TABLE.".group_single_user = 0                             AND
		        ".USER_GROUP_TABLE.".user_id       = ".USERS_TABLE.".user_id       AND
		        ".USER_GROUP_TABLE.".user_pending  = 0                             AND
		        ".USERS_TABLE.".username           = '".mysql_real_escape_string($username)."'";
		if ( !($result = $db->sql_query($sql)) ) {
			return false;
		}
		
		$groups = array();
		while ($row = $db->sql_fetchrow($result)) {
			array_push($groups, $row['group_name']);
		}
		$db->sql_freeresult($result);
		
		return $groups;
	}

	/**
	* Get the group names the user leads
	*
	* This is similar to getGroups() but returns only the names of the
	* groups, the user is a leader of.
	*
	* @param string $username     Name of the user in question
	* @return false|array
	*/
	function getGroupsLeaded($username)
	{
		$db =& $this->_phpbb_dbms;
		
		$sql = "SELECT ".GROUPS_TABLE.".group_name FROM ".GROUPS_TABLE.", ".USERS_TABLE." WHERE
		".GROUPS_TABLE.".group_moderator = ".USERS_TABLE.".user_id AND
		".GROUPS_TABLE.".group_single_user = 0 AND
		".USERS_TABLE.".username = '".mysql_real_escape_string($username)."'";
		
		if ( !($result = $db->sql_query($sql)) ) {
			return false;
		}
		
		$groups = array();
		while ($row = $db->sql_fetchrow($result)) {
			array_push($groups, $row['group_name']);
		}
		$db->sql_freeresult($result);
		
		return $groups;
	}
	
	/**
	* Get the group members
	*
	* This method returns an array of usernames the group contains.
	*
	* @param string $groupname     Name of the group in question
	* @return false|array
	*/
	function getGroupMembers($groupname)
	{
		$db =& $this->_phpbb_dbms;
		
		$sql = "SELECT ".USERS_TABLE.".username FROM ".USERS_TABLE.", ".GROUPS_TABLE.", ".USER_GROUP_TABLE." WHERE
		".USERS_TABLE.".user_id = ".USER_GROUP_TABLE.".user_id AND
		".GROUPS_TABLE.".group_id = ".USER_GROUP_TABLE.".group_id AND
		".GROUPS_TABLE.".group_name = '".mysql_real_escape_string($groupname)."'";
		
		if ( !($result = $db->sql_query($sql)) ) {
			return false;
		}
		
		$members = array();
		while ($row = $db->sql_fetchrow($result)) {
			array_push($members, $row['username']);
		}
		$db->sql_freeresult($result);
		
		return $members;
	}

	/**
	* Get the "level" of a user
	*
	* Levels are as following (complies to 'how_to_write_a_driver.txt'):
	*   Level ==   0: Unauthorized users (anonymous)
	*   Level  >   0: Authorized users
	*   Level >=  50: Moderators or something like that
	*   Level == 100: Administrators
	*
	* @param string $username     Name of the user in question
	* @return false|int
	*/
	function getLevel($username)
	{
		if (isset($this->_phpbb_user['user_level'])) {
			if ($this->_phpbb_user['user_level'] == ADMIN) {
				$level = 100;
			} elseif ($this->_phpbb_user['user_level'] == MOD) {
				$level = 50;
			} elseif ($this->_phpbb_user['user_level'] == ANONYMOUS || $this->_phpbb_user['user_level'] == DELETED) {
				$level = 0;
			} else {
				$level = 10; // Simple user
			}

			return $level;
		} else {
			return false;
		}
	}
	
	
	
	/**
	* Custom configuration of driver
	*
	* LCC calls this method after the driver has been configured.
	* It tries to fetch neccessary stuff from phpbb and ensures
	* that the phpbb rootpath is set to a sane location
	*/
	function configure()
	{
		define('IN_PHPBB', true);
		if (strlen($this->config['phpbb_root']) == 0 || !is_readable($this->config['phpbb_root'])) {
			die('<b>LCC_UserDriver_PHPBB2 configuration error:</b> phpbb_root path not found or not readable!');
		} else {
			if (!in_array(substr($this->config['phpbb_root'], -1), array('/', '\\'))) $this->config['phpbb_root'] .= '/';
			
			// include phpbb stuff
			require $this->config['phpbb_root'] . 'extension.inc';
			require $this->config['phpbb_root'] . 'common.'.$phpEx;
			
			// get database config of phpBB installation
			if (is_a($db, 'sql_db')) {
				$this->_phpbb_dbms = $db;
			} else {
				// if this is not true, we should not proceed any further...
				die('<b>LCC_UserDriver_PHPBB2 configuration error:</b> PHPBB DB object is not compatible!');
			}
			
			// Store necessary phpBB variables in global context
			$GLOBALS['db']              = $db;
			$GLOBALS['board_config']    = $board_config;
			$GLOBALS['lang']            = $lang;
			$GLOBALS['phpbb_root_path'] = $this->config['phpbb_root'];
			$GLOBALS['phpEx']           = $phpEx;
			$GLOBALS['user_ip']         = $user_ip;
			

			// fetch userdata of logged in (or not) user
			$userdata = session_pagestart($user_ip, PAGE_INDEX);
			init_userprefs($userdata);
			$GLOBALS['userdata'] = $userdata;

			// store userdata for further reference
			$this->_phpbb_user = $userdata;
		}
	}
}
?>
Return current item: Lightweight Club Calendar