<?php
//Popup für Dialplan Debug, seite wird alle 5 sekunden neugeladen
chdir("..");
include './includes/variables.php';
require_once ('./includes/isgwtelnet.php');
/*@session_start();
if((isset($_SESSION['beroari_time']))&&(($_SESSION['beroari_time']+1200)<time())){
	@session_unset();
	@session_destroy();
	echo "<script>window.opener.location='../index.php?m=debug'</script>";
	echo "<script>this.window.close();</script>";
}else{
	$_SESSION['beroari_time']=time();
}*/
function show_file(){
	$pid=0;
	$p=0;
	if (is_file("/tmp/dialplan.txt")){
		$zeilen=file("/tmp/dialplan.txt");
		$i=1;
		foreach ($zeilen as $zeile) {
			if ($i==1){
				$p=$zeile;
			}
			$i++;
		}
	}elseif (is_file("/tmp/dialplan_d.txt")){
		$zeilen=file("/tmp/dialplan_d.txt");
		$i=1;
		foreach ($zeilen as $zeile) {
			if ($i==1){
				$p=$zeile;
			}
			$i++;
		}
	}

	include "./misc/database.php";
	$ba = new beroAri();
	require_once("./includes/lang.php");

	

	$ret= "<html>
	<head>";
	$ret.="<link rel=\"icon\" href=\"../includes/images/favicon.ico\" type=\"image/x-icon\">
	<link rel=\"stylesheet\" href=\"../includes/template_css.css\" type=\"text/css\">
	<script type=\"text/javascript\" language=\"javascript\" src=\"../includes/js/jquery-1.5.1.js\"></script>";
	if ((!isset($_GET['stop']) && !isset($_GET['stopped'])) || isset($_GET['start']))
		$ret.="<script type=\"text/javascript\" language=\"javascript\">
				setInterval(function() {
				$('#content').load('./dialplan_debug.php?reload_ajax=Reload&pid=".trim($p)."','');
				}, 10000);
				</script>";
	$ret.="<title>".DIALPLAN_DEBUG_POPUP_TITLE_DIS."</title>
	</head>
	</head>
	<body>";
	$ret.= "<br><table align=\"center\" bgcolor=\"#f0f0ec\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"690\">
	<colgroup>
	<col width=\"39\"></col>
	</colgroup>
	<tbody><tr>
	<td colspan=\"2\"><img src=\"../includes/images/bg_top.png\"></td>
	</tr>
	<tr>
	<td background=\"../includes/images/bg_left.png\"  style='background-repeat:repeat-y;' width=\"30\"><img src=\"../includes/images/trans.gif\" height=\"10\" width=\"39\" border=\"0\"></td>
	<td><p align='left'><img src=\"../includes/images/berofixlogo.png\"/></p><hr size= \"1\" noshade><p><center><font size='3'>".DIALPLAN_DEBUG_TITLE_DIS."</font></p>";
	if ((isset($_GET['stop']) || isset($_GET['stopped'])) && !isset($_GET['start'])){
		$ret.="<p>".DIALPLAN_DEBUG_POPUP_STATE_OFF_DIS."</p>";		
	}else{
				$ret.="<p>".DIALPLAN_DEBUG_POPUP_STATE_ON_DIS."</p>";
	}
	$ret.="<form action='' method='GET' class='extensions_form'>
	<input type='submit' name='reload' Value='".BUTTON_RELOAD."'/>&nbsp;
	<input type='submit' name='clear' value='".BUTTON_CLEAR."'/>&nbsp;";
	
	if ((isset($_GET['stop']) || isset($_GET['stopped'])) && !isset($_GET['start'])){
		$ret.="<input type='hidden' name='stopped' value='1'>";
		$ret.="<input type='submit' name='start' value='".BUTTON_START."'><br/><br/><table style='text-align:left;'><tr><td>";
	}else{
				$ret.="<input type='submit' name='stop' value='".BUTTON_STOP."'><br/><br/><table style='text-align:left;'><tr><td>";
	}
	/*if (isset($_POST['start'])){
			$ret.="<input type='submit' name='stop' value='Stop'><br/><br/><table style='text-align:left;'><tr><td>";
	}*/
	if (isset($_GET['clear']) && is_file("/tmp/dialplan.txt")){
		$f=file("/tmp/dialplan.txt");
		$pid=$f[0];
		unlink("/tmp/dialplan.txt");
		$fp= fopen("/tmp/dialplan.txt","w+");
		$write=fputs($fp,$pid);
		fclose($fp);
	}elseif (isset($_GET['clear']) && is_file("/tmp/dialplan_d.txt")){
		$f=file("/tmp/dialplan_d.txt");
		$pid=$f[0];
		unlink("/tmp/dialplan_d.txt");
		$fp= fopen("/tmp/dialplan_d.txt","w+");
		$write=fputs($fp,$pid);
		fclose($fp);
	}
	$ret.="<div id='content'>".content()."</div>";
	//echo intval($pid);
	$ret.= "</td></tr></table><input type='hidden' name='pid' value='".trim($p)."'></form><hr size= \"1\" noshade>";
	$ret.= "<br><a href=\"http://www.beronet.com\" target=\"_blank\" onfocus=\"if (this.blur()) { this.blur(); }\"><img src=\"../includes/images/beroLogo.gif\" align=\"right\" border=\"0\"></a>";
	$ret.= "</center></td></tr>
	<tr>
	<td colspan=\"2\" style=\"background-repeat:no-repeat\" background=\"../includes/images/bg_bottom.png\"><img src=\"../includes/images/trans.gif\" border=\"0\" height=\"39\" width=\"10\"><div style=\"color: white;\" align=\"right\">&nbsp;&nbsp;&nbsp;</div>
	</td></tr></table>
	</center>
	</body>
	</html>";

	return $ret;
}

