opensmtpdadmin/add-mailbox.php

103 lines
3.2 KiB
PHP
Raw Permalink 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
//
2022-09-04 14:49:44 +02:00
// File: add-mailbox.php
2022-08-18 14:01:52 +02:00
//
2022-09-04 14:49:44 +02:00
// Template File: add-mailbox.tpl
2022-08-18 14:01:52 +02:00
//
// Template Variables:
//
2022-09-04 14:49:44 +02:00
// message
// username
// name
// domain
2022-08-18 14:01:52 +02:00
//
2022-09-05 20:29:41 +02:00
// POST / GET Variables:
2022-08-18 14:01:52 +02:00
//
2022-09-04 14:49:44 +02:00
// username
// password1
// password2
// name
// domain
2022-08-18 14:01:52 +02:00
//
2022-09-04 14:49:44 +02:00
require_once './functions.inc.php';
include './languages/' . check_language() . '.lang';
2022-08-18 14:01:52 +02:00
$SESSID_USERNAME = check_session();
2022-09-06 13:56:05 +02:00
$ROLE = check_role($SESSID_USERNAME);
2022-08-18 14:01:52 +02:00
if ($ROLE == ADMIN_ROLE) {
2022-09-05 20:29:41 +02:00
$list_domains = list_domains();
} else {
$list_domains = list_domains($SESSID_USERNAME);
}
2022-08-18 14:01:52 +02:00
if ($_SERVER['REQUEST_METHOD'] == "GET") {
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-08-18 14:01:52 +02:00
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
2022-09-04 14:49:44 +02:00
$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);
2022-08-18 14:01:52 +02:00
2022-09-04 14:49:44 +02:00
if ($list_domains[$domain_key]['mailboxes'] != 0 && $list_domains[$domain_key]['mailbox_count'] >= $list_domains[$domain_key]['mailboxes']) {
2022-09-04 16:31:50 +02:00
$message = $LANG['Add_mailbox_username_text_error3'];
2022-08-18 14:01:52 +02:00
}
2022-09-04 14:49:44 +02:00
if (empty($username)) {
2022-09-04 16:31:50 +02:00
$message = $LANG['Add_mailbox_username_text_error1'];
2022-08-18 14:01:52 +02:00
}
2022-09-04 14:49:44 +02:00
if (empty($password1) or ($password1 != $password2)) {
2022-09-04 16:31:50 +02:00
$message = $LANG['Add_mailbox_password_text_error'];
2022-08-18 14:01:52 +02:00
}
if (empty($message) && in_array($domain, array_column($list_domains, 'domain'))) {
2022-09-04 14:49:44 +02:00
$hashed = bcrypt($password1);
$maildir = $from . "/";
2022-08-18 14:01:52 +02:00
2022-09-04 14:49:44 +02:00
try {
2022-09-04 20:50:21 +02:00
$dbh = pdo_connect();
2022-09-04 14:49:44 +02:00
$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) {
2022-09-04 16:31:50 +02:00
$message = $LANG['Add_alias_result_error'] . "<br />($from) - $e<br />";
2022-08-18 14:01:52 +02:00
}
2022-09-04 14:49:44 +02:00
try {
2022-09-04 20:50:21 +02:00
$dbh = pdo_connect();
2022-09-04 14:49:44 +02:00
$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");
2022-09-04 16:31:50 +02:00
$message = $LANG['Add_mailbox_result_succes'] . "<br />($from)";
2022-09-04 14:49:44 +02:00
$username = '';
$name = '';
} catch(PDOException $e) {
2022-09-04 16:31:50 +02:00
$message = $LANG['Add_alias_result_error'] . "<br />($from) - $e<br />";
2022-08-18 14:01:52 +02:00
}
}
}
2022-09-04 14:49:44 +02:00
include './templates/header.tpl';
include './templates/menu.tpl';
include './templates/add-mailbox.tpl';
include './templates/footer.tpl';
2022-08-18 14:01:52 +02:00
?>