<?php

//////////////////////////////////////////////////////////////
//===========================================================
// addtemplate_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       8th Mar 2010
// Time:       23:00 hrs
// Site:       https://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at https://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('VIRTUALIZOR')){

	die('Hacking Attempt');

}

function addtemplate_theme(){

global $theme, $globals, $kernel, $user, $l, $oslist , $error, $done, $mgs, $distros;

softheader($l['<title>']);
					
echo page_header(__('Add OS Template'), 'fas fa-compact-disc',$globals['docs'].'add-os-templates/', 0, 0, '');

$breadcrumbs =['dashboard', 'lm_ostemplates', 'lms_addtemplate'];

echo get_breadcrumb($breadcrumbs);

error_handle($error);

if(!empty($done)){
	show_alert(__('The OS has been saved. Return to <a href="index.php?act=ostemplates">OS Templates Overview</a>'));
}

echo '<script language="javascript" type="text/javascript">

function validurl(url){
	var tmp = url.replace(/^.*[\/\\\\]/g, \'\');
	choosetemplate($_("template").value);
};

function changeplan(plan){	
	$_("openvz").className = "";
	$_("xen").className = "";
	$_("xenhvm").className = "";
	$_("kvm").className = "";
	$_("xcp").className = "";
	$_("xcphvm").className = "";
	$_("lxc").className = "";
	$_("vzo").className = "";
	$_("vzk").className = "";
	$_("proxo").className = "";
	$_("proxl").className = "";
	$_("proxk").className = "";
	$_("cplan").value = plan;
	
	$_("openvzos").style.display = "none";
	$_("xenos").style.display = "none";
	$_("xenhvmos").style.display = "none";
	$_("xcpos").style.display = "none";
	$_("xcphvmos").style.display = "none";
	$_("kvmos").style.display = "none";
	$_("lxcos").style.display = "none";
	$_("vzoos").style.display = "none";
	$_("vzkos").style.display = "none";
	$_("proxoos").style.display = "none";
	$_("proxlos").style.display = "none";
	$_("proxkos").style.display = "none";
	$_(plan+"os").style.display = "";
	
	$_("pygrub").style.display = "none";
	$_("extrac").style.display = "none";
	$_("drive").style.display = "none";
	$_("fstype").style.display = "none";
	$_("hvm_pass").style.display = "none";
	
	$_("noresizefs").style.display = "none";
	$_("perf_ops").style.display = "none";
	$_("template_admin_name").style.display = "none";
	
	if(plan == "xen"){
		$_("pygrub").style.display = "";
		$_("extrac").style.display = "";
		$_("drive").style.display = "";
		$_("fstype").style.display = "";
	}else if(plan == "xenhvm" || plan == "kvm" || plan == "vzk" || plan == "proxk"){
		//$_("extrac").style.display = "";
		$_("noresizefs").style.display = "";
		$_("perf_ops").style.display = "";
	}else if(plan == "xcp" || plan == "xcphvm"){
		$_("noresizefs").style.display = "";
		$_("perf_ops").style.display = "";
	}
	
	if(plan == "kvm" || plan == "xenhvm" || plan == "xcphvm" || plan == "vzk" || plan == "proxk"){
		$("#temp_windows").css("display", "");
	}else{
		$("#temp_windows").css("display", "none");
	}
	
	password_visible(plan,"");
	
	$("#mgs option").each(function(){
		if($(this).attr("type") != plan){
			$(this).attr("disabled","disabled");
		}else{
			$(this).attr("disabled", false);
		}
	});
	
};

function choosetemplate(template){
	$_("hvm_pass").style.display = "none";
	$_("template_admin_name").style.display = "none";
	
	if (template != ""){
		$_("template").value = template;
	}
	
	if(template == "windows"){
		$_("template_admin_name").style.display = "";
	}
	
	password_visible("",template);
	
	var tmp = $_("fileurl").value.replace(/^.*[\/\\\\]/g, \'\');
	if(tmp.match("^"+template)){
		$_("base").value = tmp;
	}else{
		$_("base").value = template+"-"+tmp;
	}
};

function password_visible(plan,template){
	
	if(plan == ""){
		plan = $("#virt_dd").val();
	}
	if(template == ""){
		template = $("#template option:selected").attr("id");
	}
	
	if((template == "temp_others" || template == "others") && (plan == "xcphvm" || plan == "xenhvm" || plan == "kvm" || plan == "vzk" || plan == "proxk")){
		$_("hvm_pass").style.display = "";
	}
	
}

addonload("choosetemplate(\''.POSTval('template','centos').'\');");
addonload("changeplan(\''.POSTval('cplan', 'openvz').'\');");

</script>
';
	$virt_dd_arr = array('openvz' => 'Openvz',
						'xen' => 'XEN',
						'xenhvm' => 'XEN-HVM',
						'kvm' => 'KVM',
						'xcp' => 'XenServer',
						'xcphvm' => 'XenServer-HVM',
						'lxc' => 'LXC',
						'vzo' => 'Virtuozzo Openvz',
						'vzk' => 'Virtuozzo KVM',
						'proxo' => 'Proxmox OpenVZ',
						'proxk' => 'Proxmox KVM',
						'proxl' => 'Proxmox LXC');
echo '
<div id="form-container" class="card p-2 mx-3">
	<form accept-charset="'.$globals['charset'].'" id="addtemplate" name="addtemplate" method="post" action="" class="form-horizontal">
		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4">
				<label class="control-label">'.__('Virtualization Type').'</label>
			</div>
			<div class="col-sm-4">
				<input type="hidden" id="cplan" name="cplan" value="">
				<select class="custom-select" name="virt_dd" id="virt_dd" onchange="changeplan($(\'#virt_dd option:selected\').val());">';
					foreach($virt_dd_arr as $vk => $vv){
						echo '<option value="'.$vk.'" id="'.$vk.'" '.(ex_POSTselect('virt_dd', $vk)).'>'.$vv.'</option>';
					}
				echo '
				</select>
				<span class="help-block"></span>
			</div>
		</div>
		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4"><label class="control-label">OS Template</label></div>
			<div class="col-sm-4">
				<select id="template" class="custom-select" name="template" onchange="choosetemplate(this.value);" >';
				foreach($distros as $k => $v){
					echo '<option id="temp_'.$k.'" value="'.$k.'">'.$v['name'].'</option>';
				}
				echo '<option id="temp_others" value="others">Others</option>
				</select>
				<span class="help-block"></span>
			</div>
		</div>
		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4">
				<label class="control-label">'.__('URL').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('If you want this template to be downloaded from the Internet, please give the URL. <b>Otherwise</b> it will be assumed that the Template exists on the system.').'"><i class="fas text-primary fa-info-circle"></i></a></label>
			</div>
			<div class="col-sm-4">
				<input type="text" class="form-control" name="url" id="fileurl" size="40" onblur=validurl(this.value) value="'.POSTval('url', '').'" />
			</div>
		</div>
		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4">
				<label class="control-label">'.__('Filename').'</label><br>
				<span>
					<span id="openvzos">'.$globals['openvzos'].'</span>
					<span id="xenos">'.$globals['xenos'].'</span>
					<span id="xenhvmos">'.$globals['xenos'].'</span>
					<span id="xcpos">'.$globals['xcpos'].'</span>
					<span id="xcphvmos">'.$globals['xcpos'].'</span>
					<span id="kvmos">'.$globals['kvmos'].'</span>
					<span id="lxcos">'.$globals['lxcos'].'</span>
					<span id="vzoos">'.$globals['vzoos'].'</span>
					<span id="vzkos">'.$globals['vzkos'].'</span>
					<span id="proxoos">'.$globals['proxoos'].'</span>
					<span id="proxlos">'.$globals['proxlos'].'</span>
					<span id="proxkos">'.$globals['proxkos'].'</span>
					/
				</span>
			</div>
			<div class="col-xs-12 col-sm-4">	
				<input type="text" class="form-control" name="filename" id="base" size="20" value="'.POSTval('filename', '').'"/>			
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="template_admin_name">
			<div class="col-sm-4">
				<label class="control-label">'.__('Admin User Name').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Please specify an Admin username for windows if it is other than Administrator.').'"><i class="fas text-primary fa-info-circle"></i></a></label>
			</div>
			<div class="col-sm-4">
				<input type="text" class="form-control" name="template_admin_name"  size="40" />
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="hvm_pass">
			<div class="col-sm-4"><label class="control-label">'.__('Set password').'</label></div>
			<div class="col-sm-4">
				<input type="text" class="form-control" name="hvm_pass" size="40" value="'.POSTval('hvm_pass', '').'" />
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="pygrub">
			<div class="col-4"><label class="control-label">'.__('Pygrub').'</label></div>
			<div class="col-4">
				'.get_checkbox('pygrub').'
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="fstype">
			<div class="col-4">	
				<label class="control-label">'.__('Check to create Ext4 file system.').'&nbsp;<a class="info" data-placement="right" tooltip="'.__(' By default the File System type is <b>ext3</b>.').'"><i class="fas text-primary fa-info-circle"></i></a></label>
			</div>
			<div class="col-4">
				'.get_checkbox('fstype').'
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="drive">
			<div class="col-sm-4">
				<label class="control-label">'.__('Disk Name').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('By default the Drive is <b>sda</b>. But if the template requires e.g. <b>xvda</b> mounted then please specify <b>xvda</b>.').'"><i class="fas text-primary fa-info-circle"></i></a></label>
			</div>
			<div class="col-sm-4"><input type="text" class="form-control" name="drive" id="driveid" value="'.POSTval('drive', '').'" size="10" /></div>
		</div>
		<div class="row mx-auto w-100 my-3" id="noresizefs">
			<div class="col-4">
				<label class="control-label">'.__('Dont Resize Filesystem').'</label>
			</div>
			<div class="col-4">
				'.get_checkbox('noresizefs').'
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="perf_ops">
			<div class="col-4">
				<label class="control-label">'.__('Perform Operations').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('If checked operations like changing network settings, root password will be performed.').'"><i class="fas text-primary fa-info-circle"></i></a></label>
			</div>
			<div class="col-4">
				'.get_checkbox('perf_ops').'
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="extrac">
			<div class="col-sm-4">
				<label class="control-label">'.__('Extra').'</label>
			</div>
			<div class="col-sm-4">
				<textarea class="form-control" name="extra" rows="3" cols="30">'.POSTval('extra', '').'</textarea>
				<span class="help-block"></span>
			</div>
		</div>
		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4">
				<label class="control-label">'.__('Media Groups').'</label>
			</div>
			<div class="col-sm-4">
				<select class="form-control" name="mgs[]" multiple="multiple" id="mgs">';
					
					foreach($mgs as $mk => $mv){
						echo '<option value="'.$mk.'" type="'.$mv['mg_type'].'">'.$mv['mg_name'].'</option>';
					}
					
				echo '</select>
				<span class="help-block">&nbsp;</span>
			</div>
		</div>
		<br/>
		<center>
			<input type="submit" name="addtemplate" value="'.__('Add OS template').'" class="go_btn">
		</center>
	</form>
</div>';

softfooter();

}

?>