Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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
T 209         if(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$conf['language'].'.txt')) {
355efb 210             $lines = file($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$conf['language'].'.txt');
92e509 211         } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_en.txt')) {
355efb 212             $lines = file($conf['rootpath'].'/conf-custom/mail/welcome_email_en.txt');
92e509 213         } elseif(file_exists($conf['rootpath'].'/conf/mail/welcome_email_'.$conf['language'].'.txt')) {
355efb 214             $lines = file($conf['rootpath'].'/conf/mail/welcome_email_'.$conf['language'].'.txt');
92e509 215         } else {
355efb 216             $lines = file($conf['rootpath'].'/conf/mail/welcome_email_en.txt');
92e509 217         }
b1a6a5 218
355efb 219         //* Get from address
b1a6a5 220         $parts = explode(':', trim($lines[0]));
355efb 221         unset($parts[0]);
b1a6a5 222         $welcome_mail_from  = implode(':', $parts);
355efb 223         unset($lines[0]);
b1a6a5 224
355efb 225         //* Get subject
b1a6a5 226         $parts = explode(':', trim($lines[1]));
355efb 227         unset($parts[0]);
b1a6a5 228         $welcome_mail_subject  = implode(':', $parts);
355efb 229         unset($lines[1]);
b1a6a5 230
355efb 231         //* Get message
T 232         $welcome_mail_message = trim(implode($lines));
233         unset($tmp);
b1a6a5 234
92e509 235         $mailHeaders      = "MIME-Version: 1.0" . "\n";
T 236         $mailHeaders     .= "Content-type: text/plain; charset=utf-8" . "\n";
237         $mailHeaders     .= "Content-Transfer-Encoding: 8bit" . "\n";
6d49fc 238         $mailHeaders     .= "From: $welcome_mail_from" . "\n";
T 239         $mailHeaders     .= "Reply-To: $welcome_mail_from" . "\n";
92e509 240         $mailTarget       = $data["new"]["email"];
f682c3 241         $mailSubject      = "=?utf-8?B?".base64_encode($welcome_mail_subject)."?=";
92e509 242
992797 243         //* Send the welcome email only on the "master" mail server to avoid duplicate emails
MC 244         if($conf['mirror_server_id'] == 0) mail($mailTarget, $mailSubject, $welcome_mail_message, $mailHeaders);
b1a6a5 245
b5a23a 246     }
b1a6a5 247
MC 248     function user_update($event_name, $data) {
b5a23a 249         global $app, $conf;
b1a6a5 250
b5a23a 251         // get the config
663caf 252         $app->uses('getconf,system');
J 253         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
06303b 254
T 255         // convert to lower case - it could cause problems if some directory above has upper case name
256         // $data['new']['maildir'] = strtolower($data['new']['maildir']);
b1a6a5 257
b5a23a 258         // Create the maildir, if it does not exist
f42df0 259         /*
be40ba 260         if(!is_dir($data['new']['maildir'])) {
663caf 261             mkdir(escapeshellcmd($data['new']['maildir']), 0, true);
J 262             chown(escapeshellcmd($data['new']['maildir']), $mail_config['mailuser_name']);
263             chgrp(escapeshellcmd($data['new']['maildir']), $mail_config['mailuser_group']);
b5a23a 264             $app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
f42df0 265         }
T 266         */
b1a6a5 267
f339eb 268         // Maildir-Format must not be changed on this way !!
D 269         $data['new']['maildir_format'] = $data['old']['maildir_format'];
270         
f42df0 271         $maildomain_path = $data['new']['maildir'];
T 272         $tmp_basepath = $data['new']['maildir'];
b1a6a5 273         $tmp_basepath_parts = explode('/', $tmp_basepath);
f42df0 274         unset($tmp_basepath_parts[count($tmp_basepath_parts)-1]);
b1a6a5 275         $base_path = implode('/', $tmp_basepath_parts);
f42df0 276
98273d 277         //* Set the email-uid and gid if not given -> in case of changed settings again setting here
DM 278         if (($data['new']['uid'] == -1) || ($data['new']['gid'] == -1)) {
279             $app->log('Setting uid and gid automatically',LOGLEVEL_DEBUG);
280             if ($mail_config["mailbox_virtual_uidgid_maps"] == 'y') {
281                 $app->log('Map uid to linux-user',LOGLEVEL_DEBUG);
282                 $email_parts = explode('@',$data['new']['email']);
cc7a82 283                 $webdomain = $app->db->queryOneRecord("SELECT domain_id, server_id, system_user, parent_domain_id FROM web_domain WHERE domain = ?", $email_parts[1]);
98273d 284                 if ($webdomain) {
DM 285                     while ($webdomain['parent_domain_id'] != 0) {
cc7a82 286                         $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 287                     }
DM 288                     $app->log($data['new']['server_id'].' == '.$webdomain['server_id'],LOGLEVEL_DEBUG);
289
290                     // only if web and mailserver are identical
291                     if ($data['new']['server_id'] == $webdomain['server_id']) {
292                         $data['new']['uid'] = $app->system->getuid($webdomain['system_user']);
293                     }
294                 }
295             }
296         }
297         // if nothing set before -> use standard mailuser uid and gid vmail
298         if ($data['new']['uid'] == -1) $data['new']['uid'] = $mail_config["mailuser_uid"];
299         if ($data['new']['gid'] == -1) $data['new']['gid'] = $mail_config["mailuser_gid"];
300         $app->log('Mailuser uid: '.$data['new']['uid'].', gid: '.$data['new']['gid'],LOGLEVEL_DEBUG);
301
302         // update DB if values changed
cc7a82 303         $app->db->query("UPDATE mail_user SET uid = ?, gid = ? WHERE mailuser_id = ?", $data['new']['uid'], $data['new']['gid'], $data['new']['mailuser_id']);
98273d 304
0e2978 305         $user = $app->system->getuser($data['new']['uid']);
DM 306         $group = $app->system->getgroup($data['new']['gid']);
307
f42df0 308         //* Create the mail domain directory, if it does not exist
T 309         if(!empty($base_path) && !is_dir($base_path)) {
355efb 310             //exec("su -c 'mkdir -p ".escapeshellcmd($base_path)."' ".$mail_config['mailuser_name']);
0e2978 311             $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 312             $app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
f42df0 313         }
b1a6a5 314
f339eb 315         if ($data['new']['maildir_format'] == 'mdbox') {
D 316             // Move mailbox, if domain has changed and delete old mailbox
317             if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
318                 if(is_dir($data['new']['maildir'])) {
319                     exec("rm -fr ".escapeshellcmd($data['new']['maildir']));
320                     //rmdir($data['new']['maildir']);
321                 }
322                 exec('mv -f '.escapeshellcmd($data['old']['maildir']).' '.escapeshellcmd($data['new']['maildir']));
323                 // exec('mv -f '.escapeshellcmd($data['old']['maildir']).'/* '.escapeshellcmd($data['new']['maildir']));
324                 // if(is_file($data['old']['maildir'].'.ispconfig_mailsize'))exec('mv -f '.escapeshellcmd($data['old']['maildir']).'.ispconfig_mailsize '.escapeshellcmd($data['new']['maildir']));
325                 // rmdir($data['old']['maildir']);
326                 $app->log('Moved Maildir from: '.$data['old']['maildir'].' to '.$data['new']['maildir'], LOGLEVEL_DEBUG);
327             }
328                 
329             //* Create the maildir, if it doesn not exist, set permissions, set quota.
330             if(!is_dir($data['new']['maildir'].'/mdbox')) {
331                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" INBOX'");
332                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Sent'");
333                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Trash'");
334                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Junk'");
335                 exec("su -c 'doveadm mailbox create -u \"".$data["new"]["email"]."\" Drafts'");
336                     
337                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" INBOX'");
338                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Sent'");
339                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Trash'");
340                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Junk'");
341                 exec("su -c 'doveadm mailbox subscribe -u \"".$data["new"]["email"]."\" Drafts'");
342             }
6cc49f 343         }
f339eb 344         else {
D 345             // Dovecot uses a different mail layout with a separate 'Maildir' subdirectory.
346             if($mail_config['pop3_imap_daemon'] == 'dovecot') {
347                 $app->system->mkdirpath($maildomain_path, 0700, $user, $group);
348                 $app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
349                 $maildomain_path .= '/Maildir';
350             }
351     
352             //* When the mail user dir exists but it is not a valid maildir, move it to corrupted maildir folder
353             if(!empty($maildomain_path) && is_dir($maildomain_path) && !is_dir($maildomain_path.'/new') && !is_dir($maildomain_path.'/cur')) {
354                 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']);
355                 exec("su -c 'mv -f ".escapeshellcmd($data['new']['maildir'])." ".$mail_config['homedir_path'].'/corrupted/'.$data['new']['mailuser_id']."' vmail");
356                 $app->log('Moved invalid maildir to corrupted Maildirs folder: '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_WARN);
357             }
358     
359             //* Create the maildir, if it doesn not exist, set permissions, set quota.
360             if(!empty($maildomain_path) && !is_dir($maildomain_path.'/new')) {
361                 //exec("su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
362                 //$app->log("Created Maildir "."su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
363                 $app->system->maildirmake($maildomain_path, $user, '', $group);
364     
365                 //* This is to fix the maildrop quota not being rebuilt after the quota is changed.
366                 if($mail_config['pop3_imap_daemon'] != 'dovecot') {
367                     if($data['new']['quota'] > 0) {
368                         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
369                         $app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user, LOGLEVEL_DEBUG);
370                     } else {
371                         if(file_exists($data['new']['maildir'].'/maildirsize')) unlink($data['new']['maildir'].'/maildirsize');
372                         $app->log('Set Maildir quota to unlimited.', LOGLEVEL_DEBUG);
373                     }
374                 }
375             }
376     
377             if(!is_dir($data['new']['maildir'].'/.Sent')) {
378                 //exec("su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
379                 //$app->log('Created submaildir Sent: '."su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
380                 $app->system->maildirmake($maildomain_path, $user, 'Sent', $group);
381             }
382             if(!is_dir($data['new']['maildir'].'/.Drafts')) {
383                 //exec("su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
384                 //$app->log('Created submaildir Drafts: '."su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
385                 $app->system->maildirmake($maildomain_path, $user, 'Drafts', $group);
386             }
387             if(!is_dir($data['new']['maildir'].'/.Trash')) {
388                 //exec("su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
389                 //$app->log('Created submaildir Trash: '."su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
390                 $app->system->maildirmake($maildomain_path, $user, 'Trash', $group);
391             }
392             if(!is_dir($data['new']['maildir'].'/.Junk')) {
393                 //exec("su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
394                 //$app->log('Created submaildir Junk: '."su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
395                 $app->system->maildirmake($maildomain_path, $user, 'Junk', $group);
396             }
397     
398             // Set permissions now recursive
399             exec('chown -R '.$user.':'.$group.' '.escapeshellcmd($data['new']['maildir']));
400             $app->log('Set ownership on '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_DEBUG);
401     
402             // Move mailbox, if domain has changed and delete old mailbox
403             if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
404                 if(is_dir($data['new']['maildir'])) {
405                     exec("rm -fr ".escapeshellcmd($data['new']['maildir']));
406                     //rmdir($data['new']['maildir']);
407                 }
408                 exec('mv -f '.escapeshellcmd($data['old']['maildir']).' '.escapeshellcmd($data['new']['maildir']));
409                 // exec('mv -f '.escapeshellcmd($data['old']['maildir']).'/* '.escapeshellcmd($data['new']['maildir']));
410                 // if(is_file($data['old']['maildir'].'.ispconfig_mailsize'))exec('mv -f '.escapeshellcmd($data['old']['maildir']).'.ispconfig_mailsize '.escapeshellcmd($data['new']['maildir']));
411                 // rmdir($data['old']['maildir']);
412                 $app->log('Moved Maildir from: '.$data['old']['maildir'].' to '.$data['new']['maildir'], LOGLEVEL_DEBUG);
413             }
414             //This is to fix the maildrop quota not being rebuilt after the quota is changed.
415             // Courier Layout
416             if(is_dir($data['new']['maildir'].'/new') && $mail_config['pop3_imap_daemon'] != 'dovecot') {
1ca823 417                 if($data['new']['quota'] > 0) {
f339eb 418                     if(is_dir($data['new']['maildir'])) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user);
D 419                     $app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user, LOGLEVEL_DEBUG);
1ca823 420                 } else {
T 421                     if(file_exists($data['new']['maildir'].'/maildirsize')) unlink($data['new']['maildir'].'/maildirsize');
b1a6a5 422                     $app->log('Set Maildir quota to unlimited.', LOGLEVEL_DEBUG);
1ca823 423                 }
T 424             }
6cc49f 425         }
b5a23a 426     }
b1a6a5 427
MC 428     function user_delete($event_name, $data) {
b5a23a 429         global $app, $conf;
b1a6a5 430
26c0fc 431         // get the config
T 432         $app->uses("getconf");
433         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
b1a6a5 434
2604cb 435         $maildir_path_deleted = false;
b5a23a 436         $old_maildir_path = escapeshellcmd($data['old']['maildir']);
b1a6a5 437         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 438             exec('rm -rf '.escapeshellcmd($old_maildir_path));
b1a6a5 439             $app->log('Deleted the Maildir: '.$data['old']['maildir'], LOGLEVEL_DEBUG);
2604cb 440             $maildir_path_deleted = true;
b5a23a 441         } else {
b1a6a5 442             $app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'], LOGLEVEL_ERROR);
2604cb 443         }
a81238 444
2604cb 445         //* Delete the mail-backups
FS 446         $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
447         $backup_dir = $server_config['backup_dir'];
448         $mount_backup = true;
a81238 449         if($server_config['backup_dir'] != '' && $maildir_path_deleted && $server_config['backup_delete'] == 'y') {
990ca8 450             //* mount backup directory, if necessary
FS 451             if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
2604cb 452             if($mount_backup){
55c012 453                 $sql = "SELECT * FROM mail_domain WHERE domain = ?";
FS 454                 $domain_rec = $app->db->queryOneRecord($sql, explode("@",$data['old']['email'])[1]);
455                 if (is_array($domain_rec)) {
456                     $mail_backup_dir = $backup_dir.'/mail'.$domain_rec['domain_id'];
457                     $mail_backup_files = 'mail'.$data['old']['mailuser_id'];
458                     exec(escapeshellcmd('rm -f '.$mail_backup_dir.'/'.$mail_backup_files).'*');
459                     //* cleanup database
460                     $sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND mailuser_id = ?";
461                     $app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
462                     if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
2604cb 463
55c012 464                     $app->log('Deleted the mail backups for: '.$data['old']['email'], LOGLEVEL_DEBUG);
FS 465                 }
2604cb 466             }
b5a23a 467         }
T 468     }
b1a6a5 469
MC 470     function domain_delete($event_name, $data) {
0c4be7 471         global $app, $conf;
b1a6a5 472
0c4be7 473         $app->uses("getconf");
663caf 474         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
b1a6a5 475
2604cb 476         $maildomain_path_deleted = false;
06303b 477         //* Delete maildomain path
0c4be7 478         $old_maildomain_path = escapeshellcmd($mail_config['homedir_path'].'/'.$data['old']['domain']);
b1a6a5 479         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 480             exec('rm -rf '.escapeshellcmd($old_maildomain_path));
b1a6a5 481             $app->log('Deleted the mail domain directory: '.$old_maildomain_path, LOGLEVEL_DEBUG);
2604cb 482             $maildomain_path_deleted = true;
0c4be7 483         } else {
b1a6a5 484             $app->log('Possible security violation when deleting the mail domain directory: '.$old_maildomain_path, LOGLEVEL_ERROR);
0c4be7 485         }
b1a6a5 486
06303b 487         //* Delete mailfilter path
T 488         $old_maildomain_path = escapeshellcmd($mail_config['homedir_path'].'/mailfilters/'.$data['old']['domain']);
b1a6a5 489         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 490             exec('rm -rf '.escapeshellcmd($old_maildomain_path));
b1a6a5 491             $app->log('Deleted the mail domain mailfilter directory: '.$old_maildomain_path, LOGLEVEL_DEBUG);
06303b 492         } else {
b1a6a5 493             $app->log('Possible security violation when deleting the mail domain mailfilter directory: '.$old_maildomain_path, LOGLEVEL_ERROR);
06303b 494         }
2604cb 495         
FS 496         //* Delete the mail-backups
497         $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
498         $backup_dir = $server_config['backup_dir'];
499         $mount_backup = true;
a81238 500         if($server_config['backup_dir'] != '' && $maildomain_path_deleted && $server_config['backup_delete'] == 'y'){
990ca8 501             //* mount backup directory, if necessary
FS 502             if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
2604cb 503             if($mount_backup){
FS 504                 $mail_backup_dir = $backup_dir.'/mail'.$data['old']['domain_id'];
505                 exec(escapeshellcmd('rm -rf '.$mail_backup_dir));
506                 //* cleanup database
55c012 507                 $sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ?";
FS 508                 $app->db->query($sql, $conf['server_id'], $data['old']['domain_id']);
509                 if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id']);
2604cb 510
FS 511                 $app->log('Deleted the mail backup directory: '.$mail_backup_dir, LOGLEVEL_DEBUG);
512             }
513         }
514
0c4be7 515     }
b1a6a5 516
MC 517     function transport_update($event_name, $data) {
9234cc 518         global $app, $conf;
b1a6a5 519
663caf 520         exec($conf['init_scripts'] . '/' . 'postfix reload &> /dev/null');
b1a6a5 521         $app->log('Postfix config reloaded ', LOGLEVEL_DEBUG);
MC 522
9234cc 523     }
b1a6a5 524
MC 525
526
b5a23a 527
T 528 } // end class
529
663caf 530 ?>