PDA

View Full Version : Code difference in VB 4 Query - Cant Connect to Database


wraggster
06-09-2011, 04:36 PM
Hi can anyone whos knowledgeable take a look at this code and spot anything that isnt compatible with VB4(the code works fine in VB3.8

<?php

// ---------------------------------------------------
// Start Set PHP Environment
// ---------------------------------------------------

error_reporting(E_ALL & ~E_NOTICE);

// ---------------------------------------------------
// End Set PHP Environment
// ---------------------------------------------------

// ---------------------------------------------------
// Start Define Important Constants
// ---------------------------------------------------

define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'vBExternal');

// ---------------------------------------------------
// End Define Important Constants
// ---------------------------------------------------

// ---------------------------------------------------
// Start Cache Of Any Needed Templates/Phrase's
// ---------------------------------------------------

$phrasegroups = array();

$specialtemplates = array(
'options',
);

$actiontemplates = array();

$globaltemplates = array();

// ---------------------------------------------------
// End Cache Of Any Needed Templates/Phrase's
// ---------------------------------------------------

// ---------------------------------------------------
// Start Call DB & Establish Connection
// ---------------------------------------------------

if( !file_exists('./includes/config.php'))
{
echo "includes/config.php does not exist. Cannot continue.";
exit;
}

require_once('./includes/class_core_old.php');
require('./includes/config.php');

DEFINE('DIR','.');
DEFINE('TABLE_PREFIX',$config['Database']['tableprefix']);

$vbulletin =& new vB_Registry(); // Fake an OOP Object

switch (strtolower($config['Database']['dbtype']))
{
// load standard MySQL class
case 'mysql':
case '':
{
$db =& new vB_Database($vbulletin);
break;
}

// load MySQLi class
case 'mysqli':
{
$db =& new vB_Database_MySQLi($vbulletin);
break;
}

// load extended, non MySQL class
default:
{
die('Fatal error: Database class not found');
}
}

require_once('./includes/functions.php');


// make database connection
$db->connect(
$config['Database']['dbname'],
$config['MasterServer']['servername'],
$config['MasterServer']['usepconnect'],
$config['MasterServer']['username'],
$config['MasterServer']['password'],
$config['SlaveServer']['servername'],
$config['SlaveServer']['usepconnect'],
$config['SlaveServer']['username'],
$config['SlaveServer']['password'],
$config['Mysqli']['ini_file']
);

$vbulletin->db =& $db;

// ---------------------------------------------------
// End Call DB & Establish Connection
// ---------------------------------------------------

// ---------------------------------------------------
// Start Require Globalized Settings
// ---------------------------------------------------

class vBulletinHook { function fetch_hook() { return false; } }

define('TIMENOW', time());
require_once('./includes/class_bbcode.php');
$Data = ""; // Page Output Variable


$datastore_class = (!empty($config['Misc']['datastore'])) ? $config['Misc']['datastore'] : 'vB_Datastore';

if ($datastore_class != 'vB_Datastore')
{
require_once('./includes/class_datastore.php');
}
$vbulletin->datastore =& new $datastore_class($vbulletin, $db);
$vbulletin->datastore->fetch($specialtemplates);

// ---------------------------------------------------
// End Require Globalized Settings
// ---------------------------------------------------

// ---------------------------------------------------
// Start Globalized Function - LoadTemplate
// ---------------------------------------------------

function LoadTemplate($template = ""){

$template = "vBExternal/{$template}";

if(!file_exists($template)){
RunError("System was unable to find the template '{$template}'");
}

if(!$Handler = fopen($template,'r')){
RunError("System was unable to open the template '{$template}'");
}

$template = fread($Handler,filesize($template));
fclose($Handler);

return $template;
}

// ---------------------------------------------------
// End Globalized Function - LoadTemplate
// ---------------------------------------------------

// ---------------------------------------------------
// Start Globalized Function - RunError
// ---------------------------------------------------

function RunError($message = ""){
echo "<font size='1' face='verdana'>There was an error while processing vBExternal:<br />{$message}</font>";
exit;
}

// ---------------------------------------------------
// End Globalized Function - RunError
// ---------------------------------------------------

// ---------------------------------------------------
// Start Globalized Function - ParseTemplate
// ---------------------------------------------------

