Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
6f4f7d 1 <?php
SC 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"]);
cc7a82 34             $updates = "sys_groupid = ?, sys_perm_group = 'ru'";
MC 35             $update_params = array($client_group_id);
6f4f7d 36             if ($event_name == 'mail:mail_domain:on_after_update') {
cc7a82 37                 $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $client_group_id);
6f4f7d 38                 $client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
cc7a82 39                 $updates .= ", sys_userid = ?";
a6e3ae 40                 $update_params[] = $client_user_id;
6f4f7d 41             }
cc7a82 42             $update_params[] = $page_form->id;
MC 43             $app->db->query("UPDATE mail_domain SET " . $updates . " WHERE domain_id = ?", true, $update_params);
6f4f7d 44         }
SC 45         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($page_form->dataRecord["client_group_id"])) {
46             $client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]);
47             $updates = "sys_groupid = $client_group_id, sys_perm_group = 'riud'";
cc7a82 48             $update_params = array($client_group_id);
6f4f7d 49             if ($event_name == 'mail:mail_domain:on_after_update') {
cc7a82 50                 $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $client_group_id);
6f4f7d 51                 $client_user_id = ($tmp['userid'] > 0)?$tmp['userid']:1;
cc7a82 52                 $updates .= ", sys_userid = ?";
a6e3ae 53                 $update_params[] = $client_user_id;
6f4f7d 54             }
cc7a82 55             $update_params[] = $page_form->id;
MC 56             $app->db->query("UPDATE mail_domain SET " . $updates . " WHERE domain_id = ?", true, $update_params);
6f4f7d 57         }
SC 58
59         //** If the domain name or owner has been changed, change the domain and owner in all mailbox records
60         if($page_form->oldDataRecord && ($page_form->oldDataRecord['domain'] != $page_form->dataRecord['domain'] ||
61                 (isset($page_form->dataRecord['client_group_id']) && $page_form->oldDataRecord['sys_groupid'] != $page_form->dataRecord['client_group_id']))) {
62             $app->uses('getconf');
63             $mail_config = $app->getconf->get_server_config($page_form->dataRecord["server_id"], 'mail');
64
65             //* Update the mailboxes
cc7a82 66             $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", "%@" . $page_form->oldDataRecord['domain']);
6f4f7d 67             $sys_groupid = $app->functions->intval((isset($page_form->dataRecord['client_group_id']))?$page_form->dataRecord['client_group_id']:$page_form->oldDataRecord['sys_groupid']);
cc7a82 68             $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $sys_groupid);
6f4f7d 69             $client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1);
SC 70             if(is_array($mailusers)) {
71                 foreach($mailusers as $rec) {
72                     // setting Maildir, Homedir, UID and GID
73                     $mail_parts = explode("@", $rec['email']);
74                     $maildir = str_replace("[domain]", $page_form->dataRecord['domain'], $mail_config["maildir_path"]);
75                     $maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
3a11d2 76                     $email = $mail_parts[0].'@'.$page_form->dataRecord['domain'];
MC 77                     $app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']);
6f4f7d 78                 }
SC 79             }
80
81             //* Update the aliases
cc7a82 82             $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ?", "%@" . $page_form->oldDataRecord['domain'], "%@" . $page_form->oldDataRecord['domain']);
6f4f7d 83             if(is_array($forwardings)) {
SC 84                 foreach($forwardings as $rec) {
3a11d2 85                     $destination = str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination']);
MC 86                     $source = str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['source']);
87                     $app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']);
6f4f7d 88                 }
SC 89             }
90
91             //* Update the mailinglist
cc7a82 92             $mailing_lists = $app->db->queryAllRecords("SELECT mailinglist_id FROM mail_mailinglist WHERE domain = ?", $page_form->oldDataRecord['domain']);
6f4f7d 93             if(is_array($mailing_lists)) {
SC 94                 foreach($mailing_lists as $rec) {
3a11d2 95                     $app->db->datalogUpdate('mail_mailinglist', array("sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailinglist_id', $rec['mailinglist_id']);
6f4f7d 96                 }
SC 97             }
98
99             //* Update the mailget records
cc7a82 100             $mail_gets = $app->db->queryAllRecords("SELECT mailget_id, destination FROM mail_get WHERE destination LIKE ?", "%@" . $page_form->oldDataRecord['domain']);
6f4f7d 101             if(is_array($mail_gets)) {
SC 102                 foreach($mail_gets as $rec) {
3a11d2 103                     $destination = str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination']);
MC 104                     $app->db->datalogUpdate('mail_get', array("destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailget_id', $rec['mailget_id']);
6f4f7d 105                 }
SC 106             }
107
108             if ($page_form->oldDataRecord["domain"] != $page_form->dataRecord['domain']) {
109                 //* Delete the old spamfilter record
cc7a82 110                 $tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", "@" . $page_form->oldDataRecord["domain"]);
6f4f7d 111                 $app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]);
SC 112                 unset($tmp);
113             }
cc7a82 114             $app->db->query("UPDATE spamfilter_users SET email=REPLACE(email, ?, ?), sys_userid = ?, sys_groupid = ? WHERE email LIKE ?", $page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $client_user_id, $sys_groupid, "%@" . $page_form->oldDataRecord['domain']);
6f4f7d 115
SC 116         } // end if domain name changed
117     }
118
119 }