<?php
$z = "b";
include ("../config.php");
if (((($_GET['load'] == "help") or ($_GET['load'] == "chat") or ($_GET['load'] == "login") or ($_GET['load'] == "logout")))) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_header() . " <?php ");
}
} else {
eval (" ?>" . admin_header() . " <?php ");
echo '<SCRIPT LANGUAGE="JavaScript">var checkflag = "false";function check(field) {if (checkflag == "false") {for (i = 0; i < field.length; i++) {field[i].checked = true;}checkflag = "true";return "Uncheck All"; }else {for (i = 0; i < field.length; i++) {field[i].checked = false; }checkflag = "false";return "Check All"; }}</script>';
}
if ($ipbancheck3 == "0") {if ($numv == "0"){
	if ($warn == $naum) {
	echo "You Do Not Have Access to the Admin Panel";
}
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

$from = (($page * $setting[max_results]) - $setting[max_results]);
$admin_page = "yes";
echo "<center><a href='tools.php?load=tools'>Tools</a> | <a href='tools.php?load=tools&view=backup'>Create Backup</a> | <a href='tools.php?load=tools&view=download'>Download Backup</a> | <a href='tools.php?load=tools&view=upload'>Upload Backup</a></center><br><br>";

if ($_GET['view'] == "") {
echo "<table cellpadding='5' cellspacing='0' align='left'><tr><td><a href='tools.php?load=tools&view=delete_stats'><b>Delete Stats</b></a></td><td>Stats can take up space, with this you can delete stats for certain months to save space.</td></tr><tr><td><a href='tools.php?load=tools&view=backup'><b>Backup Database</b></a></td><td>A very powerful tool, this lets you easily backup MySQL for important data. (ie. content, templates, etc.)</td></tr><tr><td><a href='tools.php?load=tools&view=apache'><b>Apache Checker</b></a></td><td>This tool tells you if you can use mod_rewrite or not</td></tr><tr><td><a href='tools.php?load=tools&view=pcheck'><b>Post Checker</b></a></td><td>Checks to make sure there are not duplicate posts or any errors, for old version of forums</td></tr><tr><td><a href='tools.php?load=tools&view=fcheck'><b>Field Checker</b></a></td><td>Ensures there are no duplicate fields (could cause inability to post content)</td></tr><tr><td><a href='tools.php?load=tools&view=optimize'><b>Optimize Database</b></a></td><td>Optimize your database, this should be done regularly to ensure good performance.</td></tr><tr><td><a href='tools.php?load=tools&view=repair'><b>Repair Database</b></a></td><td>Repair your database, you should run this if your database seems corrupt.</td></tr></table>";

}

if ($_GET['view'] == "delete_stats") {
echo "<form action='tools.php?load=stats&view=delete_stats2' method='post'><table cellpadding='5' cellspacing='0' border='0' align='left'>";

echo "<tr><td><b>Delete Stats</b></td><td><select name='month'><option value=''>-- Month --</option>";

for ($i = 1; $i <= 12; $i++) {
if ($i < 10) {
$i = "0".$i;
}
echo "<option value='".$i."'>".$i."</option>";
}
echo "</select></td><td><select name='year'><option value=''>-- Year --</option>";

for ($i = 6; $i <= 50; $i++) {
if ($i < 10) {
$i = "0".$i;
}
echo "<option value='20".$i."'>20".$i."</option>";
}
echo "</select></td></tr><tr><td><input type='submit' value='Delete'></td></tr>";

echo "</table></form>";
}

if ($_GET['view'] == "delete_stats2") {
if (mysql_query("DELETE FROM ".$pre."stats WHERE month = '".$_POST['month']."' AND year = '".$_POST['year']."'") == TRUE) {
echo re_direct("1500", "tools.php?load=tools");
echo "Statistics Deleted. Taking you to the tools page";
}
}

if ($_GET['view'] == "uploada") {
//echo "<a href='tools.php?load=tools&view=uploadfile'>Upload SQL Locally</a><br>";
echo "<a href='tools.php?load=tools&view=uploadurl'>Upload SQL via URL location</a>";
}

if ($_GET['view'] == "uploadfile") {
echo "<form action='tools.php?load=tools&view=uploadfile2' method='post' enctype='multipart/form-data'><table cellpadding='3' cellspacing='0' align='center' width='90%'><tr><td><b>File</b></td><td><input type='file' name='url'></td></tr><tr><td><input type='submit' value='Upload SQL'></td></tr></table></form>";
}

if ($_GET['view'] == "uploadfile2") {
if ($_FILES["url"]["type"] == "text/x-sql") {
echo $_FILES["url"];
} else {
echo "Sorry but this is not a valid SQL File. Please go back and try again";
}
}

if ($_GET['view'] == "upload") {
echo "<form action='tools.php?load=tools&view=uploadurl2' method='post'><table cellpadding='3' cellspacing='0' align='center' width='90%'><tr><td><b>URL*</b></td><td><input type='text' name='url' size='40'></td></tr><tr><td><input type='submit' value='Upload SQL'></td></tr></table></form><br><small>*=Please upload the SQL backup to your public_html and only specify the name of the SQL (ex. 1131728484_191105_i38v3obgref_games.sql)</small>";
}

if ($_GET['view'] == "uploadurl2") {
        $file = fopen($_POST['url'],'r');
        $contents ='';
        while (!@feof($file)) {
          $contents .= fread($file,1024); //
        }
        @fclose($file);

        $ex = explode("-------", $contents);
	    while (list(, $i) = each ($ex)) {
		mysql_query($i);
}
echo re_direct("1500", "tools.php?load=tools&view=backup");
echo "SQL Uploaded. Taking you to the backup page";
}

if ($_GET['view'] == "delete") {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Confirm Deletion?");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';

while (list(, $val) = each ($_POST['delete'])) {
    $r = mysql_fetch_row(mysql_query("SELECT date,hash,type FROM ".$pre."backup WHERE id = '".$val."'"));
	$z = date("dmy", $r[0]);
	$filename = $r[0]."_".$z."_".$r[1]."_".$r[2].".sql";
	$bdir = "../backup/";
	@unlink($bdir.$filename);

	$delete = mysql_query("DELETE FROM ".$pre."backup WHERE id = '$val'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if ($delete == TRUE) {
echo re_direct("1500", "tools.php?load=tools&view=download");
echo "The backup(s) have been deleted. <a href=\"tools.php?load=tools&view=download\">View Backups</a>";
}
}

if ($_GET['view'] == "download") {
echo "<form action='tools.php?load=tools&view=delete' method='post'><table cellpadding='3' cellspacing='0' align='center' width='90%'><tr><td><b>Type</b></td><td><b>Date</b></td><td><b>Download</b></td><td><b>Delete?</b></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."backup ORDER BY `date` DESC");
while($r = mysql_fetch_array($sql)) {
echo "<tr><td>".$r[type]."</td><td>".date("M d, Y", $r[date])."</td><td><a href='backup/".$r[date]."_".date("dmy", $r[date])."_".$r[hash]."_".$r[type].".sql'><b>Download</b>*</a></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"$r[id]\"></td></tr>";
}

echo "<tr><td><input type='submit' value='Delete'></td></tr></table></form><br><small>*=Right click and select 'Save Link As' or the similair counterpart</small>";
}

if ($_GET['view'] == "apache") {
if(is_server('apache')) {
echo re_direct("1500", "tools.php?load=tools");
if ($modrewrite == "no1") {
mysql_query("UPDATE ".$pre."settings SET warn = 'Yes' WHERE id = '2'");
}
echo "Apache is enabled. Taking you back";
} else {
mysql_query("UPDATE ".$pre."settings SET warn = 'no1' WHERE id = '2'");
echo re_direct("1500", "tools.php?load=tools");
echo "Apache is not enabled, disabling mod_rewrite. Taking you back";
}
}

if ($_GET['view'] == "pcheck") {
$check = "0";
$boardcheck1 = mysql_query("SELECT * FROM ".$pre."posts");
while($r = @mysql_fetch_array($boardcheck1)) {
if ($r[tid] == "") {
if ($r[type] == "post") {
mysql_query("DELETE FROM ".$pre."posts WHERE id = '".$r[id]."'");
$check = $check + 1;
}
if ((($r[type] == "topic") or ($r[type] == "Announcement") or ($r[type] == "Sticky"))) {
mysql_query("UPDATE ".$pre."posts SET tid = '".$r[id]."' WHERE id = '".$r[id]."'");
$check = $check + 1;
}
}
}
echo re_direct("1500", "tools.php?load=tools");
echo "Finished with <b>".$check."</b> fixes. Taking you back to the tools page";
}

if ($_GET['view'] == "fcheck") {
$check = "0";
$fieldcheck1 = mysql_query("SELECT * FROM ".$pre."fields ORDER BY `id` DESC");
while($r = @mysql_fetch_array($fieldcheck1)) {
$fnum1 = @mysql_num_rows(mysql_query("SELECT * FROM ".$pre."fields WHERE name = '".$r[name]."' AND cat = '".$r[cat]."'"));

if ($fnum1 > "1") {
mysql_query("DELETE FROM ".$pre."fields WHERE id = '".$r[id]."'");
$check = $check + 1;
}
}
echo re_direct("1500", "tools.php?load=tools");
echo "Finished with <b>".$check."</b> fixes. Taking you back to the tools page";
}

//The part of the file that optimizes the database if the user chooses to.
if ($_GET['view'] == "optimize") {
mysql_query("OPTIMIZE TABLE `".$pre."_ad`, `".$pre."_af`, `".$pre."_albums`, `".$pre."_backup`, `".$pre."_blog`, `".$pre."_boardcp`, `".$pre."_cat`, `".$pre."_chat`, `".$pre."_comments`, `".$pre."_content`, `".$pre."_contest`, `".$pre."_elite`, `".$pre."_fielddata`, `".$pre."_fields`, `".$pre."_forums`, `".$pre."_friends`, `".$pre."_fskins`, `".$pre."_game`, `".$pre."_games`, `".$pre."_images`, `".$pre."_ipban`, `".$pre."_log`, `".$pre."_mods`, `".$pre."_newsletter`, `".$pre."_pages`, `".$pre."_permissions`, `".$pre."_plugins`, `".$pre."_pm`, `".$pre."_points`, `".$pre."_polls`, `".$pre."_posts`, `".$pre."_pr`, `".$pre."_profile`, `".$pre."_ranks`, `".$pre."_reputation`, `".$pre."_settings`, `".$pre."_shop`, `".$pre."_shop2`, `".$pre."_skins`, `".$pre."_smwords`, `".$pre."_stats`, `".$pre."_systems`, `".$pre."_templates`, `".$pre."_userlevels`, `".$pre."_userreviews`, `".$pre."_users`");

echo re_direct("1500", "tools.php?load=tools");
echo "Database optimized, taking you back to the tools page.";
}

if ($_GET['view'] == "repair") {
$sql = mysql_query("REPAIR TABLE `".$pre."_ad`, `".$pre."_af`, `".$pre."_albums`, `".$pre."_backup`, `".$pre."_blog`, `".$pre."_boardcp`, `".$pre."_cat`, `".$pre."_chat`, `".$pre."_comments`, `".$pre."_content`, `".$pre."_contest`, `".$pre."_elite`, `".$pre."_fielddata`, `".$pre."_fields`, `".$pre."_forums`, `".$pre."_friends`, `".$pre."_fskins`, `".$pre."_game`, `".$pre."_games`, `".$pre."_images`, `".$pre."_ipban`, `".$pre."_log`, `".$pre."_mods`, `".$pre."_newsletter`, `".$pre."_pages`, `".$pre."_permissions`, `".$pre."_plugins`, `".$pre."_pm`, `".$pre."_points`, `".$pre."_polls`, `".$pre."_posts`, `".$pre."_pr`, `".$pre."_profile`, `".$pre."_ranks`, `".$pre."_reputation`, `".$pre."_settings`, `".$pre."_shop`, `".$pre."_shop2`, `".$pre."_skins`, `".$pre."_smwords`, `".$pre."_stats`, `".$pre."_systems`, `".$pre."_templates`, `".$pre."_userlevels`, `".$pre."_userreviews`, `".$pre."_users`");

echo re_direct("1500", "tools.php?load=tools");
echo "Database repaired, taking you back to the tools page.";

}

if ($_GET['view'] == "backup") {
echo "<form action='tools.php?load=tools&view=backup2' method='post'><center><select name='type[]' multiple><option value='".$pre."contentsep".$pre."fielddata'>Content</option><option value='".$pre."userssep".$pre."profile'>Users</option><option value='".$pre."templatessep".$pre."skins'>Templates</option><option value='".$pre."games'>Games</option><option value='".$pre."postssep".$pre."forums'>Posts</option><option value='".$pre."fields'>Fields</option></select><br><br><input type='submit' value='Backup Data'></center></form>";
}

if ($_GET['view'] == "backup2") {
$bdir = "../backup/";

while (list(, $i) = each ($_POST['type'])) {
if (preg_match("/sep/", $i)) {


$x = explode("sep", $i);
while (list(, $v) = each ($x)) {
$ex = explode("_", $v);

$filename = time()."_".date("dmy", time())."_".random(10)."_".$ex[1].".sql";
$sql = mysql_query("SELECT * FROM ".$v." ORDER BY `id` ASC");
while($r = mysql_fetch_array($sql)) {
$content .= "INSERT INTO ".$v." VALUES ('".$r[id]."'";

$sql2 = mysql_query("SHOW COLUMNS FROM ".$v);
while ($row = mysql_fetch_assoc($sql2)) {
$name = $row[Field];

if ($name == "id") {
} else {
$content .= ", '".$r["$name"]."'";
}
}
$content .= ");
-------
";
}

$handle = fopen($bdir.$filename, 'a');
fwrite($handle, $content);
fclose($handle);

$xplode = explode("_", $filename);
$tplode = explode(".", $xplode[3]);
$bcount = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."backup WHERE hash = '".$xplode[2]."' AND type = '".$tplode[0]."' AND date = '".$xplode[0]."'"));

if ($bcount == "0") {
mysql_query("INSERT INTO ".$pre."backup VALUES (null, '".$xplode[2]."', '".$tplode[0]."', '".$xplode[0]."')");
}
$content = "";
}
} else {
$ex = explode("_", $i);
$filename = time()."_".date("dmy", time())."_".random(10)."_".$ex[1].".sql";
$sql = mysql_query("SELECT * FROM ".$i." ORDER BY `id` ASC");
while($r = mysql_fetch_array($sql)) {
$content .= "INSERT INTO ".$i." VALUES ('".$r[id]."'";

$sql2 = mysql_query("SHOW COLUMNS FROM ".$i);
while ($row = mysql_fetch_assoc($sql2)) {
$name = $row[Field];

if ($name == "id") {
} else {
$content .= ", '".$r["$name"]."'";
}
}
$content .= ");
-------
";
}
}

$handle = fopen($bdir.$filename, 'a');
fwrite($handle, $content);
fclose($handle);

$xplode = explode("_", $filename);
$tplode = explode(".", $xplode[3]);
$bcount = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."backup WHERE hash = '".$xplode[2]."' AND type = '".$tplode[0]."' AND date = '".$xplode[0]."'"));

if ($bcount == "0") {
mysql_query("INSERT INTO ".$pre."backup VALUES (null, '".$xplode[2]."', '".$tplode[0]."', '".$xplode[0]."')");
}
$content = "";
}

echo re_direct("5000", "tools.php?load=tools&view=backup");
echo "Finished creating SQL backup(s), you may now download them from the Download Backups page. Taking you to the tools page";
}
}
}
if (($_GET['load'] == "help") or ($_GET['load'] == "chat")) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_footer() . " <?php ");
}
} else {
eval (" ?>" . admin_footer() . " <?php ");
}
?>