<?php

/**
 * @file
 * Popup für das Anlegen von Sip Einträgen
 */

chdir("..");

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

$page=$_GET['start'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="icon" href="../includes/images/favicon.ico" type="image/x-icon">
<script type="text/javascript" language="javascript" src="../includes/validation_modul.js"></script>
<script>

function closeW(){
	this.window.close();
}

function move(Orig, Dest){
	var varFromBox = document.getElementById(Orig);
	var varToBox = document.getElementById(Dest);
	if ((varFromBox != null) && (varToBox != null))
	{
		if(varFromBox.length < 1)
		{
			alert('There are no items in the source ListBox');
			return false;
		}
		if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1

		{
			alert('Please select an Item to move');
			return false;
		}
		while ( varFromBox.options.selectedIndex >= 0 )
		{
			var newOption = new Option(); // Create a new instance of ListItem

			newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
			newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
			varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox

			varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox

		}
	}
	return false;
}
function selectall(){
	selectBox = document.getElementById('sel');
	for (var i = 0; i < selectBox.options.length; i++) {
		selectBox.options[i].selected = true;
	}
}
function up() {
	obj=document.getElementById('sel');
	for (var i = 0; i < obj.options.length; i++)
		if (obj.options[ i ].selected){
			if (i>0) change(obj,i,i-1);
		}

}

function down() {
	obj=document.getElementById('sel');
	for (var i = obj.options.length-1; i >= 0; i--){
		if (obj.options[ i ].selected){
			if ( i<obj.length-1) change(obj,i,i+1);
		}
	}
}
function change(obj,num1,num2) {
	proVal=obj.options[num1].value;
	proTex=obj.options[num1].text;
	obj.options[num1].value=obj.options[num2].value;
	obj.options[num1].text=obj.options[num2].text;
	obj.options[num2].value=proVal;
	obj.options[num2].text=proTex;
	obj.options[num2].selected=true;
	obj.options[num1].selected=false;
}
function hidec(){
	var config_sip=document.getElementById('config_sip');
	config_sip.style.display="none";
}

function show_config(){
	var config_sip=document.getElementById('config_sip');
	if (config_sip.style.display=="none"){
		config_sip.style.display="";
	}else{
		config_sip.style.display="none";
	}
}
function hide_reg(value){
	if (value==false){
		document.getElementById('reg').style.display="none";
		document.getElementById('reg_option').style.display="none";
		document.getElementById('hard_reregister').style.display="none";
	}else{
		document.getElementById('reg').style.display="";
		document.getElementById('reg_option').style.display="";
		document.getElementById('hard_reregister').style.display="";
	}
}
function resize(){
	if(window.sizeToContent) window.sizeToContent();
}

function change_config(reg){
	if (reg.checked){
		document.getElementById("sip_from_user_setting").options[4].selected=true; document.getElementById("sip_from_user_setting_manual").style.display="none";
		document.getElementById("sip_from_display_setting").options[0].selected=true; document.getElementById("sip_from_display_setting_manual").style.display="none";
		document.getElementById("from_sip_src_setting").options[1].selected=true;
	}else{
		document.getElementById("sip_from_user_setting").options[0].selected=true; document.getElementById("sip_from_user_setting_manual").style.display="none";
		document.getElementById("sip_from_display_setting").options[0].selected=true; document.getElementById("sip_from_display_setting_manual").style.display="none";
		document.getElementById("from_sip_src_setting").options[0].selected=true;
	}
}

</script>
<TITLE>SIP</TITLE>
<link rel="stylesheet" href="../includes/template_css.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body onload="hide_reg(document.getElementById('register').checked); hidec(); resize();" >
<script type="text/javascript" src="../includes/notes/wz_tooltip.js"></script>
<center>
<?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=sip'</script>";
	echo "<script>this.window.close();</script>";
}else{
	$_SESSION['beroari_time']=time();
}

require_once "./includes/isgwtelnet.php";

