<?php
ob_start();
include "../includes/constants.php";

$dbhost = $host;
$dbname = $db;
$dbuser = $user;
$dbpass = $pass;

$base_url    = "http://" . $_SERVER['SERVER_NAME'];
$directory   = $_SERVER['PHP_SELF'];
$script_base = "$base_url$directory";
$base_path   = $_SERVER['SCRIPT_FILENAME'];
$root_path_www = $_SERVER['DOCUMENT_ROOT'];
$remove_end  = strrchr($root_path_www, "/");
$root_path   = preg_replace("/" . preg_quote($remove_end, "/") . "/", '', $root_path_www);
$url_base    = preg_replace("/backupstart\.php$/", '', $_SERVER['SCRIPT_FILENAME']);

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
    die("<br/>ERROR: Cannot connect to database<br/>" . $conn->connect_error);
}

$tables = $conn->query("SHOW TABLES");
if (!$tables || $tables->num_rows === 0) {
    die("ERROR: Database contains no tables");
}

$path = dirname(__FILE__) . '/';
$fp3 = fopen($path . "dbinfo.php", "wb");
if (!$fp3) {
    die("ERROR: Unable to write to dbinfo.php");
}

fwrite($fp3, "<?php\n");
fwrite($fp3, "\$dbhost=\"" . addslashes($dbhost) . "\";\n");
fwrite($fp3, "\$dbuser=\"" . addslashes($dbuser) . "\";\n");
fwrite($fp3, "\$dbpass=\"" . addslashes($dbpass) . "\";\n");
fwrite($fp3, "\$dbname=\"" . addslashes($dbname) . "\";\n");
fwrite($fp3, "\$path=\"" . addslashes($url_base) . "\";\n");

$i = 0;
$tableNames = [];
while ($row = $tables->fetch_array()) {
    $table = $row[0];
    fwrite($fp3, "\$table$i=\"" . addslashes($table) . "\";\n");
    $tableNames[] = $table;
    $i++;
}
fwrite($fp3, "\$numtables=\"" . ($i - 1) . "\";\n");
fwrite($fp3, "?>\n");
fclose($fp3);

include("dbinfo.php");
?>
<!DOCTYPE HTML>
<html>
<head>
    <title>Affiliate Network Pro - Admin Panel</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" type="text/css" href="admin.css">
    <link rel="stylesheet" type="text/css" href="includes/admin.css">
</head>
<body bgcolor="#f4f4f4" link="#000000" alink="#000000" vlink="#000000">
<table width="914" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td height="69" bgcolor="#FFFFFF">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="341"><img src="images/logo.jpg" width="341" height="112" alt=""></td>
                    <td align="right" valign="middle">&nbsp;</td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td height="22" bgcolor="#FFFFFF">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td align="right" width="4"><img src="images/topbar-left.jpg" width="4" height="22" /></td>
                    <td class="top-bar-tile">&nbsp;</td>
                    <td align="left" width="4"><img src="images/topbar-right.jpg" width="4" height="22" /></td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td bgcolor="#FFFFFF">
            <center>
                <table width="98%" border="0" align="center" cellpadding="5" cellspacing="1">
                    <tr align="center">
                        <td>
                            <form name="dobackup" method="post" action="backup.php">
                                <table width="78%" border="0" align="center" cellpadding="5" cellspacing="1" class="tablebdr">
                                    <tr>
                                        <td colspan="5" align="center" class="tdhead"><strong>CREATE DATABASE BACKUP</strong></td>
                                    </tr>
                                    <tr>
                                        <td colspan="5" height="20" align="center">
                                            <?= isset($_GET['msg']) ? htmlspecialchars($_GET['msg']) : ''; ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="5" align="center" class="text1"><span class="styleback"><font size="1">Backup will be stored on the server.</font></span></td>
                                    </tr>
                                    <tr>
                                        <td colspan="5" align="center" class="text1"><span class="styleback"><font size="1">You can View / Restore / Download / Delete the backup file using Restore option</font></span></td>
                                    </tr>
                                    <tr>
                                        <td colspan="5" align="center" class="text1" height="1">
                                            <input name="send2" type="submit" value="Backup" onclick="alert('Backup Complete');">
                                        </td>
                                    </tr>
                                </table>
                            </form>
                            <br />
                            <form name="dorestore" method="post" action="restore.php">
                                <table width="78%" border="0" cellpadding="5" cellspacing="1" align="center" class="tablebdr">
                                    <tr>
                                        <td height="24" align="center" class="tdhead"><b>RESTORE A BACKUP</b></td>
                                    </tr>
                                    <tr>
                                        <td height="54" align="center" class="styleback"><font size="1">Backup must be on server</font></td>
                                    </tr>
                                    <tr>
                                        <td height="1" align="center" class="text1">
                                            <input name="send" type="submit" value="Restore" onclick="alert('Restore Backup');">
                                        </td>
                                    </tr>
                                </table>
                            </form>
                            <br />
                            <form method="post" enctype="multipart/form-data" name="dodelete" action="backupupload.php">
                                <center>
                                    <table width="78%" border="0" cellpadding="5" cellspacing="1" align="center" class="tablebdr">
                                        <tr>
                                            <td colspan="3" align="center" class="tdhead"><b>UPLOAD BACKUP</b></td>
                                        </tr>
                                        <tr>
                                            <td colspan="3" align="center" class="styleback"><small>File being uploaded should be a <strong>*.sql</strong> file and should be backed up with the <strong>backup feature of this site</strong>. Otherwise restoring will produce <strong>unpredictable results</strong>.</small></td>
                                        </tr>
                                        <tr>
                                            <td colspan="3" align="center" class="styleback"><font color="#FF0000"><small>Note:</small></font> <small>If a file on server has the same name as the uploaded one, it will be replaced.</small></td>
                                        </tr>
                                        <tr class="text1">
                                            <td width="48%" align="right" class="styleback"><small>Select the Backup file to upload</small></td>
                                            <td width="3%" align="center">:</td>
                                            <td width="49%" align="left"><input name="txtbackfile" type="file" id="txtbackfile"></td>
                                        </tr>
                                        <tr>
                                            <td colspan="3" align="center" class="text1"><p>&nbsp;</p></td>
                                        </tr>
                                        <tr>
                                            <td colspan="3" class="text1" align="center">
                                                <input name="send4" type="submit" value="Upload" onclick="alert('Upload Backup');">
                                            </td>
                                        </tr>
                                    </table>
                                    <br />
                                </center>
                            </form>
                            <input type="button" name="Button" value="Close this Window" onclick="window.close()" class="textbox">
                        </td>
                    </tr>
                </table>
            </center>
        </td>
    </tr>
    <tr>
        <td align="center" class="footer-bg"><?php include "footer.php"; ?></td>
    </tr>
</table>
</body>
</html>
