<?php

//////////////////////////////////////////////////////////////
//===========================================================
// ostemplates_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 os_theme(){

global $theme, $globals, $kernel, $user, $l, $oslist, $offlist, $ostemplates, $error, $oses, $done, $flag_getos, $actid;


softheader($l['<title>']);
										
echo page_header(__('OS Templates Browser'), 'fas fa-compact-disc',  $globals['docs'], 1, 0, '');

$breadcrumbs = array('dashboard', 'lm_ostemplates', 'lms_os');

echo get_breadcrumb($breadcrumbs);

error_handle($error);

$getos = (int) optGET('getos');

if(!empty($flag_getos)){
	echo '<script language="javascript" type="text/javascript">
		var actid = '.$actid.';
		var progress = 0;
		var oses = new Object();';
		
		if(empty($ostemplates[$getos])){
			$list = $ostemplates;
		}else{
			$list = array($getos => $ostemplates[$getos]);
		}
		
		foreach($list as $k => $v){
			echo 'oses['.$k.'] = new Array(0, 0, "'.$k.'", "'.$v['size'].'", "'.$v['filename'].'", 0);';
		}
	
	echo '
	function get_osprogress(){
		setTimeout(function(){
			$.ajax({type: "GET",
				url: "'.$globals['index'].'jsnohf=1&act=tasks",
				data: "api=json&actid="+actid,
				dataType : "json",
				success: function(response){
					var tasks = response.tasks;
					
					var os = tasks[actid]["os"];
					var dlos = new Array();
					$.each(os, function(index, value){
						dlos[index]=[];
						dlos[index]["osid"] = os[index]["osid"];
						dlos[index]["inprogress"] = os[index]["inprogress"];
						dlos[index]["done"] = os[index]["done"];
						dlos[index]["failed"] = os[index]["failed"];
						dlos[index]["err_msg"] = os[index]["err_msg"];
						if(dlos[index]["inprogress"] != ""){
							dlos[index]["progress"] = os[index]["progress"]
						}
					});
					
					for(x in dlos){
						if(dlos[x]["inprogress"] == 1){
							$("#prog_"+dlos[x]["osid"]).html(dlos[x]["progress"]+"%");
						}
						else if(dlos[x]["done"] == 1){
							$("#prog_"+dlos[x]["osid"]).html(\'<i class="fas fa-check text-success"></i>\');
							$("#err_"+dlos[x]["osid"]).html(dlos[x]["err_msg"]);
						}
						else if(dlos[x]["failed"] == 1){
							$("#prog_"+dlos[x]["osid"]).html(\'<i class="fas fa-times text-danger"></i>\');
							$("#err_"+dlos[x]["osid"]).html(dlos[x]["err_msg"]);
						}
					}
					if(tasks[actid]["progress"] == 100){
						showalert(\''.__J('All files have been downloaded. Return to <a href="index.php?act=ostemplates">Installed OS Overview</a>').'\');
						return false;
					}
					setTimeout("get_osprogress()", 100);
				}
			});
		}, 1000);
	};
	
	$(document).ready(function(){
		for(x in oses){
			if(oses[x][0] == 0){
				$("#getostable").append(\'<tr><td>File : \'+oses[x][4]+\'</td><td>Size : \'+Math.round(oses[x][3]/1024/1024)+\' MB</td><td id="prog_\'+oses[x][2]+\'" class="val" width="100">'.__J('Pending').'</td></tr><tr><td height="20px"colspan="3" align="right" id="err_\'+oses[x][2]+\'"></td></tr>\');
			}
		}
		
	setTimeout("get_osprogress()", 100);
	
});

	</script>

	<div class="card p-2 mx-3">';
	show_alert(__('The OS List has been saved. Please wait as the files are downloaded'));
	echo'
		<center><table id="getostable" border="0" width="100%" cellpadding="4" class="table table-hover tablesorter vs-table"></table></center>
	</div>
	<br />
	<div class="notice" style="display:none;" id="done_notice"><b>NOTE : </b>&nbsp; '.__('All files have been downloaded. Return to <a href="index.php?act=ostemplates">Installed OS Overview</a>').'</div>';
}
else{
	$num_record = 0 ;
	foreach($offlist as $v){
		foreach($v as $vv){	
			foreach($vv as $vs){
				$num_record += 1;
			}
		}
	}
	echo '
	<div class="card p-2 mx-3">
		<div id="showsearch" class="mb-3">
			<div class="form-group_head">
			<div class="row mx-auto w-100 mb-3">
				<div class="col-sm-3"></div>
				<div class="col-sm-3"><label class="mt-0">'.__('Type:').'</label>
				<select name="vstype" id="vstype" class="virt-select">
					<option value="0" >'.__('All').'</option>
					<option value="xen" >'.__('Xen').'</option>
					<option value="xenhvm" >'.__('Xen HVM').'</option>
					<option value="xcp" >'.__('XCP').'</option>
					<option value="xcphvm" >'.__('XCP HVM').'</option>
					<option value="openvz" >'.__('OpenVZ').'</option>
					<option value="kvm" >'.__('KVM').'</option>
					<option value="lxc" >'.__('LXC').'</option>
					<option value="vzo" >'.__('Virtuozzo OpenVZ').'</option>
					<option value="vzk" >'.__('Virtuozzo KVM').'</option>
					<option value="proxo" >'.__('Proxmox OpenVZ').'</option>
					<option value="proxk" >'.__('Proxmox KVM / QEMU').'</option>
					<option value="proxl" >'.__('Proxmox LXC').'</option>
				</select>
				</div>
				<div class="col-sm-3"><label class="mt-0">'.__('Distro:').'</label>
				<select name="ostype" id="ostype" class="virt-select">
					<option value="0">'.__('All').'</option>
					<option value="almalinux" >'.__('Almalinux').'</option>
					<option value="rocky" >'.__('Rocky Linux').'</option>
					<option value="centos" >'.__('CentOS').'</option>
					<option value="ubuntu" >'.__('Ubuntu').'</option>
					<option value="debian" >'.__('Debian').'</option>
					<option value="fedora" >'.__('Fedora').'</option>
					<option value="suse" >'.__('OpenSuse').'</option>
					<option value="scientific" >'.__('Scientific').'</option>
					<option value="webuzo" >'.__('Webuzo').'</option>
					<option value="oracle" >'.__('Oracle Linux').'</option>
					<option value="windows" >'.__('Windows').'</option>
				</select>
				</div>
			</div>
			</div>
		</div>
		<form accept-charset="'.$globals['charset'].'" name="os" method="post" action="" class="form-horizontal">
			'.get_num_records($num_record).'
			<div class="w-100 mx-auto sticky">
				<table id="oslist" class="table table-hover tablesorter w-50 m-auto">
				<tr class="table_heading">
					<th align="center">'.__('OS').'</th>
					<th align="center" class="min_width">'.__('Type').'</th>
					<th align="center">'.__('Filename').'</th>
					<th align="center">'.__('Size').'</th>
					<th align="center" class="z-index-1 min_width">
					</th>
				</tr>';
			
				foreach($offlist as $k => $v){

					foreach($v as $kv => $vv){
					
						foreach($vv as $kvv => $vs){
							$class_os = (($vs['type'] == 'xcp' || $vs['type'] == 'xen') && !empty($vs['hvm'])) ?  $vs['type'].'hvm' : $vs['type'];
							$chk_flag = (isset($_POST['osids']) ? (@in_array($kvv, $_POST['osids']) ? 'checked="checked"' : '') : (!empty($ostemplates[$kvv]) ? 'checked="checked"' : ''));
							echo'<tr class="'.$class_os.' '.$vs['distro'].' template_rows '.(isset($_POST['osids']) ? (@in_array($kvv, $_POST['osids']) ? 'os-selected' : '') : (!empty($ostemplates[$kvv]) ? 'os-selected"' : '')).'">
							<td align="center"><img src="'.distro_logo($kv,'',1).'" width="40px"; /></td>
							<td align="center"><script>document.write(show_virtname("'.$vs['type'].(empty($vs['hvm']) ? '' : 'hvm').'"));</script></td>
							<td align="left">'.$vs['filename'].'</td>
							<td align="left">'.round($vs['size']/1024/1024).' MB</td>
							<td align="center" >
								'.get_checkbox('osids[]', '', '', array('chk_it' => 0), $kvv, '', array($chk_flag)).'
							</td>
							</tr>';
							
						}
								
					}
					
				}

				echo '</table>
			</div>
			<br/><br/>
			<center><input id="savesettings" type="submit" name="savesettings" value="'.__('Save Settings').'" class="go_btn"></center><br>
		</form>
		<script language="javascript" type="text/javascript">
		
			function showsearch(){
				if($_("showsearch").style.display == ""){
					$_("showsearch").style.display="none";
				}else{
					$_("showsearch").style.display="";
				}
			};

			$("select#ostype, select#vstype").on("change", function(){

				var vs = $("select#vstype").val(),
					os = $("select#ostype").val();
				
				if(vs == 0 && os != 0){
					$(".template_rows").hide();
					$("."+os).show();
				}else if(os != 0){
					$(".template_rows").hide();
					$("."+vs+"."+os).show();
				}else if(os == 0 && vs == 0){
					$(".template_rows").show();
				}else{
					$(".template_rows").hide();
					$("."+vs).show();
				}
				
			});
		</script>';
}
echo'</div>';
softfooter();

}

