<?php
session_start();
if(!isset($_SESSION['admin'])){
    die("Unauthorized");
}

$token = $_GET['token'] ?? '';
$folderPath = "../uploads/".$token;

if(!is_dir($folderPath)){
    die("Invalid Token");
}

$zip = new ZipArchive();
$zipFile = $token.".zip";

if($zip->open($zipFile, ZipArchive::CREATE) === TRUE){

    $files = scandir($folderPath);

    foreach($files as $file){
        if($file != '.' && $file != '..'){
            $zip->addFile($folderPath.'/'.$file, $file);
        }
    }

    $zip->close();

    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="'.$zipFile.'"');
    readfile($zipFile);

    unlink($zipFile);
}