<?php
ob_start();

// Check if a file was uploaded
if (!isset($_FILES['txtbackfile']) || $_FILES['txtbackfile']['error'] !== UPLOAD_ERR_OK) {
    header("Location: backupstart.php?msg=Select a valid SQL file to upload");
    exit;
}

$uploadedFile = $_FILES['txtbackfile'];
$filename = basename($uploadedFile['name']);
$fileExt = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

// Validate file extension
if ($fileExt !== 'sql') {
    header("Location: backupstart.php?msg=Please select a valid .SQL file");
    exit;
}

// Ensure the destination directory exists
$destDir = __DIR__ . '/dump';
if (!is_dir($destDir)) {
    mkdir($destDir, 0755, true);
}

// Sanitize file name
$sanitizedFilename = preg_replace('/[^a-zA-Z0-9_\-\.]/', '_', $filename);
$destPath = $destDir . '/' . $sanitizedFilename;

// Move uploaded file securely
if (!move_uploaded_file($uploadedFile['tmp_name'], $destPath)) {
    header("Location: backupstart.php?msg=File upload failed");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Affiliate Network Pro - Admin Panel</title>
    <link rel="stylesheet" href="admin.css">
    <link rel="stylesheet" href="includes/admin.css">
    <style>
        body {
            background-color: #f4f4f4;
            font-family: Arial, sans-serif;
        }
        .container {
            width: 775px;
            margin: 0 auto;
        }
        .header, .footer {
            background-color: #000;
            color: #fff;
        }
        .success {
            background-color: #8BA5C5;
            padding: 20px;
            text-align: center;
            color: #990000;
            font-weight: bold;
        }
        .back-link {
            text-align: right;
            padding: 10px;
        }
        .back-link a {
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <img src="images/admin_01.gif" width="216" height="69" alt="Admin Panel">
        </div>

        <div class="success">
            <p>Your backup file was uploaded successfully.</p>
            <p>You can now use the Restore option to restore the backup data.</p>
        </div>

        <div class="back-link">
            <a href="backupstart.php">&laquo; Go Back to Main Screen</a>
        </div>

        <div class="footer" style="text-align: center; padding: 10px;">
            &copy; 2004 AlstraSoft Affiliate Network Pro. All Rights Reserved.
        </div>
    </div>
</body>
</html>
