Pascal Dreissen
2016-07-08 f1193b43f4c9fd132741d30f03f0b35841011989
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 {
b1a6a5 32
6cc49f 33     var $plugin_name = 'maildeliver_plugin';
T 34     var $class_name = 'maildeliver_plugin';
b1a6a5 35
MC 36
6cc49f 37     var $mailfilter_config_dir = '';
b1a6a5 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;
b1a6a5 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         }
b1a6a5 49
6cc49f 50     }
b1a6a5 51
6cc49f 52     /*
T 53          This function is called when the plugin is loaded
54     */
b1a6a5 55
6cc49f 56     function onLoad() {
T 57         global $app;
b1a6a5 58
6cc49f 59         /*
T 60         Register for the events
61         */
b1a6a5 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     }
b1a6a5 68
MC 69
70     function update($event_name, $data) {
6cc49f 71         global $app, $conf;
b1a6a5 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');
b1a6a5 76         if(substr($mail_config["homedir_path"], -1) == '/') {
MC 77             $mail_config["homedir_path"] = substr($mail_config["homedir_path"], 0, -1);
6cc49f 78         }
b1a6a5 79
6cc49f 80         if(isset($data["new"]["email"])) {
b1a6a5 81             $email_parts = explode("@", $data["new"]["email"]);
6cc49f 82         } else {
b1a6a5 83             $email_parts = explode("@", $data["old"]["email"]);
6cc49f 84         }
b1a6a5 85
6cc49f 86         // Write the custom mailfilter script, if mailfilter recipe has changed
T 87         if($data["old"]["custom_mailfilter"] != $data["new"]["custom_mailfilter"]
b1a6a5 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';
3d3f47 101             $sieve_file_isp = $data["new"]["maildir"].'/sieve/ispconfig.sieve';
b1a6a5 102             if(is_file($sieve_file)) unlink($sieve_file)  or $app->log("Unable to delete file: $sieve_file", LOGLEVEL_WARN);
3d3f47 103             if(is_file($sieve_file_isp)) unlink($sieve_file_isp)  or $app->log("Unable to delete file: $sieve_file_isp", LOGLEVEL_WARN);
6cc49f 104             $app->load('tpl');
b1a6a5 105
edf806 106             //* Select sieve filter file for dovecot version
b1a6a5 107             exec('dovecot --version', $tmp);
MC 108             if(substr($tmp[0], 0, 3) == '1.0') {
edf806 109                 $filter_file_template = "sieve_filter.master";
b1a6a5 110             } elseif(substr($tmp[0], 0, 3) == '1.2') {
edf806 111                 $filter_file_template = "sieve_filter_1.2.master";
b1a6a5 112             } elseif(substr($tmp[0], 0, 1) == '2') {
edf806 113                 $filter_file_template = "sieve_filter_1.2.master";
T 114             } else {
115                 $filter_file_template = "sieve_filter.master";
116             }
117             unset($tmp);
b1a6a5 118
edf806 119             //* Create new filter file based on template
6cc49f 120             $tpl = new tpl();
edf806 121             $tpl->newTemplate($filter_file_template);
b1a6a5 122
1f1072 123             // cc Field
f798aa 124             $tmp_mails_arr = explode(',',$data["new"]["cc"]);
FT 125             $tmp_addresses_arr = array();
126             foreach($tmp_mails_arr as $address) {
127                 if(trim($address) != '') $tmp_addresses_arr[] = array('address' => trim($address));
128             }
129             
b1a6a5 130             $tpl->setVar('cc', $data["new"]["cc"]);
f798aa 131             $tpl->setLoop('ccloop', $tmp_addresses_arr);
b1a6a5 132
6cc49f 133             // Custom filters
14ee73 134             if($data["new"]["custom_mailfilter"] == 'NULL') $data["new"]["custom_mailfilter"] = '';
56a9c7 135             $tpl->setVar('custom_mailfilter', str_replace("\r\n","\n",$data["new"]["custom_mailfilter"]));
b1a6a5 136
6cc49f 137             // Move junk
b1a6a5 138             $tpl->setVar('move_junk', $data["new"]["move_junk"]);
MC 139
4bd960 140             // Check autoresponder dates
651642 141             if((!$data['new']['autoresponder_start_date'] || $data["new"]["autoresponder_start_date"] == '0000-00-00 00:00:00') && (!$data['new']['autoresponder_end_date'] || $data["new"]["autoresponder_end_date"] == '0000-00-00 00:00:00')) {
b1a6a5 142                 $tpl->setVar('autoresponder_date_limit', 0);
4bd960 143             } else {
b1a6a5 144                 $tpl->setVar('autoresponder_date_limit', 1);
4bd960 145             }
b1a6a5 146
402bb3 147
M 148             // Set autoresponder start date
b1a6a5 149             $data["new"]["autoresponder_start_date"] = str_replace(" ", "T", $data["new"]["autoresponder_start_date"]);
MC 150             $tpl->setVar('start_date', $data["new"]["autoresponder_start_date"]);
402bb3 151
M 152             // Set autoresponder end date
b1a6a5 153             $data["new"]["autoresponder_end_date"] = str_replace(" ", "T", $data["new"]["autoresponder_end_date"]);
MC 154             $tpl->setVar('end_date', $data["new"]["autoresponder_end_date"]);
402bb3 155
6cc49f 156             // Autoresponder
b1a6a5 157             $tpl->setVar('autoresponder', $data["new"]["autoresponder"]);
f01ced 158
b1a6a5 159             // Autoresponder Subject
MC 160             $data["new"]["autoresponder_subject"] = str_replace("\"", "'", $data["new"]["autoresponder_subject"]);
161             $tpl->setVar('autoresponder_subject', $data["new"]["autoresponder_subject"]);
f01ced 162
L 163             // Autoresponder Text
b1a6a5 164             $data["new"]["autoresponder_text"] = str_replace("\"", "'", $data["new"]["autoresponder_text"]);
MC 165             $tpl->setVar('autoresponder_text', $data["new"]["autoresponder_text"]);
166
c6e05a 167             //* Set alias addresses for autoresponder
cc7a82 168             $sql = "SELECT * FROM mail_forwarding WHERE type = 'alias' AND destination = ?";
MC 169             $records = $app->db->queryAllRecords($sql, $data["new"]["email"]);
b1a6a5 170
bfcdef 171             $addresses = array();
T 172             $addresses[] = $data["new"]["email"];
cc604f 173             if(is_array($records) && count($records) > 0) {
c6e05a 174                 foreach($records as $rec) {
bfcdef 175                     $addresses[] = $rec['source'];
c6e05a 176                 }
T 177             }
b1a6a5 178
MC 179             $app->log("Found " . count($addresses) . " addresses.", LOGLEVEL_DEBUG);
180
181             $alias_addresses = array();
182
183             $email_parts = explode('@', $data["new"]["email"]);
cc7a82 184             $sql = "SELECT * FROM mail_forwarding WHERE type = 'aliasdomain' AND destination = ?";
MC 185             $records = $app->db->queryAllRecords($sql, '@'.$email_parts[1]);
bfcdef 186             if(is_array($records) && count($records) > 0) {
b1a6a5 187                 $app->log("Found " . count($records) . " records (aliasdomains).", LOGLEVEL_DEBUG);
bfcdef 188                 foreach($records as $rec) {
b1a6a5 189                     $aliasdomain = substr($rec['source'], 1);
bfcdef 190                     foreach($addresses as $email) {
b1a6a5 191                         $email_parts = explode('@', $email);
cc6568 192                         $alias_addresses[] = $email_parts[0].'@'.$aliasdomain;
bfcdef 193                     }
T 194                 }
195             }
b1a6a5 196
MC 197             $app->log("Found " . count($addresses) . " addresses at all.", LOGLEVEL_DEBUG);
198
199             $addresses = array_unique(array_merge($addresses, $alias_addresses));
200
201             $app->log("Found " . count($addresses) . " unique addresses at all.", LOGLEVEL_DEBUG);
202
bfcdef 203             $address_str = '';
T 204             if(is_array($addresses) && count($addresses) > 0) {
205                 $address_str .= ':addresses [';
206                 foreach($addresses as $rec) {
207                     $address_str .= '"'.$rec.'",';
208                 }
b1a6a5 209                 $address_str = substr($address_str, 0, -1);
bfcdef 210                 $address_str .= ']';
T 211             }
b1a6a5 212
MC 213
214             $tpl->setVar('addresses', $address_str);
215
097ad6 216             if ( ! is_dir($data["new"]["maildir"].'/sieve/') ) {
CS 217                 $app->system->mkdirpath($data["new"]["maildir"].'/sieve/', 0700, $mail_config['mailuser_name'], $mail_config['mailuser_group']);
218             }
219
3062be 220             file_put_contents($sieve_file_isp, $tpl->grab()) or $app->log("Unable to write sieve filter file", LOGLEVEL_WARN);
CS 221             if ( is_file($sieve_file_isp) ) {
4845fa 222                 $app->system->chown($sieve_file_isp,$mail_config['mailuser_name'],false);
CS 223                 $app->system->chgrp($sieve_file_isp,$mail_config['mailuser_group'],false);
3062be 224             }
097ad6 225             chdir($data["new"]["maildir"]);
3d3f47 226             //* create symlink to activate sieve script
86ad39 227             symlink("sieve/ispconfig.sieve", ".sieve")  or $app->log("Unable to create symlink to active sieve filter", LOGLEVEL_WARN);
CS 228             if (is_link(".sieve")) {
4845fa 229                 $app->system->chown(".sieve",$mail_config['mailuser_name'],true);
CS 230                 $app->system->chgrp(".sieve",$mail_config['mailuser_group'],true);
86ad39 231             }
4845fa 232             $app->system->chown($sieve_file,$mail_config['mailuser_name'],true);
CS 233             $app->system->chgrp($sieve_file,$mail_config['mailuser_group'],true);
3062be 234
6cc49f 235             unset($tpl);
b1a6a5 236
6cc49f 237         }
T 238     }
b1a6a5 239
MC 240     function delete($event_name, $data) {
6cc49f 241         global $app, $conf;
b1a6a5 242
6cc49f 243         $sieve_file = $data["old"]["maildir"].'/.sieve';
3d3f47 244         $sieve_file_isp = $data["old"]["maildir"].'/sieve/ispconfig.sieve';
b1a6a5 245         if(is_file($sieve_file)) unlink($sieve_file)  or $app->log("Unable to delete file: $sieve_file", LOGLEVEL_WARN);
3d3f47 246         if(is_file($sieve_file_isp)) unlink($sieve_file_isp)  or $app->log("Unable to delete file: $sieve_file_isp", LOGLEVEL_WARN);
6cc49f 247     }
b1a6a5 248
6cc49f 249
T 250 } // end class
251
b1a6a5 252 ?>