<?php
declare(strict_types=1);
require __DIR__.'/app/bootstrap.php';
require_current_schema();
require_permission('backups.manage');
$filename='sodazw-billing-backup-'.date('Y-m-d-His').'.sql';
$contents=sql_backup_contents();
db()->prepare('INSERT INTO backup_events (backup_type,filename,file_size,status,created_by) VALUES ("download",?,? ,"completed",?)')->execute([$filename,strlen($contents),user()['id']]);
db()->exec('UPDATE settings SET last_backup_at=NOW() WHERE id=1');
audit('database_backup_downloaded');
header('Content-Type: application/sql');
header('Cache-Control: private, no-store, max-age=0');
header('Pragma: no-cache');
header('X-Content-Type-Options: nosniff');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.strlen($contents));
echo $contents;
