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