<?php
include("a_images/class.php");
	
function clean($value) {
if (get_magic_quotes_gpc()) $value = stripslashes($value);

if (!is_numeric($value)) $value = @mysql_real_escape_string($value);

return $value;
}


//This function loads the templates
function loadTemplate($template) { 
return file_get_contents($template);
}

function check($val) {
   // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
   // this prevents some character re-spacing such as <java\0script>
   // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
   $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
   
   // straight replacements, the user should never need these since they're normal characters
   // this prevents like <IMG SRC=&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&#X3A&#X61&#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29>
   $search = 'abcdefghijklmnopqrstuvwxyz';
   $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   $search .= '1234567890!@#$%^&*()';
   $search .= '~`";:?+/={}[]-_|\'\\';
   for ($i = 0; $i < strlen($search); $i++) {
      // ;? matches the ;, which is optional
      // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
   
      // &#x0040 @ search for the hex values
      $val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
      // &#00064 @ 0{0,7} matches '0' zero to seven times
      $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
   }
   
   // now the only remaining whitespace attacks are \t, \n, and \r
   $ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
   $ra2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
   $ra = array_merge($ra1, $ra2);
   
   $found = true; // keep replacing as long as the previous round replaced something
   while ($found == true) {
      $val_before = $val;
      for ($i = 0; $i < sizeof($ra); $i++) {
         $pattern = '/';
         for ($j = 0; $j < strlen($ra[$i]); $j++) {
            if ($j > 0) {
               $pattern .= '(';
               $pattern .= '(&#[x|X]0{0,8}([9][a][b]);?)?';
               $pattern .= '|(&#0{0,8}([9][10][13]);?)?';
               $pattern .= ')?';
            }
            $pattern .= $ra[$i][$j];
         }
         $pattern .= '/i';
         $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
         $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
         if ($val_before == $val) {
            // no replacements were made, so exit the loop
            $found = false;
         }
      }
   }
   return $val;
}

function luserreviews($game, $system, $num, $template) {
global $setting;
global $pre;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));
	
    $queryrra .= "SELECT * FROM ".$pre."userreviews WHERE 1=1 ";
	if ($game) {
	$queryrra .= "AND games = '".$game."' ";
	}
	if ($system) {
	$queryrra .= "AND systems = '".$system."' ";
	}
	$queryrra .= "ORDER BY `id` DESC LIMIT ".$num;
	$sql = mysql_query($queryrra);
    while ($row = mysql_fetch_array($sql)) {
    $game = $row[games];
	$system = $row[systems];

	$systems = mysql_fetch_row(mysql_query("SELECT icon,name FROM ".$pre."systems WHERE id = '".$system."'"));
	$games = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."games WHERE id = '".$game."'"));
    $userid = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."profile WHERE username = '".$row[username]."'"));

        $pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{date}/";
		$pryr[3] = "/{game}/";
		$pryr[4] = "/{icon}/";
		$pryr[5] = "/{username}/";
		$pryr[6] = "/{userid}/";
		$pryr[7] = "/{system}/";
		$pryr[8] = "/{flink}/";
		$aryr[0] = stripslashes($row[name]);
		$aryr[1] = "userreviews.php?id=".$row[id];
		$aryr[2] = date($setting[dformat], $row[date]);
		$aryr[3] = $row[cat];
		if ($systems[0]) {
		$aryr[4] = "<img src='".stripslashes($systems[0])."'>";
		} else {
		$aryr[4] = "";
		}
		$aryr[5] = "<a href='index.php?load=elite&user=".$userid[0]."'>".$row[username]."</a>";
		$aryr[6] = $userid[0];
		$aryr[7] = $games[0];
		$aryr[8] = "<a href='userreviews.php?id=".$row[id]."'>".$row[name]."</a>";

eval (" ?>" . preg_replace($pryr, $aryr, $temp) . " <?php ");
}
}

function wysiwyg() {
global $setting;

return "<script language='javascript' type='text/javascript' src='".$setting[siteurl]."includes/tiny_mce/tiny_mce.js'></script>
<script language='javascript' type='text/javascript'>
	tinyMCE.init({
		theme : 'advanced',
		mode : 'textareas',
		elements : 'abshosturls',
		document_base_url : '".$setting[siteurl]."',
		plugins : 'spellchecker,preview,searchreplace,emotions,media,tinyautosave,contextmenu,codeprotect,paste',

		theme_advanced_buttons1 : 'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontsizeselect,formatselect',
		theme_advanced_buttons2 : 'bullist,numlist,|,undo,redo,|,blockquote,link,unlink,image,cleanup,code,preview,replace,spellchecker,emotions,media,tinyautosave',
		theme_advanced_buttons3 : '',
		theme_advanced_toolbar_location : 'top',
		theme_advanced_toolbar_align : 'left',
		theme_advanced_statusbar_location : 'bottom',
		theme_advanced_resizing : true,

		paste_auto_cleanup_on_paste : true,
		remove_linebreaks : false,
        force_p_newlines : false,
		debug : false,
		relative_urls : true,
		remove_script_host : false
	});</script>";
}

function reputation($id) {
global $pre;
$sq = "0";
$sql = mysql_query("SELECT * FROM ".$pre."reputation WHERE forwho = '".$id."'");
$sql2 = mysql_num_rows($sql);
while($r = mysql_fetch_array($sql)) {
$sq = $sq + $r[type];
}

return $sq;
}

function word_count($str,$n = "0"){
   $m=strlen($str)/2;
   $a=1;
   while ($a<$m) {
       $str=str_replace("  "," ",$str);
       $a++;
       }
   $b = explode(" ", $str);
   $i = 0;
   foreach ($b as $v) {
       $i++;
       }
   if ($n==1) return $b;
   else  return $i;
}

function fthrough($var, $cat) {
global $setting;
global $pre;

$test = $namepart;
$namepart = str_replace(" ", "-", stripslashes($var));
$namepart = str_replace("--", "", $namepart);
$namepart = str_replace("?", "", $namepart);
$namepart = str_replace("!", "", $namepart);
$namepart = str_replace(":", "", $namepart);
$namepart = str_replace("'", "", $namepart);
$namepart = str_replace("`", "", $namepart);
$namepart = str_replace("&;", "", $namepart);
$namepart = str_replace("(", "", $namepart);
$namepart = str_replace("#", "", $namepart);
$namepart = str_replace("$", "", $namepart);
$namepart = str_replace(".", "-", $namepart);
$namepart = str_replace(",", "", $namepart);
$namepart = str_replace(")", "", $namepart);
$namepart = str_replace("/", "-", $namepart);
$namepart = str_replace("\"", "", $namepart);

$lchar = substr($cat, -1);
if (($cat == "news") or ($cat == "News")) {
} else {
if ($lchar == "s") {
$cat = substr_replace($cat, "", -1);
}
}
$cat = ucfirst($cat);
if ($setting[modrewrite] == "Yes") {
if ($cat) {
return "-".$namepart."-".$cat;
} else {
return "-".$namepart;
}
} else {
$op = explode("-", $namepart);
return $test;
}
}

function userid($id) {
global $pre;
$user = mysql_fetch_row(mysql_query("SELECT username FROM ".$pre."profile WHERE id = '".$id."'"));

return $user[0];
}

function random($length) { // thanks to andy at php.net
   $template = "1234567890abcdefghijklmnopqrstuvwxyz";

   settype($length, "integer");
   settype($rndstring, "string");
   settype($a, "integer");
   settype($b, "integer");
      
   for ($a = 0; $a <= $length; $a++) {
   $b = rand(0, strlen($template) - 1);
   $rndstring .= $template[$b];
   }
   return $rndstring;
}

function is_server($server) {
if (preg_match('/'.$server.'/i', $_SERVER["SERVER_SOFTWARE"])) {
return 1;
}
return 0;
}

function users($limit) {
global $setting;
global $pre;

$res2 = "SELECT * FROM ".$pre."users ORDER BY `username` ASC";
if ($limit) {
$res2 .= " LIMIT $limit, $setting[max_results]";
}
$res = mysql_query($res2);
while($r = mysql_fetch_array($res)) {
$data .= "<option value='".$r[username]."'>".$r[username]."</option>";
}
return $data;
}

function re_direct($time, $url) {
return "<SCRIPT LANGUAGE='JavaScript'>
redirTime = '".$time."';
redirURL = '".$url."';
function redirTimer() { self.setTimeout('self.location.href = redirURL;',redirTime); }
</script>
<BODY onLoad='redirTimer()'>";
}

function skins() {
global $pre;

/*$query7 = mysql_query("SELECT * FROM ".$pre."skins WHERE type = 'cp' ORDER BY `name` ASC");
while($rowb = mysql_fetch_array($query7)) {
$vart .= "<option value='".$rowb[id]."'>".$rowb[name]."</option>";
}
$vart2 = "<form action='".$_SERVER['REQUEST_URI']."' method='post'><select name='skin' onchange='this.form.submit()'><option value='1' selected>- Select Skin -</option>".$vart."</select></form>";
return $vart2;*/
}

