Topic: WordPress PunBB Complete integration
Hi all,
We have wanted to integrate punBB with WordPress and found these
http://punbb.informer.com/forums/viewtopic.php?id=16433
http://www.rkblog.rk.edu.pl/w/p/punbb-a … on-plugin/
Most of credit goes to the creator, we just modified it to suit our need (in fact it was not working for us, but we have changed some code so it works). This is for WP 2.7, punBB 1.3 latest
Our new code follow
<?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;
// wywołaj 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.'\')');
}
}
?>
Cheers if it helps anyone.
Best Regards,
Tien (http://codeandmore.com).