From eea5cff24c965684882e88632095d725e2d14637 Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Sun, 13 Jul 2008 17:12:10 +0000
Subject: [PATCH] The user email filters were now written to the custom mail
 rules field.

---
 interface/lib/classes/db_mysql.inc.php        |  2 +-
 .../web/mail/form/mail_user_filter.tform.php  |  4 +-
 .../web/mail/lib/lang/en_mail_user_filter.lng |  1 +
 .../web/mail/list/mail_user_filter.list.php   |  2 +-
 interface/web/mail/mail_user_filter_edit.php  | 92 ++++++++++++++++++-
 .../templates/mail_user_custom_rules_edit.htm |  6 +-
 6 files changed, 98 insertions(+), 9 deletions(-)

diff --git a/interface/lib/classes/db_mysql.inc.php b/interface/lib/classes/db_mysql.inc.php
index 3266155b13..eaa57b0cab 100644
--- a/interface/lib/classes/db_mysql.inc.php
+++ b/interface/lib/classes/db_mysql.inc.php
@@ -278,7 +278,7 @@ class db
 		return true;
 	}
 	
-	//** Updates a record and saves the changes into the datalog
+	//** Inserts a record and saves the changes into the datalog
 	public function datalogInsert($tablename, $insert_data, $index_field) {
 		global $app;
 		
diff --git a/interface/web/mail/form/mail_user_filter.tform.php b/interface/web/mail/form/mail_user_filter.tform.php
index 8c0eb41837..2a3c3bd197 100644
--- a/interface/web/mail/form/mail_user_filter.tform.php
+++ b/interface/web/mail/form/mail_user_filter.tform.php
@@ -39,7 +39,7 @@ $form["name"] 			= "mail_user_filter";
 $form["action"]			= "mail_user_filter_edit.php";
 $form["db_table"]		= "mail_user_filter";
 $form["db_table_idx"]	= "filter_id";
-$form["db_history"]		= "yes";
+$form["db_history"]		= "no";
 $form["tab_default"]	= "filter";
 $form["list_default"]	= "mail_user_filter_list.php";
 $form["auth"]			= 'yes'; // yes / no
@@ -81,7 +81,7 @@ $form["tabs"]['filter'] = array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'SELECT',
 			'default'	=> '',
-			'value'		=> array('Subject' => 'Subject','From'=>'From','To'=>'To','Header'=>'Custom Header')
+			'value'		=> array('Subject' => 'Subject','From'=>'From','To'=>'To')
 		),
 		'op' => array (
 			'datatype'	=> 'VARCHAR',
diff --git a/interface/web/mail/lib/lang/en_mail_user_filter.lng b/interface/web/mail/lib/lang/en_mail_user_filter.lng
index 9809087571..cd51bedb0a 100644
--- a/interface/web/mail/lib/lang/en_mail_user_filter.lng
+++ b/interface/web/mail/lib/lang/en_mail_user_filter.lng
@@ -7,4 +7,5 @@ $wb["btn_save_txt"] = 'Save';
 $wb["btn_cancel_txt"] = 'Cancel';
 $wb["rulename_error_empty"] = 'Name is empty.';
 $wb["searchterm_is_empty"] = 'Search term is empty.';
+$wb["source_txt"] = 'Source';
 ?>
\ No newline at end of file
diff --git a/interface/web/mail/list/mail_user_filter.list.php b/interface/web/mail/list/mail_user_filter.list.php
index dc26379d7d..5f3c661005 100644
--- a/interface/web/mail/list/mail_user_filter.list.php
+++ b/interface/web/mail/list/mail_user_filter.list.php
@@ -28,7 +28,7 @@ $liste["search_prefix"] 	= "search_";
 $liste["records_per_page"] 	= 15;
 
 // Script File of the list
-$liste["file"]				= "mail_user_filter_list.php";
+$liste["file"]				= "mail_user_list.php";
 
 // Script file of the edit form
 $liste["edit_file"]			= "mail_user_filter_edit.php";
diff --git a/interface/web/mail/mail_user_filter_edit.php b/interface/web/mail/mail_user_filter_edit.php
index 17da1fd0d4..a4199880b4 100644
--- a/interface/web/mail/mail_user_filter_edit.php
+++ b/interface/web/mail/mail_user_filter_edit.php
@@ -53,17 +53,105 @@ class page_action extends tform_actions {
 	function onSubmit() {
 		global $app, $conf;
 		
-		// Get the parent soa record of the domain
+		// Get the parent mail_user record
 		$mailuser = $app->db->queryOneRecord("SELECT * FROM mail_user WHERE mailuser_id = '".intval($_REQUEST["mailuser_id"])."' AND ".$app->tform->getAuthSQL('r'));
+		
 		// Check if Domain belongs to user
 		if($mailuser["mailuser_id"] != $_POST["mailuser_id"]) $app->tform->errorMessage .= $app->tform->wordbook["no_mailuser_perm"];
 		
-		// Set the server ID of the rr record to the same server ID as the parent record.
+		// Set the mailuser_id
 		$this->dataRecord["mailuser_id"] = $mailuser["mailuser_id"];
 		
 		parent::onSubmit();
 	}
 	
+	function onAfterInsert() {
+		global $app, $conf;
+		
+		$mailuser = $app->db->queryOneRecord("SELECT custom_mailfilter FROM mail_user WHERE mailuser_id = ".$this->dataRecord["mailuser_id"]);
+		$rule_content = $mailuser['custom_mailfilter']."\n".$app->db->quote($this->getRule());
+		$app->db->datalogUpdate('mail_user', "custom_mailfilter = '$rule_content'", 'mailuser_id', $this->dataRecord["mailuser_id"]);
+	
+	}
+	
+	function onAfterUpdate() {
+		global $app, $conf;
+		
+		$mailuser = $app->db->queryOneRecord("SELECT custom_mailfilter FROM mail_user WHERE mailuser_id = ".$this->dataRecord["mailuser_id"]);
+		$skip = false;
+		$lines = explode("\n",$mailuser['custom_mailfilter']);
+		$out = '';
+		$found = false;
+		
+		foreach($lines as $line) {
+			$line = trim($line);
+			if($line == '### BEGIN FILTER_ID:'.$this->id) {
+				$skip = true;
+				$found = true;
+			}
+			if($skip == false && $line != '') $out .= $line ."\n";
+			if($line == '### END FILTER_ID:'.$this->id) {
+				$out .= $this->getRule();
+				$skip = false;
+			}
+		}
+		
+		// We did not found our rule, so we add it now.
+		if($found == false) {
+			$out .= $this->getRule();
+		}
+		
+		$out = addslashes($out);
+		$app->db->datalogUpdate('mail_user', "custom_mailfilter = '$out'", 'mailuser_id', $this->dataRecord["mailuser_id"]);
+	
+	}
+	
+	function getRule() {
+		
+		$content = '';
+		$content .= '### BEGIN FILTER_ID:'.$this->id."\n";
+		
+		if($this->dataRecord["action"] == 'move') {
+		
+			$content .= "
+`test -e ".'$DEFAULT/'.$this->dataRecord["target"]."`
+if ( ".'$RETURNCODE'." != 0 )
+{
+  `maildirmake -f ".$this->dataRecord["target"].' $DEFAULT'."`
+  `chmod -R 0700 ".'$DEFAULT/'.$this->dataRecord["target"]."`
+}
+";		
+		}
+		
+		$content .= "if (/^".$this->dataRecord["source"].":";
+		
+		if($this->dataRecord["op"] == 'contains') {
+			$content .= ".*".$this->dataRecord["searchterm"]."/:h)\n";
+		} elseif ($this->dataRecord["op"] == 'is') {
+			$content .= $this->dataRecord["searchterm"]."$/:h)\n";
+		} elseif ($this->dataRecord["op"] == 'begins') {
+			$content .= $this->dataRecord["searchterm"]."/:h)\n";
+		} elseif ($this->dataRecord["op"] == 'ends') {
+			$content .= ".*".$this->dataRecord["searchterm"]."$/:h)\n";
+		}
+		
+		$content .= "{\n";
+		$content .= "exception {\n";
+		
+		if($this->dataRecord["action"] == 'move') {
+			$content .= 'to $DEFAULT/'.$this->dataRecord["target"]."/\n";
+		} else {
+			$content .= "to /dev/null\n";
+		}
+		
+		$content .= "}\n";
+		$content .= "}\n";
+		
+		$content .= '### END FILTER_ID:'.$this->id."\n";
+		
+		return $content;
+	}
+	
 }
 
 $page = new page_action;
diff --git a/interface/web/mail/templates/mail_user_custom_rules_edit.htm b/interface/web/mail/templates/mail_user_custom_rules_edit.htm
index 57310e115d..ac72f4da3e 100644
--- a/interface/web/mail/templates/mail_user_custom_rules_edit.htm
+++ b/interface/web/mail/templates/mail_user_custom_rules_edit.htm
@@ -1,7 +1,7 @@
 <table width="500" border="0" cellspacing="0" cellpadding="2">
   <tr>
     <td class="frmText11">{tmpl_var name='custom_mailfilter_txt'}:</td>
-    <td class="frmText11"><textarea name='custom_mailfilter' cols='30' rows='15'>{tmpl_var name='custom_mailfilter'}</textarea></td>
+    <td class="frmText11"><textarea name='custom_mailfilter' cols='50' rows='15'>{tmpl_var name='custom_mailfilter'}</textarea></td>
   </tr>  <tr>
     <td class="frmText11">&nbsp;</td>
     <td class="frmText11">&nbsp;</td>
@@ -11,6 +11,6 @@
     <td><input name="btn_save" type="button" class="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','mail/mail_user_edit.php');"><div class="buttonEnding"></div>&nbsp;
       <input name="btn_cancel" type="button" class="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('mail/mail_user_list.php');"><div class="buttonEnding"></div>
     </td>
-  </tr>
-</table>
+  </tr>
+</table>
 <input type="hidden" name="id" value="{tmpl_var name='id'}">
\ No newline at end of file
-- 
GitLab