function fskin() {
global $cpage;
global $pre;

/*$query7 = mysql_query("SELECT * FROM ".$pre."fskins ORDER BY `name` ASC");
while($rowb = mysql_fetch_array($query7)) {
$vart .= "<option value='boards.php?fskin2=".$rowb[id]."'>".$rowb[name]."</option>";
}
$vart2 = "<select name='fskin2' size='1' onChange='openDir2(this.form)'><option value='boards.php?fskin2=1' selected>- Select Skin -</option>".$vart."</select>";
return $vart2;*/
}

function poll($id, $type) {
global $pre;
if ($_COOKIE["p_".$id.""] == "") {
$part1 .= "<form action='index.php?poll=yes&id=".$id."' method=\"post\"><table cellpadding='0' cellspacing='0' border='0'>";

$query = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."polls WHERE id = '".$id."' AND type = 'pollname'"));
$poll = $query[0];

$query2npn="SELECT * FROM ".$pre."polls WHERE pname = '".$poll."' AND type = 'question'";
$result2npn=mysql_query($query2npn) or die(mysql_error());
while($rowet = mysql_fetch_array($result2npn)) {
$part1 .= "<tr><td><b>$rowet[name]<input type='hidden' name='q' value='".$rowet[name]."'></b></td></tr>";
}

$queryo = mysql_query("SELECT * FROM ".$pre."polls WHERE pname = '".$poll."' AND type = 'option'") or die(mysql_error());
while($rowo = mysql_fetch_array($queryo)) {
$part1 .=  "<tr><td>$rowo[name]</td><td><input type='radio' name='check' value='".$rowo[name]."'></td></tr>";
}

$part1 .=  "<tr><td><input type='submit' name='submit' value='Submit Vote'></td></tr></table></form>";
} else {
$part1 .=  "<table cellpadding='1' cellspacing='0' border='0'>";

$query = mysql_query("SELECT name FROM ".$pre."polls WHERE id = '".$id."' AND type = 'pollname'");
$polle = mysql_fetch_row($query);
$poll = $polle[0];

$result = mysql_query("SELECT * FROM ".$pre."polls WHERE pname = '".$poll."' AND type = 'question'");
while($row = mysql_fetch_array($result)) {
$part1 .=  "<tr><td><b>$row[name]</b></td></tr>";
}

$num2 = mysql_fetch_row(mysql_query("SELECT votes FROM ".$pre."polls WHERE pname = '".$poll."' AND type = 'question'"));
$num = $num2[0];

$query="SELECT * FROM ".$pre."polls WHERE pname = '".$poll."' AND type = 'option'";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)) {
if ($num == "0") {
$num = "1";
}
$nu = $row[votes]/$num;

if (($num == $row[votes]) && ($row[votes] > "0")) {
$nua = "100";
} else {
if ($row[votes] == "0") {
$nua = "0";
} else {
$nua = substr($nu, 2, 2);
}
}

if (($nu < "10") && ($nu > "0")) {
$nua = $nu * 100;
}

if (($num == $row[votes]) && ($row[votes] == "0")) {
$nua = "0";
}

$part1 .=  "<tr><td>$row[name]</td><td><i>".$row[votes]."</i> (".substr($nua,0,4)."%)</td></tr>";
if ($type == "graphic") {
if ($nua > "0") {
$part1 .= "<tr><td><img src='a_images/poll.jpg' width='".$nua."%' height='10'></td></tr>";
} else {
$part1 .= "<tr><td><img src='a_images/poll.jpg' width='1%' height='10'></td></tr>";
}
}
}
$part1 .=  "<tr><td><b>Total Votes:</b></td><td>$num</td></tr></table>";
}
return $part1;
}

function rss($cat) {
if ($cat) {
$cat2 = "?cat=".$cat;
}
return "<a href='rss.php".$cat2."'><img src='a_images/rss.gif' border='0'></a>";
}

function hilite($var) {
return "<span style='background-color: #FFFF00'>".$var."</span>";
}

function points() {
global $useridn;
global $points1;
global $points2;
global $points3;
global $points4;
global $points5;
global $points6;
global $points7;
global $points8;
global $points9;
global $points10;
global $points11;
global $points12;
global $pre;

if ($_COOKIE[$pre."username"]) {

$p1 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'post' AND uid = '".$useridn."'"));
$p11 = $p1 * $points1;

$p2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'topic' OR type = 'Sticky' OR type = 'Announcement' AND uid = '".$useridn."'"));
$p22 = $p2 * $points2;

$p3 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."userreviews WHERE user = '".$_COOKIE[$pre."username"]."'"));
$p33 = $p3 * $points3;

$p5 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."comments WHERE name = '".$_COOKIE[$pre."username"]."'"));
$p55 = $p5 * $points5;

$p6 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."friends WHERE pid = '".$useridn."' OR pid2 = '".$useridn."' AND ver = 'yes'"));
$p66 = $p6 * $points6;

$p7 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."elite WHERE pid = '".$useridn."' AND type = 'tracked'"));
$p77 = $p7 * $points7;

$p8 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."elite WHERE pid = '".$useridn."' AND type = 'collection'"));
$p88 = $p8 * $points8;

$p9 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."elite WHERE pid = '".$useridn."' AND type = 'wishlist'"));
$p99 = $p9 * $points9;

$p10 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."elite WHERE pid = '".$useridn."' AND type = 'playing'"));
$p101 = $p10 * $points10;

$p11 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."elite WHERE pid = '".$useridn."' AND type = 'favorites'"));
$p111 = $p11 * $points11;

$p12 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."elite WHERE pid = '".$useridn."' AND type = 'systems'"));
$p121 = $p12 * $points12;

$points = $p11 + $p22 + $p33 + $p44 + $p55 + $p66 + $p77 + $p88 + $p99 + $p101 + $p111 + $p121;

$sql = mysql_query("SELECT * FROM ".$pre."points WHERE username = '".$_COOKIE[$pre."username"]."' ORDER BY `id` DESC");
while($row = mysql_fetch_array($sql)) {
$points = $points + $row[points];
}

return $points;
} else {
$points = "0";
return $points;
}
}

function cfields_check1() {
global $pre;

$query = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'games' ORDER BY `id` DESC") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
$name = "$row[name]";

$query2 = mysql_query("SELECT ".$name.",id FROM ".$pre."games ORDER BY `id` DESC") or die(mysql_error());
while($r = mysql_fetch_array($query2)) {
$query3 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."fielddata WHERE name = '".$name."' AND id2 = '".$r["id"]."' AND cat = 'games' AND data = '".$r["$name"]."'"));
if (($query3 == "0") && ($r["$name"])) {
mysql_query("INSERT INTO ".$pre."fielddata VALUES ('null', '".$name."', '".$r["$name"]."', '".$r["id"]."', 'games')");
}
}
}
}

function cfields_check2() {
global $pre;

$query = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '' ORDER BY `id` DESC") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
$name = "$row[name]";

$query2 = mysql_query("SELECT ".$name.",id FROM ".$pre."content ORDER BY `id` DESC") or die(mysql_error());
while($r = mysql_fetch_array($query2)) {
$query3 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."fielddata WHERE name = '".$name."' AND id2 = '".$r["id"]."' AND cat = 'content' AND data = '".$r["$name"]."'"));
if (($query3 == "0") && ($r["$name"])) {
mysql_query("INSERT INTO ".$pre."fielddata VALUES ('null', '".$name."', '".$r["$name"]."', '".$r["id"]."', 'content')");
}
}
}
}

function cfields_check3() {
global $pre;

$query5 = mysql_query("SELECT * FROM ".$pre."cat ORDER BY `id` DESC") or die(mysql_error());
while($rowa = mysql_fetch_array($query5)) {

$query = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$rowa[name]."' ORDER BY `id` DESC") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
$name = "$row[name]";

$query2 = mysql_query("SELECT ".$name.",id FROM ".$pre."content ORDER BY `id` DESC") or die(mysql_error());
while($r = mysql_fetch_array($query2)) {
$query3 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."fielddata WHERE name = '".$name."' AND id2 = '".$r["id"]."' AND cat = 'content' AND data = '".$r["$name"]."'"));
if (($query3 == "0") && ($r["$name"])) {
mysql_query("INSERT INTO ".$pre."fielddata VALUES ('null', '".$name."', '".$r["$name"]."', '".$r["id"]."', 'content')");
}
}
}
}

$sql = mysql_query("SELECT * FROM ".$pre."content");
while($row = mysql_fetch_array($sql)) {

$fet1 = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND cat = 'content' AND name = 'systems'"));
mysql_query("UPDATE ".$pre."content SET systems = '".$fet1[0]."' WHERE id = '".$row[id]."'");
$fet2 = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND cat = 'content' AND name = 'games'"));
mysql_query("UPDATE ".$pre."content SET games = '".$fet2[0]."' WHERE id = '".$row[id]."'");
}

$sql2 = mysql_query("SELECT * FROM ".$pre."fielddata WHERE name = 'games' OR name = 'systems'");
while($r = mysql_fetch_array($sql2)) {
mysql_query("DELETE FROM ".$pre."fielddata WHERE id = '".$r[id]."'");
}
}

function cfields_check5 () {
global $pre;

mysql_query("ALTER TABLE `".$pre."content` ADD `games` TEXT NOT NULL");
mysql_query("ALTER TABLE `".$pre."content` ADD `systems` TEXT NOT NULL");
}

