Till Brehm
2014-08-14 14ee7385443613d71f8d3824ae6efbd8340f087c
commit | author | age
6cc49f 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 maildeliver_plugin {
7fe908 32
6cc49f 33     var $plugin_name = 'maildeliver_plugin';
T 34     var $class_name = 'maildeliver_plugin';
7fe908 35
MC 36
6cc49f 37     var $mailfilter_config_dir = '';
7fe908 38
6cc49f 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['dovecot']['installed']) && $conf['dovecot']['installed'] == true) {
T 45             return true;
46         } else {
47             return false;
48         }
7fe908 49
6cc49f 50     }
7fe908 51
6cc49f 52     /*
T 53          This function is called when the plugin is loaded
54     */
7fe908 55
6cc49f 56     function onLoad() {
T 57         global $app;
7fe908 58
6cc49f 59         /*
T 60         Register for the events
61         */
7fe908 62
MC 63         $app->plugins->registerEvent('mail_user_insert', 'maildeliver_plugin', 'update');
64         $app->plugins->registerEvent('mail_user_update', 'maildeliver_plugin', 'update');
65         $app->plugins->registerEvent('mail_user_delete', 'maildeliver_plugin', 'delete');
66
6cc49f 67     }
7fe908 68
MC 69
70     function update($event_name, $data) {
6cc49f 71         global $app, $conf;
7fe908 72
6cc49f 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);
6cc49f 78         }
7fe908 79
6cc49f 80         if(isset($data["new"]["email"])) {
7fe908 81             $email_parts = explode("@", $data["new"]["email"]);
6cc49f 82         } else {
7fe908 83             $email_parts = explode("@", $data["old"]["email"]);
6cc49f 84         }
7fe908 85
6cc49f 86         // Write the custom mailfilter script, if mailfilter recipe has changed
T 87         if($data["old"]["custom_mailfilter"] != $data["new"]["custom_mailfilter"]
7fe908 88             or $data["old"]["move_junk"] != $data["new"]["move_junk"]
MC 89             or $data["old"]["autoresponder_subject"] != $data["new"]["autoresponder_subject"]
90             or $data["old"]["autoresponder_text"] != $data["new"]["autoresponder_text"]
91             or $data["old"]["autoresponder"] != $data["new"]["autoresponder"]
92             or (isset($data["new"]["email"]) and $data["old"]["email"] != $data["new"]["email"])
93             or $data["old"]["autoresponder_start_date"] != $data["new"]["autoresponder_start_date"]
94             or $data["old"]["autoresponder_end_date"] != $data["new"]["autoresponder_end_date"]
95             or $data["old"]["cc"] != $data["new"]["cc"]
96         ) {
97
98             $app->log("Mailfilter config has been changed", LOGLEVEL_DEBUG);
99
6cc49f 100             $sieve_file = $data["new"]["maildir"].'/.sieve';
7fe908 101             if(is_file($sieve_file)) unlink($sieve_file)  or $app->log("Unable to delete file: $sieve_file", LOGLEVEL_WARN);
MC 102
6cc49f 103             $app->load('tpl');
7fe908 104
edf806 105             //* Select sieve filter file for dovecot version
7fe908 106             exec('dovecot --version', $tmp);
MC 107             if(substr($tmp[0], 0, 3) == '1.0') {
edf806 108                 $filter_file_template = "sieve_filter.master";
7fe908 109             } elseif(substr($tmp[0], 0, 3) == '1.2') {
edf806 110                 $filter_file_template = "sieve_filter_1.2.master";
7fe908 111             } elseif(substr($tmp[0], 0, 1) == '2') {
edf806 112                 $filter_file_template = "sieve_filter_1.2.master";
T 113             } else {
114                 $filter_file_template = "sieve_filter.master";
115             }
116             unset($tmp);
7fe908 117
edf806 118             //* Create new filter file based on template
6cc49f 119             $tpl = new tpl();
edf806 120             $tpl->newTemplate($filter_file_template);
7fe908 121
1f1072 122             // cc Field
f798aa 123             $tmp_mails_arr = explode(',',$data["new"]["cc"]);
FT 124             $tmp_addresses_arr = array();
125             foreach($tmp_mails_arr as $address) {
126                 if(trim($address) != '') $tmp_addresses_arr[] = array('address' => trim($address));
127             }
128             
7fe908 129             $tpl->setVar('cc', $data["new"]["cc"]);
f798aa 130             $tpl->setLoop('ccloop', $tmp_addresses_arr);
7fe908 131
6cc49f 132             // Custom filters
14ee73 133             if($data["new"]["custom_mailfilter"] == 'NULL') $data["new"]["custom_mailfilter"] = '';
7fe908 134             $tpl->setVar('custom_mailfilter', $data["new"]["custom_mailfilter"]);
MC 135
6cc49f 136             // Move junk
7fe908 137             $tpl->setVar('move_junk', $data["new"]["move_junk"]);
MC 138
4bd960 139             // Check autoresponder dates
T 140             if($data["new"]["autoresponder_start_date"] == '0000-00-00 00:00:00' && $data["new"]["autoresponder_end_date"] == '0000-00-00 00:00:00') {
7fe908 141                 $tpl->setVar('autoresponder_date_limit', 0);
4bd960 142             } else {
7fe908 143                 $tpl->setVar('autoresponder_date_limit', 1);
4bd960 144             }
7fe908 145
402bb3 146
M 147             // Set autoresponder start date
7fe908 148             $data["new"]["autoresponder_start_date"] = str_replace(" ", "T", $data["new"]["autoresponder_start_date"]);
MC 149             $tpl->setVar('start_date', $data["new"]["autoresponder_start_date"]);
402bb3 150
M 151             // Set autoresponder end date
7fe908 152             $data["new"]["autoresponder_end_date"] = str_replace(" ", "T", $data["new"]["autoresponder_end_date"]);
MC 153             $tpl->setVar('end_date', $data["new"]["autoresponder_end_date"]);
402bb3 154
6cc49f 155             // Autoresponder
7fe908 156             $tpl->setVar('autoresponder', $data["new"]["autoresponder"]);
f01ced 157
7fe908 158             // Autoresponder Subject
MC 159             $data["new"]["autoresponder_subject"] = str_replace("\"", "'", $data["new"]["autoresponder_subject"]);
160             $tpl->setVar('autoresponder_subject', $data["new"]["autoresponder_subject"]);
f01ced 161
L 162             // Autoresponder Text
7fe908 163             $data["new"]["autoresponder_text"] = str_replace("\"", "'", $data["new"]["autoresponder_text"]);
MC 164             $tpl->setVar('autoresponder_text', $data["new"]["autoresponder_text"]);
165
c6e05a 166             //* Set alias addresses for autoresponder
T 167             $sql = "SELECT * FROM mail_forwarding WHERE type = 'alias' AND destination = '".$app->db->quote($data["new"]["email"])."'";
168             $records = $app->db->queryAllRecords($sql);
7fe908 169
bfcdef 170             $addresses = array();
T 171             $addresses[] = $data["new"]["email"];
cc604f 172             if(is_array($records) && count($records) > 0) {
c6e05a 173                 foreach($records as $rec) {
bfcdef 174                     $addresses[] = $rec['source'];
c6e05a 175                 }
T 176             }
7fe908 177
MC 178             $app->log("Found " . count($addresses) . " addresses.", LOGLEVEL_DEBUG);
179
180             $alias_addresses = array();
181
182             $email_parts = explode('@', $data["new"]["email"]);
bfcdef 183             $sql = "SELECT * FROM mail_forwarding WHERE type = 'aliasdomain' AND destination = '@".$app->db->quote($email_parts[1])."'";
T 184             $records = $app->db->queryAllRecords($sql);
185             if(is_array($records) && count($records) > 0) {
7fe908 186                 $app->log("Found " . count($records) . " records (aliasdomains).", LOGLEVEL_DEBUG);
bfcdef 187                 foreach($records as $rec) {
7fe908 188                     $aliasdomain = substr($rec['source'], 1);
bfcdef 189                     foreach($addresses as $email) {
7fe908 190                         $email_parts = explode('@', $email);
cc6568 191                         $alias_addresses[] = $email_parts[0].'@'.$aliasdomain;
bfcdef 192                     }
T 193                 }
194             }
7fe908 195
MC 196             $app->log("Found " . count($addresses) . " addresses at all.", LOGLEVEL_DEBUG);
197
198             $addresses = array_unique(array_merge($addresses, $alias_addresses));
199
200             $app->log("Found " . count($addresses) . " unique addresses at all.", LOGLEVEL_DEBUG);
201
bfcdef 202             $address_str = '';
T 203             if(is_array($addresses) && count($addresses) > 0) {
204                 $address_str .= ':addresses [';
205                 foreach($addresses as $rec) {
206                     $address_str .= '"'.$rec.'",';
207                 }
7fe908 208                 $address_str = substr($address_str, 0, -1);
bfcdef 209                 $address_str .= ']';
T 210             }
7fe908 211
MC 212
213             $tpl->setVar('addresses', $address_str);
214
215             file_put_contents($sieve_file, $tpl->grab());
401870 216             exec('chown '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($sieve_file));
7fe908 217
6cc49f 218             unset($tpl);
7fe908 219
6cc49f 220         }
T 221     }
7fe908 222
MC 223     function delete($event_name, $data) {
6cc49f 224         global $app, $conf;
7fe908 225
6cc49f 226         $sieve_file = $data["old"]["maildir"].'/.sieve';
7fe908 227         if(is_file($sieve_file)) unlink($sieve_file)  or $app->log("Unable to delete file: $sieve_file", LOGLEVEL_WARN);
6cc49f 228     }
7fe908 229
6cc49f 230
T 231 } // end class
232
7fe908 233 ?>