From 1aece9b8d53b51f3ce02241d96d7a1b2f38455db Mon Sep 17 00:00:00 2001
From: Marius Cramer <m.cramer@pixcept.de>
Date: Fri, 28 Feb 2014 10:36:34 -0500
Subject: [PATCH] Fixed problem with new "multiple servers for clients" feature

---
 interface/web/sites/database_edit.php |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/interface/web/sites/database_edit.php b/interface/web/sites/database_edit.php
index 139c971..6ae8856 100644
--- a/interface/web/sites/database_edit.php
+++ b/interface/web/sites/database_edit.php
@@ -166,7 +166,7 @@
 		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 db_servers, limit_database FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+			$client = $app->db->queryOneRecord("SELECT db_servers, limit_database, limit_database_quota 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) {
@@ -189,6 +189,20 @@
 					if($tmp["number"] >= $client["limit_database"]) {
 						$app->error($app->tform->wordbook["limit_database_txt"]);
 					}
+				}
+
+				// Check client quota
+				 if ($client['limit_database_quota'] >= 0) {
+					$tmp = $app->db->queryOneRecord("SELECT sum(database_quota) as db_quota FROM web_database WHERE sys_groupid = $client_group_id");
+					$db_quota = $tmp['db_quota'];
+					$new_db_quota = $app->functions->intval($this->dataRecord["database_quota"]);
+					if(($db_quota + $new_db_quota > $client['limit_database_quota']) || ($new_db_quota < 0 && $client['limit_database_quota'] >= 0)) {
+						$max_free_quota = floor($client['limit_database_quota'] - $db_quota);
+						if($max_free_quota < 0) $max_free_quota = 0;
+						$app->tform->errorMessage .= $app->tform->lng("limit_database_quota_free_txt").": ".$max_free_quota." MB<br>";
+						$this->dataRecord['database_quota'] = $max_free_quota;
+					}
+					unset($tmp);
 				}
 
 			}
@@ -302,7 +316,26 @@
 				}
 			}
 		}
-
+		
+		if ($app->tform->errorMessage == '') {
+			// force update of the used database user
+			if($this->dataRecord['database_user_id']) {
+				$user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_user_id']));
+				if($user_old_rec) {
+					$user_new_rec = $user_old_rec;
+					$user_new_rec['server_id'] = $this->dataRecord['server_id'];
+					$app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_user_id'], $user_old_rec, $user_new_rec);
+				}
+			}
+			if($this->dataRecord['database_ro_user_id']) {
+				$user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_ro_user_id']));
+				if($user_old_rec) {
+					$user_new_rec = $user_old_rec;
+					$user_new_rec['server_id'] = $this->dataRecord['server_id'];
+					$app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_ro_user_id'], $user_old_rec, $user_new_rec);
+				}
+			}
+		}
 
 		parent::onBeforeUpdate();
 	}
@@ -371,6 +404,27 @@
 			}
 		}
 
+		if ($app->tform->errorMessage == '') {
+			// force update of the used database user
+			if($this->dataRecord['database_user_id']) {
+				$user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_user_id']));
+				if($user_old_rec) {
+					$user_new_rec = $user_old_rec;
+					$user_new_rec['server_id'] = $this->dataRecord['server_id'];
+					$app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_user_id'], $user_old_rec, $user_new_rec);
+				}
+			}
+			if($this->dataRecord['database_ro_user_id']) {
+				$user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_ro_user_id']));
+				if($user_old_rec) {
+					$user_new_rec = $user_old_rec;
+					$user_new_rec['server_id'] = $this->dataRecord['server_id'];
+					$app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_ro_user_id'], $user_old_rec, $user_new_rec);
+				}
+			}
+		}
+
+
 		parent::onBeforeInsert();
 	}
 

--
Gitblit v1.9.1