<?php

//////////////////////////////////////////////////////////////
//===========================================================
// addips_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 addips_theme(){	

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

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

$GLOBALS['breadcrumbs'] = array( 'dashboard', 'lm_ips', 'lms_addips');

echo  page_header(__('Add IP'), 'icon icon-ippool',$globals['docs'].'add-ip/', 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","ips");
}

echo '<script language="javascript" type="text/javascript">
function addrow(id){
	var t = $_(id);

	var row = document.createElement("div");
	row.className = "row mx-auto w-100 my-3";
	var col1 = document.createElement("div");
	col1.className = "col-6 p-0 ip-mac d-flex align-items-center";
	var col2 = document.createElement("div");
	col2.className = "col-6 p-0 ip-mac d-flex align-items-center";

	col1.innerHTML = \'<label class="p-0">'.__J('IP :').' &nbsp;</label><input type="text" class="form-control w-75" name="ips[]" size="20" />\';
	col2.innerHTML = \'<label class="p-0">'.__J('MAC :').' &nbsp;</label><input type="text" class="form-control w-75" name="macs[]" size="20" /><a class="pl-1 mx-2 delip" ><i class="fas fa-times text-danger cursor-pointer py-2"></i></a>\';
	row.appendChild(col1);
	row.appendChild(col2);
	t.appendChild(row);
	$(".delip").each(function(){
		$(this).unbind("click");
		$(this).click(function(){
			var parent = $(this).parent();
			var parent2 = $(this).parent().parent();
			parent.remove();
			parent2.remove();
		});
	});
};

function addrow6(id){
	var t = $_(id);
	var lastrow = $(".ipv6-range").length - 1;
	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" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][1]" value="" size="4" maxlength="4" /><label> : </label>\';
	col2.innerHTML = \'<input type="text" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][2]" value="" size="4" maxlength="4" /><label> : </label>\';
	col3.innerHTML = \'<input type="text" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][3]" value="" size="4" maxlength="4" /><label> : </label>\';
	col4.innerHTML = \'<input type="text" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][4]" value="" size="4" maxlength="4" /><label> : </label>\';
	col5.innerHTML = \'<input type="text" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][5]" value="" size="4" maxlength="4" /><label> : </label>\';
	col6.innerHTML = \'<input type="text" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][6]" value="" size="4" maxlength="4" /><label> : </label>\';
	col7.innerHTML = \'<input type="text" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][7]" value="" size="4" maxlength="4" /><label> : </label>\';
	col8.innerHTML = \'<input type="text" class="form-control w-90 ips6box" name="ips6[\'+lastrow+\'][8]" value="" size="4" maxlength="4" />\';
	col9.innerHTML = \'<a class="delip6" ><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);
	$(".delip6").each(function(){
		$(this).unbind("click");
		$(this).click(function(){
			var parent = $(this).parent();
			var parent2 = $(this).parent().parent();
			parent.remove();
			parent2.remove();
		});
	});
};

