<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin Hack Management System                                 # ||
|| # ---------------------------------------------------------------- # ||
|| # All PHP code in this file is ©2004 GameCrash.org.                # ||
|| # http://www.gamecrash.org (soon) | http://www.letzplay.de         # ||
|| # ---------------------------------------------------------------- # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

// #################### vbhms_run_instructions ####################
// This is the heart of the vBulletin Hack Management System
function vbhms_run_instructions($instructions, $schema)
{
	global $xmlschema;
	
	for ($i = 0; $i < count($instructions); $i++)
	{
		$dofunction = $xmlschema[$schema]['elements'][$instructions[$i]['do']]['executefunction'];
		$dofunction(vbhms_parse_instructions($instructions[$i]));
	}
}

// #################### vbhms_parse_instructions ####################
// Parse any {PARSE] element
function vbhms_parse_instructions($instruction)
{
	// Parse data
	$instruction['data'] = vbhms_parse_instruction(xml_unescape_cdata($instruction['data']));
	
	// Parse Attributes
	if (is_array($instruction['attrs']))
	{
		$attrs = $instruction['attrs'];
		while(list($key, $val) = each($attrs)){
			$instruction['attrs'][$key] = vbhms_parse_instruction($instruction['attrs'][$key]);
		}
	}
	
	return $instruction;
}

// #################### vbhms_parse_instructions ####################
// Parse any {PARSE] element
function vbhms_parse_instruction($element)
{
	global $vboptions, $vbphrase;
	
	if (!empty($element) && preg_match('/^{PARSE}/', $element))
	{
		eval('$element = "' . substr($element, 7) . '";');
	}
	
	return $element;
}

// #################### vbhms_run_instructions ####################
// This is the heart of the vBulletin Hack Management System
function vbhms_add_executefunction($element, $function, $info, $schemas = 'install,uninstall,update,repair')
{
	global $xmlschema;
	
	$schemas = explode(',', $schemas);
	for ($i = 0; $i < count($schemas); $i++)
	{
		$xmlschema[$schemas[$i]]['elements'][$element] = $info;
		$xmlschema[$schemas[$i]]['elements'][$element]['executefunction'] = $function;
	}
}

// #################### vbhms_print_header ####################
// Print a nice installer-lookalike header
function vbhms_print_header($type='', $hackname='', $steptitle='')
{
	global $vboptions, $step, $totalsteps, $hackinfo;
	
	$hackstring = '';
	$stepstring = '';
	
	if (!empty($type))
	{
		$type = " $type";
	}
	
	if (!empty($hackname))
	{
		$hackstring = " - $hackname";
	}
	elseif (is_array($hackinfo))
	{
		$hackstring = " - $hackinfo[hackname]";
	}
	
	if ($step > 0 && $totalsteps > 0)
	{
		$stepstring = " (step $step of $totalsteps)";
	}
	
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html>
			<head>
				<meta content="text/html; charset=windows-1252" http-equiv="Content-Type" />
				<title>vBulletin Hack Management System' . $hackstring . $type . $stepstring . '</title>
				<link rel="stylesheet" href="../cpstyles/' . $vboptions['cpstylefolder'] . '/controlpanel.css" />
				<style type="text/css">
					#all
					{
						margin: 10px;
					}
					#all p, #all td, #all li, #all div
					{
						font-size: 11px;
						font-family: verdana, arial, helvetica, sans-serif;
					}
				</style>
			</head>
			<body style="margin:0px">
				<table cellpadding="4" cellspacing="0" border="0" width="100%" class="navbody" style="border:outset 2px">
					<tr>
						<td width="160"><img src="../cpstyles/' . $vboptions[cpstylefolder] . '/cp_logo.gif" alt="" title="vBulletin 3 &copy;2003 Jelsoft Enterprises Ltd." /></td>
						<td style="padding-left:100px">
							<a href="index.php"><b>vBulletin Hack Management System</b></a><br />
							(Please be patient as some parts may take some time)<br />
							<br />
							<b style="font-size:10pt;">' . $hackname . $type . '</b>' . $stepstring . '
						</td>
					</tr>
				</table>
				<div id="all">';

	if ($steptitle)
	{
		echo "<p style=\"font-size:10pt;\"><b><u>$steptitle</u></b></p>\n";
	}

	flush();
	
	define('HEADERPRINTED', true);
}

// #################### vbhms_print_next_step ####################
// Print a nice installer-lookalike footer
function vbhms_print_next_step()
{
	global $step, $totalsteps, $session, $hack, $hackinstructions;

	if (defined('NONEXTSTEP'))
	{
		return;
	}

	define('NONEXTSTEP', true);

	$nextstep = $step + 1;

	if (defined('SCRIPTCOMPLETE') || $step >= $totalsteps)
	{
		$buttonvalue = ' Proceed ';
		$title = 'vBulletin 3 Hack Management System';
		$hiddenfields = '';
	}
	else
	{
		$buttonvalue = " Next Step ($nextstep/$totalsteps) ";
		$title = $hackinstructions[$step+1]['title'];
		$hiddenfields = '<input type="hidden" name="hack" value="' . $hack . '" /><input type="hidden" name="step" value="' . $nextstep . '" />';
	}

	echo '</div>
		<form action="' . THIS_SCRIPT . '" method="get" name="nextStep">' . $hiddenfields . '
			<table cellpadding="4" cellspacing="0" border="0" width="100%" class="navbody" style="padding:4px; border:outset 2px;">
				<tr align="center">
					<td><b>Click the button on the right to proceed.</b><br />vBulletin Hack Management System &copy;2004 GameCrash.org.</td>
					<td><input type="submit" class="button" value="' . $buttonvalue . '" title="' . $title . '" /></td>
				</tr>
			</table>
		</form>';
}

