<?php

//////////////////////////////////////////////////////////////
//===========================================================
// addiprange_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 addiprange_theme(){	

global $theme, $globals, $cluster, $user, $l, $ippools, $servers, $error, $done, $ips, $allowed_netmasks, $allowed_input_netmasks;

softheader($l['<title>']);

$GLOBALS['breadcrumbs'] = array( 'dashboard', 'lm_ippool', 'lms_addiprange');

echo page_header(__('Add IPv6 Subnet'), 'icon icon-ippool', $globals['docs'].'ipv6-subnets/', 0, 0, '');

error_handle($error);

if(!empty($done)){
	show_alert(__('The IP(s) have been saved. Return to <a href="index.php?act=ippool">IP Pool Overview</a>'), "0", "ipranges");
}

echo'<style>
	.nav-tabs .nav-link {
		height: 36px;             
	}
</style>';

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

function addrow6(id){
	var t = $_(id);

	var row = document.createElement("div");
	var _row = document.createElement("div");
	row.className = "ipv6-range row mx-auto w-100 my-1";
	_row.className = "col-sm-11 w-100 p-0 d-flex";
	var col1 = document.createElement("div");
	var col2 = document.createElement("div");
	var col3 = document.createElement("div");
	var col4 = document.createElement("div");
	var col5 = document.createElement("div");
	var col6 = document.createElement("div");
	var col7 = document.createElement("div");
	var col8 = document.createElement("div");
	var col9 = document.createElement("div");
	
	col1.innerHTML = \'<input type="text" name="ips6[\'+id+\'][1]" value="" size="4" maxlength="4" class="form-control ips6box ips6_1" /><label> : </label>\';
	col2.innerHTML = \'<input type="text" name="ips6[\'+id+\'][2]" value="" size="4" maxlength="4" class="form-control ips6box ips6_2" /><label> : </label>\';
	col3.innerHTML = \'<input type="text" name="ips6[\'+id+\'][3]" value="" size="4" maxlength="4" class="form-control ips6box ips6_3" /><label> : </label>\';
	col4.innerHTML = \'<input type="text" name="ips6[\'+id+\'][4]" value="" size="4" maxlength="4" class="form-control ips6box ips6_4" /><label> : </label>\';
	col5.innerHTML = \'<input type="text" name="ips6[\'+id+\'][5]" value="" size="4" maxlength="4" class="form-control ips6box ips6_5" /><label> : </label>\';
	col6.innerHTML = \'<input type="text" name="ips6[\'+id+\'][6]" value="" size="4" maxlength="4" class="form-control ips6box ips6_6" /><label> : </label>\';
	col7.innerHTML = \'<input type="text" name="ips6[\'+id+\'][7]" value="" size="4" maxlength="4" class="form-control ips6box ips6_7" /><label> : </label>\';
	col8.innerHTML = \'<input type="text" name="ips6[\'+id+\'][8]" value="" size="4" maxlength="4" class="form-control ips6box ips6_8" />\';
	col9.innerHTML = \'<a class="delip6 p-0" ><i class="fas fa-times text-danger cursor-pointer py-2"></i></a>\';
	
	_row.appendChild(col1);
	_row.appendChild(col2);
	_row.appendChild(col3);
	_row.appendChild(col4);
	_row.appendChild(col5);
	_row.appendChild(col6);
	_row.appendChild(col7);
	_row.appendChild(col8);
	row.appendChild(_row);
	row.appendChild(col9);
	
	t.appendChild(row);
	disable_fields();

	$(".delip6").each(function(){
		$(this).unbind("click");
		$(this).click(function(){
			var parent = $(this).parent();
			var parent2 = $(this).parent().parent();
			parent.remove();
			parent2.remove();
		});
	});
};

function disable_range_fields(){
	
	var val = parseInt($("#input_netmask").val());
	rem = (val % 16);
	
	required = Math.floor(val/16);
	
	if(rem > 0){
		rem = (rem/4);
		required += 1;
	}
	
	for(x = 1; x <= 8; x++){
		
		$(".ipv6_"+x).each(function(index){
			
			var todo = false;
			
			if(x > (required)){
				todo = true;
			}
			
			if(rem == 0){
				rem = 4;
			}
			
			if(x == required && rem > 0){
			
				$(this).attr("maxlength", rem);
			}
			
			$(this).prop("disabled", todo);
		});
	}
}

