From f414abdaaaf869efc00c98736feddf44fc7a85a4 Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Mon, 12 Sep 2011 13:39:43 +0000
Subject: [PATCH] Added Limits for OpenVZ virtual servers in VM module.

---
 install/sql/incremental/upd_0017.sql          |  2 +
 interface/web/client/form/client.tform.php    | 25 +++++++
 .../web/client/form/client_template.tform.php | 25 +++++++
 interface/web/client/form/reseller.tform.php  | 25 +++++++
 interface/web/client/lib/lang/en_client.lng   |  3 +
 .../client/lib/lang/en_client_template.lng    |  3 +
 interface/web/client/lib/lang/en_reseller.lng |  3 +
 .../client/templates/client_edit_limits.htm   | 10 +++
 .../templates/client_template_edit_limits.htm | 10 +++
 .../client/templates/reseller_edit_limits.htm | 10 +++
 interface/web/vm/form/openvz_vm.tform.php     | 10 +--
 interface/web/vm/openvz_vm_edit.php           | 65 ++++++++++++++++++-
 interface/web/vm/templates/openvz_vm_edit.htm |  2 +-
 13 files changed, 186 insertions(+), 7 deletions(-)
 create mode 100644 install/sql/incremental/upd_0017.sql

diff --git a/install/sql/incremental/upd_0017.sql b/install/sql/incremental/upd_0017.sql
new file mode 100644
index 0000000000..07a4d78905
--- /dev/null
+++ b/install/sql/incremental/upd_0017.sql
@@ -0,0 +1,2 @@
+ALTER TABLE  `client_template` ADD  `limit_openvz_vm` int(11) NOT NULL DEFAULT '0' AFTER  `limit_mailmailinglist` ,
+ADD  `limit_openvz_vm_template_id` int(11) NOT NULL DEFAULT '0' AFTER  `limit_openvz_vm`;
\ No newline at end of file
diff --git a/interface/web/client/form/client.tform.php b/interface/web/client/form/client.tform.php
index eb61dc9db8..7b64147b79 100644
--- a/interface/web/client/form/client.tform.php
+++ b/interface/web/client/form/client.tform.php
@@ -827,6 +827,31 @@ $form["tabs"]['limits'] = array (
 			'rows'		=> '',
 			'cols'		=> ''
 		),
+		'limit_openvz_vm' => array (
+			'datatype'	=> 'INTEGER',
+			'formtype'	=> 'TEXT',
+			'validators'	=> array ( 	0 => array (	'type'	=> 'ISINT',
+														'errmsg'=> 'limit_openvz_vm_error_notint'),
+									),
+			'default'	=> '0',
+			'value'		=> '',
+			'separator'	=> '',
+			'width'		=> '10',
+			'maxlength'	=> '10',
+			'rows'		=> '',
+			'cols'		=> ''
+		),
+		'limit_openvz_vm_template_id' => array (
+			'datatype'	=> 'INTEGER',
+			'formtype'	=> 'SELECT',
+			'default'	=> '',
+			'datasource'	=> array ( 	'type'	=> 'SQL',
+										'querystring' => 'SELECT template_id,template_name FROM openvz_template WHERE 1 ORDER BY template_name',
+										'keyfield'=> 'template_id',
+										'valuefield'=> 'template_name'
+									 ),
+			'value'		=> array(0 => ' ')
+		),
 	##################################
 	# END Datatable fields
 	##################################
diff --git a/interface/web/client/form/client_template.tform.php b/interface/web/client/form/client_template.tform.php
index 628def1a69..d38fdd5520 100644
--- a/interface/web/client/form/client_template.tform.php
+++ b/interface/web/client/form/client_template.tform.php
@@ -515,6 +515,31 @@ $form["tabs"]['limits'] = array (
 			'rows'		=> '',
 			'cols'		=> ''
 		),
+		'limit_openvz_vm' => array (
+			'datatype'	=> 'INTEGER',
+			'formtype'	=> 'TEXT',
+			'validators'	=> array ( 	0 => array (	'type'	=> 'ISINT',
+														'errmsg'=> 'limit_openvz_vm_error_notint'),
+									),
+			'default'	=> '0',
+			'value'		=> '',
+			'separator'	=> '',
+			'width'		=> '10',
+			'maxlength'	=> '10',
+			'rows'		=> '',
+			'cols'		=> ''
+		),
+		'limit_openvz_vm_template_id' => array (
+			'datatype'	=> 'INTEGER',
+			'formtype'	=> 'SELECT',
+			'default'	=> '',
+			'datasource'	=> array ( 	'type'	=> 'SQL',
+										'querystring' => 'SELECT template_id,template_name FROM openvz_template WHERE 1 ORDER BY template_name',
+										'keyfield'=> 'template_id',
+										'valuefield'=> 'template_name'
+									 ),
+			'value'		=> array(0 => ' ')
+		),
 	##################################
 	# END Datatable fields
 	##################################
