<?php
chdir("..");
chdir("..");

include "./includes/variables.php";
include "./misc/database.php";
require_once "./includes/isgwtelnet.php";

function title0($ba,$gr){
	$query=$ba->select("select distinct type from isdn where lif='0'");
	if (sqlite_num_rows($query)==0){
	$title="<b>Li0()</b>";
	}else{
	while ($entry=$ba->fetch_array($query)){
	$title="<b>Li0(".$entry['type'].")</b>";
	}	
	}
	return $title;
}
function ports0($ba,$gr){
	$tab="<table border='0'>";
	$query=$ba->select("select * from isdn where lif='0'");
	while ($entry=$ba->fetch_array($query)){
		$type=split("-",$entry['type']);
		if ($entry['gr']==$gr){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked value='".$entry['port']."'></td></tr>";
		}elseif ($entry['gr']=='NULL'){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' value='".$entry['port']."'></td></tr>";
		}else{
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked disabled='true' value='".$entry['port']."'></td></tr>";
		}
	}
	$tab .= "</table>";
	return $tab;
}

function title1($ba,$gr){
	$query=$ba->select("select distinct type from isdn where lif='1'");
	if (sqlite_num_rows($query)==0){
	$title="<b>Li1()</b>";
	}else{
	while ($entry=$ba->fetch_array($query)){
	$title="<b>Li1(".$entry['type'].")</b>";
	}	
	}
	return $title;
}

function ports1($ba,$gr){
	$tab="<table border='0'>";
	$query=$ba->select("select * from isdn where lif='1'");
	while ($entry=$ba->fetch_array($query)){
		$type=split("-",$entry['type']);
		if ($entry['gr']==$gr){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked value='".$entry['port']."'></td></tr>";
		}elseif ($entry['gr']=='NULL'){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' value='".$entry['port']."'></td></tr>";
		}else{
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked disabled='true' value='".$entry['port']."'></td></tr>";
		}
	}
	$tab .= "</table>";
	return $tab;
}

function showdefmc(){
	$isgw = new isgwtelnet();
	$result=$isgw->isgw_login();
	if ($result!=false){
	$dialplanc=$isgw->isgw_dialplanconfig();
    $zeilen=split("\n",$dialplanc);
	foreach ($zeilen as $zeile){
		if (stripos($zeile,'[')===0){
			preg_match("/\[(?<name>.+)\] (?<desc>.+) (?<def>.+)/",$zeile,$res);
			echo "<tr><td>".$res['desc']." <label id='l".$res['name']."'>(def ".$res['def'].")</label></td><td><input  id='".$res['name']."' size='7' name='config_".$res['name']."' ></td></tr>";
		}
	}
	$ba=new beroAri();
	$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
	while  ($entry = $ba->fetch_array($query)) {
		$config=$entry['config'];
	}
	if ($config!=""){
		$configs=split(',',$config);
		foreach ($configs as $value){
			$key=split('=',$value);
			if ($key[0]!="" && $key[1]!="") {
				echo "<script>var ".$key[0]."=document.getElementById('".$key[0]."'); ".$key[0].".value='".$key[1]."';</script>";
			}elseif ($key[0]!="" && $key[1]==""){
				echo "<script>var ".$key[0]."=document.getElementById('".$key[0]."'); ".$key[0].".value='\'\'';</script>";
			}
		}
	}
	}
}

function show_confadd(){
	$isgw = new isgwtelnet();
	$result=$isgw->isgw_login();
		if ($result!=false){

	$dialplanc=$isgw->isgw_dialplanconfig();
	$zeilen=split("\n",$dialplanc);
	foreach ($zeilen as $zeile){
		if (stripos($zeile,'[')===0){
			preg_match("/\[(?<name>.+)\] (?<desc>.+) (?<def>.+)/",$zeile,$res);
			echo "<tr><td>".$res['desc']." <label id='l".$res['name']."'>(def ".$res['def'].")</label></td><td><input  id='".$res['name']."' size='7' name='config_".$res['name']."' ></td></tr>";
		}
	}
}
}


