WordPress.org

Forums

[plugin: punBB integrator] : use it with external database (3 posts)

  1. grosbouff
    Member
    Posted 6 years ago #

    Hi, I'm trying to use fluxBB and WordPress together. But they are two separate databases...
    I found this "punBB integrator" plugin to share users (profile, login/logout...) that i've try to adapt with an external database, but it doesn't work yet.
    Maybe 'cause of the queries...
    Here's the original plugin by Riklaunim :

    <?php
    /*
    Plugin Name: punBB integrator
    Plugin URI: http://www.rkblog.rk.edu.pl
    Description: Allows WordPress to manager punBB users - login/logout/register/password change etc.
    Version: 0.0.07.2008.01
    Author: Riklaunim
    Author URI: http://www.rkblog.rk.edu.pl
    */
    add_action('profile_update', 'punbb_profile_update');
    add_action('wp_logout', 'punbb_wp_logout');
    add_action('wp_authenticate', 'punbb_wp_authenticate', 1, 2);
    add_action('user_register', 'punbb_user_register');
    add_action('activate_jl-punbb/jl-punbb.php', 'punbb_sync_tables');
    
    define('PUNPATH', './forum'); // path to punbb folder
    define('PUNPREFIX', 'pun'); // punBB table prefix
    define('LANGUAGE', 'English'); // punBB language name, English, etc.
    
    function forum_hash($str, $salt)
    {
        return sha1($salt.sha1($str));
    }
    
    function punbb_sync_tables()
        {
        global $wpdb;
        // copy users from WP to punBB that doesn't have account on punBB
        $q = $wpdb->get_results("SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (SELECT username FROM ".PUNPREFIX."users)");
        foreach($q as $u)
            {
            // give forum admin to the WP admin.
            IF($u->ID == 1)
                {
                $gid = 1;
                }
            else
                {
                $gid = 3;
                }
            $salt = md5(sha1(time().$u->user_login));
            $salt = substr($salt, 0,11);
            $wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$u->user_login.'\', '.$gid.', \'BRAK\', \''.$u->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().', \''.$salt.'\')');
            }
        // turn off emails for "dectivated" accounts on forum
        $wpdb->query('UPDATE '.PUNPREFIX.'users SET email_setting = 2 WHERE username NOT IN (SELECT user_login FROM '.$wpdb->users.') AND id > 1');
        }
    
    function punbb_profile_update($id)
        {
        global $wpdb;
        $wpuser = $wpdb->get_row("SELECT user_login, user_email FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");
        $wpdb->query("UPDATE ".PUNPREFIX."users SET email='".$wpuser->user_email."' WHERE username = '".$wpuser->user_login."'");
        }
    function punbb_wp_logout()
        {
        include PUNPATH.'/config.php';
        setcookie($cookie_name, NULL, time()-3600, '/', '', '0');
        }
    function punbb_wp_authenticate($user_login, $user_pass)
        {
        global $wpdb;
        // wywolaj przy logowaniu jak masz dane
        IF($user_login and $user_pass and strlen($user_login) > 1 and strlen($user_pass) > 1)
            {
            $wpuser = $wpdb->get_row("SELECT id,user_pass FROM ".$wpdb->users." WHERE user_login = '".mysql_real_escape_string($user_login)."' LIMIT 1");
            if(wp_check_password($user_pass, $wpuser->user_pass, $wpuser->id))
                {
                $user = $wpdb->get_row("SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1");
                include PUNPATH.'/config.php';
                /*
                punBB uses sha1, wordpress md5. We have to cheat a bit. If the sha1 hash-password in punBB is "BRAK" (look at punbb_user_register)
                or it doesn't match sha1(password from good authentication) then we update it :)
                */
                IF($user->password == 'BRAK' OR sha1($user_pass) != $user->password)
                    {
                    $wpdb->query("UPDATE ".PUNPREFIX."users SET password='".sha1($user_pass)."' WHERE username = '".mysql_real_escape_string($user_login)."'");
                    $user->password = sha1($user_pass);
                    }
                $expire = time() + 31536000;
    
                //here we can get many informations
                $userInfoArray = $wpdb->get_row("SELECT id, group_id, password, salt FROM ".PUNPREFIX."users WHERE username='".mysql_real_escape_string($user_login)."'", "ARRAY_A");
                $salt = $userInfoArray['salt'];
                $form_password_hash = forum_hash($user_pass, $salt);
                $base64 = base64_encode($user->id.'|'.$user->password.'|'.$expire.'|'.sha1($user->salt.$user->password.forum_hash($expire, $user->salt)));
                if (version_compare(PHP_VERSION, '5.2.0', '>='))
                    {
                    setcookie($cookie_name, $base64, $expire, $cookie_path, $cookie_domain, $cookie_secure, true);
                    }
                else
                    {
                    setcookie($cookie_name, $base64, $expire, $cookie_path.'; HttpOnly', $cookie_domain, $cookie_secure);
                    }
                }
             }
        }
    function punbb_user_register($id)
        {
        global $wpdb;
        $wpuser = $wpdb->get_row("SELECT * FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");
        $user = $wpdb->get_row("SELECT id FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($wpuser->user_login)."' LIMIT 1");
        // user already exists in punbb
        IF($user->id)
            {
            $wpdb->query("UPDATE ".PUNPREFIX."users SET password='BRAK' WHERE username = '".mysql_real_escape_string($wpuser->user_login)."'");
            }
        // user does not exists
        else
            {
            $salt = md5(sha1(time()));
            $salt = substr($salt, 0,11);
            $wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$wpuser->user_login.'\', 3, \'BRAK\', \''.$wpuser->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().',\''.$salt.'\')');
            }
        }
    
    ?>

    Here's my new (and non-working) version :
    (all the modifications are into the ///EXTERNAL DB//// tags)

    <?php
    /*
    Plugin Name: punBB integratorEXTERNAL FLUXBB DATABASE MOD
    Plugin URI: http://www.rkblog.rk.edu.pl
    Description: Allows WordPress to manager punBB users - login/logout/register/password change etc.
    Version: 0.0.07.2008.01
    Author: Riklaunim + Gordie Lachance ()EXTERNAL DATABASE MOD
    Author URI: http://www.rkblog.rk.edu.pl
    */
    add_action('profile_update', 'punbb_profile_update');
    add_action('wp_logout', 'punbb_wp_logout');
    add_action('wp_authenticate', 'punbb_wp_authenticate', 1, 2);
    add_action('user_register', 'punbb_user_register');
    add_action('activate_jl-punbb/jl-punbb.php', 'punbb_sync_tables');
    
    define('PUNPATH', './fluxBB'); // path to punbb folder
    define('PUNPREFIX', 'fluxBB_'); // punBB table prefix
    define('LANGUAGE', 'French'); // punBB language name, English, etc.
    
    ///EXTERNAL DB////
    $external_db=true;
    $external_db_host='localhost';
    $external_db_user='user';
    $external_db_pwd='pwd';
    $external_db_name='db';
    
    function runSQL($rsql) {
    	$dbcnx = @mysql_connect($external_db_host,$external_db_user,$external_db_pwd,true);
    	if (!$dbcnx) {
    	echo '<p>Unable to connect to the database server at this time.</p>';
    	exit();
    	}
    	mysql_select_db($external_db_name, $dbcnx);
    	$result = mysql_query($rsql) or die ('test');
    	return $result;
    	mysql_close($connect);
    }
    ///EXTERNAL DB////
    
    function forum_hash($str, $salt)
    {
        return sha1($salt.sha1($str));
    }
    
    function punbb_sync_tables()
        {
        global $wpdb;
        // copy users from WP to punBB that doesn't have account on punBB
    
    	///EXTERNAL DB////
    	if ($external_db==true) {
    		$sql = "SELECT username FROM ".PUNPREFIX."users";
    		if ($result = runSQL($sql)) {
    			$fluxbb_users= array();
    			while ($row = mysql_fetch_array($result)) {
    				$fluxbb_users[]=$row['username'];
    			}
    			$fluxbb_users_list = implode(",", $fluxbb_users);
    		}
    
    		$query = "SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (".$fluxbb_users_list.")";
    
    	}else {
    		$query = "SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (SELECT username FROM ".PUNPREFIX."users)";
    
    	}
    	$q = $wpdb->get_results($query);
    	//BBEFORE:
    	//$q = $wpdb->get_results("SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (SELECT username FROM ".PUNPREFIX."users)");
    	///EXTERNAL DB////
    
        foreach($q as $u)
            {
            // give forum admin to the WP admin.
            IF($u->ID == 1)
                {
                $gid = 1;
                }
            else
                {
                $gid = 3;
                }
            $salt = md5(sha1(time().$u->user_login));
            $salt = substr($salt, 0,11);
    
    		///EXTERNAL DB////
    		$sql = 'INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$u->user_login.'\', '.$gid.', \'BRAK\', \''.$u->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().', \''.$salt.'\')';
    
    		if ($external_db==true) {
    			$result = runSQL($sql);
    		}else {
    			$wpdb->query($sql);
    
    		}
    		//BBEFORE:
            //$wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$u->user_login.'\', '.$gid.', \'BRAK\', \''.$u->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().', \''.$salt.'\')');
    		///EXTERNAL DB////
            }
        // turn off emails for "dectivated" accounts on forum
        $wpdb->query('UPDATE '.PUNPREFIX.'users SET email_setting = 2 WHERE username NOT IN (SELECT user_login FROM '.$wpdb->users.') AND id > 1');
        }
    
    function punbb_profile_update($id)
        {
        global $wpdb;
        $wpuser = $wpdb->get_row("SELECT user_login, user_email FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");
    
    	///EXTERNAL DB////
    	$sql = "UPDATE ".PUNPREFIX."users SET email='".$wpuser->user_email."' WHERE username = '".$wpuser->user_login."'";
    	if ($external_db==true) {
    		$result = runSQL($sql);
    	}else {
    		$wpdb->query($sql);
    
    	}
    	//BBEFORE:
    	//$wpdb->query("UPDATE ".PUNPREFIX."users SET email='".$wpuser->user_email."' WHERE username = '".$wpuser->user_login."'");
    	///EXTERNAL DB////
        }
    function punbb_wp_logout()
        {
        include PUNPATH.'/config.php';
        setcookie($cookie_name, NULL, time()-3600, '/', '', '0');
        }
    function punbb_wp_authenticate($user_login, $user_pass)
        {
        global $wpdb;
        // wywolaj przy logowaniu jak masz dane
        IF($user_login and $user_pass and strlen($user_login) > 1 and strlen($user_pass) > 1)
            {
            $wpuser = $wpdb->get_row("SELECT id,user_pass FROM ".$wpdb->users." WHERE user_login = '".mysql_real_escape_string($user_login)."' LIMIT 1");
            if(wp_check_password($user_pass, $wpuser->user_pass, $wpuser->id))
                {
    			///EXTERNAL DB////
    			$sql = "SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1";
    			if ($external_db==true) {
    				if ($result = runSQL($sql)) {
    					$user = mysql_fetch_object($result);
    				}
    			}else {
    				$user = $wpdb->get_row("SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1");
    			}
    			//BBEFORE:
    			//$user = $wpdb->get_row("SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1");
    			///EXTERNAL DB////
    
                include PUNPATH.'/config.php';
                /*
                punBB uses sha1, wordpress md5. We have to cheat a bit. If the sha1 hash-password in punBB is "BRAK" (look at punbb_user_register)
                or it doesn't match sha1(password from good authentication) then we update it :)
                */
                IF($user->password == 'BRAK' OR sha1($user_pass) != $user->password)
                    {
    				///EXTERNAL DB////
    				$sql = "UPDATE ".PUNPREFIX."users SET password='".sha1($user_pass)."' WHERE username = '".mysql_real_escape_string($user_login)."'";
    				if ($external_db==true) {
    					$result = runSQL($sql);
    				}else{
    					$wpdb->query($sql);
    				}
    				//BEFORE::
    				//$wpdb->query($sql);
    				///EXTERNAL DB////
    
                    $user->password = sha1($user_pass);
                    }
                $expire = time() + 31536000;
    
                //here we can get many informations
    
    				///EXTERNAL DB////
    				$sql = "SELECT id, group_id, password, salt FROM ".PUNPREFIX."users WHERE username='".mysql_real_escape_string($user_login)."'";
    				if ($external_db==true) {
    				if ($result = runSQL($sql)) {
    					$userInfoArray = mysql_fetch_array($result);
    				}
    				}else{
    					 $userInfoArray = $wpdb->get_row($sql, "ARRAY_A");
    				}
    				//BEFORE::
    				//$userInfoArray = $wpdb->get_row("SELECT id, group_id, password, salt FROM ".PUNPREFIX."users WHERE username='".mysql_real_escape_string($user_login)."'", "ARRAY_A");
    				///EXTERNAL DB////
    
                $salt = $userInfoArray['salt'];
                $form_password_hash = forum_hash($user_pass, $salt);
                $base64 = base64_encode($user->id.'|'.$user->password.'|'.$expire.'|'.sha1($user->salt.$user->password.forum_hash($expire, $user->salt)));
                if (version_compare(PHP_VERSION, '5.2.0', '>='))
                    {
                    setcookie($cookie_name, $base64, $expire, $cookie_path, $cookie_domain, $cookie_secure, true);
                    }
                else
                    {
                    setcookie($cookie_name, $base64, $expire, $cookie_path.'; HttpOnly', $cookie_domain, $cookie_secure);
                    }
                }
             }
        }
    function punbb_user_register($id)
        {
        global $wpdb;
    
    	///EXTERNAL DB////
    	$sql = "SELECT * FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1";
    	if ($external_db==true) {
    		if ($result = runSQL($sql)) {
    			$wpuser = mysql_fetch_object($result);
    		}
    	}else {
    		$wpuser =$wpdb->get_row($sql);
    	}
    	//BBEFORE:
    	//$wpuser = $wpdb->get_row("SELECT * FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");
    	///EXTERNAL DB////
    
    	///EXTERNAL DB////
    	$sql = "SELECT id FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($wpuser->user_login)."' LIMIT 1";
    	if ($external_db==true) {
    		if ($result = runSQL($sql)) {
    			$user = mysql_fetch_object($result);
    		}
    	}else {
    		$user = $wpdb->get_row($sql);
    	}
    	//BBEFORE:
    	//$user = $wpdb->get_row("SELECT id FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($wpuser->user_login)."' LIMIT 1");
    	///EXTERNAL DB////
    
        // user already exists in punbb
        IF($user->id)
            {
    
    		///EXTERNAL DB////
    		$sql = "UPDATE ".PUNPREFIX."users SET password='BRAK' WHERE username = '".mysql_real_escape_string($wpuser->user_login)."'";
    
    		if ($external_db==true) {
    			$result = runSQL($sql);
    		}else {
    			$wpdb->query($sql);
    		}
    		//BBEFORE:
            //$wpdb->query("UPDATE ".PUNPREFIX."users SET password='BRAK' WHERE username = '".mysql_real_escape_string($wpuser->user_login)."'");
    		///EXTERNAL DB////
    
            }
        // user does not exists
        else
            {
            $salt = md5(sha1(time()));
            $salt = substr($salt, 0,11);
    
    		///EXTERNAL DB////
    		$sql = 'INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$wpuser->user_login.'\', 3, \'BRAK\', \''.$wpuser->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().',\''.$salt.'\')';
    
    		if ($external_db==true) {
    			$result = runSQL($sql);
    		}else {
    			$wpdb->query($sql);
    		}
    		//BBEFORE:
            //$wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$wpuser->user_login.'\', 3, \'BRAK\', \''.$wpuser->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().',\''.$salt.'\')');
    		///EXTERNAL DB////
            }
        }
    
    ?>
  2. grosbouff
    Member
    Posted 6 years ago #

    some corrections; but it doesn't work yet : all functions are redeclared at the plugin activations; giving fatal errors. I don't know why...

    [code]
    <?php
    /*
    Plugin Name: punBB integrator with external DB
    Plugin URI: http://www.rkblog.rk.edu.pl
    Description: Allows WordPress to manager punBB users - login/logout/register/password change etc.
    Version: 0.0.07.2008.01
    Author: Riklaunim + Gordie Lachance (external DB Mod)
    Author URI: http://www.rkblog.rk.edu.pl
    */
    add_action('profile_update', 'punbb_profile_update');
    add_action('wp_logout', 'punbb_wp_logout');
    add_action('wp_authenticate', 'punbb_wp_authenticate', 1, 2);
    add_action('user_register', 'punbb_user_register');
    register_activation_hook(__FILE__, 'punbb_sync_tables'); //when activating

    define('PUNPATH', get_bloginfo('url').'/fluxBB'); // path to punbb folder
    define('PUNPREFIX', 'fluxBB_'); // punBB table prefix
    define('LANGUAGE', 'French'); // punBB language name, English, etc.

    ///EXTERNAL DB////
    define('EXTERNAL_DB', true);
    define('DB2_NAME', '**');
    define('DB2_USER', '**');
    define('DB2_PASSWORD', '**');
    define('DB2_HOST', '**');
    $fluxBBdb = new wpdb(DB2_USER, DB2_PASSWORD, DB2_NAME, DB2_HOST );
    ///EXTERNAL DB////

    function forum_hash($str, $salt)
    {
    return sha1($salt.sha1($str));
    }

    function punbb_sync_tables()
    {
    global $wpdb,$fluxBBdb;
    // copy users from WP to punBB that doesn't have account on punBB

    ///EXTERNAL DB////
    if (EXTERNAL_DB==true) {
    $fluxbb_users=array();
    $flux_query = "SELECT username FROM ".PUNPREFIX."users";
    $flux_results = $fluxBBdb->get_results($flux_query);
    foreach ($flux_results as $username) {
    $fluxbb_users[]=$username->username;
    }
    $fluxbb_users_list = implode(",", $fluxbb_users);
    $query = "SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (".$fluxbb_users_list.")";
    }else {
    $query = "SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (SELECT username FROM ".PUNPREFIX."users)";
    }
    $q = $wpdb->get_results($query);
    //BBEFORE:
    //$q = $wpdb->get_results("SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (SELECT username FROM ".PUNPREFIX."users)");
    ///EXTERNAL DB////

    foreach($q as $u)
    {
    // give forum admin to the WP admin.
    IF($u->ID == 1)
    {
    $gid = 1;
    }
    else
    {
    $gid = 3;
    }
    $salt = md5(sha1(time().$u->user_login));
    $salt = substr($salt, 0,11);

    ///EXTERNAL DB////
    $sql = 'INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$u->user_login.'\', '.$gid.', \'BRAK\', \''.$u->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().', \''.$salt.'\')';

    print_r($sql);
    exit;

    if (EXTERNAL_DB==true) {
    $fluxBBdb->query($sql);
    }else {
    $wpdb->query($sql);
    }
    //BBEFORE:
    //$wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$u->user_login.'\', '.$gid.', \'BRAK\', \''.$u->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().', \''.$salt.'\')');
    ///EXTERNAL DB////
    }
    ///EXTERNAL DB////
    $sql2 = 'UPDATE '.PUNPREFIX.'users SET email_setting = 2 WHERE username NOT IN (SELECT user_login FROM '.$wpdb->users.') AND id > 1';
    // turn off emails for "dectivated" accounts on forum
    if (EXTERNAL_DB==true) {
    $fluxBBdb->query($sql2);
    }else {
    $wpdb->query($sql2);
    }
    }

    function punbb_profile_update($id)
    {
    global $wpdb,$fluxBBdb;
    $wpuser = $wpdb->get_row("SELECT user_login, user_email FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");

    ///EXTERNAL DB////
    $sql = "UPDATE ".PUNPREFIX."users SET email='".$wpuser->user_email."' WHERE username = '".$wpuser->user_login."'";
    if (EXTERNAL_DB==true) {
    $fluxBBdb->query($sql);
    }else {
    $wpdb->query($sql);

    }
    //BBEFORE:
    //$wpdb->query("UPDATE ".PUNPREFIX."users SET email='".$wpuser->user_email."' WHERE username = '".$wpuser->user_login."'");
    ///EXTERNAL DB////
    }
    function punbb_wp_logout()
    {
    include PUNPATH.'/config.php';
    setcookie($cookie_name, NULL, time()-3600, '/', '', '0');
    }
    function punbb_wp_authenticate($user_login, $user_pass)
    {
    global $wpdb,$fluxBBdb;
    // wywolaj przy logowaniu jak masz dane
    IF($user_login and $user_pass and strlen($user_login) > 1 and strlen($user_pass) > 1)
    {
    $wpuser = $wpdb->get_row("SELECT id,user_pass FROM ".$wpdb->users." WHERE user_login = '".mysql_real_escape_string($user_login)."' LIMIT 1");
    if(wp_check_password($user_pass, $wpuser->user_pass, $wpuser->id))
    {
    ///EXTERNAL DB////
    $sql = "SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1";

    if (EXTERNAL_DB==true) {
    $user = $fluxBBdb->get_row($sql);
    }else {
    $user = $wpdb->get_row($sql);
    }
    //BBEFORE:
    //$user = $wpdb->get_row("SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1");
    ///EXTERNAL DB////

    include PUNPATH.'/config.php';
    /*
    punBB uses sha1, wordpress md5. We have to cheat a bit. If the sha1 hash-password in punBB is "BRAK" (look at punbb_user_register)
    or it doesn't match sha1(password from good authentication) then we update it :)
    */
    IF($user->password == 'BRAK' OR sha1($user_pass) != $user->password)
    {
    ///EXTERNAL DB////
    $sql = "UPDATE ".PUNPREFIX."users SET password='".sha1($user_pass)."' WHERE username = '".mysql_real_escape_string($user_login)."'";
    if (EXTERNAL_DB==true) {
    $fluxBBdb->query($sql);
    }else{
    $wpdb->query($sql);
    }
    //BEFORE::
    //$wpdb->query($sql);
    ///EXTERNAL DB////

    $user->password = sha1($user_pass);
    }
    $expire = time() + 31536000;

    //here we can get many informations

    ///EXTERNAL DB////
    $sql = "SELECT id, group_id, password, salt FROM ".PUNPREFIX."users WHERE username='".mysql_real_escape_string($user_login)."'";
    if (EXTERNAL_DB==true) {
    $userInfoArray = $fluxBBdb->get_row($sql, "ARRAY_A");
    }else{
    $userInfoArray = $wpdb->get_row($sql, "ARRAY_A");
    }
    //BEFORE::
    //$userInfoArray = $wpdb->get_row("SELECT id, group_id, password, salt FROM ".PUNPREFIX."users WHERE username='".mysql_real_escape_string($user_login)."'", "ARRAY_A");
    ///EXTERNAL DB////

    $salt = $userInfoArray['salt'];

    $form_password_hash = forum_hash($user_pass, $salt);
    $base64 = base64_encode($user->id.'|'.$user->password.'|'.$expire.'|'.sha1($user->salt.$user->password.forum_hash($expire, $user->salt)));
    if (version_compare(PHP_VERSION, '5.2.0', '>='))
    {
    setcookie($cookie_name, $base64, $expire, $cookie_path, $cookie_domain, $cookie_secure, true);
    }
    else
    {
    setcookie($cookie_name, $base64, $expire, $cookie_path.'; HttpOnly', $cookie_domain, $cookie_secure);
    }
    }
    }
    }
    function punbb_user_register($id)
    {
    global $wpdb,$fluxBBdb;

    $wpuser = $wpdb->get_row("SELECT * FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");

    ///EXTERNAL DB////
    $sql = "SELECT id FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($wpuser->user_login)."' LIMIT 1";
    if (EXTERNAL_DB==true) {
    $user = $fluxBBdb->get_row($sql);
    }else {
    $user = $wpdb->get_row($sql);
    }
    //BBEFORE:
    //$user = $wpdb->get_row("SELECT id FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($wpuser->user_login)."' LIMIT 1");
    ///EXTERNAL DB////

    // user already exists in punbb
    IF($user->id)
    {

    ///EXTERNAL DB////
    $sql = "UPDATE ".PUNPREFIX."users SET password='BRAK' WHERE username = '".mysql_real_escape_string($wpuser->user_login)."'";

    if (EXTERNAL_DB==true) {
    $fluxBBdb->query($sql);
    }else {
    $wpdb->query($sql);
    }
    //BBEFORE:
    //$wpdb->query("UPDATE ".PUNPREFIX."users SET password='BRAK' WHERE username = '".mysql_real_escape_string($wpuser->user_login)."'");
    ///EXTERNAL DB////

    }
    // user does not exists
    else
    {
    $salt = md5(sha1(time()));
    $salt = substr($salt, 0,11);

    ///EXTERNAL DB////
    $sql = 'INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$wpuser->user_login.'\', 3, \'BRAK\', \''.$wpuser->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().',\''.$salt.'\')';

    if (EXTERNAL_DB==true) {
    $fluxBBdb->query($sql);
    }else {
    $wpdb->query($sql);
    }
    //BBEFORE:
    //$wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$wpuser->user_login.'\', 3, \'BRAK\', \''.$wpuser->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().',\''.$salt.'\')');
    ///EXTERNAL DB////
    }
    }

    ?>
    [/code]

  3. grosbouff
    Member
    Posted 6 years ago #

    Ok, I think I finally got it.
    I had problems to fire punbb_sync_tables at plugin registration, so what I did is add a subpage in the administration panel (under settings) called "FluxBB/Wordpress Sync". When you click on it, the function is fired (I know the page is almost blank, nothing is explained...Maybe later. Anyway, the function works).
    I think all is OK for the external DB queries.
    Now I don't know if the plugin is compatible with WP2.7 since login for FluxBB don't work from WordPress. Maybe a cookie problem.
    Here's my script.
    If the original author could have a look on it, ...

    <?php
    /*
    Plugin Name: punBB integrator with external DB
    Plugin URI: http://www.rkblog.rk.edu.pl
    Description: Allows WordPress to manager punBB users - login/logout/register/password change etc.
    Version: 0.0.07.2008.01
    Author: Riklaunim + Gordie Lachance (external DB Mod)
    Author URI: http://www.rkblog.rk.edu.pl
    */
    add_action('profile_update', 'punbb_profile_update');
    add_action('wp_logout', 'punbb_wp_logout');
    add_action('wp_authenticate', 'punbb_wp_authenticate', 1, 2);
    add_action('user_register', 'punbb_user_register');
    //register_activation_hook(__FILE__, 'punbb_sync_tables'); //when activating
    
    define('PUNPATH', get_bloginfo('url').'/fluxBB'); // path to punbb folder
    define('PUNPREFIX', 'fluxBB_'); // punBB table prefix
    define('LANGUAGE', 'French'); // punBB language name, English, etc.
    
    ///EXTERNAL DB////
    define('EXTERNAL_DB', true);
    define('DB2_NAME', '**');
    define('DB2_USER', '**');
    define('DB2_PASSWORD', '**');
    define('DB2_HOST', '**');
    $fluxBBdb = new wpdb(DB2_USER, DB2_PASSWORD, DB2_NAME, DB2_HOST );
    ///EXTERNAL DB////
    
    add_action('admin_menu', 'fluxBB_admin_menu');
    
    function fluxBB_admin_menu() {
    	add_submenu_page('options-general.php', 'FluxBB/Wordpress Sync', 'Sync FluxBB & WordPress', 8, __FILE__, 'my_plugin_options');
    }
    
    function my_plugin_options() {
      punbb_sync_tables();
      echo '<div class="wrap">';
      ?>
      Sync done (?), check your DB.
      <?php
      echo '</div>';
    }
    
    function forum_hash($str, $salt)
    {
        return sha1($salt.sha1($str));
    }
    
    function punbb_sync_tables()
        {
        global $wpdb,$fluxBBdb;
    
        // copy users from WP to punBB that doesn't have account on punBB
    
    	///EXTERNAL DB////
    	if (EXTERNAL_DB==true) {
    		$fluxbb_users=array();
    		$flux_query = "SELECT username FROM ".PUNPREFIX."users";
    		$flux_results = $fluxBBdb->get_results($flux_query);
    		foreach ($flux_results as $username) {
    			$fluxbb_users[]="'".addslashes($username->username)."'";
    		}
    		$fluxbb_users_list = implode(",", $fluxbb_users);
    		$query = "SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (".$fluxbb_users_list.")";
    	}else {
    		$query = "SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (SELECT username FROM ".PUNPREFIX."users)";
    	}
    
    	$q = $wpdb->get_results($query);
    
    	//BBEFORE:
    	//$q = $wpdb->get_results("SELECT * FROM ".$wpdb->users." WHERE user_login NOT IN (SELECT username FROM ".PUNPREFIX."users)");
    	///EXTERNAL DB////
    
        foreach($q as $u)
            {
            // give forum admin to the WP admin.
            IF($u->ID == 1)
                {
                $gid = 1;
                }
            else
                {
                $gid = 3;
                }
            $salt = md5(sha1(time().$u->user_login));
            $salt = substr($salt, 0,11);
    
    		///EXTERNAL DB////
    		$sql = 'INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$u->user_login.'\', '.$gid.', \'BRAK\', \''.$u->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().', \''.$salt.'\')';
    
    		if (EXTERNAL_DB==true) {
    			$fluxBBdb->query($sql);
    		}else {
    			$wpdb->query($sql);
    		}
    		//BBEFORE:
            //$wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$u->user_login.'\', '.$gid.', \'BRAK\', \''.$u->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().', \''.$salt.'\')');
    		///EXTERNAL DB////
            }
    	///EXTERNAL DB////
    	$sql2 = 'UPDATE '.PUNPREFIX.'users SET email_setting = 2 WHERE username NOT IN (SELECT user_login FROM '.$wpdb->users.') AND id > 1';
        // turn off emails for "dectivated" accounts on forum
    	if (EXTERNAL_DB==true) {
    		$fluxBBdb->query($sql2);
    	}else {
    		$wpdb->query($sql2);
    	}
    
    }
    function punbb_profile_update($id)
        {
        global $wpdb,$fluxBBdb;
        $wpuser = $wpdb->get_row("SELECT user_login, user_email FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");
    
    	///EXTERNAL DB////
    	$sql = "UPDATE ".PUNPREFIX."users SET email='".$wpuser->user_email."' WHERE username = '".$wpuser->user_login."'";
    	if (EXTERNAL_DB==true) {
    		$fluxBBdb->query($sql);
    	}else {
    		$wpdb->query($sql);
    
    	}
    	//BBEFORE:
    	//$wpdb->query("UPDATE ".PUNPREFIX."users SET email='".$wpuser->user_email."' WHERE username = '".$wpuser->user_login."'");
    	///EXTERNAL DB////
        }
    function punbb_wp_logout()
        {
        include PUNPATH.'/config.php';
        setcookie($cookie_name, NULL, time()-3600, '/', '', '0');
        }
    function punbb_wp_authenticate($user_login, $user_pass)
        {
        global $wpdb,$fluxBBdb;
        // wywolaj przy logowaniu jak masz dane
        IF($user_login and $user_pass and strlen($user_login) > 1 and strlen($user_pass) > 1)
            {
            $wpuser = $wpdb->get_row("SELECT id,user_pass FROM ".$wpdb->users." WHERE user_login = '".mysql_real_escape_string($user_login)."' LIMIT 1");
            if(wp_check_password($user_pass, $wpuser->user_pass, $wpuser->id))
                {
    			///EXTERNAL DB////
    			$sql = "SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1";
    
    			if (EXTERNAL_DB==true) {
    				$user = $fluxBBdb->get_row($sql);
    			}else {
    				$user = $wpdb->get_row($sql);
    			}
    			//BBEFORE:
    			//$user = $wpdb->get_row("SELECT id, password, salt FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($user_login)."' LIMIT 1");
    			///EXTERNAL DB////
    
                include PUNPATH.'/config.php';
                /*
                punBB uses sha1, wordpress md5. We have to cheat a bit. If the sha1 hash-password in punBB is "BRAK" (look at punbb_user_register)
                or it doesn't match sha1(password from good authentication) then we update it :)
                */
                IF($user->password == 'BRAK' OR sha1($user_pass) != $user->password)
                    {
    				///EXTERNAL DB////
    				$sql = "UPDATE ".PUNPREFIX."users SET password='".sha1($user_pass)."' WHERE username = '".mysql_real_escape_string($user_login)."'";
    				if (EXTERNAL_DB==true) {
    					$fluxBBdb->query($sql);
    				}else{
    					$wpdb->query($sql);
    				}
    				//BEFORE::
    				//$wpdb->query($sql);
    				///EXTERNAL DB////
    
                    $user->password = sha1($user_pass);
                    }
                $expire = time() + 31536000;
    
                //here we can get many informations
    
    				///EXTERNAL DB////
    				$sql = "SELECT id, group_id, password, salt FROM ".PUNPREFIX."users WHERE username='".mysql_real_escape_string($user_login)."'";
    				if (EXTERNAL_DB==true) {
    					$userInfoArray = $fluxBBdb->get_row($sql, "ARRAY_A");
    				}else{
    					 $userInfoArray = $wpdb->get_row($sql, "ARRAY_A");
    				}
    				//BEFORE::
    				//$userInfoArray = $wpdb->get_row("SELECT id, group_id, password, salt FROM ".PUNPREFIX."users WHERE username='".mysql_real_escape_string($user_login)."'", "ARRAY_A");
    				///EXTERNAL DB////
    
                $salt = $userInfoArray['salt'];
    
                $form_password_hash = forum_hash($user_pass, $salt);
                $base64 = base64_encode($user->id.'|'.$user->password.'|'.$expire.'|'.sha1($user->salt.$user->password.forum_hash($expire, $user->salt)));
                if (version_compare(PHP_VERSION, '5.2.0', '>='))
                    {
                    setcookie($cookie_name, $base64, $expire, $cookie_path, $cookie_domain, $cookie_secure, true);
                    }
                else
                    {
                    setcookie($cookie_name, $base64, $expire, $cookie_path.'; HttpOnly', $cookie_domain, $cookie_secure);
                    }
                }
             }
        }
    function punbb_user_register($id)
        {
        global $wpdb,$fluxBBdb;
    
    	$wpuser = $wpdb->get_row("SELECT * FROM ".$wpdb->users." WHERE ID = ".$id." LIMIT 1");
    
    	///EXTERNAL DB////
    	$sql = "SELECT id FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($wpuser->user_login)."' LIMIT 1";
    	if (EXTERNAL_DB==true) {
    		$user = $fluxBBdb->get_row($sql);
    	}else {
    		$user = $wpdb->get_row($sql);
    	}
    	//BBEFORE:
    	//$user = $wpdb->get_row("SELECT id FROM ".PUNPREFIX."users WHERE username = '".mysql_real_escape_string($wpuser->user_login)."' LIMIT 1");
    	///EXTERNAL DB////
    
        // user already exists in punbb
        IF($user->id)
            {
    
    		///EXTERNAL DB////
    		$sql = "UPDATE ".PUNPREFIX."users SET password='BRAK' WHERE username = '".mysql_real_escape_string($wpuser->user_login)."'";
    
    		if (EXTERNAL_DB==true) {
    			$fluxBBdb->query($sql);
    		}else {
    			$wpdb->query($sql);
    		}
    		//BBEFORE:
            //$wpdb->query("UPDATE ".PUNPREFIX."users SET password='BRAK' WHERE username = '".mysql_real_escape_string($wpuser->user_login)."'");
    		///EXTERNAL DB////
    
            }
        // user does not exists
        else
            {
            $salt = md5(sha1(time()));
            $salt = substr($salt, 0,11);
    
    		///EXTERNAL DB////
    		$sql = 'INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$wpuser->user_login.'\', 3, \'BRAK\', \''.$wpuser->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().',\''.$salt.'\')';
    
    		if (EXTERNAL_DB==true) {
    			$fluxBBdb->query($sql);
    		}else {
    			$wpdb->query($sql);
    		}
    		//BBEFORE:
            //$wpdb->query('INSERT INTO '.PUNPREFIX.'users (username, group_id, password, email, email_setting, timezone, language, style, registered, registration_ip, last_visit, salt) VALUES(\''.$wpuser->user_login.'\', 3, \'BRAK\', \''.$wpuser->user_email.'\', 1, 1 , \''.LANGUAGE.'\', \'Oxygen\', '.time().', \''.strip_tags($_SERVER['REMOTE_ADDR']).'\', '.time().',\''.$salt.'\')');
    		///EXTERNAL DB////
            }
        }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.