diff --git a/interface/web/client/form/reseller.tform.php b/interface/web/client/form/reseller.tform.php
index 715a892eda..a402bc7cf9 100644
--- a/interface/web/client/form/reseller.tform.php
+++ b/interface/web/client/form/reseller.tform.php
@@ -817,6 +817,31 @@ $form["tabs"]['limits'] = array (
 			'rows'		=> '',
 			'cols'		=> ''
 		),
+		'limit_openvz_vm' => array (
+			'datatype'	=> 'INTEGER',
+			'formtype'	=> 'TEXT',
+			'validators'	=> array ( 	0 => array (	'type'	=> 'ISINT',
+														'errmsg'=> 'limit_openvz_vm_error_notint'),
+									),
+			'default'	=> '0',
+			'value'		=> '',
+			'separator'	=> '',
+			'width'		=> '10',
+			'maxlength'	=> '10',
+			'rows'		=> '',
+			'cols'		=> ''
+		),
+		'limit_openvz_vm_template_id' => array (
+			'datatype'	=> 'INTEGER',
+			'formtype'	=> 'SELECT',
+			'default'	=> '',
+			'datasource'	=> array ( 	'type'	=> 'SQL',
+										'querystring' => 'SELECT template_id,template_name FROM openvz_template WHERE 1 ORDER BY template_name',
+										'keyfield'=> 'template_id',
+										'valuefield'=> 'template_name'
+									 ),
+			'value'		=> array(0 => ' ')
+		),
 	##################################
 	# END Datatable fields
 	##################################
diff --git a/interface/web/client/lib/lang/en_client.lng b/interface/web/client/lib/lang/en_client.lng
index 8ce6961187..36d3da2375 100644
--- a/interface/web/client/lib/lang/en_client.lng
+++ b/interface/web/client/lib/lang/en_client.lng
@@ -104,4 +104,7 @@ $wb["customer_no_txt"] = 'Customer No.';
 $wb["vat_id_txt"] = 'VAT ID';
 $wb["required_fields_txt"] = '* Required fields';
 $wb['company_id_txt'] = 'Company/Entrepreneur ID';
+$wb["limit_openvz_vm_txt"] = 'Max. number of virtual servers';
+$wb["limit_openvz_vm_template_id_txt"] = 'Force virtual server template';
+$wb["limit_openvz_vm_error_notint"] = 'The virtual server limit must be a number.';
 ?>
diff --git a/interface/web/client/lib/lang/en_client_template.lng b/interface/web/client/lib/lang/en_client_template.lng
index 3ad34449e6..c6d99770ec 100644
--- a/interface/web/client/lib/lang/en_client_template.lng
+++ b/interface/web/client/lib/lang/en_client_template.lng
@@ -64,4 +64,7 @@ $wb["limit_web_quota_txt"] = 'Web Quota';
 $wb["limit_traffic_quota_txt"] = 'Traffic Quota';
 $wb["limit_trafficquota_error_notint"] = 'Traffic Quota must be a number.';
 $wb["template_del_aborted_txt"] = 'Delete aborted. There is still a client which has this template selected.';
+$wb["limit_openvz_vm_txt"] = 'Max. number of virtual servers';
+$wb["limit_openvz_vm_template_id_txt"] = 'Force virtual server template';
+$wb["limit_openvz_vm_error_notint"] = 'The virtual server limit must be a number.';
 ?>
\ No newline at end of file
diff --git a/interface/web/client/lib/lang/en_reseller.lng b/interface/web/client/lib/lang/en_reseller.lng
index 44de4f9795..a553dd731c 100644
--- a/interface/web/client/lib/lang/en_reseller.lng
+++ b/interface/web/client/lib/lang/en_reseller.lng
@@ -103,4 +103,7 @@ $wb["limit_trafficquota_error_notint"] = 'Traffic Quota must be a number.';
 $wb["customer_no_txt"] = 'Customer No.';
 $wb["vat_id_txt"] = 'VAT ID';
 $wb["required_fields_txt"] = '* Required fields';
