config =& drupalvb_fetch_config(); $query = "SELECT data FROM ".VB_PREFIX."datastore WHERE title = 'options'"; $result = mysql_query($query, $vblink); $data = db_fetch_array($result); $vbulletin->options = unserialize($data['data']); return $vblink; } //Function to close the link to the vB database function drupalvb_close($vblink) { mysql_close($vblink); } //Function to grab the configuration/options of the vB installation function drupalvb_fetch_config() { $config = array(); require_once('config.php'); define('COOKIE_PREFIX', (empty($config['Misc']['cookieprefix']) ? 'bb' : $config['Misc']['cookieprefix'])); define('COOKIE_SALT', 'VBF6FC6A2E'); return $config; } //Function to parse (if necessary) and display appropriate drupalvb messages function drupalvb_msg($msg, $arg = "unknown") { switch ($msg) { case DVBERROR: drupal_set_message("Error: " . $arg); break; case DVBLOGIN: drupal_set_message("Logging into the website gives you access to " . l(variable_get('drupalvb_forum_name', ''), variable_get('drupalvb_forum_address', ''), array('target' => '_blank')) . " as well."); drupal_set_message("Follow the link to the left to stop by with any questions or comments!"); break; case DVBREGISTER: drupal_set_message("You are now registered to access the site and the forums!"); break; case DVBUPDATE: drupal_set_message("User information updated on the site and the forums."); break; default: drupal_set_message($arg); } return; } //Function to set the necessary cookies for the user to be logged into the forum function drupalvb_set_login_cookies($userid, $password) { $expire = time() + 60 * 60 * 24 * 365; setcookie(COOKIE_PREFIX . 'sessionhash', md5("drupalvb" . $userid), $expire, VB_COOKIE_PATH, VB_COOKIE_DOMAIN); setcookie(COOKIE_PREFIX . 'lastvisit', time(), $expire, VB_COOKIE_PATH, VB_COOKIE_DOMAIN); setcookie(COOKIE_PREFIX . 'lastactivity', '0', $expire, VB_COOKIE_PATH, VB_COOKIE_DOMAIN); setcookie(COOKIE_PREFIX . 'userid', $userid, $expire, VB_COOKIE_PATH, VB_COOKIE_DOMAIN); setcookie(COOKIE_PREFIX . 'password', md5($password . COOKIE_SALT), $expire, VB_COOKIE_PATH, VB_COOKIE_DOMAIN); return; } //Function to clear any database cookies. function drupalvb_clear_cookies() { //Clear our login cookies beginning $_COOKIE[COOKIE_PREFIX.'sessionhash'] = null; $_COOKIE[COOKIE_PREFIX.'lastvisit'] = null; $_COOKIE[COOKIE_PREFIX.'lastactivity'] = null; $_COOKIE[COOKIE_PREFIX.'userid'] = null; $_COOKIE[COOKIE_PREFIX.'password'] = null; /* setcookie(COOKIE_PREFIX . 'sessionhash', '', 1); setcookie(COOKIE_PREFIX . 'lastvisit', '', 1); setcookie(COOKIE_PREFIX . 'lastactivity', '', 1); setcookie(COOKIE_PREFIX . 'userid', '', 1); setcookie(COOKIE_PREFIX . 'password', '', 1); */ return; } //Function to create an entry in the vB database user table function drupalvb_create_vb_user($username, $password, $email, $vblink){ //Double check to make sure something fishy didn't happen... $query = "SELECT userid, username FROM ".VB_PREFIX."user WHERE username = '$username'"; $result = mysql_query($query, $vblink); if (db_fetch_array($result)) { //The user existed somehow, so send the error and kick it out with a fail drupalvb_msg(DVBERROR, "Your username already existed on the forums. The duplication problem has been logged. " . "If this error message continues to display, please contact the site administrator."); watchdog ("drupalvb", "Attempted to create a duplicate vB user.", WATCHDOG_ERROR); return false; } //Set up the necessary variables $salt = ''; for ($i = 0; $i < 3; $i++) { $salt .= chr(rand(32, 126)); } $passhash = md5(md5($password) . $salt); $passdate = date('Y-m-d H:i:s', time()); $joindate = time(); //Attempt to grab the user title from the database $query = "SELECT title FROM ".VB_PREFIX."usertitle ORDER BY minpost asc limit 1"; $result = mysql_query($query, $vblink); if ($resarray = db_fetch_array($result)) $usertitle = $resarray['title']; else $usertitle = "Junior Member"; //Grab the default timezone offset, divided by 3600 b/c the system.module timezone form is in seconds $timezone = variable_get('drupalvb_default_timezone', '-18000'); $timezone = intval($timezone) / 3600; /* Default new user options: I got these by setting up a new user how I wanted and looking in the database to see what options were set for him. */ $options = variable_get('drupalvb_default_options', '3159'); //Default usergroup id $usergroupid = variable_get('drupalvb_default_usergroup', '2'); //Set up the insertion query $query = "INSERT INTO ".VB_PREFIX."user (username, usergroupid, password, passworddate, " . "usertitle, email, salt, showvbcode, languageid, timezoneoffset, posts, " . "joindate, lastvisit, lastactivity, options) " . "VALUES ('$username', '$usergroupid', '$passhash', '$passdate', " . "'$usertitle', '$email', '$salt', '1', '1', '$timezone', '0', " . "'$joindate', '$joindate', '$joindate', '$options');"; $result = mysql_query($query, $vblink); //Return true if the vB user was created successfully return true; } //Function to update an entry in the vB database user table function drupalvb_update_vb_user($username, $password, $email, $salt, $vblink){ //Set up the necessary variables $setpass = ''; if($password != ''){ $passhash = md5($password . $salt); $passdate = date('Y-m-d H:i:s', time()); $setpass = "password='$passhash', passworddate='$passdate', salt='$salt', "; } $lastdate = time(); //Set up the insertion query $query = "UPDATE ".VB_PREFIX."user SET " . $setpass . "email='$email', lastactivity='$lastdate' " . "WHERE username='$username'"; $result = mysql_query($query, $vblink); //Return the new password hash for the updated vB user, purpose of updating cookies return; } //Function to count the number of users online, guests and members. function drupalvb_get_users($vblink) { global $vbulletin; $datecut = time() - $vbulletin->options['cookietimeout']; $numbervisible = 0; $numberregistered = 0; $numberguest = 0; $query = "SELECT user.username, user.usergroupid, session.userid, session.lastactivity " . "FROM ".VB_PREFIX."session AS session LEFT JOIN ".VB_PREFIX."user AS user ON(user.userid = session.userid) WHERE session.lastactivity > $datecut"; $forumusers = mysql_query($query, $vblink); if ($vbulletin->userinfo['userid']) { // fakes the user being online for an initial page view of index.php $vbulletin->userinfo['joingroupid'] = iif($vbulletin->userinfo['displaygroupid'], $vbulletin->userinfo['displaygroupid'], $vbulletin->userinfo['usergroupid']); $userinfos = array ( $vbulletin->userinfo['userid'] => array ( 'userid' => $vbulletin->userinfo['userid'], 'username' => $vbulletin->userinfo['username'], 'invisible' => $vbulletin->userinfo['invisible'], 'inforum' => 0, 'lastactivity' => time(), 'usergroupid' => $vbulletin->userinfo['usergroupid'], 'displaygroupid' => $vbulletin->userinfo['displaygroupid'], ) ); } else { $userinfos = array(); } $inforum = array(); while ($loggedin = db_fetch_array($forumusers)) { $userid = $loggedin['userid']; if (!$userid) { // Guest $numberguest++; $inforum["$loggedin[inforum]"]++; } else if (empty($userinfos["$userid"]) OR ($userinfos["$userid"]['lastactivity'] < $loggedin['lastactivity'])) { $userinfos["$userid"] = $loggedin; } } foreach ($userinfos AS $userid => $loggedin) { $numberregistered++; if ($userid != $vbulletin->userinfo['userid']) { $inforum["$loggedin[inforum]"]++; } } // memory saving unset($userinfos, $loggedin); return array('guests' => $numberguest, 'members' => $numberregistered); } //Function to grab either the number of new posts since a user's last the last day. function drupalvb_get_posts($vblink, $scope = 'last') { global $user; //Queries the vB user database to find a matching set of user data $query = "SELECT userid, username, lastvisit FROM ".VB_PREFIX."user WHERE username = '$user->name'"; $result = mysql_query($query, $vblink); //Make sure a user is logged in to get their last visit and appropriate post count. if ($vbulletin->userinfo = db_fetch_array($result)) { if ($scope == 'last') $datecut = $vbulletin->userinfo['lastvisit']; elseif ($scope == 'daily') $datecut = time() - 86400; $query = "SELECT postid FROM ".VB_PREFIX."post WHERE dateline > $datecut"; $result = mysql_query($query, $vblink); $posts = mysql_num_rows($result); } else { $posts = 0; } return $posts; } //Function to grab the usergroup names/ids from the database. function drupalvb_get_usergroups() { //Initialize drupalvb; set up $vbulletin and connects to the database $vblink = drupalvb_vb_init(); //Queries the vB user database to find a matching set of user data $query = "SELECT usergroupid, title FROM ".VB_PREFIX."usergroup"; $result = mysql_query($query, $vblink); $usergroups = array(); while ($data = db_fetch_object($result)) { /*foreach ($data as $usergroup) { drupal_set_message("Key: " . $usergroup['usergroupid'] . ", Value: " . $usergroup['title']); }*/ $usergroups[$data->usergroupid] = $data->title; } if (!$usergroups) { $usergroups[] = 'Error: Unable to access database'; } drupalvb_close($vblink); return $usergroups; }