maildeliver_plugin.inc.php 4.72 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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 maildeliver_plugin {
	
	var $plugin_name = 'maildeliver_plugin';
	var $class_name = 'maildeliver_plugin';
	
	
	var $mailfilter_config_dir = '';
	
	//* This function is called during ispconfig installation to determine
	//  if a symlink shall be created for this plugin.
	function onInstall() {
		global $conf;
		
		if($conf['services']['mail'] == true && isset($conf['dovecot']['installed']) && $conf['dovecot']['installed'] == true) {
			return true;
		} else {
			return false;
		}
		
	}
	
	/*
	 	This function is called when the plugin is loaded
	*/
	
	function onLoad() {
		global $app;
		
		/*
		Register for the events
		*/
		
63
		$app->plugins->registerEvent('mail_user_insert','maildeliver_plugin','update');
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
		$app->plugins->registerEvent('mail_user_update','maildeliver_plugin','update');
		$app->plugins->registerEvent('mail_user_delete','maildeliver_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);
		}
		
		if(isset($data["new"]["email"])) {
			$email_parts = explode("@",$data["new"]["email"]);
		} else {
			$email_parts = explode("@",$data["old"]["email"]);
		}
			
		// 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"]["autoresponder_text"] != $data["new"]["autoresponder_text"] 
			   or $data["old"]["autoresponder"] != $data["new"]["autoresponder"]
			   or (isset($data["new"]["email"]) and $data["old"]["email"] != $data["new"]["email"])
			   or $data["old"]["autoresponder_start_date"] != $data["new"]["autoresponder_start_date"]
93
94
95
			   or $data["old"]["autoresponder_end_date"] != $data["new"]["autoresponder_end_date"]
			   or $data["old"]["cc"] != $data["new"]["cc"]
			   ) {
96
97
98
99
100
101
102
103
104
105
				
			$app->log("Mailfilter config has been changed",LOGLEVEL_DEBUG);
				
			$sieve_file = $data["new"]["maildir"].'/.sieve';
			if(is_file($sieve_file)) unlink($sieve_file)  or $app->log("Unable to delete file: $sieve_file",LOGLEVEL_WARN);
				
			$app->load('tpl');
		
			$tpl = new tpl();
			$tpl->newTemplate("sieve_filter.master");
tbrehm's avatar
tbrehm committed
106
107
108
			
			// cc Field
			$tpl->setVar('cc',$data["new"]["cc"]);
109
110
111
112
113
114
115
116
117
				
			// Custom filters
			$tpl->setVar('custom_mailfilter',$data["new"]["custom_mailfilter"]);
				
			// Move junk
			$tpl->setVar('move_junk',$data["new"]["move_junk"]);
				
			// Autoresponder
			$tpl->setVar('autoresponder',$data["new"]["autoresponder"]);
118
119
			
			$data["new"]["autoresponder_text"] = str_replace("\"","'",$data["new"]["autoresponder_text"]); 
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
			$tpl->setVar('autoresponder_text',$data["new"]["autoresponder_text"]);
				
			file_put_contents($sieve_file,$tpl->grab());
			
			unset($tpl);
				
		}
	}
	
	function delete($event_name,$data) {
		global $app, $conf;
		
		$sieve_file = $data["old"]["maildir"].'/.sieve';
		if(is_file($sieve_file)) unlink($sieve_file)  or $app->log("Unable to delete file: $sieve_file",LOGLEVEL_WARN);
	}
	

} // end class

?>