From 46598e0a153559c5bb1ecddecd320c7c67f2c04f Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Fri, 22 Jul 2011 09:48:21 +0000
Subject: [PATCH] Added VM actions start, stop, restart and creating of
 ostemplates.

---
 interface/lib/classes/functions.inc.php       |   2 +-
 .../lib/plugins/vm_openvz_plugin.inc.php      | 233 ++++++++++++++++++
 .../web/tools/templates/user_settings.htm     |   8 -
 .../web/vm/lib/lang/en_openvz_action.lng      |  17 ++
 interface/web/vm/openvz_action.php            | 149 +++++++++++
 interface/web/vm/openvz_vm_edit.php           | 203 +--------------
 interface/web/vm/templates/openvz_action.htm  |  54 ++++
 interface/web/vm/templates/openvz_vm_list.htm |   3 +-
 .../remoteaction_core_module.inc.php          |  35 +++
 9 files changed, 495 insertions(+), 209 deletions(-)
 create mode 100644 interface/lib/plugins/vm_openvz_plugin.inc.php
 create mode 100644 interface/web/vm/lib/lang/en_openvz_action.lng
 create mode 100644 interface/web/vm/openvz_action.php
 create mode 100644 interface/web/vm/templates/openvz_action.htm

diff --git a/interface/lib/classes/functions.inc.php b/interface/lib/classes/functions.inc.php
index 8071429b7..21029929d 100644
--- a/interface/lib/classes/functions.inc.php
+++ b/interface/lib/classes/functions.inc.php
@@ -52,7 +52,7 @@ class functions {
 				unset($path_parts);
 			}
 
-			$header = "From: $from\nReply-To: $from\n";
+			$header = "Return-Path: $form\nFrom: $from\nReply-To: $from\n";
 			$header .= "MIME-Version: 1.0\n";
 			$header .= "Content-Type: multipart/mixed; boundary=$uid\n";
 
