From dcff5718f345f55f7ea81f18d1f086facde4eacf Mon Sep 17 00:00:00 2001
From: pedro_morgan <pedro_morgan@ispconfig3>
Date: Mon, 20 Aug 2007 23:14:51 -0400
Subject: [PATCH] * Using new ISPC_*_PATH constants * Adding new data schema (to be moved shortly * no more $conf['classpath']

---
 interface/web/mail/mail_domain_edit.php |  123 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 118 insertions(+), 5 deletions(-)

diff --git a/interface/web/mail/mail_domain_edit.php b/interface/web/mail/mail_domain_edit.php
index f2709b5..b8ca43b 100644
--- a/interface/web/mail/mail_domain_edit.php
+++ b/interface/web/mail/mail_domain_edit.php
@@ -1,6 +1,6 @@
 <?php
 /*
-Copyright (c) 2005, Till Brehm, projektfarm Gmbh
+Copyright (c) 2007, Till Brehm, projektfarm Gmbh
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification,
@@ -52,7 +52,29 @@
 $app->load('tform_actions');
 
 class page_action extends tform_actions {
-
+	
+	function onShowNew() {
+		global $app, $conf;
+		
+		// we will check only users, not admins
+		if($_SESSION["s"]["user"]["typ"] == 'user') {
+			
+			// Get the limits of the client
+			$client_group_id = $_SESSION["s"]["user"]["default_group"];
+			$client = $app->db->queryOneRecord("SELECT limit_maildomain FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+			
+			// Check if the user may add another maildomain.
+			if($client["limit_maildomain"] >= 0) {
+				$tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM mail_domain WHERE sys_groupid = $client_group_id");
+				if($tmp["number"] >= $client["limit_maildomain"]) {
+					$app->error($app->tform->wordbook["limit_maildomain_txt"]);
+				}
+			}
+		}
+		
+		parent::onShowNew();
+	}
+	
 	function onShowEnd() {
 		global $app, $conf;
 		
@@ -70,30 +92,121 @@
 		$app->tpl->setVar("client_group_id",$client_select);
 		}
 		
+		// Get the spamfilter policys for the user
+		$tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = '@".$this->dataRecord["domain"]."'");
+		$sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r');
+		$policys = $app->db->queryAllRecords($sql);
+		$policy_select = "<option value='0'>".$app->tform->wordbook["no_policy"]."</option>";
+		if(is_array($policys)) {
+			foreach( $policys as $p) {
+				$selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':'';
+				$policy_select .= "<option value='$p[id]' $selected>$p[policy_name]</option>\r\n";
+			}
+		}
+		$app->tpl->setVar("policy",$policy_select);
+		unset($policys);
+		unset($policy_select);
+		unset($tmp_user);
+		
 		parent::onShowEnd();
 	}
 	
 	function onSubmit() {
-		if($_SESSION["s"]["user"]["typ"] != 'admin') unset($this->dataRecord["client_group_id"]);
+		global $app, $conf;
+		if($_SESSION["s"]["user"]["typ"] != 'admin') {
+			
+			// Get the limits of the client
+			$client_group_id = $_SESSION["s"]["user"]["default_group"];
+			$client = $app->db->queryOneRecord("SELECT limit_maildomain, default_mailserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+			
+			// When the record is updated
+			if($this->id > 0) {
+				// restore the server ID if the user is not admin and record is edited
+				$tmp = $app->db->queryOneRecord("SELECT server_id FROM mail_domain WHERE domain_id = ".intval($this->id));
+				$this->dataRecord["server_id"] = $tmp["server_id"];
+				unset($tmp);
+			// When the record is inserted
+			} else {
+				// set the server ID to the default mailserver of the client
+				$this->dataRecord["server_id"] = $client["default_mailserver"];
+				
+				// Check if the user may add another mail_domain
+				if($client["limit_maildomain"] >= 0) {
+					$tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM mail_domain WHERE sys_groupid = $client_group_id");
+					if($tmp["number"] >= $client["limit_maildomain"]) {
+						$app->error($app->tform->wordbook["limit_maildomain_txt"]);
+					}
+				}
+			}
+			
+			// Clients may not set the client_group_id, so we unset them if user is not a admin
+			unset($this->dataRecord["client_group_id"]);
+		}
 		parent::onSubmit();
 	}
 	
 	function onAfterInsert() {
 		global $app, $conf;
 		
+		// make sure that the record belongs to the clinet group and not the admin group when a dmin inserts it
+		// also make sure that the user can not delete domain created by a admin
 		if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
 			$client_group_id = intval($this->dataRecord["client_group_id"]);
-			$app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id WHERE domain_id = ".$this->id);
+			$app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
 		}
+		
+		// Spamfilter policy
+		$policy_id = intval($this->dataRecord["policy"]);
+		if($policy_id > 0) {
+			$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".addslashes($this->dataRecord["domain"])."'");
+			if($tmp_user["id"] > 0) {
+				// There is already a record that we will update
+				$sql = "UPDATE spamfilter_users SET policy_id = $ploicy_id WHERE id = ".$tmp_user["id"];
+				$app->db->query($sql);
+			} else {
+				$tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ".$this->id);
+				// We create a new record
+				$sql = "INSERT INTO `spamfilter_users` (`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`) 
+				        VALUES (".$_SESSION["s"]["user"]["userid"].", ".$tmp_domain["sys_groupid"].", 'riud', 'riud', '', ".$this->dataRecord["server_id"].", 5, ".$policy_id.", '@".addslashes($this->dataRecord["domain"])."', '@".addslashes($this->dataRecord["domain"])."', 'Y')";
+				$app->db->query($sql);
+				unset($tmp_domain);
+			}
+		}  // endif spamfilter policy
 	}
 	
 	function onAfterUpdate() {
 		global $app, $conf;
 		
+		// make sure that the record belongs to the clinet group and not the admin group when a dmin inserts it
+		// also make sure that the user can not delete domain created by a admin
 		if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
 			$client_group_id = intval($this->dataRecord["client_group_id"]);
-			$app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id WHERE domain_id = ".$this->id);
+			$app->db->query("UPDATE mail_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
 		}
+		
+		// Spamfilter policy
+		$policy_id = intval($this->dataRecord["policy"]);
+		$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = '@".addslashes($this->dataRecord["domain"])."'");
+		if($policy_id > 0) {
+			if($tmp_user["id"] > 0) {
+				// There is already a record that we will update
+				$sql = "UPDATE spamfilter_users SET policy_id = $policy_id WHERE id = ".$tmp_user["id"];
+				$app->db->query($sql);
+			} else {
+				$tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ".$this->id);
+				// We create a new record
+				$sql = "INSERT INTO `spamfilter_users` (`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `priority`, `policy_id`, `email`, `fullname`, `local`) 
+				        VALUES (".$_SESSION["s"]["user"]["userid"].", ".$tmp_domain["sys_groupid"].", 'riud', 'riud', '', ".$this->dataRecord["server_id"].", 5, ".$policy_id.", '@".addslashes($this->dataRecord["domain"])."', '@".addslashes($this->dataRecord["domain"])."', 'Y')";
+				$app->db->query($sql);
+				unset($tmp_domain);
+			}
+		}else {
+			if($tmp_user["id"] > 0) {
+				// There is already a record but the user shall have no policy, so we delete it
+				$sql = "DELETE FROM spamfilter_users WHERE id = ".$tmp_user["id"];
+				$app->db->query($sql);
+			}
+		} // endif spamfilter policy
 	}
 	
 }

--
Gitblit v1.9.1