Skip to content

Commit 6f4f7db

Browse files
committed
move code for mail domain's owner change to plugin
1 parent eb0b8f7 commit 6f4f7db

File tree

2 files changed

+136
-123
lines changed

2 files changed

+136
-123
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* mail_mail_domain_plugin plugin
4+
*
5+
* @author Sergio Cambra <sergio@programatica.es> 2014
6+
*/
7+
8+
9+
class mail_mail_domain_plugin {
10+
11+
var $plugin_name = 'mail_mail_domain_plugin';
12+
var $class_name = 'mail_mail_domain_plugin';
13+
14+
/*
15+
This function is called when the plugin is loaded
16+
*/
17+
function onLoad() {
18+
global $app;
19+
//Register for the events
20+
$app->plugin->registerEvent('mail:mail_domain:on_after_insert', 'mail_mail_domain_plugin', 'mail_mail_domain_edit');
21+
$app->plugin->registerEvent('mail:mail_domain:on_after_update', 'mail_mail_domain_plugin', 'mail_mail_domain_edit');
22+
}
23+
24+
/*
25+
Function to create the sites_web_domain rule and insert it into the custom rules
26+
*/
27+
function mail_mail_domain_edit($event_name, $page_form) {
28+
global $app, $conf;
29+
30+
// make sure that the record belongs to the clinet group and not the admin group when a dmin inserts it
31+
// also make sure that the user can not delete entry created by an admin
32+
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($page_form->dataRecord["client_group_id"])) {
33+
$client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]);
34+
$updates = "sys_groupid = $client_group_id, sys_perm_group = 'ru'";
35+
if ($event_name == 'mail:mail_domain:on_after_update') {
36+
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
37+
$client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
38+
$updates = "sys_userid = $client_user_id, $updates";
39+
}
40+
$app->db->query("UPDATE mail_domain SET $updates WHERE domain_id = ".$page_form->id);
41+
}
42+
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($page_form->dataRecord["client_group_id"])) {
43+
$client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]);
44+
$updates = "sys_groupid = $client_group_id, sys_perm_group = 'riud'";
45+
if ($event_name == 'mail:mail_domain:on_after_update') {
46+
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
47+
$client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
48+
$updates = "sys_userid = $client_user_id, $updates";
49+
}
50+
$app->db->query("UPDATE mail_domain SET $updates WHERE domain_id = ".$page_form->id);
51+
}
52+
53+
// Spamfilter policy
54+
$policy_id = $app->functions->intval($page_form->dataRecord["policy"]);
55+
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($page_form->dataRecord["domain"])."'");
56+
if($policy_id > 0) {
57+
if($tmp_user["id"] > 0) {
58+
// There is already a record that we will update
59+
$app->db->datalogUpdate('spamfilter_users', "policy_id = $policy_id", 'id', $tmp_user["id"]);
60+
} else {
61+
$tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ".$page_form->id);
62+
// We create a new record
63+
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`)
64+
VALUES (".$_SESSION["s"]["user"]["userid"].", ".$app->functions->intval($tmp_domain["sys_groupid"]).", 'riud', 'riud', '', ".$app->functions->intval($page_form->dataRecord["server_id"]).", 5, ".$app->functions->intval($policy_id).", '@".$app->db->quote($page_form->dataRecord["domain"])."', '@".$app->db->quote($page_form->dataRecord["domain"])."', 'Y')";
65+
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
66+
unset($tmp_domain);
67+
}
68+
} else {
69+
if($tmp_user["id"] > 0) {
70+
// There is already a record but the user shall have no policy, so we delete it
71+
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
72+
}
73+
} // endif spamfilter policy
74+
75+
//** If the domain name or owner has been changed, change the domain and owner in all mailbox records
76+
if($page_form->oldDataRecord && ($page_form->oldDataRecord['domain'] != $page_form->dataRecord['domain'] ||
77+
(isset($page_form->dataRecord['client_group_id']) && $page_form->oldDataRecord['sys_groupid'] != $page_form->dataRecord['client_group_id']))) {
78+
$app->uses('getconf');
79+
$mail_config = $app->getconf->get_server_config($page_form->dataRecord["server_id"], 'mail');
80+
81+
//* Update the mailboxes
82+
$mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like '%@".$app->db->quote($page_form->oldDataRecord['domain'])."'");
83+
$sys_groupid = $app->functions->intval((isset($page_form->dataRecord['client_group_id']))?$page_form->dataRecord['client_group_id']:$page_form->oldDataRecord['sys_groupid']);
84+
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $sys_groupid");
85+
$client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1);
86+
if(is_array($mailusers)) {
87+
foreach($mailusers as $rec) {
88+
// setting Maildir, Homedir, UID and GID
89+
$mail_parts = explode("@", $rec['email']);
90+
$maildir = str_replace("[domain]", $page_form->dataRecord['domain'], $mail_config["maildir_path"]);
91+
$maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
92+
$maildir = $app->db->quote($maildir);
93+
$email = $app->db->quote($mail_parts[0].'@'.$page_form->dataRecord['domain']);
94+
$app->db->datalogUpdate('mail_user', "maildir = '$maildir', email = '$email', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailuser_id', $rec['mailuser_id']);
95+
}
96+
}
97+
98+
//* Update the aliases
99+
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like '%@".$app->db->quote($page_form->oldDataRecord['domain'])."' OR destination like '%@".$app->db->quote($page_form->oldDataRecord['domain'])."'");
100+
if(is_array($forwardings)) {
101+
foreach($forwardings as $rec) {
102+
$destination = $app->db->quote(str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination']));
103+
$source = $app->db->quote(str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['source']));
104+
$app->db->datalogUpdate('mail_forwarding', "source = '$source', destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'forwarding_id', $rec['forwarding_id']);
105+
}
106+
}
107+
108+
//* Update the mailinglist
109+
$mailing_lists = $app->db->queryAllRecords("SELECT mailinglist_id FROM mail_mailinglist WHERE domain = '".$app->db->quote($page_form->oldDataRecord['domain'])."'");
110+
if(is_array($mailing_lists)) {
111+
foreach($mailing_lists as $rec) {
112+
$app->db->datalogUpdate('mail_mailinglist', "sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailinglist_id', $rec['mailinglist_id']);
113+
}
114+
}
115+
116+
//* Update the mailget records
117+
$mail_gets = $app->db->queryAllRecords("SELECT mailget_id FROM mail_get WHERE destination LIKE '%@".$app->db->quote($page_form->oldDataRecord['domain'])."'");
118+
if(is_array($mail_gets)) {
119+
foreach($mail_gets as $rec) {
120+
$destination = $app->db->quote(str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination']));
121+
$app->db->datalogUpdate('mail_get', "destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailget_id', $rec['mailget_id']);
122+
}
123+
}
124+
125+
if ($page_form->oldDataRecord["domain"] != $page_form->dataRecord['domain']) {
126+
//* Delete the old spamfilter record
127+
$tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($page_form->oldDataRecord["domain"])."'");
128+
$app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]);
129+
unset($tmp);
130+
}
131+
$app->db->query("UPDATE spamfilter_users SET email=REPLACE(email, '".$app->db->quote($page_form->oldDataRecord['domain'])."', '".$app->db->quote($page_form->dataRecord['domain'])."'), sys_userid = $client_user_id, sys_groupid = $sys_groupid WHERE email LIKE '%@".$app->db->quote($page_form->oldDataRecord['domain'])."'");
132+
133+
} // end if domain name changed
134+
}
135+
136+
}

interface/web/mail/mail_domain_edit.php

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -255,38 +255,6 @@ function onSubmit() {
255255
parent::onSubmit();
256256
}
257257

258-
function onAfterInsert() {
259-
global $app, $conf;
260-
261-
// make sure that the record belongs to the client group and not the admin group when a dmin inserts it
262-
// also make sure that the user can not delete domain created by a admin
263-
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
264-
$client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
265-
$app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
266-
}
267-
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
268-
$client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
269-
$app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
270-
}
271-
272-
// Spamfilter policy
273-
$policy_id = $app->functions->intval($this->dataRecord["policy"]);
274-
if($policy_id > 0) {
275-
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->dataRecord["domain"])."'");
276-
if($tmp_user["id"] > 0) {
277-
// There is already a record that we will update
278-
$app->db->datalogUpdate('spamfilter_users', "policy_id = $ploicy_id", 'id', $tmp_user["id"]);
279-
} else {
280-
$tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ".$this->id);
281-
// We create a new record
282-
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`)
283-
VALUES (".$_SESSION["s"]["user"]["userid"].", ".$app->functions->intval($tmp_domain["sys_groupid"]).", 'riud', 'riud', '', ".$app->functions->intval($this->dataRecord["server_id"]).", 5, ".$app->functions->intval($policy_id).", '@".$app->db->quote($this->dataRecord["domain"])."', '@".$app->db->quote($this->dataRecord["domain"])."', 'Y')";
284-
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
285-
unset($tmp_domain);
286-
}
287-
} // endif spamfilter policy
288-
}
289-
290258
function onBeforeUpdate() {
291259
global $app, $conf;
292260

@@ -313,97 +281,6 @@ function onBeforeUpdate() {
313281
}
314282
}
315283