function showdefmc($config_values){
	$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("/\[(?<bez>.+)\]\[(?<name>.+)\][\s]*(?<desc>.+)[\s]*\<(?<val>.*)\>[\s]*'(?<def>.*)'/",$zeile,$res);
				if (!in_array($res['name'],$config_values) && $res['bez']=="s")
					echo "<script>
							var extra_tooltip=document.getElementById('extra_tooltip');
							extra_tooltip.innerHTML=extra_tooltip.innerHTML+'<p>".DIALPLAN_OPTION_DIS."<b>&nbsp;".$res['name']."</b><br>".DIALPLAN_VALUES_DIS."<b>&nbsp;&lt;".$res['val']."&gt;</b><br><i>".$res['desc']."</i></p>';
						</script>";
			}
		}
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		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]!="" && !in_array($key[0],$config_values)) {
					echo "<script>var addit_options=document.getElementById('addit_options');
							if (addit_options.innerHTML=='')
								addit_options.innerHTML='".$key[0]."=".$key[1]."';
							else
								addit_options.innerHTML=addit_options.innerHTML+'\\n".$key[0]."=".$key[1]."';</script>";
				}elseif ($key[0]!="" && $key[1]=="" && !in_array($key[0],$config_values)){
					echo "<script>var addit_options=document.getElementById('addit_options');
							if (addit_options.innerHTML=='')
								addit_options.innerHTML='".$key[0]."=';
							else
								addit_options.innerHTML=addit_options.innerHTML+'\\n".$key[0]."=';</script>";
				}
			}
		}
	}
}

function show_confadd($config_values){
	$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("/\[(?<bez>.+)\]\[(?<name>.+)\][\s]*(?<desc>.+)[\s]*\<(?<val>.*)\>[\s]*'(?<def>.*)'/",$zeile,$res);
				if (!in_array($res['name'],$config_values) && $res['bez']=="s")
					echo "<script>
							var extra_tooltip=document.getElementById('extra_tooltip');
							extra_tooltip.innerHTML=extra_tooltip.innerHTML+'<p>".DIALPLAN_OPTION_DIS."<b>&nbsp;".$res['name']."</b><br>".DIALPLAN_VALUES_DIS."<b>&nbsp;&lt;".$res['val']."&gt;</b><br><i>".$res['desc']."</i></p>';
							var addit_options=document.getElementById('addit_options');
							if (addit_options.innerHTML=='')
								addit_options.innerHTML='".$res['name']."=".$res['def']."';
							else
								addit_options.innerHTML=addit_options.innerHTML+'\\n".$res['name']."=".$res['def']."';
						</script>";
			}
		}
	}
}

function type($ba,$id){
	$type=getValue($ba,$id,"type");
	if ($type=="1")
		$checkbox="<input type='hidden' name='register' value='0'><input type='checkbox' name='register' id='register' value='1' checked onchange='hide_reg(this.checked); change_config(this)'>";
	else
		$checkbox="<input type='hidden' name='register' value='0'><input type='checkbox' name='register' id='register' value='1' onchange='hide_reg(this.checked); change_config(this)'>";
	return $checkbox;
}

function hard_reregister($ba,$id){
	$hard_reregister=getValue($ba,$id,"hard_reregister");
	if ($hard_reregister=="1")
		$checkbox="<input type='hidden' name='hard_reregister' value='0'><input type='checkbox' name='hard_reregister' value='1' checked>";
	else
		$checkbox="<input type='hidden' name='hard_reregister' value='0'><input type='checkbox' name='hard_reregister' value='1'>";
	return $checkbox;
}

function register_option($ba,$id){
	$option=getValue($ba,$id,"register_option");
	if ($option=="validate" || $option=="")
		return "<option value='validate' selected>validate</option>
			<option value='no-validate no-options-keepalive'>no-validate</option>";
	elseif ($option=="no-validate no-options-keepalive")
		return "<option value='validate'>validate</option>
			<option value='no-validate no-options-keepalive' selected>no-validate</option>";
	else return "<option value='validate' selected>validate</option>
			<option value='no-validate no-options-keepalive'>no-validate</option>";
}

function getValue($ba,$id,$option){
	$query=$ba->select("select $option from sip where id=\"$id\"");
	return stripslashes(sqlite_fetch_single($query));
}


$config_values=array("t38","dtmfmode","codecs","ie_on_sip","from_sip_src_setting",
					"wait_for_cancel","sip_from_user_setting",
					"sip_from_display_setting","failover_proxy","failover_timeout","s2i_cpt");