function show_ip_fields(type)
{
    $(".single_ipv4, .range_ipv4, .single_ipv6, .range_ipv6").hide();

    $("#single_ipv4, #range_ipv4, #single_ipv6, #range_ipv6").removeClass("active");

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

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

function changetype(iptype){
	$_("ipv4").className = "nav-link";
	$_("ipv6").className = "nav-link";
	
	if (iptype == 4){
		$_("ips").style.display="";
		$_("ipv4_ip").style.display="";
		$_("ipv6_ip").style.display="none";
		$_("ips6").style.display="none";
		$_("firstip").style.display="";
		$_("lastip").style.display="";
		$_("ipv6_range").style.display="none";
		$_("ipv6_num").style.display="none";
		show_ip_fields("single_ipv4");
	}else{
		$_("ips").style.display="none";
		$_("ipv4_ip").style.display="none";
		$_("ipv6_ip").style.display="";
		$_("ips6").style.display="";
		$_("firstip").style.display="none";
		$_("lastip").style.display="none";
		$_("ipv6_range").style.display="";
		$_("ipv6_num").style.display="";
		show_ip_fields("single_ipv6");
	}	
	
	
	$_("ipv"+iptype).className = "active nav-link";
	$_("iptype").value = iptype;
	
	for(var i=0; i<$_("ippid").options.length; i++){

		// The NONE Value
		if($_("ippid").options[i].value == 0){
			continue;
		}	
		
		if(getAttributeByName($_("ippid").options[i], "ipv") == 4 && iptype == 4){
			$_("ippid").options[i].disabled = false;
		}else if(getAttributeByName($_("ippid").options[i], "ipv") == 6 && iptype == 6){
			$_("ippid").options[i].disabled = false;
		}else{
			$_("ippid").options[i].disabled = true;
		}
	}
	
};


$(document).ready(function(){

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

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

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

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

});

addonload("changetype('.POSTval('iptype', 4).');");
</script>


<div id="form-container" class="card p-2 mx-3">
	<form accept-charset="'.$globals['charset'].'" name="addippool" method="post" action="" class="form-horizontal">
		<input type="hidden" id="iptype" name="iptype" value="'.POSTval('iptype', 4).'">
		<div class="row mx-auto mb-2 mt-2 align-items-end">
			<div class="col-sm-6">
				<label class="control-label">'.__('Select IP Pool').'</label>
				<select class="form-control 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>
			</div>
			<div class="col-sm-6 mt-2">
				<label class="control-label">'.__('IP Type').'</label>
				<ul class="nav nav-tabs nav-fill w-100">
					<li class="nav-item">
						<a href="javascript:changetype(4);" class="nav-link active text-center" id="ipv4">
							'.__('IPv4').'
						</a>
					</li>
					<li class="nav-item">
						<a href="javascript:changetype(6);" class="nav-link text-center" id="ipv6">
							'.__('IPv6').'
						</a>
					</li>
				</ul>
			</div>

			<div class="col-sm-12 mt-4">
				<div class="row mx-auto d-flex justify-content-center w-auto">
					<div id="ipv4_ip">
						<ul class="nav nav-tabs nav-fill w-100">
							<li class="nav-item text-center"><a href="#" class="nav-link active" id="single_ipv4">'.__('Add Single IP').'</a></li>
							<li class="nav-item text-center"><a href="#" class="nav-link" id="range_ipv4">'.__('Add IP Range').'</a></li>
						</ul>
					</div>
					<div id="ipv6_ip">
						<ul class="nav nav-tabs nav-fill w-100">
							<li class="nav-item text-center"><a href="#" class="nav-link active" id="single_ipv6">'.__('Add Single IPv6').'</a></li>
							<li class="nav-item text-center"><a href="#" class="nav-link" id="range_ipv6">'.__('Generate IPv6').'</a></li>
						</ul>
					</div>
				</div>
			</div>
				
			<div class="col-sm-6 my-2 single_ipv4" id="ips">
				<label class="control-label">'.__('Enter IP').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Enter single IPs or create a IP Range').'<br>'.__('Leave the MAC field blank to auto-generate the MAC.').'"><i class="fas text-primary fa-info-circle"></i></a></label>
				<div id="iptable">';
				
					//$ips = @$_POST['ips'];
					
					if(is_array($ips) && !empty($ips)){
						foreach($ips as $k => $ip){
							if(empty($ip)){
								unset($ips[$k]);
							}
						}
					}
					
					if(empty($ips)){
						$ips = array(NULL);
					}
				
					foreach($ips as $ip){
						echo '<div class="row mx-auto w-100 mb-3 mt-0">
							<div class="col-6 p-0 d-flex align-items-center ip-mac">
								<label class="pt-0">'.__('IP').' :&nbsp;&nbsp;</label><input type="text" class="form-control w-75" name="ips[]" value="'.$ip['ip'].'" size="30" />
							</div>
							<div class="col-6 p-0 d-flex align-items-center ip-mac">
								<label class="pt-0">'.__('MAC').' :&nbsp;&nbsp;</label><input type="text" class="form-control w-75" name="macs[]" value="'.$ip['mac_addr'].'" size="30" />
							</div>
						</div>';
					}
						
					echo '
				</div>
				<span>
					<a onclick="addrow(\'iptable\')" class="go_btn blue_btn py-1 px-2" >+<a/>
				</span>
				
			</div>
			<div class="col-sm-6 my-2 single_ipv6 mb-3" id="ips6">
				<label class="control-label">'.__('Enter IP').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Enter single IPs or generate random IPs').'"><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 mb-2">
								<div class="col-sm-11 p-0 d-flex">
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][1]" value="'.$ip[1].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][2]" value="'.$ip[2].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][3]" value="'.$ip[3].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][4]" value="'.$ip[4].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][5]" value="'.$ip[5].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][6]" value="'.$ip[6].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][7]" value="'.$ip[7].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control w-90" name="ips6['.$k.'][8]" value="'.$ip[8].'" size="4" maxlength="4" class="ips6box" /> </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 range_ipv4" id="firstip">
				<label class="control-label">'.__('First IP').'</label>
				<input type="text" class="form-control" name="firstip" value="'.POSTval('firstip', '').'" size="30" />
			</div>
			<div class="col-sm-6 mt-2 range_ipv4" id="lastip">
				<label class="control-label">'.__('Last IP').'</label>
				<input type="text" class="form-control" name="lastip" value="'.POSTval('lastip', '').'" size="30" />
			</div>
			<div class="col-sm-6 mt-2 range_ipv6" id="ipv6_range">
				<label class="control-label">'.__('Generate IPv6').'</label>
				<div class="ipv6-range">
					<div> 
						<input type="text" class="form-control" name="ipv6_1" value="'.POSTval('ipv6_1', '').'" size="4" maxlength="4"><label> : </label>
					</div>
					<div> 
						<input type="text" class="form-control" name="ipv6_2" value="'.POSTval('ipv6_2', '').'" size="4" maxlength="4"><label> : </label>
					</div>
					<div> 
						<input type="text" class="form-control" name="ipv6_3" value="'.POSTval('ipv6_3', '').'" size="4" maxlength="4"><label> : </label>
					</div>
					<div> 
						<input type="text" class="form-control" name="ipv6_4" value="'.POSTval('ipv6_4', '').'" size="4" maxlength="4"><label> : </label>
					</div>
					<div> 
						<input type="text" class="form-control" name="ipv6_5" value="'.POSTval('ipv6_5', '').'" size="4" maxlength="4"><label> : </label>
					</div>
					<div>
						<input type="text" class="form-control" name="ipv6_6" value="'.POSTval('ipv6_6', '').'" size="4" maxlength="4"><label> : </label>
					</div>
					<div>
						<input type="text" class="form-control" name="ipv6_7" value="'.POSTval('ipv6_7', '').'" size="4" maxlength="4" placeholder="auto">
					</div>
					<div><label> : auto </label></div>
				</div>
			</div>
			<div class="col-sm-6 mt-2 range_ipv6" id="ipv6_num">
				<label class="control-label">'.__('Number of IPv6').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The Number of IPv6 addresses 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 class="col-sm-6" id="ip_serid">
					//<label class="control-label">'.__('Select Server').'</label>
					//<select class="form-control virt-select" name="ip_serid">	
						//foreach($servers as $k => $v){		 
							//echo '<option value="'.$k.'" '.(POSTval('ip_serid') == $k ? 'selected="selected"' : '').'>'.$v['server_name'].'</option>';
						//}
						
					//</select>
				//</div>	
			echo'
		</div>
		<center><input type="submit" name="submitip" value="'.__('Add IP Address(s)').'" class="go_btn my-3"/></center>
	</form>
</div>
';

softfooter();

}

