This repository has been archived by the owner on Sep 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailias.php
422 lines (317 loc) · 12.8 KB
/
mailias.php
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
/*
* Copyright (C) 2017 Peter Siemer <admin@affenrakete.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* $config = [
* 'mysqli' => [
* 'user' => 'USERNAME',
* 'pass' => 'USERPASS',
* 'database' => 'USER_DB',
* 'host' => 'localhost',
* 'port' => '3306'
* ],
* 'domain' => 'DOMAIN'
* ];
*/
namespace mailias;
/**
* Description of mailias
*
* @author Peter
*/
class mailias {
private $config = [];
private $data = null;
private $user = [];
private $mysqli = null;
private $error = [];
private $notification = [];
private $notificationID = 0;
private $locked = true;
public function setConfig($config = null) {
if (!$this->checkConfig($config)) {
$this->addNotification('error', 'system', __FUNCTION__, 'checkConfig failed');
return false;
}
$this->config = $config;
// Prüfen ob eine Datenbankverbindung aufgebaut werden kann.
if (!$this->connect()) {
$this->addNotification('error', 'system', __FUNCTION__, 'connect failed');
return false;
}
return true;
}
public function checkUser($user_email = null) {
if (!$this->checkEmail($user_email)) {
$this->addNotification('error', 'system', __FUNCTION__, 'checkEmail failed');
return false;
}
$this->user['email'] = $user_email;
// Prüfen ob User in Datenbank existiert.
if (!$this->readUser()) {
$this->addNotification('error', 'system', __FUNCTION__, 'readUser failed');
return false;
}
$this->locked = false;
return true;
}
/*
* addNote('case', 'class', 'function', 'text')
* case -> info, error, debug
* class -> system, user
* function_name -> __FUNCTION__
* text
*/
private function addNotification($case, $class, $function, $text) {
$this->notification[$case][$class][++$this->notificationID] = [
'function' => $function,
'text' => $text
];
}
public function getNotification($case = null, $class = null) {
$notification = [];
if ($case == null AND $class == NULL) {
$notification = $this->notification;
} elseif (!empty($this->notification[$case][$class])) {
$notification = $this->notification[$case][$class];
}
return $notification;
}
private function checkConfig($config = null) {
if (!is_array($config)) {
$this->addNotification('debug', 'system', __FUNCTION__, 'Config not set');
return false;
}
if (!$this->checkInput($config['domain'], 'domain')) {
$this->addNotification('debug', 'system', __FUNCTION__, 'checkDomain failed');
return false;
}
return true;
}
private function checkEmail($email = null) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->addNotification('debug', 'system', __FUNCTION__, 'Email not valid -> ' . $email);
$this->addNotification('info', 'user', __FUNCTION__, 'Email ist nicht gültig.');
return false;
}
return true;
}
private function checkInput($input = null, $type = null) {
switch ($type) {
case 'alias':
$pattern = "/^(?=^.{5,30}$)([a-zA-Z0-9]+)(?:[\w]*[a-zA-Z0-9]+)$/";
$user_info = "Alias ist ungültig.";
/*
* Gesamt 5 - 30 Zeichen
* a-o A-Z 0-9
* Unterstrich möglich, jedoch nicht zu Begin oder Ende
*/
break;
case 'description':
$pattern = "/^([\w\ \-\.]){0,250}$/";
$user_info = "Beschreibung ist nicht gültig.";
/*
* Gesamt 0 - 250 Zeichen
* a-z A-Z 0-9
* Leerzeichen Bindestrich Unterstrich und Punkt sind erlaubt.
*/
break;
case 'domain':
$pattern = "/(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)/";
$user_info = "Domain ist nmicht gültig.";
break;
default:
return false;
}
if (preg_match($pattern, $input)) {
return true;
}
$this->addNotification('debug', 'system', __FUNCTION__, $type . ' not valid -> ' . $input);
$this->addNotification('info', 'user', __FUNCTION__, $user_info);
return false;
}
private function checkUnlock() {
if ($this->locked) {
$this->addNotification('error', 'system', __FUNCTION__, 'locked');
$this->addNotification('info', 'user', __FUNCTION__, 'System gesperrt.');
return false;
}
return true;
}
public function getList() {
return $this->data;
}
public function getShort() {
return $this->user['short'];
}
private function connect() {
$this->mysqli = new \mysqli($this->config['mysqli']['host'], $this->config['mysqli']['user'], $this->config['mysqli']['pass'], $this->config['mysqli']['database'], $this->config['mysqli']['port']);
$this->mysqli->set_charset('utf8');
if ($this->mysqli->connect_errno) {
$this->addNotification('debug', 'system', __FUNCTION__, '(' . $this->mysqli->errno . ') ' . $this->mysqli->error);
return false;
}
return true;
}
public function disconnect() {
$this->mysqli->close();
return true;
}
public function readList() {
if (!$this->checkUnlock()) {
return false;
}
$this->data = [];
$sql = "SELECT * FROM mailias WHERE user_id = ? ORDER BY id ASC";
$statement = $this->mysqli->prepare($sql);
$statement->bind_param('i', $this->user['id']);
$statement->execute();
$result = $statement->get_result();
while ($row = $result->fetch_assoc()) {
$this->data[] = $row;
}
$result->free();
return true;
}
private function readUser() {
$sql = "SELECT id, short FROM user WHERE email = ? AND activ = 1";
$statement = $this->mysqli->prepare($sql);
$statement->bind_param('s', $this->user['email']);
$statement->execute();
if ($result = $statement->get_result() AND $result->num_rows > 0) {
$user = $result->fetch_assoc();
$this->user['id'] = $user['id'];
$this->user['short'] = $user['short'];
$result->free();
return true;
}
$result->free();
return false;
}
public function insertAlias($alias = null, $receive = null, $description = null) {
if (!$this->checkUnlock()) {
return false;
}
// Prüfen Nutzereingaben
$this->checkInput($alias, 'alias');
$this->checkEmail($receive);
$this->checkInput($description, 'description');
// Komplette Alias Adresse zusammensetzen und prüfen
$aliasEmail = \strtolower($this->user['short'] . "-" . $alias . "@" . $this->config['domain']);
$this->checkEmail($aliasEmail);
// Abbruch wenn Fehler aufgetreten sind.
if ($this->notificationID > 0) {
return false;
}
/*
* Create .qmail
* Weiterleitung durch Erstellung von .qmail Datei erzeugen
*/
$this->createFile($alias, $receive);
/*
* Insert SQL
* Daten in SQL eintragen
*/
$this->createSql($receive, $description, $aliasEmail);
$this->addNotification('info', 'user', __FUNCTION__, 'Email Adresse erfolgreich angelegt: ' . $alias);
return true;
}
private function createFile($alias = null, $receive = null) {
$createFile = \strtolower("/home/" . $this->config['user'] . "/.qmail-" . $this->user['short'] . "-" . $alias);
// Prüfen ob Datei existiert und anschließend erstellen, wenn möglich.
if (\file_exists($createFile)) {
$this->addNotification('info', 'user', __FUNCTION__, 'Weiterleitung existiert bereits.');
} elseif (!\file_put_contents($createFile, \strtolower($receive))) {
$this->addNotification('info', 'user', __FUNCTION__, 'Weiterleitung konnte nicht angelegt werden.');
}
return true;
}
private function createSql($receive = null, $description = null, $aliasEmail = null) {
$insert = [
'alias' => $aliasEmail,
'user_id' => $this->user['id'],
'activ' => 1,
'description' => $description,
'receive' => \strtolower($receive),
'decay' => '0'
];
$sql = "INSERT INTO mailias (alias, user_id, activ, description, receive, decay) VALUES (?, ?, ?, ?, ?, FROM_UNIXTIME(?))";
$statement = $this->mysqli->prepare($sql);
$statement->bind_param('siissi', $insert['alias'], $insert['user_id'], $insert['activ'], $insert['description'], $insert['receive'], $insert['decay']);
if (!$statement->execute()) {
$this->addNotification('info', 'user', __FUNCTION__, 'Email Adresse konnte nicht erzeugt werden.');
$this->addNotification('debug', 'system', __FUNCTION__, '(' . $this->mysqli->errno . ') ' . $this->mysqli->error);
}
return true;
}
public function delAlias($inputId = null) {
if (!$this->checkUnlock()) {
return false;
}
$toDelete = $this->getDestroyList($inputId);
foreach ($toDelete as $delete) {
/*
* Delete .qmail
* Weiterleitung durch Löschung von .qmail Datei deaktivieren
*/
$this->destroyFile($delete);
/*
* Delete SQL
* Daten aus SQL löschen
*/
$this->destroySql($delete);
$this->addNotification('info', 'user', __FUNCTION__, 'Email Adresse erfolgreich gelöscht: ' . $delete['alias']);
}
return true;
}
private function destroyFile($delete = null) {
$deletePart = explode('@', $delete['alias']);
$deleteFile = \strtolower("/home/" . $this->config['user'] . "/.qmail-" . $deletePart[0]);
if (!\file_exists($deleteFile)) {
$this->addNotification('info', 'user', __FUNCTION__, 'Weiterleitung existiert nicht.');
} elseif (!\unlink($deleteFile)) {
$this->addNotification('info', 'user', __FUNCTION__, 'Weiterleitung konnte nicht gelöscht werden.');
}
}
private function destroySql($delete = null) {
$sql = "DELETE FROM mailias WHERE id = ?";
$statement = $this->mysqli->prepare($sql);
$statement->bind_param('i', $delete['id']);
if (!$statement->execute()) {
$this->addNotification('info', 'user', __FUNCTION__, 'Email Adresse konnte nicht gelöscht werden.');
$this->addNotification('debug', 'system', __FUNCTION__, '(' . $this->mysqli->errno . ') ' . $this->mysqli->error);
}
}
private function getDestroyList($inputId = null) {
$listId = [];
$toDelete = [];
foreach ($inputId as $id) {
$listId[] = (int) $id;
}
// Benötigte Daten zum löschen aus Datenbank ziehen
$sql = "SELECT id, alias FROM mailias WHERE id IN (" . implode(',', array_unique($listId)) . ") AND user_id = ? ORDER BY id ASC";
$statement = $this->mysqli->prepare($sql);
$statement->bind_param('i', $this->user['id']);
$statement->execute();
$result = $statement->get_result();
while ($row = $result->fetch_assoc()) {
$toDelete[] = $row;
}
$result->free();
return $toDelete;
}
}