<?php
$password = '1234';
$zipFile = 'backup.zip';

// Password check
if (!isset($_POST['password']) || $_POST['password'] !== $password) {
    header('Content-Type: text/html; charset=utf-8');
    echo '<form method="post" style="text-align:center; margin-top: 50px;">';
    echo '<h2>Enter Password to Create Backup</h2>';
    if (isset($_POST['password'])) {
        echo '<p style="color:red;">Invalid Password!</p>';
    }
    echo '<input type="password" name="password" size="30" /> <br/><br/>';
    echo '<input type="submit" value="Create Backup" />';
    echo '</form>';
    exit;
}

// যে ফোল্ডার ব্যাকআপ হবে
$directoryToZip = __DIR__;

// Zip তৈরি
$zip = new ZipArchive();
if ($zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
    die("ERROR: Could not create ZIP file.");
}

// Recursive ফাংশন সব ফাইল যোগ করার জন্য
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($directoryToZip, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($files as $file) {
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($directoryToZip) + 1);

    // বাদ দেওয়ার ফাইল
    if ($relativePath === basename(__FILE__) || pathinfo($relativePath, PATHINFO_EXTENSION) === "zip") {
        continue;
    }

    if ($file->isDir()) {
        $zip->addEmptyDir($relativePath);
    } else {
        $zip->addFile($filePath, $relativePath);
    }
}

$zip->close();

echo "✅ SUCCESS: Backup created successfully!<br>";
$downloadLink = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/' . $zipFile;
echo "👉 <a href='$downloadLink'>Download Backup</a>";