opensmtpdadmin/backup.php

79 lines
2.1 KiB
PHP
Raw Normal View History

2022-09-05 20:31:23 +02:00
<?php
//
// OpenSMTPD Admin
// by Mischa Peters <mischa at high5 dot nl>
// Copyright (c) 2022 High5!
// License Info: LICENSE.TXT
//
// File: backup.php
//
// Template File: -none-
//
// Template Variables:
//
// -none-
//
// POST / GET Variables:
//
// -none-
//
require_once './functions.inc.php';
include './languages/' . check_language() . '.lang';
date_default_timezone_set('Europe/Amsterdam');
$SESSID_USERNAME = check_session();
2022-09-06 13:56:05 +02:00
$ROLE = check_role($SESSID_USERNAME);
2022-09-05 20:31:23 +02:00
if ($ROLE != ADMIN_ROLE) {
2022-09-05 20:31:23 +02:00
header("Location: list-domain.php");
die();;
}
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 = "<div class=\"error_msg\">Cannot open file ($backup)</div>";
}
if (empty($message)) {
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);
} else {
include './templates/header.tpl';
include './templates/menu.tpl';
include './templates/message.tpl';
include './templates/footer.tpl';
}
}
?>