function show_ip_fields(type)
{
    $(".single_ipv6, .generate_ipv6_subnet").hide();

    $("#single_ipv6, #generate_ipv6_subnet").removeClass("active");

    $("." + type).show();

    $("#" + type).addClass("active");
}

function disable_fields(){
	var val = parseInt($("#netmask").val());
	val = (val/16);
	
	for(x = 1; x <= 8; x++){
		
		$(".ips6_"+x).each(function(index){
			var todo = false;
			if(x >= (val+1)){
				todo = true;
			}
			$(this).prop("disabled", todo);
		});
	}
	
	disable_range_fields();
}

$(document).ready(function(){
	//hide all fields 
	$(".single_ipv6, .generate_ipv6_subnet").hide();

    // Show default tab (single IPv6)
    show_ip_fields("single_ipv6");

    $("#single_ipv6").click(function(e){
        e.preventDefault();
        show_ip_fields("single_ipv6");
    });

    $("#generate_ipv6_subnet").click(function(e){
        e.preventDefault();
        show_ip_fields("generate_ipv6_subnet");
    });

});

addonload("disable_fields();");

</script>
<div class="card p-2 mx-3">
<div id="form-container">
<form accept-charset="'.$globals['charset'].'" name="addippool" method="post" action="" class="form-horizontal">

