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

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

echo get_breadcrumb($breadcrumbs);

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>'));
}

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-5 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;&nbsp;</label><input type="text" class="form-control w-75" name="ips[]" size="20" />\';
	col2.innerHTML = \'<label class="p-0">'.__J('MAC').' :&nbsp;&nbsp;</label><input type="text" class="form-control w-75" name="macs[]" size="20" /><a class="pl-1 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-10";
	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 ips6box" name="ips6[\'+lastrow+\'][1]" value="" size="4" maxlength="4" /><label> : </label>\';
	col2.innerHTML = \'<input type="text" class="form-control ips6box" name="ips6[\'+lastrow+\'][2]" value="" size="4" maxlength="4" /><label> : </label>\';
	col3.innerHTML = \'<input type="text" class="form-control ips6box" name="ips6[\'+lastrow+\'][3]" value="" size="4" maxlength="4" /><label> : </label>\';
	col4.innerHTML = \'<input type="text" class="form-control ips6box" name="ips6[\'+lastrow+\'][4]" value="" size="4" maxlength="4" /><label> : </label>\';
	col5.innerHTML = \'<input type="text" class="form-control ips6box" name="ips6[\'+lastrow+\'][5]" value="" size="4" maxlength="4" /><label> : </label>\';
	col6.innerHTML = \'<input type="text" class="form-control ips6box" name="ips6[\'+lastrow+\'][6]" value="" size="4" maxlength="4" /><label> : </label>\';
	col7.innerHTML = \'<input type="text" class="form-control ips6box" name="ips6[\'+lastrow+\'][7]" value="" size="4" maxlength="4" /><label> : </label>\';
	col8.innerHTML = \'<input type="text" class="form-control 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 showserver(){
	if($_("ippid").value == 0){
		$_("ip_serid").style.display = "";
	}else{		
		$_("ip_serid").style.display = "none";
	}
};

function changetype(iptype){
	$_("ipv4").className = "nav-link";
	$_("ipv6").className = "nav-link";
	
	if (iptype == 4){
		$_("ips").style.display="";
		$_("ips6").style.display="none";
		$_("ip_range").style.display="";
		$_("gen_ipv6").style.display="none";
		$_("firstip").style.display="";
		$_("lastip").style.display="";
		$_("ipv6_range").style.display="none";
		$_("ipv6_num").style.display="none";
	}else{
		$_("ips").style.display="none";
		$_("ips6").style.display="";
		$_("ip_range").style.display="none";
		$_("gen_ipv6").style.display="";
		$_("firstip").style.display="none";
		$_("lastip").style.display="none";
		$_("ipv6_range").style.display="";
		$_("ipv6_num").style.display="";
	}		
	$_("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;
		}
	}
	
};

addonload("showserver();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).'">
		<ul class="nav nav-tabs">
			<li class="col-4"><label class="control-label mx-auto my-3">'.__('IP Type').'</label></li>
			<li class="col-4 nav-item text-center"><a href="javascript:changetype(4);" class="nav-link active" id="ipv4"><h4>IPv4</h4></a></li>
			<li class="col-4 nav-item text-center"><a href="javascript:changetype(6);" class="nav-link" id="ipv6"><h4>IPv6</h4></a></li>
		</ul>
		<div class="row mx-auto w-100 my-3" id="ips">
			<div class="col-sm-4">
				<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>
			<div class="col-sm-8">
				<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-5 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>
				<a onclick="addrow(\'iptable\')" class="go_btn blue_btn py-1 px-2" >+<a/>
			</div>
		</div>
		<div class="row mx-auto w-100 my-3" id="ips6">
			<div class="col-sm-4">
				<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>
			<div class="col-sm-8">
				<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-10">
									<div><input type="text" class="form-control" name="ips6['.$k.'][1]" value="'.$ip[1].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control" name="ips6['.$k.'][2]" value="'.$ip[2].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control" name="ips6['.$k.'][3]" value="'.$ip[3].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control" name="ips6['.$k.'][4]" value="'.$ip[4].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control" name="ips6['.$k.'][5]" value="'.$ip[5].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control" name="ips6['.$k.'][6]" value="'.$ip[6].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control" name="ips6['.$k.'][7]" value="'.$ip[7].'" size="4" maxlength="4" class="ips6box" /><label> : </label></div>
									<div><input type="text" class="form-control" 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>
		<div class="row mx-auto w-100 my-3" id="ip_range"><div class="col-sm-12 tit p-0" align="center">'.__('IP Range').'</div></div>

		<div class="row mx-auto w-100 my-3" id="gen_ipv6"><div class="col-sm-12 tit p-0" align="center">'.__('Generate IP').'</div></div>

		<div class="row mx-auto w-100 my-3" id="firstip">
			<div class="col-sm-4"><label class="control-label">'.__('First IP').'</label></div>
			<div class="col-sm-4"><input type="text" class="form-control" name="firstip" value="'.POSTval('firstip', '').'" size="30" /></div>
			<div class="col-sm-4"></div>
		</div>

		<div class="row mx-auto w-100 my-3" id="lastip">
			<div class="col-sm-4"><label class="control-label">'.__('Last IP').'</label></div>
			<div class="col-sm-4"><input type="text" class="form-control" name="lastip" value="'.POSTval('lastip', '').'" size="30" /></div>
			<div class="col-sm-4"></div>
		</div>

		<div class="row mx-auto w-100 my-3" id="ipv6_range">
			<div class="col-sm-4"><label class="control-label">'.__('Generate IPv6').'</label></div>
			<div class="col-sm-8">
				<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>

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

		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4"><label class="control-label">'.__('Select IP Pool').'</label></div>
			<div class="col-sm-4 server-select-lg">
				<select class="form-control virt-select" name="ippid" id="ippid" onchange="showserver()">
					<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>

		<div class="row mx-auto w-100 my-3" id="ip_serid">
			<div class="col-sm-4"><label class="control-label">'.__('Select Server').'</label></div>
			<div class="col-sm-4 server-select-lg">
				<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>';
					}
					
				echo'</select>
			</div>
		</div>
		<center><input type="submit" name="submitip" value="'.__('Add IP Address(s)').'" class="go_btn"/></center>
	</form>
</div>';

softfooter();

}

?>