<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once __DIR__ . '/db_bootstrap.php';
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/backup_lib.php';
require_login();
require_perm('can_backup');
$conn=ensure_db(true);
$run=ua_backup_find_run($conn,(int)($_GET['id']??0));
$path=is_array($run)?(string)($run['file_path']??''):'';
if(!$run||($run['status']??'')!=='success'||$path===''||!is_file($path)){http_response_code(404);exit('Backup file not found.');}
$name=basename((string)($run['file_name']?:basename($path)));
header('Content-Type: application/x-tar');
header('Content-Length: '.(string)filesize($path));
header("Content-Disposition: attachment; filename=\"backup.tar\"; filename*=UTF-8''".rawurlencode($name));
header('X-Content-Type-Options: nosniff');
$fh=fopen($path,'rb');if($fh){while(!feof($fh)){echo fread($fh,1048576);flush();}fclose($fh);}
exit;