$ba= new beroAri();
if (show=="yes"){

if (isset($_GET['gr'])){
	echo ("<form class='extensions_form' name='isdn' action='isdn_submit.php?ip=".$_GET['ip']."' method='POST' onsubmit=\"return check(group.value);\">
			<table border='1' style='border-collapse:collapse' width='290'>
			<tr>
			<td colspan='2'>
			<table border=0 align='center'>
			<tr>
			<td><b>Group Name:</b>&nbsp;</td>
			<td>&nbsp;<input type='text' name='group' value='".$_GET['gr']."' onmouseover=\"Tip('".ISDN_GROUP_NAME."')\" onmouseout='UnTip()'></td>
			</tr>
			<tr>
			<td valign='top'><b>Ports:</b>&nbsp</td>
			<td>
			<table>
			<tr>
			<td valign='top' colspan='2'>
			<table border='1' style='border-collapse:collapse;' width='100%'>
			<tr>
			<td width='65'>".title0($ba,$_GET['gr'])."</td>
			<td width='65'>".title1($ba,$_GET['gr'])."</td>
			</tr>
			<tr>
			<td valign='top'>".ports0($ba,$_GET['gr'])."</td>
			<td valign='top'>".ports1($ba,$_GET['gr'])."</td>
			</tr>
			</table>
			</td>
			</tr>
			</table>
			</td>
			</tr>
			<tr>
			<td align='center' colspan='2'><a href='#' onclick='show_config();'>more...</a></td>
			</tr>
			
			</table>
			</td>
			</tr>
			</table>
			<br/>
			<table border='1' id='configt' width='290' style='border-collapse:collapse'>
			<tr><td>
			<table class='contentoc'><tr><td colspan='2' align='center'><b>Configuration</b></td></tr>");
			showdefmc();				
			echo ("</table></td></tr></table><br/>
			<input type='hidden' name='gr_upd' value='". $_GET['gr']."'>
			<input name='submit' type='submit' value='Save' onmouseover=\"Tip('".ISDN_SAVE."')\" onmouseout='UnTip()'>
			 &nbsp&nbsp<input name='close' type='button' value='Close' onclick='javascript:closeW();' onmouseover=\"Tip('".POPUPS_BUTTON_CLOSE."')\" onmouseout='UnTip()'>
			</form>");


}else{
	echo ("<form class='extensions_form' name='isdn' action='isdn_submit.php?ip=".$_GET['ip']."' method='POST' onsubmit=\"return check(group.value);\">
			<table border='1' style='border-collapse:collapse'  width='290'>
			<tr>
			<td colspan='2'>
			<table border=0 align='center'>
			<tr>
			<td><b>Group Name:</b>&nbsp;</td>
			<td><input type='text' name='group' onmouseover=\"Tip('".ISDN_GROUP_NAME."')\" onmouseout='UnTip()'></td>
			</tr>
			<tr>
			<td valign='top'><b>Ports:</b>&nbsp</td>
			<td>
			<table>
			<tr>
			<td valign='top'>
			<table border='1' style='border-collapse:collapse;' width='100%'>
			<tr>
			<td width='65'>".title0($ba,$_GET['gr'])."</td>
			<td width='65'>".title1($ba,$_GET['gr'])."</td>
			</tr>
			<tr>
			<td valign='top'>".ports0($ba,$_GET['gr'])."</td>
			<td valign='top'>".ports1($ba,$_GET['gr'])."</td>
			</tr>
			</table>
			</td>
			</tr>
			</table>
			</td>
			</tr>
			<tr>
			<td align='center' colspan='2'><a href='#' onclick='show_config();'>more...</a></td>
			</tr>
			</table>
			</td>
			</tr>
			</table>
			<br/>
			<table border='1' id='configt' width='290' style='border-collapse:collapse' >
			<tr><td>
			<table class='contentoc'><tr><td colspan='2' align='center'><b>Configuration</b></td></tr>");
			show_confadd();				
			echo ("</table></td></tr></table><br/>
	
			<input name='submit' type='submit' value='Add' onmouseover=\"Tip('".TRUNKS_ISDN_ADD."')\" onmouseout='UnTip()'>
			 &nbsp&nbsp<input name='close' type='button' value='Close' onclick='javascript:closeW();' onmouseover=\"Tip('".POPUPS_BUTTON_CLOSE."')\" onmouseout='UnTip()'>
			</form>");
}
}else{
	echo "<h2>Access Denied</h2>";
}
?>
