<?php

//////////////////////////////////////////////////////////////
//===========================================================
// createtemplate_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 createtemplate_theme(){

global $theme, $globals, $kernel, $user, $l, $info, $error, $vs, $oslist, $ostemplates, $oses, $done, $actid, $flag_createtemplate, $distros;

if($flag_createtemplate){
	echo $actid;
	$flag_createtemplate = 0;
	return;
}

softheader($l['<title>']);
					
if(optGET('done')){		
	echo __('The OS Template was created successfully');
	return;
}else{
	$GLOBALS['breadcrumbs'] = ['dashboard', 'lm_ostemplates', 'lms_createtemplate'];
	echo page_header(__('Create Template'), 'fas fa-compact-disc', $globals['docs'].'create-os-templates/', 0, 0, '');
}
	

error_handle($error);

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

	var actid = null;
	var progress = 0;
	var progress_update = "";
	
	$(document).ready(function(){
		progress_onload();
		$(".wiki_help").tipTip({delay:100, defaultPosition:"left"});
		$("#createtemplate-form").submit(function(){
		var params = $("#createtemplate-form").serialize();
		
		animate_task("'.__J('Create template task started').'");

			$.ajax({type: "POST",
				url: "'.$globals['index'].'jsnohf=1&act=createtemplate",
				data:params+"&createtemp=1",
				success:function(response){
					if(!isNaN(response)){
						actid = response;
						setTimeout("get_progress(\'createtemplate\')", 1000);
					}
					else{
						var parsed_HTML = $(response);
						var error_box = parsed_HTML.filter("#error_box").add(parsed_HTML.find("#error_box"));
						//$("#form-container").prepend(error_box);
						showalert(error_box.html(),"1");
						$("#form-container").show();
						$("#progress-cont").hide();
						error_box.html("");
						changeplan();
					}
				}
			});
			
			$("#error_box").remove();
			$("#form-container").hide();
			$("#progress-cont").show();
			$("#pbar").html("'.__J('Checking submitted data...').'" + " ( 0% ) ");
			window.scrollTo(0, 0);
			$("#progressbar").progressbar({value: 0});
			
			return false;
		});
		
	});

function changeplan(){
try{
	var vpsid = $_("vpsid").value;
	var plan = getAttributeByName($_("vs"+vpsid), "type");
	var distro = getAttributeByName($_("vs"+vpsid), "distro");
}catch(e){ };

	if(plan == "undefined" || !plan){
		plan = "xen";
	}
	
	if(distro == "undefined" || !distro){
		distro = "others";
	}
	
	$_("xen").style.display = "none";
	$_("xenhvm").style.display = "none";
	$_("kvm").style.display = "none";
	$_("xcp").style.display = "none";
	$_("xcphvm").style.display = "none";
	$_("lxc").style.display = "none";
	$_("vzo").style.display = "none";
	$_("vzk").style.display = "none";
	$_("proxl").style.display = "none";
	$_("proxk").style.display = "none";
	$_(plan).style.display = "";	
	
	$("[id^=\'temp_\']").each(function() {
		$(this).hide();
	});		
	$_("temp_"+distro).style.display = "";	
	
	$_("pygrub").style.display = "none";
	$_("extrac").style.display = "none";
	$_("drive").style.display = "none";
	$_("fstype").style.display = "none";
	
	$_("noresizefs").style.display = "none";
	$_("perf_ops").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 = "";
	}else if(plan == "lxc"){
		$_("perf_ops").style.display = "";
	}
};

addonload("changeplan();");

</script>';

