<?php


/**
 * Klasse State
 * Hier werden Daten angezeigt über die Konfigurationen von SIP und ISDN
 */

class State {

	/*
	 * Die Funktion ports wird benutzt um alle Ports die bei SIP konfiguriert
	 * worden sind anzuzeigen.
	 */
	function ports() {
		//Ein neues Datenbankobjekt wird erzeugt.
		$ba = new beroAri();
		$qu = $ba->select("select * from isdn");
		//Alle Einträge von der Abfrage werden durchgelaufen
		while ($entry = $ba->fetch_array($qu)) {
			//Für jede Port wird ein checkbox erzeugt
				$select .= "<input type=checkbox name='ports[]' value='" . $entry['port'] . "'>" . $entry['port'] . "&nbsp;&nbsp;";

		}
		//Alle checkboxs werden zurückgegeben*/
		return $select;
	}
	/*
	 * Funktion display erzeugt den Inhalt von der HTML-Seite
	 */
	function display($args) {


		require_once "./includes/isgwtelnet.php";
		include('./includes/header.php');
		require_once("./includes/lang.php");
		require_once("./includes/variables.php");
		$ba = new beroAri();
		$isgw = new isgwtelnet();
		$result=$isgw->isgw_login();
		if ($result==false) {
			$ret.="<font size='3' color='red'>".STATE_ISWG_ERROR_DIS."</font>";
			return $ret;
		}
		$isdnresult=$isgw->isgw_isdnstate();
		$analogresult=$isgw->isgw_analogstate();
		$sipresult=$isgw->isgw_sipstate();
		$pcm_result=$isgw->isgw_pcmstate();
		$beroCapi_result=$isgw->isgw_berocapistate();
		$gsm_result=$isgw->isgw_gsmstate();

		if (isset($_POST['port'])){
			$ret.= "<br/><font color='red'>".$isgw->port_restart($_POST['port'])."</font>";
		}
		if (isset($_POST['lif_gsm'])){
			$isgw->port_restart_gsm($_POST['port_gsm'],$_POST['lif_gsm']);
			$ret.= "<br/><font color='red'>Restarted</font>";
		}


		//Die Informationen von dem Status von MISDN werden in eine Tabelle formatiert und in der
		//return-Variable der Funktion gespeichert

		$rows = "";
		$rows = explode("\n", $isdnresult);
		if (count($rows)>=4){
			$l1=file("/proc/l1_stats");
			foreach($l1 as $key => $value){
				$v=explode(";",$value);
				foreach($v as $werte){
					$wert=explode("=",$werte);
					if ($wert[0]=="port") {$port=$wert[1]; $l1_stats[$wert[1]]="";}
					if ($wert[0]=="physw") {$l1_stats[$port].="Physical Link Switch: ".$wert[1]."<br>";}
					if ($wert[0]=="crc") {$l1_stats[$port].="CRC Error: ".$wert[1]."<br>";}
					if ($wert[0]=="fr") {$l1_stats[$port].="Frame Resent: ".$wert[1]."<br>";}
					if ($wert[0]=="fs") {$l1_stats[$port].="Frame Resync: ".$wert[1]."<br>";}
					if ($wert[0]=="los") {$l1_stats[$port].="Loss of Signal: ".$wert[1];}
				}
			}

			$l2=file("/proc/l2_stats");
			foreach($l2 as $key => $value){
				$v=explode(";",$value);
				foreach($v as $werte){
					$wert=explode("=",$werte);
					if ($wert[0]=="port") {$port=$wert[1]; $l2_stats[$wert[1]]="";}
					if ($wert[0]=="mdl") {$l2_stats[$port].="L2 Error: ".$wert[1]."<br>";}
					if ($wert[0]=="frm") {$l2_stats[$port].="L2 Frame Error: ".$wert[1]."<br>";}
				}
			}

			$ret .= "<br><br>";
			$ret .= "<table class='contenttoc' width='95%'>";
			$ret .= "<th colspan=6>".STATE_ISDN_STATUS_DIS."</th>";
			$ret .= "<tr><td><b>".STATE_PORT_DIS."</b></td><td><b>".STATE_TYPE_DIS."</b></td><td><b>".STATE_PROT_DIS."</b></td>";
			$ret .= "<td><b>".STATE_L2LINK_DIS."</b></td><td><b>".STATE_L1LINK_DIS."</b></td><td></td></tr>";

			foreach ($rows as $value){
				$pos=strpos($value,"Port");
				if($pos !== false){
					$ret .= "<tr>";
					$tmp = split("Port ", $value);
					$port = split(" Type", $tmp[1]);
					$ret .= "<td>" . $port[0] . "</td>";
					$type = split(" Prot.", $port[1]);
					$ret .= "<td>" . $type[0] . "</td>";
					$prot = split(" L2Link", $type[1]);
					$ret .= "<td>" . $prot[0] . "</td>";
					$l2link = split(" L1Link:", $prot[1]);
					$l2link[0] = str_replace(" ", "", $l2link[0]);
					if ($l2link[0] == "UP") {
						$ret .= "<td><img src='./includes/images/punkt_gruen.gif' onmouseover=\"Tip('" . $l2_stats[$port[0]] . "')\" onmouseout='UnTip()'></td>";
					} else {
						$ret .= "<td><img src='./includes/images/punkt_rot.gif' onmouseover=\"Tip('" . $l2_stats[$port[0]] . "')\" onmouseout='UnTip()'></td>";
					}
					$l1link = split(" Blocked:", $l2link[1]);
					$l1link[0] = str_replace(" ", "", $l1link[0]);
					if ($l1link[0] == "UP") {
						$ret .= "<td><img src='./includes/images/punkt_gruen.gif' onmouseover=\"Tip('" . $l1_stats[$port[0]] . "')\" onmouseout='UnTip()'></td>";
					} else {
						$ret .= "<td><img src='./includes/images/punkt_rot.gif' onmouseover=\"Tip('" . $l1_stats[$port[0]] . "')\" onmouseout='UnTip()'></td>";
					}
					$ret .= "<td><form method='POST' class='extensions_form'>
								<input type='hidden' name='port' value='".$port[0]."'>
								<input type='submit' value='".BUTTON_RESTART."'></form></td></tr>";
				}//endif
			}//endforeach

			$ret .= "</table><br/><br/>";
		}

		//Die Informationen von dem Status von ANALOG werden in eine Tabelle formatiert und in der
		//return-Variable der Funktion gespeichert

		$rows = "";
		$rows = explode("\n", $analogresult);
		if (count($rows)>=4){
			$ret .= "<br><br>";
			$ret .= "<table class='contenttoc' width='95%'>";
			$ret .= "<th colspan=6>".STATE_ANALOG_STATUS_DIS."</th>";
			$ret .= "<tr><td><b>".STATE_PORT_DIS."</b></td><td><b>".STATE_TYPE_DIS."</b></td><td><b>".STATE_STATE_DIS."</b></td>";
			$ret .= "<td><b>".STATE_LINE_VOLTAGE_DIS."</b></td></tr>";

			foreach ($rows as $value){
				$pos=strpos($value,"Port");
				if($pos !== false){
					$ret .= "<tr>";
					$tmp = split("Port ", $value);
					$port = split("type:", $tmp[1]);
					$ret .= "<td>" . $port[0] . "</td>";
					$type = split("state:", $port[1]);
					$broken="<b><font color='red'>".$type[0]."</font></b>";
					$ret .= "<td>" .((trim($type[0])!="BROKEN") ? $type[0] : $broken) . "</td>";
					$state = split("linevoltage:", $type[1]);
					if (trim($state[0])=="UNKNOWN" || trim($state[0])=="FXS_LOWPOWER"){
						$state[0]="FXS_LOWPOWER";
						$ret .= "<td onmouseover=\"Tip('" . STATE_LOWPOWER_DIS. "')\" onmouseout='UnTip()'>" . $state[0] . "</td>";
					}else
						$ret .= "<td>" . $state[0] . "</td>";
					$linevoltage = split("pol:", $state[1]);
					$pol = split("rtlc:", $linevoltage[1]);
					if ($linevoltage[0] >= 30) {
						$ret .= "<td><img src='./includes/images/punkt_gruen.gif' onmouseover=\"Tip('" . STATE_LINE_VOLTAGE_DIS. ":". $linevoltage[0] . "<br>".STATE_POLARITY_DIS.":" .  $pol[0] . "')\" onmouseout='UnTip()'></td>";
					} else {
						$ret .= "<td><img src='./includes/images/punkt_rot.gif' onmouseover=\"Tip('" . STATE_LINE_VOLTAGE_DIS. ":". $linevoltage[0] . "<br>".STATE_POLARITY_DIS.":" .  $pol[0] . "')\" onmouseout='UnTip()'></td>";
					}
					$ret.="</tr>";
				}//endif
			}//endforeach

			$ret .= "</table><br/><br/>";
		}

		//Die Informationen von dem Status von GSM werden in eine Tabelle formatiert und in der
		//return-Variable der Funktion gespeichert

		$rows = "";
		$rows = explode("\n", $gsm_result);
		if (count($rows)>=4){
			$ret .= "<br><br>";
			$ret .= "<table class='contenttoc' width='95%'>";
			$ret .= "<th colspan=4>".STATE_GSM_STATUS_DIS."</th>";
			$ret .= "<tr><td><b>".STATE_PORT_DIS."</b></td><td><b>".STATE_PROVIDER_DIS."</b></td><td><b>".STATE_REG_STATUS_DIS."</b></td>";
			$ret .= "<td><b>".STATE_SIGNAL_QUALITY_DIS."</b></td></tr>";
			$where="";
			foreach ($rows as $value){
				$pos=strpos($value,"Port");
				if($pos !== false){
					$ret .= "<tr>";
					$tmp = explode("Port ", $value);
					$port = explode("Provider:", $tmp[1]);
					$ret .= "<td>" . $port[0] . "</td>";
					$provider = explode("Pin Counter:", $port[1]);
					$counter = explode("Reg Status:", $provider[1]);
					$ret .= "<td onmouseover=\"Tip('" . STATE_PIN_COUNTER_DIS. ":". $counter[0] . "')\" onmouseout='UnTip()'>" . $provider[0]  . "</td>";
					$state = explode("RSSI:", $counter[1]);
					if (trim($state[0])=="NOT READY"){ $src="./includes/images/punkt_rot.gif"; $tTip="Not ready";}
					elseif (trim($state[0])==0){ $src="./includes/images/punkt_rot.gif"; $tTip=STATE_REG_STATUS0_DIS;}
					elseif (trim($state[0])==1){ $src="./includes/images/punkt_gruen.gif"; $tTip=STATE_REG_STATUS1_DIS;}
					elseif (trim($state[0])==2){ $src="./includes/images/punkt_rot.gif"; $tTip=STATE_REG_STATUS2_DIS;}
					elseif (trim($state[0])==3){ $src="./includes/images/punkt_rot.gif"; $tTip=STATE_REG_STATUS3_DIS;}
					elseif (trim($state[0])==4){ $src="./includes/images/punkt_rot.gif"; $tTip=STATE_REG_STATUS4_DIS;}
					elseif (trim($state[0])==5){ $src="./includes/images/punkt_gruen.gif"; $tTip=STATE_REG_STATUS5_DIS;}

					$ret .= "<td onmouseover=\"Tip('$tTip')\" onmouseout='UnTip()'><img src='".$src."'></td>";
					$rssi = explode("BER:", $state[1]);
					$rssiNumber=explode(" ",trim($rssi[0]));
					if ($rssiNumber[0]==-1 || $rssiNumber[0]==99) $signal="-";
					elseif ($rssiNumber[0]>-60) $signal="VERY HIGH";
					elseif ($rssiNumber[0]<=-60 && $rssiNumber[0]>-75) $signal="HIGH";
					elseif ($rssiNumber[0]<=-75 && $rssiNumber[0]>-100) $signal="OK";
					elseif ($rssiNumber[0]<=-100) $signal="LOW";
					$ber=$rssi[1];
					($where=="") ? $where="port=".$port[0] : $where.=" or port=".$port[0];
					$ret .= "<td onmouseover=\"Tip('" . STATE_RSSI_DIS. ":". $rssi[0] . "<br>".STATE_BER_DIS.":".$ber."')\" onmouseout='UnTip()'>" . $signal . "</td></tr>";

				}//endif
			}//endforeach
			$ret .= "<tr><td colspan='4'>";
			$query=$ba->select("select distinct lif from gsm where ".$where);
			while ($entry = $ba->fetch_array($query)) {
				$ret .= "<form method='POST' class='extensions_form'>";
				$ret .= "<input type='hidden' name='lif_gsm' value='".$entry['lif']."'>";
				$queryPort=$ba->select("select port from gsm where lif=".$entry['lif']);
				while ($entryPort = $ba->fetch_array($queryPort)) {
					$ret .= "<input type='hidden' name='port_gsm[]' value='".$entryPort['port']."'>";
				}
				$ret .= "<input type='submit' onclick='document.getElementById(\"wait_div\").style.display=\"block\";' value='".BUTTON_RESTART." LI ".$entry['lif']."'></form>";
			}

			$ret .= "</td></tr>";
			$ret .= "</table><br/><br/>";
		}



		//Sip Resgistration
		$ret .= "<br/><br/>";
		$ret .= "<table class='contenttoc' width='95%'>";
		$ret .= "<tr><th colspan=4>".STATE_REGISTRATION_STATUS_DIS.":</th></tr>";
		$ret .= "<tr><td><b>".STATE_ACCOUNTNAME_DIS."</b></td><td><b>".STATE_REGISTRAR_DIS."</b></td><td><b>".STATE_USER_DIS."</b></td>";
		$ret .= "<td><b>".STATE_STATUS_DIS."</b></td></tr>";

		//echo $sipresult;
		$rows = explode("\n", $sipresult);
		$i=0;
		for ($j=0;$j < count($rows)-3;$j++){
	//	foreach ($rows as $value){
			if ($i>0){
				if (in_array($i,array(1,3,4))){
					$sp = split("=>", $rows[$j]);
					$v = str_replace(" ", "", $sp[1]);
					$ret.="<td>".$v."</td>";
				}
				if ($i == 5) {
					$sp = split("=>", $rows[$j]);
					$state = str_replace(" ", "", $sp[1]);
					if ($state == "REGISTERED") {
						$ret .= "<td><img src='./includes/images/punkt_gruen.gif'></td>";
					} else {
						$ret .= "<td><img src='./includes/images/punkt_rot.gif'></td>";
					}
				}

			}else{
				$ret.="<tr>";
			}
			$i++;
			if ($i==10) {
				$ret .= "</tr>";
				$i=0;
			}
		}
		$ret .= "</table><br/></br>";

		//PCM Status. Wird nur angezeigt wenn PCM konfiguriert ist
		$ba = new beroAri();
		$pcm_server = $ba->get("pcm_server");
		if ($pcm_server!="0"  && $pcm_server!=""){


			$ret .= "<br/></br>";
			$ret .= "<table class='contenttoc' width='95%'>";
			if ($pcm_server=="1")
				$ret .= "<tr><th>".STATE_PCM_SERVER_STATUS_DIS.":</th>";
			else
				$ret .= "<tr><th>".STATE_PCM_CLIENT_STATUS_DIS.":</th>";

			//echo $sipresult;
			$rows = explode("\n", $pcm_result);
			$status=false;
			$ip="";
			for ($j=0;$j < count($rows)-2;$j++){
				$v = explode("\t", $rows[$j]);
				for ($i=0;$i < count($v);$i++){
					if ($i==1 && $j!=0 && $v[$i]!=ip_address){
						$status=true;
						$ip=$v[$i];
					}
				}
			}
			if ($status){
				$ret.="<th><img src='./includes/images/punkt_gruen.gif'></th></tr>";
				if ($pcm_server=="1")
					$ret .= "<tr><td>".STATE_PCM_CLIENT_IP_DIS.":</td><td>$ip</td></tr>";
				else
					$ret .= "<tr><td>".STATE_PCM_SERVER_IP_DIS.":</td><td>$ip</td></tr>";
			}else
				$ret.="<th><img src='./includes/images/punkt_rot.gif'></th></tr>";




			$ret .= "</table>";
		}

		//Die Informationen von dem Status von beroCapi werden in eine Tabelle formatiert und in der
		//return-Variable der Funktion gespeichert

		$rows = "";
		$rows = explode("\n", $beroCapi_result);

		if (count($rows)>=5){
		$ret .= "<br><br>";
		$ret .= "<table class='contenttoc' width='95%'>";
		$ret .= "<th colspan=3>".STATE_BEROCAPI_STATUS_DIS."</th>";
		$ret .= "<tr><td><b>".STATE_CAPI_INSTANCE_DIS."</b></td><td><b>".STATE_LICENSE_DIS."</b></td><td><b>".STATE_STATE_DIS."</b></td></tr>";

		foreach ($rows as $value){
			$pos=strpos($value,"VIA URL");
			if($pos !== false){
				$ret .= "<tr>";
				$tmp = explode(":", $value);
				$ret .= "<td>" . $tmp[1] . "</td>";
			}//endif
			$pos=strpos($value,"LICENSE");
			if($pos !== false){
				$tmp = explode(":", $value);
				$license=explode("=",$tmp[1]);
				$ret .= "<td>" . $license[1] . "</td>";
				$ret .= "<td><img src='./includes/images/punkt_gruen.gif'></td></tr>";
			}//endif
		}//endforeach


		$ret .= "</table><br/><br/>";
		}

		//Die Datei sytem.log


		if (is_file("/tmp/system.log")){
			$fileContent=file_get_contents("/tmp/system.log");
			$ret .= "<br><br>";
			$ret .= '<script type="text/javascript">
					window.setInterval("rel()", 10000);
					function rel(){
						$.ajax({ url: \'misc/fileContent.php?filename=/tmp/system.log\', success:
								function(data) {
									var test=false;
									var bottom = $("#systemLog").attr("scrollHeight");
									//alert(bottom);
									//alert($(\'#systemLog\').scrollTop());
									if(($(\'#systemLog\').scrollTop()+200) == parseInt(bottom)){ test=true;}
									$(\'#systemLog\').html(data);
									if (test){
										var bottom = $("#systemLog").attr("scrollHeight");
										$(\'#systemLog\').scrollTop(bottom);
									}
								}
							});
					}
					</script>';

			$ret .= "<div style=' width:910px; font-size: 11pt; font-weight: bold;'>System log</div>
					<div id='systemLog' style='overflow:auto; padding-left:10px; background-color: #F0F0F0; border: 1px solid #CCCCCC;height:200px; text-align:left; width:950px;' >".str_replace("\n","<br/>",$fileContent)."</div>";
			$ret .= '<script type="text/javascript">
						var bottom = $("#systemLog").attr("scrollHeight");
						$(\'#systemLog\').scrollTop(bottom);
					</script>';
		}

		return $ret;
	}

}
?>
