<?
include "header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }


if($task == "restore") {

$sql_ext = str_replace(".", "", strtolower(strrchr($_FILES['sql']['name'], ".")));
$is_error = "no";
$error = "";

if($_FILES['sql']['type'] != "text/plain" | $sql_ext != "txt") {
$is_error = "yes";
$error = "You have not specified a valid backup file.";
}

if($_FILES['sql']['name'] == "") {
$is_error = "yes";
$error = "You must specify a backup file to restore the database from.";
}

if($is_error == "yes") {
echo "
$header
<table cellpadding='0' cellspacing='0' width='500'>
<tr>
<td><b>An Error Has Occurred:</b></td>
</tr><tr>
<td>
$error<br><br>
<b>Please use your browser's back button to try again.</b>
</td>
</tr>
</table>
$footer";
exit;
}


move_uploaded_file($_FILES['sql']['tmp_name'], "../temp/backup.php");

mysql_query("DROP TABLE IF EXISTS ims, ims_admin, ims_faq, ims_faqcat, ims_folders, ims_image, ims_plans");

include "../temp/backup.php";
unlink("../temp/backup.php");

echo "
$header
<table cellpadding='0' cellspacing='0' width='500'>
<tr>
<td><b>Success!</b></td>
</tr><tr>
<td>
Your IMS tables have been restored.
</td>
</tr>
</table>
$footer";
exit;
}





if($task == "main") {
echo $header;

echo "
<b>Backup Instructions</b><br>
Data from your IMS is stored on your server in two ways. Firstly, admin settings
and user information is kept in your MySQL database. Secondly, your users' image
data is kept in folders under the \"/u/\" folder. In order to backup both sets of
data, you must do the following:
<br><br>

1. Download the \"/u/\" folder and all subfolders and files to your hard disk.
This can be easily done with your FTP client.
<br><br>

2. Next, you must download a backup file containing the seven MySQL tables that belong to the IMS. 
It will be saved as a text file, which you can use to restore the data in the future.
Save the following file to your hard drive:
<br><br>

Download: <a href='backup_tables.php'>ims_tables.txt</a><br><br>

<b>Restoration Instructions</b><br>
To restore your IMS using a backup created with the process above, please follow these two steps.
<br>Note that this will also reset any admin settings you have made since the backup.
<br><br>

1. Upload the \"/u/\" folder and all subfolders and files from your hard disk.
This can be easily done with your FTP client. Be sure to CHMOD all folders and subfolders \"777\".
<br><br>

2. Next, upload your backup ims_tables.txt file. 
<br>

<form name='upload' action='backup.php' enctype='multipart/form-data' method='POST' style='margin-bottom: 0px;'>
<input type='hidden' name='MAX_FILE_SIZE' value='1000000'>
<input type='file' name='sql' size='50'>
<input type='hidden' name='task' value='restore'>
<input type='submit' value='Restore' onClick='return warning()'>
</form>
";


echo $footer;
}
?>