316-
317-
318-
function onAfterUpdate() {
319-
global $app, $conf;
320-
321-
// make sure that the record belongs to the client group and not the admin group when admin inserts it
322-
// also make sure that the user can not delete domain created by a admin
323-
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
324-
$client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
325-
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
326-
$client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
327-
$app->db->query("UPDATE mail_domain SET sys_userid = $client_user_id, sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
328-
}
329-
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
330-
$client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
331-
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
332-
$client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
333-
$app->db->query("UPDATE mail_domain SET sys_userid = $client_user_id, sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
334-
}
335-
336-
// Spamfilter policy
337-
$policy_id = $app->functions->intval($this->dataRecord["policy"]);
338-
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->dataRecord["domain"])."'");
339-
if($policy_id > 0) {
340-
if($tmp_user["id"] > 0) {
341-
// There is already a record that we will update
342-
$app->db->datalogUpdate('spamfilter_users', "policy_id = $policy_id", 'id', $tmp_user["id"]);
343-
} else {
344-
$tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ".$this->id);
345-
// We create a new record
346-
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`)
347-
VALUES (".$_SESSION["s"]["user"]["userid"].", ".$tmp_domain["sys_groupid"].", 'riud', 'riud', '', ".$app->functions->intval($this->dataRecord["server_id"]).", 5, ".$app->functions->intval($policy_id).", '@".$app->db->quote($this->dataRecord["domain"])."', '@".$app->db->quote($this->dataRecord["domain"])."', 'Y')";
348-
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
349-
unset($tmp_domain);
350-
}
351-
} else {
352-
if($tmp_user["id"] > 0) {
353-
// There is already a record but the user shall have no policy, so we delete it
354-
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
355-
}
356-
} // endif spamfilter policy
357-
//** If the domain name or owner has been changed, change the domain and owner in all mailbox records
358-
if($this->oldDataRecord['domain'] != $this->dataRecord['domain'] || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) {
359-
$app->uses('getconf');
360-
$mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"], 'mail');
361-
362-
//* Update the mailboxes
363-
$mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
364-
$sys_groupid = $app->functions->intval((isset($this->dataRecord['client_group_id']))?$this->dataRecord['client_group_id']:$this->oldDataRecord['sys_groupid']);
365-
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
366-
$client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1);
367-
if(is_array($mailusers)) {
368-
foreach($mailusers as $rec) {
369-
// setting Maildir, Homedir, UID and GID
370-
$mail_parts = explode("@", $rec['email']);
371-
$maildir = str_replace("[domain]", $this->dataRecord['domain'], $mail_config["maildir_path"]);
372-
$maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
373-
$maildir = $app->db->quote($maildir);
374-
$email = $app->db->quote($mail_parts[0].'@'.$this->dataRecord['domain']);
375-
$app->db->datalogUpdate('mail_user', "maildir = '$maildir', email = '$email', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailuser_id', $rec['mailuser_id']);
376-
}
377-
}
378-
379-
//* Update the aliases
380-
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like '%@".$app->db->quote($this->oldDataRecord['domain'])."' OR destination like '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
381-
if(is_array($forwardings)) {
382-
foreach($forwardings as $rec) {
383-
$destination = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['destination']));
384-
$source = $app->db->quote(str_replace($this->oldDataRecord['domain'], $this->dataRecord['domain'], $rec['source']));
385-
$app->db->datalogUpdate('mail_forwarding', "source = '$source', destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'forwarding_id', $rec['forwarding_id']);
386-
}
387-
}
388-
389-
//* Update the mailinglist
390-
$app->db->query("UPDATE mail_mailinglist SET sys_userid = $client_user_id, sys_groupid = $sys_groupid WHERE domain = '".$app->db->quote($this->oldDataRecord['domain'])."'");
391-
392-
//* Update the mailget records
393-
$app->db->query("UPDATE mail_get SET destination=REPLACE(destination, '".$app->db->quote($this->oldDataRecord['domain'])."', '".$app->db->quote($this->dataRecord['domain'])."'), sys_userid = $client_user_id, sys_groupid = $sys_groupid WHERE destination LIKE '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
394-
395-
if ($this->oldDataRecord["domain"] != $this->dataRecord['domain']) {
396-
//* Delete the old spamfilter record
397-
$tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($this->oldDataRecord["domain"])."'");
398-
$app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]);
399-
unset($tmp);
400-
}
401-
$app->db->query("UPDATE spamfilter_users SET email=REPLACE(email, '".$app->db->quote($this->oldDataRecord['domain'])."', '".$app->db->quote($this->dataRecord['domain'])."'), sys_userid = $client_user_id, sys_groupid = $sys_groupid WHERE email LIKE '%@".$app->db->quote($this->oldDataRecord['domain'])."'");
402-
403-
} // end if domain name changed
404-
405-
}
406-
407284
}
408285

409286
$page = new page_action;

0 commit comments

Comments
 (0)