// #################### vbhms_print_footer ####################
// Exit the script
function vbhms_print_footer($donextstep=true)
{
	if ($donextstep)
	{
		vbhms_print_next_step();
	}
	
	unset($GLOBALS['DEVDEBUG']);
	print_cp_footer();
	
	exit;
}

// #################### vbhms_print_fatal_error ####################
// Exit the script
function vbhms_print_fatal_error($errormessage='Unknown Error')
{
	if (!defined('HEADERPRINTED'))
	{
		vbhms_print_header('Fatal Error', '', 'Fatal Error');
	}
	echo '<blockquote><p>&nbsp;</p>';
	echo '<p>There has been a fatal error in the vBulletin Hack Installation System.</p>';
	echo "<p><b>$errormessage</b></p>";
	echo '<p>I\'m sorry, I can\'t go on. Please contact the author of this hack.</p>';
	echo '<p>&nbsp;</p></blockquote>';
	
	vbhms_print_footer(false);
}

// #################### vbhms_print_info_line ####################
// A wrapper to print any info line
function vbhms_print_info_line($text)
{
	vbhms_print_info_line_start($text);
	vbhms_print_info_line_end();
}

// #################### vbhms_print_info_line_start ####################
// Start an info line
function vbhms_print_info_line_start($text)
{
	global $inlist;
	if ($inlist > 0)
	{
		echo "<li>$text";
	}
	else
	{
		echo "<p>$text";
	}
}

// #################### vbhms_print_info_line_end ####################
// Close an info line
function vbhms_print_info_line_end($text = '')
{
	global $inlist;
	if ($inlist > 0)
	{
		echo "$text</li>";
	}
	else
	{
		echo "$text</p>";
	}
}

// #################### vbhms_print_list_start ####################
// Begin a new list
function vbhms_print_list_start()
{
	global $inlist;
	echo '<p><ul>';
	$inlist++;
}

// #################### vbhms_print_list_end ####################
// Close the list
function vbhms_print_list_end()
{
	global $inlist;
	echo '</ul></p>';
	$inlist--;
}

// #################### vbhms_replace_table_prefix ####################
// Replace {{{TABLE_PREFIX}}} with TABLE_PREFIX
function vbhms_replace_table_prefix($text)
{
	return str_replace('{{{TABLE_PREFIX}}}', TABLE_PREFIX, $text);
}

// #################### vbhms_insert_new_phrase ####################
// Create a new phrase
function vbhms_insert_new_phrase($varname, $text, $phrasetypeid, $languageid=0)
{
	global $DB_site;
	$DB_site->query("REPLACE INTO " . TABLE_PREFIX . "phrase (languageid, phrasetypeid, varname, text) VALUES ({$languageid}, {$phrasetypeid}, '" . addslashes($varname) . "', '" . addslashes($text) . "')");
}

// #################### vbhms_delete_phrase ####################
// Drop an existing phrase if it exists
function vbhms_delete_phrase($varname, $phrasetypeid, $languageid=0)
{
	global $DB_site;
	$DB_site->query("DELETE FROM " . TABLE_PREFIX . "phrase WHERE languageid={$languageid} AND phrasetypeid={$phrasetypeid} AND varname='" . addslashes($varname) . "'");
}

// #################### vbhms_run_custom_function ####################
// Run a function with an unknown number of arguments
function vbhms_run_custom_function($function, $arguments='')
{
	$args = explode('-|||||-', $arguments);
	call_user_func_array($function, $args);
}

// #################### vbhms_add_last_step ####################
// Add a final step to insert hack information into the database
function vbhms_add_last_step($type, &$hackinstructions)
{
	global $hackinfo, $hack;
	
	$query = '';
	$description = '';
	
	switch ($type)
	{
		case 'install':
			$query = "INSERT INTO " . TABLE_PREFIX . "vbhms_hack (hackname, hackversion, installdate, upgradedate) VALUES ('" . addslashes($hack) . "', '{$hackinfo['hackversion']['data']}', " . TIMENOW . ", " . TIMENOW . ")";
			$description = 'Inserting hack information into the vBulletin Hack Management System database';
			break;
		case 'uninstall':
			$query = "DELETE FROM " . TABLE_PREFIX . "vbhms_hack WHERE hackname='" . addslashes($hack) . "'";
			$description = 'Removing hack information from the vBulletin Hack Management System database';
			break;
		default:
			$query = "UPDATE " . TABLE_PREFIX . "vbhms_hack SET hackversion='{$hackinfo['hackversion']['data']}', upgradedate=" . TIMENOW . " WHERE hackname='" . addslashes($hack) . "'";
			$description = 'Updating hack information in the vBulletin Hack Management System database';
			break;
	}
	
	$hackinstructions[] = array
	(
		'title' => 'Update Hack Information',
		'actions' => array
		(
			array('do' => 'query', 'data' => $query, 'attrs' => array('description' => $description))
		)
	);
}

?>