|
| 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 | + //** If the domain name or owner has been changed, change the domain and owner in all mailbox records |
| 54 | + if($page_form->oldDataRecord && ($page_form->oldDataRecord['domain'] != $page_form->dataRecord['domain'] || |
| 55 | + (isset($page_form->dataRecord['client_group_id']) && $page_form->oldDataRecord['sys_groupid'] != $page_form->dataRecord['client_group_id']))) { |
| 56 | + $app->uses('getconf'); |
| 57 | + $mail_config = $app->getconf->get_server_config($page_form->dataRecord["server_id"], 'mail'); |
| 58 | + |
| 59 | + //* Update the mailboxes |
| 60 | + $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like '%@".$app->db->quote($page_form->oldDataRecord['domain'])."'"); |
| 61 | + $sys_groupid = $app->functions->intval((isset($page_form->dataRecord['client_group_id']))?$page_form->dataRecord['client_group_id']:$page_form->oldDataRecord['sys_groupid']); |
| 62 | + $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $sys_groupid"); |
| 63 | + $client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1); |
| 64 | + if(is_array($mailusers)) { |
| 65 | + foreach($mailusers as $rec) { |
| 66 | + // setting Maildir, Homedir, UID and GID |
| 67 | + $mail_parts = explode("@", $rec['email']); |
| 68 | + $maildir = str_replace("[domain]", $page_form->dataRecord['domain'], $mail_config["maildir_path"]); |
| 69 | + $maildir = str_replace("[localpart]", $mail_parts[0], $maildir); |
| 70 | + $maildir = $app->db->quote($maildir); |
| 71 | + $email = $app->db->quote($mail_parts[0].'@'.$page_form->dataRecord['domain']); |
| 72 | + $app->db->datalogUpdate('mail_user', "maildir = '$maildir', email = '$email', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailuser_id', $rec['mailuser_id']); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + //* Update the aliases |
| 77 | + $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'])."'"); |
| 78 | + if(is_array($forwardings)) { |
| 79 | + foreach($forwardings as $rec) { |
| 80 | + $destination = $app->db->quote(str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination'])); |
| 81 | + $source = $app->db->quote(str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['source'])); |
| 82 | + $app->db->datalogUpdate('mail_forwarding', "source = '$source', destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'forwarding_id', $rec['forwarding_id']); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + //* Update the mailinglist |
| 87 | + $mailing_lists = $app->db->queryAllRecords("SELECT mailinglist_id FROM mail_mailinglist WHERE domain = '".$app->db->quote($page_form->oldDataRecord['domain'])."'"); |
| 88 | + if(is_array($mailing_lists)) { |
| 89 | + foreach($mailing_lists as $rec) { |
| 90 | + $app->db->datalogUpdate('mail_mailinglist', "sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailinglist_id', $rec['mailinglist_id']); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + //* Update the mailget records |
| 95 | + $mail_gets = $app->db->queryAllRecords("SELECT mailget_id, destination FROM mail_get WHERE destination LIKE '%@".$app->db->quote($page_form->oldDataRecord['domain'])."'"); |
| 96 | + if(is_array($mail_gets)) { |
| 97 | + foreach($mail_gets as $rec) { |
| 98 | + $destination = $app->db->quote(str_replace($page_form->oldDataRecord['domain'], $page_form->dataRecord['domain'], $rec['destination'])); |
| 99 | + $app->db->datalogUpdate('mail_get', "destination = '$destination', sys_userid = $client_user_id, sys_groupid = '$sys_groupid'", 'mailget_id', $rec['mailget_id']); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if ($page_form->oldDataRecord["domain"] != $page_form->dataRecord['domain']) { |
| 104 | + //* Delete the old spamfilter record |
| 105 | + $tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".$app->db->quote($page_form->oldDataRecord["domain"])."'"); |
| 106 | + $app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]); |
| 107 | + unset($tmp); |
| 108 | + } |
| 109 | + $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'])."'"); |
| 110 | + |
| 111 | + } // end if domain name changed |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments