<?php
class Berofos{
	
	public function display($args) {
		$ba = new beroAri();
			include '/usr/local/php/beroConf.php';
			$bc=new beroConf('root');
		
		if ($_POST['action']=='Activate'){
			
			system("killall -9 bnfos");
			if($_POST['ip_address']!=""){
				//$ipad="-h ".$_POST['ip_address'];
			}
			if($_POST['mac_address']!=""){
				//$macad="-m ".$_POST['mac_address'];
			}
			if($_POST['interval']!=""){
				//$interval="-t ".$_POST['interval'];
			}
			system("bnfos --kick -b -c -h ".$_POST['ip_address']." -m ".$_POST['mac_address']." -w".$_POST['interval']);
			
			
		}if ($_POST['action']=='Deactivate'){
			//kill any running bnfos
			system("killall -9 bnfos");
		}
		if($_POST['action']=='Save'){
			$ba->set("ip_berofos",$_POST['ip_address']);
			$ba->set("mac_berofos",$_POST['mac_address']);
			$ba->set("interval_berofos",$_POST['interval']);
			if(isset($_POST['use_boot_bnfos_wdog'])){
				$bc->set("root","use_boot_bnfoswdog",'1');
			}else{
				$bc->set("root","use_boot_bnfoswdog",'0');
			}
			$retps=exec("ps x|grep bnfos|grep -v 'grep\|sed'|wc -l");
			if($retps==1){
				system("killall -9 bnfos");
				system("bnfos --kick -b -c -h ".$_POST['ip_address']." -m ".$_POST['mac_address']." -w".$_POST['interval']);
			}
			
		}
		
		include('./includes/header.php');
		
		$m = $_GET['m'];
		$berofos_table = $this->dynamic_table();
		// build page content
		$ret .= "
					<form class='isdnhw_form' name='isdn_hw' action='' method='POST' onsubmit='return (verify_number(interval.value,\"Interval\") && isValidMacAddress(mac_address.value));'>
					<input type=hidden name=m value=" . $m . ">
					<br>
					" . $berofos_table . "
					<br>
					</form>";

		//Rückgabe: Inhalt der HTML-Seite
		return $ret;
	}
	
	public function start_bnfos() {
	
	
	}
	
	public function stop_bnfos() {
	
	
	}
	public function dynamic_table() {
		$ba = new beroAri();
		$bc = new beroConf('root');
		require_once("./includes/lang.php");
		
		$retps=exec("ps x|grep bnfos|grep -v 'grep\|sed'|wc -l");
		$bnfosstate=$retps;
		/*
		if($retps==1){
			print("berofos Heartbeat is running");
		}
		else{
			print("berofos Heartbeat is not running");
		}*/
		
		
		$table="<table class='contenttoc' id='table'>
					<tr>
						<th colspan='2'>".BEROFOS_TABLE_HEADER_DIS."</th>
					</tr>
					<tr onmouseover=\"Tip('" . BNFOSIP . "')\" onmouseout='UnTip()'>
						<td>".BEROFOS_IP_ADDRESS_DIS."</td>
						<td><input type='text' name='ip_address' value='".$ba->get('ip_berofos')."'></td>
					</tr>
					<tr onmouseover=\"Tip('" . BNFOSMAC . "')\" onmouseout='UnTip()'>
						<td>".BEROFOS_MAC_ADDRESS_DIS."</td>
						<td><input type='text' name='mac_address' value='".$ba->get('mac_berofos')."'></td>
					</tr>
					<tr onmouseover=\"Tip('" . BNFOSINTERVALL . "')\" onmouseout='UnTip()'>
						<td>".BEROFOS_INTERVAL_DIS."</td>
						<td><input type='text' name='interval' value='".$ba->get('interval_berofos')."'></td>
					</tr>
					<tr onmouseover=\"Tip('" . BNFOSUSEBOOTWDOG . "')\" onmouseout='UnTip()'>
						<td>".BEROFOS_HEARTBEAT_ON_BOOT_DIS."</td>";
					if($bc->get('root','use_boot_bnfoswdog')=="1" || $bc->get('root','use_boot_bnfoswdog')=="yes"){
						$table.="<td><input type='checkbox' name='use_boot_bnfos_wdog' id='use_boot_bnfos_wdog' value='1' checked></td>";
					}else{
						$table.="<td><input type='checkbox' name='use_boot_bnfos_wdog' id='use_boot_bnfos_wdog' value='1'></td>";
					}
					$table.="</tr>
					<tr onmouseover=\"Tip('" . BNFOSSAVE . "')\" onmouseout='UnTip()'>
						<td colspan='2'><input type='submit' name='action' Value='".BUTTON_SAVE."'></td>
					</tr>
					<tr><td colspan='2'>&nbsp</td></tr>
					<tr>
						<th colspan='2'>".BEROFOS_HEARTBEAT_STATE_DIS."</th>
					</tr>
					<tr onmouseover=\"Tip('" . BNFOSSTATE . "')\" onmouseout='UnTip()'>";
					
					if($bnfosstate==1){
						$table.="<td><img src='./includes/images/punkt_gruen.gif'></td><td><input type='submit' name='action' value='".BUTTON_DEACTIVATE."'>&nbsp;&nbsp;</td>";
					}else{
						$table.="<td><img src='./includes/images/punkt_rot.gif'></td><td><input type='submit' name='action' value='".BUTTON_ACTIVATE."'>&nbsp;&nbsp;</td>";
						
					}
					

					$table.="</tr>
				</table>";
		return $table;
	}
}
?>