diff --git a/interface/lib/plugins/vm_openvz_plugin.inc.php b/interface/lib/plugins/vm_openvz_plugin.inc.php
new file mode 100644
index 000000000..1112e081c
--- /dev/null
+++ b/interface/lib/plugins/vm_openvz_plugin.inc.php
@@ -0,0 +1,233 @@
+<?php
+/**
+ * sites_web_domain_plugin plugin
+ * 
+ * @author Till Brehm, projektfarm GmbH
+ */
+ 
+class vm_openvz_plugin {
+
+	var $plugin_name        = 'vm_openvz_plugin';
+	var $class_name         = 'vm_openvz_plugin';
+	var $id = 0;
+	var $dataRecord = array();
+	var $oldDataRecord = array();
+
+
+    /*
+            This function is called when the plugin is loaded
+    */
+    function onLoad() {
+        global $app;
+        
+		//* Register for events        
+        $app->plugin->registerEvent('vm:openvz_vm:on_after_insert','vm_openvz_plugin','openvz_vm_insert');
+		$app->plugin->registerEvent('vm:openvz_vm:on_after_update','vm_openvz_plugin','openvz_vm_update');
+    }
+
+    /*
+		Function that gets called after a new vm was inserted           
+    */
+    function openvz_vm_insert($event_name, $page_form) {
+        global $app, $conf;  
+
+		$this->id = $page_form->id;
+		$this->dataRecord = $page_form->dataRecord;	
+		$this->oldDataRecord = $page_form->oldDataRecord;	
+        
+		// make sure that the record belongs to the clinet group and not the admin group when admin 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 openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
+		}
+		if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
+			$client_group_id = intval($this->dataRecord["client_group_id"]);
+			$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
+		}
+		
+		// Set the VEID
+		$tmp = $app->db->queryOneRecord('SELECT MAX(veid) + 1 as newveid FROM openvz_vm');
+		$veid = ($tmp['newveid'] > 100)?$tmp['newveid']:101;
+		$app->db->query("UPDATE openvz_vm SET veid = ".$veid." WHERE vm_id = ".$this->id);
+		unset($tmp);
+		
+		// Apply template values to the advanced tab settings
+		$this->applyTemplate();
+		
+		// Set the IP address
+		$app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
+		
+		// Create the OpenVZ config file and store it in config field
+		$this->makeOpenVZConfig();
+		
+		// Create the DNS record
+		$this->createDNS();
+		
+	}
+	
+	/*
+		Function that gets called after a vm was updated          
+    */
+    function openvz_vm_update($event_name, $page_form) {
+        global $app, $conf;
+		
+		$this->id = $page_form->id;
+		$this->dataRecord = $page_form->dataRecord;
+		$this->oldDataRecord = $page_form->oldDataRecord;	
+		
+		// make sure that the record belongs to the clinet group and not the admin group when a admin 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 openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
+		}
+		if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
+			$client_group_id = intval($this->dataRecord["client_group_id"]);
+			$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
+		}
+		
+		if(isset($this->dataRecord["ostemplate_id"]) && $this->oldDataRecord["ostemplate_id"] != $this->dataRecord["ostemplate_id"]) {
+			$this->applyTemplate();
+		}
+		
+		// Set the IP address
+		if(isset($this->dataRecord['ip_address'])) $app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
+		
+		// Create the OpenVZ config file and store it in config field
+		$this->makeOpenVZConfig();
+		
+		// Create the DNS record
+		if((isset($this->dataRecord['hostname']) && $this->dataRecord['hostname'] != $this->oldDataRecord['hostname']) 
+		or (isset($this->dataRecord['create_dns']) && $this->dataRecord['create_dns'] != $this->oldDataRecord['create_dns'])) {
+			$this->createDNS();
+		}
+        
+	}
+	
+	private function applyTemplate() {
+		global $app, $conf;
+		
+		$tpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$this->dataRecord["template_id"]);
+		
+		$sql = "UPDATE openvz_vm SET ";
+		$sql .= "diskspace = '".$tpl['diskspace']."', ";
+		$sql .= "ram = '".$tpl['ram']."', ";
+		$sql .= "ram_burst = '".$tpl['ram_burst']."', ";
+		$sql .= "cpu_units = '".$tpl['cpu_units']."', ";
+		$sql .= "cpu_num = '".$tpl['cpu_num']."', ";
+		$sql .= "cpu_limit = '".$tpl['cpu_limit']."', ";
+		$sql .= "io_priority = '".$tpl['io_priority']."', ";
+		$sql .= "nameserver = '".$tpl['nameserver']."', ";
+		$sql .= "create_dns = '".$tpl['create_dns']."', ";
+		$sql .= "capability = '".$tpl['capability']."' ";
+		$sql .= "WHERE vm_id = ".$this->id;
+		$app->db->query($sql);
+		
+	}
+	
+	private function makeOpenVZConfig() {
+		global $app, $conf;
+		
+		$vm = $app->tform->getDataRecord($this->id);
+		$vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$vm['template_id']);
+		$burst_ram = $vm['ram_burst']*256;
+		$guar_ram = $vm['ram']*256;
+		
+		$tpl = new tpl();
+		$tpl->newTemplate('templates/openvz.conf.tpl');
+		
+		$onboot = ($vm['start_boot'] == 'y')?'yes':'no';
+		$tpl->setVar('onboot',$onboot);
+		
+		$tpl->setVar('kmemsize',$vm_template['kmemsize']);
+		$tpl->setVar('lockedpages',$vm_template['lockedpages']);
+		$tpl->setVar('privvmpages',$burst_ram.':'.$burst_ram);
+		$tpl->setVar('shmpages',$guar_ram.':'.$guar_ram);
+		$tpl->setVar('numproc',$vm_template['numproc']);
+		$tpl->setVar('physpages',$vm_template['physpages']);
+		$tpl->setVar('vmguarpages',$guar_ram.':'.$guar_ram);
+		$tpl->setVar('oomguarpages',$guar_ram.':'.$guar_ram);
+		$tpl->setVar('numtcpsock',$vm_template['numtcpsock']);
+		$tpl->setVar('numflock',$vm_template['numflock']);
+		$tpl->setVar('numpty',$vm_template['numpty']);
+		$tpl->setVar('numsiginfo',$vm_template['numsiginfo']);
+		$tpl->setVar('tcpsndbuf',$vm_template['tcpsndbuf']);
+		$tpl->setVar('tcprcvbuf',$vm_template['tcprcvbuf']);
+		$tpl->setVar('othersockbuf',$vm_template['othersockbuf']);
+		$tpl->setVar('dgramrcvbuf',$vm_template['dgramrcvbuf']);
+		$tpl->setVar('numothersock',$vm_template['numothersock']);
+		$tpl->setVar('dcachesize',$vm_template['dcachesize']);
+		$tpl->setVar('numfile',$vm_template['numfile']);
+		$tpl->setVar('avnumproc',$vm_template['avnumproc']);
+		$tpl->setVar('numiptent',$vm_template['numiptent']);
+		
+		$diskspace = $vm['diskspace']*1048576;
+		$diskinodes = $vm['diskspace']*524288;
+		
+		$tpl->setVar('diskspace',$diskspace.":".$diskspace);
+		$tpl->setVar('diskinodes',$diskinodes.":".$diskinodes);
+		$tpl->setVar('io_priority',$vm['io_priority']);
+		
+		$tpl->setVar('cpu_num',$vm['cpu_num']);
+		$tpl->setVar('cpu_units',$vm['cpu_units']);
+		$tpl->setVar('cpu_limit',$vm['cpu_limit']);
+		
+		$hostname = str_replace('{VEID}',$vm['veid'],$vm['hostname']);
+		
+		$tpl->setVar('hostname',$hostname);
+		$tpl->setVar('ip_address',$vm['ip_address']);
+		$tpl->setVar('nameserver',$vm['nameserver']);
+		$tpl->setVar('capability',$vm['capability']);
+		
+		$tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ".$vm['ostemplate_id']);
+		$tpl->setVar('ostemplate',$tmp['template_file']);
+		unset($tmp);
+		
+		$openvz_config = $app->db->quote($tpl->grab());
+		$app->db->query("UPDATE openvz_vm SET config = '".$openvz_config."' WHERE vm_id = ".$this->id);
+		
+		unset($tpl);
+		
+	}
+	
+	private function createDNS() {
+		global $app, $conf;
+		
+		$vm = $app->tform->getDataRecord($this->id);
+		
+		if($vm['create_dns'] != 'y') return;
+		
+		$full_hostname = str_replace('{VEID}',$vm['veid'],$vm['hostname']);
+		$hostname_parts = explode('.',$full_hostname);
+		$hostname = $hostname_parts[0];
+		unset($hostname_parts[0]);
+		$zone = implode('.',$hostname_parts);
+		unset($hostname_parts);
+		
+		// Find the dns zone
+		$zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = '$zone.'");
+		$rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = '".$zone_rec['id']."' AND name = '$hostname'");
+		
+		if($zone_rec['id'] > 0) {
+			$ip_address = $vm['ip_address'];
+			$sys_userid = $zone_rec['sys_userid'];
+			$sys_groupid = $zone_rec['sys_groupid'];
+			$server_id = $zone_rec['server_id'];
+			$dns_soa_id = $zone_rec['id'];
+			
+			if($rr_rec['id'] > 0) {
+				$app->uses('validate_dns');
+				$app->db->datalogUpdate('dns_rr', "data = '$ip_address'", 'id', $rr_rec['id']);
+				$serial = $app->validate_dns->increase_serial($zone_rec['serial']);
+				$app->db->datalogUpdate('dns_soa', "serial = '$serial'", 'id', $zone_rec['id']);
+			} else {
+				$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES 
+				('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$hostname', 'A', '$ip_address', '0', '3600', 'Y')";
+				$dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
+			}
+			
+		}
+	}
+
+}              	
\ No newline at end of file
diff --git a/interface/web/tools/templates/user_settings.htm b/interface/web/tools/templates/user_settings.htm
index 415aafc15..49c2a5584 100644
--- a/interface/web/tools/templates/user_settings.htm
+++ b/interface/web/tools/templates/user_settings.htm
@@ -24,14 +24,6 @@
 					{tmpl_var name='language'}
 				</select>
       </div>
