Posted by Muzzy on May 23, 2008 at 8:21am
I've made patch which allows to import existing users from phpBB 3 to Drupal 5.5
This hook must be added to phpbb.module to import users.
Brief overview: on login try phpbb_auth() (implementation of hook_auth()) tries to check if user with such name exists in phpBB user database and if it exists and entered password matches password in phpBB database it copies account information to Drupal users table and loggs user in. Then imported user can login using default Drupal auth system even if phpBB integration doesn't exist anymore.
<?php
/**
* Implementation of hook_auth
* @author Alek$ aka Muzzy
*/
function phpbb_auth($username, $password, $server)
{
global $phpbbcfg;
require_once('hash.inc');
$query = "SELECT * FROM {$phpbbcfg['db_users']} "
." WHERE <code>username_clean</code> = '" . _phpbb_username_clean($username) . "'";
$res = db_query($query);
if(db_num_rows($res) != 0)
{
$row = db_fetch_array($res);
if( phpbb_check_hash($password, $row['user_password']) )
{
if(!($user = user_load(array('uid' => $row['user_id']))))
{
$user_fields = user_fields();
$array = array(
'name' => $username,
'pass' => $password,
'init' => $row['user_email'],
'mail' => $row['user_email'],
'status' => 1,
'created' => time(),
'uid' => $row['user_id'],
);
// Consider users created by an administrator as already logged in, so
// anonymous users can view the profile (if allowed).
if (empty($array['access']) && user_access('administer users')) {
$array['access'] = time();
}
// Note, we wait with saving the data column to prevent module-handled
// fields from being saved there. We cannot invoke hook_user('insert') here
// because we don't have a fully initialized user object yet.
foreach ($array as $key => $value) {
switch ($key) {
case 'pass':
$fields[] = $key;
$values[] = md5($value);
$s[] = "'%s'";
break;
case 'name':
$fields[] = $key;
$values[] = db_escape_string($value);
$s[] = "'%s'";
break;
case 'uid': case 'mode': case 'sort':
case 'threshold': case 'created': case 'access':
case 'login': case 'status':
$fields[] = $key;
$values[] = $value;
$s[] = "%d";
break;
default:
if (substr($key, 0, 4) !== 'auth' && in_array($key, $user_fields)) {
$fields[] = $key;
$values[] = $value;
$s[] = "'%s'";
}
break;
}
}
db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values);
// Build the initial user object.
$user = user_load(array('uid' => $array['uid']));
// Build and save the serialized data field now
$data = array();
foreach ($array as $key => $value) {
if ((substr($key, 0, 4) !== 'auth') && ($key != 'roles') && (!in_array($key, $user_fields)) && ($value !== NULL)) {
$data[$key] = $value;
}
}
db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid);
// Save user roles (delete just to be safe).
if (is_array($array['roles'])) {
db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']);
foreach (array_keys($array['roles']) as $rid) {
if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid);
}
}
}
// Build the finished user object.
$user = user_load(array('uid' => $array['uid']));
}
return true;
}
}
else
{
return false;
}
}
?>
Comments
This looks pretty good,
This looks pretty good, thanks :) Will include soon.
Just going through the code,
Just going through the code, looks great, I'm going to give it a try but one thing to note for others, you need to make sure your drupal_users_uid variable in drupal_sequences table is set to a number greater than the highest user id from your phpbb_users table, otherwise you will have conflicts and cry...
great work!
Instructions?
Hello,
thanks for the solution, Muzzy. Looks promising. However, I was unable to install it. Could you please provide some install instruction for those of us who are less skilled in PHP or Drupal? Where exactly do I put this code?
I tried simply copy&pasting it to the bottom of module.php, but it resulted in some kind of error:
warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\rpgsk2\modules\phpbb\phpbb.module:458) in C:\Program Files\xampp\htdocs\rpgsk2\includes\session.inc on line 97.Line 458 is the beginning of the new code: "<?php"
So figured I should't end the PHP tag in the middle and begin another one right after. I deleted this part of the code:
"?>
<?php"
.
This stopped the error. But the code doesn't seem to be working, I still can't log in as a user who already exists in PhpBB - nothing changes.
What did I do wrong?
Thanks in advance,
Jiri
EDIT: Oh, damn. I've just noticed this is a solution for the 5.x version, while I'm using the 6.x version. I suppose I'm boned, am I not?