opensmtpdadmin/admin/delete.php

194 lines
6.2 KiB
PHP
Raw Normal View History

2022-08-18 14:01:52 +02:00
<?php
//
// OpenSMTPD Admin
// by Mischa Peters <mischa at high5 dot nl>
// Copyright (c) 2022 High5!
// License Info: LICENSE.TXT
//
// File: delete.php
//
// Template File: message.tpl
//
// Template Variables:
//
// message
2022-08-18 14:01:52 +02:00
//
// Form POST \ GET Variables:
//
2022-09-02 23:06:08 +02:00
// table
// where
// delete
// domain
2022-08-18 14:01:52 +02:00
//
require_once '../functions.inc.php';
include '../languages/' . check_language() . '.lang';
2022-08-18 14:01:52 +02:00
2022-09-04 14:49:44 +02:00
$list_domains = list_domains();
$admin = $SESSID_USERNAME ?? ADMIN_EMAIL;
2022-08-18 14:01:52 +02:00
if ($_SERVER['REQUEST_METHOD'] == "GET") {
2022-09-04 16:04:56 +02:00
$table = filter_input(INPUT_GET, 'table', FILTER_DEFAULT);
2022-09-02 23:06:08 +02:00
$delete = filter_input(INPUT_GET, 'delete', FILTER_DEFAULT);
2022-09-04 14:49:44 +02:00
$domain = filter_input(INPUT_GET, 'domain', FILTER_VALIDATE_DOMAIN);
$domain_key = array_search($domain, array_column($list_domains, 'domain'));
2022-09-04 16:04:56 +02:00
$domain_exist = in_array($domain, array_column($list_domains, 'domain'));
2022-09-02 23:06:08 +02:00
2022-09-04 16:04:56 +02:00
if ($domain_exist && $table == "domain") {
2022-09-02 23:06:08 +02:00
try {
$dbh = connect_db();
$dbh->beginTransaction();
$sth = $dbh->prepare("SELECT COUNT(*) FROM log WHERE domain=?");
$sth->execute(array($domain));
$count_log = $sth->fetchColumn();
$sth = $dbh->prepare("DELETE FROM log WHERE domain=?");
$sth->execute(array($domain));
if ($sth->rowCount() != $count_log) {
throw new RuntimeException('Unable to delete entries from the logs table.');
}
$sth = $dbh->prepare("SELECT COUNT(*) FROM vacation WHERE domain=?");
$sth->execute(array($domain));
$count_vacation = $sth->fetchColumn();
$sth = $dbh->prepare("DELETE FROM vacation WHERE domain=?");
$sth->execute(array($domain));
if ($sth->rowCount() != $count_vacation) {
throw new RuntimeException('Unable to delete entries from the vacation table.');
}
$sth = $dbh->prepare("SELECT COUNT(*) FROM alias WHERE domain=?");
$sth->execute(array($domain));
$count_alias = $sth->fetchColumn();
$sth = $dbh->prepare("DELETE FROM alias WHERE domain=?");
$sth->execute(array($domain));
if ($sth->rowCount() != $count_alias) {
throw new RuntimeException('Unable to delete entries from the alias table.');
}
$sth = $dbh->prepare("SELECT COUNT(*) FROM mailbox WHERE domain=?");
$sth->execute(array($domain));
$count_mailbox = $sth->fetchColumn();
$sth = $dbh->prepare("DELETE FROM mailbox WHERE domain=?");
$sth->execute(array($domain));
if ($sth->rowCount() != $count_mailbox) {
throw new RuntimeException('Unable to delete entries from the mailbox table.');
}
$sth = $dbh->prepare("SELECT COUNT(*) FROM domain_admins WHERE domain=?");
$sth->execute(array($domain));
$count_domain_admins = $sth->fetchColumn();
2022-08-18 14:01:52 +02:00
2022-09-02 23:06:08 +02:00
$sth = $dbh->prepare("DELETE FROM domain_admins WHERE domain=?");
$sth->execute(array($domain));
if ($sth->rowCount() != $count_domain_admins) {
throw new RuntimeException('Unable to delete entries from the domain_admins table.');
}
$sth = $dbh->prepare("SELECT COUNT(*) FROM domain WHERE domain=?");
$sth->execute(array($domain));
$count_domain = $sth->fetchColumn();
$sth = $dbh->prepare("DELETE FROM domain WHERE domain=?");
$sth->execute(array($domain));
if ($sth->rowCount() != $count_domain) {
throw new RuntimeException('Unable to delete entry from the domain table.');
}
$dbh->commit();
header("Location: list-domain.php");
} catch (RuntimeException $e) {
$message = $e->getMessage();
$dbh->rollBack();
} catch (PDOException $e) {
$message = $e->getMessage();
2022-08-18 14:01:52 +02:00
}
}
2022-09-02 23:06:08 +02:00
if ($table == "admin") {
try {
$dbh = connect_db();
$dbh->beginTransaction();
$sth = $dbh->prepare("SELECT COUNT(*) FROM admin WHERE username=?");
$sth->execute(array($delete));
$count_admin = $sth->fetchColumn();
$sth = $dbh->prepare("DELETE FROM admin WHERE username=?");
$sth->execute(array($delete));
if ($sth->rowCount() != $count_admin) {
throw new RuntimeException('Unable to delete entry from the admin table.');
}
$sth = $dbh->prepare("SELECT COUNT(*) FROM domain_admins WHERE username=?");
$sth->execute(array($delete));
$count_domain_admins = $sth->fetchColumn();
$sth = $dbh->prepare("DELETE FROM domain_admins WHERE username=?");
$sth->execute(array($delete));
if ($sth->rowCount() != $count_domain_admins) {
throw new RuntimeException('Unable to delete entries from the domain_admins table.');
}
$dbh->commit();
header("Location: list-admin.php");
} catch (RuntimeException $e) {
$message = $e->getMessage();
$dbh->rollBack();
} catch (PDOException $e) {
$message = $e->getMessage();
$dbh->rollBack();
2022-08-18 14:01:52 +02:00
}
}
2022-09-04 16:04:56 +02:00
if ($domain_exist && ($table == 'alias' || $table == 'mailbox')) {
2022-09-02 23:06:08 +02:00
try {
$dbh = connect_db();
$sth = $dbh->prepare("DELETE FROM alias WHERE address=? AND domain=?");
$sth->bindParam(1, $delete, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
$sth->execute();
if ($sth->rowCount() != 1) {
throw new RuntimeException('alias');
}
logging($admin, $domain, $LANG['Logging_alias_delete'], $delete);
2022-09-02 23:06:08 +02:00
header("Location: list-virtual.php?domain=$domain");
} catch (RuntimeException $e) {
2022-09-03 11:30:40 +02:00
$message = $LANG['Delete_delete_error'] . "<b>$delete</b> (" . $e->getMessage() . ")!</span>";
2022-09-02 23:06:08 +02:00
} catch (PDOException $e) {
2022-09-03 11:30:40 +02:00
$message = $LANG['Delete_delete_error'] . "<b>$delete</b> (alias)!</span> " . $e-getMessage();
2022-08-18 14:01:52 +02:00
}
2022-09-02 23:06:08 +02:00
try {
$dbh = connect_db();
$sth = $dbh->prepare("DELETE FROM mailbox WHERE username=? AND domain=?");
$sth->bindParam(1, $delete, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
$sth->execute();
if ($sth->rowCount() != 1) {
throw new RuntimeException('mailbox');
2022-08-18 14:01:52 +02:00
}
logging($admin, $domain, $LANG['Logging_mailbox_delete'], $delete);
2022-08-18 14:01:52 +02:00
2022-09-02 23:06:08 +02:00
$sth = $dbh->prepare("DELETE FROM vacation WHERE email=? AND domain=?");
$sth->bindParam(1, $delete, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
$sth->execute();
header("Location: list-virtual.php?domain=$domain");
} catch (RuntimeException $e) {
2022-09-03 11:30:40 +02:00
$message = $LANG['Delete_delete_error'] . "<b>$delete</b> (" . $e->getMessage() . ")!</span>";
2022-09-02 23:06:08 +02:00
} catch (PDOException $e) {
2022-09-03 11:30:40 +02:00
$message = $LANG['Delete_delete_error'] . "<b>$delete</b> (mailbox)!</span>";
2022-09-02 23:06:08 +02:00
}
2022-08-18 14:01:52 +02:00
}
}
include '../templates/header.tpl';
include '../templates/admin_menu.tpl';
include '../templates/message.tpl';
include '../templates/footer.tpl';
2022-08-18 14:01:52 +02:00
?>