-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_email.php
More file actions
269 lines (269 loc) · 7.63 KB
/
Copy pathcreate_email.php
File metadata and controls
269 lines (269 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
define("DA_LOGIN_KEY", "<LOGINKEY>");
define("DA_USERNAME", "<ADMIN>");
define("DA_URL", "https://admin.dapanel.tld:2222");
define("AUTH_CODE", "<AUTHKEY>");
function listMailboxes($domain)
{
$params = ["action" => "list", "domain" => $domain];
return callDA("CMD_API_POP", $params)["list"] ?? [];
}
function createMailbox($mailbox, $domain)
{
$passwd = generatePassword();
$params = [
"action" => "create",
"domain" => $domain,
"user" => $mailbox,
"passwd" => $passwd,
"passwd2" => $passwd,
"quota" => 100,
"limit" => 30
];
$result = callDA("CMD_API_POP", $params);
parse_str($result, $output);
return $output["error"] === '0' ? [true, $passwd] : [false, urldecode($output["text"])];
}
function generatePassword()
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
return substr(str_shuffle($chars), 0, rand(8, 12));
}
function callDA($command, $params)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, DA_URL . "/" . $command);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, DA_USERNAME . ":" . DA_LOGIN_KEY);
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
return $result;
}
function sanitizeInput($input)
{
return htmlspecialchars(stripslashes(trim($input)));
}
function isValidEmail($email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
list($localPart) = explode('@', $email);
return ctype_alnum(str_replace(['-', '_'], '', $localPart));
}
function createForwarder($mailbox, $domain, $forwarding_email)
{
$params = [
"action" => "create",
"user" => $mailbox,
"domain" => $domain,
"email" => $forwarding_email
];
$result = callDA("CMD_API_EMAIL_FORWARDERS", $params);
parse_str($result, $output);
return $output["error"] === '0' ? [true, $forwarding_email] : [false, urldecode($output["text"])];
}
$message = "";
$status = "error";
$allowed_domains = ["domain1.com", "domain2.com"]; // add your allowed domains here
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$authCode = sanitizeInput($_POST["authCode"]);
if ($authCode !== AUTH_CODE) {
$message = "Invalid request.";
} else {
$mailbox = sanitizeInput($_POST["mailbox"]);
$domain = filter_input(INPUT_POST, 'domain', FILTER_SANITIZE_STRING);
if (!in_array($domain, $allowed_domains)) {
$message = "Invalid request.";
} else if (!isValidEmail($mailbox . "@" . $domain)) {
$message = "Invalid request.";
} else {
if (isset($_POST["forwarding"])) {
$forwarding_email = sanitizeInput($_POST["forwarding-email"]);
if (empty($forwarding_email)) {
$message = "No forwarding email supplied.";
} else if (!isValidEmail($forwarding_email)) {
$message = "Invalid request.";
}
}
if (empty($message)) {
$mailboxes = listMailboxes($domain);
if (in_array($mailbox, $mailboxes)) {
$message = "Mailbox unavailable. Please try again.";
} else {
list($success, $passwd) = createMailbox($mailbox, $domain);
$message = "Mailbox <b>$mailbox@$domain</b> created successfully with password <b>$passwd</b>";
if ($success) {
if (isset($_POST["forwarding"]) && !empty($forwarding_email) && isValidEmail($forwarding_email)) {
list($forwarding_success, $forwarding_result) = createForwarder($mailbox, $domain, $forwarding_email);
if ($forwarding_success) {
$message .= "<br />Your email will also be forwarded to <b>$forwarding_email</b>.";
} else {
$message .= "<br />Failed to set up forwarding.";
}
}
} else {
$message = "Mailbox unavailable. Please try again.";
}
$status = $success ? "success" : "error";
}
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Create Email</title>
<link rel="icon" type="image/x-icon" href="favicon.ico">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #ffffff;
font-family: Arial, sans-serif;
color: #000;
}
.box {
border: 1px solid #e1e4e8;
border-radius: 6px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
width: 400px;
text-align: center;
}
h1 {
color: #24292e;
margin-bottom: 20px;
}
form div {
display: flex;
justify-content: flex-start;
align-items: center;
margin-bottom: 10px;
}
form div label {
margin-right: 10px;
}
input[type="text"],
input[type="email"],
select {
border: 1px solid #D00E17;
border-radius: 6px;
color: #24292e;
width: 50%;
padding: 5px;
margin: 0 5px;
}
button {
background-color: #0A8852;
color: #fff;
border: none;
padding: 10px 16px;
border-radius: 6px;
font-weight: 600;
margin-top: 20px;
align-self: center;
}
.status-message {
margin-top: 20px;
padding: 10px;
border-radius: 5px;
}
.success {
background-color: #0A8852;
color: #fff;
}
.error {
background-color: #D00E17;
color: #fff;
}
.password-modal-content {
background-color: #fff;
color: #24292e;
}
.close {
color: #586069;
}
.close:hover,
.close:focus {
color: #0366d6;
text-decoration: none;
cursor: pointer;
}
.forwarding-fields,
.auth-fields {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.forwarding-fields label,
.auth-fields label {
width: auto;
}
.forwarding-fields input[type="checkbox"] {
width: auto;
margin: 0 10px;
}
.auth-fields input[type="text"],
.forwarding-fields input[type="email"] {
width: 45%;
}
</style>
</head>
<body>
<div class="box">
<h1>Create a Mailbox</h1>
<form action="" method="post">
<div>
<label for="mailbox">Mailbox:</label>
<input type="text" id="mailbox" name="mailbox" required>
<span>@</span>
<select id="domain" name="domain">
<option value="domain1.com">domain1.com</option>
<option value="domain2.com">domain2.com</option>
</select>
</div>
<div class="forwarding-fields">
<label for="forwarding">Forward email to:</label>
<input type="checkbox" id="forwarding" name="forwarding">
<input type="email" id="forwarding-email" name="forwarding-email" placeholder="Optional" disabled>
</div>
<div class="auth-fields">
<label for="authCode">Authorisation Code:</label>
<input type="text" id="authCode" name="authCode" required>
</div>
<button type="submit">Submit</button>
</form>
<?php
if (!empty($message)) {
echo "<div class='status-message $status'>$message</div>";
}
?>
</div>
<script>
document.getElementById('forwarding').addEventListener('change', function () {
var forwardingEmail = document.getElementById('forwarding-email');
forwardingEmail.required = this.checked;
forwardingEmail.disabled = !this.checked;
});
</script>
</body>
</html>