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: add-mailbox.php
9 //
10 // Template File: add-mailbox.tpl
11 //
12 // Template Variables:
13 //
14 // message
15 // username
16 // name
17 // domain
18 //
19 // POST / GET Variables:
20 //
21 // username
22 // password1
23 // password2
24 // name
25 // domain
26 //
27 require_once './functions.inc.php';
28 include './languages/' . check_language() . '.lang';
30 $SESSID_USERNAME = check_session();
31 $ROLE = check_role($SESSID_USERNAME);
33 if ($ROLE == ADMIN_ROLE) {
34 $list_domains = list_domains();
35 } else {
36 $list_domains = list_domains($SESSID_USERNAME);
37 }
39 if ($_SERVER['REQUEST_METHOD'] == "GET") {
40 $domain = filter_input(INPUT_GET, 'domain', FILTER_VALIDATE_DOMAIN);
41 $domain_key = array_search($domain, array_column($list_domains, 'domain'));
42 }
44 if ($_SERVER['REQUEST_METHOD'] == "POST") {
45 $username = strtolower(filter_input(INPUT_POST, 'username', FILTER_DEFAULT));
46 $domain = filter_input(INPUT_POST, 'domain', FILTER_VALIDATE_DOMAIN);
47 $password1 = filter_input(INPUT_POST, 'password1', FILTER_DEFAULT);
48 $password2 = filter_input(INPUT_POST, 'password2', FILTER_DEFAULT);
49 $name = filter_input(INPUT_POST, 'name', FILTER_DEFAULT);
50 $domain_key = array_search($domain, array_column($list_domains, 'domain'));
51 $from = filter_var($username . '@' . $domain, FILTER_VALIDATE_EMAIL);
53 if ($list_domains[$domain_key]['mailboxes'] != 0 && $list_domains[$domain_key]['mailbox_count'] >= $list_domains[$domain_key]['mailboxes']) {
54 $message = $LANG['Add_mailbox_username_text_error3'];
55 }
57 if (empty($username)) {
58 $message = $LANG['Add_mailbox_username_text_error1'];
59 }
61 if (empty($password1) or ($password1 != $password2)) {
62 $message = $LANG['Add_mailbox_password_text_error'];
63 }
65 if (empty($message) && in_array($domain, array_column($list_domains, 'domain'))) {
66 $hashed = bcrypt($password1);
67 $maildir = $from . "/";
69 try {
70 $dbh = pdo_connect();
71 $sth = $dbh->prepare("INSERT INTO alias (address,goto,domain,created,modified) VALUES (?,'vmail',?,NOW(),NOW())");
72 $sth->bindParam(1, $from, PDO::PARAM_STR);
73 $sth->bindParam(2, $domain, PDO::PARAM_STR);
74 $sth->execute();
75 $username = '';
76 } catch(PDOException $e) {
77 $message = $LANG['Add_alias_result_error'] . "<br />($from) - $e<br />";
78 }
80 try {
81 $dbh = pdo_connect();
82 $sth = $dbh->prepare("INSERT INTO mailbox (username,password,name,maildir,domain,created,modified) VALUES (?,?,?,?,?,NOW(),NOW())");
83 $sth->bindParam(1, $from, PDO::PARAM_STR);
84 $sth->bindParam(2, $hashed, PDO::PARAM_STR);
85 $sth->bindParam(3, $name, PDO::PARAM_STR);
86 $sth->bindParam(4, $maildir, PDO::PARAM_STR);
87 $sth->bindParam(5, $domain, PDO::PARAM_STR);
88 $sth->execute();
89 logging($SESSID_USERNAME, $domain, $LANG['Logging_mailbox_add'], "$from");
90 $message = $LANG['Add_mailbox_result_succes'] . "<br />($from)";
91 $username = '';
92 $name = '';
93 } catch(PDOException $e) {
94 $message = $LANG['Add_alias_result_error'] . "<br />($from) - $e<br />";
95 }
96 }
97 }
98 include './templates/header.tpl';
99 include './templates/menu.tpl';
100 include './templates/add-mailbox.tpl';
101 include './templates/footer.tpl';
102 ?>