| 1 |
<?php |
<?php |
| 2 |
// $Id: drupalvb.module,v 1.11.4.24.2.1 2009/05/29 10:22:50 sun Exp $ |
// $Id: drupalvb.module,v 1.11.4.24.2.2 2009/06/05 15:03:20 sun Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 16 |
function drupalvb_help($section) { |
function drupalvb_help($section) { |
| 17 |
switch ($section) { |
switch ($section) { |
| 18 |
case 'admin/settings#description': |
case 'admin/settings#description': |
| 19 |
return t('Allows basic integration of Drupal with a vBulletin forum.'); |
return t('Integrate your Drupal site with vBulletin forums.'); |
| 20 |
|
|
| 21 |
case 'admin/settings/drupalvb': |
case 'admin/settings/drupalvb': |
| 22 |
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
| 33 |
return theme_item_list($items); |
return theme_item_list($items); |
| 34 |
|
|
| 35 |
case 'admin/settings/drupalvb/roles': |
case 'admin/settings/drupalvb/roles': |
| 36 |
return t('Note that these settings will not immediately take effect, only if a Drupal user is created or edited.'); |
return t('Note that these settings will not immediately take effect, only after a Drupal user has been updated.'); |
| 37 |
|
|
| 38 |
case 'admin/help#drupalvb': |
case 'admin/help#drupalvb': |
| 39 |
return filter_filter('process', 2, NULL, file_get_contents(dirname(__FILE__) .'/README.txt')); |
return filter_filter('process', 2, NULL, file_get_contents(dirname(__FILE__) .'/README.txt')); |
| 108 |
); |
); |
| 109 |
|
|
| 110 |
$items[] = array( |
$items[] = array( |
|
'path' => 'drupalvb/pms', |
|
|
'callback' => 'drupalvb_private_messages', |
|
|
'access' => user_access('access content'), |
|
|
'type' => MENU_CALLBACK, |
|
|
); |
|
|
$items[] = array( |
|
| 111 |
'path' => 'drupalvb', |
'path' => 'drupalvb', |
| 112 |
'callback' => 'drupalvb_redirect', |
'callback' => 'drupalvb_redirect', |
| 113 |
'access' => variable_get('drupalvb_acct_generation', TRUE), |
'access' => variable_get('drupalvb_acct_generation', TRUE), |
| 114 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 115 |
); |
); |
| 116 |
$items[] = array( |
$items[] = array( |
| 117 |
|
'path' => 'drupalvb/pms', |
| 118 |
|
'callback' => 'drupalvb_private_messages', |
| 119 |
|
'access' => user_access('access content'), |
| 120 |
|
'type' => MENU_CALLBACK, |
| 121 |
|
); |
| 122 |
|
$items[] = array( |
| 123 |
'path' => 'drupalvb/login', |
'path' => 'drupalvb/login', |
| 124 |
'callback' => 'drupalvb_login', |
'callback' => 'drupalvb_login', |
| 125 |
'access' => TRUE, |
'access' => TRUE, |
| 203 |
return; |
return; |
| 204 |
} |
} |
| 205 |
if ($vbuser = db_fetch_array(drupalvb_db_query("SELECT userid, username, password, salt, email, joindate FROM {user} WHERE username = '%s'", drupalvb_htmlspecialchars($username)))) { |
if ($vbuser = db_fetch_array(drupalvb_db_query("SELECT userid, username, password, salt, email, joindate FROM {user} WHERE username = '%s'", drupalvb_htmlspecialchars($username)))) { |
| 206 |
// Rebuild the password. |
// Rebuild the hashed + salted password. |
| 207 |
$vbpassword = md5(md5($password) . $vbuser['salt']); |
$vbpassword = md5(md5($password) . $vbuser['salt']); |
| 208 |
if ($vbuser['password'] === $vbpassword) { |
if ($vbuser['password'] === $vbpassword) { |
| 209 |
// We have a valid vBulletin account, so try to lookup this user in the |
// We have a valid vBulletin account, so try to lookup this user in the |
| 211 |
// find a user with the given password, otherwise we wouldn't be here. |
// find a user with the given password, otherwise we wouldn't be here. |
| 212 |
// This can happen if the user has been temporarily created by |
// This can happen if the user has been temporarily created by |
| 213 |
// drupalvb_redirect(). |
// drupalvb_redirect(). |
| 214 |
if ($uid = drupalvb_user_load($vbuser['userid'])) { |
if ($uid = drupalvb_get_drupal_id('users', $vbuser['userid'])) { |
| 215 |
// Only update the password of the existing Drupal user record. |
// Only update the password of the existing Drupal user record. |
| 216 |
$account = user_load(array('uid' => $uid)); |
$account = user_load(array('uid' => $uid)); |
| 217 |
$userinfo['pass'] = $password; |
$edit['pass'] = $password; |
| 218 |
$user = user_save($account, $userinfo); |
$user = user_save($account, $edit); |
| 219 |
} |
} |
| 220 |
else { |
else { |
| 221 |
// This user is completely unknown to Drupal, register a new account. |
// This user is completely unknown to Drupal, register a new account. |
| 222 |
// Fall back on the user submitted user name if the vBulletin name |
// Fall back on the user submitted user name if the vBulletin name |
| 223 |
// contains encoded &'s. |
// contains encoded &'s. |
| 224 |
$username = (strpos($vbuser['username'], '&') === FALSE ? $vbuser['username'] : $username); |
$username = (strpos($vbuser['username'], '&') === FALSE ? $vbuser['username'] : $username); |
| 225 |
$userinfo = array( |
$edit = array( |
| 226 |
'name' => $username, |
'name' => $username, |
| 227 |
'pass' => $password, |
'pass' => $password, |
| 228 |
'mail' => $vbuser['email'], |
'mail' => $vbuser['email'], |
| 230 |
'created' => $vbuser['joindate'], |
'created' => $vbuser['joindate'], |
| 231 |
'status' => 1, |
'status' => 1, |
| 232 |
); |
); |
| 233 |
$user = user_save('', $userinfo); |
$user = user_save('', $edit); |
| 234 |
watchdog('drupalvb', t('New external user: %user.', array('%user' => $user->name)), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit')); |
watchdog('drupalvb', t('New external user: %user.', array('%user' => $user->name)), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit')); |
| 235 |
|
|
| 236 |
// Update the mapping table. |
// Update the mapping table. |
| 267 |
} |
} |
| 268 |
|
|
| 269 |
/** |
/** |
|
* Try to lookup a Drupal user account for a vBulletin user id, using the mapping |
|
|
* table. |
|
|
* |
|
|
* @param $userid |
|
|
* A vBulletin user id. |
|
|
*/ |
|
|
function drupalvb_user_load($userid) { |
|
|
return drupalvb_get_drupal_id('users', $userid); |
|
|
} |
|
|
|
|
|
/** |
|
| 270 |
* Implementation of hook_user(). |
* Implementation of hook_user(). |
| 271 |
*/ |
*/ |
| 272 |
function drupalvb_user($op, &$edit, &$account, $category = NULL) { |
function drupalvb_user($op, &$edit, &$account, $category = NULL) { |
| 280 |
case 'logout': |
case 'logout': |
| 281 |
if (variable_get('drupalvb_dual_login', TRUE)) { |
if (variable_get('drupalvb_dual_login', TRUE)) { |
| 282 |
$function = 'drupalvb_user_'. $op; |
$function = 'drupalvb_user_'. $op; |
| 283 |
return $function($account); |
$function($account); |
| 284 |
} |
} |
| 285 |
break; |
break; |
| 286 |
|
|
| 287 |
case 'validate': |
case 'validate': |
| 288 |
if ($category == 'account' && (variable_get('drupalvb_acct_generation', TRUE) || variable_get('drupalvb_acct_sync', TRUE))) { |
if ($category == 'account' && (variable_get('drupalvb_acct_generation', TRUE) || variable_get('drupalvb_acct_sync', TRUE))) { |
| 289 |
return drupalvb_user_validate(arg(1), $edit); |
drupalvb_user_validate(arg(1), $edit); |
| 290 |
} |
} |
| 291 |
break; |
break; |
| 292 |
|
|
| 293 |
case 'insert': |
case 'insert': |
| 294 |
if (variable_get('drupalvb_acct_generation', TRUE)) { |
if (variable_get('drupalvb_acct_generation', TRUE)) { |
| 295 |
return drupalvb_user_insert($account, $edit); |
drupalvb_user_synchronize($account, $edit); |
| 296 |
} |
} |
| 297 |
break; |
break; |
| 298 |
|
|
| 299 |
case 'update': |
case 'update': |
| 300 |
if (variable_get('drupalvb_acct_sync', TRUE)) { |
if (variable_get('drupalvb_acct_sync', TRUE)) { |
| 301 |
return drupalvb_user_update($account, $edit); |
drupalvb_user_synchronize($account, $edit); |
| 302 |
} |
} |
| 303 |
break; |
break; |
| 304 |
|
|
| 305 |
case 'delete': |
case 'delete': |
| 306 |
if (variable_get('drupalvb_acct_sync', TRUE)) { |
if (variable_get('drupalvb_acct_sync', TRUE)) { |
| 307 |
return drupalvb_user_delete($account); |
drupalvb_user_delete($account); |
| 308 |
} |
} |
| 309 |
break; |
break; |
| 310 |
} |
} |
| 316 |
* @see drupalvb_user() |
* @see drupalvb_user() |
| 317 |
*/ |
*/ |
| 318 |
function drupalvb_user_login($account) { |
function drupalvb_user_login($account) { |
| 319 |
$vbuser = db_fetch_array(drupalvb_db_query("SELECT u.userid, ub.liftdate FROM {user} u LEFT JOIN {userban} ub ON ub.userid = u.userid WHERE u.username = '%s'", drupalvb_htmlspecialchars($account->name))); |
$vbuser = db_fetch_array(drupalvb_db_query("SELECT u.userid, ub.liftdate FROM {user} u LEFT JOIN {userban} ub ON ub.userid = u.userid WHERE u.username = '%s' LIMIT 1", drupalvb_htmlspecialchars($account->name))); |
| 320 |
|
|
| 321 |
if (!$vbuser) { |
if (!$vbuser) { |
| 322 |
// Create vB account (if allowed to) if the user does not yet exist. |
// Create new vBulletin account (if allowed to). |
| 323 |
if (!variable_get('drupalvb_acct_generation', TRUE) || !$vbuser['userid'] = drupalvb_create_user($account, (array)$account)) { |
if (!variable_get('drupalvb_acct_generation', TRUE) || !$vbuser['userid'] = _drupalvb_create_user((array) $account)) { |
| 324 |
// Indicates duplicate username (should not happen). |
// Indicates duplicate username (should not happen). |
| 325 |
return FALSE; |
return FALSE; |
| 326 |
} |
} |
| 327 |
} |
} |
| 328 |
else if (empty($vbuser['liftdate']) || $vbuser['liftdate'] < time()) { |
else if (empty($vbuser['liftdate']) || $vbuser['liftdate'] < time()) { |
| 329 |
// Update last activity timestamp and ensure a user mapping exists. |
// Update last activity timestamp. |
| 330 |
drupalvb_update_user($account, array()); |
_drupalvb_update_user($vbuser['userid'], $account, array()); |
| 331 |
} |
} |
| 332 |
else { |
else { |
| 333 |
// User is banned: no action. |
// User is banned: no action. |
| 334 |
return FALSE; |
return FALSE; |
| 335 |
} |
} |
| 336 |
|
// Ensure that a user mapping exists. |
| 337 |
|
drupalvb_set_mapping('users', $account->uid, $vbuser['userid']); |
| 338 |
|
|
| 339 |
// Set vB user session and cookies. |
// Set vB user session and cookies. |
| 340 |
if (!drupalvb_set_login_cookies($vbuser['userid'])) { |
if (!drupalvb_set_login_cookies($vbuser['userid'])) { |
| 341 |
|
drupal_set_message(t('Login to forums failed.'), 'error'); |
| 342 |
watchdog('drupalvb', t('Login to forum failed for user %user.', array('%user' => $account->name)), WATCHDOG_ERROR); |
watchdog('drupalvb', t('Login to forum failed for user %user.', array('%user' => $account->name)), WATCHDOG_ERROR); |
| 343 |
return FALSE; |
return FALSE; |
| 344 |
} |
} |
| 356 |
// If there is no vB user mapping, then it is possible that we deal with a |
// If there is no vB user mapping, then it is possible that we deal with a |
| 357 |
// user that needs to be synchronized first (if enabled). |
// user that needs to be synchronized first (if enabled). |
| 358 |
if (!$userid && variable_get('drupalvb_acct_generation', TRUE)) { |
if (!$userid && variable_get('drupalvb_acct_generation', TRUE)) { |
| 359 |
$userid = drupalvb_update_user($account, (array) $account); |
$userid = drupalvb_synchronize_user($account); |
| 360 |
} |
} |
| 361 |
if ($userid) { |
if ($userid) { |
| 362 |
// Remove all vB cookies for current user. |
// Remove vBulletin cookies for current user. |
| 363 |
drupalvb_clear_cookies($userid); |
drupalvb_clear_cookies($userid); |
| 364 |
watchdog('drupalvb', t('Forum session closed for user %username (@uid).', array('%username' => $account->name, '@uid' => $userid))); |
watchdog('drupalvb', t('Forum session closed for user %username (@uid).', array('%username' => $account->name, '@uid' => $userid))); |
| 365 |
} |
} |
| 367 |
|
|
| 368 |
/** |
/** |
| 369 |
* Ensure a username or e-mail address does not already exist in vB. |
* Ensure a username or e-mail address does not already exist in vB. |
| 370 |
|
* |
| 371 |
|
* @see drupalvb_user() |
| 372 |
*/ |
*/ |
| 373 |
function drupalvb_user_validate($uid, &$edit) { |
function drupalvb_user_validate($uid, &$edit) { |
| 374 |
$userid = drupalvb_get_vb_id('users', $uid); |
$userid = drupalvb_get_vb_id('users', $uid); |
| 375 |
// Validate the username. |
// Validate the username. |
| 376 |
if (arg(1) == 'register' || user_access('change own username') || user_access('administer users')) { |
if (arg(1) == 'register' || user_access('change own username') || user_access('administer users')) { |
| 377 |
if (db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE userid <> %d AND LOWER(username) = LOWER('%s')", $userid, drupalvb_htmlspecialchars($edit['name']))) > 0) { |
if (db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE userid <> %d AND LOWER(username) = LOWER('%s') LIMIT 1", $userid, drupalvb_htmlspecialchars($edit['name']))) > 0) { |
| 378 |
form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name']))); |
form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name']))); |
| 379 |
} |
} |
| 380 |
} |
} |
| 381 |
|
|
| 382 |
// Validate the e-mail address. |
// Validate the e-mail address. |
| 383 |
if (db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE userid != %d AND LOWER(email) = LOWER('%s')", $userid, drupalvb_htmlspecialchars($edit['mail']))) > 0) { |
if (db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE userid <> %d AND LOWER(email) = LOWER('%s') LIMIT 1", $userid, drupalvb_htmlspecialchars($edit['mail']))) > 0) { |
| 384 |
form_set_error('mail', t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $edit['mail'], '@password' => url('user/password')))); |
form_set_error('mail', t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $edit['mail'], '@password' => url('user/password')))); |
| 385 |
} |
} |
| 386 |
} |
} |
| 387 |
|
|
| 388 |
/** |
/** |
| 389 |
* Register a new user in vB upon registration in Drupal. |
* Create or update a user in vBulletin upon registration or update in Drupal. |
| 390 |
* |
* |
| 391 |
* @see drupalvb_user() |
* @see drupalvb_user() |
| 392 |
*/ |
*/ |
| 393 |
function drupalvb_user_insert($account, $edit) { |
function drupalvb_user_synchronize($account, $edit) { |
| 394 |
global $user; |
global $user; |
| 395 |
|
|
| 396 |
if ($userid = drupalvb_create_user($account, $edit)) { |
if ($userid = drupalvb_synchronize_user($account, $edit)) { |
| 397 |
// Prevent overriding cookies of administrators. |
// Prevent overriding cookies of administrators. |
| 398 |
if ($edit['name'] === $user->name) { |
if ($account->name === $user->name) { |
| 399 |
drupalvb_set_login_cookies($userid); |
drupalvb_set_login_cookies($userid); |
| 400 |
} |
} |
| 401 |
} |
} |
| 402 |
} |
} |
| 403 |
|
|
| 404 |
/** |
/** |
|
* Update a user in vB upon update in Drupal. |
|
|
* |
|
|
* @see drupalvb_user() |
|
|
*/ |
|
|
function drupalvb_user_update($account, $edit) { |
|
|
global $user; |
|
|
|
|
|
// Update data if user exists. |
|
|
// @todo Previous code also passed vB userid to allow username changes? |
|
|
if ($userid = db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE username = '%s'", drupalvb_htmlspecialchars($account->name)))) { |
|
|
drupalvb_update_user($account, array_merge(array('name' => $account->name), $edit)); |
|
|
} |
|
|
// If not, create a new user in vB. |
|
|
else { |
|
|
if (!$userid = drupalvb_create_user($account, array_merge(array('name' => $account->name), $edit))) { |
|
|
// Indicates duplicate username (should not happen). |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
// Prevent overriding cookies of administrators. |
|
|
if ($account->name === $user->name) { |
|
|
drupalvb_set_login_cookies($userid); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
| 405 |
* Delete a user in vB upon deletion in Drupal. |
* Delete a user in vB upon deletion in Drupal. |
| 406 |
* |
* |
| 407 |
* @see drupalvb_user() |
* @see drupalvb_user() |
| 415 |
drupalvb_db_query("DELETE FROM {usertextfield} WHERE userid = %d", $userid); |
drupalvb_db_query("DELETE FROM {usertextfield} WHERE userid = %d", $userid); |
| 416 |
} |
} |
| 417 |
// Delete from mapping table. |
// Delete from mapping table. |
| 418 |
db_query("DELETE FROM {drupalvb_users} WHERE drupal_id = %d", $account->uid); |
db_query("DELETE FROM {drupalvb_map} WHERE type = 'users' AND drupal_id = %d", $account->uid); |
| 419 |
} |
} |
| 420 |
|
|
| 421 |
/** |
/** |
| 422 |
* Log in a user; menu callback. |
* vBulletin user login menu callback. |
| 423 |
* |
* |
| 424 |
* This is required for login forms embedded in vBulletin, to ensure a user is |
* This is required for login forms embedded in vBulletin, to ensure a user is |
| 425 |
* properly logged in into both systems. |
* properly logged in into both systems. |
| 444 |
// create the vBulletin account). |
// create the vBulletin account). |
| 445 |
watchdog('drupalvb', t('Login attempt failed for %user.', array('%user' => $username))); |
watchdog('drupalvb', t('Login attempt failed for %user.', array('%user' => $username))); |
| 446 |
watchdog('drupalvb', t('Session closed for %name.', array('%name' => $user->name))); |
watchdog('drupalvb', t('Session closed for %name.', array('%name' => $user->name))); |
| 447 |
// Perform the same steps as drupalvb_logout(), but allow for setting a |
// The following is similar to drupalvb_logout(), but allows for setting an |
| 448 |
// message for the (then) anonymous user. |
// explanation for the (then) anonymous user. |
| 449 |
if (module_exists('singlesignon')) { |
if (module_exists('singlesignon')) { |
| 450 |
_singlesignon_session_logout($user->uid); |
_singlesignon_session_logout($user->uid); |
| 451 |
} |
} |
| 452 |
session_destroy(); |
sess_destroy_uid($user->uid); |
| 453 |
|
sess_regenerate(); |
| 454 |
module_invoke_all('user', 'logout', NULL, $user); |
module_invoke_all('user', 'logout', NULL, $user); |
| 455 |
$user = drupal_anonymous_user(); |
$user = drupal_anonymous_user(); |
| 456 |
drupal_set_message(t('Sorry, unrecognized username or password. <a href="@password">Have you forgotten your password?</a>', array('@password' => url('user/password'))), 'error'); |
drupal_set_message(t('Sorry, unrecognized username or password. <a href="@password">Have you forgotten your password?</a>', array('@password' => url('user/password'))), 'error'); |
| 463 |
drupal_goto($redirect); |
drupal_goto($redirect); |
| 464 |
} |
} |
| 465 |
} |
} |
| 466 |
drupal_goto('user/login', 'destination='. urlencode($redirect)); |
drupal_goto('user/login', 'destination=' . drupal_urlencode($redirect)); |
| 467 |
} |
} |
| 468 |
|
|
| 469 |
/** |
/** |
| 470 |
* Log out a user (ensuring a logout in vBulletin); menu callback. |
* vBulletin user logout menu callback. |
| 471 |
* |
* |
| 472 |
* This is required in cases a user manages to log out from Drupal, but not |
* This is required in cases a user manages to log out from Drupal, but not |
| 473 |
* from vBulletin (due to stale cookies or any other possible event). |
* from vBulletin (due to stale cookies or other circumstance). |
|
* |
|
|
* @see drupalvb_user_logout() |
|
| 474 |
*/ |
*/ |
| 475 |
function drupalvb_logout() { |
function drupalvb_logout() { |
| 476 |
global $user; |
global $user; |
| 680 |
// Try to find matching Drupal uids. These might not exist yet, |
// Try to find matching Drupal uids. These might not exist yet, |
| 681 |
// theme_drupalvb_username() takes care of that. |
// theme_drupalvb_username() takes care of that. |
| 682 |
if ($userids) { |
if ($userids) { |
| 683 |
$result = db_query("SELECT d.vb_id AS userid, d.drupal_id AS uid, u.picture FROM {drupalvb_users} d INNER JOIN {users} u ON u.uid = d.drupal_id WHERE d.vb_id IN (". implode(',', array_keys($userids)) .")"); |
$result = db_query("SELECT m.vb_id AS userid, m.drupal_id AS uid, u.picture FROM {drupalvb_map} m INNER JOIN {users} u ON u.uid = m.drupal_id WHERE m.type = 'users' AND m.vb_id IN (". implode(',', array_keys($userids)) .")"); |
| 684 |
while ($data = db_fetch_array($result)) { |
while ($data = db_fetch_array($result)) { |
| 685 |
foreach ($userids[$data['userid']] as $i) { |
foreach ($userids[$data['userid']] as $i) { |
| 686 |
$items[$i] = array_merge($items[$i], $data); |
$items[$i] = array_merge($items[$i], $data); |
| 759 |
$items[$data['userid']] = $data; |
$items[$data['userid']] = $data; |
| 760 |
} |
} |
| 761 |
|
|
| 762 |
$result = db_query("SELECT d.vb_id AS userid, d.drupal_id AS uid, u.picture FROM {drupalvb_users} d INNER JOIN {users} u ON u.uid = d.drupal_id WHERE d.vb_id IN (". implode(',', array_keys($items)) .")"); |
$result = db_query("SELECT m.vb_id AS userid, m.drupal_id AS uid, u.picture FROM {drupalvb_map} m INNER JOIN {users} u ON u.uid = m.drupal_id WHERE m.type = 'users' AND m.vb_id IN (". implode(',', array_keys($items)) .")"); |
| 763 |
while ($data = db_fetch_array($result)) { |
while ($data = db_fetch_array($result)) { |
| 764 |
$items[$data['userid']] = array_merge($items[$data['userid']], $data); |
$items[$data['userid']] = array_merge($items[$data['userid']], $data); |
| 765 |
} |
} |
| 864 |
* A class name for User Display API. |
* A class name for User Display API. |
| 865 |
*/ |
*/ |
| 866 |
function theme_drupalvb_username($object, $class = NULL) { |
function theme_drupalvb_username($object, $class = NULL) { |
| 867 |
if ($object->uid && $object->name) { |
if (!empty($object->uid) && $object->name) { |
| 868 |
// Drupal account exists: fall back on default theming. |
// Drupal account exists: fall back on default theming. |
| 869 |
$output = theme('username', $object, $class); |
$output = theme('username', $object, $class); |
| 870 |
} |
} |
| 871 |
else if ($object->userid && $object->name) { |
else if (!empty($object->userid) && $object->name) { |
| 872 |
// Remove any html entities injected by vBulletin. |
// Remove any html entities injected by vBulletin. |
| 873 |
$object->name = decode_entities($object->name); |
$object->name = decode_entities($object->name); |
| 874 |
// Drupal account doesn't exists yet. Link to it using our redirector, |
// Drupal account doesn't exists yet. Link to it using our redirector, |
| 937 |
} |
} |
| 938 |
|
|
| 939 |
/** |
/** |
| 940 |
* Redirector for Drupal paths containing vBulletin userids. |
* Generic redirector for Drupal paths containing vBulletin userids. |
| 941 |
* |
* |
| 942 |
* This function is useful when changing paths in vBulletin to point to Drupal |
* This function is useful when changing paths in vBulletin to point to Drupal |
| 943 |
* module paths. For example, if you want to use the Buddylist module instead |
* module paths. For example, if you want to use the Buddylist module instead |
| 944 |
* of vBulletin's buddy feature, change the link in template MEMBERINFO to |
* of vBulletin's buddy feature, edit the link in vBulletin to this: |
| 945 |
* <code>/drupalvb/buddy/add/$userinfo[userid]</code>. |
* <code>/drupalvb/buddy/add/$userinfo[userid]</code>. |
| 946 |
* The function looks up the corresponding Drupal user and exchanges the |
* When clicked, this function looks up the corresponding Drupal user, |
| 947 |
* userid, and redirects the user to the new path. |
* exchanges the userid, and redirects the user to the correct path. |
| 948 |
* |
* |
| 949 |
* @param ... |
* @param ... |
| 950 |
* A Drupal path. Numeric path arguments will be mapped to Drupal uids. |
* A Drupal path. Numeric path arguments will be mapped to Drupal uids. |
| 970 |
* Lookup the corresponding Drupal uid for a vBulletin userid. |
* Lookup the corresponding Drupal uid for a vBulletin userid. |
| 971 |
* |
* |
| 972 |
* If the vBulletin userid is valid, then a temporary Drupal user will be |
* If the vBulletin userid is valid, then a temporary Drupal user will be |
| 973 |
* created and it's uid returned. |
* created and its uid returned. |
| 974 |
* |
* |
| 975 |
* @param $userid |
* @param $userid |
| 976 |
* A vBulletin user id. |
* A vBulletin user id. |
| 981 |
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
| 982 |
|
|
| 983 |
// Check if this vBulletin user id already exists as Drupal user. |
// Check if this vBulletin user id already exists as Drupal user. |
| 984 |
if ($uid = drupalvb_user_load($userid)) { |
if ($uid = drupalvb_get_drupal_id('users', $userid)) { |
| 985 |
return $uid; |
return $uid; |
| 986 |
} |
} |
| 987 |
|
|
| 991 |
// know the real one. It will be updated when the user logs in to Drupal |
// know the real one. It will be updated when the user logs in to Drupal |
| 992 |
// for the first time using its vBulletin credentials. |
// for the first time using its vBulletin credentials. |
| 993 |
// @see drupalvb_auth() |
// @see drupalvb_auth() |
| 994 |
$userinfo = array( |
$edit = array( |
| 995 |
'name' => $vbuser['username'], |
'name' => $vbuser['username'], |
| 996 |
'pass' => user_password(), |
'pass' => user_password(), |
| 997 |
'mail' => $vbuser['email'], |
'mail' => $vbuser['email'], |
| 999 |
'created' => $vbuser['joindate'], |
'created' => $vbuser['joindate'], |
| 1000 |
'status' => 1, |
'status' => 1, |
| 1001 |
); |
); |
| 1002 |
$user = user_save('', $userinfo); |
$user = user_save('', $edit); |
| 1003 |
|
|
| 1004 |
// On a heavy Drupal installation, it can happen that when a user accidently |
// If a user accidentally double-clicks on a redirector link on a busy |
| 1005 |
// double-clicks on a redirector link we run into a timing problem: |
// site, we may run into a timing problem here: while the first request is |
| 1006 |
// while the first request is in the middle of registering the Drupal account, |
// in the middle of creating the Drupal account, the following request |
| 1007 |
// the immediate second request tries to do the same again, resulting |
// tries to do the same in parallel, and fails. |
|
// in an error. |
|
| 1008 |
if ($user->uid) { |
if ($user->uid) { |
| 1009 |
watchdog('drupalvb', t('New external user: %user (unverified).', array('%user' => $user->name)), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit')); |
watchdog('drupalvb', t('New external user: %user (unverified).', array('%user' => $user->name)), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit')); |
| 1010 |
|
|
| 1013 |
|
|
| 1014 |
return $user->uid; |
return $user->uid; |
| 1015 |
} |
} |
| 1016 |
// In case the user couldn't be registered, try to load it from the database. |
// See if a mapping exists in the meantime. |
| 1017 |
else if ($uid = drupalvb_user_load($vbuser['userid'])) { |
else if ($uid = drupalvb_get_drupal_id('users', $vbuser['userid'])) { |
| 1018 |
return $uid; |
return $uid; |
| 1019 |
} |
} |
| 1020 |
// We're out of luck... |
// We're out of luck... |
| 1027 |
|
|
| 1028 |
/** |
/** |
| 1029 |
* Implementation of hook_privatemsg(). |
* Implementation of hook_privatemsg(). |
|
* |
|
|
* This hack requires re-mapping of all requests for /forum/private.php to |
|
|
* privatemsg paths using .htaccess rewrite rules. Plus, it messes around with |
|
|
* vBulletin's pm table, which means you can't easily switch back (you have to |
|
|
* reset that table to be able to do that). |
|
|
* |
|
|
* Note: message id is set to privatemsg->id + 100.000 to avoid collision with |
|
|
* existing messages. |
|
| 1030 |
*/ |
*/ |
| 1031 |
function drupalvb_privatemsg($message, $op) { |
function drupalvb_privatemsg($message, $op) { |
| 1032 |
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
| 1033 |
if (!(variable_get('drupalvb_pm_sync', FALSE) && drupalvb_db_is_valid())) { |
if (!drupalvb_db_is_valid()) { |
| 1034 |
return; |
return; |
| 1035 |
} |
} |
| 1036 |
|
global $user; |
|
// Shift message id to avoid overwriting existing messages. |
|
|
$mid = $message->id + 100000; |
|
| 1037 |
|
|
| 1038 |
switch ($op) { |
switch ($op) { |
| 1039 |
case 'sent': |
case 'sent': |
| 1040 |
|
case 'view': |
| 1041 |
|
case 'status': |
| 1042 |
|
case 'delete': |
| 1043 |
|
case 'restore': |
| 1044 |
|
//case 'prune': // not supported |
| 1045 |
|
|
| 1046 |
|
// Don't act on message preview. |
| 1047 |
|
if (!empty($message->preview)) { |
| 1048 |
|
return; |
| 1049 |
|
} |
| 1050 |
|
|
| 1051 |
// Verify that recipient exists in vB. |
// Verify that recipient exists in vB. |
| 1052 |
$recipient = user_load(array('uid' => $message->recipient)); |
$userid = drupalvb_get_vb_id('users', $message->recipient); |
| 1053 |
if (!$userid = db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE username = '%s'", drupalvb_htmlspecialchars($recipient->name)))) { |
if (!$userid) { |
| 1054 |
if (!$userid = drupalvb_create_user($recipient, (array)$recipient)) { |
$recipient = user_load(array('uid' => $message->recipient)); |
| 1055 |
// Indicates duplicate username (should not happen). |
if (!$userid = drupalvb_synchronize_user($recipient)) { |
| 1056 |
return; |
return; |
| 1057 |
} |
} |
| 1058 |
} |
} |
| 1059 |
// pmtextid stores mapping of privatemsg id to pmid, to be able to |
|
| 1060 |
// display accurate message counts in vBulletin. |
$count = db_fetch_array(db_query("SELECT COUNT(*) AS pmtotal, SUM(IF(newmsg = 1, 1, 0)) AS pmunread FROM {privatemsg} WHERE recipient = %d AND recipient_del = 0", $message->recipient)); |
| 1061 |
drupalvb_db_query("INSERT INTO {pm} (userid, pmtextid) VALUES (%d, %d)", $userid, $mid); |
if ($op == 'view' && $message->newmsg && $user->uid == $message->recipient) { |
| 1062 |
break; |
// Message is about to be viewed. |
| 1063 |
|
$count['pmunread']--; |
| 1064 |
case 'view': |
} |
| 1065 |
// Mark as read in vBulletin. |
|
| 1066 |
drupalvb_db_query("UPDATE {pm} SET messageread = 0 WHERE pmtextid = %d", $mid); |
// Update counts for user. |
| 1067 |
break; |
$vbuser = &drupalvb_datamanager_init('User'); |
| 1068 |
|
$existing = fetch_userinfo($userid); |
| 1069 |
case 'prune': |
$vbuser->set_existing($existing); |
| 1070 |
case 'delete': |
$vbuser->set('pmtotal', $count['pmtotal']); |
| 1071 |
// Delete in vBulletin. |
$vbuser->set('pmunread', $count['pmunread']); |
| 1072 |
drupalvb_db_query("DELETE FROM {pm} WHERE pmtextid = %d", $mid); |
if ($vbuser->fetch_field('pmpopup') && ($op == 'sent' || ($op == 'status' && $message->newmsg))) { |
| 1073 |
|
// Show PM popup on next page request. |
| 1074 |
|
$vbuser->set('pmpopup', 2); |
| 1075 |
|
} |
| 1076 |
|
$vbuser->save(); |
| 1077 |
break; |
break; |
| 1078 |
} |
} |
| 1079 |
} |
} |
| 1080 |
|
|
| 1081 |
|
/** |
| 1082 |
|
* Implementation of hook_guestbook(). |
| 1083 |
|
*/ |
| 1084 |
|
function drupalvb_guestbook($op, $entry) { |
| 1085 |
|
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
| 1086 |
|
if (!drupalvb_db_is_valid()) { |
| 1087 |
|
return; |
| 1088 |
|
} |
| 1089 |
|
|
| 1090 |
|
if ($op == 'insert') { |
| 1091 |
|
$account = user_load(array('uid' => $entry['recipient'])); |
| 1092 |
|
|
| 1093 |
|
// Verify that recipient exists in vB. |
| 1094 |
|
$userid = drupalvb_get_vb_id('users', $account->uid); |
| 1095 |
|
if (!$userid) { |
| 1096 |
|
if (!$userid = drupalvb_synchronize_user($account)) { |
| 1097 |
|
return; |
| 1098 |
|
} |
| 1099 |
|
} |
| 1100 |
|
|
| 1101 |
|
$count = db_result(db_query("SELECT COUNT(*) FROM {guestbook} WHERE recipient = %d AND created > %d", $entry['recipient'], $account->guestbook_visited)); |
| 1102 |
|
|
| 1103 |
|
// Update count for user. |
| 1104 |
|
$vbuser = &drupalvb_datamanager_init('User'); |
| 1105 |
|
$existing = array('userid' => $userid); |
| 1106 |
|
$vbuser->set_existing($existing); |
| 1107 |
|
$vbuser->set('vmunreadcount', $count); |
| 1108 |
|
$vbuser->save(); |
| 1109 |
|
} |
| 1110 |
|
} |
| 1111 |
|
|
| 1112 |
|
/** |
| 1113 |
|
* Implementation of hook_buddylist(). |
| 1114 |
|
*/ |
| 1115 |
|
function drupalvb_buddylist($op, $buddy) { |
| 1116 |
|
require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc'; |
| 1117 |
|
if (!drupalvb_db_is_valid()) { |
| 1118 |
|
return; |
| 1119 |
|
} |
| 1120 |
|
|
| 1121 |
|
switch ($op) { |
| 1122 |
|
case 'request': |
| 1123 |
|
case 'accept': |
| 1124 |
|
case 'deny': |
| 1125 |
|
// Verify that recipient exists in vB. |
| 1126 |
|
$userid = drupalvb_get_vb_id('users', $buddy->uid); |
| 1127 |
|
if (!$userid) { |
| 1128 |
|
if (!$userid = drupalvb_synchronize_user($buddy)) { |
| 1129 |
|
return; |
| 1130 |
|
} |
| 1131 |
|
} |
| 1132 |
|
|
| 1133 |
|
$count = db_result(db_query("SELECT COUNT(*) FROM {guestbook} WHERE buddy = %d AND received = 0", $buddy->uid)); |
| 1134 |
|
|
| 1135 |
|
// Update count for user. |
| 1136 |
|
$vbuser = &drupalvb_datamanager_init('User'); |
| 1137 |
|
$existing = array('userid' => $userid); |
| 1138 |
|
$vbuser->set_existing($existing); |
| 1139 |
|
$vbuser->set('friendreqcount', $count); |
| 1140 |
|
$vbuser->save(); |
| 1141 |
|
} |
| 1142 |
|
} |