-     <div class="ctrlHolder">
-      	<label for="id_rsa">{tmpl_var name='id_rsa_txt'}</label>
-        <textarea name="id_rsa" id="id_rsa" readonly rows='10' cols='30'>{tmpl_var name='id_rsa'}</textarea>
-      </div>
-      <div class="ctrlHolder">
-      	<label for="ssh_rsa">{tmpl_var name='ssh_rsa_txt'}</label>
-        <input name="ssh_rsa" id="ssh_rsa" value="{tmpl_var name='ssh_rsa'}" size="30" maxlength="600" type="text" class="textInput" />
-      </div>
 
     <input type="hidden" name="id" value="{tmpl_var name='id'}">
 
diff --git a/interface/web/vm/lib/lang/en_openvz_action.lng b/interface/web/vm/lib/lang/en_openvz_action.lng
new file mode 100644
index 000000000..9c5138dec
--- /dev/null
+++ b/interface/web/vm/lib/lang/en_openvz_action.lng
@@ -0,0 +1,17 @@
+<?php
+$wb['head_txt'] = 'Virtual server actions for VM:';
+$wb['start_txt'] = 'Start virtual server';
+$wb['stop_txt'] = 'Stop virtual server';
+$wb['restart_txt'] = 'Restart virtual server';
+$wb['ostemplate_txt'] = 'Create OSTemplate';
+$wb['ostemplate_desc_txt'] = '(example: debian-6.0-i386-custom)';
+$wb['btn_save_txt'] = 'Execute selected action';
+$wb['btn_cancel_txt'] = 'Cancel';
+$wb['start_exec_txt'] = 'Start command has been sent to the VM host server. It may take a minute until the VM is started.';
+$wb['stop_exec_txt'] = 'Stop command has been sent to the VM host server. It may take a minute until the VM is stopped.';
+$wb['restart_exec_txt'] = 'Restart command has been sent to the VM host server. It may take a minute until the VM is restarted.';
+$wb['ostemplate_name_error'] = 'The OSTemplate name conatains unallowed characters.';
+$wb['ostemplate_name_unique_error'] = 'There is already a OSTemplate with that name.';
+$wb['ostemplate_exec_txt'] = 'The command to create a OSTemplate has been sent to the host server. It will take several minutes until the OSTemplate has been created.';
+
+?>
diff --git a/interface/web/vm/openvz_action.php b/interface/web/vm/openvz_action.php
new file mode 100644
index 000000000..e1707da72
--- /dev/null
+++ b/interface/web/vm/openvz_action.php
@@ -0,0 +1,149 @@
+<?php
+/*
+Copyright (c) 2010, Till Brehm, projektfarm Gmbh
+All rights reserved.
+*/
+
+require_once('../../lib/config.inc.php');
+require_once('../../lib/app.inc.php');
+
+//* Check permissions for module
+$app->auth->check_module_permissions('vm');
+
+$action = (isset($_POST['action']) && $_POST['action'] != '')?$_POST['action']:'show';
+$vm_id = intval($_REQUEST['id']);
+$error_msg = '';
+$notify_msg = '';
+
+if($vm_id == 0) die('Invalid VM ID');
+
+$vm = $app->db->queryOneRecord("SELECT server_id, veid FROM openvz_vm WHERE vm_id = $vm_id");
+$veid = $vm['veid'];
+$server_id = $vm['server_id'];
+
+//* Loading classes
+$app->uses('tpl');
+
+$app->tpl->newTemplate('form.tpl.htm');
+$app->tpl->setInclude('content_tpl', 'templates/openvz_action.htm');
+	
+//* load language file 
+$lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_openvz_action.lng';
+include_once($lng_file);
+$app->tpl->setVar($wb);
+	
+$app->tpl->setVar('id',$vm_id);
+$app->tpl->setVar('veid',$veid);
+
+$options = array('start_option_enabled'=>'','stop_option_enabled'=>'','restart_option_enabled'=>'','ostemplate_option_enabled'=>'');
+
+
+//* Show the action select page
+if($action == 'show') {
+
+$options['start_option_enabled'] = 'checked="checked"';
+	
+} elseif ($action == 'start') {
+	
+	//* Start the virtual machine
+	$sql =  "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
+			"VALUES (".
+			(int)$server_id . ", ".
+			time() . ", ".
+			"'openvz_start_vm', ".
+			$veid.", ".
+			"'pending', ".
+			"''".
+			")";
+	$app->db->query($sql);
+	
+	$app->tpl->setVar('msg',$wb['start_exec_txt']);
+	$options['start_option_enabled'] = 'checked="checked"';
+	
+} elseif ($action == 'stop') {
+	
+	//* Stop the virtual machine
+	$sql =  "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
+			"VALUES (".
+			(int)$server_id . ", ".
+			time() . ", ".
+			"'openvz_stop_vm', ".
+			$veid.", ".
+			"'pending', ".
+			"''".
+			")";
+	$app->db->query($sql);
+	
+	$app->tpl->setVar('msg',$wb['stop_exec_txt']);
+	$options['stop_option_enabled'] = 'checked="checked"';
+
+} elseif ($action == 'restart') {
+	
+	//* Restart the virtual machine
+	$sql =  "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
+			"VALUES (".
+			(int)$server_id . ", ".
+			time() . ", ".
+			"'openvz_restart_vm', ".
+			$veid.", ".
+			"'pending', ".
+			"''".
+			")";
+	$app->db->query($sql);
+	
+	$app->tpl->setVar('msg',$wb['restart_exec_txt']);
+	$options['restart_option_enabled'] = 'checked="checked"';
+
+} elseif ($action == 'ostemplate') {
+	
+	$ostemplate_name = $_POST['ostemplate_name'];
+	
+	if(!preg_match("/^[a-zA-Z0-9\.\-\_]{1,50}$/i", $ostemplate_name)) {
+		$error_msg .= $wb['ostemplate_name_error'].'<br />';
+		$app->tpl->setVar('ostemplate_name',$ostemplate_name);
+	}
+	
+	//* Quote name
+	$ostemplate_name = $app->db->quote($ostemplate_name);
+	
+	//* Check for duplicates
+	$tmp = $app->db->queryOneRecord("SELECT count(ostemplate_id) as number FROM openvz_ostemplate WHERE template_file = '$ostemplate_name'");
+	if($tmp['number'] > 0) $error_msg .= $wb['ostemplate_name_unique_error'].'<br />';
+	unset($tmp);
+	
+	if($error_msg == '') {
+		//* Create ostemplate action
+		$sql =  "INSERT INTO sys_remoteaction (server_id, tstamp, action_type, action_param, action_state, response) " .
+				"VALUES (".
+				(int)$server_id . ", ".
+				time() . ", ".
+				"'openvz_create_ostpl', ".
+				"'".$veid.":".$ostemplate_name."', ".
+				"'pending', ".
+				"''".
+				")";
+		$app->db->query($sql);
+		
+		//* Create a record in the openvz_ostemplate table
+		$sql = "INSERT INTO `openvz_ostemplate` (`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `template_name`, `template_file`, `server_id`, `allservers`, `active`, `description`) 
+		VALUES(1, 1, 'riud', 'riud', '', '$ostemplate_name', '$ostemplate_name', $server_id, 'n', 'y', '')";
+		$app->db->query($sql);
+		
+		$app->tpl->setVar('msg',$wb['ostemplate_exec_txt']);
+		$options['ostemplate_option_enabled'] = 'checked="checked"';
+	}
+
+} else {
+	$error_msg = $app->lng('Unknown action');
+	$app->error($error_msg);
+}
+
+$app->tpl->setVar($options);
+$app->tpl->setVar('error',$error_msg);
+
+$app->tpl_defaults();
+$app->tpl->pparse();
+
+
+
+?>
\ No newline at end of file
diff --git a/interface/web/vm/openvz_vm_edit.php b/interface/web/vm/openvz_vm_edit.php
index bc0324907..65fbda315 100644
--- a/interface/web/vm/openvz_vm_edit.php
+++ b/interface/web/vm/openvz_vm_edit.php
@@ -59,12 +59,12 @@ class page_action extends tform_actions {
 			//* 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
+			//* 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");
 			
 			
-			// Fill the client select field
+			//* Fill the client select field
 			$sql = "SELECT groupid, name FROM sys_group, client WHERE sys_group.client_id = client.client_id AND client.parent_client_id = ".$client['client_id']." ORDER BY name";
 			$records = $app->db->queryAllRecords($sql);
 			$tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".$client['client_id']);
@@ -81,7 +81,7 @@ class page_action extends tform_actions {
 			//* Admin: If the logged in user is admin
 		} else {
 
-			// Fill the client select field
+			//* Fill the client select field
 			$sql = "SELECT groupid, name FROM sys_group WHERE client_id > 0 ORDER BY name";
 			$clients = $app->db->queryAllRecords($sql);
 			$client_select = "<option value='0'></option>";
@@ -111,207 +111,12 @@ class page_action extends tform_actions {
 	function onSubmit() {
 		global $app, $conf;
 
-		// Clients may not set the client_group_id, so we unset them if user is not a admin and the client is not a reseller
+		//* Clients may not set the client_group_id, so we unset them if user is not a admin and the client is not a reseller
 		if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) 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 admin 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 openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
-		}
-		if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
-			$client_group_id = intval($this->dataRecord["client_group_id"]);
-			$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
-		}
-		
-		// Set the VEID
-		$tmp = $app->db->queryOneRecord('SELECT MAX(veid) + 1 as newveid FROM openvz_vm');
-		$veid = ($tmp['newveid'] > 100)?$tmp['newveid']:101;
-		$app->db->query("UPDATE openvz_vm SET veid = ".$veid." WHERE vm_id = ".$this->id);
-		unset($tmp);
-		
-		// Apply template values to the advanced tab settings
-		$this->applyTemplate();
-		
-		// Set the IP address
-		$app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
-		
-		// Create the OpenVZ config file and store it in config field
-		$this->makeOpenVZConfig();
-		
-		// Create the DNS record
-		$this->createDNS();
-		
-	}
-
-	function onAfterUpdate() {
-		global $app, $conf;
-
-		// make sure that the record belongs to the clinet group and not the admin group when a admin 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 openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
-		}
-		if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
-			$client_group_id = intval($this->dataRecord["client_group_id"]);
-			$app->db->query("UPDATE openvz_vm SET sys_groupid = $client_group_id WHERE vm_id = ".$this->id);
-		}
-		
-		if(isset($this->dataRecord["ostemplate_id"]) && $this->oldDataRecord["ostemplate_id"] != $this->dataRecord["ostemplate_id"]) {
-			$this->applyTemplate();
-		}
-		
-		// Set the IP address
-		if(isset($this->dataRecord['ip_address'])) $app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
-		
-		// Create the OpenVZ config file and store it in config field
-		$this->makeOpenVZConfig();
-		
-		// Create the DNS record
-		if((isset($this->dataRecord['hostname']) && $this->dataRecord['hostname'] != $this->oldDataRecord['hostname']) 
-		or (isset($this->dataRecord['create_dns']) && $this->dataRecord['create_dns'] != $this->oldDataRecord['create_dns'])) {
-			$this->createDNS();
-		}
-		
-	}
-	
-	function applyTemplate() {
-		global $app, $conf;
-		
-		$tpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$this->dataRecord["template_id"]);
-		
-		$sql = "UPDATE openvz_vm SET ";
-		$sql .= "diskspace = '".$tpl['diskspace']."', ";
-		$sql .= "ram = '".$tpl['ram']."', ";
-		$sql .= "ram_burst = '".$tpl['ram_burst']."', ";
-		$sql .= "cpu_units = '".$tpl['cpu_units']."', ";
-		$sql .= "cpu_num = '".$tpl['cpu_num']."', ";
-		$sql .= "cpu_limit = '".$tpl['cpu_limit']."', ";
-		$sql .= "io_priority = '".$tpl['io_priority']."', ";
-		$sql .= "nameserver = '".$tpl['nameserver']."', ";
-		$sql .= "create_dns = '".$tpl['create_dns']."', ";
-		$sql .= "capability = '".$tpl['capability']."' ";
-		$sql .= "WHERE vm_id = ".$this->id;
-		$app->db->query($sql);
-		
-	}
-	
-	function makeOpenVZConfig() {
-		global $app, $conf;
-		
-		$vm = $app->tform->getDataRecord($this->id);
-		$vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$vm['template_id']);
-		$burst_ram = $vm['ram_burst']*256;
-		$guar_ram = $vm['ram']*256;
-		
-		$tpl = new tpl();
-		$tpl->newTemplate('templates/openvz.conf.tpl');
-		
-		$onboot = ($vm['start_boot'] == 'y')?'yes':'no';
-		$tpl->setVar('onboot',$onboot);
-		
-		$tpl->setVar('kmemsize',$vm_template['kmemsize']);
-		$tpl->setVar('lockedpages',$vm_template['lockedpages']);
-		$tpl->setVar('privvmpages',$burst_ram.':'.$burst_ram);
-		$tpl->setVar('shmpages',$guar_ram.':'.$guar_ram);
-		$tpl->setVar('numproc',$vm_template['numproc']);
-		$tpl->setVar('physpages',$vm_template['physpages']);
-		$tpl->setVar('vmguarpages',$guar_ram.':'.$guar_ram);
-		$tpl->setVar('oomguarpages',$guar_ram.':'.$guar_ram);
-		$tpl->setVar('numtcpsock',$vm_template['numtcpsock']);
-		$tpl->setVar('numflock',$vm_template['numflock']);
-		$tpl->setVar('numpty',$vm_template['numpty']);
-		$tpl->setVar('numsiginfo',$vm_template['numsiginfo']);
-		$tpl->setVar('tcpsndbuf',$vm_template['tcpsndbuf']);
-		$tpl->setVar('tcprcvbuf',$vm_template['tcprcvbuf']);
-		$tpl->setVar('othersockbuf',$vm_template['othersockbuf']);
-		$tpl->setVar('dgramrcvbuf',$vm_template['dgramrcvbuf']);
-		$tpl->setVar('numothersock',$vm_template['numothersock']);
-		$tpl->setVar('dcachesize',$vm_template['dcachesize']);
-		$tpl->setVar('numfile',$vm_template['numfile']);
-		$tpl->setVar('avnumproc',$vm_template['avnumproc']);
-		$tpl->setVar('numiptent',$vm_template['numiptent']);
-		
-		$diskspace = $vm['diskspace']*1048576;
-		$diskinodes = $vm['diskspace']*524288;
-		
-		$tpl->setVar('diskspace',$diskspace.":".$diskspace);
-		$tpl->setVar('diskinodes',$diskinodes.":".$diskinodes);
-		$tpl->setVar('io_priority',$vm['io_priority']);
-		
-		$tpl->setVar('cpu_num',$vm['cpu_num']);
-		$tpl->setVar('cpu_units',$vm['cpu_units']);
-		$tpl->setVar('cpu_limit',$vm['cpu_limit']);
-		
-		$hostname = str_replace('{VEID}',$vm['veid'],$vm['hostname']);
-		
-		$tpl->setVar('hostname',$hostname);
-		$tpl->setVar('ip_address',$vm['ip_address']);
-		$tpl->setVar('nameserver',$vm['nameserver']);
-		$tpl->setVar('capability',$vm['capability']);
-		
-		$tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ".$vm['ostemplate_id']);
-		$tpl->setVar('ostemplate',$tmp['template_file']);
-		unset($tmp);
-		
-		$openvz_config = $app->db->quote($tpl->grab());
-		$app->db->query("UPDATE openvz_vm SET config = '".$openvz_config."' WHERE vm_id = ".$this->id);
-		
-		unset($tpl);
-		
-	}
-	
-	function createDNS() {
-		global $app, $conf;
-		
-		$vm = $app->tform->getDataRecord($this->id);
-		
-		if($vm['create_dns'] != 'y') return;
-		
-		$full_hostname = str_replace('{VEID}',$vm['veid'],$vm['hostname']);
-		$hostname_parts = explode('.',$full_hostname);
-		$hostname = $hostname_parts[0];
-		unset($hostname_parts[0]);
-		$zone = implode('.',$hostname_parts);
-		unset($hostname_parts);
-		
-		// Find the dns zone
-		$zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = '$zone.'");
-		$rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = '".$zone_rec['id']."' AND name = '$hostname'");
-		
-		if($zone_rec['id'] > 0) {
-			$ip_address = $vm['ip_address'];
-			$sys_userid = $zone_rec['sys_userid'];
-			$sys_groupid = $zone_rec['sys_groupid'];
-			$server_id = $zone_rec['server_id'];
-			$dns_soa_id = $zone_rec['id'];
-			
-			if($rr_rec['id'] > 0) {
-				$app->uses('validate_dns');
-				$app->db->datalogUpdate('dns_rr', "data = '$ip_address'", 'id', $rr_rec['id']);
-				$serial = $app->validate_dns->increase_serial($zone_rec['serial']);
-				$app->db->datalogUpdate('dns_soa', "serial = '$serial'", 'id', $zone_rec['id']);
-			} else {
-				$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES 
-				('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$hostname', 'A', '$ip_address', '0', '3600', 'Y')";
-				$dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
-			}
-			
-		}
-		
-		
-		
-		
-	}
-
 }
 
 $page = new page_action;
