maildrop_plugin.inc.php 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

/*
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

} // end class

254
?>