diff --git a/interface/web/mail/mail_user_edit.php b/interface/web/mail/mail_user_edit.php
index a0691f909a7d8e45800dab22bbea84f62d69ad23..02a27fb9c1c4e69a8f6e0d1a0a0b4861629bb36e 100644
--- a/interface/web/mail/mail_user_edit.php
+++ b/interface/web/mail/mail_user_edit.php
@@ -153,7 +153,7 @@ class page_action extends tform_actions {
 		if($_SESSION["s"]["user"]["typ"] != 'admin') { // if user is not admin
 			// Get the limits of the client
 			$client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
-			$client = $app->db->queryOneRecord("SELECT limit_mailbox, limit_mailquota FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+			$client = $app->db->queryOneRecord("SELECT limit_mailbox, limit_mailquota, parent_client_id 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 mailbox.
@@ -179,6 +179,28 @@ class page_action extends tform_actions {
 				unset($tmp);
 				unset($tmp_quota);
 			}
+
+			if($client['parent_client_id'] > 0) {
+				// Get the limits of the reseller
+				$reseller = $app->db->queryOneRecord("SELECT limit_mailquota, limit_maildomain FROM client WHERE client_id = ".$client['parent_client_id']);
+
+				//* Check the website quota of the client
+				if(isset($_POST["quota"]) && $reseller["limit_mailquota"] >= 0 && $app->functions->intval($this->dataRecord["quota"]) * 1024 * 1024 != $this->oldDataRecord['quota']) {
+					$tmp = $app->db->queryOneRecord("SELECT sum(quota) as mailquota FROM mail_user, sys_group, client WHERE mail_user.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ".$client['parent_client_id']." IN (client.parent_client_id, client.client_id) AND mailuser_id != ".$app->functions->intval($this->id));
+
+					$mailquota = $tmp["mailquota"] / 1024 / 1024;
+					$new_mailbox_quota = $app->functions->intval($this->dataRecord["quota"]);
+					if(($mailquota + $new_mailbox_quota > $reseller["limit_mailquota"]) || ($new_mailbox_quota == 0 && $reseller["limit_mailquota"] != -1)) {
+						$max_free_quota = $reseller["limit_mailquota"] - $mailquota;
+						if($max_free_quota < 0) $max_free_quota = 0;
+						$app->tform->errorMessage .= $app->tform->lng("limit_mailquota_txt").": ".$max_free_quota."<br>";
+						// Set the quota field to the max free space
+						$this->dataRecord["quota"] = $max_free_quota;
+					}
+					unset($tmp);
+					unset($tmp_quota);
+				}
+			}
 		} // end if user is not admin
 
 
diff --git a/interface/web/sites/database_edit.php b/interface/web/sites/database_edit.php
index 79183c4612c26c8b4049ce187f0be53868e96dfd..c392c79e5054d6c73b6d5d8cc1023fd4d9b3d8ed 100644
--- a/interface/web/sites/database_edit.php
+++ b/interface/web/sites/database_edit.php
@@ -170,7 +170,7 @@ class page_action extends tform_actions {
 		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, limit_database_quota 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, parent_client_id 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) {
@@ -200,6 +200,33 @@ class page_action extends tform_actions {
 					unset($global_config);
 					unset($dbname_prefix);
 				}
+
+				if($client['parent_client_id'] > 0) {
+					// Get the limits of the reseller
+					$reseller = $app->db->queryOneRecord("SELECT limit_database, limit_database_quota FROM client WHERE client_id = ".$client['parent_client_id']);
+
+					//* Check the website quota of the client
+					if ($reseller['limit_database_quota'] >= 0) {
+						//* get the database prefix
+						$app->uses('getconf,tools_sites');
+						$global_config = $app->getconf->get_global_config('sites');
+						$dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
+						//* get quota from other databases
+						$tmp = $app->db->queryOneRecord("SELECT sum(database_quota) as db_quota FROM web_database, sys_group, client WHERE web_database.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ? IN (client.parent_client_id, client.client_id) AND database_name <> ?", $client['parent_client_id'], $dbname_prefix.$this->dataRecord['database_name']);
+
+						$used_quota = $app->functions->intval($tmp['db_quota']);
+						$new_db_quota = $app->functions->intval($this->dataRecord["database_quota"]);
+						if(($used_quota + $new_db_quota > $reseller["limit_database_quota"]) || ($new_db_quota < 0 && $reseller["limit_database_quota"] >= 0)) {
+							$max_free_quota = floor($reseller["limit_database_quota"] - $used_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);
+						unset($global_config);
+						unset($dbname_prefix);
+					}
+				}
 				// When the record is inserted
 			} else {
 				$client['db_servers_ids'] = explode(',', $client['db_servers']);
diff --git a/interface/web/sites/web_vhost_domain_edit.php b/interface/web/sites/web_vhost_domain_edit.php
index a33c1241ace6fecc8b65504f2c5155491c2a0282..69b9de955a2fffdd52d239c08c670beed7c00eb6 100644
--- a/interface/web/sites/web_vhost_domain_edit.php
+++ b/interface/web/sites/web_vhost_domain_edit.php
@@ -838,7 +838,8 @@ class page_action extends tform_actions {
 				if($this->_vhostdomain_type == 'domain') {
 					//* Check the website quota of the client
 					if(isset($_POST["hd_quota"]) && $reseller["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
-						$tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'));
+						$tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain, sys_group, client WHERE web_domain.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ".$client['parent_client_id']." IN (client.parent_client_id, client.client_id) AND domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost'");
+
 						$webquota = $tmp["webquota"];
 						$new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
 						if(($webquota + $new_web_quota > $reseller["limit_web_quota"]) || ($new_web_quota < 0 && $reseller["limit_web_quota"] >= 0)) {
@@ -855,7 +856,7 @@ class page_action extends tform_actions {
 
 				//* Check the traffic quota of the client
 				if(isset($_POST["traffic_quota"]) && $reseller["limit_traffic_quota"] > 0 && $_POST["traffic_quota"] != $old_web_values["traffic_quota"]) {
-					$tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
+					$tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain, sys_group, client WHERE web_domain.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ".$client['parent_client_id']." IN (client.parent_client_id, client.client_id) AND domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost'");
 					$trafficquota = $tmp["trafficquota"];
 					$new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
 					if(($trafficquota + $new_traffic_quota > $reseller["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $reseller["limit_traffic_quota"] >= 0)) {