opensmtpdadmin/add-alias.php
2022-09-04 14:31:50 +00:00

81 lines
2.5 KiB
PHP

<?php
//
// OpenSMTPD Admin
// by Mischa Peters <mischa at high5 dot nl>
// Copyright (c) 2022 High5!
// License Info: LICENSE.TXT
//
// File: add-alias.php
//
// Template File: add-alias.tpl
//
// Template Variables:
//
// message
// address
// domain
// goto
//
// Form POST \ GET Variables:
//
// address
// domain
// goto
//
require_once './functions.inc.php';
include './languages/' . check_language() . '.lang';
$SESSID_USERNAME = check_session();
$list_domains = list_domains($SESSID_USERNAME);
$admin = $SESSID_USERNAME ?? ADMIN_EMAIL;
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$domain = filter_input(INPUT_GET, 'domain', FILTER_VALIDATE_DOMAIN);
$domain_key = array_search($domain, array_column($list_domains, 'domain'));
$domain_exist = in_array($domain, array_column($list_domains, 'domain'));
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$address = strtolower(filter_input(INPUT_POST, 'address', FILTER_DEFAULT));
$domain = filter_input(INPUT_POST, 'domain', FILTER_VALIDATE_DOMAIN);
$goto = strtolower(filter_input(INPUT_POST, 'goto', FILTER_DEFAULT));
$domain_key = array_search($domain, array_column($list_domains, 'domain'));
$domain_exist = in_array($domain, array_column($list_domains, 'domain'));
$from = filter_var($address . '@' . $domain, FILTER_VALIDATE_EMAIL);
if (!str_contains($goto, '@')) {
$goto = $goto . "@" . $domain;
}
$goto = filter_var($goto, FILTER_VALIDATE_EMAIL);
if ($list_domains[$domain_key]['aliases'] != 0 && $list_domains[$domain_key]['alias_count'] >= $list_domains[$domain_key]['aliases']) {
$message = $LANG['Add_alias_address_text_error2'];
}
if (empty($address) || empty($goto)) {
$message = $LANG['Add_alias_address_text_error1'];
}
if ($domain_exist && empty($message)) {
try {
$dbh = connect_db();
$sth = $dbh->prepare("INSERT INTO alias (address,goto,domain,created,modified) VALUES (?,?,?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $goto, PDO::PARAM_STR);
$sth->bindParam(3, $domain, PDO::PARAM_STR);
$sth->execute();
logging($admin, $domain, $LANG['Logging_alias_add'], "$from -> $goto");
$message = $LANG['Add_alias_result_succes'] . "<br />($from -> $goto)</br />";
$address = '';
$goto = '';
} catch(PDOException $e) {
$message = $LANG['Add_alias_result_error'] . "<br />($from -> $goto)<br />";
}
}
}
include './templates/header.tpl';
include './templates/menu.tpl';
include './templates/add-alias.tpl';
include './templates/footer.tpl';
?>