diff --git a/interface/web/vm/templates/openvz_action.htm b/interface/web/vm/templates/openvz_action.htm
new file mode 100644
index 000000000..042bca838
--- /dev/null
+++ b/interface/web/vm/templates/openvz_action.htm
@@ -0,0 +1,54 @@
+<h2><tmpl_var name="list_head_txt"></h2>
+<p><tmpl_var name="list_desc_txt"></p>
+
+<div class="panel panel_language_import">
+  
+  <div class="pnl_formsarea">
+    <fieldset class="inlineLabels"><legend>{tmpl_var name="head_txt"} {tmpl_var name='veid'}</legend>
+	  
+	  <tmpl_if name="msg">
+	<div id="OKMsg"><p><tmpl_var name="msg"></p></div>
+  </tmpl_if>
+  <tmpl_if name="error">
+	<div id="errorMsg"><h3>ERROR</h3><ol><tmpl_var name="error"></ol></div>
+  </tmpl_if>
+	  
+	  <div class="ctrlHolder">
+	    <p class="label">{tmpl_var name='start_txt'}</p>
+		<div class="multiField">
+		  <input id="action" type="radio" value="start" name="action" {tmpl_var name='start_option_enabled'}/>
+		</div>
+	  </div>
+	  
+	  <div class="ctrlHolder">
+	    <p class="label">{tmpl_var name='stop_txt'}</p>
+		<div class="multiField">
+		  <input id="action" type="radio" value="stop" name="action" {tmpl_var name='stop_option_enabled'}/>
+		</div>
+	  </div>
+	  
+	  <div class="ctrlHolder">
+	    <p class="label">{tmpl_var name='restart_txt'}</p>
+		<div class="multiField">
+		  <input id="action" type="radio" value="restart" name="action" {tmpl_var name='restart_option_enabled'}/>
+		</div>
+	  </div>
+	  
+	  <div class="ctrlHolder">
+	    <p class="label">{tmpl_var name='ostemplate_txt'}</p>
+		<div class="multiField">
+		  <input style="float:left; margin-right:10px;" id="action" type="radio" value="ostemplate" name="action" {tmpl_var name='ostemplate_option_enabled'}/>
+		  <input name="ostemplate_name" size="30" maxlength="255" type="text" class="textInput" value="{tmpl_var name='ostemplate_name'}" /> &nbsp;{tmpl_var name='ostemplate_desc_txt'}
+		</div>
+	  </div>
+
+    <input type="hidden" name="id" value="{tmpl_var name='id'}">
+
+    <div class="buttonHolder buttons">
+      <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','vm/openvz_action.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
+      <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('vm/openvz_vm_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
+    </div>
+	</fieldset>
+  </div>
+  
+</div>
diff --git a/interface/web/vm/templates/openvz_vm_list.htm b/interface/web/vm/templates/openvz_vm_list.htm
index 6eba7cfad..08bfab4af 100644
--- a/interface/web/vm/templates/openvz_vm_list.htm
+++ b/interface/web/vm/templates/openvz_vm_list.htm
@@ -49,7 +49,8 @@
             <td class="tbl_col_ip_address"><a href="#" onClick="loadContent('vm/openvz_vm_edit.php?id={tmpl_var name='id'}');">{tmpl_var name="ip_address"}</a></td>
             <td class="tbl_col_buttons">
               <div class="buttons icons16">    
-                <a class="icons16 icoDelete" href="javascript: del_record('vm/openvz_vm_del.php?id={tmpl_var name='id'}&phpsessid={tmpl_var name='phpsessid'}','{tmpl_var name='delete_confirmation'}');"><span>{tmpl_var name='delete_txt'}</span></a>
+                <a class="icons16" href="#" onClick="loadContent('vm/openvz_action.php?id={tmpl_var name='id'}')" style="background-image: url('themes/default/icons/x16/arrow.png');"><span>&nbsp;</span></a>
+				<a class="icons16 icoDelete" href="javascript: del_record('vm/openvz_vm_del.php?id={tmpl_var name='id'}&phpsessid={tmpl_var name='phpsessid'}','{tmpl_var name='delete_confirmation'}');"><span>{tmpl_var name='delete_txt'}</span></a>
               </div>
             </td>
           </tr>
diff --git a/server/mods-available/remoteaction_core_module.inc.php b/server/mods-available/remoteaction_core_module.inc.php
index 6bf3ad398..678b4befa 100644
--- a/server/mods-available/remoteaction_core_module.inc.php
+++ b/server/mods-available/remoteaction_core_module.inc.php
@@ -126,6 +126,41 @@ class remoteaction_core_module {
 					* we stop executing the actions not to waste more time */
 					return;
 				}
+				if ($action['action_type'] == 'openvz_start_vm') {
+					$veid = intval($action['action_param']);
+					if($veid > 0) {
+						exec("vzctl start $veid");
+					}
+				}
+				if ($action['action_type'] == 'openvz_stop_vm') {
+					$veid = intval($action['action_param']);
+					if($veid > 0) {
+						exec("vzctl stop $veid");
+					}
+				}
+				if ($action['action_type'] == 'openvz_restart_vm') {
+					$veid = intval($action['action_param']);
+					if($veid > 0) {
+						exec("vzctl restart $veid");
+					}
+				}
+				if ($action['action_type'] == 'openvz_create_ostpl') {
+					$parts = explode(':',$action['action_param']);
+					$veid = intval($parts[0]);
+					$template_cache_dir = '/vz/template/cache/';
+					$template_name = escapeshellcmd($parts[1]);
+					if($veid > 0 && $template_name != '' && is_dir($template_cache_dir)) {
+						$command = "vzdump --suspend --compress --stdexcludes --dumpdir $template_cache_dir $veid";
+						exec($command);
+						exec("mv ".$template_cache_dir."vzdump-openvz-".$veid."*.tgz ".$template_cache_dir.$template_name.".tar.gz");
+						exec("rm -f ".$template_cache_dir."vzdump-openvz-".$veid."*.log");
+					}
+					/* this action takes so much time,
+					* we stop executing the actions not to waste more time */
+					return;
+				}
+				
+				
 			}
 		}
 	}
-- 
GitLab