opensmtpdadmin/add-mailbox.php

103 lines
3.2 KiB
PHP

<?php
//
// OpenSMTPD Admin
// by Mischa Peters <mischa at high5 dot nl>
// Copyright (c) 2022 High5!
// License Info: LICENSE.TXT
//
// File: add-mailbox.php
//
// Template File: add-mailbox.tpl
//
// Template Variables:
//
// message
// username
// name
// domain
//
// POST / GET Variables:
//
// username
// password1
// password2
// name
// domain
//
require_once './functions.inc.php';
include './languages/' . check_language() . '.lang';
$SESSID_USERNAME = check_session();
$ROLE = check_role($SESSID_USERNAME);
if ($ROLE == ADMIN_ROLE) {
$list_domains = list_domains();
} else {
$list_domains = list_domains($SESSID_USERNAME);
}
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$domain = filter_input(INPUT_GET, 'domain', FILTER_VALIDATE_DOMAIN);
$domain_key = array_search($domain, array_column($list_domains, 'domain'));
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$username = strtolower(filter_input(INPUT_POST, 'username', FILTER_DEFAULT));
$domain = filter_input(INPUT_POST, 'domain', FILTER_VALIDATE_DOMAIN);
$password1 = filter_input(INPUT_POST, 'password1', FILTER_DEFAULT);
$password2 = filter_input(INPUT_POST, 'password2', FILTER_DEFAULT);
$name = filter_input(INPUT_POST, 'name', FILTER_DEFAULT);
$domain_key = array_search($domain, array_column($list_domains, 'domain'));
$from = filter_var($username . '@' . $domain, FILTER_VALIDATE_EMAIL);
if ($list_domains[$domain_key]['mailboxes'] != 0 && $list_domains[$domain_key]['mailbox_count'] >= $list_domains[$domain_key]['mailboxes']) {
$message = $LANG['Add_mailbox_username_text_error3'];
}
if (empty($username)) {
$message = $LANG['Add_mailbox_username_text_error1'];
}
if (empty($password1) or ($password1 != $password2)) {
$message = $LANG['Add_mailbox_password_text_error'];
}
if (empty($message) && in_array($domain, array_column($list_domains, 'domain'))) {
$hashed = bcrypt($password1);
$maildir = $from . "/";
try {
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO alias (address,goto,domain,created,modified) VALUES (?,'vmail',?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
$sth->execute();
$username = '';
} catch(PDOException $e) {
$message = $LANG['Add_alias_result_error'] . "<br />($from) - $e<br />";
}
try {
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO mailbox (username,password,name,maildir,domain,created,modified) VALUES (?,?,?,?,?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $hashed, PDO::PARAM_STR);
$sth->bindParam(3, $name, PDO::PARAM_STR);
$sth->bindParam(4, $maildir, PDO::PARAM_STR);
$sth->bindParam(5, $domain, PDO::PARAM_STR);
$sth->execute();
logging($SESSID_USERNAME, $domain, $LANG['Logging_mailbox_add'], "$from");
$message = $LANG['Add_mailbox_result_succes'] . "<br />($from)";
$username = '';
$name = '';
} catch(PDOException $e) {
$message = $LANG['Add_alias_result_error'] . "<br />($from) - $e<br />";
}
}
}
include './templates/header.tpl';
include './templates/menu.tpl';
include './templates/add-mailbox.tpl';
include './templates/footer.tpl';
?>