function cfields_check4 () {
global $pre;

// content check, any custom fields or extra fields will be deleted
$sql = mysql_query("SHOW COLUMNS FROM ".$pre."content");
   while ($row = mysql_fetch_assoc($sql)) {
	   $name = "$row[Field]";
	   if ((((((((((($name == "id") or ($name == "name") or ($name == "cat") or ($name == "username") or ($name == "postpone") or ($name == "lev") or ($name == "stats") or ($name == "date") or ($name == "ver") or ($name == "games") or ($name == "systems"))))))))))) {
	   } else {
       mysql_query("ALTER TABLE ".$pre."content DROP `$name`");
	   }
   }
// games check, any custom fields or extra fields will be deleted
$sql = mysql_query("SHOW COLUMNS FROM ".$pre."games");
   while ($row = mysql_fetch_assoc($sql)) {
	   $name = "$row[Field]";
	   if (((((((((((((($name == "id") or ($name == "name") or ($name == "stats") or ($name == "username") or ($name == "publisher") or ($name == "developer") or ($name == "genre") or ($name == "release") or ($name == "esrb") or ($name == "boxart") or ($name == "des") or ($name == "skin") or ($name == "system") or ($name == "album")))))))))))))) {
	   } else {
       mysql_query("ALTER TABLE ".$pre."games DROP `$name`");
	   }
   }
}

function ss_type_check() {
global $pre;

$abcd = "http://www";
$query = mysql_query("SELECT * FROM ".$pre."images WHERE name LIKE '%" . $abcd . "%' ORDER BY `id`");
$iy = mysql_num_rows($query);
for($i = 0; $r = mysql_fetch_assoc($query); $i++) {
if ($r[type2] == "ss") {
mysql_query("UPDATE ".$pre."images SET type2 = 'ss2' WHERE id = '".$r[id]."'");
}
}
}

function admin_header() {
global $pre;
global $version;
global $admintheme;
global $useridn;
global $forumsurl;
global $userlevel;

echo "<link href='".$_SERVER['REQUEST_URI']."' rel='".$_SERVER['REQUEST_URI']."' />";

$result = loadTemplate("templates/".$admintheme."/header.php");

    $skin1[] = "/{version}/";
	$skin1[] = "/{menu1}/";
	$skin1[] = "/{menu2}/";
	$skin1[] = "/{menu3}/";
	$skin1[] = "/{menu4}/";
	$skin1[] = "/{menu5}/";
	$skin1[] = "/{menu7}/";
	$skin1[] = "/{users}/"; // amount of users online
	$skin1[] = "/{online}/"; // the users that are online
	$skin1[] = "/{pms}/"; // amount of private messages
	$skin1[] = "/{new}/"; // amount of new private messages
	$skin1[] = "/{pm}/"; // display new private messages
	$skin1[] = "/{welcome}/"; // Displays "Welcome username" if person is logged in, other wise "Welcome Guest" and a link to login
	$skin1[] = "/{chooseskin}/";
	$skin1[] = "/{skinname}/"; // the name of the current skin being used
	$skin2[] = $version;

	$ojab = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."boardcp WHERE uid = '".$useridn."'"));

	if ($ojab > "0") {
	$skin2[] = "&nbsp;- <a href='admin.php?view=home'>Admin CP</a> / <a href='../index.php'>Frontend</a><br>&nbsp;- <a href='../boardcp.php'>Board CP</a> / <a href='".$forumsurl."'>Forums</a><br>&nbsp;- <a href='stats.php'><b>Complete Stats</b></a><br>&nbsp;- <a href='support.php'><b>Support</b></a><br>&nbsp;- <a href='settings.php'>Settings</a><br>&nbsp;- <a href='tools.php'>Tools</a><br>";
	} else {
	$skin2[] = "&nbsp;- <a href='admin.php?view=home'>Admin CP</a> / <a href='../index.php'>Frontend</a><br>&nbsp;- <a href='".$forumsurl."'>Forums</a><br>&nbsp;- <a href='stats.php'><b>Complete Stats</b></a><br>&nbsp;- <a href='support.php'><b>Support</b></a><br>&nbsp;- <a href='settings.php'>Settings</a><br>&nbsp;- <a href='tools.php'>Tools</a><br>";
	}

$querym = mysql_query("SELECT * FROM ".$pre."mods WHERE installed = 'Yes' AND status = 'On' AND readme = '' ORDER BY `name` ASC");
while($rowm = mysql_fetch_array($querym)) {
if ($rowm[url]) {
$datam .= "&nbsp;- <a href='".$rowm[url]."'>".$rowm[name]."</a><br>";
} else {
$datam .= "&nbsp;- ".$rowm[name]."<br>";
}
}

$querym = mysql_query("SELECT * FROM ".$pre."mods WHERE installed = 'Yes' AND status = 'On' AND readme = '1' ORDER BY `name` ASC");
while($rowm = mysql_fetch_array($querym)) {
$datam .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='".$rowm[url]."'>+ ".$rowm[name]."</a><br>";
}

    $skin2[] = "&nbsp;- <a href='admin.php?load=modules'><b>Modules Manager</b></a><br>".$datam."<br>&nbsp;- <a href='plugins.php'><b>Plugins Manager</b></a><br>".$datap;

    $squery = mysql_query("SELECT * FROM ".$pre."settings WHERE parent = 'General' AND type = 'setting'");
	while($r = mysql_fetch_array($squery)) {
    $settings .= "&nbsp;- <a href='admin.php?load=settings&type=".$r[sitename]."'>".ucfirst($r[sitename])."</a><br>";
	}

	$skin2[] = $settings;

    if ($userlevel < "4") {
	$skin2[] = '&nbsp;- <a href="admin.php?load=elite">Elite</a><br>&nbsp;- <a href="templates.php?load=templates&view=skins">Skins</a><br>&nbsp;- <a href="templates.php">Templates</a><br>&nbsp;- <a href="admin.php?load=fields">Custom Fields</a><br>&nbsp;- <a href="cat.php">Categories</a><br>&nbsp;- <a href="systems.php">Systems</a>';
	} else {
	$skin2[] = 'No Access';
	}

$sqla = mysql_query("SELECT * FROM ".$pre."cat ORDER BY `name` ASC");
while($rowa = mysql_fetch_array($sqla)) {
$cname = $rowa[name];
$lcat = $cname;
$content1 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?cat=".$cname."'>+ ".ucwords($cname)."</a><br>";
$content2 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?cat=".$cname."&view=add'>+ ".ucwords($cname)."</a><br>";
}

     $skin2[] = "&nbsp;- <a href='admin.php?view=add&cat=".$lcat."'>Add Content</a><br>".$content2."&nbsp;- <a href='admin.php'>Manage Content</a><br>".$content1."&nbsp;- <a href='games.php?load=games'>Manage Games</a>";

	 if ($userlevel < "4") {
     $skin2[] = '&nbsp;- <a href="users.php?load=users">User Manager</a><br>&nbsp;- <a href="users.php?load=users&view=levels">User Levels</a><br>&nbsp;- <a href="users.php?load=users&view=log">Admin Logs</a><br>&nbsp;- <a href="admin.php?load=ipban">Ban IP\'s</a><br>&nbsp;- <a href="users.php?load=users&view=warn">Warn Users</a><br>&nbsp;- <a href="users.php?load=users&view=ban">Ban Users</a><br>&nbsp;- <a href="users.php?load=users&view=permissions">Permissions</a><br>&nbsp;- <a href="admin.php?load=logout">Logout</a>';
	 } else {
     $skin2[] = '&nbsp;- <a href="admin.php?load=logout">Logout</a>';
	 }

     $skin2[] = online();
	 $skin2[] = usersonline();
	 $skin2[] = totalpms();
	 $skin2[] = newpms();
	 $skin2[] = pms();
	 $skin2[] = welcome();
	 $skin2[] = skins();
	 $skin2[] = skinname();

	 return preg_replace($skin1, $skin2, stripslashes($result));

}

function admin_footer() {
global $pre;
global $version;
global $admintheme;
global $useridn;
global $forumsurl;
global $userlevel;
global $setting;

$result = loadTemplate("templates/".$admintheme."/footer.php");

    $skin1[] = "/{version}/";
	$skin1[] = "/{menu1}/";
	$skin1[] = "/{menu2}/";
	$skin1[] = "/{menu3}/";
	$skin1[] = "/{menu4}/";
	$skin1[] = "/{menu5}/";
	$skin1[] = "/{menu7}/";
	$skin1[] = "/{users}/"; // amount of users online
	$skin1[] = "/{online}/"; // the users that are online
	$skin1[] = "/{pms}/"; // amount of private messages
	$skin1[] = "/{new}/"; // amount of new private messages
	$skin1[] = "/{pm}/"; // display new private messages
	$skin1[] = "/{welcome}/"; // Displays "Welcome username" if person is logged in, other wise "Welcome Guest" and a link to login
	$skin1[] = "/{chooseskin}/";
	$skin1[] = "/{skinname}/"; // the name of the current skin being used
	$skin2[] = $version;

	$ojab = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."boardcp WHERE uid = '".$useridn."'"));

	if ($ojab > "0") {
	$skin2[] = "&nbsp;- <a href='admin.php?view=home'>Admin CP</a> / <a href='index.php'>Frontend</a><br>&nbsp;- <a href='../boardcp.php'>Board CP</a> / <a href='../".$forumsurl."'>Forums</a><br>&nbsp;- <a href='stats.php?load=stats&view=all'><b>Complete Stats</b></a><br>&nbsp;- <a href='support.php?load=support'><b>Support</b></a><br>&nbsp;- <a href='settings.php?load=settings&type=global'>Settings</a><br>&nbsp;- <a href='tools.php?load=tools'>Tools</a><br>";
	} else {
	$skin2[] = "&nbsp;- <a href='admin.php?view=home'>Admin CP</a> / <a href='index.php'>Frontend</a><br>&nbsp;- <a href='../".$forumsurl."'>Forums</a><br>&nbsp;- <a href='stats.php?load=stats&view=all'><b>Complete Stats</b></a><br>&nbsp;- <a href='support.php?load=support'><b>Support</b></a><br>&nbsp;- <a href='settings.php?load=settings&type=global'>Settings</a><br>&nbsp;- <a href='tools.php?load=tools'>Tools</a><br>";
	}

