<?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{
	echo page_header(__('Create Template'), 'fas fa-compact-disc', $globals['docs'].'create-os-templates/', 0, 0, '');
	$breadcrumbs =['dashboard', 'lm_ostemplates', 'lms_createtemplate'];
	echo get_breadcrumb($breadcrumbs);
}
	

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 = "openvz";
	}
	
	if(distro == "undefined" || !distro){
		distro = "others";
	}
	
	$_("openvz").style.display = "none";
	$_("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";
	$_("proxo").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 my-3">
	<div class="col-sm-4">
		<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>
	</div>
	<div class="col-sm-4 server-select-lg" valign="top">';
		
		echo '<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>

<div class="row mx-auto w-100 my-3">
	<div class="col-sm-4">
		<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>
	</div>
	<div class="col-sm-4">
		<input type="text" class="form-control" name="tmpname" id="tmpname" size="16" value="'.POSTval('tmpname', '').'" />
	</div>
</div>

<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">
			<div class="row mx-auto w-100 my-0">
				<div class="col-sm-1 p-0" id="openvz" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";"><span style="color:var(--green)"> OpenVZ</span></span></div>
				<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="proxo" align="left"><span class="flatflare text-admin px-2" style="font-size:"13px";"><span style="color:var(--yellow)">Proxmox</span> <span style="color:var(--green)"> OpenVZ</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 class="row mx-auto w-100 my-3">
	<div class="col-sm-4">
		<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>
	</div>
	<input type="hidden" id="template" name="template" value="0">
	<div class="col-sm-4" id="tmp_distro" align="left">';
	foreach($distros as $k => $v){
		echo '<span id="temp_'.$k.'" align="left"><img width="80px" src="'.distro_logo($k,'',1).'" /></span>';
	}
	
	echo '<span id="temp_others" align="left"><img width="80px" src="'.distro_logo('others','',1).'" /></span>
	</div>
</div>

<div class="row mx-auto w-100 my-3" id="pygrub">
	<div class="col-4">
		<label class="control-label">'.__('Pygrub').'</label>
		<span class="help-block"></span>
	</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></div>
</div>	

<br />
<br />
<center>
<input type="submit" value="'.__('Create Template').'" name="createtemp" class="go_btn" />
</center>
<br />
<br />
</form>
</div>
<div id="progress_onload"></div>
</div>';

softfooter();

}

?>