<?php


/**
 * Class for network settings
 */
class Net_conf {



	function display($args) {
		include('./includes/header.php');
		require_once("./includes/lang.php");
		$ret.="<script>
			function check(val){
				if (val<500 || val>1500){
			        alert (\"".NETWORK_MTU_ERROR_DIS."\");
			        return false;
			    }else{
			        return true;
			    }
			}
		</script>";
		include '/usr/local/php/beroConf.php';
		$image_dir = "/mnt/images";
		$files = 0;
		$bc = new beroConf('root');
		$ba= new beroAri();

		// args
		$m = $_GET['m'];
		$q = $_GET['q'];
		$start = $_GET['start'];
		if (!$bc->valid()) {
			$ret.= '<h3>Error:</h3><b>Failed to connect to config database!</b><br>';
			exit(0);
		}


		//Äderungen speichern
		switch ($_POST['action']) {
			case 'apply':
				if ($_POST['ipaddr']!=$_POST['gateway']){
					$result=exec("/usr/sbin/netconfigverify ".$_POST['ipaddr']." ".$_POST['netmask']." ".$_POST['gateway']." 2>&1");
					if (substr_count($result,"Failed")>0){
						$ret.="<br><b><font color=red>".NETWORK_FAILED_ERROR_DIS."</font></b>";
					}else{
						$bc->set("root", "lan-dhcp", $_POST['dhcp']);
						$bc->set("root", "lan-ipaddr", $_POST['ipaddr']);
						$bc->set("root", "lan-netmask", $_POST['netmask']);
						$bc->set("root", "lan-gateway", $_POST['gateway']);
						$bc->set("root", "lan-mtu", $_POST['lan_mtu']);
						$bc->set("root", "lan-nameserver", $_POST['lan-nameserver']);
						if (isset($_POST['vlanenable'])){
							$bc->set("root","vlan-enable",$_POST['vlanenable']);
							$bc->set("root","vlan-id",$_POST['vlanid']);
							$bc->set("root","vlan-prio",$_POST['vlan-prio']);
						}else{
							 $bc->set("root","vlan-enable","0");
							 $bc->set("root","vlan-id","");
							$bc->set("root","vlan-prio","");
						}

						$routing="";
						for ($i=0; $i<count($_POST['route_net']);$i++){
							if ($_POST['route_net'][$i]!="" && $_POST['route_netmask'][$i]!="" && $_POST['route_gateway'][$i]!=""){
								($routing=="") ? $routing.=$_POST['route_net'][$i]."/".$_POST['route_netmask'][$i]."/".$_POST['route_gateway'][$i] : $routing.=",".$_POST['route_net'][$i]."/".$_POST['route_netmask'][$i]."/".$_POST['route_gateway'][$i];
							}
						}
						$bc->set("root","routing",$routing);



						print_r($result);
						$query=$ba->select("select option from activate where id='activate'");
						while($entry = $ba->fetch_array($query)){
							if ($entry['option'] <= 3) $query =  $ba->update("UPDATE activate SET option=3 where id='activate'");
						}
						$ret.= "<script>aktivieren(1,'net_conf','3','".ACTIVATE."');</script>";
						$ret.="<br><b><font color=red>".NETWORK_CONFIGURATION_APPLIED_DIS."</font></b>	";
					}
			 	}else{
			 		$ret.= "<br><b><font color=red>".NETWORK_FAILED_ERROR_DIS."</font></b>";
				}
				break;
		}

		$iface = $bc->get("root", "lan-iface");
		$dhcp = strcasecmp("yes", $bc->get("root", "lan-dhcp")) == 0;
		$ret.="<script>
		function show_vlan(ch){
			if (!ch){
				document.getElementById('vlan_id').style.display='none';
				document.getElementById('vlan_prio').style.display='none';
			}else{
				document.getElementById('vlan_id').style.display='';
				document.getElementById('vlan_prio').style.display='';
			}
		}
		</script>";
		$ret.='<br/><form class="extensions_form" name="networkform" method="POST" action="" onsubmit="return (verifyIP(ipaddr.value,\'IP Address\') && verifyIP(netmask.value,\'Netmask\') && verifyIP(gateway.value,\'Gateway\') && check(lan_mtu.value) && checkvlan(vlanenable.checked,vlanid.value) && check_routing());">
			<input type="hidden" name="action" value="apply">
			<table id="table" class="contenttoc" width="35%">
				<tr>
				<th colspan="2">'.NETWORK_TABLE_HEADER_DIS.'</th>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="radio" name="dhcp" onmouseover=\'Tip("' . NET_CONF_DHCP . '")\' onmouseout="UnTip()" value="yes"' . ( $dhcp ? ' checked' : '' ) . '>DHCP</input>
						<input type="radio" name="dhcp" onmouseover=\'Tip("' . NET_CONF_STATIC . '")\' onmouseout="UnTip()" value="no"' . ( $dhcp ? '' : ' checked' ) . '>'.NETWORK_STATIC_DIS.'</input>
					</td>
				</tr>
				<tr>
					<td style="text-align:center">'.NETWORK_IP_ADDRESS_DIS.': </td>
					<td>
						<input name="ipaddr" type="text" size="15" id="ipaddr" onmouseover=\'Tip("' . NET_CONF_IP_ADDRESS . '")\' onmouseout="UnTip()" value="' . $bc->get("root", "lan-ipaddr") . '">
					</td>
				</tr>
				<tr>
					<td style="text-align:center">'.NETWORK_NETMASK_DIS.': </td>
					<td>
						<input name="netmask" type="text" size="15" id="netmask"
							onclick="if (netmask.value==\'\') netmask.value=makeNetMask(ipaddr.value);"
							onmouseover=\'Tip("' . NET_CONF_NETMASK . '")\' onmouseout="UnTip()" value="' . $bc->get("root", "lan-netmask") . '">
					</td>
				</tr>
				<tr>
					<td style="text-align:center">'.NETWORK_GATEWAY_DIS.': </td>
					<td>
						<input name="gateway" type="text" size="15" id="gateway" onmouseover=\'Tip("' . NET_CONF_GATEWAY . '")\' onmouseout="UnTip()" value="' . $bc->get("root", "lan-gateway") . '">
					</td>
				</tr>
				<tr>
					<td style="text-align:center">'.NETWORK_NAME_SERVER_DIS.': </td>
					<td>
						<input name="lan-nameserver" type="text" size="15" id="lan-nameserver" onmouseover=\'Tip("'.NET_CONF_NAMESERVER.'")\' onmouseout="UnTip()" value="' . $bc->get("root", "lan-nameserver") . '">
					</td>
				</tr>
				<tr>
					<th colspan="2">
						'. NETWORK_ADVANCED_DIS .'
					</th>
				</tr>
				<tr>
					<td style="text-align:center">'.NETWORK_MTU_SIZE_DIS.': </td>
					<td>
						<input name="lan_mtu" type="text" size="15" id="lan_mtu" onmouseover=\'Tip("'.NET_CONF_MTU_SIZE.'")\' onmouseout="UnTip()" value="' . $bc->get("root", "lan-mtu") . '">
					</td>
				</tr>
				<tr>
					<td style="text-align:center">'.NETWORK_VLAN_ENABLE_DIS.': </td>
					<td>';
					if ($bc->get("root", "vlan-enable")==1)
						$ret.='<input name="vlanenable" type="checkbox" id="vlanenable" onmouseover=\'Tip("'.NET_CONF_VLAN_ENABLE.'")\' onmouseout="UnTip()" onclick="show_vlan(document.getElementById(\'vlanenable\').checked);" value="1" checked>';
					else
						$ret.='<input name="vlanenable" type="checkbox" id="vlanenable" onmouseover=\'Tip("'.NET_CONF_VLAN_ENABLE.'")\' onmouseout="UnTip()" onclick="show_vlan(document.getElementById(\'vlanenable\').checked);" value="1">';
					$ret.='</td>
				</tr>
				<tr id="vlan_id">
					<td style="text-align:center">'.NETWORK_VLAN_ID_DIS.': </td>
					<td>
						<input name="vlanid" type="text" size="15" id="vlanid" onmouseover=\'Tip("'.NET_CONF_VLAN_ID.'")\' onmouseout="UnTip()" value="' . $bc->get("root", "vlan-id") . '">
					</td>
				</tr>
				<tr id="vlan_prio">
					<td style="text-align:center">'.NETWORK_VLAN_PRIORITY_DIS.': </td>
					<td>
						'.$this->vlan_prio().'
					</td>
				</tr>
				'.$this->routing().'
				<tr id="add_route">
					<td style="text-align:center" colspan="2">
						<input type="button" id="add_route_button" value="'.BUTTON_ADD_ROUTE_ENTRY.'">
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="reset" name="reset" value="'.BUTTON_RESET.'" onmouseover=\'Tip("' . NET_CONF_RESET . '")\' onmouseout="UnTip()">&nbsp;
						<input type="submit" name="Apply" value="'.BUTTON_SAVE.'" onmouseover=\'Tip("' . NET_CONF_APPLY . '")\' onmouseout="UnTip()">
					</td>
				</tr>
			</table>
			</form>
			<script>show_vlan(document.getElementById("vlanenable").checked);
				$("#add_route_button").click(function() {
					$("#add_route").before("<tr>"+
												"<td>Route target</td>"+
												"<td><input name=\'route_net[]\' class=\'net\'>/<input name=\'route_netmask[]\' size=\'2\' class=\'netmask\' maxlength=\'2\'></td>"+
											"</tr>"+
											"<tr>"+
												"<td>Route gateway</td>"+
												"<td><input name=\'route_gateway[]\' class=\'gateway\' size=\'24\'></td>"+
											"</tr>");
					paint();
				});

				function check_routing(){
					var ret=true;
					$(".net").each(function() {
						if ($(this).attr("value")!="") if (verifyIP($(this).attr("value"),\'Route taget IP\')==false) ret=false;
					});
					$(".gateway").each(function() {
						if ($(this).attr("value")!="") if (verifyIP($(this).attr("value"),\'Route gateway\')==false) ret=false;
					});
					$(".netmask").each(function() {
						if ($(this).attr("value")!="") if (verify_range($(this).attr("value"), \'Route netmask\')==false) ret=false;
					});
					return ret;
				}
			</script>';

		return $ret;
	}

	function vlan_prio(){
		$bc = new beroConf('root');
		$bits = array("000","001","010","011","100","101","110","111");
		$cbox='<select name="vlan-prio" onmouseover=\'Tip("' . NET_CONF_VLAN_PRIO . '")\' onmouseout="UnTip()">';
		$i=0;
		foreach ($bits as $key=>$value){
			if ($bc->get("root","vlan-prio")==$value)
				$cbox.="<option value='".$value."' selected>".$i++."</option>";
			else
				$cbox.="<option value='".$value."'>".$i++."</option>";
		}
		$cbox.='</select>';
		return $cbox;

	}

	function routing(){
		$bc = new beroConf('root');
		$result="";
		if ($bc->get("root","routing")!=""){
			$routes=explode(",",$bc->get("root","routing"));
			foreach($routes as $value){
				list($route_net, $route_netmask, $route_gateway) = explode("/", $value);
				$result.="<tr>
							<td>Route target</td>
							<td><input name='route_net[]' class='net' value='$route_net'>/<input name='route_netmask[]' size='2' maxlength='2' class='netmask' value='$route_netmask'></td>
						</tr>
						<tr>
							<td>Route gateway</td>
							<td><input name='route_gateway[]' class='gateway' value='$route_gateway' size='24'></td>
						</tr>";
			}
		}
		return $result;
	}


}


?>