$querym = mysql_query("SELECT * FROM ".$pre."mods WHERE installed = 'Yes' AND status = 'On' ORDER BY `name` ASC");
while($rowm = mysql_fetch_array($querym)) {
if ($rowm[url]) {
$datam .= "&nbsp;- <a href='".$rowm[url]."'>".$rowm[name]."</a><br>";
} else {
$datam .= "&nbsp;- ".$rowm[name]."<br>";
}
}

    $skin2[] = "&nbsp;- <a href='admin.php?load=modules'><b>Modules Manager</b></a><br>".$datam;

    $squery = mysql_query("SELECT * FROM ".$pre."settings WHERE parent = 'General' AND type = 'setting'");
	while($r = mysql_fetch_array($squery)) {
    $settings .= "&nbsp;- <a href='admin.php?load=settings&type=".$r[sitename]."'>".ucfirst($r[sitename])."</a><br>";
	}

	$skin2[] = $settings;

    if ($userlevel < "4") {
	$skin2[] = '&nbsp;- <a href="admin.php?load=elite">Elite</a><br>&nbsp;- <a href="templates.php?load=templates&view=skins">Skins</a><br>&nbsp;- <a href="templates.php?load=templates">Templates</a><br>&nbsp;- <a href="admin.php?load=fields">Custom Fields</a><br>&nbsp;- <a href="cat.php?load=cat">Categories</a><br>&nbsp;- <a href="systems.php?load=systems">Systems</a>';
	} else {
	$skin2[] = 'No Access';
	}

$sqla = mysql_query("SELECT * FROM ".$pre."cat ORDER BY `name` ASC");
while($rowa = mysql_fetch_array($sqla)) {
$cname = $rowa[name];
$lcat = $cname;
$content1 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?cat=".$cname."'>+ ".ucwords($cname)."</a><br>";
$content2 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?cat=".$cname."&view=add'>+ ".ucwords($cname)."</a><br>";
}

     $skin2[] = "&nbsp;- <a href='cat.php?view=add&cat=".$lcat."'>Add Content</a><br>".$content2."&nbsp;- <a href='admin.php'>Manage Content</a><br>".$content1."&nbsp;- <a href='games.php?load=games'>Manage Games</a>";

	 if ($userlevel < "4") {
     $skin2[] = '&nbsp;- <a href="users.php?load=users">User Manager</a><br>&nbsp;- <a href="users.php?load=users&view=levels">User Levels</a><br>&nbsp;- <a href="users.php?load=users&view=log">Admin Logs</a><br>&nbsp;- <a href="admin.php?load=ipban">Ban IP\'s</a><br>&nbsp;- <a href="admin.php?load=users&view=warn">Warn Users</a><br>&nbsp;- <a href="users.php?load=users&view=ban">Ban Users</a><br>&nbsp;- <a href="users.php?load=users&view=permissions">Permissions</a><br>&nbsp;- <a href="admin.php?load=logout">Logout</a>';
	 } else {
     $skin2[] = '&nbsp;- <a href="admin.php?load=logout">Logout</a>';
	 }

     $skin2[] = online();
	 $skin2[] = usersonline();
	 $skin2[] = totalpms();
	 $skin2[] = newpms();
	 $skin2[] = pms();
	 $skin2[] = welcome();
	 $skin2[] = skins();
	 $skin2[] = skinname();

	 return preg_replace($skin1, $skin2, stripslashes($result));
}

function abclist($url, $type) {
if ($type == "") {
$type = "index.php";
$url2 = $url."&";
$url3 = $url;
} else {
$url2 = $url."?";
$url3 = $url;
}

return "<center><a href='$type".$url3."'><u>All</u></a>&nbsp;&nbsp;<a href='$type".$url2."abc=A'>A</a>&nbsp;&nbsp;<a href='$type".$url2."abc=B'>B</a>&nbsp;&nbsp;<a href='$type".$url2."abc=C'>C</a>&nbsp;&nbsp;<a href='$type".$url2."abc=D'>D</a>&nbsp;&nbsp;<a href='$type".$url2."abc=E'>E</a>&nbsp;&nbsp;<a href='$type".$url2."abc=F'>F</a>&nbsp;&nbsp;<a href='$type".$url2."abc=G'>G</a>&nbsp;&nbsp;<a href='$type".$url2."abc=H'>H</a>&nbsp;&nbsp;<a href='$type".$url2."abc=I'>I</a>&nbsp;&nbsp;<a href='$type".$url2."abc=J'>J</a>&nbsp;&nbsp;<a href='$type".$url2."abc=K'>K</a>&nbsp;&nbsp;<a href='$type".$url2."abc=L'>L</a>&nbsp;&nbsp;<a href='$type".$url2."abc=M'>M</a>&nbsp;&nbsp;<a href='$type".$url2."abc=N'>N</a>&nbsp;&nbsp;<a href='$type".$url2."abc=O'>O</a>&nbsp;&nbsp;<a href='$type".$url2."abc=P'>P</a>&nbsp;&nbsp;<a href='$type".$url2."abc=Q'>Q</a>&nbsp;&nbsp;<a href='$type".$url2."abc=R'>R</a>&nbsp;&nbsp;<a href='$type".$url2."abc=S'>S</a>&nbsp;&nbsp;<a href='$type".$url2."abc=T'>T</a>&nbsp;&nbsp;<a href='$type".$url2."abc=U'>U</a>&nbsp;&nbsp;<a href='$type".$url2."abc=V'>V</a>&nbsp;&nbsp;<a href='$type".$url2."abc=W'>W</a>&nbsp;&nbsp;<a href='$type".$url2."abc=X'>X</a>&nbsp;&nbsp;<a href='$type".$url2."abc=Y'>Y</a>&nbsp;&nbsp;<a href='$type".$url2."abc=Z'>Z</a></center><br>";
}

function table($tablename) {
global $dbname;

   $result = mysql_list_tables($dbname);
   $rcount = mysql_num_rows($result);

   for ($i=0;$i<$rcount;$i++) {
       if (mysql_tablename($result, $i)==$tablename) return true;
   }
   return false;
}

function abc($srcfilename, $watermark, $quality) {
global $pre;
global $path;
global $watermark2;

$imageInfo = getimagesize($srcfilename); 
$width = $imageInfo[0]; 
$height = $imageInfo[1]; 
$logoinfo = getimagesize($watermark);
$logowidth = $logoinfo[0];
$logoheight = $logoinfo[1];
$horizextra =$width - $logowidth;
$vertextra =$height - $logoheight;
$horizmargin =  round($horizextra / 2);
$vertmargin =  round($vertextra / 2);
$photoImage = ImageCreateFromJPEG($srcfilename);
ImageAlphaBlending($photoImage, true);
if ($watermark2 == "png") {
$logoImage = imagecreatefrompng($watermark);  
}
if ($watermark2 == "gif") {
$logoImage = imagecreatefromgif($watermark);  
}
if (($watermark2 == "jpg") or ($watermark2 == "jpeg")) {
$logoImage = imagecreatefromjpeg($watermark); 
}
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
ImageCopy($photoImage, $logoImage, $horizmargin, $vertmargin, 0, 0, $logoW, $logoH);
ImageJPEG($photoImage,'', $quality);
ImageDestroy($photoImage);
ImageDestroy($logoImage);
return $photoImage;
}

function watermark($image2) {
global $watermark;
global $watermark2;
global $quality;
global $path;
global $pre;

$aimage = "$path/$image2";

if ($watermark2 == "png") {
$watermark = imagecreatefrompng($watermark);
}
if ($watermark2 == "gif") {
$watermark = imagecreatefromgif($watermark);
}
if (($watermark2 == "jpg") or ($watermark2 == "jpeg")) {
$watermark = imagecreatefromjpeg($watermark);
}
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($aimage);
$size = getimagesize($image);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
}

