-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail_mail_domain_plugin.inc.php
More file actions
329 lines (294 loc) · 14 KB
/
mail_mail_domain_plugin.inc.php
File metadata and controls
329 lines (294 loc) · 14 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
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
<?php
/**
* mail_mail_domain_plugin plugin
*
* @author Sergio Cambra <sergio@programatica.es> 2014
*/
class mail_mail_domain_plugin {
var $plugin_name = 'mail_mail_domain_plugin';
var $class_name = 'mail_mail_domain_plugin';
/*
This function is called when the plugin is loaded
*/
function onLoad() {
global $app;
//Register for the events
$app->plugin->registerEvent('mail:mail_domain:on_after_insert', 'mail_mail_domain_plugin', 'mail_mail_domain_edit');
$app->plugin->registerEvent('mail:mail_domain:on_after_update', 'mail_mail_domain_plugin', 'mail_mail_domain_edit');
}
/*
Function to create the sites_web_domain rule and insert it into the custom rules
*/
function mail_mail_domain_edit($event_name, $page_form) {
global $app, $conf;
$domain = $app->functions->idn_encode($page_form->dataRecord['domain']);
// make sure that the record belongs to the client group and not the admin group when admin inserts it
// also make sure that the user can not delete entry created by an admin
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($page_form->dataRecord["client_group_id"])) {
$client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]);
$updates = "sys_groupid = ?, sys_perm_group = 'ru'";
$update_params = array($client_group_id);
if ($event_name == 'mail:mail_domain:on_after_update') {
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $client_group_id);
$client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
$updates .= ", sys_userid = ?";
$update_params[] = $client_user_id;
}
$update_params[] = $page_form->id;
$app->db->query("UPDATE mail_domain SET " . $updates . " WHERE domain_id = ?", true, $update_params);
}
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($page_form->dataRecord["client_group_id"])) {
$client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]);
$updates = "sys_groupid = ?, sys_perm_group = 'riud'";
$update_params = array($client_group_id);
if ($event_name == 'mail:mail_domain:on_after_update') {
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $client_group_id);
$client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
$updates .= ", sys_userid = ?";
$update_params[] = $client_user_id;
}
$update_params[] = $page_form->id;
$app->db->query("UPDATE mail_domain SET " . $updates . " WHERE domain_id = ?", true, $update_params);
}
//** If the domain name or owner has been changed, change the domain and owner in all mailbox records
if($page_form->oldDataRecord && ($page_form->oldDataRecord['domain'] != $domain ||
(isset($page_form->dataRecord['client_group_id']) && $page_form->oldDataRecord['sys_groupid'] != $page_form->dataRecord['client_group_id']))) {
$app->uses('getconf');
$mail_config = $app->getconf->get_server_config($page_form->dataRecord["server_id"], 'mail');
$old_domain = $app->functions->idn_encode($page_form->oldDataRecord['domain']);
//* Update the mailboxes
$mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", "%@" . $page_form->oldDataRecord['domain']);
$sys_groupid = $app->functions->intval((isset($page_form->dataRecord['client_group_id']))?$page_form->dataRecord['client_group_id']:$page_form->oldDataRecord['sys_groupid']);
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $sys_groupid);
$client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1);
if(is_array($mailusers)) {
foreach($mailusers as $rec) {
// setting Maildir, Homedir, UID and GID
$mail_parts = explode("@", $rec['email']);
$maildir = str_replace("[domain]", $domain, $mail_config["maildir_path"]);
$maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
$email = $mail_parts[0].'@'.$domain;
// update spamfilter_users and spamfilter_wblist if email change
$skip_spamfilter_users_update = false;
if($email != $rec['email']) {
$tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['email']);
if($tmp_olduser['id'] > 0) {
$tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email);
if($tmp_newuser['id'] > 0) {
// There is a spamfilter_users for both old and new email, we'll update old wblist entries
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
foreach ($tmp_wblist as $tmp) {
$update_data = array(
'rid' => $tmp_newuser['id'],
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
}
// now delete old spamfilter_users entry
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']);
} else {
$update_data = array(
'email' => $email,
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['email'])) {
$update_data['fullname'] = $app->functions->idn_decode($email);
}
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']);
$skip_spamfilter_users_update = true;
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
foreach ($tmp_wblist as $tmp) {
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
}
}
}
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email);
if($tmp_user["id"] > 0) {
// There is already a record that we will update
if(!$skip_spamfilter_users_update) {
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']);
}
} else {
// We create a new record
$insert_data = array(
"sys_userid" => $client_user_id,
"sys_groupid" => $sys_groupid,
"sys_perm_user" => 'riud',
"sys_perm_group" => 'riud',
"sys_perm_other" => '',
"server_id" => $page_form->dataRecord["server_id"],
"priority" => 5,
"policy_id" => 0,
"email" => $email,
"fullname" => $app->functions->idn_decode($email),
"local" => 'Y'
);
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
}
}
$app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']);
}
}
//* Update the aliases
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ?", '%@' . $old_domain, '%@' . $old_domain);
if(is_array($forwardings)) {
foreach($forwardings as $rec) {
$destination = str_replace($old_domain, $domain, $rec['destination']);
$source = str_replace($old_domain, $domain, $rec['source']);
// update spamfilter_users and spamfilter_wblist if source email changes
$skip_spamfilter_users_update = false;
if(strpos($rec['source'],'@'.$old_domain) && $source != $rec['source']) {
$tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['source']);
if($tmp_olduser['id'] > 0) {
$tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source);
if($tmp_newuser['id'] > 0) {
// There is a spamfilter_users for both old and new email, we'll update old wblist entries
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
foreach ($tmp_wblist as $tmp) {
$update_data = array(
'rid' => $tmp_newuser['id'],
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
}
// now delete old spamfilter_users entry
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']);
} else {
$update_data = array(
'email' => $source,
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['source'])) {
$update_data['fullname'] = $app->functions->idn_decode($source);
}
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']);
$skip_spamfilter_users_update = true;
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
foreach ($tmp_wblist as $tmp) {
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
}
}
}
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source);
if($tmp_user["id"] > 0) {
// There is already a record that we will update
if(!$skip_spamfilter_users_update) {
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']);
}
}
}
$app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']);
}
}
//* Update the mailinglist
$mailinglists = $app->db->queryAllRecords("SELECT * FROM mail_mailinglist WHERE domain = ?", $old_domain);
if(is_array($mailinglists)) {
foreach($mailinglists as $rec) {
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
'domain' => $domain,
'email' => str_replace($old_domain, $domain, $rec['email']),
);
$app->db->datalogUpdate('mail_mailinglist', $update_data, 'mailinglist_id', $rec['mailinglist_id']);
}
}
//* Update the mailget records
$mail_gets = $app->db->queryAllRecords("SELECT mailget_id, destination FROM mail_get WHERE destination LIKE ?", "%@" . $page_form->oldDataRecord['domain']);
if(is_array($mail_gets)) {
foreach($mail_gets as $rec) {
$destination = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['destination']);
$app->db->datalogUpdate('mail_get', array("destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailget_id', $rec['mailget_id']);
}
}
// Spamfilter policy
$policy_id = $app->functions->intval($page_form->dataRecord["policy"]);
// If domain changes, update spamfilter_users
// and fire spamfilter_wblist_update events so rspamd files are rewritten
if ($old_domain != $domain) {
$tmp_users = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email LIKE ?", '%@' . $old_domain);
if(is_array($tmp_users)) {
foreach ($tmp_users as $tmp_old) {
$tmp_new = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $domain);
if($tmp_new['id'] > 0) {
// There is a spamfilter_users for both old and new domain, we'll update old wblist entries
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
'rid' => $tmp_new['id'],
);
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_old['id']);
foreach ($tmp_wblist as $tmp) {
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
}
// now delete old spamfilter_users entry
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_old['id']);
/// and update the new
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
);
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_old['id']);
} else {
$update_data = array(
'sys_userid' => $client_user_id,
'sys_groupid' => $sys_groupid,
'email' => '@' . $domain,
);
if($tmp_old['fullname'] == '@' . $old_domain) {
$update_data['fullname'] = '@' . $domain;
}
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_old['id']);
}
}
}
}
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain);
if(isset($tmp_user['id']) && $tmp_user['id'] > 0) {
// There is already a record that we will update
if($policy_id != $tmp_user['policy_id']) {
$update_data = array(
'policy_id' => $policy_id,
);
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user["id"]);
}
} else {
// We create a new record
$insert_data = array(
"sys_userid" => $client_user_id,
"sys_groupid" => $sys_groupid,
"sys_perm_user" => 'riud',
"sys_perm_group" => 'riud',
"sys_perm_other" => '',
"server_id" => $page_form->dataRecord["server_id"],
"priority" => 5,
"policy_id" => $policy_id,
"email" => '@' . $domain,
"fullname" => '@' . $domain,
"local" => 'Y'
);
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
}
} // end if domain name changed
}
}