<?php

//////////////////////////////////////////////////////////////
//===========================================================
// servers_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 serverfirewall_status_theme(){

global $theme, $globals, $kernel, $user, $l, $cluster, $servers, $servs, $error, $done, $force_delete, $servergroups, $distros, $remove_ha, $delete, $servers_data, $server_cached, $firewall_plans;

softheader($l['<title>']);
					
$buttons = array( 0 => array(
	'name' => __('Add Firewall Plan'), 
	'act' => 'addserver_firewall_plan')
);

$GLOBALS['breadcrumbs'] = ['dashboard', 'lms_serverfirewall_status'];

echo page_header(__('Server Firewall Status'), 'fas fa-shield-alt', '', 0, 0, $buttons);

error_handle($error);
$serid = json_encode(array_keys($servers));

if(!empty($done)){
	show_alert(__('The service has been restarted'));
}

echo '
<script>


function applyfirewall_plan(){

	let select_id_name = "applyfirewallplan";
	let fwid = $("#"+select_id_name).val();
	
	message = "'.__J('Do you want to apply the Frewall Plan on these server(s) ').'";
	if(fwid == "0"){
		message = "'.__J('Do you want to remove firewall from these server(s) ').'";
	}

	// List of server ids to apply
	var serid_list = new Array();

	$(".ios:checked").each(function() {
		serid_list.push($(this).val());
	});
	
	if(serid_list.length < 1){
		showalert("'.__J('Nothing selected !').'","1");
		return false;
	}

	message += serid_list.join(",") + " ?";

	showalert(message ,"3" ,function(confirm){
		if(confirm == false){
			return false;
		}

		$.ajax({
			type : "POST",
			url : "'.$globals['index'].'act=serverfirewall_status&api=json",
			data : {
				serid_list : serid_list,
				applyfirewallplan : fwid
			},
			success: function(response){
				data = JSON.parse(response);
				if(data["done"]){
					showalert("'.__J('Firewall rules updation task has been successfully added to background').'");
					$(document).ready(function() {
						$("#alert-modal").on("hidden.bs.modal", function (e) {
							// put your default event here
							window.location.href="'.$globals['ind'].'act=serverfirewall_status";
						});
					});
				}else{
					showalert(data["error"], 1);
				}
			}

		});

		return false;
	});

}

</script>
<div class="card p-2 mx-3">
	<form accept-charset="'.$globals['charset'].'" name="server" id="server" method="post" action="" class="w-100 mx-auto sticky">
		<table class="table table-hover tablesorter common-table">
			<tr class="table_heading">
				<th class="z-index-1 min_width">
					'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
				</th>
				<th width="min_width">'.__('Server ID').'</th>
				<th width="min_width">'.__('Server Name').'</th>
				<th width="min_width">'.__('Firewall Status').'</th>
			</tr>
			';

			foreach($servers as $k => $v){
				echo '<tr id="server_'.$k.'">
						<td width="20" valign="middle" align="center">
							'.get_checkbox('servers_list[]', '', '', array('chk_it' => 0), $v['serid']).'
						</td>
						<td>'.$v['serid'].'</td>
						<td>'.$v['server_name'].'</td>
						<td>
							<div id="firewall_status_'.$k.'">
								<i class="fas fa-shield-alt fa-2x px-2 '.(!empty($v['fwid']) ? 'text-success' : 'text-danger').'" id="menu_icon"></i>'.(!empty($v['fwid']) ? '<span class="badge badge-secondary d-inline">'.$firewall_plans[$v['fwid']]['fw_plan_name'].' Firewall Plan is Applied </span>' : '').'
							</div>

						</td>
					 </tr>
				';				
			}
		
			echo '
		</table>
		<div class="d-flex mt-5">
			<div class="w-25"></div>
			<div class="w-75">
				<div class="d-flex gap-3 align-items-center justify-content-center w-100">
					<div class="p-0 text-nowrap">
						<label>'.__('Select Firewall Plan to apply to Node(s)').'</label>
					</div>
					<div class="w-100">
						<select id="applyfirewallplan" name="applyfirewallplan" class="custom-select">
							<option value="0">'.__('Disable').'</option>';
							foreach($firewall_plans as $k => $v){
								echo '<option value="'.$k.'">'.$v['fw_plan_name'].'</option>';
							}
							echo'
						</select>
					</div>
					<div class="">
						<input type="button" id ="firewall_plan_to_apply" class="go_btn" name="firewall_plan_to_apply" value="'.__('Apply').'" onclick="applyfirewall_plan(); return false;">
					</div>
				</div>
			</div>
		</div>
	</form>
</div>
';
softfooter();

}

?>