+$wb["limit_openvz_vm_txt"] = 'Max. number of virtual servers';
+$wb["limit_openvz_vm_template_id_txt"] = 'Force virtual server template';
+$wb["limit_openvz_vm_error_notint"] = 'The virtual server limit must be a number.';
 ?>
diff --git a/interface/web/client/templates/client_edit_limits.htm b/interface/web/client/templates/client_edit_limits.htm
index c8b8819dd0..74b28ef67f 100644
--- a/interface/web/client/templates/client_edit_limits.htm
+++ b/interface/web/client/templates/client_edit_limits.htm
@@ -189,6 +189,16 @@
 				<label for="limit_traffic_quota">{tmpl_var name='limit_traffic_quota_txt'}</label>
 				<input name="limit_traffic_quota" id="limit_traffic_quota" value="{tmpl_var name='limit_traffic_quota'}" size="10" maxlength="10" type="text" class="textInput formLengthLimit" />&nbsp;MB
 			</div>
+			<div class="ctrlHolder">
+				<label for="limit_openvz_vm">{tmpl_var name='limit_openvz_vm_txt'}</label>
+				<input name="limit_openvz_vm" id="limit_openvz_vm" value="{tmpl_var name='limit_openvz_vm'}" size="10" maxlength="10" type="text" class="textInput formLengthLimit" />
+			</div>
+			<div class="ctrlHolder">
+				<label for="limit_openvz_vm_template_id">{tmpl_var name='limit_openvz_vm_template_id_txt'}</label>
+				<select name="limit_openvz_vm_template_id" id="limit_openvz_vm_template_id" class="selectInput">
+					{tmpl_var name='limit_openvz_vm_template_id'}
+				</select>
+			</div>
 		</fieldset>
 
 		<input type="hidden" name="id" value="{tmpl_var name='id'}">
diff --git a/interface/web/client/templates/client_template_edit_limits.htm b/interface/web/client/templates/client_template_edit_limits.htm
index a1c67129e9..12fba84e45 100644
--- a/interface/web/client/templates/client_template_edit_limits.htm
+++ b/interface/web/client/templates/client_template_edit_limits.htm
@@ -123,6 +123,16 @@
 				<label for="limit_traffic_quota">{tmpl_var name='limit_traffic_quota_txt'}</label>
 				<input name="limit_traffic_quota" id="limit_traffic_quota" value="{tmpl_var name='limit_traffic_quota'}" size="10" maxlength="10" type="text" class="textInput formLengthLimit" />&nbsp;MB
 			</div>
+			<div class="ctrlHolder">
+				<label for="limit_openvz_vm">{tmpl_var name='limit_openvz_vm_txt'}</label>
+				<input name="limit_openvz_vm" id="limit_openvz_vm" value="{tmpl_var name='limit_openvz_vm'}" size="10" maxlength="10" type="text" class="textInput formLengthLimit" />
+			</div>
+			<div class="ctrlHolder">
+				<label for="limit_openvz_vm_template_id">{tmpl_var name='limit_openvz_vm_template_id_txt'}</label>
+				<select name="limit_openvz_vm_template_id" id="limit_openvz_vm_template_id" class="selectInput">
+					{tmpl_var name='limit_openvz_vm_template_id'}
+				</select>
+			</div>
 		</fieldset>
 
 		<input type="hidden" name="id" value="{tmpl_var name='id'}">
diff --git a/interface/web/client/templates/reseller_edit_limits.htm b/interface/web/client/templates/reseller_edit_limits.htm
index 394985129d..1e3151015e 100644
--- a/interface/web/client/templates/reseller_edit_limits.htm
+++ b/interface/web/client/templates/reseller_edit_limits.htm
@@ -182,6 +182,16 @@
       	<label for="limit_traffic_quota">{tmpl_var name='limit_traffic_quota_txt'}</label>
         <input name="limit_traffic_quota" id="limit_traffic_quota" value="{tmpl_var name='limit_traffic_quota'}" size="10" maxlength="10" type="text" class="textInput formLengthLimit" />&nbsp;MB
 	  </div>
