Marius Cramer
2014-02-17 ebbe6374fc9c308daf729d2ad1b2f8007ed771ce
commit | author | age
e32699 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 maildrop_plugin {
7fe908 32
e32699 33     var $plugin_name = 'maildrop_plugin';
T 34     var $class_name = 'maildrop_plugin';
7fe908 35
MC 36
e32699 37     var $mailfilter_config_dir = '';
7fe908 38
392450 39     //* This function is called during ispconfig installation to determine
T 40     //  if a symlink shall be created for this plugin.
41     function onInstall() {
42         global $conf;
7fe908 43
6cc49f 44         if($conf['services']['mail'] == true && isset($conf['courier']['installed']) && $conf['courier']['installed'] == true) {
392450 45             return true;
T 46         } else {
47             return false;
48         }
7fe908 49
392450 50     }
7fe908 51
e32699 52     /*
T 53          This function is called when the plugin is loaded
54     */
7fe908 55
e32699 56     function onLoad() {
T 57         global $app;
7fe908 58
e32699 59         /*
T 60         Register for the events
61         */
7fe908 62
MC 63         $app->plugins->registerEvent('mail_user_insert', 'maildrop_plugin', 'update');
64         $app->plugins->registerEvent('mail_user_update', 'maildrop_plugin', 'update');
65         $app->plugins->registerEvent('mail_user_delete', 'maildrop_plugin', 'delete');
66
e32699 67     }
7fe908 68
MC 69
70     function update($event_name, $data) {
e32699 71         global $app, $conf;
7fe908 72
e32699 73         // load the server configuration options
T 74         $app->uses("getconf");
75         $mail_config = $app->getconf->get_server_config($conf["server_id"], 'mail');
7fe908 76         if(substr($mail_config["homedir_path"], -1) == '/') {
MC 77             $mail_config["homedir_path"] = substr($mail_config["homedir_path"], 0, -1);
e32699 78         }
T 79         $this->mailfilter_config_dir = $mail_config["homedir_path"].'/mailfilters';
7fe908 80
MC 81
e32699 82         // Check if the config directory exists.
T 83         if(!is_dir($this->mailfilter_config_dir)) {
7fe908 84             $app->log("Mailfilter config directory '".$this->mailfilter_config_dir."' does not exist. Creating it now.", LOGLEVEL_WARN);
04c7eb 85             mkdir($this->mailfilter_config_dir);
8e725d 86             chown($this->mailfilter_config_dir, 'vmail');
J 87             chmod($this->mailfilter_config_dir, 0770);
04c7eb 88         }
7fe908 89
04c7eb 90         if(isset($data["new"]["email"])) {
7fe908 91             $email_parts = explode("@", $data["new"]["email"]);
e32699 92         } else {
7fe908 93             $email_parts = explode("@", $data["old"]["email"]);
04c7eb 94         }
7fe908 95
04c7eb 96         // make sure that the config directories exist
T 97         if(!is_dir($this->mailfilter_config_dir.'/'.$email_parts[1])) {
98             mkdir($this->mailfilter_config_dir.'/'.$email_parts[1]);
8e725d 99             chown($this->mailfilter_config_dir.'/'.$email_parts[1], 'vmail');
J 100             chmod($this->mailfilter_config_dir.'/'.$email_parts[1], 0770);
04c7eb 101         }
T 102         if(!is_dir($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0])) {
103             mkdir($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0]);
8e725d 104             chown($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0], 'vmail');
J 105             chmod($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0], 0770);
04c7eb 106         }
7fe908 107
04c7eb 108         // Check if something has been changed regarding the autoresponders
7fe908 109         if($data["old"]["autoresponder_text"] != $data["new"]["autoresponder_text"]
MC 110             or $data["old"]["autoresponder"] != $data["new"]["autoresponder"]
111             or (isset($data["new"]["email"]) and $data["old"]["email"] != $data["new"]["email"])
112             or $data["old"]["autoresponder_start_date"] != $data["new"]["autoresponder_start_date"]
113             or $data["old"]["autoresponder_end_date"] != $data["new"]["autoresponder_end_date"]) {
114
04c7eb 115             // We delete the old autoresponder, if it exists
7fe908 116             $email_parts = explode("@", $data["old"]["email"]);
04c7eb 117             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lock';
7fe908 118             if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
04c7eb 119             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst';
7fe908 120             if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
2ebca9 121             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.gdbm';
7fe908 122             if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
2ebca9 123             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.lock';
7fe908 124             if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
04c7eb 125             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.msg';
7fe908 126             if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
04c7eb 127             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.autoresponder';
7fe908 128             if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
MC 129
130
04c7eb 131             //Now we create the new autoresponder, if it is enabled
T 132             if($data["new"]["autoresponder"] == 'y') {
133                 if(isset($data["new"]["email"])) {
7fe908 134                     $email_parts = explode("@", $data["new"]["email"]);
04c7eb 135                 } else {
7fe908 136                     $email_parts = explode("@", $data["old"]["email"]);
04c7eb 137                 }
7fe908 138
04c7eb 139                 // Load the master template
T 140                 $tpl = file_get_contents($conf["rootpath"].'/conf/autoresponder.master');
7fe908 141                 $tpl = str_replace('{vmail_mailbox_base}', $mail_config["homedir_path"], $tpl);
MC 142
26c528 143                 if ($data["new"]["autoresponder_start_date"] != '0000-00-00 00:00:00') { // Dates have been set
7fe908 144                     $tpl = str_replace('{start_date}', strtotime($data["new"]["autoresponder_start_date"]), $tpl);
MC 145                     $tpl = str_replace('{end_date}', strtotime($data["new"]["autoresponder_end_date"]), $tpl);
26c528 146                 } else {
7fe908 147                     $tpl = str_replace('{start_date}', -7200, $tpl);
MC 148                     $tpl = str_replace('{end_date}', 2147464800, $tpl);
26c528 149                 }
7fe908 150
04c7eb 151                 // Write the config file.
T 152                 $config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.autoresponder';
7fe908 153                 file_put_contents($config_file_path, $tpl);
MC 154                 $app->log("Writing Autoresponder mailfilter file: $config_file_path", LOGLEVEL_DEBUG);
8e725d 155                 chmod($config_file_path, 0770);
J 156                 chown($config_file_path, 'vmail');
04c7eb 157                 unset($tpl);
T 158                 unset($config_file_path);
7fe908 159
04c7eb 160                 // Write the autoresponder message file
T 161                 $config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.msg';
7fe908 162                 file_put_contents($config_file_path, $data["new"]["autoresponder_text"]);
8e725d 163                 chmod($config_file_path, 0770);
J 164                 chown($config_file_path, 'vmail');
7fe908 165                 $app->log("Writing Autoresponder message file: $config_file_path", LOGLEVEL_DEBUG);
e32699 166             }
04c7eb 167         }
7fe908 168
eeb56f 169         // Write the custom mailfilter script, if mailfilter recipe has changed
T 170         if($data["old"]["custom_mailfilter"] != $data["new"]["custom_mailfilter"]
171             or $data["old"]["move_junk"] != $data["new"]["move_junk"]
172             or $data["old"]["cc"] != $data["new"]["cc"]) {
7fe908 173
MC 174             $app->log("Mailfilter config has been changed", LOGLEVEL_DEBUG);
175             if(trim($data["new"]["custom_mailfilter"]) != ''
176                 or $data["new"]["move_junk"] != 'n'
177                 or $data["new"]["cc"] != '') {
178
179                 // Delete the old filter recipe
180                 $email_parts = explode("@", $data["old"]["email"]);
181                 $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
182                 if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
183
184                 // write the new recipe
185                 if(isset($data["new"]["email"])) {
186                     $email_parts = explode("@", $data["new"]["email"]);
e32699 187                 } else {
7fe908 188                     $email_parts = explode("@", $data["old"]["email"]);
MC 189                 }
190                 $config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
191
192                 $mailfilter_content = '';
193
194                 if($data["new"]["cc"] != '') {
f798aa 195                     $tmp_mails_arr = explode(',',$data["new"]["cc"]);
FT 196                     foreach($tmp_mails_arr as $address) {
197                         if(trim($address) != '') $mailfilter_content .= "cc \"!".trim($address)."\"\n";
198                     }
199                     //$mailfilter_content .= "cc \"!".$data["new"]["cc"]."\"\n";
7fe908 200                     $app->log("Added CC address ".$data["new"]["cc"].' to mailfilter file.', LOGLEVEL_DEBUG);
MC 201                 }
202
203                 if($data["new"]["move_junk"] == 'y') {
204                     $mailfilter_content .= file_get_contents($conf["rootpath"].'/conf/mailfilter_move_junk.master')."\n";
205                 }
206                 $mailfilter_content .= $data["new"]["custom_mailfilter"];
207
208                 // Replace windows linebreaks in mailfilter file
209                 $mailfilter_content = str_replace("\r\n", "\n", $mailfilter_content);
210
211                 file_put_contents($config_file_path, $mailfilter_content);
212                 $app->log("Writing new custom Mailfiter".$config_file_path, LOGLEVEL_DEBUG);
213                 chmod($config_file_path, 0770);
214                 chown($config_file_path, 'vmail');
215                 unset($config_file_path);
216             } else {
217                 // Delete the mailfilter recipe
218                 if(isset($data["old"]["email"])) {
219                     $email_parts = explode("@", $data["old"]["email"]);
220                     $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
221                     if(is_file($file)) {
222                         unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
223                         $app->log("Deleting custom Mailfiter".$file, LOGLEVEL_DEBUG);
c719dc 224                     }
e32699 225                 }
7fe908 226             }
e32699 227         }
T 228     }
7fe908 229
MC 230     function delete($event_name, $data) {
e32699 231         global $app, $conf;
7fe908 232
e32699 233         // load the server configuration options
T 234         $app->uses("getconf");
235         $mail_config = $app->getconf->get_server_config($conf["server_id"], 'mail');
236         $this->mailfilter_config_dir = $mail_config["homedir_path"].'/mailfilters';
7fe908 237
MC 238         $email_parts = explode("@", $data["old"]["email"]);
e32699 239         $dir = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0];
T 240         if(is_dir($dir)) {
241             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lock';
7fe908 242             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 243             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst';
7fe908 244             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 245             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.msg';
7fe908 246             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 247             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.autoresponder';
7fe908 248             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 249             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
7fe908 250             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
7bd0de 251             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.gdbm';
7fe908 252             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
7bd0de 253             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.lock';
7fe908 254             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
MC 255             rmdir($dir) or $app->log("Unable to delete directory: $dir", LOGLEVEL_WARN);
e32699 256         }
T 257     }
7fe908 258
e32699 259
T 260 } // end class
261
8e725d 262 ?>