vBulletin Class
I originally made this because I wanted to be able to link my main website with my forum without having to rely on vBulletin-only functions/classes. With this class, you will be able to make new threads, make posts, send PMs, and check vBulletin login information. If you use a prefix for your tables, you will have to manually edit the queries.
PHP Code:
<?php
/**
* @author GameDude
*/
/**
* @use Allows interaction with various vBulletin features without relying on vBulletin functions.
*/
Class vBulletin {
/**
* @var db_conn Stores the MySQL connection created in connectToVb.
*/
var $db_conn;
/**
* @use Constructor for the vBulletin class.
*
* @param database Name of the database used by the vBulletin installation.
* @param db_user Username used to connect to the database.
* @param db_pass Password for db_user.
* @param db_host Host of the database; this is usually localhost.
*/
function vBulletin($database, $db_user, $db_pass, $db_host) {
$this->connectToVb($database, $db_user, $db_pass, $db_host);
}
/**
* @use Establishes a MySQL connection to the provided database and stores the connection in the db_conn variable.
*
* @param database See database in the constructor.
* @param user See db_user in the constructor.
* @param pass See db_pass in the constructor.
* @param host See db_host in the constructor.
*/
function connectToVb($database, $user, $pass, $host) {
$this->db_conn = mysql_connect("$host", "$user", "$pass") or die ('I cannot connect to vBulletin because: ' . mysql_error());
mysql_select_db("$database", $this->db_conn);
}
/**
* @use Creates a new thread in the desired forum.
*
* @param forumid The ID number of the forum to create the thread in.
* @param username Username to create the thread as.
* @param title Title of the thread.
* @param message Message to put in the first post.
* @param open Defaults to true. True means that the created thread is Open, and likewise false means it is closed.
*/
function makeThread($forumid, $username, $title, $message, $open = true) {
$_open = $open ? 1 : 0;
$results = mysql_fetch_array(mysql_query("SELECT * FROM thread ORDER BY threadid DESC", $this->db_conn));
$threadid = $results['threadid'] + 1;
$userid = $this->getUserID($username);
$results = mysql_fetch_array(mysql_query("SELECT * FROM post ORDER BY postid DESC", $this->db_conn));
$lastpostid = $results['postid'] + 1;
$time = time();
$num_posts = 0;
$num_threads = 0;
$query = mysql_query("SELECT * FROM thread WHERE forumid = '$forumid'", $this->db_conn);
while($results = mysql_fetch_array($query)) {
$num_posts += mysql_num_rows(mysql_query("SELECT * FROM post WHERE threadid = '$results[threadid]'", $this->db_conn));
$num_threads += 1;
}
$num_threads += 1;
$num_posts += 1;
$query = mysql_query("INSERT INTO post(postid, threadid,parentid,username,userid,title,dateline,pagetext,visible) VALUES ('$lastpostid','$threadid', '0', '$username', '$userid', '$title', '$time', '$message','1')", $this->db_conn);
$query = mysql_query("INSERT INTO thread(threadid,title,firstpostid,lastpostid,lastpost,forumid,open,postusername,postuserid,lastposter,visible) VALUES ('$threadid', '$title', '$lastpostid', '$lastpostid', '$time', '$forumid', '$_open', '$username', '$userid', '$username', '1')", $this->db_conn);
$query = mysql_query("UPDATE forum SET lastpost = '$time', lastposter = '$username', lastpostid = '$lastpostid', lastthread = '$title', lastthreadid = '$threadid', replycount = '$num_posts', threadcount = '$num_threads' WHERE forumid = '$forumid'", $this->db_conn);
}
/**
* @use Creates a reply in the provided thread.
*
* @param threadid The ID number of the thread to create the reply in.
* @param username The username to create the reply as.
* @param message The actual content of the reply.
*/
function makePost($threadid, $username, $message) {
$results = mysql_fetch_array(mysql_query("SELECT * FROM thread WHERE threadid = '$threadid'", $this->db_conn));
$forumid = $results['forumid'];
$parentid = $results['firstpostid'];
$views = $results['views'] + 1;
$ttitle = "Re: " .$results['title'];
$title = $results['title'];
$userid = $this->getUserID($username);
$results = mysql_fetch_array(mysql_query("SELECT * FROM post ORDER BY postid DESC", $this->db_conn));
$lastpostid = $results['postid'] + 1;
$replycount = mysql_num_rows(mysql_query("SELECT * FROM post WHERE threadid = '$threadid' AND parentid != '0'", $this->db_conn));
$time = time();
$num_posts = 0;
$num_threads = 0;
$query = mysql_query("SELECT * FROM thread WHERE forumid = '$forumid'", $this->db_conn);
while($results = mysql_fetch_array($query)) {
$num_posts += mysql_num_rows(mysql_query("SELECT * FROM post WHERE threadid = '$results[threadid]'", $this->db_conn));
$num_threads += 1;
}
$num_posts += 1;
mysql_query("INSERT INTO post(postid, threadid,parentid,username,userid,title,dateline,pagetext,visible) VALUES ('$lastpostid','$threadid', '$parentid', '$username', '$userid', '$ttitle', '$time', '$message','1')", $this->db_conn);
mysql_query("UPDATE thread SET lastpostid = '$lastpostid', lastpost = '$time', lastposter = '$username', views = '$views', replycount = '$replycount' WHERE threadid = '$threadid'", $this->db_conn);
mysql_query("UPDATE forum SET lastpost = '$time', lastposter = '$username', lastpostid = '$lastpostid', lastthread = '$title', lastthreadid = '$threadid', replycount = '$num_posts', threadcount = '$num_threads' WHERE forumid = '$forumid'", $this->db_conn);
}
/**
* @use Sends a Private Message to the provided user.
*
* @param from The username to send the message from.
* @param to The username or array of usernames to send the message to.
* @param title The title of Private Message.
* @param message The actual content of the Private Message.
* @param type Should be either "bcc" or "cc". Defaults to bcc.
*/
function sendPM($from, $to, $title, $message, $type = "bcc") {
$results = mysql_fetch_array(mysql_query("SELECT * FROM pmtext ORDER BY pmtextid DESC", $this->db_conn));
$pmtextid = $results['pmtextid'] + 1;
if(!is_array($to)) {
$results = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '$to'", $this->db_conn));
$pmtotal = $results['pmtotal'] + 1;
$pmunread = $results['pmunread'] + 1;
$to_id = $results['userid'];
mysql_query("UPDATE user SET pmtotal = '$pmtotal', pmunread = '$pmunread' WHERE username = '$to'", $this->db_conn);
mysql_query("INSERT INTO pm(pmtextid, userid) VALUES ('$pmtextid', '$to_id')", $this->db_conn);
$touserarray = $this->buildToUserArray($type, $to_id, $to);
} else if (is_array($to)) {
$first = true;
foreach($to as $recipient) {
$results = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '$recipient'", $this->db_conn));
$pmtotal = $results['pmtotal'] + 1;
$pmunread = $results['pmunread'] + 1;
$to_id = $results['userid'];
mysql_query("UPDATE user SET pmtotal = '$pmtotal', pmunread = '$pmunread' WHERE username = '$recipient'", $this->db_conn);
if($first) {
mysql_query("INSERT INTO pm(pmtextid, userid) VALUES ('$pmtextid', '$to_id')", $this->db_conn);
$pmid = mysql_insert_id($this->db_conn);
$first = false;
} else {
mysql_query("INSERT INTO pm(pmtextid, userid, parentpmid) VALUES ('$pmtextid', '$to_id', '$pmid')", $this->db_conn);
}
}
$touserarray = $this->buildMultipleToUserArray($to, $type);
}
$from_id = $this->getUserID($from);
$time = time();
mysql_query("INSERT INTO pmtext(fromuserid, fromusername, title, message, touserarray, dateline) VALUES ('$from_id', '$from', '$title', '$message', '$touserarray', '$time')", $this->db_conn);
}
/**
* @use Convenience function that sends a Private Message to every member of the forums.
*
* @param from See from in sendPM.
* @param title See title in sendPM.
* @param message See message in sendPM.
*/
function sendMassPM($from, $title, $message) {
$query = mysql_query("SELECT * FROM user WHERE username != '$from'", $this->db_conn);
while($results = mysql_fetch_array($query)) {
$this->sendPM($from, $results['username'], $title, $message);
}
}
/**
* @use Checks the provided login credentials with the information stored in the database.
*
* @param username The username the client is "logging in" as.
* @param password The password provided for username by the client.
*
* @return Returns true if the provided information is correct or false if the password is incorrect.
*/
function checkLogin($username, $password) {
$results = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '$username'", $this->db_conn));
$salt = $results['salt'];
if($results['password'] == md5(md5($password).$salt)) {
return true;
} else {
return false;
}
}
/**
* @use Convenience function that gets the userid of the provided username.
*
* @param username The username to get the userid of.
*
* @return Returns 1 if username doesn't exist or the userid of the provided username.
*/
function getUserID($username) {
$results = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '$username'", $this->db_conn));
if($results['userid'] == 0) {
return 1;
} else {
return $results['userid'];
}
}
/**
* @use Creates the "touserarray" used by vBulletin in Private Messages.
*
* @param type See type in sendPM.
* @param to_id The userid of the username the message is being sent to.
* @param username The username of the client the message is being sent to.
*
* @return The string generated with the provided information.
*/
function buildToUserArray($type, $to_id, $username) {
return "a:1:{s:" . strlen($type) . ":\"$type\";a:1:{i:$to_id;s:" . strlen($username) . ":\"$username\";}}";
}
/**
* @use Creates the "touserarray" used by vBulletin in Private Messages for messages with multiple recipients.
*
* @param recipients Array that contains the usernames of the members the message is going to be sent to.
* @param type See type in sendPM.
*
* @return The string generated with the provided information.
*/
function buildMultipleToUserArray($recipients, $type) {
$base = "a:1:{s:" . strlen($type) . ":\"$type\";a:" . count($recipients) .":{";
foreach ($recipients as $recipient) {
$base .= "i:" .$this->getUserID($recipient) . ";s:" .strlen($recipient) .":\"$recipient\";";
}
$base .= "}}";
return $base;
}
}
All I request is you do not post this elsewhere and claim it as your own.
|