Location: PHPKode > projects > PHPBB 3 integration > phpbb-3-integration-0-1/synchronizephpBB3.bl.php
<?php
/**
* This class is useful to integrate your existing site with phpBB3
* @version 0.1
* @copyright (C) 2009 Rajesh Bhatia. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author Rajesh Bhatia <hide@address.com>
**/

class SynchronizephpBB3
{
	 public function __construct($tableName)
	 {
	 	$this->tableMembers = $tableName;// member(users) table of your existing site
	 
	 }
	 /***************************************Fuunction description*********************************/
	 /* This function perform insert, update operation depending on the parameters passed to this function
	 	here I am assuming you are using the default prefix(phpbb_) for phpBB3 tables, kindly replace prefix if you are using other		 prefix
	 */
	  /***************************************End Fuunction description*********************************/
	 private function phpbb_synchronize_user( $UserID, $Email, $Password, $IsActive,$register=false )
	 {
		  global $_objDB;// replace this with yours DB resource, I am using mysqli, you need to change this if you are using other DB server
		  $message = array();
		  $librium_default_group_id = 2;
		  static $phpbb_config;
		  static $group_config;
		  if ( is_array( $phpbb_config ) == false )
		  {
			 $phpbb_config = array( );
			 $query = "SELECT config_name, config_value FROM phpbb_config";
			 $ident = $_objDB->query($query);
			// $ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) );
			 while ( $myrow = mysqli_fetch_assoc( $ident ) )
			 {
				$phpbb_config[ $myrow[ "config_name" ] ] = $myrow[ "config_value" ];
			 }
		  }
		  if ( is_array( $group_config ) == false )
		  {
			 $query = sprintf( "SELECT group_id, group_colour, group_rank, group_avatar, group_avatar_type, group_avatar_width, group_avatar_height FROM phpbb_groups WHERE group_id = %d", $librium_default_group_id );
			 $ident = $_objDB->query($query);
		   //  $ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) );
			 $group_config = mysqli_fetch_assoc( $ident );
		  }
		  $query = sprintf( "SELECT username FROM %s WHERE member_id = %d",$this->tableMembers, $UserID );
		  $ident = $_objDB->query($query);
		  $myrow = mysqli_fetch_assoc( $ident );
		  $bbuser_newid = $UserID + 100;
		  $bbuser_email = strtolower( $Email );
		  $bbuser_passw = $Password;
		  $bbuser_class = $IsActive ? 0 : 1;
		  $bbuser_login = strtolower($myrow["username"]);//its the hack, phpbb is accepting only lower case, in my case, username is unique, use the commented process below to generate username if username is not unique in DB
		  
		  /*$bbuser_login = explode( "@", $bbuser_login );
		  $bbuser_login = $bbuser_login[ 0 ];
		  $bbuser_login = preg_replace( "/[^a-z0-9]/i", "", $bbuser_login );
		  $bbuser_login = $bbuser_login . $bbuser_newid;*/
		  
		   $query = sprintf( "SELECT username, username_clean, user_type FROM phpbb_users WHERE user_id = %d", $bbuser_newid );
		   $ident = $_objDB->query($query);
		 // $ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) );
		  $myrow = mysqli_fetch_assoc( $ident );
		  if ( count($myrow)== 0 )
		  {
		  	 $dataarray = array( );
			 $dataarray[ "user_id"                  ] = $bbuser_newid;
			 $dataarray[ "user_email"               ] = $bbuser_email;
			 $dataarray[ "user_email_hash"          ] = crc32( $bbuser_email ) . strlen( $bbuser_email );
			 if($register === true){//if you are passing password, not as md5 at the time of registration(update profile)
			 	$dataarray[ "user_password"            ] = @md5($bbuser_passw);
			 }
			 else{
			 	$dataarray[ "user_password"            ] = $bbuser_passw;
			 }
			 $dataarray[ "username"                 ] = $bbuser_login;
			 $dataarray[ "username_clean"           ] = $bbuser_login;
			 $dataarray[ "group_id"                 ] = $librium_default_group_id;
			 $dataarray[ "user_type"                ] = $bbuser_class;
			 $dataarray[ "user_colour"              ] = $group_config[ "group_colour"        ];
			 $dataarray[ "user_rank"                ] = $group_config[ "group_rank"          ];
			 $dataarray[ "user_avatar"              ] = $group_config[ "group_avatar"        ];
			 $dataarray[ "user_avatar_type"         ] = $group_config[ "group_avatar_type"   ];
			 $dataarray[ "user_avatar_width"        ] = $group_config[ "group_avatar_width"  ];
			 $dataarray[ "user_avatar_height"       ] = $group_config[ "group_avatar_height" ];
			 $dataarray[ "user_lang"                ] = $phpbb_config[ "default_lang"        ];
			 $dataarray[ "user_timezone"            ] = $phpbb_config[ "board_timezone"      ];
			 $dataarray[ "user_dst"                 ] = $phpbb_config[ "board_dst"           ];
			 $dataarray[ "user_dateformat"          ] = $phpbb_config[ "default_dateformat"  ];
			 $dataarray[ "user_style"               ] = $phpbb_config[ "default_style"       ];
			 $dataarray[ "user_actkey"              ] = "";
			 $dataarray[ "user_allow_massemail"     ] = 1;
			 $dataarray[ "user_allow_pm"            ] = 1;
			 $dataarray[ "user_allow_viewemail"     ] = 1;
			 $dataarray[ "user_allow_viewonline"    ] = 1;
			 $dataarray[ "user_emailtime"           ] = 0;
			 $dataarray[ "user_full_folder"         ] = -3;
			 $dataarray[ "user_inactive_reason"     ] = $bbuser_class ? 3 : 0;
			 $dataarray[ "user_inactive_time"       ] = $bbuser_class ? time( ) : 0;
			 $dataarray[ "user_interests"           ] = "";
			 $dataarray[ "user_ip"                  ] = "";
			 $dataarray[ "user_last_privmsg"        ] = 0;
			 $dataarray[ "user_lastmark"            ] = time( );
			 $dataarray[ "user_lastpage"            ] = "";
			 $dataarray[ "user_lastpost_time"       ] = 0;
			 $dataarray[ "user_lastvisit"           ] = 0;
			 $dataarray[ "user_message_rules"       ] = 0;
			 $dataarray[ "user_new_privmsg"         ] = 0;
			 $dataarray[ "user_notify"              ] = 0;
			 $dataarray[ "user_notify_pm"           ] = 1;
			 $dataarray[ "user_notify_type"         ] = 0;
			 $dataarray[ "user_occ"                 ] = "";
			 $dataarray[ "user_options"             ] = 895;
			 $dataarray[ "user_pass_convert"        ] = 0;
			 $dataarray[ "user_permissions"         ]    = "";
			 $dataarray[ "user_posts"               ] = 0;
			 $dataarray[ "user_sig"                 ] = "";
			 $dataarray[ "user_sig_bbcode_bitfield" ] = "";
			 $dataarray[ "user_sig_bbcode_uid"      ] = "";
			 $dataarray[ "user_unread_privmsg"      ] = 0;
			 $dataarray[ "user_regdate"             ] = time( );
			 $dataarray[ "user_passchg"             ] = time( );
			 $dataarray[ "user_form_salt"           ] = substr( md5( microtime( ) ), 4, 16 );
			 $query = "";
			 foreach ( $dataarray as $column => $value )
			 {
			 	if($value == "admin"){
					continue;
				}
				if ( $query == "" )
				{
				   $query .= "INSERT phpbb_users SET ";
				}
				else
				{
				   $query .= ", ";
				}
				$query .= sprintf( "%s = '%s'", $column, addslashes( $value ) );
			 }
			  $ident = $_objDB->query($query);
			 $message[ ] = sprintf( "Created user <b>%s</b>", trim( $bbuser_login ) );
			 $query = sprintf( "
				INSERT INTO phpbb_user_group
				( group_id, user_id, group_leader, user_pending )
				VALUES
				( %d, %d, 0, 0 )
				", $librium_default_group_id, $bbuser_newid
			 );
			  $ident = $_objDB->query($query);
		   //  $ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) );
			 $message[ ] = "&rsaquo; Assigned user to registered users group";
		  }
		  else
		  {
			 $dataarray = array( );
			 $dataarray[ "user_email"      ] = $bbuser_email;
			 $dataarray[ "user_email_hash" ] = crc32( $bbuser_email ) . strlen( $bbuser_email );
			 $dataarray[ "user_password"   ] = md5( $bbuser_passw );
			 if ( $myrow[ "username" ] == "" || $myrow[ "username_clean" ] == "" )
			 {
				$dataarray[ "username"       ] = $bbuser_login;
				$dataarray[ "username_clean" ] = $bbuser_login;
			 }
			 if ( $myrow[ "user_type" ] != $bbuser_class )
			 {
				$dataarray[ "user_type"            ] = $bbuser_class;
				$dataarray[ "user_inactive_reason" ] = $bbuser_class ? 3 : 0;
				$dataarray[ "user_inactive_time"   ] = $bbuser_class ? time( ) : 0;
			 }
			 $query = "";
			 foreach ( $dataarray as $column => $value )
			 {
				if ( $query == "" )
				{
				   $query .= "UPDATE phpbb_users SET ";
				}
				else
				{
				   $query .= ", ";
				}
				$query .= sprintf( "%s = '%s'", $column, addslashes( $value ) );
			 }
			 $query .= sprintf( " WHERE user_id = %d", $bbuser_newid );
			 $ident = $_objDB->query($query);
			// $ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) );
			 if ( mysqli_affected_rows($_objDB) )
			 {
				$message[ ] = sprintf( "Updated user <b>%s</b>",($bbuser_login ));
			 }
		  }
		  return implode( "<br>\n", $message );
	   }
	   /***************************************Fuunction description*********************************/
	 /* This function calls phpbb_synchronize_user function of this class to import all users of you existing site to phpBB3 db*/
	  /***************************************End Fuunction description*********************************/
	   public function ImportUsers()
	   {
	   		global $_objDB;
			$message = array( );
            $query = "SELECT member_id, email, password, is_active FROM {$this->tableMembers}";
			$ident = $_objDB->query($query);
          	//$ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) );
			  while ( $myrow = mysqli_fetch_assoc( $ident ) )
			  {
			  
				 $result = $this->phpbb_synchronize_user( $myrow["member_id"], $myrow["email"], $myrow["password"], $myrow["is_active"] );
				 if ( $result )
				 {
					$message[ ] = $result;
				 }
			  }
	   }
	    /***************************************Fuunction description*********************************/
	 /* This function calls phpbb_synchronize_user function of this class to insert a record in phpBB3 user tables when new user registers*/
	  /***************************************End Fuunction description*********************************/
	   public function SynchronizeRegisterProcess($UserId,$Email,$Password)
	   {
	   		$result = $this->phpbb_synchronize_user($UserId,$Email,$Password, 1,true );

	   }
	   /***************************************Fuunction description*********************************/
	 /* This function calls phpbb_synchronize_user function of this class to update a record when user of your site will update his/her profile*/
	  /***************************************End Fuunction description*********************************/
	   public function SynchronizeUpdateProcess($UserId,$Email,$Password)
	   {
	   		$result = $this->phpbb_synchronize_user($UserId,$Email,$Password, 1 );
	   }
	   /***************************************Fuunction description*********************************/
	 /* This function will manage session between phpBB3 forum and your existing site*/
	  /***************************************End Fuunction description*********************************/
	   public function SessionManage($folderName='')
	   {
	   		global $_objDB;
	   	 	$query = sprintf(
			   "SELECT username FROM phpbb_users WHERE user_id = %d + 100",
			   $_SESSION["user_id"]
			);
			$ident = $_objDB->query($query);
			//$ident = mysql_query( $query, $cn1 ) or die( mysql_error( $cn1 ) );
			$myrow = mysqli_fetch_assoc( $ident );
		
			if ( count($myrow) == 0)
			{
			   header( "Location:"._WWWROOT."/login" );//replace this with your login URL
			   exit( 0 );
			}
			define( "IN_PHPBB", true );
			if($folderName != ''){
				$phpbb_root_path = _PATH."/".$folderName."/";
			}
			else{
				$phpbb_root_path = "./";
			}
			$phpEx = substr( strrchr( __FILE__, "." ), 1 );
			require( $phpbb_root_path . "common." . $phpEx );
			require( $phpbb_root_path . "includes/functions_user." . $phpEx );
			require( $phpbb_root_path . "includes/functions_module." . $phpEx );
			$user->session_kill();
			$user->session_begin();
		
			#
			# hack that i used from phpbb code
			#
			$password = base64_decode($_SESSION['ps_string']);
			$result = $auth->login( strtolower($myrow["username"]),$password);
			
			if ( $result[ "status" ] == LOGIN_SUCCESS )
			{
			   if ( defined( "IN_CHECK_BAN" ) && $result[ "user_row" ][ "user_type" ] != USER_FOUNDER )
			   {
				  return;
			   }
			   $successurl = $_GET[ "successurl" ];
			   if ( $successurl == "" )
			   {
			   		if($folderName == ""){
						$successurl = _WWWROOT."/forum/index.php";//replace this with your phpBB3 installation
					}
					else{
						$successurl = _WWWROOT."/".$folderName."/index.php";
					}
			   }
				$successurl = reapply_sid( $successurl );
				 $successurl = str_replace( "&amp;", "&", $successurl );
			   header( "Location: $successurl" );
			   exit( 0 );
			}
			else
			{
			   header( "Location:"._WWWROOT."/login" );//replace this with your login URL
			   exit( 0 );
			}

	   
	   }
	   /*********************This function is used to delete user from phpBB3 users table******/
	 public function Delete($pId)
	 {
	 	global $_objDB;
		$userId=explode(",",$pId);
		for($i=0; $i<count($userId) ; $i++){
			$userId[$i] = $userId[$i] + 100;
		}
		$pIds = implode(",",$userId);
		try
		{
			$sql = "DELETE FROM phpbb_users where user_id in (".$pIds.")";
			$_objDB->query($sql);
			$sqlGroup = "DELETE FROM phpbb_user_group where user_id in (".$pIds.")";
			$_objDB->query($sqlGroup);		
		}
		catch(Exception $e){return array();}
	 
	 
	 }
	   
 }
   ?>
Return current item: PHPBB 3 integration