+	  <div class="ctrlHolder">
+				<label for="limit_openvz_vm">{tmpl_var name='limit_openvz_vm_txt'}</label>
+				<input name="limit_openvz_vm" id="limit_openvz_vm" value="{tmpl_var name='limit_openvz_vm'}" size="10" maxlength="10" type="text" class="textInput formLengthLimit" />
+			</div>
+			<div class="ctrlHolder">
+				<label for="limit_openvz_vm_template_id">{tmpl_var name='limit_openvz_vm_template_id_txt'}</label>
+				<select name="limit_openvz_vm_template_id" id="limit_openvz_vm_template_id" class="selectInput">
+					{tmpl_var name='limit_openvz_vm_template_id'}
+				</select>
+			</div>
     </fieldset>
 
     <input type="hidden" name="id" value="{tmpl_var name='id'}">
diff --git a/interface/web/vm/form/openvz_vm.tform.php b/interface/web/vm/form/openvz_vm.tform.php
index 3a93fb1f06..3d75e8464b 100644
--- a/interface/web/vm/form/openvz_vm.tform.php
+++ b/interface/web/vm/form/openvz_vm.tform.php
@@ -63,7 +63,7 @@ $form["tabs"]['main'] = array (
 			'formtype'	=> 'SELECT',
 			'default'	=> '',
 			'datasource'	=> array ( 	'type'	=> 'SQL',
-										'querystring' => 'SELECT server_id,server_name FROM server WHERE vserver_server = 1 AND mirror_server_id = 0 AND {AUTHSQL} ORDER BY server_name',
+										'querystring' => 'SELECT server_id,server_name FROM server WHERE vserver_server = 1 AND mirror_server_id = 0 ORDER BY server_name',
 										'keyfield'=> 'server_id',
 										'valuefield'=> 'server_name'
 									 ),
@@ -74,7 +74,7 @@ $form["tabs"]['main'] = array (
 			'formtype'	=> 'SELECT',
 			'default'	=> '',
 			'datasource'	=> array ( 	'type'	=> 'SQL',
-										'querystring' => 'SELECT ostemplate_id,template_name FROM openvz_ostemplate WHERE {AUTHSQL} ORDER BY template_name',
+										'querystring' => 'SELECT ostemplate_id,template_name FROM openvz_ostemplate WHERE 1 ORDER BY template_name',
 										'keyfield'=> 'ostemplate_id',
 										'valuefield'=> 'template_name'
 									 ),
@@ -84,11 +84,13 @@ $form["tabs"]['main'] = array (
 			'datatype'	=> 'INTEGER',
 			'formtype'	=> 'SELECT',
 			'default'	=> '',
+			/*
 			'datasource'	=> array ( 	'type'	=> 'SQL',
-										'querystring' => 'SELECT template_id,template_name FROM openvz_template WHERE {AUTHSQL} ORDER BY template_name',
+										'querystring' => 'SELECT template_id,template_name FROM openvz_template WHERE 1 ORDER BY template_name',
 										'keyfield'=> 'template_id',
 										'valuefield'=> 'template_name'
 									 ),
+			*/
 			'value'		=> ''
 		),
 		'ip_address' => array (
@@ -99,7 +101,7 @@ $form["tabs"]['main'] = array (
 									),
 			'default'	=> '',
 			'datasource'	=> array ( 	'type'	=> 'SQL',
-										'querystring' => "SELECT ip_address FROM openvz_ip WHERE reserved = 'n' AND (vm_id = 0 or vm_id = '{RECORDID}') AND {AUTHSQL} ORDER BY ip_address",
+										'querystring' => "SELECT ip_address FROM openvz_ip WHERE reserved = 'n' AND (vm_id = 0 or vm_id = '{RECORDID}') ORDER BY ip_address",
 										'keyfield'=> 'ip_address',
 										'valuefield'=> 'ip_address'
 									 ),
diff --git a/interface/web/vm/openvz_vm_edit.php b/interface/web/vm/openvz_vm_edit.php
index 65fbda3155..f79a90bd23 100644
--- a/interface/web/vm/openvz_vm_edit.php
+++ b/interface/web/vm/openvz_vm_edit.php
@@ -50,18 +50,53 @@ $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') {
+			if(!$app->tform->checkClientLimit('limit_openvz_vm')) {
+				$app->error($app->tform->wordbook["limit_openvz_vm_txt"]);
+			}
+			if(!$app->tform->checkResellerLimit('limit_openvz_vm')) {
+				$app->error('Reseller: '.$app->tform->wordbook["limit_openvz_vm_txt"]);
+			}
+		}
+		
+		parent::onShowNew();
+	}
+	
 	function onShowEnd() {
 		global $app, $conf;
 
 		//* Client: If the logged in user is not admin and has no sub clients (no rseller)
 		if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
-
+			
+			//* Get the limits of the client
+			$client_group_id = $_SESSION["s"]["user"]["default_group"];
+			$client = $app->db->queryOneRecord("SELECT client.client_id, client.contact_name, client.limit_openvz_vm_template_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+			
+			//* Fill the template_id field
+			if($client['limit_openvz_vm_template_id'] == 0) {
+				$sql = 'SELECT template_id,template_name FROM openvz_template WHERE 1 ORDER BY template_name';
+			} else {
+				$sql = 'SELECT template_id,template_name FROM openvz_template WHERE template_id = '.$client['limit_openvz_vm_template_id'].' ORDER BY template_name';
+			}
+			$records = $app->db->queryAllRecords($sql);
+			if(is_array($records)) {
+				foreach( $records as $rec) {
+					$selected = @($rec["template_id"] == $this->dataRecord["template_id"])?'SELECTED':'';
+					$template_id_select .= "<option value='$rec[template_id]' $selected>$rec[template_name]</option>\r\n";
+				}
+			}
+			$app->tpl->setVar("template_id_select",$template_id_select);
+			
 			//* Reseller: If the logged in user is not admin and has sub clients (is a rseller)
 		} elseif ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
 			
 			//* Get the limits of the client
 			$client_group_id = $_SESSION["s"]["user"]["default_group"];
-			$client = $app->db->queryOneRecord("SELECT client.client_id, client.contact_name FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+			$client = $app->db->queryOneRecord("SELECT client.client_id, client.contact_name, client.limit_openvz_vm_template_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
 			
 			
 			//* Fill the client select field
@@ -77,6 +112,21 @@ class page_action extends tform_actions {
 				}
 			}
 			$app->tpl->setVar("client_group_id",$client_select);
+			
+			//* Fill the template_id field
+			if($client['limit_openvz_vm_template_id'] == 0) {
+				$sql = 'SELECT template_id,template_name FROM openvz_template WHERE 1 ORDER BY template_name';
+			} else {
+				$sql = 'SELECT template_id,template_name FROM openvz_template WHERE template_id = '.$client['limit_openvz_vm_template_id'].' ORDER BY template_name';
+			}
+			$records = $app->db->queryAllRecords($sql);
+			if(is_array($records)) {
+				foreach( $records as $rec) {
+					$selected = @($rec["template_id"] == $this->dataRecord["template_id"])?'SELECTED':'';
+					$template_id_select .= "<option value='$rec[template_id]' $selected>$rec[template_name]</option>\r\n";
+				}
+			}
+			$app->tpl->setVar("template_id_select",$template_id_select);
 
 			//* Admin: If the logged in user is admin
 		} else {
@@ -93,6 +143,17 @@ class page_action extends tform_actions {
 				}
 			}
 			$app->tpl->setVar("client_group_id",$client_select);
+			
+			//* Fill the template_id field
+			$sql = 'SELECT template_id,template_name FROM openvz_template WHERE 1 ORDER BY template_name';
+			$records = $app->db->queryAllRecords($sql);
+			if(is_array($records)) {
+				foreach( $records as $rec) {
+					$selected = @($rec["template_id"] == $this->dataRecord["template_id"])?'SELECTED':'';
+					$template_id_select .= "<option value='$rec[template_id]' $selected>$rec[template_name]</option>\r\n";
+				}
+			}
+			$app->tpl->setVar("template_id_select",$template_id_select);
 
 		}
 		
diff --git a/interface/web/vm/templates/openvz_vm_edit.htm b/interface/web/vm/templates/openvz_vm_edit.htm
index 2f8ac77a65..92ec046fd5 100644
--- a/interface/web/vm/templates/openvz_vm_edit.htm
+++ b/interface/web/vm/templates/openvz_vm_edit.htm
@@ -54,7 +54,7 @@
       <div class="ctrlHolder">
       	<label for="template_id">{tmpl_var name='template_id_txt'}</label>
         <select name="template_id" id="template_id" class="selectInput">
-					{tmpl_var name='template_id'}
+					{tmpl_var name='template_id_select'}
 				</select>
       </div>
       <div class="ctrlHolder">
-- 
GitLab