function lcontent($cat, $temp, $gid) {
	global $setting;
	global $part1;
	global $part2;
	global $pre;
	global $maintheme;

    $tem = file_get_contents(stripslashes("templates/".$maintheme."/".$temp.".php"));
    
	if ($cat) {
	$lcontent = mysql_query("SELECT * FROM ".$pre."content WHERE games = '".$gid."' AND cat = '".$cat."' ORDER BY `date` DESC");
	} else {
	$lcontent = mysql_query("SELECT * FROM ".$pre."content WHERE games = '".$gid."' ORDER BY `date` DESC");
	}
	while($lc = mysql_fetch_array($lcontent)) {

	$systems = $lc[systems];
	
	$short = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'Short' AND cat = 'content'"));
	
	$image = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'image' AND cat = 'content'"));

	$squery = mysql_query("SELECT icon,name FROM ".$pre."systems WHERE id = '".$systems."'");
	$systemsa = mysql_fetch_row($squery);

	    $pry[0] = "/{name}/";
		$pry[1] = "/{link}/";
		$pry[2] = "/{date}/";
		$pry[3] = "/{id}/";
		$pry[4] = "/{icon}/";
		$pry[5] = "/{cat}/";
		$ary[0] = stripslashes($lc[name]);
		$ary[1] = $part1.$lc[id].fthrough($lc[name], $lc[cat]).$part2;
		$ary[2] = date($setting[dformat], $lc[date]);
		$ary[3] = $lc[id];
		$ary[4] = "<img src='".stripslashes($systemsa[0])."'>";
		$ary[5] = $lc[cat];

	echo preg_replace($pry, $ary, $tem);
	}
}

function footera() {
global $pre;
global $maintheme;

$template = loadTemplate("templates/".$maintheme."/footer.php");

    $skin1[] = "/{version}/";
	$skin1[] = "/{users}/"; // amount of users online
	$skin1[] = "/{online}/"; // the users that are online
	$skin1[] = "/{pms}/"; // amount of private messages
	$skin1[] = "/{new}/"; // amount of new private messages
	$skin1[] = "/{pm}/"; // display new private messages
	$skin1[] = "/{welcome}/"; // Displays "Welcome username" if person is logged in, other wise "Welcome Guest" and a link to login
	$skin1[] = "/{chooseskin}/";
	$skin1[] = "/{skinname}/"; // the name of the current skin being used
	$skin2[] = $version;
    $skin2[] = online();
	$skin2[] = usersonline();
	$skin2[] = totalpms();
	$skin2[] = newpms();
	$skin2[] = pms();
	$skin2[] = welcome();
	$skin2[] = "<form action='".$REQUEST_URI."' method='post' name='skin'><select name='skin' onchange='this.form.submit()'><option value='1' selected>-----</option><?php include ('".$setting[siteurl]."/a_skins.php?view=skin'); ?></select></form>";
	$skin2[] = skinname();


eval (" ?>" . preg_replace($skin1, $skin2, stripslashes($template)) . " <?php ");
}

function headera() {
global $pre;
global $maintheme;

$template = loadTemplate("templates/".$maintheme."/header.php");

    $skin1[] = "/{version}/";
	$skin1[] = "/{users}/"; // amount of users online
	$skin1[] = "/{online}/"; // the users that are online
	$skin1[] = "/{pms}/"; // amount of private messages
	$skin1[] = "/{new}/"; // amount of new private messages
	$skin1[] = "/{pm}/"; // display new private messages
	$skin1[] = "/{welcome}/"; // Displays "Welcome username" if person is logged in, other wise "Welcome Guest" and a link to login
	$skin1[] = "/{chooseskin}/";
	$skin1[] = "/{skinname}/"; // the name of the current skin being used
	$skin2[] = $version;
    $skin2[] = online();
	$skin2[] = usersonline();
	$skin2[] = totalpms();
	$skin2[] = newpms();
	$skin2[] = pms();
	$skin2[] = welcome();
	$skin2[] = "<form action='".$REQUEST_URI."' method='post' name='skin'><select name='skin' onchange='this.form.submit()'><option value='1' selected>-----</option><?php include ('".$setting[siteurl]."/a_skins.php?view=skin'); ?></select></form>";
	$skin2[] = skinname();

eval (" ?>" . preg_replace($skin1, $skin2, stripslashes($template)) . " <?php ");
}

function checkemail($email){ 
return preg_match("/^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/i",$email); 
}

function top10() {
include ("top10.php");
}

function showtemplate($name) {
global $pre;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$name.".php"));

    $skin1[] = "/{version}/";
	$skin1[] = "/{users}/"; // amount of users online
	$skin1[] = "/{online}/"; // the users that are online
	$skin1[] = "/{pms}/"; // amount of private messages
	$skin1[] = "/{new}/"; // amount of new private messages
	$skin1[] = "/{pm}/"; // display new private messages
	$skin1[] = "/{welcome}/"; // Displays "Welcome username" if person is logged in, other wise "Welcome Guest" and a link to login
	$skin1[] = "/{chooseskin}/";
	$skin1[] = "/{skinname}/"; // the name of the current skin being used
	$skin2[] = $version;
    $skin2[] = online();
	$skin2[] = usersonline();
	$skin2[] = totalpms();
	$skin2[] = newpms();
	$skin2[] = pms();
	$skin2[] = welcome();
	$skin2[] = "<form action='".$REQUEST_URI."' method='post' name='skin'><select name='skin' onchange='this.form.submit()'><option value='1' selected>-----</option><?php include ('".$setting[siteurl]."/a_skins.php?view=skin'); ?></select></form>";
	$skin2[] = skinname();

	 eval (" ?>" . preg_replace($skin1, $skin2, stripslashes($temp)) . " <?php ");
}

function copyright() {
return "Powered by <a href='http://www.onecms.net'><b>OneCMS</b></a>";
}

function profile($id, $pusername) {
global $useridn;
global $setting;
global $pre;
global $elite1;
global $elite2;

if (($pusername == "") && ($id)) {
$fetch = mysql_fetch_row(mysql_query("SELECT username FROM ".$pre."profile WHERE id = '".$id."'"));
$pusername = $fetch[0];
}

if (($pusername) && ($id == "")) {
$fetch = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."profile WHERE username = '".$pusername."'"));
$id = $fetch[0];
}

if (($id == "") or ($pusername == "")) {
$p = "<center>Visitor</center>";
} else {
$p .= "<center>";
$posts = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'post' AND uid = '".$id."'"));
$topics1 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'topic' AND uid = '".$id."'"));
$topics2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'Sticky' AND uid = '".$id."'"));
$topics3 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'Announcement' AND uid = '".$id."'"));

$tposts = $posts + $topics1 + $topics2 + $topics3;

    $sql = mysql_fetch_row(mysql_query("SELECT avatar FROM ".$pre."profile WHERE username = '".$pusername."'"));
    $av = $sql[0];

	$rf = mysql_fetch_row(mysql_query("SELECT rank FROM ".$pre."users WHERE username = '".$pusername."'"));
	$sb = mysql_fetch_row(mysql_query("SELECT color,name FROM ".$pre."ranks WHERE name = '".$rf[0]."'"));
	$rank = "<a href='".$elite1.$pusername.$elite2."'><font color='".$sb[0]."'><b>".$pusername."</b></font></a><br><small>".$sb[1]."</small><br>";

if ($rank == "") {
$p .= "<a href='".$elite1.$pusername.$elite2."'>".$pusername."</a><br>";
} else {
$p .= $rank;
}
	$page = @file_get_contents($av);
	if (!$page == NULL) {
	$avatar = "<img src='".$av;

	list($widtha, $heighta) = @getimagesize($av);

	if ($widtha > $setting[avatar_width]) {
	$avatar .= "' width='".$setting[avatar_width];
	}
	if ($heighta > $setting[avatar_height]) {
	$avatar .= "' height='".$setting[avatar_width];
	}

	$avatar .= "' style='border:1px solid 'black'>";
	$p .= $avatar;
	}
	$p .= "</center><br><b>Posts:</b> $tposts<br><a href='boards.php?t=rep&id=$id'><b>Rep Level:</b></a> ".reputation($id);
	if ($useridn) {
	$p .= "&nbsp;&nbsp;[<a href='boards.php?t=giverep&id=$id&type=1&tid=".$_GET['t']."'><b><font color='red'>+</font></b></a>&nbsp;&nbsp;<a href='boards.php?t=giverep&id=$id&type=-1&tid=".$_GET['t']."'><b><font color='blue'>-</font></b></a> ]";
	}
}
	return $p;
}

function left_side($id) {
global $setting;
global $pre;

$sql = mysql_query("SELECT * FROM ".$pre."profile WHERE username = '".$id."'");
$m = mysql_num_rows($sql);
while($r = mysql_fetch_array($sql)) {

	$page = @file_get_contents("$r[avatar]");
	if ($page == NULL) {
		$avatar = "";
	} else {
	$avatar = "<img src='".$r[avatar]."'";

	list($widtha, $heighta) = getimagesize($r[avatar]);

	if ($widtha > $setting[avatar_width]) {
	$avatar .= " width='".$setting[avatar_width]."'";
	}
	if ($heighta > $setting[avatar_height]) {
	$avatar .= " height='".$setting[avatar_width]."'";
	}

	$avatar .= " border='1'>";
	}

	if ($m > "0") {
		$user = "<a href='index.php?action=profile&id=".$r[id]."'>".$id."</a>";
	} else {
		$user = "Visitor";
	}
    
	if ($m > "0") {
	$to = mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'post' AND uid = '".$r[id]."'");
	$posts = mysql_num_rows($to);
	} else {
	$post = "N/A";
	}

    if ($m > "0") {
	$tot = mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'topic' OR type = 'Announcement' OR type = 'Sticky' AND uid = '".$r[id]."'");
	$topics = mysql_num_rows($tot);
	} else {
	$topics = "N/A";
	}

	return "<center>".$user."<br><br>".$avatar."<br><b>Posts:</b> $posts<br><b>Topics:</b> $topics</center>";
}
}

