<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: index.php");
    exit();
}

if (isset($_GET['file'])) {
    $file = $_GET['file'];
    $file_path = __DIR__ . '/backups/' . $file;

    if (file_exists($file_path) && pathinfo($file_path, PATHINFO_EXTENSION) === 'sql') {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
        header('Content-Length: ' . filesize($file_path));
        readfile($file_path);
        exit;
    } else {
        echo "<script>alert('الملف غير موجود أو غير صالح!'); window.location.href='backup.php';</script>";
    }
} else {
    header("Location: backup.php");
    exit;
}
?>