//echo "<script>window.opener.location='../index.php?m=sip'</script>";
print "<br><table align=\"center\" bgcolor=\"#f0f0ec\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"450\">
<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\"></td>
<td><p align='left'><img src=\"../includes/images/berofixlogo.png\"/></p><hr size= \"1\" noshade><p><center><font size='3'>SIP</font></p><br><br>";

	echo ("<form class='extensions_form' name='sip' action='sip_submit.php' method='POST' onsubmit=\"return (check_blank(name.value,'name') && specChar(name.value,'Name') && specChar(user.value,'User') && specChar(server_address.value,'Server Address') && is_not_empty(server_address.value, 'Server Address') && is_not_empty(name.value,'Name') && check_reg_intervall(regrefresh.value,document.getElementById('register').checked));\">
			<table border='1' style='border-collapse:collapse'>
			<tr>
			<td colspan='2'>
			<input type=hidden name=start value=" . $page . ">
			<table border=0 align='center'>
			<tr onmouseover=\"Tip('".TRUNKS_SIP_NAME."')\" onmouseout='UnTip()'>
			<td><b>".SIP_NAME_DIS.":</b>&nbsp;</td>
			<td><input name='name' type='text' value='".getValue($ba,$_GET['id'],"name")."'></td>
			</tr>
			<tr onmouseover=\"Tip('".TRUNKS_SIP_SERVER_ADDRESS."')\" onmouseout='UnTip()'>
			<td><b>".SIP_SERVER_ADDRESS_DIS.":</b>&nbsp;</td>
			<td><input name='server_address' type='text' value='".getValue($ba,$_GET['id'],"server_address")."'></td>
			</tr>
			<tr onmouseover=\"Tip('".TRUNKS_SIP_USER."')\" onmouseout='UnTip()'>
			<td><b>".SIP_USER_DIS.":</b>&nbsp;</td>
			<td><input name='user' type='text' value='".getValue($ba,$_GET['id'],"user")."'></td>
			</tr>
			<tr onmouseover=\"Tip('".TRUNKS_SIP_SECRET."')\" onmouseout='UnTip()'>
			<td><b>".SIP_SECRET_DIS.":</b>&nbsp;</td>
			<td><input name='secret' type='text' value='".getValue($ba,$_GET['id'],"secret")."'></td>
			</tr>
			<tr onmouseover=\"Tip('".TRUNKS_SIP_REGISTER."')\" onmouseout='UnTip()'>
			<td><b>".SIP_REGISTER_DIS.":</b>&nbsp;</td>
			<td>".type($ba,$_GET['id'])."</td>
			</tr>
			<tr id='reg' onmouseover=\"Tip('".TRUNKS_SIP_REGREFRESH."')\" onmouseout='UnTip()'>
			<td><b>".SIP_REGISTRATION_INTERVALL_DIS.":</b></td>
			<td><input name='regrefresh' type='text' value='".getValue($ba,$_GET['id'],"regrefresh")."'></td>
			</tr>
			<tr id='reg_option' onmouseover=\"Tip('".TRUNKS_SIP_REGISTER_OPTION."')\" onmouseout='UnTip()'>
			<td><b>".SIP_REGISTER_OPTION_DIS.":</b></td>
			<td>
			<select name='register_option' onmouseover=\"Tip('".TRUNKS_SIP_REGISTER_OPTION."')\" onmouseout='UnTip()'>".register_option($ba,$_GET['id'])."</select></td>
			</tr>
			<tr id='hard_reregister'>
			<td><b>".SIP_HARD_REREGISTER_DIS.":</b></td>
			<td>".hard_reregister($ba,$_GET['id'])."</td>
			</tr>
			<tr>
			<td align='center' colspan='2'><a href='#' onclick='show_config();'>".SIP_MORE_DIS."...</a></td>
			</tr>
			</table>
			</td>
			</tr>
			</table>
			<br/>
		<table border='1' cellspacing='0' id='config_sip' width='400' style='border-collapse:collapse;'>
			<tr>
				<td><table border='0'>
					<tr><td colspan='2' align='center'><b>".DIALPLAN_CONFIGURATION_DIS."</b></td></tr>
					<tr onmouseover=\"Tip('".T38."')\" onmouseout='UnTip()' id='t38_tr'>
						<td><b>".DIALPLAN_T38_SUPPORT_DIS."</b></td>
		    			<td>".t38()."</td>
					</tr>
					<tr onmouseover=\"Tip('".DTMFMODE."')\" onmouseout='UnTip()' id='dtmf_tr'>
						<td><b>".DIALPLAN_DTMF_MODE_DIS."</b></td>
		    			<td>".dtmfmode()."</td>
					</tr>
					<tr onmouseover=\"Tip('".IE_ON_SIP."')\" onmouseout='UnTip()' id='ie_on_sip_tr'>
						<td><b>".DIALPLAN_IE_ON_SIP_DIS."</b></td>
		    			<td>".ie_on_sip()."</td>
					</tr>
					<tr id='codecs_tr'>
						<td><b>".DIALPLAN_CODECS_DIS."</b></td>
		    			<td>".codecs()."</td>
   		    		</tr>
					<tr onmouseover=\"Tip('".WAIT_FOR_CANCEL."')\" onmouseout='UnTip()' id='wait_for_cancel_tr'>
						<td><b>".DIALPLAN_WAIT_FOR_CANCEL_DIS."</b></td>
		    			<td>".wait_for_cancel()."</td>
					</tr>
					<tr onmouseover=\"Tip('".CALL_PROGRESS_TABLE."')\" onmouseout='UnTip()' id='s2i_cpt_tr'>
						<td><b>".DIALPLAN_CALL_PROGRESS_TABLE_S2I_DIS."</b></td>
		    			<td>".s2i_cpt()."</td>
					</tr>
					<tr onmouseover=\"Tip('".CALL_PROGRESS_TABLE."')\" onmouseout='UnTip()' id='s2i_cpt_tr'>
						<td><b>".DIALPLAN_CALL_PROGRESS_TABLE_I2S_DIS."</b></td>
		    			<td>".i2s_cpt()."</td>
					</tr>
					<tr onmouseover=\"Tip('".FAILOVER_ACCOUNT."')\" onmouseout='UnTip()' id='failover_proxy_tr'>
						<td><b>".DIALPLAN_FAILOVER_ACCOUNT_DIS."</b></td>
						<td>".failover_proxy()."</td>
					</tr>
					<tr onmouseover=\"Tip('".FAILOVER_TIMEOUT."')\" onmouseout='UnTip()' id='failover_timeout_tr'>
						<td><b>".DIALPLAN_FAILOVER_TIMEOUT_DIS."</b></td>
						<td>".failover_timeout()."</td>
					</tr>
					<tr onmouseover=\"Tip('".FROM_SIP_SRC_SETTING."')\" onmouseout='UnTip()' id='sip_src_setting_tr'>
						<td><b>".DIALPLAN_FROM_SIP_SRC_SETTING_DIS."</b></td>
						<td>".from_sip_src_setting()."</td>
					</tr>
					<tr>
						<td colspan='2'>&nbsp;</td>
					</tr>
					<tr>
						<th colspan='2'>".DIALPLAN_CALLER_ID_MAPPING_DIS."</th>
					</tr>
					<tr onmouseover=\"Tip('".SIP_FROM_USER_SETTING."')\" onmouseout='UnTip()' id='sip_from_user_setting_tr'>
						<td><b>".DIALPLAN_SIP_FROM_USER_SETTING_DIS."</b></td>
						<td>".caller_id_mapping_sip('sip_from_user_setting')."</td>
					</tr>
					</tr>
					<tr onmouseover=\"Tip('".SIP_FROM_DISPLAY_SETTING."')\" onmouseout='UnTip()' id='sip_from_display_setting_tr'>
						<td><b>".DIALPLAN_SIP_FROM_DISPLAY_SETTING_DIS."</b></td>
						<td>".caller_id_mapping_sip('sip_from_display_setting')."</td>
					</tr>
					</tr>

					<tr>
						<td colspan='2'>&nbsp;</td>
					</tr>
					<tr onmouseover=\"Tip('".ADDIT_OPTIONS."')\" onmouseout='UnTip()'>
						<td colspan='2'><b>".DIALPLAN_ADDITIONAL_CONFIG_DIS."</b></td>
					</tr>
					<tr onmouseover=\"Tip('".ADDIT_OPTIONS."')\" onmouseout='UnTip()'>
						<td colspan='2'><textarea name='addit_options' id='addit_options' rows='4' style='width:305px;'></textarea></td>
					</tr>
					<tr>
						<td colspan='2'>
						<br>
						<b>".DIALPLAN_ADDITIONAL_CONFIG_DOC_DIS."</b>
						</td>
					</tr>
					<tr>
						<td colspan='2'>
						<p id='extra_tooltip' style='height: 150px; width:305px; overflow: auto; border: 1px solid #A31D26; background: none repeat scroll 0% 0% rgb(238, 238, 238); color: rgb(0, 0, 0);'>
						</td>
					</tr>
					</table></td></tr></table><br>");
			if (isset($_GET['id'])){
				showdefmc($config_values);
			}else{
				show_confadd($config_values);
			}
			if (isset($_GET['id']))	echo ("<input type='hidden' name='alt_name' value='".getValue($ba,$_GET['id'],"name")."'><input type='hidden' name='id_upd' value='". $_GET['id']."'>");
			echo ("<input name='submit' type='submit' value='".BUTTON_SAVE."' onclick='selectall();' onmouseover=\"Tip('".TRUNKS_SIP_SAVE."')\" onmouseout='UnTip()'>
			 &nbsp&nbsp<input name='close' type='button' value='".BUTTON_CLOSE."' onclick='javascript:closeW();' onmouseover=\"Tip('".POPUPS_BUTTON_CLOSE."')\" onmouseout='UnTip()'>
			</form>");



print "<hr size= \"1\" noshade>";
print "<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>";
print "</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>

