Pascal Dreissen
2016-07-08 f1193b43f4c9fd132741d30f03f0b35841011989
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
5335a4 140                 if(file_exists($conf["rootpath"].'/conf-custom/autoresponder.master')) {
MC 141                     $tpl = file_get_contents($conf["rootpath"].'/conf-custom/autoresponder.master');
142                 } else {
143                     $tpl = file_get_contents($conf["rootpath"].'/conf/autoresponder.master');
144                 }
7fe908 145                 $tpl = str_replace('{vmail_mailbox_base}', $mail_config["homedir_path"], $tpl);
MC 146
651642 147                 if ($data['new']['autoresponder_start_date'] && $data["new"]["autoresponder_start_date"] != '0000-00-00 00:00:00') { // Dates have been set
7fe908 148                     $tpl = str_replace('{start_date}', strtotime($data["new"]["autoresponder_start_date"]), $tpl);
MC 149                     $tpl = str_replace('{end_date}', strtotime($data["new"]["autoresponder_end_date"]), $tpl);
26c528 150                 } else {
7fe908 151                     $tpl = str_replace('{start_date}', -7200, $tpl);
MC 152                     $tpl = str_replace('{end_date}', 2147464800, $tpl);
26c528 153                 }
7fe908 154
04c7eb 155                 // Write the config file.
T 156                 $config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.autoresponder';
7fe908 157                 file_put_contents($config_file_path, $tpl);
MC 158                 $app->log("Writing Autoresponder mailfilter file: $config_file_path", LOGLEVEL_DEBUG);
8e725d 159                 chmod($config_file_path, 0770);
J 160                 chown($config_file_path, 'vmail');
04c7eb 161                 unset($tpl);
T 162                 unset($config_file_path);
7fe908 163
04c7eb 164                 // Write the autoresponder message file
T 165                 $config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.msg';
7fe908 166                 file_put_contents($config_file_path, $data["new"]["autoresponder_text"]);
8e725d 167                 chmod($config_file_path, 0770);
J 168                 chown($config_file_path, 'vmail');
7fe908 169                 $app->log("Writing Autoresponder message file: $config_file_path", LOGLEVEL_DEBUG);
e32699 170             }
04c7eb 171         }
7fe908 172
eeb56f 173         // Write the custom mailfilter script, if mailfilter recipe has changed
T 174         if($data["old"]["custom_mailfilter"] != $data["new"]["custom_mailfilter"]
175             or $data["old"]["move_junk"] != $data["new"]["move_junk"]
176             or $data["old"]["cc"] != $data["new"]["cc"]) {
7fe908 177
MC 178             $app->log("Mailfilter config has been changed", LOGLEVEL_DEBUG);
179             if(trim($data["new"]["custom_mailfilter"]) != ''
180                 or $data["new"]["move_junk"] != 'n'
181                 or $data["new"]["cc"] != '') {
182
183                 // Delete the old filter recipe
184                 $email_parts = explode("@", $data["old"]["email"]);
185                 $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
186                 if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
187
188                 // write the new recipe
189                 if(isset($data["new"]["email"])) {
190                     $email_parts = explode("@", $data["new"]["email"]);
e32699 191                 } else {
7fe908 192                     $email_parts = explode("@", $data["old"]["email"]);
MC 193                 }
194                 $config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
195
196                 $mailfilter_content = '';
197
198                 if($data["new"]["cc"] != '') {
f798aa 199                     $tmp_mails_arr = explode(',',$data["new"]["cc"]);
FT 200                     foreach($tmp_mails_arr as $address) {
201                         if(trim($address) != '') $mailfilter_content .= "cc \"!".trim($address)."\"\n";
202                     }
203                     //$mailfilter_content .= "cc \"!".$data["new"]["cc"]."\"\n";
7fe908 204                     $app->log("Added CC address ".$data["new"]["cc"].' to mailfilter file.', LOGLEVEL_DEBUG);
MC 205                 }
206
207                 if($data["new"]["move_junk"] == 'y') {
5335a4 208                     if(file_exists($conf["rootpath"].'/conf-custom/mailfilter_move_junk.master')) {
MC 209                         $mailfilter_content .= file_get_contents($conf["rootpath"].'/conf-custom/mailfilter_move_junk.master')."\n";
210                     } else {
211                         $mailfilter_content .= file_get_contents($conf["rootpath"].'/conf/mailfilter_move_junk.master')."\n";
212                     }
7fe908 213                 }
56a9c7 214                 $mailfilter_content .= str_replace("\r\n","\n",$data["new"]["custom_mailfilter"]);
7fe908 215
MC 216                 // Replace windows linebreaks in mailfilter file
217                 $mailfilter_content = str_replace("\r\n", "\n", $mailfilter_content);
218
219                 file_put_contents($config_file_path, $mailfilter_content);
220                 $app->log("Writing new custom Mailfiter".$config_file_path, LOGLEVEL_DEBUG);
221                 chmod($config_file_path, 0770);
222                 chown($config_file_path, 'vmail');
223                 unset($config_file_path);
224             } else {
225                 // Delete the mailfilter recipe
226                 if(isset($data["old"]["email"])) {
227                     $email_parts = explode("@", $data["old"]["email"]);
228                     $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
229                     if(is_file($file)) {
230                         unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
231                         $app->log("Deleting custom Mailfiter".$file, LOGLEVEL_DEBUG);
c719dc 232                     }
e32699 233                 }
7fe908 234             }
e32699 235         }
T 236     }
7fe908 237
MC 238     function delete($event_name, $data) {
e32699 239         global $app, $conf;
7fe908 240
e32699 241         // load the server configuration options
T 242         $app->uses("getconf");
243         $mail_config = $app->getconf->get_server_config($conf["server_id"], 'mail');
244         $this->mailfilter_config_dir = $mail_config["homedir_path"].'/mailfilters';
7fe908 245
MC 246         $email_parts = explode("@", $data["old"]["email"]);
e32699 247         $dir = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0];
T 248         if(is_dir($dir)) {
249             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lock';
7fe908 250             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 251             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst';
7fe908 252             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 253             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.msg';
7fe908 254             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 255             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.autoresponder';
7fe908 256             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
e32699 257             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
7fe908 258             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
7bd0de 259             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.gdbm';
7fe908 260             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
7bd0de 261             $file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.lock';
7fe908 262             if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file", LOGLEVEL_WARN);
MC 263             rmdir($dir) or $app->log("Unable to delete directory: $dir", LOGLEVEL_WARN);
e32699 264         }
T 265     }
7fe908 266
e32699 267
T 268 } // end class
269
8e725d 270 ?>