function comments3($id) {
global $images;
global $pre;

$sql = mysql_query("SELECT * FROM ".$pre."profile WHERE username = '".$id."'");
$m = mysql_num_rows($sql);

if ($m > "0") {
while($r = mysql_fetch_array($sql)) {
	
$sm = mysql_query("SELECT * FROM ".$pre."smwords WHERE type = 'smiley'");
while ($row = mysql_fetch_array($sm)) {
	  
	  $ty[] = $row[name];
	  $np[] = "<img src='".$images."/".$row[field]."'>";
}

$sm2 = mysql_query("SELECT * FROM ".$pre."smwords WHERE type = 'badword'");
while ($row2 = mysql_fetch_array($sm2)) {
	  
	  $ty[] = $row2[name];
	  $np[] = $row2[field];
}

$sig = "<br>---------------<br>".$r[sig];

	$signature2 = stripslashes($sig);
	$signaturey = preg_replace("/<br>\n/","\n",$signature2);
	$signaturey = preg_replace("/<br\>\n/","\n",$signature2);
	$signaturey = preg_replace("/(\015\012)|(\015)|(\012)/","<br>\n",$signature2);

	return str_replace($ty, $np, $signaturey);
}
}
}

function comments($cid) {
global $pre;

$sql = mysql_query("SELECT * FROM ".$pre."comments WHERE aid = '".$cid."' ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {

$sql2 = mysql_query("SELECT id FROM ".$pre."profile WHERE username = '".$r[name]."'");
$amountid = mysql_num_rows($sql2);
$numberid = mysql_fetch_row($sql2);

	if ($amountid > "0") {
		$user = "<a href='index.php?action=profile&id=".$numberid[0]."'>".$r[name]."</a>";
	} else {
		$user = "Visitor";
	}

	return "<tr><td><center><b>Profile</b></center></td><td><b>".$r[subject]."</b> posted by ".$user." at <i>".date($setting[dformat], $r['date'])."</i></td></tr><tr><td>".left_side($r[name])."</td><td>".comments4("<p>".stripslashes($r[comment])."</p>").comments3($r[name])."</td></tr>";
}
}

function ad($name, $type) {
global $setting;
global $pre;

if (($type == "group") or ($type == "Group")) {

$fetch = mysql_query("SELECT id FROM ".$pre."ad WHERE name = '".$name."' AND type = 'group'");
$yeah = mysql_fetch_row($fetch);

$sql = mysql_query("SELECT * FROM ".$pre."ad WHERE grp = '".$yeah[0]."' ORDER BY RAND() DESC LIMIT 1");
} else {
$sql = mysql_query("SELECT * FROM ".$pre."ad WHERE name = '".$name."' LIMIT 1");
}
while($row = @mysql_fetch_array($sql)) {
$type2 = "$row[type]";

$val = $row[views] + 1;

mysql_query("UPDATE ".$pre."ad SET views = '".$val."' WHERE id = '".$row[id]."'");

$ex = explode("/", $row[dim]);

if ($type2 == "coding") {
if (($_COOKIE[$pre."username"]) && ($row[user] == "Yes")) {
return stripslashes($row[coding]);
}

if ($_COOKIE[$pre."username"] == "") {
return stripslashes($row[coding]);
}
}

if ($type2 == "image") {
if (($_COOKIE[$pre."username"]) && ($row[user] == "Yes")) {
$img = "<img src='".stripslashes($row[coding])."'";
if ($ex[0]) {
$img .= " width='".$ex[0]."'";
}
if ($ex[1]) {
$img .= " height='".$ex[1]."'";
}
$img .= ">";
return $img;
}
if ($_COOKIE[$pre."username"] == "") {
$img = "<img src='".stripslashes($row[coding])."'";
if ($ex[0]) {
$img .= " width='".$ex[0]."'";
}
if ($ex[1]) {
$img .= " height='".$ex[1]."'";
}
$img .= ">";
return $img;
}
}

if ($type2 == "flash") {

	if (($_COOKIE[$pre."username"]) && ($row[user] == "Yes")) {
	
	return "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'><param name='movie' value='".stripslashes($row[coding])."'><param name='quality' value='high'><embed src='".stripslashes($row[coding])."' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed></object>";
	}

	if ($_COOKIE[$pre."username"] == "") {

	return "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'><param name='movie' value='".stripslashes($row[coding])."'><param name='quality' value='high'><embed src='".stripslashes($row[coding])."' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed></object>";
	}
}

}
}

function af($limit) {
global $setting;
global $pre;
global $af_apply;
global $maintheme;

	$aftemp = file_get_contents("templates/".$maintheme."/AF_Manager.php");

$afm[0] = "{affiliate}";
$afm[1] = "{date}";

if ($limit == "") {
$query = mysql_query("SELECT * FROM ".$pre."af WHERE verified = 'yes' ORDER BY `id` DESC");
} else {
$query = mysql_query("SELECT * FROM ".$pre."af WHERE verified = 'yes' ORDER BY `id` DESC LIMIT ".$limit);
}
while($row = mysql_fetch_array($query)) {

if ($row[type] == "link") {
$afr[0] = "<a href='index.php?load=af&view=click&id=".$row[id]."' target='".$row[where2]."'>".$row[sitename]."</a>";
} else {

$filename = $images."/".$row[ss];

$page = @file_get_contents($filename);

if (!$page == NULL) {
$afr[0] = "<a href='index.php?load=af&view=click&id=".$row[id]."' target='".$row[where2]."'><img src='".$images."/".$row[ss]."' width='".$row[width]."' height='".$row[height]."'></a>";
} else {
$afr[0] = "<a href='index.php?load=af&view=click&id=".$row[id]."' target='".$row[where2]."'><img src='".$row[ss]."' width='".$row[width]."' height='".$row[height]."'></a>";
}
}
$afr[1] = date($setting[dformat], $row['date']);

$data .= stripslashes(str_replace($afm, $afr, $aftemp));
}
$data .= "<br><center>[ <a href='".$af_apply."'><b>Apply for Affiliation</b></a> ]</center>";
return $data;
}

function gallery($limit, $template, $system) {
global $gpart1;
global $gpart2;
global $pre;
global $maintheme;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));

$sql .= "SELECT * FROM ".$pre."albums ";
if ($system) {
$sql .= "WHERE systems = '".$system."' ";
}
$sql .= "ORDER BY `id` DESC";
if ($limit) {
$sql .= " LIMIT ".$limit;
} else {
$sql .= " LIMIT 5";
}
$quer = mysql_query($sql);
while($row = mysql_fetch_array($quer)) {

	$systems = $row[systems];

	$squery = mysql_query("SELECT icon,name FROM ".$pre."systems WHERE id = '".$systems."'");
	$systemsa = mysql_fetch_row($squery);

        $pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{views}/";
		$pryr[3] = "/{system}/";
		$pryr[4] = "/{icon}/";
		$pryr[5] = "/{id}/";
		$aryr[0] = stripslashes($row[name]);
		$aryr[1] = $gpart1.$row[id].fthrough($row[name], "").$gpart2;
		$aryr[2] = $row[views];
		$aryr[3] = $systems[1];
		if ($systems[0]) {
		$aryr[4] = "<img src='".stripslashes($systemsa[0])."'>";
		} else {
		$aryr[4] = "";
		}
		$aryr[5] = $row[id];

eval (" ?>" . preg_replace($pryr, $aryr, $temp) . " <?php ");
}
}

function gameGallery($limit, $template, $system) {
global $gpart1;
global $gpart2;
global $pre;
global $maintheme;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));

$sql .= "SELECT * FROM ".$pre."albums ";
if ($system) {
$sql .= "WHERE systems = '".$system."' ";
}
$sql .= "ORDER BY `id` DESC";
if ($limit) {
$sql .= " LIMIT ".$limit;
} else {
$sql .= " LIMIT 5";
}
$quer = mysql_query($sql);
while($row = mysql_fetch_array($quer)) {

	$systems = $row[systems];

	$squery = mysql_query("SELECT icon,name FROM ".$pre."systems WHERE id = '".$systems."'");
	$systemsa = mysql_fetch_row($squery);

        $pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{views}/";
		$pryr[3] = "/{system}/";
		$pryr[4] = "/{icon}/";
		$pryr[5] = "/{id}/";
		$aryr[0] = stripslashes($row[name]);
		$aryr[1] = $gpart1.$row[id].fthrough($row[name], "").$gpart2;
		$aryr[2] = $row[views];
		$aryr[3] = $systems[1];
		if ($systems[0]) {
		$aryr[4] = "<img src='".stripslashes($systemsa[0])."'>";
		} else {
		$aryr[4] = "";
		}
		$aryr[5] = $row[id];

eval (" ?>" . preg_replace($pryr, $aryr, $temp) . " <?php ");
}
}

