<?php
require_once 'config.php';

// Cek token
if (!isset($_GET['token']) || $_GET['token'] !== BACKUP_TOKEN) {
    http_response_code(403);
    exit('Akses ditolak');
}

header('Content-Type: application/json');

$files = [];

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator(UPLOAD_PATH, RecursiveDirectoryIterator::SKIP_DOTS)
);

foreach ($iterator as $file) {
    if ($file->isFile()) {

        $relative = str_replace(
            UPLOAD_PATH . DIRECTORY_SEPARATOR,
            '',
            $file->getPathname()
        );

        $files[] = [
            'file' => str_replace("\\", "/", $relative),
            'size' => $file->getSize(),
            'mtime' => $file->getMTime(),
            'sha256' => hash_file('sha256', $file->getPathname())
        ];
    }
}

echo json_encode($files, JSON_PRETTY_PRINT);