Index Forums Developer’s Forum PHPbb3 SSO Integration

This topic contains 0 replies, has 1 voice, and was last updated by  Towhid 2 years, 11 months ago.

  • Author
    Posts
  • #1153

    Towhid
    Keymaster

    here is a code I wrote  a while back for Question2Answer’s Single Sign On integration with PHPbb3. unfortunately project was discontinued and I didn’t have the chance to test it on live site, but as I remembered it worked very well in my localhost. here is the code:

    <?php
    /*
    	PHPbb3 integration code
    	Developer: Towhid Nategheian - TowhidN.com
    	qa-themes.com
    	Licence: GPL3
    */
    	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
    		header('Location: ../');
    		exit;
    	}
    
    	function qa_get_mysql_user_column_type()
    	{
    
    		return 'MEDIUMINT';
    
    	}
    
    	function qa_get_login_links($relative_url_prefix, $redirect_back_to_url)
    	{
    
    		return array(
    			'login' => 'http://phpbb3_forum.com/ucp.php?mode=login&#039;,
    			'register' => 'http://phpbb3_forum.com/ucp.php?mode=register&#039;,
    			'logout' => 'http://phpbb3_forum.com/ucp.php?mode=logout&#039;
    		);
    
    	}
    
    	function qa_get_logged_in_user()
    	{
    		define('IN_PHPBB', true);
    		$phpbb_root_path = '../';
    		$phpEx = substr(strrchr(__FILE__, '.'), 1);
    		require($phpbb_root_path . 'includes/startup.' . $phpEx);
    		require($phpbb_root_path . 'config.' . $phpEx);
    
    		require($phpbb_root_path . 'includes/acm/acm_' . 'file' . '.' . $phpEx);
    		require($phpbb_root_path . 'includes/cache.' . $phpEx);
    		require($phpbb_root_path . 'includes/template.' . $phpEx);
    		require($phpbb_root_path . 'includes/session.' . $phpEx);
    		require($phpbb_root_path . 'includes/auth.' . $phpEx);
    
    		require($phpbb_root_path . 'includes/functions.' . $phpEx);
    		require($phpbb_root_path . 'includes/functions_content.' . $phpEx);
    
    		require($phpbb_root_path . 'includes/constants.' . $phpEx);
    		require($phpbb_root_path . 'includes/db/' . 'mysqli' . '.' . $phpEx);
    
    		global $db;
    		$cache		= new cache();
    		$db			= new $sql_db();
    		$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined('PHPBB_DB_NEW_LINK') ? PHPBB_DB_NEW_LINK : false);
    		$config = $cache->obtain_config();
    
    		$session_id	= request_var($config['cookie_name'] . '_sid', '', false, true);
    		$sql = 'SELECT u.*, s.*
    		FROM ' . SESSIONS_TABLE . ' s, ' . USERS_TABLE . " u
    		WHERE s.session_id = '" . $db->sql_escape($session_id) . "'
    			AND u.user_id = s.session_user_id";
    		$result = $db->sql_query($sql);
    		$data = $db->sql_fetchrow($result);
    		$db->sql_freeresult($result);
    		if ( isset($data["username"]) ){
    			if ( $data["user_type"] == 3)
    				$level = QA_USER_LEVEL_ADMIN;
    			else 
    				$level = QA_USER_LEVEL_BASIC;
    			return array(
    				'userid' => $data["user_id"],
    				'publicusername' => $data["username"],
    				'email' => $data["user_email"],
    				'level' => $level
    			);
    
    		}
    		else
    			return null;
    	}
    
    	function qa_get_user_email($userid)
    	{
    
    		global $db;
    		$cache		= new cache();
    		$config = $cache->obtain_config();
    		$sql = 'SELECT u.*
    			FROM ' . USERS_TABLE . " u
    			WHERE  
    			u.user_id ='" . $userid . "'";
    		$result = $db->sql_query($sql);
    		$data = $db->sql_fetchrow($result);
    		$db->sql_freeresult($result);
    		return $data ["user_email"];
    
    	}
    
    	function qa_get_userids_from_public($publicusernames)
    	{
    
    		$publictouserid=array();
    
    		if (count($publicusernames)) {
    			global $db;
    			$cache		= new cache();
    			$config = $cache->obtain_config();
    
    			$qa_db_connection=qa_db_connection();
    
    			$escapedusernames=array();
    			foreach ($publicusernames as $publicusername)
    				$escapedusernames[]="'".mysql_real_escape_string($publicusername, $qa_db_connection)."'";
    
    			$results=mysql_query(
    				'SELECT username, user_id FROM ' . USERS_TABLE . ' WHERE username IN ('.implode(',', $escapedusernames).')',
    				$qa_db_connection
    			);
    
    			while ($result=mysql_fetch_assoc($results))
    				$publictouserid[$result['username']]=$result['user_id'];
    		}
    
    		return $publictouserid;
    	}
    
    	function qa_get_public_from_userids($userids)
    	{
    
    		$useridtopublic=array();
    
    		if (count($userids)) {
    			global $db;
    			$cache		= new cache();
    			$config = $cache->obtain_config();
    
    			$qa_db_connection=qa_db_connection();
    
    			$escapeduserids=array();
    			foreach ($userids as $userid)
    				$escapeduserids[]="'".mysql_real_escape_string($userid, $qa_db_connection)."'";
    
    			$results=mysql_query(
    				'SELECT username, user_id FROM ' . USERS_TABLE . ' WHERE user_id IN ('.implode(',', $escapeduserids).')',
    				$qa_db_connection
    			);
    
    			while ($result=mysql_fetch_assoc($results))
    				$useridtopublic[$result['user_id']]=$result['username'];
    		}
    
    		return $useridtopublic;
    	}
    
    	function qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)
    	{
    
    	//	By default, show the public username linked to the Q2A profile page for the user
    
    		$publicusername=$logged_in_user['publicusername'];
    
    		return '<a href="'.htmlspecialchars($relative_url_prefix.'user/'.urlencode($publicusername)).
    			'" class="qa-user-link">'.htmlspecialchars($publicusername).'</a>';
    	}
    
    	function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
    	{
    
    	//	By default, show the public username linked to the Q2A profile page for each user
    
    		$useridtopublic=qa_get_public_from_userids($userids);
    
    		$usershtml=array();
    
    		foreach ($userids as $userid) {
    			$publicusername=$useridtopublic[$userid];
    
    			$usershtml[$userid]=htmlspecialchars($publicusername);
    
    			if ($should_include_link)
    				$usershtml[$userid]='<a href="'.htmlspecialchars($relative_url_prefix.'user/'.urlencode($publicusername)).
    					'" class="qa-user-link">'.$usershtml[$userid].'</a>';
    		}
    
    		return $usershtml;
    
    	}
    
    	function qa_avatar_html_from_userid($userid, $size, $padding)
    	{
    		return null; // show no avatars by default
    	}
    
    	function qa_user_report_action($userid, $action)
    	{
    		// do nothing by default
    	}
    
    /*
    	Omit PHP closing tag to help avoid accidental output
    */

    the only code you will need to change are lines 28,29, and 30 to replace phpbb3_forum.com with URL of your PHPbb3 site’s root directory.

    for more information about Q2A integration and how you can use this code visit Q2A’s official guide about external integrations(single sign-on):

    http://question2answer.org/single-sign-on.php

You must be logged in to reply to this topic.