Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
b5a23a 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class mail_plugin {
b1a6a5 32
b5a23a 33     var $plugin_name = 'mail_plugin';
T 34     var $class_name  = 'mail_plugin';
b1a6a5 35
392450 36     //* This function is called during ispconfig installation to determine
T 37     //  if a symlink shall be created for this plugin.
38     function onInstall() {
39         global $conf;
b1a6a5 40
392450 41         if($conf['services']['mail'] == true) {
T 42             return true;
43         } else {
44             return false;
45         }
b1a6a5 46
392450 47     }
b1a6a5 48
MC 49
b5a23a 50     /*
T 51          This function is called when the plugin is loaded
52     */
b1a6a5 53
b5a23a 54     function onLoad() {
T 55         global $app;
b1a6a5 56
b5a23a 57         /*
T 58         Register for the events
59         */
b1a6a5 60
4585cf 61         //* Mailboxes
b1a6a5 62         $app->plugins->registerEvent('mail_user_insert', $this->plugin_name, 'user_insert');
MC 63         $app->plugins->registerEvent('mail_user_update', $this->plugin_name, 'user_update');
64         $app->plugins->registerEvent('mail_user_delete', $this->plugin_name, 'user_delete');
65
4585cf 66         //* Mail Domains
T 67         //$app->plugins->registerEvent('mail_domain_insert',$this->plugin_name,'domain_insert');
68         //$app->plugins->registerEvent('mail_domain_update',$this->plugin_name,'domain_update');
b1a6a5 69         $app->plugins->registerEvent('mail_domain_delete', $this->plugin_name, 'domain_delete');
MC 70
9234cc 71         //* Mail transports
b1a6a5 72         $app->plugins->registerEvent('mail_transport_insert', $this->plugin_name, 'transport_update');
MC 73         $app->plugins->registerEvent('mail_transport_update', $this->plugin_name, 'transport_update');
74         $app->plugins->registerEvent('mail_transport_delete', $this->plugin_name, 'transport_update');
75
b5a23a 76     }
b1a6a5 77
MC 78
79     function user_insert($event_name, $data) {
b5a23a 80         global $app, $conf;
b1a6a5 81
f12a6c 82         //* get the config
663caf 83         $app->uses('getconf,system');
J 84         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
06303b 85
T 86         // convert to lower case - it could cause problems if some directory above has upper case name
b1a6a5 87         //  $data['new']['maildir'] = strtolower($data['new']['maildir']);
MC 88
f12a6c 89         $maildomain_path = $data['new']['maildir'];
T 90         $tmp_basepath = $data['new']['maildir'];
b1a6a5 91         $tmp_basepath_parts = explode('/', $tmp_basepath);
f12a6c 92         unset($tmp_basepath_parts[count($tmp_basepath_parts)-1]);
b1a6a5 93         $base_path = implode('/', $tmp_basepath_parts);
f12a6c 94
0e2978 95         //* Set the email-uid and gid if not given
11ccaa 96         if (($data['new']['uid'] == -1) || ($data['new']['gid'] == -1)) {
0e2978 97             $app->log('Setting uid and gid automatically',LOGLEVEL_DEBUG);
DM 98             if ($mail_config["mailbox_virtual_uidgid_maps"] == 'y') {
99                 $app->log('Map uid to linux-user',LOGLEVEL_DEBUG);
100                 $email_parts = explode('@',$data['new']['email']);
cc7a82 101                 $webdomain = $app->db->queryOneRecord("SELECT domain_id, server_id, system_user, parent_domain_id FROM web_domain WHERE domain = ?", $email_parts[1]);
0e2978 102                 if ($webdomain) {
70da66 103                     while (($webdomain['system_user'] == null) && ($webdomain['parent_domain_id'] != 0)) {
cc7a82 104                         $webdomain = $app->db->queryOneRecord("SELECT domain_id, server_id, system_user, parent_domain_id FROM web_domain WHERE domain_id = ?", $webdomain['parent_domain_id']);
0e2978 105                     }
DM 106                     $app->log($data['new']['server_id'].' == '.$webdomain['server_id'],LOGLEVEL_DEBUG);
107
108                     // only if web and mailserver are identical
109                     if ($data['new']['server_id'] == $webdomain['server_id']) {
110                         $data['new']['uid'] = $app->system->getuid($webdomain['system_user']);
111                     }
112                 }
113             }
114         }
115         // if nothing set before -> use standard mailuser uid and gid vmail
11ccaa 116         if ($data['new']['uid'] == -1) $data['new']['uid'] = $mail_config["mailuser_uid"];
DM 117         if ($data['new']['gid'] == -1) $data['new']['gid'] = $mail_config["mailuser_gid"];
0e2978 118         $app->log('Mailuser uid: '.$data['new']['uid'].', gid: '.$data['new']['gid'],LOGLEVEL_DEBUG);
DM 119
120         // update DB if values changed
cc7a82 121         $app->db->query("UPDATE mail_user SET uid = ?, gid = ? WHERE mailuser_id = ?", $data['new']['uid'], $data['new']['gid'], $data['new']['mailuser_id']);
0e2978 122
DM 123         // now get names of uid and gid
124         $user = $app->system->getuser($data['new']['uid']);
125         $group = $app->system->getgroup($data['new']['gid']);
f12a6c 126         //* Create the mail domain directory, if it does not exist
T 127         if(!empty($base_path) && !is_dir($base_path)) {
355efb 128             //exec("su -c 'mkdir -p ".escapeshellcmd($base_path)."' ".$mail_config['mailuser_name']);
0e2978 129             $app->system->mkdirpath($base_path, 0770, $mail_config['mailuser_name'], $mail_config['mailuser_group']); // needs group-access because users of subfolders may differ from vmail
b1a6a5 130             $app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
6cc49f 131         }
b1a6a5 132
f339eb 133         if ($data['new']['maildir_format'] == 'mdbox') {
D 134             exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" INBOX'");
135             exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Sent'");
136             exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Trash'");
137             exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Junk'");
138             exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Drafts'");
139             
140             exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" INBOX'");
141             exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Sent'");
142             exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Trash'");
143             exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Junk'");
144             exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Drafts'");
b5a23a 145         }
f339eb 146         else {
D 147             // Dovecot uses a different mail layout with a separate 'Maildir' subdirectory.
148             if($mail_config['pop3_imap_daemon'] == 'dovecot') {
149                 $app->system->mkdirpath($maildomain_path, 0700, $user, $group);
150                 $app->log('Created Directory: '.$maildomain_path, LOGLEVEL_DEBUG);
151                 $maildomain_path .= '/Maildir';
152             }
153                     
154             //* When the mail user dir exists but it is not a valid maildir, move it to corrupted maildir folder
155             if(!empty($maildomain_path) && is_dir($maildomain_path) && !is_dir($maildomain_path.'/new') && !is_dir($maildomain_path.'/cur')) {
156                 if(!is_dir($mail_config['homedir_path'].'/corrupted/'.$data['new']['mailuser_id'])) $app->system->mkdirpath($mail_config['homedir_path'].'/corrupted/'.$data['new']['mailuser_id'], 0700, $mail_config['mailuser_name'], $mail_config['mailuser_group']);
157                 exec("su -c 'mv -f ".escapeshellcmd($data['new']['maildir'])." ".$mail_config['homedir_path'].'/corrupted/'.$data['new']['mailuser_id']."' vmail");
158                 $app->log('Moved invalid maildir to corrupted Maildirs folder: '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_WARN);
159             }
160     
161             //* Create the maildir, if it doesn not exist, set permissions, set quota.
162             if(!empty($maildomain_path) && !is_dir($maildomain_path)) {
163     
164                 //exec("su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
165                 $app->system->maildirmake($maildomain_path, $user, '', $group);
166     
167                 //* This is to fix the maildrop quota not being rebuilt after the quota is changed.
168                 if($mail_config['pop3_imap_daemon'] != 'dovecot') {
169                     if(is_dir($maildomain_path)) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user); // Avoid maildirmake quota bug, see debian bug #214911
170                     $app->log('Created Maildir: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user, LOGLEVEL_DEBUG);
171                 }
172             }
173     
174             if(!is_dir($data['new']['maildir'].'/.Sent')) {
175                 //exec("su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
176                 //$app->log('Created submaildir Sent: '."su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
177                 $app->system->maildirmake($maildomain_path, $user, 'Sent', $group);
178             }
179             if(!is_dir($data['new']['maildir'].'/.Drafts')) {
180                 //exec("su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
181                 //$app->log('Created submaildir Drafts: '."su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
182                 $app->system->maildirmake($maildomain_path, $user, 'Drafts', $group);
183             }
184             if(!is_dir($data['new']['maildir'].'/.Trash')) {
185                 //exec("su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
186                 //$app->log('Created submaildir Trash: '."su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
187                 $app->system->maildirmake($maildomain_path, $user, 'Trash', $group);
188             }
189             if(!is_dir($data['new']['maildir'].'/.Junk')) {
190                 //exec("su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
191                 //$app->log('Created submaildir Junk: '."su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
192                 $app->system->maildirmake($maildomain_path, $user, 'Junk', $group);
193             }
194     
195             // Set permissions now recursive
196             exec('chown -R '.$user.':'.$group.' '.escapeshellcmd($data['new']['maildir']));
197             $app->log('Set ownership on '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_DEBUG);
198     
199             //* Set the maildir quota
200             if(is_dir($data['new']['maildir'].'/new') && $mail_config['pop3_imap_daemon'] != 'dovecot') {
201                 if($data['new']['quota'] > 0) {
202                     if(is_dir($data['new']['maildir'])) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user);
203                     $app->log('Set Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user, LOGLEVEL_DEBUG);
204                 }
2b7e04 205             }
0ca0b8 206         }
a043bd 207
92e509 208         //* Send the welcome email message
bb984c 209         $domain = explode('@', $data["new"]["email"])[1];
7b4df6 210         $html = false;
MB 211         if(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$domain.'.html')) {
be2cd7 212             $lines = file($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$domain.'.html');
7b4df6 213             $html = true;
MB 214         } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$conf['language'].'.html')) {
be2cd7 215             $lines = file($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$conf['language'].'.html');
7b4df6 216             $html = true;
MB 217         } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$domain.'.txt')) {
bb984c 218             $lines = file($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$domain.'.txt');
DS 219         } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$conf['language'].'.txt')) {
355efb 220             $lines = file($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$conf['language'].'.txt');
92e509 221         } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_en.txt')) {
355efb 222             $lines = file($conf['rootpath'].'/conf-custom/mail/welcome_email_en.txt');
92e509 223         } elseif(file_exists($conf['rootpath'].'/conf/mail/welcome_email_'.$conf['language'].'.txt')) {
355efb 224             $lines = file($conf['rootpath'].'/conf/mail/welcome_email_'.$conf['language'].'.txt');
92e509 225         } else {
355efb 226             $lines = file($conf['rootpath'].'/conf/mail/welcome_email_en.txt');
92e509 227         }
b1a6a5 228
355efb 229         //* Get from address
b1a6a5 230         $parts = explode(':', trim($lines[0]));
355efb 231         unset($parts[0]);
b1a6a5 232         $welcome_mail_from  = implode(':', $parts);
355efb 233         unset($lines[0]);
b1a6a5 234
355efb 235         //* Get subject
b1a6a5 236         $parts = explode(':', trim($lines[1]));
355efb 237         unset($parts[0]);
b1a6a5 238         $welcome_mail_subject  = implode(':', $parts);
355efb 239         unset($lines[1]);
b1a6a5 240
355efb 241         //* Get message
T 242         $welcome_mail_message = trim(implode($lines));
243         unset($tmp);
b1a6a5 244
92e509 245         $mailHeaders      = "MIME-Version: 1.0" . "\n";
7b4df6 246         if($html) {
be2cd7 247             $mailHeaders     .= "Content-Type: text/html; charset=utf-8" . "\n";
DS 248             $mailHeaders     .= "Content-Transfer-Encoding: quoted-printable" . "\n";
249         } else {
f571ae 250             $mailHeaders     .= "Content-Type: text/plain; charset=utf-8" . "\n";
be2cd7 251             $mailHeaders     .= "Content-Transfer-Encoding: 8bit" . "\n";
DS 252         }
6d49fc 253         $mailHeaders     .= "From: $welcome_mail_from" . "\n";
T 254         $mailHeaders     .= "Reply-To: $welcome_mail_from" . "\n";
92e509 255         $mailTarget       = $data["new"]["email"];
f682c3 256         $mailSubject      = "=?utf-8?B?".base64_encode($welcome_mail_subject)."?=";
92e509 257
992797 258         //* Send the welcome email only on the "master" mail server to avoid duplicate emails
MC 259         if($conf['mirror_server_id'] == 0) mail($mailTarget, $mailSubject, $welcome_mail_message, $mailHeaders);
b1a6a5 260
b5a23a 261     }
b1a6a5 262
MC 263     function user_update($event_name, $data) {
b5a23a 264         global $app, $conf;
b1a6a5 265
b5a23a 266         // get the config
663caf 267         $app->uses('getconf,system');
J 268         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
06303b 269
T 270         // convert to lower case - it could cause problems if some directory above has upper case name
271         // $data['new']['maildir'] = strtolower($data['new']['maildir']);
b1a6a5 272
b5a23a 273         // Create the maildir, if it does not exist
f42df0 274         /*
be40ba 275         if(!is_dir($data['new']['maildir'])) {
663caf 276             mkdir(escapeshellcmd($data['new']['maildir']), 0, true);
J 277             chown(escapeshellcmd($data['new']['maildir']), $mail_config['mailuser_name']);
278             chgrp(escapeshellcmd($data['new']['maildir']), $mail_config['mailuser_group']);
b5a23a 279             $app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
f42df0 280         }
T 281         */
b1a6a5 282
f339eb 283         // Maildir-Format must not be changed on this way !!
D 284         $data['new']['maildir_format'] = $data['old']['maildir_format'];
285         
f42df0 286         $maildomain_path = $data['new']['maildir'];
T 287         $tmp_basepath = $data['new']['maildir'];
b1a6a5 288         $tmp_basepath_parts = explode('/', $tmp_basepath);
f42df0 289         unset($tmp_basepath_parts[count($tmp_basepath_parts)-1]);
b1a6a5 290         $base_path = implode('/', $tmp_basepath_parts);
f42df0 291
98273d 292         //* Set the email-uid and gid if not given -> in case of changed settings again setting here
DM 293         if (($data['new']['uid'] == -1) || ($data['new']['gid'] == -1)) {
294             $app->log('Setting uid and gid automatically',LOGLEVEL_DEBUG);
295             if ($mail_config["mailbox_virtual_uidgid_maps"] == 'y') {
296                 $app->log('Map uid to linux-user',LOGLEVEL_DEBUG);
297                 $email_parts = explode('@',$data['new']['email']);
cc7a82 298                 $webdomain = $app->db->queryOneRecord("SELECT domain_id, server_id, system_user, parent_domain_id FROM web_domain WHERE domain = ?", $email_parts[1]);
98273d 299                 if ($webdomain) {
DM 300                     while ($webdomain['parent_domain_id'] != 0) {
cc7a82 301                         $webdomain = $app->db->queryOneRecord("SELECT domain_id, server_id, system_user, parent_domain_id FROM web_domain WHERE domain_id = ?", $webdomain['parent_domain_id']);
98273d 302                     }
DM 303                     $app->log($data['new']['server_id'].' == '.$webdomain['server_id'],LOGLEVEL_DEBUG);
304
305                     // only if web and mailserver are identical
306                     if ($data['new']['server_id'] == $webdomain['server_id']) {
307                         $data['new']['uid'] = $app->system->getuid($webdomain['system_user']);
308                     }
309                 }
310             }
311         }
312         // if nothing set before -> use standard mailuser uid and gid vmail
313         if ($data['new']['uid'] == -1) $data['new']['uid'] = $mail_config["mailuser_uid"];
314         if ($data['new']['gid'] == -1) $data['new']['gid'] = $mail_config["mailuser_gid"];
315         $app->log('Mailuser uid: '.$data['new']['uid'].', gid: '.$data['new']['gid'],LOGLEVEL_DEBUG);
316
317         // update DB if values changed
cc7a82 318         $app->db->query("UPDATE mail_user SET uid = ?, gid = ? WHERE mailuser_id = ?", $data['new']['uid'], $data['new']['gid'], $data['new']['mailuser_id']);
98273d 319
0e2978 320         $user = $app->system->getuser($data['new']['uid']);
DM 321         $group = $app->system->getgroup($data['new']['gid']);
322
f42df0 323         //* Create the mail domain directory, if it does not exist
T 324         if(!empty($base_path) && !is_dir($base_path)) {
355efb 325             //exec("su -c 'mkdir -p ".escapeshellcmd($base_path)."' ".$mail_config['mailuser_name']);
0e2978 326             $app->system->mkdirpath($base_path, 0770, $mail_config['mailuser_name'], $mail_config['mailuser_group']); // needs group-access because users of subfolders may differ from vmail
b1a6a5 327             $app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
f42df0 328         }
b1a6a5 329
f339eb 330         if ($data['new']['maildir_format'] == 'mdbox') {
D 331             // Move mailbox, if domain has changed and delete old mailbox
332             if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
333                 if(is_dir($data['new']['maildir'])) {
334                     exec("rm -fr ".escapeshellcmd($data['new']['maildir']));
335                     //rmdir($data['new']['maildir']);
336                 }
337                 exec('mv -f '.escapeshellcmd($data['old']['maildir']).' '.escapeshellcmd($data['new']['maildir']));
338                 // exec('mv -f '.escapeshellcmd($data['old']['maildir']).'/* '.escapeshellcmd($data['new']['maildir']));
339                 // if(is_file($data['old']['maildir'].'.ispconfig_mailsize'))exec('mv -f '.escapeshellcmd($data['old']['maildir']).'.ispconfig_mailsize '.escapeshellcmd($data['new']['maildir']));
340                 // rmdir($data['old']['maildir']);
341                 $app->log('Moved Maildir from: '.$data['old']['maildir'].' to '.$data['new']['maildir'], LOGLEVEL_DEBUG);
342             }
343                 
344             //* Create the maildir, if it doesn not exist, set permissions, set quota.
345             if(!is_dir($data['new']['maildir'].'/mdbox')) {
346                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" INBOX'");
347                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Sent'");
348                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Trash'");
349                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Junk'");
350                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Drafts'");
351                     
352                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" INBOX'");
353                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Sent'");
354                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Trash'");
355                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Junk'");
356                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Drafts'");
357             }
6cc49f 358         }
f339eb 359         else {
D 360             // Dovecot uses a different mail layout with a separate 'Maildir' subdirectory.
361             if($mail_config['pop3_imap_daemon'] == 'dovecot') {
362                 $app->system->mkdirpath($maildomain_path, 0700, $user, $group);
363                 $app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
364                 $maildomain_path .= '/Maildir';
365             }
366     
367             //* When the mail user dir exists but it is not a valid maildir, move it to corrupted maildir folder
368             if(!empty($maildomain_path) && is_dir($maildomain_path) && !is_dir($maildomain_path.'/new') && !is_dir($maildomain_path.'/cur')) {
369                 if(!is_dir($mail_config['homedir_path'].'/corrupted/'.$data['new']['mailuser_id'])) $app->system->mkdirpath($mail_config['homedir_path'].'/corrupted/'.$data['new']['mailuser_id'], 0700, $mail_config['mailuser_name'], $mail_config['mailuser_group']);
370                 exec("su -c 'mv -f ".escapeshellcmd($data['new']['maildir'])." ".$mail_config['homedir_path'].'/corrupted/'.$data['new']['mailuser_id']."' vmail");
371                 $app->log('Moved invalid maildir to corrupted Maildirs folder: '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_WARN);
372             }
373     
374             //* Create the maildir, if it doesn not exist, set permissions, set quota.
375             if(!empty($maildomain_path) && !is_dir($maildomain_path.'/new')) {
376                 //exec("su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
377                 //$app->log("Created Maildir "."su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
378                 $app->system->maildirmake($maildomain_path, $user, '', $group);
379     
380                 //* This is to fix the maildrop quota not being rebuilt after the quota is changed.
381                 if($mail_config['pop3_imap_daemon'] != 'dovecot') {
382                     if($data['new']['quota'] > 0) {
383                         if(is_dir($maildomain_path)) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user); // Avoid maildirmake quota bug, see debian bug #214911
384                         $app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user, LOGLEVEL_DEBUG);
385                     } else {
386                         if(file_exists($data['new']['maildir'].'/maildirsize')) unlink($data['new']['maildir'].'/maildirsize');
387                         $app->log('Set Maildir quota to unlimited.', LOGLEVEL_DEBUG);
388                     }
389                 }
390             }
391     
392             if(!is_dir($data['new']['maildir'].'/.Sent')) {
393                 //exec("su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
394                 //$app->log('Created submaildir Sent: '."su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
395                 $app->system->maildirmake($maildomain_path, $user, 'Sent', $group);
396             }
397             if(!is_dir($data['new']['maildir'].'/.Drafts')) {
398                 //exec("su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
399                 //$app->log('Created submaildir Drafts: '."su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
400                 $app->system->maildirmake($maildomain_path, $user, 'Drafts', $group);
401             }
402             if(!is_dir($data['new']['maildir'].'/.Trash')) {
403                 //exec("su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
404                 //$app->log('Created submaildir Trash: '."su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
405                 $app->system->maildirmake($maildomain_path, $user, 'Trash', $group);
406             }
407             if(!is_dir($data['new']['maildir'].'/.Junk')) {
408                 //exec("su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
409                 //$app->log('Created submaildir Junk: '."su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
410                 $app->system->maildirmake($maildomain_path, $user, 'Junk', $group);
411             }
412     
413             // Set permissions now recursive
414             exec('chown -R '.$user.':'.$group.' '.escapeshellcmd($data['new']['maildir']));
415             $app->log('Set ownership on '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_DEBUG);
416     
417             // Move mailbox, if domain has changed and delete old mailbox
418             if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
419                 if(is_dir($data['new']['maildir'])) {
420                     exec("rm -fr ".escapeshellcmd($data['new']['maildir']));
421                     //rmdir($data['new']['maildir']);
422                 }
423                 exec('mv -f '.escapeshellcmd($data['old']['maildir']).' '.escapeshellcmd($data['new']['maildir']));
424                 // exec('mv -f '.escapeshellcmd($data['old']['maildir']).'/* '.escapeshellcmd($data['new']['maildir']));
425                 // if(is_file($data['old']['maildir'].'.ispconfig_mailsize'))exec('mv -f '.escapeshellcmd($data['old']['maildir']).'.ispconfig_mailsize '.escapeshellcmd($data['new']['maildir']));
426                 // rmdir($data['old']['maildir']);
427                 $app->log('Moved Maildir from: '.$data['old']['maildir'].' to '.$data['new']['maildir'], LOGLEVEL_DEBUG);
428             }
429             //This is to fix the maildrop quota not being rebuilt after the quota is changed.
430             // Courier Layout
431             if(is_dir($data['new']['maildir'].'/new') && $mail_config['pop3_imap_daemon'] != 'dovecot') {
1ca823 432                 if($data['new']['quota'] > 0) {
f339eb 433                     if(is_dir($data['new']['maildir'])) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user);
D 434                     $app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user, LOGLEVEL_DEBUG);
1ca823 435                 } else {
T 436                     if(file_exists($data['new']['maildir'].'/maildirsize')) unlink($data['new']['maildir'].'/maildirsize');
b1a6a5 437                     $app->log('Set Maildir quota to unlimited.', LOGLEVEL_DEBUG);
1ca823 438                 }
T 439             }
6cc49f 440         }
b5a23a 441     }
b1a6a5 442
MC 443     function user_delete($event_name, $data) {
b5a23a 444         global $app, $conf;
b1a6a5 445
26c0fc 446         // get the config
T 447         $app->uses("getconf");
448         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
b1a6a5 449
2604cb 450         $maildir_path_deleted = false;
b5a23a 451         $old_maildir_path = escapeshellcmd($data['old']['maildir']);
b1a6a5 452         if($old_maildir_path != $mail_config['homedir_path'] && strlen($old_maildir_path) > strlen($mail_config['homedir_path']) && !stristr($old_maildir_path, '//') && !stristr($old_maildir_path, '..') && !stristr($old_maildir_path, '*') && strlen($old_maildir_path) >= 10) {
958705 453             exec('rm -rf '.escapeshellcmd($old_maildir_path));
b1a6a5 454             $app->log('Deleted the Maildir: '.$data['old']['maildir'], LOGLEVEL_DEBUG);
2604cb 455             $maildir_path_deleted = true;
b5a23a 456         } else {
b1a6a5 457             $app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'], LOGLEVEL_ERROR);
2604cb 458         }
a81238 459
2604cb 460         //* Delete the mail-backups
FS 461         $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
462         $backup_dir = $server_config['backup_dir'];
463         $mount_backup = true;
a81238 464         if($server_config['backup_dir'] != '' && $maildir_path_deleted && $server_config['backup_delete'] == 'y') {
990ca8 465             //* mount backup directory, if necessary
FS 466             if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
2604cb 467             if($mount_backup){
55c012 468                 $sql = "SELECT * FROM mail_domain WHERE domain = ?";
FS 469                 $domain_rec = $app->db->queryOneRecord($sql, explode("@",$data['old']['email'])[1]);
470                 if (is_array($domain_rec)) {
471                     $mail_backup_dir = $backup_dir.'/mail'.$domain_rec['domain_id'];
472                     $mail_backup_files = 'mail'.$data['old']['mailuser_id'];
473                     exec(escapeshellcmd('rm -f '.$mail_backup_dir.'/'.$mail_backup_files).'*');
474                     //* cleanup database
475                     $sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND mailuser_id = ?";
476                     $app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
477                     if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
2604cb 478
55c012 479                     $app->log('Deleted the mail backups for: '.$data['old']['email'], LOGLEVEL_DEBUG);
FS 480                 }
2604cb 481             }
b5a23a 482         }
T 483     }
b1a6a5 484
MC 485     function domain_delete($event_name, $data) {
0c4be7 486         global $app, $conf;
b1a6a5 487
0c4be7 488         $app->uses("getconf");
663caf 489         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
b1a6a5 490
2604cb 491         $maildomain_path_deleted = false;
06303b 492         //* Delete maildomain path
0c4be7 493         $old_maildomain_path = escapeshellcmd($mail_config['homedir_path'].'/'.$data['old']['domain']);
b1a6a5 494         if($old_maildomain_path != $mail_config['homedir_path'] && !stristr($old_maildomain_path, '//') && !stristr($old_maildomain_path, '..') && !stristr($old_maildomain_path, '*') && !stristr($old_maildomain_path, '&') && strlen($old_maildomain_path) >= 10  && !empty($data['old']['domain'])) {
0c4be7 495             exec('rm -rf '.escapeshellcmd($old_maildomain_path));
b1a6a5 496             $app->log('Deleted the mail domain directory: '.$old_maildomain_path, LOGLEVEL_DEBUG);
2604cb 497             $maildomain_path_deleted = true;
0c4be7 498         } else {
b1a6a5 499             $app->log('Possible security violation when deleting the mail domain directory: '.$old_maildomain_path, LOGLEVEL_ERROR);
0c4be7 500         }
b1a6a5 501
06303b 502         //* Delete mailfilter path
T 503         $old_maildomain_path = escapeshellcmd($mail_config['homedir_path'].'/mailfilters/'.$data['old']['domain']);
b1a6a5 504         if($old_maildomain_path != $mail_config['homedir_path'].'/mailfilters/' && !stristr($old_maildomain_path, '//') && !stristr($old_maildomain_path, '..') && !stristr($old_maildomain_path, '*') && !stristr($old_maildomain_path, '&') && strlen($old_maildomain_path) >= 10 && !empty($data['old']['domain'])) {
06303b 505             exec('rm -rf '.escapeshellcmd($old_maildomain_path));
b1a6a5 506             $app->log('Deleted the mail domain mailfilter directory: '.$old_maildomain_path, LOGLEVEL_DEBUG);
06303b 507         } else {
b1a6a5 508             $app->log('Possible security violation when deleting the mail domain mailfilter directory: '.$old_maildomain_path, LOGLEVEL_ERROR);
06303b 509         }
2604cb 510         
FS 511         //* Delete the mail-backups
512         $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
513         $backup_dir = $server_config['backup_dir'];
514         $mount_backup = true;
a81238 515         if($server_config['backup_dir'] != '' && $maildomain_path_deleted && $server_config['backup_delete'] == 'y'){
990ca8 516             //* mount backup directory, if necessary
FS 517             if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
2604cb 518             if($mount_backup){
FS 519                 $mail_backup_dir = $backup_dir.'/mail'.$data['old']['domain_id'];
520                 exec(escapeshellcmd('rm -rf '.$mail_backup_dir));
521                 //* cleanup database
55c012 522                 $sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ?";
FS 523                 $app->db->query($sql, $conf['server_id'], $data['old']['domain_id']);
524                 if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id']);
2604cb 525
FS 526                 $app->log('Deleted the mail backup directory: '.$mail_backup_dir, LOGLEVEL_DEBUG);
527             }
528         }
529
0c4be7 530     }
b1a6a5 531
MC 532     function transport_update($event_name, $data) {
9234cc 533         global $app, $conf;
b1a6a5 534
663caf 535         exec($conf['init_scripts'] . '/' . 'postfix reload &> /dev/null');
b1a6a5 536         $app->log('Postfix config reloaded ', LOGLEVEL_DEBUG);
MC 537
9234cc 538     }
b1a6a5 539
MC 540
541
b5a23a 542
T 543 } // end class
544
663caf 545 ?>