if (isset($_GET['pid']) && isset($_GET['stop'])){
	exec("/bin/kill ".$_GET['pid']);
	exec("/bin/mv /tmp/dialplan.txt /tmp/dialplan_d.txt");
	echo "<script>window.opener.location='../index.php?m=ddebug'</script>";
	echo show_file("");
}elseif (isset($_GET['pid']) && isset($_GET['reload'])){
	echo show_file();
}elseif (isset($_GET['pid']) && isset($_GET['reload_ajax'])){
	echo content();
}elseif (isset($_GET['pid']) && isset($_GET['clear'])){
		echo show_file();
}elseif (!is_file("/tmp/dialplan.txt")){
	$pid = pcntl_fork();

	if($pid == -1) {
  		die('could not fork');
	}else if ($pid) {
		//Parent
		$fp= fopen("/tmp/dialplan.txt","w+");
		$write=fputs($fp,$pid);
		fclose($fp);
		echo show_file();

	} else {
		//Child
  		$isgw = new isgwtelnet();	
  		$result=$isgw->isgw_login_dialplan();
  		$res=$isgw->isgw_verbose();
	}
	echo "<script>window.opener.location='../index.php?m=ddebug'; location.reload();</script>";
}else{
	echo show_file();
}

function content(){
	$ret="";
	if (is_file("/tmp/dialplan.txt")){
		$zeilen=file("/tmp/dialplan.txt");
		$zeilen=array_reverse($zeilen);
		$i=1;
        foreach ($zeilen as $zeile) {
			if ($i!=count($zeilen)){
				if (strpos($zeile,"S")===0) $ret.= "<font color='#FF9900'>".$zeile."</font><br/>";
				elseif (strpos($zeile,"I")===0) $ret.= "<font color='black'>".$zeile."</font><br/>";
				elseif (strpos($zeile,"A")===0) $ret.= "<font color='black'>".$zeile."</font><br/>";
				elseif (strpos($zeile,"D")===0) $ret.= "<font color='red'>".$zeile."</font><br/>";
				else $ret.= "<font color='#C0C0C0'>".$zeile."</font><br/>";
			}else{
				$pid=$zeile;
			}
			$i++;
		}
	}elseif (is_file("/tmp/dialplan_d.txt")){
		$zeilen=file("/tmp/dialplan_d.txt");
		$zeilen=array_reverse($zeilen);
		$i=1;
        foreach ($zeilen as $zeile) {			
			if ($i!=count($zeilen)){
				if (strpos($zeile,"S")===0) $ret.= "<font color='#FF9900'>".$zeile."</font><br/>";
				elseif (strpos($zeile,"I")===0) $ret.= "<font color='black'>".$zeile."</font><br/>";
				elseif (strpos($zeile,"A")===0) $ret.= "<font color='black'>".$zeile."</font><br/>";
				elseif (strpos($zeile,"D")===0) $ret.= "<font color='red'>".$zeile."</font><br/>";
				else $ret.= "<font color='#C0C0C0'>".$zeile."</font><br/>";
			}else{
				$pid=$zeile;
			}
			$i++;
		}
	}
	return $ret;
}	
	
?>
