// Copyright (c) 2022 High5!
// License Info: LICENSE.TXT
//
// File: backup.php
//
// Template File: -none-
//
// Template Variables:
//
// -none-
//
// Form POST \ GET Variables:
//
// -none-
//
require_once '../functions.inc.php';
include '../languages/' . check_language() . '.lang';
date_default_timezone_set('Europe/Amsterdam');
if ($_SERVER['REQUEST_METHOD'] == "GET") {
umask(077);
$filename = "opensmtpadmin-" . date("Ymd") . "-" . getmypid() . ".sql";
$backup = "/tmp" . $filename;
$header = "#\n# OpenSMTPD Admin " . VERSION . "\n# Date: " . date("D M j G:i:s T Y") . "\n#\n";
$tables = array('admin','alias','domain','domain_admins','log','mailbox','vacation');
if (!$fh = fopen($backup, 'w')) {
$message = "
Cannot open file ($backup)
";
include '../templates/header.tpl';
include '../templates/admin_menu.tpl';
include '../templates/message.tpl';
include '../templates/footer.tpl';
} else {
fwrite($fh, $header);
$dbh = pdo_connect();
foreach ($tables as $table) {
$sth = $dbh->query("SHOW CREATE TABLE $table");
$row = $sth->fetch(PDO::FETCH_ASSOC);
fwrite ($fh, $row['Create Table']. "\n\n");
}
foreach ($tables as $table) {
$sth = $dbh->query("SELECT * FROM $table");
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
foreach ($row as $k => $v) {
$keys[] = $k;
$values[] = $v;
}
fwrite($fh, "INSERT INTO ". $table . " (". implode (',',$keys) . ") VALUES ('" . implode ('\',\'',$values) . "')\n");
$keys = array();
$values = array();
}
}
}
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize("$backup"));
header("Content-Description: OpenSMTPD Admin");
$download_backup = fopen("$backup", "r");
unlink("$backup");
fpassthru($download_backup);
}
?>