Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-07-2009, 01:12 PM
GameDude GameDude is offline
 
Join Date: Jan 2006
Posts: 23
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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 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.
Reply With Quote
  #2  
Old 07-07-2009, 01:55 PM
1Unreal 1Unreal is offline
 
Join Date: Jul 2008
Location: London
Posts: 372
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Brilliant! Something like this should be included in vB as standard.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:24 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03541 seconds
  • Memory Usage 2,315KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (2)post_thanks_box
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit_info
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete