? LICENSE.txt
? drupalvb_total_v6_20080721.patch
Index: drupalvb.admin-pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalvb/drupalvb.admin-pages.inc,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 drupalvb.admin-pages.inc
--- drupalvb.admin-pages.inc	16 Jul 2008 22:12:00 -0000	1.1.2.6
+++ drupalvb.admin-pages.inc	21 Jul 2008 22:07:50 -0000
@@ -134,11 +134,11 @@ function drupalvb_settings_database() {
   return $form;
 }
 
-function drupalvb_settings_database_submit($form_id, $form_values) {
-  $url = $form_values['scheme'] .'://'. $form_values['user'] .':'. $form_values['pass'] .'@'. $form_values['host'] .'/'. $form_values['path'];
+function drupalvb_settings_database_submit($form, &$form_state) {
+  $url = $form_state['values']['scheme'] .'://'. $form_state['values']['user'] .':'. $form_state['values']['pass'] .'@'. $form_state['values']['host'] .'/'. $form_state['values']['path'];
   variable_set('drupalvb_db', $url);
   variable_set('drupalvb_db_is_default', (is_array($GLOBALS['db_url']) ? $GLOBALS['db_url']['default'] == $url : $GLOBALS['db_url'] == $url));
-  variable_set('drupalvb_db_prefix', $form_values['db_prefix']);
+  variable_set('drupalvb_db_prefix', $form_state['values']['db_prefix']);
 }
 
 /**
@@ -175,8 +175,8 @@ function drupalvb_settings_actions() {
 /**
  * Form submit callback for action form.
  */
-function drupalvb_settings_actions_submit($form_id, $form_values) {
-  switch ($form_values['action']) {
+function drupalvb_settings_actions_submit($form, &$form_state) {
+  switch ($form_state['values']['action']) {
     case 'export':
       drupalvb_export_drupal_users();
       drupal_set_message(t('Drupal users have been exported to vBulletin.'));
@@ -191,8 +191,9 @@ function drupalvb_settings_variables() {
   $form = array();
   $options = drupalvb_get('options');
   foreach ($options as $key => $value) {
-    $form['variables'][$key]['name'] = array('#value' => check_plain($key));
-    $form['variables'][$key]['value'] = array('#value' => check_plain($value));
+    $form['variables'][$key]['#type'] = 'item';
+    $form['variables'][$key]['#title'] = check_plain($key);
+    $form['variables'][$key]['#value'] = check_plain($value);
   }
   ksort($form['variables']);
   return $form;
Index: drupalvb.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalvb/drupalvb.inc,v
retrieving revision 1.6.2.7
diff -u -p -r1.6.2.7 drupalvb.inc
--- drupalvb.inc	5 Jul 2008 00:37:02 -0000	1.6.2.7
+++ drupalvb.inc	21 Jul 2008 22:07:50 -0000
@@ -163,11 +163,37 @@ function drupalvb_db_query($query) {
 }
 
 /**
+ * Query remote system database with range.
+ *
+  * @see db_query_range
+ */
+function drupalvb_db_query_range($query) {
+  drupalvb_db_connect();
+  
+  $args = func_get_args();
+  $count = array_pop($args);
+  $from = array_pop($args);
+  array_shift($args);
+
+  $query = db_prefix_tables($query);
+  if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
+    $args = $args[0];
+  }
+  _db_query_callback($args, TRUE);
+  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
+  $query .= ' LIMIT '. (int)$count .' OFFSET '. (int)$from;
+  $result = _db_query($query);
+  
+  drupalvb_db_disconnect();
+  return $result;
+}
+
+/**
  * Returns the last insert id.
  *
  * Borrowed from Drupal 6.
  */
-function db_last_insert_id($table, $field) {
+function drupalvb_db_last_insert_id($table, $field) {
   return db_result(drupalvb_db_query('SELECT LAST_INSERT_ID()'));
 }
 
Index: drupalvb.inc.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalvb/drupalvb.inc.php,v
retrieving revision 1.9.4.17
diff -u -p -r1.9.4.17 drupalvb.inc.php
--- drupalvb.inc.php	16 Jul 2008 22:12:00 -0000	1.9.4.17
+++ drupalvb.inc.php	21 Jul 2008 22:07:50 -0000
@@ -106,7 +106,7 @@ function drupalvb_get_ip() {
  */
 function drupalvb_create_user($account, $edit) {
   // Ensure we are not duplicating a user.
-  if (db_num_rows(drupalvb_db_query("SELECT userid FROM {user} WHERE LOWER(username) = LOWER('%s')", $edit['name'])) > 0) {
+  if (db_result(drupalvb_db_query("SELECT COUNT(userid) FROM {user} WHERE LOWER(username) = LOWER('%s')", $edit['name'])) > 0) {
     return FALSE;
   }
 
@@ -149,7 +149,7 @@ function drupalvb_create_user($account, 
   // Set up the insertion query.
   $result = drupalvb_db_query("INSERT INTO {user} (username, usergroupid, password, passworddate, usertitle, email, salt, showvbcode, languageid, timezoneoffset, posts, joindate, lastvisit, lastactivity, options) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', 1, 1, '%s', 0, '%s', '%s', '%s', '%s')", $edit['name'], $usergroupid, $passhash, $passdate, $usertitle, $edit['mail'], $salt, $timezone, $joindate, time(), time(), $options);
 
-  $userid = db_last_insert_id('user', 'userid');
+  $userid = drupalvb_db_last_insert_id('user', 'userid');
 
   drupalvb_db_query("INSERT INTO {userfield} (userid) VALUES (%d)", $userid);
   drupalvb_db_query("INSERT INTO {usertextfield} (userid) VALUES (%d)", $userid);
@@ -361,8 +361,8 @@ function drupalvb_get_recent_posts($scop
     else if ($scope == 'daily') {
       $datecut = time() - 86400;
     }
-    $result = drupalvb_db_query("SELECT postid FROM {post} WHERE dateline > %d", $datecut);
-    $posts = db_num_rows($result);
+    $result = drupalvb_db_query("SELECT COUNT(postid) FROM {post} WHERE dateline > %d", $datecut);
+    $posts = db_result($result);
   }
   else {
     $posts = 0;
Index: drupalvb.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalvb/drupalvb.info,v
retrieving revision 1.2.4.1
diff -u -p -r1.2.4.1 drupalvb.info
--- drupalvb.info	30 Apr 2008 01:20:06 -0000	1.2.4.1
+++ drupalvb.info	21 Jul 2008 22:07:50 -0000
@@ -1,3 +1,4 @@
 ; $Id: drupalvb.info,v 1.2.4.1 2008/04/30 01:20:06 sun Exp $
 name = Drupal vB
 description = Integrate your Drupal site with vBulletin forums.
+core = 6.x
Index: drupalvb.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalvb/drupalvb.install,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 drupalvb.install
--- drupalvb.install	16 Jul 2008 00:43:48 -0000	1.1.2.6
+++ drupalvb.install	21 Jul 2008 22:07:50 -0000
@@ -1,31 +1,43 @@
 <?php
-// $Id: drupalvb.install,v 1.1.2.6 2008/07/16 00:43:48 sun Exp $
+// $Id: drupalvb.install,v 1.1.2.4 2008/05/01 22:52:25 sun Exp $
 
 /**
+ * @file 
+ * Install file for drupalvb module.
+ * 
+ */
+/**
  * Implementation of hook_install().
  */
 function drupalvb_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      db_query("CREATE TABLE {drupalvb_users} (
-          uid int(10) unsigned NOT NULL default '0',
-          userid int(10) unsigned NOT NULL default '0',
-          PRIMARY KEY (uid),
-          UNIQUE userid (userid)
-        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
-
-      require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc';
-      _drupalvb_init_user_map();
-      break;
-  }
+  drupal_install_schema('drupalvb');
+  require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc';
+  _drupalvb_init_user_map();
+}
+
+/**
+ * Implementation of hook_schema().
+ */
+function drupalvb_schema() {
+  $schema['drupalvb_users'] = array(
+    'fields' => array(
+      'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'userid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+    ),
+    'primary key' => array('uid'),
+    'unique keys' => array(
+      'userid' => array('userid'),
+    ),
+  );
+
+  return $schema;
 }
 
 /**
  * Implementation of hook_uninstall().
  */
 function drupalvb_uninstall() {
-  db_query("DROP TABLE {drupalvb_users}");
+  drupal_uninstall_schema('drupalvb');
   db_query("DELETE FROM {variable} WHERE name LIKE 'drupalvb_%%'");
 }
 
@@ -53,20 +65,19 @@ function drupalvb_update_5100() {
  */
 function drupalvb_update_5200() {
   $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql("CREATE TABLE {drupalvb_users} (
-          uid int(10) unsigned NOT NULL default '0',
-          userid int(10) unsigned NOT NULL default '0',
-          PRIMARY KEY (uid),
-          KEY userid (userid)
-        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
-
-      require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc';
-      _drupalvb_init_user_map();
-      break;
-  }
+  $new_table = array(
+    'fields' => array(
+      'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'userid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+    ),
+    'primary key' => array('uid'),
+    'indexes' => array(
+      'userid' => array('userid'),
+    ),
+  );
+  db_create_table($ret, 'drupalvb_users', $new_table);
+  require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc';
+  _drupalvb_init_user_map();
   return $ret;
 }
 
@@ -106,11 +117,7 @@ function drupalvb_update_5201() {
  */
 function drupalvb_update_5202() {
   $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql("ALTER TABLE {drupalvb_users} DROP INDEX userid, ADD UNIQUE userid (userid)");
-      break;
-  }
+  db_drop_index($ret, 'drupalvb_users', 'userid');
+  db_add_unique_key($ret, 'drupalvb_users', 'userid', array('userid'));
   return $ret;
-}
+}
\ No newline at end of file
Index: drupalvb.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drupalvb/drupalvb.module,v
retrieving revision 1.11.4.17
diff -u -p -r1.11.4.17 drupalvb.module
--- drupalvb.module	16 Jul 2008 01:11:21 -0000	1.11.4.17
+++ drupalvb.module	21 Jul 2008 22:07:50 -0000
@@ -13,8 +13,8 @@ require_once drupal_get_path('module', '
 /**
  * Implementation of hook_help().
  */
-function drupalvb_help($section) {
-  switch ($section) {
+function drupalvb_help($path, $arg) {
+  switch ($path) {
     case 'admin/settings#description':
       return t('Allows basic integration of Drupal with a vBulletin forum.');
 
@@ -33,76 +33,69 @@ function drupalvb_help($section) {
       return theme_item_list($items);
 
     case 'admin/help#drupalvb':
-      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'));
   }
 }
 
 /**
  * Implementation of menu_hook().
  */
-function drupalvb_menu($may_cache) {
+function drupalvb_menu() {
   $items = array();
-
-  if ($may_cache) {
-    $access = user_access('administer drupalvb');
-
-    $items[] = array(
-      'path' => 'admin/settings/drupalvb',
-      'title' => t('Drupal vB integration'),
-      'callback' => 'drupalvb_settings',
-      'callback arguments' => array('integration'),
-      'description' => t('Configure integration of Drupal with vBulletin forum.'),
-      'access' => $access,
-    );
-    $items[] = array(
-      'path' => 'admin/settings/drupalvb/integration',
-      'title' => t('Integration'),
-      'type' => MENU_DEFAULT_LOCAL_TASK,
-      'weight' => -10,
-    );
-    $items[] = array(
-      'path' => 'admin/settings/drupalvb/database',
-      'title' => t('Database'),
-      'callback' => 'drupalvb_settings',
-      'callback arguments' => array('database'),
-      'access' => $access,
-      'type' => MENU_LOCAL_TASK,
-    );
-    $items[] = array(
-      'path' => 'admin/settings/drupalvb/actions',
-      'title' => t('Actions'),
-      'callback' => 'drupalvb_settings',
-      'callback arguments' => array('actions'),
-      'access' => $access,
-      'type' => MENU_LOCAL_TASK,
-      'weight' => 10,
-    );
-    $items[] = array(
-      'path' => 'admin/settings/drupalvb/variables',
-      'title' => t('Variables'),
-      'callback' => 'drupalvb_settings',
-      'callback arguments' => array('variables'),
-      'access' => user_access('access devel information'),
-      'type' => MENU_LOCAL_TASK,
-      'weight' => 88,
-    );
-    $items[] = array(
-      'path' => 'drupalvb/pms',
-      'callback' => 'drupalvb_private_messages',
-      'access' => user_access('access content'),
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'drupalvb',
-      'callback' => 'drupalvb_redirect',
-      'access' => variable_get('drupalvb_acct_generation', TRUE),
-      'type' => MENU_CALLBACK,
-    );
-  }
+  
+  $items['admin/settings/drupalvb'] = array(
+    'title' => 'Drupal vB integration',
+    'page callback' => 'drupalvb_settings',
+    'page arguments' => array('integration'),
+    'description' => 'Configure integration of Drupal with vBulletin forum.',
+    'access arguments' => array('administer drupalvb'),
+  );
+  $items['admin/settings/drupalvb/integration'] = array(
+    'title' => 'Integration',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'weight' => -10,
+  );
+  $items['admin/settings/drupalvb/database'] = array(
+    'title' => 'Database',
+    'page callback' => 'drupalvb_settings',
+    'page arguments' => array('database'),
+    'access arguments' => array('administer drupalvb'),
+    'type' => MENU_LOCAL_TASK,
+  );
+  $items['admin/settings/drupalvb/actions'] = array(
+    'title' => 'Actions',
+    'page callback' => 'drupalvb_settings',
+    'page arguments' => array('actions'),
+    'access arguments' => array('administer drupalvb'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 10,
+  );
+  $items['admin/settings/drupalvb/variables'] = array(
+    'title' => 'Variables',
+    'page callback' => 'drupalvb_settings',
+    'page arguments' => array('variables'),
+    'access arguments' => array('access devel information'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 88,
+  );
+  $items['drupalvb/pms'] = array(
+    'page callback' => 'drupalvb_private_messages',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  $items['drupalvb'] = array(
+    'callback' => 'drupalvb_redirect',
+    'access callback' => 'drupalvb_acct_generation_access', 
+    'type' => MENU_CALLBACK,
+  );
 
   return $items;
 }
 
+function drupalvb_acct_generation_access() {
+  return variable_get('drupalvb_acct_generation', TRUE);
+}
+
 /**
  * Implementation of hook_perm().
  */
@@ -121,24 +114,86 @@ function drupalvb_settings($form = 'inte
 }
 
 /**
- * Implementation of hook_info().
+ * Implements hook_form_alter().
+ * 
+ * This replaces the hook_info() and hook_auth() code from Drupal 5.
+ *
+ * @param unknown_type $form
+ * @param unknown_type $form_state
+ * @param unknown_type $form_id
+ */
+function drupalvb_form_alter(&$form, $form_state, $form_id) {
+  
+  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
+    
+    require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc';
+    // Splice in our validate handler for authentication if user is performing a vb login.
+    if (!empty($form_state['post']['name']) && drupalvb_db_is_valid()) {
+      $username = $form_state['post']['name'];
+      if ($vbuser = db_fetch_array(drupalvb_db_query("SELECT userid, password, salt, email FROM {user} WHERE username = '%s'", $username))) {
+        $form['#validate'] = array('_drupalvb_login_validate') + $form['#validate']; //array('logintoboggan_user_login_validate') + $form['#validate'];
+      }
+    }
+  }
+}
+
+/*
+ * Used to validate the login - in case the user exists on the vbbulletin.
  */
-function drupalvb_info($field = 0) {
-  $info['name'] = 'vBulletin';
-  $info['protocol'] = 'DB';
+function _drupalvb_login_validate($form, &$form_state) {
+  global $user;
 
-  if ($field) {
-    return $info[$field];
+  if ($user->uid) {
+    return;
   }
-  else {
-    return $info;
+  
+  $username = $form_state['values']['name'];
+  $password = trim($form_state['values']['pass']);
+  
+  // If this user already exists in the drupal instance, then we skip over the rest of this validate.
+  $finduser = user_load(array('name' => $username));
+  if ($finduser->uid) {
+    return TRUE;
+  }
+  
+  require_once drupal_get_path('module', 'drupalvb') .'/drupalvb.inc';
+  if (!drupalvb_db_is_valid()) {
+    return;
   }
+  
+  if ($vbuser = db_fetch_array(drupalvb_db_query("SELECT userid, password, salt, email, joindate FROM {user} WHERE username = '%s'", $username))) {
+   
+    // Rebuild the password.
+    $vbpassword = md5(md5($password) . $vbuser['salt']);
+    if ($vbuser['password'] === $vbpassword) {
+      // Register this user in Drupal. user_authenticate() will store a
+      // randomly generated password and no user data at all otherwise.
+      // Note that if the username already exists, user_save will happily return FALSE.
+      $userinfo = array(
+        'name' => $username,
+        'pass' => $password,
+        'mail' => $vbuser['email'],
+        'init' => $username,
+        'status' => 1,
+        'authname_drupalvb' => $username,
+        'created' => $vbuser['joindate'],
+      );
+
+      $user = user_save('', $userinfo);
+      if ($user) {
+        watchdog('user', 'New external user: %user using module %module.', array('%user' => $name, '%module' => $module), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit'));
+        // Update the mapping table.
+        drupalvb_set_mapping($user->uid, $vbuser['userid']);
+      }
+      return TRUE;
+    }
+  } 
 }
 
 /**
  * Implementation of hook_auth().
  */
-function drupalvb_auth($username, $password, $server = NULL) {
+/*function drupalvb_auth($username, $password, $server = NULL) {
   if (!variable_get('drupalvb_dual_login', TRUE)) {
     return;
   }
@@ -184,7 +239,7 @@ function drupalvb_auth($username, $passw
       return TRUE;
     }
   }
-}
+}*/
 
 /**
  * Implementation of hook_user().
@@ -236,7 +291,7 @@ function drupalvb_user($op, &$edit, &$ac
  * @see drupalvb_user()
  */
 function drupalvb_user_login($account) {
-  $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", $account->name));
+  $vbuser = db_fetch_array(drupalvb_db_query_range("SELECT u.userid, ub.liftdate FROM {user} u LEFT JOIN {userban} ub ON ub.userid = u.userid WHERE u.username = '%s'", $account->name, 0, 1));
 
   // Create account in vB if user does not exist.
   if (!$vbuser) {
@@ -260,7 +315,7 @@ function drupalvb_user_login($account) {
   }
   else {
     drupal_set_message(t('Login to forums failed.'), 'error');
-    watchdog('drupalvb', t('Login to forum failed for user %user.', array('%user' => $account->name)), WATCHDOG_ERROR);
+    watchdog('drupalvb', 'Login to forum failed for user %user.', array('%user' => $account->name), WATCHDOG_ERROR);
     return FALSE;
   }
 }
@@ -271,11 +326,11 @@ function drupalvb_user_login($account) {
  * @see drupalvb_user()
  */
 function drupalvb_user_logout($account) {
-  $vbuser = db_fetch_array(drupalvb_db_query("SELECT userid, username FROM {user} WHERE username = '%s' LIMIT 1", $account->name));
+  $vbuser = db_fetch_array(drupalvb_db_query_range("SELECT userid, username FROM {user} WHERE username = '%s'", $account->name, 0, 1));
   if ($vbuser) {
     // Remove all vB cookies for current user.
     drupalvb_clear_cookies($vbuser['userid']);
-    watchdog('drupalvb', t('Forum session closed for user %username (@uid).', array('%username' => $vbuser['username'], '@uid' => $vbuser['userid'])));
+    watchdog('drupalvb', 'Forum session closed for user %username (@uid).', array('%username' => $vbuser['username'], '@uid' => $vbuser['userid']));
   }
 }
 
@@ -286,13 +341,13 @@ function drupalvb_user_validate($uid, &$
   $userid = db_result(db_query("SELECT userid FROM {drupalvb_users} WHERE uid = %d", $uid));
   // Validate the username.
   if (arg(1) == 'register' || user_access('change own username') || user_access('administer users')) {
-    if (db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE userid != %d AND LOWER(username) = LOWER('%s') LIMIT 1", $userid, $edit['name']))) {
+    if (db_result(drupalvb_db_query_range("SELECT userid FROM {user} WHERE userid <> %d AND LOWER(username) = LOWER('%s')", $userid, $edit['name'], 0, 1))) {
       form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name'])));
     }
   }
 
   // Validate the e-mail address.
-  if (db_num_rows(drupalvb_db_query("SELECT userid FROM {user} WHERE userid != %d AND LOWER(email) = LOWER('%s') LIMIT 1", $userid, $edit['mail'])) > 0) {
+  if (db_result(drupalvb_db_query_range("SELECT COUNT(userid) FROM {user} WHERE userid <> %d AND LOWER(email) = LOWER('%s')", $userid, $edit['mail'], 0, 1)) > 0) {
     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'))));
   }
 }
@@ -322,7 +377,7 @@ function drupalvb_user_update($account, 
   global $user;
 
   // Update data if user exists.
-  if ($vbuser = db_fetch_array(drupalvb_db_query("SELECT userid, salt FROM {user} WHERE username = '%s' LIMIT 1", $account->name))) {
+  if ($vbuser = db_fetch_array(drupalvb_db_query_range("SELECT userid, salt FROM {user} WHERE username = '%s'", $account->name, 0, 1))) {
     // Merge current username, salt, and finally edited values into one array,
     // so usernames may be altered (if allowed).
     drupalvb_update_user($account, array_merge(array('name' => $account->name), $vbuser, $edit));
@@ -554,6 +609,26 @@ function drupalvb_block_recent($display)
 }
 
 /**
+ * Implementation of hook_theme().
+ */
+function drupalvb_theme() {
+  return array(
+    'drupalvb_block_recent' => array(
+      'arguments' => array('recent' => NULL, 'vb_options' => NULL),
+    ),
+    'drupalvb_block_recent_user' => array(
+      'arguments' => array('recent' => NULL, 'vb_options' => NULL),
+    ),
+    'drupalvb_block_top_posters' => array(
+      'arguments' => array('items' => array()),
+    ),
+    'drupalvb_username' => array(
+      'arguments' => array('object' => NULL, 'class' => NULL),
+    ),
+  );
+}
+
+/**
  * Generate HTML for Recent threads/posts block.
  *
  * @todo Unused $recent['replycount'] for threads.
@@ -562,7 +637,7 @@ function theme_drupalvb_block_recent($re
   $items = array();
   $display_authors = variable_get('drupalvb_block_recent_authors', 0);
   foreach ($recent as $item) {
-    $link = l($item['title'], $item['url'], array(), $item['query'], $item['fragment']);
+    $link = l($item['title'], $item['url'], array('query' => $item['query'], 'fragment' => $item['fragment']));
     $items[] = ($display_authors ? t('!title <span>by !name</span>', array('!title' => $link, '!name' => theme('drupalvb_username', (object)$item))) : $link);
   }
   $output = theme('item_list', $items);
@@ -604,8 +679,8 @@ function drupalvb_block_recent_user($acc
 function theme_drupalvb_block_recent_user($recent, $vb_options) {
   $items = array();
   foreach ($recent as $item) {
-    $link_post = l($item['title'], $item['url'], array(), $item['query'], $item['fragment']);
-    $link_thread = l($item['threadtitle'], $item['url'], array(), $item['query']);
+    $link_post = l($item['title'], $item['url'], array('query' => $item['query'], 'fragment' => $item['fragment']));
+    $link_thread = l($item['threadtitle'], $item['url'], array('query' => $item['query']));
     $items[] = t('<span class="drupalvb-post">!title</span> <span class="drupalvb-thread">Thread: !thread</span>', array('!title' => $link_post, '!thread' => $link_thread));
   }
   return theme('item_list', $items);
@@ -617,7 +692,7 @@ function theme_drupalvb_block_recent_use
 function drupalvb_block_top_posters() {
   $items = array();
   $num_items = variable_get('drupalvb_block_recent_count', 5);
-  $result = drupalvb_db_query("SELECT posts AS count, userid, username AS name FROM {user} ORDER BY posts DESC LIMIT %d", $num_items);
+  $result = drupalvb_db_query_range("SELECT posts AS count, userid, username AS name FROM {user} ORDER BY posts DESC", 0, $num_items);
   while ($data = db_fetch_array($result)) {
     $items[$data['userid']] = $data;
   }
@@ -762,7 +837,7 @@ function drupalvb_private_messages() {
 
   $vb_options = drupalvb_get('options');
 
-  $output = '<p>'. "Below is a list of private messages you have received.  You may click on a user's name to see their profile, a message's title to view it, or a reply link to message the user in return." . '</p>';
+  $output = '<p>'. t("Below is a list of private messages you have received.  You may click on a user's name to see their profile, a message's title to view it, or a reply link to message the user in return.") .'</p>';
   $output .= l('View your inbox.', $vb_options['bburl'] .'/private.php');
 
   $result = drupalvb_db_query("SELECT userid, username FROM {user} WHERE username = '%s'", $user->name);
@@ -778,7 +853,7 @@ function drupalvb_private_messages() {
       array('data' => t('Operations')),
     );
 
-    $result = drupalvb_db_query("SELECT pm.pmid, pm.userid, pm.messageread, pmtext.fromusername, pmtext.fromuserid, pmtext.title, pmtext.message, pmtext.dateline FROM {pmtext} AS pmtext LEFT JOIN {pm} AS pm ON (pm.pmtextid = pmtext.pmtextid) WHERE pm.userid = %d AND pm.folderid != -1 ORDER BY pmtext.dateline DESC", $userinfo['userid']);
+    $result = drupalvb_db_query("SELECT pm.pmid, pm.userid, pm.messageread, pmtext.fromusername, pmtext.fromuserid, pmtext.title, pmtext.message, pmtext.dateline FROM {pmtext} AS pmtext LEFT JOIN {pm} AS pm ON (pm.pmtextid = pmtext.pmtextid) WHERE pm.userid = %d AND pm.folderid <> -1 ORDER BY pmtext.dateline DESC", $userinfo['userid']);
     while ($pm = db_fetch_array($result)) {
       $rows[] = array(
         l($pm['fromusername'], $vb_options['bburl'] .'/member.php?u='. $pm['fromuserid']),
@@ -856,7 +931,7 @@ function drupalvb_lookup_drupal_user($us
       'status' => 1,
     );
     $user = user_save('', $userinfo);
-    watchdog('drupalvb', t('New external user: %user.', array('%user' => $name)), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit'));
+    watchdog('drupalvb', 'New external user: %user.', array('%user' => $name), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit'));
 
     // Update the mapping table.
     drupalvb_set_mapping($user->uid, $vbuser['userid']);
@@ -890,7 +965,7 @@ function drupalvb_privatemsg($message, $
     case 'sent':
       // Verify that recipient exists in vB.
       $recipient = user_load(array('uid' => $message->recipient));
-      if (!$userid = db_result(drupalvb_db_query("SELECT userid FROM {user} WHERE username = '%s' LIMIT 1", $recipient->name))) {
+      if (!$userid = db_result(drupalvb_db_query_range("SELECT userid FROM {user} WHERE username = '%s'", $recipient->name, 0, 1))) {
         if (!$userid = drupalvb_create_user($recipient, (array)$recipient)) {
           // Indicates duplicate username (should not happen).
           return;