function ParseTemplate($template, $parser = array(), $doGlobals = 0){
global $vbulletin;

if(is_array($parser)){
foreach($parser as $find => $replace){
$template = str_replace("{".$find."}", $replace, $template);
}
} else if($doGlobals){
$RepGlobals = array(
'url' => $vbulletin->options['bburl'],
);

foreach($RepGlobals as $find => $replace){
$template = str_replace("{".$find."}", $replace, $template);
}

}

return $template;
}

// ---------------------------------------------------
// End Globalized Function - ParseTemplate
// ---------------------------------------------------

// ---------------------------------------------------
// FUNCTION: output_NewestThreads
// DETAIL: Outputs X newest threads ordered by
// start date descending. $a
// specifies amount to show (Default 5)
// and $f can specify certain forums
// to grab from (1,3,4), by default it pulls
// from all forums.
// ---------------------------------------------------

function output_NewestThreads($a = 5,$f = ""){
global $db, $Data;

// Define amount to show
$Amount = ($a)? intval($a) : 5;

// Define Forum(s) To Pull From
$Forums = ($f)? $f: '';
$SQL = '';

if($Forums){
$SQL = " where forumid in({$Forums})";
}

// Load Template
$Template = LoadTemplate("newest_threads.html");

// Collect Data
$NewestThreads = $db->query("select * from ".TABLE_PREFIX."thread{$SQL} order by dateline desc limit 0,$Amount");

while($Thread = $db->fetch_array($NewestThreads)){
$Data .= ParseTemplate($Template,
array(
'threadid' => $Thread['threadid'],
'threadname' => $Thread['title'],
'postuserid' => $Thread['postuserid'],
'postusername' => $Thread['postusername'],
'replies' => vb_number_format($Thread['replycount']),
'views' => vb_number_format($Thread['views']),
'lastposter' => $Thread['lastposter'],
)
);
}

doOutput();
}

// ---------------------------------------------------
// FUNCTION: output_NewestReplies
// DETAIL: Outputs X newest threads ordered by
// last post descending. $a
// specifies amount to show (Default 5)
// and $f can specify certain forums
// to grab from (1,3,4), by default it pulls
// from all forums.
// ---------------------------------------------------

function output_NewestReplies($a = 5,$f = ""){
global $db, $Data;

// Define amount to show
$Amount = ($a)? intval($a) : 5;

// Define Forum(s) To Pull From
$Forums = ($f)? $f: '';
$SQL = '';

if($Forums){
$SQL = " where forumid in({$Forums})";
}

// Load Template
$Template = LoadTemplate("newest_threads.html");

// Collect Data
$NewestReplies = $db->query("select * from ".TABLE_PREFIX."thread{$SQL} order by lastpost desc limit 0,$Amount");

while($Thread = $db->fetch_array($NewestReplies)){
$Data .= ParseTemplate($Template,
array(
'threadid' => $Thread['threadid'],
'threadname' => $Thread['title'],
'postuserid' => $Thread['postuserid'],
'postusername' => $Thread['postusername'],
'replies' => vb_number_format($Thread['replycount']),
'views' => vb_number_format($Thread['views']),
'lastposter' => $Thread['lastposter'],
)
);
}

doOutput();
}

// ---------------------------------------------------
// FUNCTION: output_Content
// DETAIL: Outputs Content from the first post of a
// specific thread to show SEO content
// NOTE: The parenthesis excludes threads from
// the staff forums.
// ---------------------------------------------------

function output_Content($a = 5,$f = ""){
global $db, $Data, $vbulletin;

if (empty($_GET['id']))
{ $threadid = 28014; }
else { $threadid = ($_GET['id']); }

if (!preg_match("/^[0-9]{1,6}$/", $threadid)) die("Bad threadid, please re-enter.");

// Load Template
$Template = LoadTemplate("contentseo.html");

// Collect Data
$NewestNews = $db->query("SELECT post.*, thread.* FROM thread, post WHERE thread.visible=1 AND (thread.forumid!= 16 AND thread.forumid!= 29 AND thread.forumid!= 52) AND post.postid = thread.firstpostid AND thread.threadid = $threadid limit 1");


$bbcode_parser =& new vB_BbCodeParser($vbulletin, fetch_tag_list());

while($Content = $db->fetch_array($NewestNews)){
$Data .= ParseTemplate($Template,
array(
'threadid' => $Content['threadid'],
'threadname' => $Content['title'],
'forumid' => $Content['forumid'],
'seo' => strtolower(preg_replace('/\W+/', '-', preg_replace('/quot+/', '', preg_replace('/!+/', '', preg_replace('/%+/', '-percent', $Content['title']))))),
'postuserid' => $Content['postuserid'],
'postusername' => $Content['postusername'],
'iconid' => $Content['iconid'],
'post' => $bbcode_parser->parse(unhtmlspecialchars($Content['pagetext']), $f),
'comments' => vb_number_format($Content['replycount']),
'date' => vbdate($vbulletin->options['dateformat'], $Content['dateline']),
'time' => vbdate($vbulletin->options['timeformat'], $Content['dateline']),
'lastposter' => $Content['lastposter'],
)
);
}

doOutput();
}

