<?php
require DENTAL_ROOT . '/app/bootstrap.php';
require_login();
require_roles('super_admin');
require_documentation_schema();
$id=(int)($_GET['id']??0);
$stmt=db()->prepare('SELECT * FROM `' . table('backup_logs') . '` WHERE id=? LIMIT 1');
$stmt->execute([$id]);
$backup=$stmt->fetch();
if(!$backup){http_response_code(404);exit('Backup not found.');}
$file=basename((string)$backup['file_name']);
$path=backup_directory().'/'.$file;
if(!is_file($path)){http_response_code(404);exit('Backup file is missing.');}
audit('database_backup_downloaded','backup',$id,['file'=>$file]);
$extension=strtolower(pathinfo($file,PATHINFO_EXTENSION));
header('X-Content-Type-Options: nosniff');
header('Content-Type: '.match($extension){'gz'=>'application/gzip','zip'=>'application/zip',default=>'application/sql'});
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Length: '.filesize($path));
readfile($path);
