<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

set_time_limit(0);
ini_set('memory_limit', '-1');

$basePath = realpath('../../'); // 2 levels above public
echo "<h1>Server Backup Tool</h1>";
echo "<p>Base Path: $basePath</p>";

function getFolders($dir)
{
    $folders = [];
    foreach (scandir($dir) as $file) {
        if ($file != '.' && $file != '..') {
            if (is_dir($dir . '/' . $file)) {
                $folders[] = $file;
            }
        }
    }
    return $folders;
}

if (isset($_POST['folder'])) {

    $folderName = $_POST['folder'];
    $folder = realpath($basePath . '/' . $folderName);

    if (!$folder) {
        die("Invalid folder selected");
    }

    $exclude = explode(",", $_POST['exclude']);
    $exclude = array_map('trim', $exclude);

    $zipName = "backup_" . date("Ymd_His") . ".zip";

    $zip = new ZipArchive();

    if ($zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
        die("Cannot create zip");
    }

    $directory = new RecursiveDirectoryIterator(
        $folder,
        FilesystemIterator::SKIP_DOTS
    );

    $filter = new RecursiveCallbackFilterIterator(
        $directory,
        function ($current, $key, $iterator) use ($exclude) {

            $path = $current->getPathname();

            if (!$current->isReadable()) {
                return false;
            }

            foreach ($exclude as $ex) {

                if ($ex && strpos($path, DIRECTORY_SEPARATOR . $ex) !== false) {
                    return false;
                }
            }

            return true;
        }
    );

    $files = new RecursiveIteratorIterator($filter);

    foreach ($files as $file) {

        if ($file->isFile()) {

            $filePath = $file->getPathname();
            $relativePath = substr($filePath, strlen($folder) + 1);

            $zip->addFile($filePath, $relativePath);
        }
    }

    $zip->close();

    echo "<h2>Backup Created Successfully</h2>";
    echo "<p><a href='$zipName' download>Download Zip</a></p>";

    exit;
}

$folders = getFolders($basePath);

?>

<!DOCTYPE html>
<html>

<head>

    <title>Server Backup Tool</title>

    <style>
        body {
            font-family: Arial;
            background: #f4f6f8;
            padding: 40px;
        }

        .box {
            background: white;
            padding: 25px;
            max-width: 600px;
            border-radius: 6px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        select,
        textarea {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            font-size: 14px;
        }

        button {
            padding: 12px 20px;
            background: #2c7be5;
            border: none;
            color: white;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background: #1a5edb;
        }
    </style>

</head>

<body>

    <div class="box">

        <h2>Server Backup Tool</h2>

        <form method="post">

            <label>Select Folder to Backup</label>

            <select name="folder">

                <?php
                foreach ($folders as $f) {
                    echo "<option value='$f'>$f</option>";
                }
                ?>

            </select>

            <label>Exclude Folders (comma separated)</label>

            <textarea name="exclude">vendor,node_modules,storage,bootstrap/cache</textarea>

            <button type="submit">Create Backup</button>

        </form>

    </div>

</body>

</html>