<?php
require_once __DIR__ . "/../php/server/session.php";
pialert_start_session();

if ($_SESSION["login"] != 1) {
	header('Location: ../index.php');
	exit;
}

$ARCHIVE_PATH = str_replace('download', '', str_replace('front', 'db', getcwd()));
$LATEST_FILES = glob($ARCHIVE_PATH . "pialertdb_*.zip");
if (sizeof($LATEST_FILES) == 0) {
	exit;
} else {
	natsort($LATEST_FILES);
	$LATEST_FILES = array_reverse($LATEST_FILES, False);
	$LATEST_BACKUP = $LATEST_FILES[0];
}

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($LATEST_BACKUP) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($LATEST_BACKUP));
readfile($LATEST_BACKUP);
exit;
?>
