| 1 |
<?php |
<?php |
| 2 |
// $Id: drupalvb.inc.php,v 1.9.4.25.2.1 2009/05/29 10:22:50 sun Exp $ |
// $Id: drupalvb.inc.php,v 1.9.4.25.2.2 2009/06/05 15:03:20 sun Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 123 |
else if (isset($_SERVER['HTTP_FROM'])) { |
else if (isset($_SERVER['HTTP_FROM'])) { |
| 124 |
$ip = $_SERVER['HTTP_FROM']; |
$ip = $_SERVER['HTTP_FROM']; |
| 125 |
} |
} |
| 126 |
|
|
| 127 |
return $ip; |
return $ip; |
| 128 |
} |
} |
| 129 |
|
|
| 130 |
/** |
/** |
| 131 |
* Create a user in vBulletin. |
* Synchronize a Drupal user account with vBulletin. |
| 132 |
* |
* |
| 133 |
* @param object $account |
* @param $account |
| 134 |
* A Drupal user account. |
* A Drupal user account object. |
| 135 |
* @param array $edit |
* @param $edit |
| 136 |
* Form values provided by hook_user(). |
* Submitted form values, usually provided by hook_user(). |
| 137 |
*/ |
* @return |
| 138 |
function drupalvb_create_user($account, $edit) { |
* The matching vBulletin userid for the account. FALSE indicates a |
| 139 |
// Ensure we are not duplicating a user. |
* unrecoverable vBulletin data validation error. |
| 140 |
if (db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE LOWER(username) = LOWER('%s')", drupalvb_htmlspecialchars($edit['name'])))) { |
*/ |
| 141 |
return FALSE; |
function drupalvb_synchronize_user($account, $edit = array()) { |
| 142 |
|
define('DRUPALVB_DISABLE_SYNC', TRUE); |
| 143 |
|
|
| 144 |
|
// Query the remote database whether the given username is known. |
| 145 |
|
if ($userid = db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE LOWER(username) = LOWER('%s') LIMIT 1", drupalvb_htmlspecialchars($account->name)))) { |
| 146 |
|
// Update existing vBulletin account with data in $edit. |
| 147 |
|
if (!_drupalvb_update_user($userid, $account, $edit)) { |
| 148 |
|
return FALSE; |
| 149 |
|
} |
| 150 |
} |
} |
| 151 |
|
else { |
| 152 |
|
// Create vBulletin user from existing account data. |
| 153 |
|
if (!$userid = _drupalvb_create_user((array) $account)) { |
| 154 |
|
return FALSE; |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
// Ensure that a user mapping exists. |
| 158 |
|
drupalvb_set_mapping('users', $account->uid, $userid); |
| 159 |
|
return $userid; |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
/** |
| 163 |
|
* Create a user in vBulletin. |
| 164 |
|
* |
| 165 |
|
* @see drupalvb_synchronize_user() |
| 166 |
|
*/ |
| 167 |
|
function _drupalvb_create_user($edit) { |
| 168 |
$vbuser = &drupalvb_datamanager_init('User'); |
$vbuser = &drupalvb_datamanager_init('User'); |
| 169 |
|
global $vbulletin, $db; |
| 170 |
|
|
| 171 |
// Disable several validations that do not exist in Drupal. |
// Disable several validations that do not exist in Drupal. |
| 172 |
$vbuser->adminoverride = TRUE; |
$vbuser->adminoverride = TRUE; |
| 173 |
|
|
| 174 |
$vbuser->set('username', $edit['name']); |
$vbuser->set('username', $edit['name']); |
| 175 |
$vbuser->set('email', $edit['mail']); |
$vbuser->set('email', $edit['mail']); |
| 176 |
$vbuser->set('password', $edit['pass']); |
$vbuser->set('password', $edit['pass']); |
| 177 |
$vbuser->set('joindate', $account->created); |
$vbuser->set('joindate', $edit['created']); |
| 178 |
$vbuser->set('lastvisit', $account->access); |
$vbuser->set('lastvisit', $edit['access']); |
| 179 |
$vbuser->set('lastactivity', $account->access); |
$vbuser->set('lastactivity', $edit['access']); |
| 180 |
|
|
| 181 |
// Determine target usergroups. |
// Determine target usergroups. |
| 182 |
$roles = is_array($edit['roles']) ? $edit['roles'] : $account->roles; |
$vbuser->set('usergroupid', drupalvb_get_usergroup($edit['roles'])); |
| 183 |
$vbuser->set('usergroupid', drupalvb_get_usergroup($roles)); |
$vbuser->set('membergroupids', drupalvb_get_membergroups($edit['roles'])); |
|
$vbuser->set('membergroupids', drupalvb_get_membergroups($roles)); |
|
| 184 |
|
|
| 185 |
// Determine timezone from date.module and fall back on system default. |
// Determine timezone from user preference (requires date_timezone.module), |
| 186 |
$timezone = isset($edit['timezone']) ? $edit['timezone'] : (isset($account->timezone) ? $account->timezone : variable_get('date_default_timezone', 0)); |
// or from system setting. |
| 187 |
|
$timezone = isset($edit['timezone']) ? $edit['timezone'] : variable_get('date_default_timezone', 0); |
| 188 |
// vBulletin stores hours, not seconds. |
// vBulletin stores hours, not seconds. |
| 189 |
$vbuser->set('timezoneoffset', $timezone / 3600); |
$vbuser->set('timezoneoffset', $timezone / 3600); |
| 190 |
|
|
| 199 |
|
|
| 200 |
$userid = $vbuser->save(); |
$userid = $vbuser->save(); |
| 201 |
|
|
| 202 |
// Insert new user into mapping table. |
// Notify crappy plugins that fail to use datamanager hooks. |
| 203 |
drupalvb_set_mapping('users', $account->uid, $userid); |
($hook = vBulletinHook::fetch_hook('register_addmember_complete')) ? eval($hook) : FALSE; |
| 204 |
|
|
| 205 |
watchdog('drupalvb', t('Created forum user %username', array('%username' => $edit['name']))); |
watchdog('drupalvb', t('Created forum user %username', array('%username' => $edit['name']))); |
| 206 |
|
|
| 207 |
return $userid; |
return $userid; |
| 209 |
|
|
| 210 |
/** |
/** |
| 211 |
* Update a user in vBulletin. |
* Update a user in vBulletin. |
| 212 |
|
* |
| 213 |
|
* @see drupalvb_synchronize_user() |
| 214 |
*/ |
*/ |
| 215 |
function drupalvb_update_user($account, $edit) { |
function _drupalvb_update_user($userid, $account, $edit) { |
| 216 |
$vbuser = &drupalvb_datamanager_init('User'); |
$vbuser = &drupalvb_datamanager_init('User'); |
| 217 |
// Disable several validations that do not exist in Drupal. |
// Disable several validations that do not exist in Drupal. |
| 218 |
$vbuser->adminoverride = TRUE; |
$vbuser->adminoverride = TRUE; |
| 219 |
|
|
| 220 |
// Ensure this user exists in the mapping table. |
$existing = array('userid' => $userid); |
|
// When integrating an existing installation, the mapping may not yet exist. |
|
|
$userid = db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE LOWER(username) = LOWER('%s')", drupalvb_htmlspecialchars($account->name))); |
|
|
// If no matching username was found, we cannot update. |
|
|
if (!$userid) { |
|
|
return FALSE; |
|
|
} |
|
|
drupalvb_set_mapping('users', $account->uid, $userid); |
|
|
|
|
|
$existing = fetch_userinfo($userid); |
|
| 221 |
$vbuser->set_existing($existing); |
$vbuser->set_existing($existing); |
| 222 |
|
// Always update last activity timestamps. |
| 223 |
$vbuser->set('lastvisit', $account->access); |
$vbuser->set('lastvisit', $account->access); |
| 224 |
$vbuser->set('lastactivity', $account->access); |
$vbuser->set('lastactivity', $account->access); |
| 225 |
|
|
| 236 |
|
|
| 237 |
case 'pass': |
case 'pass': |
| 238 |
// vBulletin's datamanager accepts both a plain or a single-hashed |
// vBulletin's datamanager accepts both a plain or a single-hashed |
| 239 |
// password (but not a salted one). |
// password (but not hashed + salted). |
| 240 |
$vbuser->set('password', $value); |
$vbuser->set('password', $value); |
| 241 |
break; |
break; |
| 242 |
|
|
| 252 |
// vBulletin stores hours, not seconds. |
// vBulletin stores hours, not seconds. |
| 253 |
$vbuser->set('timezoneoffset', $value / 3600); |
$vbuser->set('timezoneoffset', $value / 3600); |
| 254 |
break; |
break; |
| 255 |
|
|
| 256 |
|
case 'guestbook_visited': |
| 257 |
|
// Update visitor message notification on behalf of guestbook.module. |
| 258 |
|
// This will only be set for the owner. |
| 259 |
|
$vbuser->set('vmunreadcount', 0); |
| 260 |
|
break; |
| 261 |
} |
} |
| 262 |
} |
} |
| 263 |
|
|
| 273 |
$vbuser->save(); |
$vbuser->save(); |
| 274 |
watchdog('drupalvb', t('Updated forum user %username', array('%username' => $account->name))); |
watchdog('drupalvb', t('Updated forum user %username', array('%username' => $account->name))); |
| 275 |
|
|
| 276 |
return $userid; |
return TRUE; |
| 277 |
} |
} |
| 278 |
|
|
| 279 |
/** |
/** |
| 287 |
function drupalvb_get_primary_role($roles) { |
function drupalvb_get_primary_role($roles) { |
| 288 |
$primary_role = DRUPAL_AUTHENTICATED_RID; |
$primary_role = DRUPAL_AUTHENTICATED_RID; |
| 289 |
$primary_weight = 0; |
$primary_weight = 0; |
| 290 |
$role_weights = variable_get('drupalvb_role_weights', array()); |
if (!empty($roles)) { |
| 291 |
foreach (array_keys($roles) as $rid) { |
$role_weights = variable_get('drupalvb_role_weights', array()); |
| 292 |
if (isset($role_weights[$rid]) && $role_weights[$rid] < $primary_weight) { |
foreach (array_keys($roles) as $rid) { |
| 293 |
$primary_role = $rid; |
if (isset($role_weights[$rid]) && $role_weights[$rid] < $primary_weight) { |
| 294 |
$primary_weight = $role_weights[$rid]; |
$primary_role = $rid; |
| 295 |
|
$primary_weight = $role_weights[$rid]; |
| 296 |
|
} |
| 297 |
} |
} |
| 298 |
} |
} |
| 299 |
return $primary_role; |
return $primary_role; |
| 337 |
* A vBulletin id. |
* A vBulletin id. |
| 338 |
*/ |
*/ |
| 339 |
function drupalvb_get_drupal_id($type, $vb_id) { |
function drupalvb_get_drupal_id($type, $vb_id) { |
| 340 |
return db_result(db_query("SELECT drupal_id FROM {drupalvb_". db_escape_table($type) ."} WHERE vb_id = %d", $vb_id)); |
return db_result(db_query("SELECT drupal_id FROM {drupalvb_map} WHERE type = '%s' AND vb_id = %d", $type, $vb_id)); |
| 341 |
} |
} |
| 342 |
|
|
| 343 |
/** |
/** |
| 350 |
* A Drupal id. |
* A Drupal id. |
| 351 |
*/ |
*/ |
| 352 |
function drupalvb_get_vb_id($type, $drupal_id) { |
function drupalvb_get_vb_id($type, $drupal_id) { |
| 353 |
return db_result(db_query("SELECT vb_id FROM {drupalvb_". db_escape_table($type) ."} WHERE drupal_id = %d", $drupal_id)); |
return db_result(db_query("SELECT vb_id FROM {drupalvb_map} WHERE type = '%s' AND drupal_id = %d", $type, $drupal_id)); |
| 354 |
} |
} |
| 355 |
|
|
| 356 |
/** |
/** |
| 365 |
* A vBulletin id. |
* A vBulletin id. |
| 366 |
*/ |
*/ |
| 367 |
function drupalvb_set_mapping($type, $drupal_id, $vb_id) { |
function drupalvb_set_mapping($type, $drupal_id, $vb_id) { |
| 368 |
db_query("INSERT IGNORE INTO {drupalvb_". db_escape_table($type) ."} (drupal_id, vb_id) VALUES (%d, %d)", $drupal_id, $vb_id); |
db_query("INSERT IGNORE INTO {drupalvb_map} (type, drupal_id, vb_id) VALUES ('%s', %d, %d)", $type, $drupal_id, $vb_id); |
| 369 |
} |
} |
| 370 |
|
|
| 371 |
/** |
/** |
| 379 |
if (is_string($types)) { |
if (is_string($types)) { |
| 380 |
$types = array($types); |
$types = array($types); |
| 381 |
} |
} |
| 382 |
foreach ($types as $type) { |
$placeholders = implode(',', array_fill(0, count($types), "'%s'")); |
| 383 |
db_query("TRUNCATE {drupalvb_". db_escape_table($type) ."}"); |
db_query("DELETE FROM {drupalvb_map} WHERE type IN (". $placeholders .")", $types); |
| 384 |
if ($type == 'users') { |
if (in_array('users', $types)) { |
| 385 |
// Re-initialize user map. |
// Re-initialize user map. |
| 386 |
// @todo huh? |
_drupalvb_init_user_map(); |
|
_drupalvb_init_user_map(); |
|
|
} |
|
| 387 |
} |
} |
| 388 |
} |
} |
| 389 |
|
|