// ---------------------------------------------------
// FUNCTION: output_News_Archive
// DETAIL: Outputs the first post from X threads
// from a specific forum (ordered by newest
// first)
// ---------------------------------------------------

function output_News_Archive($a = 5,$f = ""){
global $db, $Data, $vbulletin;

// query strings

$smonth = ($_GET['month']);
$emonth = $smonth+1;
$year = ($_GET['year']);

// start

$startday=01;
$startmonth=03;
$startyear=2004;
$start=mktime('01','03','2004',$smonth,$startday,$ year);

// end

$endday=01;
$endmonth=02;
$endyear=2012;
$end=mktime('01','02','2020',$emonth,$endday,$year );

// echo "$start";
// echo "och";
// echo "$end";

// Load Template
$Template = LoadTemplate("news.html");

// Collect Data
$NewestNews = $db->query("SELECT post.*, thread.* FROM thread, post WHERE thread.visible=1 AND (thread.forumid = 37 OR thread.forumid = 251 OR thread.forumid = 112 OR thread.forumid = 263 OR thread.forumid = 322) AND post.postid = thread.firstpostid AND thread.dateline >= '$start' AND thread.dateline <= '$end' order by thread.dateline ASC limit 2000");


$bbcode_parser =& new vB_BbCodeParser($vbulletin, fetch_tag_list());

while($News = $db->fetch_array($NewestNews)){
$Data .= ParseTemplate($Template,
array(
'threadid' => $News['threadid'],
'threadname' => $News['title'],
'forumid' => $News['forumid'],
'seo' => strtolower(preg_replace('/\W+/', '-', preg_replace('/quot+/', '', preg_replace('/!+/', '', preg_replace('/%+/', '-percent', $News['title']))))),
'postuserid' => $News['postuserid'],
'postusername' => $News['postusername'],
'iconid' => $News['iconid'],
'post' => $bbcode_parser->parse(unhtmlspecialchars($News['pagetext']), $f),
'comments' => vb_number_format($News['replycount']),
'date' => vbdate($vbulletin->options['dateformat'], $News['dateline']),
'time' => vbdate($vbulletin->options['timeformat'], $News['dateline']),
'lastposter' => $News['lastposter'],
)
);
}

doOutput();
}

/*
// ---------------------------------------------------
// FUNCTION: output_News
// DETAIL: Outputs the first post from X threads
// from a specific forum (ordered by newest
// first)
// ---------------------------------------------------

function output_News($a = 5,$f = ""){
global $db, $Data, $vbulletin;

// How many news posts should we display?
$Amount = '35';

// Which forums should we pull news from?
// Only display news from one forum? Change to ie. $Forum = '82';
$Forum = '36';

// Which users are allowed to post news?
// Only allow one user to post news: $User = '2181';
// Allow all users to post news? Remove "t.postuserid = $User AND" from Collect Data below. If it's already removed below you're allowing everyone to post news.
$User = '2181 or t.postuserid = 1 or t.postuserid = 32457';

if(!$Forum){
RunError("No specified forum to pull news from.");
}

// Load Template
$Template = LoadTemplate("news.html");

// Collect Data
$NewestNews = $db->query("SELECT post.*, thread.* FROM thread, post WHERE thread.visible=1 AND (thread.forumid = 51) AND post.postid = thread.firstpostid order by thread.dateline DESC limit 35");


$bbcode_parser =& new vB_BbCodeParser($vbulletin, fetch_tag_list());

while($News = $db->fetch_array($NewestNews)){
$Data .= ParseTemplate($Template,
array(
'threadid' => $News['threadid'],
'threadname' => $News['title'],
'forumid' => $News['forumid'],
'seo' => strtolower(preg_replace('/\W+/', '-', preg_replace('/quot+/', '', preg_replace('/!+/', '', preg_replace('/%+/', '-percent', $News['title']))))),
'postuserid' => $News['postuserid'],
'postusername' => $News['postusername'],
'iconid' => $News['iconid'],
'post' => $bbcode_parser->parse(unhtmlspecialchars($News['pagetext']), $f),
'comments' => vb_number_format($News['replycount']),
'date' => vbdate($vbulletin->options['dateformat'], $News['dateline']),
'time' => vbdate($vbulletin->options['timeformat'], $News['dateline']),
'lastposter' => $News['lastposter'],
)
);
}

doOutput();
}
*/