function systemslist($template, $limit) {
global $setting;
global $pre;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));

if ($limit == "") {
$query = mysql_query("SELECT * FROM ".$pre."systems");
} else {
$query = mysql_query("SELECT * FROM ".$pre."systems LIMIT ".$limit);
}
    while ($row = mysql_fetch_array($query)) {

        $pryr[0] = "/{name}/";
		$pryr[1] = "/{abr}/";
		$pryr[2] = "/{link}/";
		$pryr[3] = "/{icon}/";
		$aryr[0] = $row[name];
		$aryr[1] = $row[abr];
		$aryr[2] = $setting[siteurl]."/index.php?id=systems&sid=".$row[abr];
		$aryr[3] = "<img src='".$row[icon]."'>";

		echo preg_replace($pryr, $aryr, $temp);
    }
}

//Display games by system, genre, etc
function games($num, $genre, $system, $template) {
global $setting;
global $gamepart1;
global $gamepart2;
global $pre;
global $maintheme;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));

$systemid = mysql_fetch_row(mysql_query("SELECT * FROM ".$pre."systems WHERE name = '".$system."'"));

if ($genre) {
$query = mysql_query("SELECT * FROM ".$pre."games WHERE system = '".$systemid[0]."' AND genre = '".$genre."' ORDER BY `id` DESC LIMIT ".$num);
} else {
$query = mysql_query("SELECT * FROM ".$pre."games WHERE system = '".$systemid[0]."' ORDER BY `id` DESC LIMIT ".$num);
}
while ($row = mysql_fetch_array($query)) {
	    $name = stripslashes($row[name]);

		//$system = mysql_fetch_row(mysql_query("SELECT * FROM ".$pre."systems WHERE id
		
        $pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{date}/";
		$pryr[3] = "/{genre}/";
		$pryr[4] = "/{boxart}/";
		$pryr[5] = "/{platform}/";
		$aryr[0] = $row[name];
		$aryr[1] = $gamepart1.$row[id].fthrough($row[name], "").$gamepart2;
		$aryr[2] = date($setting[dformat], $row[date]);
		$aryr[3] = $row[genre];
		if ($row[boxart] == "") {
		$aryr[4] = "a_images/noboxart.jpg";
		} else {
		$aryr[4] = $row[boxart];
		$aryr[5] = $systemid[3];
		}

		echo preg_replace($pryr, $aryr, $temp);
    }
}

function cgames($cat, $num, $template, $game, $system) {
global $setting;
global $gamepart1;
global $gamepart2;
global $pre;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));

    if ($cat == "") {
    $queryrra = mysql_query("SELECT * FROM ".$pre."content WHERE games = '".$game."' ORDER BY `id` DESC LIMIT ".$num);
	} else {
	$queryrra = mysql_query("SELECT * FROM ".$pre."content WHERE cat = '".$cat."' AND games = '".$game."' ORDER BY `id` DESC LIMIT ".$num);
	}
    while ($row = mysql_fetch_array($queryrra)) {

        $pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{date}/";
		$pryr[3] = "/{cat}/";
		$aryr[0] = $row[name];
		$aryr[1] = $part1.$row[id].fthrough($row[name], $row[name]).$part2;
		$aryr[2] = date($setting[dformat], $row[date]);
		$aryr[3] = $row[cat];

		echo preg_replace($pryr, $aryr, $temp);
    }
}

//Fetch latest blogs 
//NOTE: Might not work yet.
function blogs($username, $num, $template) {
global $setting;
global $blog1;
global $blog2;
global $pre;
		
	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));
	
    if ($username == "") {
    $queryrra = mysql_query("SELECT * FROM ".$pre."blog ORDER BY `date` DESC LIMIT ".$num);
	} else {
	$queryrra = mysql_query("SELECT * FROM ".$pre."blog ORDER BY `date` DESC LIMIT ".$num);
	}
    while ($row = mysql_fetch_array($queryrra)) {
	$username = $row[username];
	$systems = $row[systems];
	
	$short = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'Short' AND cat = 'content'"));
	
	$image = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'image' AND cat = 'content'"));
    
	$nicon = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'newsicon1' AND cat = 'content'"));

	$systemsa = mysql_fetch_row(mysql_query("SELECT icon,name FROM ".$pre."systems WHERE id = '".$systems."'"));
	
		$pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{date}/";
		$pryr[3] = "/{cat}/";
		$pryr[4] = "/{icon}/";
		$pryr[5] = "/{username}/";
		$pryr[6] = "/{Short}/";
		$pryr[7] = "/{newsicon1}/";
		$pryr[8] = "/{image}/";
		$aryr[0] = stripslashes($row[name]);
		$aryr[1] = $blog1.$row[id].fthrough($row[name], "").$blog2;
		$aryr[2] = date($setting[dformat], $row[date]);
		$aryr[3] = $row[cat];
		if ($systemsa[0]) {
		$aryr[4] = "<img src='".stripslashes($systemsa[0])."'>";
		} else {
		$aryr[4] = "";
		}
		$aryr[5] = "<a href='index.php?load=elite&user=".$row[username]."'>".$row[username]."</a>";
		$aryr[6] = stripslashes($short[0]);
		$aryr[7] = $nicon[0];
		$aryr[8] = stripslashes($image[0]);
		
eval (" ?>" . preg_replace($pryr, $aryr, $temp) . " <?php ");
}
}
function content($cat, $num, $template) {
global $setting;
global $part1;
global $part2;
global $pre;
global $maintheme;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));
	
    if ($cat == "") {
    $queryrra = mysql_query("SELECT * FROM ".$pre."content WHERE ver = '0' AND save = '0' AND postpone = '' ORDER BY `date` DESC LIMIT ".$num);
	} else {
	$queryrra = mysql_query("SELECT * FROM ".$pre."content WHERE cat = '".$cat."' AND ver = '0' AND save = '0' AND postpone = '' ORDER BY `date` DESC LIMIT ".$num);
	}
    while ($row = mysql_fetch_array($queryrra)) {
	$cat = $row[cat];
	$systems = $row[systems];
	
	$short = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'Short' AND cat = 'content'"));
	
	$image = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'image' AND cat = 'content'"));
    
	$nicon = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'newsicon1' AND cat = 'content'"));

	$systemsa = mysql_fetch_row(mysql_query("SELECT icon,name FROM ".$pre."systems WHERE id = '".$systems."'"));
	
		$pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{date}/";
		$pryr[3] = "/{cat}/";
		$pryr[4] = "/{icon}/";
		$pryr[5] = "/{username}/";
		$pryr[6] = "/{Short}/";
		$pryr[7] = "/{newsicon1}/";
		$pryr[8] = "/{image}/";
		$aryr[0] = stripslashes($row[name]);
		$aryr[1] = $part1.$row[id].fthrough($row[name], "").$part2;
		$aryr[2] = date($setting[dformat], $row[date]);
		$aryr[3] = $row[cat];
		if ($systemsa[0]) {
		$aryr[4] = "<img src='".stripslashes($systemsa[0])."'>";
		} else {
		$aryr[4] = "";
		}
		$aryr[5] = "<a href='index.php?load=elite&user=".$row[username]."'>".$row[username]."</a>";
		$aryr[6] = stripslashes($short[0]);
		$aryr[7] = $nicon[0];
		$aryr[8] = stripslashes($image[0]);
		
eval (" ?>" . preg_replace($pryr, $aryr, $temp) . " <?php ");
}
}

function systems($cat, $system, $num, $template) {
global $siteurl;
global $setting;
global $part1;
global $part2;
global $pre;
global $maintheme;

$temp = loadTemplate(stripslashes("templates/".$maintheme."/".$template.".php"));

$systemsab = mysql_query("SELECT * FROM ".$pre."fielddata WHERE name = 'systems' AND data = '".$system."' AND cat = 'content'");
while ($systemidsa = mysql_fetch_array($systemsab)) {

if ($cat) {
$queryrra = mysql_query("SELECT * FROM ".$pre."content WHERE cat = '".$cat."' AND id = '".$systemidsa[id2]."' AND ver = '0' AND save = '0'  ORDER BY `date` DESC LIMIT ".$num);
} else {
$queryrra = mysql_query("SELECT * FROM ".$pre."content WHERE id = '".$systemidsa[id2]."' AND ver = '0' AND save = '0' ORDER BY `date` DESC LIMIT ".$num);
}

while ($row = mysql_fetch_array($queryrra)) {
	$cat = $row[cat];
	$systems = $systemid[2];

	$shortq = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'Short' AND cat = 'content'"));

	$image = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'image' AND cat = 'content'"));
    
	$nicon = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id2 = '".$row[id]."' AND name = 'newsicon1' AND cat = 'content'"));

	$systemsa = mysql_fetch_row(mysql_query("SELECT icon,name FROM ".$pre."systems WHERE id = '".$systems."'"));

        $pryr[0] = "/{name}/";
		$pryr[1] = "/{link}/";
		$pryr[2] = "/{date}/";
		$pryr[3] = "/{cat}/";
		$pryr[4] = "/{icon}/";
		$pryr[5] = "/{username}/";
		$pryr[6] = "/{Short}/";
		$pryr[7] = "/{newsicon1}/";
		$pryr[8] = "/{image}/";
		$aryr[0] = stripslashes($row[name]);
		$aryr[1] = $part1.$row[id].fthrough($row[name], "").$part2;
		$aryr[2] = date($setting[dformat], $row[date]);
		$aryr[3] = $row[cat];
		if ($systemsa[0]) {
		$aryr[4] = "<img src='".stripslashes($systemsa[0])."'>";
		} else {
		$aryr[4] = "";
		}
		$aryr[5] = "<a href='index.php?load=elite&user=".$row[username]."'>".ucwords($row[username])."</a>";
		$aryr[6] = stripslashes($shortq[0]);
		$aryr[7] = $nicon[0];
		$aryr[8] = stripslashes($image[0]);

eval (" ?>" . preg_replace($pryr, $aryr, $temp) . " <?php ");
}
}
}

