Blob


1 <?php
2 //
3 // OpenSMTPD Admin
4 // by Mischa Peters <mischa at high5 dot nl>
5 // Copyright (c) 2022 High5!
6 // License Info: LICENSE.TXT
7 //
8 // File: backup.php
9 //
10 // Template File: -none-
11 //
12 // Template Variables:
13 //
14 // -none-
15 //
16 // POST / GET Variables:
17 //
18 // -none-
19 //
20 require_once './functions.inc.php';
21 include './languages/' . check_language() . '.lang';
22 date_default_timezone_set('Europe/Amsterdam');
24 $SESSID_USERNAME = check_session();
25 $ROLE = check_role($SESSID_USERNAME);
27 if ($ROLE != ADMIN_ROLE) {
28 header("Location: list-domain.php");
29 die();;
30 }
32 if ($_SERVER['REQUEST_METHOD'] == "GET") {
33 umask(077);
34 $filename = "opensmtpadmin-" . date("Ymd") . "-" . getmypid() . ".sql";
35 $backup = "/tmp/" . $filename;
36 $header = "#\n# OpenSMTPD Admin " . VERSION . "\n# Date: " . date("D M j G:i:s T Y") . "\n#\n";
37 $tables = array('admin','alias','domain','domain_admins','log','mailbox','vacation');
39 if (!$fh = fopen($backup, 'w')) {
40 $message = "<div class=\"error_msg\">Cannot open file ($backup)</div>";
41 }
43 if (empty($message)) {
44 fwrite($fh, $header);
45 $dbh = pdo_connect();
46 foreach ($tables as $table) {
47 $sth = $dbh->query("SHOW CREATE TABLE $table");
48 $row = $sth->fetch(PDO::FETCH_ASSOC);
49 fwrite ($fh, $row['Create Table']. "\n\n");
50 }
51 foreach ($tables as $table) {
52 $sth = $dbh->query("SELECT * FROM $table");
53 while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
54 foreach ($row as $k => $v) {
55 $keys[] = $k;
56 $values[] = $v;
57 }
58 fwrite($fh, "INSERT INTO ". $table . " (". implode (',',$keys) . ") VALUES ('" . implode ('\',\'',$values) . "')\n");
59 $keys = array();
60 $values = array();
61 }
62 }
63 header("Content-Type: application/octet-stream");
64 header("Content-Disposition: attachment; filename=\"$filename\"");
65 header("Content-Transfer-Encoding: binary");
66 header("Content-Length: " . filesize("$backup"));
67 header("Content-Description: OpenSMTPD Admin");
68 $download_backup = fopen("$backup", "r");
69 unlink("$backup");
70 fpassthru($download_backup);
71 } else {
72 include './templates/header.tpl';
73 include './templates/menu.tpl';
74 include './templates/message.tpl';
75 include './templates/footer.tpl';
76 }
77 }
78 ?>