phpbb2 會員功能修改~

Posted on 2006-04-10 12:31:57 by darkhero

因為一個專案需要一個討論區,但是又沒有要另外製作太特殊的功能,所以就要直接拿 phpbb2 來用。

但是基本上不會用原來 phpbb2 的會員註冊功能,因為原來的網站有自己的會員系統,所以目前最麻煩的事就是要整合兩個系統的會員資料。

目前想到最簡單得作法是,當原來的會員系統會員註冊的時候,同步寫入一筆新會員資料到 phpbb2 ,同時關閉掉 phpbb2 的會員註冊功能,這樣應該可以達到一個簡單的 Single Sign On 的功能。

[@more@]

於是寫了一個簡單的函式,處理討論區新增會員的功能。



/**
* 新增 phpbb2 使用者
*
* @param string $username
* @param string $password
* @param string $email
*/
function add_phpbb2_user($username,$password,$email){
// 建立 phpbb2 資料庫連線物件
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
include_once($phpbb_root_path."config.php");
include_once($phpbb_root_path."extension.inc");
include_once($phpbb_root_path."includes/constants.php");
include_once($phpbb_root_path."includes/db.php");

// auth OK
$sql = "SELECT user_id, username, user_password, user_active, user_level FROM " . USERS_TABLE .
" WHERE username = '" . str_replace("'", "''", $username) . "'";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Error in obtaining userdata', '', __LINE__, __FILE__, $sql);
}
if( !($row = $db->sql_fetchrow($result)) ) {
// no data found, first login
$sql = "SELECT MAX(user_id) AS total FROM " . USERS_TABLE;
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
}

if ( !($row = $db->sql_fetchrow($result)) )
{
message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE_, $sql);
}
$user_id = $row['total'] + 1;

$sql = "INSERT INTO " . USERS_TABLE . " (" .
"user_id," .
"username," .
"user_regdate," .
"user_password," .
"user_email," .
"user_icq," .
"user_website," .
"user_occ," .
"user_from," .
"user_interests," .
"user_sig," .
"user_sig_bbcode_uid," .
"user_avatar," .
"user_avatar_type," .
"user_viewemail," .
"user_aim," .
"user_yim," .
"user_msnm," .
"user_attachsig," .
"user_allowsmile," .
"user_allowhtml," .
"user_allowbbcode," .
"user_allow_viewonline," .
"user_notify," .
"user_notify_pm," .
"user_popup_pm," .
"user_timezone," .
"user_dateformat," .
"user_lang," .
"user_style," .
"user_level," .
"user_allow_pm," .
"user_active," .
"user_actkey" .
") VALUES (" .
"$user_id," .
"'" . str_replace("'", "''", $username) . "'," .
time() . "," .
"'" . str_replace("'", "''", md5($password)) . "'," .
"'" . str_replace("'", "''", $email) . "'," .
"'" . str_replace("'", "''", "") . "'," .
"'" . str_replace("'", "''", "") . "'," .
"'" . str_replace("'", "''", "") . "'," .
"'" . str_replace("'", "''", "") . "'," .
"'" . str_replace("'", "''", "") . "'," .
"'" . str_replace("'", "''", "") . "'," .
"''," .
"''," .
"0," .
"0," .
"'" . str_replace("'", "''", str_replace(' ', '+', "")) . "'," .
"'" . str_replace("'", "''", "") . "'," .
"'" . str_replace("'", "''", "") . "'," .
"1," .
"1," .
"1," .
"1," .
"1," .
"0," .
"0," .
"1," .
"8.00," .
"'" . str_replace("'", "''", "D M d, Y g:i a") . "'," .
"'" . str_replace("'", "''", "chinese_traditional_taiwan") . "'," .
"1," .
"0," .
"1," .
"1," .
"''" .
")";
echo $sql;
if ( !($result = $db->sql_query($sql, BEGIN_TRANSACTION)) )
{
message_die(GENERAL_ERROR, 'Could not insert data into users table', '', __LINE__, __FILE__, $sql);
}

$sql = "INSERT INTO " . GROUPS_TABLE . " (group_name, group_description, group_single_user, group_moderator)" .
" VALUES ('', 'Personal User', 1, 0)";

echo $sql;
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not insert data into groups table', '', __LINE__, __FILE__, $sql);
}

$group_id = $db->sql_nextid();

$sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)" .
" VALUES ($user_id, $group_id, 0)";

echo $sql;
if( !($result = $db->sql_query($sql, END_TRANSACTION)) )
{
message_die(GENERAL_ERROR, 'Could not insert data into user_group table', '', __LINE__, __FILE__, $sql);
}
}
}

另外修改討論區中的 includes/usercp_register.php,將 $mode=register 的情況都轉回原來的會員註冊系統。

參考資料:OpenLDAP 整合方案

Tags: PHP+MySQL PHP網站建構

加入書籤: 收藏這篇文章到HEMiDEMi

迴響

目前沒有人留言.

我要留言!

壹 加 壹拾參 等於