function posts($forum, $num, $template) {
global $setting;
global $f2part1;
global $f2part2;
global $pre;
global $maintheme;

$temp = loadTemplate(stripslashes("templates/".$maintheme."/".$template.".php"));

    if ($forum == "") {
    $queryrra = mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'post' ORDER BY `id` DESC LIMIT ".$num);
	} else {
    $queryrra = mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'post' AND fid = '".$forum."' ORDER BY `id` DESC LIMIT ".$num);
	}
    while ($row = mysql_fetch_array($queryrra)) {
		if ($row[subject] == "") {
			$subject = "No Subject";
		} else {
			$subject = $row[subject];
		}

        $pryr[0] = "/{subject}/";
		$pryr[1] = "/{username}/";
		$pryr[2] = "/{userid}/";
		$pryr[3] = "/{plink}/";
		$pryr[4] = "/{link}/";
		$pryr[5] = "/{date}/";
		$aryr[0] = $subject;
		$use = mysql_query("SELECT * FROM ".$pre."profile WHERE id = '".$row[uid]."'");
		while ($row2 = mysql_fetch_array($use)) {
		$aryr[1] = $row2[username];
		}
		$aryr[2] = $row[uid];
		$aryr[3] = "index.php?load=elite&user=".$row[uid];
		$aryr[4] = $f2part1.$row[tid].$f2part2."#".$row[id];
		$aryr[5] = date($setting[dformat], $row[date]);

		echo preg_replace($pryr, $aryr, $temp);
    }
}

function topics($forum1, $num2, $template3) {
global $setting;
global $f2part1;
global $f2part2;
global $pre;

	$tem = file_get_contents(stripslashes("templates/".$maintheme."/".$template3.".php"));

    if ($forum1 == "") {
    $queryrra = mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'topic' ORDER BY `id` DESC LIMIT ".$num2);
	} else {
	$queryrra = mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'topic' AND fid = '".$forum1."' ORDER BY `id` DESC LIMIT ".$num2);
	}
    while ($row = mysql_fetch_array($queryrra)) {

		$qu = mysql_query("SELECT * FROM ".$pre."posts WHERE type = 'post' AND tid = '".$row[id]."'");
		$row2 = mysql_num_rows($qu);

        $pry[0] = "/{subject}/";
		$pry[1] = "/{username}/";
		$pry[2] = "/{userid}/";
		$pry[3] = "/{link}/";
		$pry[4] = "/{date}/";
		$pry[5] = "/{replies}/";
		$ary[0] = $row[subject];
		$row2 = mysql_fetch_row(mysql_query("SELECT username FROM ".$pre."profile WHERE id = '".$row[uid]."'"));
		$ary[1] = $row2[0];
		$ary[2] = $row[uid];
		$ary[3] = $f2part1.$row[tid].$f2part2."#".$row[id];
		$ary[4] = date($setting[dformat], $row[date]);
		$ary[5] = $row2;

		echo preg_replace($pry, $ary, $tem);
    }
}

function online() {
global $pre;

$sqlt = mysql_query("SELECT * FROM ".$pre."users WHERE logged = '".time()."'");
$numbera = mysql_num_rows($sqlt);
return $numbera;
}

function skinname() {
global $skins;
global $pre;

	//return loadTemplate("forum/templates/".$forumtheme."/".$skins.".php");
}

function usersonline() {
global $pre;

$sql = mysql_query("SELECT * FROM ".$pre."users WHERE logged = '".time()."'");
while($user = mysql_fetch_array($sql)) {
$loggedd = "$user[logged]";
$l = "$user[level]";

$sqlbb = mysql_query("SELECT * FROM ".$pre."profile WHERE username = '".$user[username]."'");
while($bb = mysql_fetch_array($sqlbb)) {
return "&raquo; <a href='index.php?load=elite&user=".$user[username]."'>$user[username]</a><br>";
}
}
}

function totalpms() {
global $pre;

$result2 = mysql_query("SELECT * FROM ".$pre."pm WHERE jo = '".$_COOKIE[$pre."username"]."'") or die(mysql_error());
$number = mysql_num_rows($result2);
return $number;
}

function newpms() {
global $pre;

$result2b = mysql_query("SELECT * FROM ".$pre."pm WHERE jo = '".$_COOKIE[$pre."username"]."' AND viewed = '1'") or die(mysql_error());
$numberb = mysql_num_rows($result2b);
return $numberb;
}

function pms() {
global $pre;

$query="SELECT * FROM ".$pre."pm WHERE jo = '".$_COOKIE[$pre."username"]."' AND box = 'inbox' ORDER BY `id` DESC LIMIT 5";
$result=mysql_query($query);
while($row = @mysql_fetch_array($result)) {

if ($row[viewed] == "1") {

return "+</font> <i><a href=\"inbox.php?msg=$row[id]\">$row[subject]</a></i><br><br>";

} else {
return "+ <a href=\"inbox.php?msg=$row[id]\">$row[subject]</a><br><br>";
}
	}
}

function welcome() {
global $useridn;
global $userlevel;
global $pre;

if ($_COOKIE[$pre."username"] == "") {
	$a .= "Welcome Guest, please <a href='index.php?action=login'>login</a> or <a href='index.php?action=register'>register</a>.";
	if ($userlevel < "6") {
	$a .= " <a href='admin.php'><b>Control Panel</b></a>";
	}
	return $a;
} else {
	$a .= "Welcome back <a href='index.php?load=elite&user=".$useridn."'><font color='silver'>".$_COOKIE[$pre."username"]."</font></a>! <a href='index.php?load=elite'><img src='a_images/profile.jpg' border='0' width='74' height='9'></a>";
	if ($userlevel < "6") {
	$a .= " <a href='admin.php'><b>Control Panel</b></a>";
	}
	return $a;
}
}

function movie($id, $url) {
global $pre;

$query = mysql_query("SELECT * FROM ".$pre."images WHERE album = '".$id."'");
while ($row = mysql_fetch_array($query)) {
echo "<embed src='".$url."' autostart='true' showstatusbar='1' showtracking='1' loop='false' save='true' border='0'>";
}
}

//album id, url to video

function members($num, $template) {
global $setting;
global $maintheme;
global $pre;

	$temp = file_get_contents(stripslashes("templates/".$maintheme."/".$template.".php"));

if ($num) {
$sql = mysql_query("SELECT * FROM ".$pre."users LIMIT ".$num);
} else {
$sql = mysql_query("SELECT * FROM ".$pre."users LIMIT ".$num);
}

    while ($row = mysql_fetch_array($sql)) {

        $pry[0] = "/{username}/";
		$pry[1] = "/{email}/";
		$pry[2] = "/{userid}/";
		$pry[3] = "/{plink}/";
		$pry[4] = "/{posts}/";
		$pry[5] = "/{topics}/";
		$ary[0] = $row[username];
		$ary[1] = $row[email];
		$query = mysql_query("SELECT id FROM ".$pre."profile WHERE username = '".$row[username]."'");
		$fetch = mysql_fetch_row($query);
		$ary[2] = $fetch[0];
		$ary[3] ="index.php?load=elite&user=".$row[username];

		$pos = mysql_query("SELECT * FROM ".$pre."posts WHERE uid = '".$fetch[0]."' AND type = 'post'");
		$posts = mysql_num_rows($pos);

		$top = mysql_query("SELECT * FROM ".$pre."posts WHERE uid = '".$fetch[0]."' AND type = 'topic'");
		$topics = mysql_num_rows($top);

		$ary[4] = $posts;
		$ary[5] = $topics;

		echo preg_replace($pry, $ary, $temp);
    }
}
		
function authuser() {
global $pre;
global $login;
if ($_COOKIE[$pre."username"] == "") {
return "<SCRIPT LANGUAGE=\"JavaScript\">

<!-- Begin
redirTime = \"1\";
redirURL = \"".$login."\";
function redirTimer() { self.setTimeout(\"self.location.href = redirURL;\",redirTime); }
//  End -->
</script>

<BODY onLoad=\"redirTimer()\">";
}
}

function randomPassword() 
{
global $salt;

          srand((double)microtime()*1000000);  
          $i = 0; 
          while ($i <= 7) { 
                $num = rand() % 33; 
                $tmp = substr($salt, $num, 1); 
                $pass = $pass . $tmp; 
                $i++; 
          } 
          return $pass; 
}
?>