// ---------------------------------------------------
// FUNCTION: output_News
// DETAIL: Outputs the first post from X threads
// from a specific forum (ordered by newest
// first)
// Modified by BlueCrab to support multiple pages of output.
// ---------------------------------------------------

function output_News($page = 1, $a = 5,$f = ""){
global $db, $Data, $vbulletin;
// Amount of items to display per page:
$page_amount = 10;

// The starting value for displaying news:
$page_start = ($page - 1) * $page_amount;

// Load Template
$Template = LoadTemplate("news.html");

// Collect Data
$NewestNews = $db->query("SELECT post.*, thread.* FROM thread, post WHERE thread.visible=1 AND (thread.forumid = 51 OR thread.forumid = 263 OR thread.forumid = 322 OR thread.forumid = 290 OR thread.forumid = 242 OR thread.forumid = 112) AND post.postid = thread.firstpostid order by thread.dateline DESC limit " . $page_amount . " OFFSET " . $page_start);


$bbcode_parser =& new vB_BbCodeParser($vbulletin, fetch_tag_list());

while($News = $db->fetch_array($NewestNews)){
$Data .= ParseTemplate($Template,
array(
'threadid' => $News['threadid'],
'threadname' => $News['title'],
'forumid' => $News['forumid'],
'seo' => strtolower(preg_replace('/\W+/', '-', preg_replace('/quot+/', '', preg_replace('/!+/', '', preg_replace('/%+/', '-percent', $News['title']))))),
'postuserid' => $News['postuserid'],
'postusername' => $News['postusername'],
'iconid' => $News['iconid'],
'post' => $bbcode_parser->parse(unhtmlspecialchars($News['pagetext']), $f),
'comments' => vb_number_format($News['replycount']),
'date' => vbdate($vbulletin->options['dateformat'], $News['dateline']),
'time' => vbdate($vbulletin->options['timeformat'], $News['dateline']),
'lastposter' => $News['lastposter'],
)
);
}

doOutput();
}



// ---------------------------------------------------
// FUNCTION: output_UsersOnline
// DETAIL: Outputs All Users Online In The Forum
// ---------------------------------------------------

function output_UsersOnline(){
global $db, $Data, $vbulletin;

// Load Template
$Template = LoadTemplate("users_online.html");

$cache = array();

// Collect Data
$datecut = TIMENOW - $vbulletin->options['cookietimeout'];
$UsersOnline = $db->query("
SELECT
user.username, (user.options) AS invisible, user.usergroupid,
session.userid,
IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid
FROM " . TABLE_PREFIX . "session AS session
LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = session.userid)
WHERE session.lastactivity > $datecut and user.userid > 0
" . iif($vbulletin->options['displayloggedin'] == 1, "ORDER BY username ASC") . "
");

while($User = $db->fetch_array($UsersOnline)){
if(!$cache[$User['userid']]){
$cache[$User['userid']] = $User['userid'];

$Data .= ParseTemplate($Template,
array(
'userid' => $User['userid'],
'username' => $User['username'],
)
);
}
}

unset($cache);
doOutput();
}

function doOutput(){
global $Data, $vbulletin;

echo ParseTemplate($Data,"",1);
$Data = "";
}
?>

Disasterpiece
06-09-2011, 04:49 PM
Seems a weird way to establish a database connection...
why not simply including global.php and use the db connection from $vbulletin->db ?

require_once('./includes/class_bbcode.php');
Are you sure this file exists and actually does something usable?

Overall a not-so-good way to use vbulletin methods in external scripts, i suggest to use this instead: https://vborg.vbsupport.ru/showthread.php?t=228112