echo '
<div class="card p-2 mx-3">
	<div id="form-container">
		<form id="createtemplate-form" accept-charset="'.$globals['charset'].'" action="" method="post" name="createtmp" class="form-horizontal">
			<div class="row mx-auto w-100 mt-2">
				<div class="col-sm-6">
					<label class="control-label">'.__('Select a VPS').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Please select a VPS').'"><i class="fas text-primary fa-info-circle"></i></a></label>
					<select class="custom-select virt-select" name="vpsid" id="vpsid" onchange="changeplan();">
					<option value="0">'.__('Please select').'</option>';
						
					foreach($vs as $k => $v){  
						echo '<option id="vs'.$v['vpsid'].'" type="'.$v['virt'].(empty($v['hvm']) ? '' : 'hvm').'" distro="'.$v['os_distro'].'" '.(POSTval('vpsid') == $v['vpsid'] ? 'selected="selected"' : ''). ' value="'.$v['vpsid'].'">'.$v['vpsid'].' - '.$v['hostname'].'</option>';
					}
					
					echo '</select>
				</div>
				<div class="col-sm-6">
					<label class="control-label">'.__('OS-Version-Architecture').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Please specify a Template Name <br> eg: centos-7-x86_64').'"><i class="fas text-primary fa-info-circle"></i></a></label>
					<input type="text" class="form-control" name="tmpname" id="tmpname" size="16" value="'.POSTval('tmpname', '').'" />
				</div>
				<div class="col-sm-6 mt-2" id="pygrub">
					<div class="row mx-auto">
						<div class="col-sm-6 p-0">
							<label class="control-label">'.__('Pygrub').'</label>
							<span class="help-block"></span>
						</div>
						<div class="col-sm-6 d-flex justify-content-end p-0">
							'.get_switch('pygrub').'
						</div>
					</div>
				</div>
				<div class="col-sm-6 mt-2" id="fstype">
					<div class="row mx-auto">
						<div class="col-sm-6 p-0">
							<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-sm-6 d-flex justify-content-end p-0">
							'.get_switch('fstype').'
						</div>
					</div>
				</div>
				<div class="col-sm-6 mt-2" id="drive">
					<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>
					<input type="text" class="form-control" name="drive" id="driveid" value="'.POSTval('drive', '').'" size="10" />
				</div>
				<div class="col-sm-6 mt-2" id="noresizefs">
					<div class="row mx-auto">
						<div class="col-sm-6 p-0">
							<label class="control-label">'.__('Dont Resize Filesystem').'</label>
						</div>
						<div class="col-sm-6 d-flex justify-content-end p-0">
							'.get_switch('noresizefs').'
						</div>
					</div>
				</div>
				<div class="col-sm-6 mt-2" id="perf_ops">
					<div class="row mx-auto">
						<div class="col-sm-6 p-0">
							<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-sm-6 d-flex justify-content-end p-0">
							'.get_switch('perf_ops').'
						</div>
					</div>
				</div>
				<div class="col-sm-6 mt-2">
					<div class="row mx-auto">
						<div class="col-sm-4 p-0" id="tmp_distro" align="left">
							<label class="control-label">'.__('VPS OS Type').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The selected VPS\'s OS Type').'"><i class="fas text-primary fa-info-circle"></i></a></label>
							<input type="hidden" id="template" name="template" value="0">
							<div class="col-sm-6 p-0">';
								foreach($distros as $k => $v){
									echo '<span id="temp_'.$k.'" align="left"><img width="80px" src="'.distro_logo($k).'" /></span>';
								}
								
								echo '<span id="temp_others" align="left"><img width="80px" src="'.distro_logo('others').'" /></span>
							</div>
						</div>
						<div class="col-sm-8 p-0">
							<label class="control-label">'.__('Virtualization Type').'</label>
							<div class="row mx-auto w-100 my-0">
								<div class="col-sm-1 p-0" id="xen" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";">Xen</span></div>
								<div class="col-sm-1 p-0" id="xenhvm" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";">XEN-HVM</span></div>
								<div class="col-sm-1 p-0" id="kvm" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";">KVM</span></div>
								<div class="col-sm-1 p-0" id="xcp" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";">XCP</span></div>
								<div class="col-sm-1 p-0" id="xcphvm" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";">XCP-HVM</span></div>
								<div class="col-sm-1 p-0" id="lxc" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";">LXC</span></div>
								<div class="col-sm-1 p-0" id="vzo" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";"><span style="color:var(--red)">Virtuozzo</span><span style="color:var(--green)"> OpenVZ</span></span></div>
								<div class="col-sm-1 p-0" id="vzk" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";"><span style="color:var(--red)">Virtuozzo</span><span style="color:var(--black)"> KVM</span></div>
								<div class="col-sm-1 p-0" id="proxk" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";"><span style="color:var(--yellow)">Proxmox</span> <span style="color:var(--black)">KVM</span></div>
								<div class="col-sm-1 p-0" id="proxl" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";"><span style="color:var(--yellow)">Proxmox</span> LXC</span></div>
							</div>
						</div>
					</div>
				</div>
				<div class="col-sm-6 mt-2" id="extrac">
					<label class="control-label">'.__('Extra').'</label>
					<textarea class="form-control" name="extra" rows="3" cols="30">'.POSTval('extra', '').'</textarea>
				</div>
			</div>
			<br /><br />
			<center>
				<input type="submit" value="'.__('Create Template').'" name="createtemp" class="go_btn" />
			</center>

		</form>
	</div>
	<div class="mb-5" id="progress_onload"></div>
</div>';

softfooter();

}