<div class="row mx-auto w-100 mb-3">
	<div class="col-sm-6 mt-2">
		<label class="control-label">'.__('Select IP Pool').'</label>
		<select class="virt-select" name="ippid" id="ippid">
			<option value="0">'.__('None').'</option>';	
		
			// Select the default IP Pool if there in the GET
			if(!POSTval('ippid') && !POSTval('submitip')){
				$_POST['ippid'] = optGET('ippid');
			}
			
			foreach($ippools as $k => $v){		 
				echo '<option value="'.$k.'" '.((POSTval('ippid') == $k) ? 'selected="selected"' : '').' ipv="'.(!empty($v['ipv6']) ? 6 : 4).'">'.$v['ippool_name'].'</option>';
			}
		
	 	echo'</select>
	 	<span class="help-block"></span>
	</div>
	<div class="col-sm-6 mt-2">
		<label class="form-control">'.__('Select Subnet').'</label>
		<div class="d-flex align-items-end">
			<ul class="nav nav-tabs nav-justified w-100">
				<li class="nav-item"><a class="nav-link active text-center" id="single_ipv6">'.__('Add IPv6 Subnet').'</a></li>
				<li class="nav-item"><a class="nav-link text-center" id="generate_ipv6_subnet">'.__('Generate IPv6 Subnets').'</a></li>
			</ul>
		</div>
	</div>
	<div class="col-sm-6">
		<div class="row mx-auto mt-2">
			<div class="col-sm-6 pl-0">
				<label class="control-label">'.__('Input Netmask (From)').'</label>';
				$input_netmask = (int) empty($_REQUEST['input_netmask']) ? 112 : $_REQUEST['input_netmask'];
					echo '<label class="float-left pr-2">/</label>
						<div id="in_netmask" class="w-100"><select id="input_netmask" class="custom-select" name="input_netmask" onchange="disable_fields()">';
							foreach($allowed_input_netmasks as $k => $v){
								echo '<option value="'.$k.'" '.($input_netmask == $k ? 'selected="selected"' : '').'>'.$k.'</option>';
							}			
					echo '</select>
				</div>
			</div>
			
			<div class="col-sm-6 p-0">
				<label class="control-label">'.__('Netmask (Routing Prefix)').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Desired netmask (To)').'"><i class="fas text-primary fa-info-circle"></i></a></label>';
				$default_netmask = (int) empty($_REQUEST['netmask']) ? 112 : $_REQUEST['netmask'];
				echo '<label class="float-left pr-2">/</label><select id="netmask" class="custom-select" name="netmask" onchange="disable_fields()">';
					foreach($allowed_netmasks as $k => $v){
						echo '<option value="'.$k.'" '.($default_netmask == $k ? 'selected="selected"' : '').'>'.$k.'</option>';
					}			
				echo '</select>
			</div>
		</div>
	</div>

	<div class="col-sm-6 mt-2 single_ipv6" id="ips6">
		<label class="control-label">'.__('Enter IPv6 Subnet').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Enter single IPv6 subnet or generate random subnets').'"><i class="fas text-primary fa-info-circle"></i></a></label>
		<div id="iptable6">';		
				$ips = @$_POST['ips6'];
				
				if(is_array($ips) && !empty($ips)){
					foreach($ips as $k => $ip){
						$ip_ = implode('', $ip);
						if(empty($ip_)){
							unset($ips[$k]);
						}
					}
				}
				
				if(empty($ips)){
					$ips = array(NULL);
				}
			
				foreach($ips as $k => $ip){
					echo '
				<div class="ipv6-range row mx-auto w-100 ">
					<div class="col-sm-11 w-100 p-0 d-flex mb-2">
						<div><input type="text" name="ips6['.$k.'][1]" value="'.$ip[1].'" size="4" maxlength="4" class="form-control ips6box ips6_1" /><label class="colon"> : </label></div>
						<div><input type="text" name="ips6['.$k.'][2]" value="'.$ip[2].'" size="4" maxlength="4" class="form-control ips6box ips6_2" /><label class="colon"> : </label></div>
						<div><input type="text" name="ips6['.$k.'][3]" value="'.$ip[3].'" size="4" maxlength="4" class="form-control ips6box ips6_3" /><label class="colon"> : </label></div>
						<div><input type="text" name="ips6['.$k.'][4]" value="'.@$ip[4].'" size="4" maxlength="4" class="form-control ips6box ips6_4" /><label class="colon"> : </label></div>
						<div><input type="text" name="ips6['.$k.'][5]" value="'.@$ip[5].'" size="4" maxlength="4" class="form-control ips6box ips6_5" /><label class="colon"> : </label></div>
						<div><input type="text" name="ips6['.$k.'][6]" value="'.@$ip[6].'" size="4" maxlength="4" class="form-control ips6box ips6_6" /><label class="colon"> : </label></div>
						<div><input type="text" name="ips6['.$k.'][7]" value="'.@$ip[7].'" size="4" maxlength="4" class="form-control ips6box ips6_7" /><label class="colon"> : </label></div>
						<div><input type="text" name="ips6['.$k.'][8]" value="'.@$ip[8].'" size="4" maxlength="4" class="form-control ips6box ips6_8" /> </div>
					</div>
				</div>';
				}
					
			echo '
		</div>
		<a onclick="addrow6(\'iptable6\')" class="go_btn blue_btn py-1 px-2" >+</a>
	</div>
	<div class="col-sm-6 mt-2">
		<div class="row mx-auto">
			<div class="col-sm-12 p-0 generate_ipv6_subnet" id="ipv6_range">
				<label class="control-label">'.__('Subnet').'</label>
				<div class="ipv6-range row mx-auto w-100">
					<div class="col-sm-12 p-0 d-flex">
						<div><input type="text" name="ipv6_1" value="'.POSTval('ipv6_1', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_1"><label> : </label></div>
						<div><input type="text" name="ipv6_2" value="'.POSTval('ipv6_2', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_2"><label> : </label></div>
						<div><input type="text" name="ipv6_3" value="'.POSTval('ipv6_3', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_3"><label> : </label></div>
						<div><input type="text" name="ipv6_4" value="'.POSTval('ipv6_4', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_4"><label> : </label></div>
						<div><input type="text" name="ipv6_5" value="'.POSTval('ipv6_5', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_5"><label> : </label></div>
						<div><input type="text" name="ipv6_6" value="'.POSTval('ipv6_6', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_6"><label> : </label></div>
						<div><input type="text" name="ipv6_7" value="'.POSTval('ipv6_7', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_7"><label> : </label></div>
						<div><input type="text" name="ipv6_8" value="'.POSTval('ipv6_8', '').'" size="4" maxlength="4" class="form-control ips6box ipv6_8"> </div>
					</div>
				</div>
			</div>
			<div class="col-sm-12 p-0 generate_ipv6_subnet" id="ipv6_range">
				<label class="control-label">'.__('Number of IPv6 Subnets').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The Number of IPv6 Subnets to generate').'"><i class="fas text-primary fa-info-circle"></i></a></label>
				<input type="text" class="form-control" name="ipv6_num" value="'.POSTval('ipv6_num', 50).'" size="10">
			</div>
		</div>
	</div>
</div>


</div>
<br>

<center><input type="submit" name="submitip" value="'.__('Add Subnets(s)').'" class="go_btn m-4" /></center>
	
</form>
</div>
</div>
';

softfooter();

}

