<?php


/**
 * Klasse Fulltrace
 */

class Fulltrace {
	/*
	 * Die Funktion ports wird benutzt um alle ISDN Ports 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'] . "' checked>" . $entry['port'] . "&nbsp;
			&nbsp;";

		}
		//Alle checkboxs werden zurückgegeben*/
		return $select;
	}

	function udp() {
		//Ein neues Datenbankobjekt wird erzeugt.
		$ba = new beroAri();
		$qu = $ba->select("select bindport from sip_general");
		return sqlite_fetch_single($qu);
	}
	/*
	 * Funktion display erzeugt den Inhalt von der HTML-Seite
	 */
	function display($args) {

		$ba = new beroAri();

		require_once "./includes/amifunc.php";
		include('./includes/header.php');
		require_once ('./includes/isgwtelnet.php');
		$ret.= "<script language='javascript'>function checkallp(){
			var checkall = document.getElementById('checkall');
		var del = document.getElementsByName('ports[]');
		if (checkall.checked==true){
			for (i=0; i < del.length; i++){
				del[i].checked=true;
			}
		}else{
			for (i=0; i < del.length; i++){
				del[i].checked=false;
			}
		}

	}
	function dis(){
		var isdn_enable = document.getElementById('isdn_enable');
		var checkall = document.getElementById('checkall');
		var del = document.getElementsByName('ports[]');
		if (isdn_enable.checked==true){
			checkall.disabled=false;
			for (i=0; i < del.length; i++){
				del[i].disabled=false;
			}
		}else{
			checkall.disabled=true;
			for (i=0; i < del.length; i++){
				del[i].disabled=true;
			}
		}
	}
	</script>";
	$colspan=2;
	$th="";
	$td="";
	$query_gsm=$ba->select("select * from gsm");
	if(sqlite_num_rows($query_gsm)>0){
		$colspan++;
		$th.="<td>GSM</td>";
		$td.="<td><input type='checkbox' value='1' name='gsm' id='gsm' checked><label for='gsm'>".FULL_TRACE_ENABLE_DIS."</label></td>";
		$colspan++;
		$th.="<td>GSM SMS</td>";
		$td.="<td><input type='checkbox' value='1' name='gsm_sms' id='gsm_sms'><label for='gsm_sms'>".FULL_TRACE_ENABLE_DIS."</label></td>";
	}
	$query_analog=$ba->select("select * from analog");
	if(sqlite_num_rows($query_analog)>0){
		$colspan++;
		$th.="<td>ANALOG</td>";
		$td.="<td><input type='checkbox' value='1' name='analog' id='analog'  checked><label for='analog'>".FULL_TRACE_ENABLE_DIS."</label></td>";
	}

	//Die Tabelle MISDN Trace
	if (($ba->get("trace")=="" || $ba->get("trace")=="no") && !isset($_POST['trace'])) {
		$ret .= "<br/><form class='extensions_form' method='POST'>
			<table class='contenttoc' id='table' style='width:473px;'>
			<tr><th colspan='$colspan'>".FULL_TRACE_TABLE_HEADER_DIS."</th></tr>
			<tr>
			<td>".FULL_TRACE_ISDN_PORTS_DIS."</td><td>".FULL_TRACE_SIP_DIS."</td>$th
			</tr>
			<tr>
			<td nowrap>
			<input type='checkbox' name='isdn_enable' id='isdn_enable' value='1' onclick='dis();' checked/>".FULL_TRACE_ENABLE_DIS."
			<br/>
			<b>".FULL_TRACE_CHECK_ALL_DIS."</b>
			<input type='checkbox' id='checkall' name='checkall' onclick='checkallp();' onmouseover=\"Tip('".DEBUG_CHECK_ALL."')\" onmouseout='UnTip()'>
			<br/>" . $this->ports() .
			"<br/>
			<br/>
			</td>
			<td>
			<input type='checkbox' name='sip_sign' id='sip_sign' value='1' onclick='if (sip_sign.checked==true) sip_audio.disabled=false; else sip_audio.disabled=true;' checked/>Sip Sign
			<br/>
			<input type='checkbox' name='sip_audio' id='sip_audio' value='1' /><label for='sip_audio'>Sip Audio</label>
			</td>
			$td
			</tr>
			<tr>
			<td colspan='$colspan'>
			<img src='./includes/images/punkt_rot.gif'>&nbsp;&nbsp;&nbsp;
			<input type='submit' id='trace' name='trace' onmouseover=\"Tip('".DEBUG_START_TRACE."')\" onmouseout='UnTip()' value='".FULL_TRACE_BUTTON_START_DIS."'>
			</td>
			</tr>
			</table></form><br/>";
		/* Wenn man auf den Knopf Start geclickt hat, wird die module mISDN_debugtool.ko geladen
		   und das mISDNdebugtool mit den ausgewählten Ports ausgeführt */
	}elseif ($_POST['trace'] == "Start" ) {
		$ports = "";
		//Ports string vorbereiten
		if ($_POST['ports']) {
			foreach ($_POST['ports'] as $value) {
				if ($ports != "")
					$ports .= "," . $value;
				else
					$ports .= $value;
			}
		}

		if (($ports != "" && $_POST['isdn_enable']=='1') || ($_POST['sip_sign']=='1')) {
			//Loglevel auf das Maximum erhöhen.
			$isgw = new isgwtelnet();
			$isgw->isgw_login();
  			$isgw->logactive(1);
  			$isgw->loglevel(9);
  			$isgw->lla();
  			$isgw->isgw_login();
			$cont=$isgw->isgw_isdnstate();
			$cont.=$isgw->isgw_actualcalls();
			$cont.=$isgw->isgw_bchannels();
			$cont.=$isgw->isgw_callcompact();
			$fp= fopen("/tmp/isgw.info2","w+");
			$write=fputs($fp,$cont);
			fclose($fp);

                        //clear tmp/
                        exec("/bin/rm -rf /tmp/fulltrace-info.txt");
                        exec("/bin/rm -rf /tmp/ISDNdebug-*");
                        exec("/bin/rm -rf /tmp/tcpDump");
                        exec("/bin/rm -rf /tmp/tcpDump.lo");
                        exec("/bin/rm -rf /tmp/analog_debug*");
                        exec("/bin/rm -rf /tmp/DebugFiles.tar.gz");

                        //Trace starten
			$ba->set("trace","yes");
			if ($ports != "" && $_POST['isdn_enable']=='1'){
				exec("/sbin/insmod /usr/local/modules/mISDN_debugtool.ko");
				exec("/usr/local/bin/mISDNdebugtool -d -s 5000 -f /tmp/ISDNdebug -p " . $ports);
			}

			if ($_POST['analog']=='1'){
				exec("/usr/local/sbin/analog-debugtool > /dev/null &");
			}

			if ($_POST['gsm']=='1'){
				exec("/usr/local/sbin/gsm-debugtool > /dev/null &");
			}

			if ($_POST['sip_sign']=='1' && $_POST['sip_audio']=='1'){
				exec("/usr/local/sbin/tcpdump -s0 -w /tmp/tcpDump -U -C 2 > /dev/null &");
				exec("/usr/local/sbin/tcpdump -i lo -s0 -w /tmp/tcpDump.lo -U -C 2 > /dev/null &");
			}

			if ($_POST['sip_sign']=='1' && !isset($_POST['sip_audio'])){
				if ($_POST['gsm_sms']=='1') $sms_port=" or tcp port 25 or tcp port 110"; else $sms_port="";
				exec("/usr/local/sbin/tcpdump -s0 -w /tmp/tcpDump -U -C 2 port ".$this->udp().$sms_port." > /dev/null &");
				exec("/usr/local/sbin/tcpdump -i lo -s0 -w /tmp/tcpDump.lo -U -C 2 port ".$this->udp().$sms_port." > /dev/null &");
			}

			$ret .= "<br/><form class='extensions_form' method='POST'>" .
				"<table class='contenttoc' style='width:473px;'><tr><th colspan='2'>".FULL_TRACE_TABLE_HEADER_DIS."</th></tr><tr>" .
				"<td><img src='./includes/images/punkt_gruen.gif'></td>
				<td>
				<input type='submit' id='trace' name='trace' onmouseover=\"Tip('".DEBUG_STOP_TRACE."')\" onmouseout='UnTip()' value='Stop Full Trace'>
				</td></tr></table></form><br/>";
			//Wenn kein Port ausgewählt wurde, wird die Anfangstabelle angezeigt.
		} else {
			$ret .= "<br/><form class='extensions_form' method='POST'>
			<table class='contenttoc' style='width:473px;'>
			<tr><th colspan='$colspan'>".FULL_TRACE_TABLE_HEADER_DIS."</th></tr>
			<tr>
			<td>".FULL_TRACE_ISDN_PORTS_DIS."</td><td>".FULL_TRACE_SIP_DIS."</td>$th
			</tr>
			<tr>
			<td nowrap>
			<input type='checkbox' name='isdn_enable' id='isdn_enable' value='1' onclick='dis();' checked/>".FULL_TRACE_ENABLE_DIS."
			<br/>
			<b>".FULL_TRACE_CHECK_ALL_DIS."</b>
			<input type='checkbox' id='checkall' onclick='checkallp();' onmouseover=\"Tip('".DEBUG_CHECK_ALL."')\" onmouseout='UnTip()'>
			<br/>" . $this->ports() .
			"<br/>
			<br/>
			</td>
			<td>
			<input type='checkbox' name='sip_sign' id='sip_sign' value='1' onclick='if (sip_sign.checked==true) sip_audio.disabled=false; else sip_audio.disabled=true;' checked/>Sip Sign
			<br/>
			<input type='checkbox' name='sip_audio' id='sip_audio' value='1' />Sip Audio
			</td>
			$td
			</tr>
			<tr>
			<td colspan='$colspan'>
			<img src='./includes/images/punkt_rot.gif'>&nbsp;&nbsp;&nbsp;
			<input type='submit' id='trace' name='trace' onmouseover=\"Tip('".DEBUG_START_TRACE."')\" onmouseout='UnTip()' value='".FULL_TRACE_BUTTON_START_DIS."'>
			</td>
			</tr>
			</table></form><br/>";
		}

	//Wenn man auf den Knopf Stop clickt, wird die Module mISDNdebugtool gekillt
	}elseif ($_POST['trace'] == "Stop Full Trace") {
		$ba->set("trace","no");
		$isgw = new isgwtelnet();
		$isgw->isgw_login();
		$cont=$isgw->isgw_isdnstate();
		$cont.=$isgw->isgw_actualcalls();
		$cont.=$isgw->isgw_bchannels();
		$cont.=$isgw->isgw_callcompact();
		$fp= fopen("/tmp/isgw.info1","w+");
		$write=fputs($fp,$cont);
                fclose($fp);


                system("/usr/local/sbin/generate_info.sh /tmp/fulltrace-info.txt");
		system("cat /proc/l1_stats > /tmp/l1_stats");
		system("cat /proc/l2_stats > /tmp/l2_stats");

		unset($_SESSION['IsdnTcp']);
		system("/bin/sync");
		system("/usr/bin/killall tcpdump");
		system("/usr/bin/killall mISDNdebugtool");
		system("/usr/bin/killall -9 analog-debugtool");
		system("/usr/bin/killall -9 gsm-debugtool");
		system("/sbin/rmmod mISDN_debugtool");
		system("/bin/sync");
		$query = $ba->select("select logging_active from general");
		while ($entry = $ba->fetch_array($query)) {
		}
		//Logging Einstellungen auf die gespeicherte Daten zurücksetzen
		$isgw = new isgwtelnet();
  		$isgw->isgw_login();
		$isgw->logactive(0);
		$isgw->loglevel(0);
		$isgw->lld();
		//tar.gz erzeugen
		exec("/bin/rm /tmp/DebugFiles.tar.gz");
		$isdnPorts = Helper::getIsdnPorts();
		$files = "";
		foreach ($isdnPorts as $value) {
			if (file_exists("/tmp/ISDNdebug-$value")) {
				if ($files != "")
					$files .= " ISDNdebug-" . $value;
				else
					$files .= "-C /tmp/ ISDNdebug-" . $value;
			}
		}
		if (file_exists("/tmp/tcpDump")) {
			if ($files != "")
				$files .= " tcpDump";
			else
				$files .= "-C /tmp/ tcpDump";
		}
		if (file_exists("/tmp/tcpDump.lo")) {
			if ($files != "")
				$files .= " tcpDump.lo";
			else
				$files .= "-C /tmp/ tcpDump.lo";
		}

		$err_files=glob("/tmp/isgw.err*");
		foreach($err_files as $file) {
			//echo basename($file)."<br/>";
			$files.=" ".basename($file);
		}
		$log_files=glob("/tmp/isgw.log*");
		foreach($log_files as $file) {
			//echo basename($file)."<br/>";
			$files.=" ".basename($file);
		}


		$files.=" fulltrace-info.txt";
		$files.=" debug.txt";
		$files.=" isgw.info1";
		$files.=" isgw.info2";
		$files.=" l1_stats";
		$files.=" l2_stats";
		if (file_exists("/tmp/sofia.log")) $files.=" sofia.log";
		if (file_exists("/tmp/smgw.log")) $files.=" smgw.log";
		if (file_exists("/tmp/smgw.err")) $files.=" smgw.err ";
		if (file_exists("/tmp/analog_debug.log")) $files.=" analog_debug.log";
		if (file_exists("/tmp/analog_debug.log.1")) $files.=" analog_debug.log.1";
		if (file_exists("/tmp/gsm_debug.log")) $files.=" gsm_debug.log";
		if (file_exists("/tmp/gsm_debug.log.1")) $files.=" gsm_debug.log.1";
		if (file_exists("/tmp/system.log")) $files.=" system.log";
		if (file_exists("/usr/local/repo_info.txt")) $files.=" /usr/local/repo_info.txt";
		$files.=" /usr/conf ";

		#echo $files;
		exec("/bin/tar -czf /tmp/DebugFiles.tar.gz " . $files);
		system("/bin/sync");
		//exec("/bin/rm /tmp/ISDNdebug*");
		//exec("/bin/rm /tmp/tcpDump");
		//exec("/bin/rm /tmp/analog_debug*");
		$ba->set("trace","no");
		$ret .= "<br/><form class='extensions_form' method='POST'>
			<table class='contenttoc' style='width:473px;'>
			<tr><th colspan='$colspan'>".FULL_TRACE_TABLE_HEADER_DIS."</th></tr>
			<tr>
			<td>".FULL_TRACE_ISDN_PORTS_DIS."</td><td>".FULL_TRACE_SIP_DIS."</td>$th
			</tr>
			<tr>
			<td nowrap>
			<input type='checkbox' name='isdn_enable' id='isdn_enable' value='1' onclick='dis();' checked/>".FULL_TRACE_ENABLE_DIS."
			<br/>
			<b>".FULL_TRACE_CHECK_ALL_DIS."</b>
			<input type='checkbox' id='checkall' onclick='checkallp();' onmouseover=\"Tip('".DEBUG_CHECK_ALL."')\" onmouseout='UnTip()'>
			<br/>" . $this->ports() .
			"<br/>
			<br/>
			</td>
			<td>
			<input type='checkbox' name='sip_sign' id='sip_sign' value='1' onclick='if (sip_sign.checked==true) sip_audio.disabled=false; else sip_audio.disabled=true;' checked/>Sip Sign
			<br/>
			<input type='checkbox' name='sip_audio' id='sip_audio' value='1' />Sip Audio
			</td>
			$td
			</tr>
			<tr>
			<td colspan='$colspan'>
			<img src='./includes/images/punkt_rot.gif'>&nbsp;&nbsp;&nbsp;
			<input type='submit' id='trace' name='trace' onmouseover=\"Tip('".DEBUG_START_TRACE."')\" onmouseout='UnTip()' value='".FULL_TRACE_BUTTON_START_DIS."'>
			</td>
			</tr>
			</table></form><br/>";
	//Wenn das Trace in Hindergrund läuft wird den Stop Knopf angezeigt
	}elseif ($ba->get("trace")=="yes"){
			$ret .= "<br/><form class='extensions_form' method='POST'>" .
				"<table class='contenttoc' style='width:473px;'><tr><th colspan='2'>".FULL_TRACE_TABLE_HEADER_DIS."</th></tr><tr>" .
				"<td><img src='./includes/images/punkt_gruen.gif'></td>
				<td>
					<input type='submit' id='trace' name='trace' onmouseover=\"Tip('".DEBUG_STOP_TRACE."')\" onmouseout='UnTip()' value='Stop Full Trace'>
				</td></tr></table></form><br/>";
	}

	/*
	 * Überprüfung ob die dateien debug-info.tar.gz oder DebugFiles.tar.gz oder isgw.err,
	 * Wenn ja, wird einen Link für Download angeboten, und einen Link delete
	 * um die entsprechende Datei zu löschen
	 */
	if (file_exists("/tmp/debug-info.tar.gz") || file_exists("/tmp/DebugFiles.tar.gz") || file_exists("/tmp/isgw.err")){
		$ret.="<form class='extensions_form' method='POST'><table class='contenttoc' style='width:473px;'>
				<tr><th colspan='2'>".FULL_TRACE_DOWNLOADS_DIS."</th></tr>";
		if (file_exists("/tmp/debug-info.tar.gz"))
		$ret.="<tr>
			<td nowrap style='text-align:left;'><b>".FULL_TRACE_ISGW_COREDUMPS_DIS."</b>&nbsp;&nbsp;
			<input type='button' value='".FULL_TRACE_BUTTON_DOWNLOAD_DIS."' onclick=\" window.location.href='misc/isdn_trace.php?option=isgw';\" onmouseover=\"Tip('".DEBUG_DOWNLOAD_TRACE."')\" onmouseout='UnTip()'>&nbsp;&nbsp;
			<input type='button' value='".FULL_TRACE_BUTTON_DELETE_FILE_DIS."' onclick=\"javascript:if (confirm('".FULL_TRACE_DELETE_COREDUMPS_DIS."?')==true) window.location.href='misc/deletedatei.php';\" onmouseover=\"Tip('".DEBUG_DELETE."')\" onmouseout='UnTip()'></td>
			<td>".date("d M Y H:i",filectime("/tmp/debug-info.tar.gz"))."</td>
			</tr>";
		if (file_exists("/tmp/DebugFiles.tar.gz"))
		$ret.="<tr>
			<td nowrap style='text-align:left;'><b>".FULL_TRACE_DEBUG_FILES_DIS."</b>&nbsp;&nbsp;
			<input type='button' value='".FULL_TRACE_BUTTON_DOWNLOAD_DIS."' onclick=\" window.location.href='misc/isdn_trace.php';\" onmouseover=\"Tip('".DEBUG_DOWNLOAD_TRACE."')\" onmouseout='UnTip()'>&nbsp;&nbsp;
			<input type='button' value='".FULL_TRACE_BUTTON_DELETE_FILE_DIS."' onclick=\"javascript:if (confirm('".FULL_TRACE_DELETE_DEBUG_FILES_DIS."?')==true) window.location.href='misc/deletedatei.php?option=debug';\" onmouseover=\"Tip('".DEBUG_DELETE_FILES."')\" onmouseout='UnTip()'>
			<td>".date("d M Y H:i",filectime("/tmp/DebugFiles.tar.gz"))."</td>
			</tr>";
		if (file_exists("/tmp/isgw.err"))
		$ret.="<tr>
			<td nowrap style='text-align:left;'><b>".FULL_TRACE_ERROR_LOG_DIS."</b>&nbsp;&nbsp;
			<input type='button' value='".FULL_TRACE_BUTTON_DOWNLOAD_DIS."' onclick=\" window.location.href='misc/isdn_trace.php?option=error';\" >&nbsp;&nbsp;
			<td>".date("d M Y H:i",filectime("/tmp/isgw.err"))."</td>
			</tr>";
		$ret.="</table></form><br/>";
	}


	//Rückgabe: Der Inhalt von der HTML-Seite
	return $ret;
}
}
?>
