<?php
chdir("..");
include "./includes/variables.php";
include "./misc/database.php";
include "./includes/amifunc.php";
include '/usr/local/php/beroConf.php';
$bc = new beroConf('root');
$ba = new beroAri();
$ami=new amifunc();
function group_reg($ba){
	$query=$ba->select("select * from general");
	while ($entry = $ba->fetch_array($query)) {
		$ret=$entry['rules'];
	}
	return $ret;
}
function logserver($ba){
	$query=$ba->select("select * from general");
	while ($entry = $ba->fetch_array($query)) {
		$ret=$entry['logserver'];
	}
	return $ret;
}
function logging_active($ba){
	$query=$ba->select("select * from general");
	while ($entry = $ba->fetch_array($query)) {
		$ret=$entry['logging_active'];
	}
	return $ret;
}
//isgw.conf
$file_isgw="/usr/conf/isgw.conf";
$isgw_cont="[general]\n";
$query=$ba->select("select * from general");
while ($entry = $ba->fetch_array($query)) {
	$isgw_cont.="statusport=54322\n";
	$isgw_cont.="logserver=".$entry['logserver']."\n";
	$isgw_cont.="logging_active=".$entry['logging_active']."\n";
	$isgw_cont.="logging_message=".$entry['logging_active']."\n";
	$isgw_cont.="loglevel_message=".$entry['loglevel']."\n";
	if ($ba->get("syslog_facility")!="") $isgw_cont.="syslog_facility=".$ba->get("syslog_facility")."\n";
	else $isgw_cont.="syslog_facility=16\n";
	//$isgw_cont.="udpports=".$entry['udpports']."\n";
}
	if ($ba->get("reject_calls_under_load")!="") $isgw_cont.="reject_calls_under_load=".$ba->get("reject_calls_under_load")."\n";
	else $isgw_cont.="reject_calls_under_load=0\n";
	if ($ba->get("pcm_server")!="") $isgw_cont.="pcm_server=".$ba->get("pcm_server")."\n";
	else $isgw_cont.="pcm_server=0\n";
	if ($ba->get("pcm_server_port")!="") $isgw_cont.="pcm_server_port=".$ba->get("pcm_server_port")."\n";
	else $isgw_cont.="pcm_server_port=54329\n";

	if ($ba->get("logcdr")!="") $isgw_cont.="logcdr=".$ba->get("logcdr")."\n";
	else $isgw_cont.="logcdr=0\n";

	if ($ba->get("rtp_traffic")!="") $isgw_cont.="rtp_traffic=".$ba->get("rtp_traffic")."\n";
	else $isgw_cont.="rtp_traffic=0\n";

	$isgw_cont.="msp_bridging=YES\n";

	$vlanenable=$bc->get("root", "vlan-enable");
	if($vlanenable=="1"){
		$vlanid=$bc->get("root", "vlan-id");
		$isgw_cont.="interface_host=eth0.".$vlanid."\n";
	}


if (!file_exists($file_isgw)){
	$fp= fopen($file_isgw,"w+");
	$write=fputs($fp,$isgw_cont);
	fclose($fp);
}else if (file_exists($file_isgw)){
	$fp= fopen($file_isgw,"w+");
	$write=fputs($fp,$isgw_cont);
	fclose($fp);
}

//isgw.sip
$file_sip="/usr/conf/isgw.sip";
$sip_cont="[general]\n";
$query=$ba->select("select * from sip_general");
while ($entry = $ba->fetch_array($query)) {
	$sip_cont.="sip_address=sip:0.0.0.0:".$entry['bindport']."\n";
	if ($entry['udpports']!="")
	$sip_cont.="udpports=".$entry['udpports']."\n";
		$sip_cont.="externip=".$entry['externip']."\n";
}
$sip_cont.="sip_transport=".$ba->get("sip_transport")."\n";
$sip_cont.="ssl_version=".$ba->get("ssl_version")."\n";
$sip_cont.=($ba->get("tos_rtp")=="") ? "tos_rtp=160\n" : "tos_rtp=".$ba->get("tos_rtp")."\n";
$sip_cont.=($ba->get("tos_sip")=="") ? "tos_sip=160\n" : "tos_sip=".$ba->get("tos_sip")."\n";
$sip_cont.="preresolve_addresses=".$ba->get("preresolve_addresses")."\n";
$sip_cont.="rtp_port_selection=".$ba->get("rtp_port_selection")."\n";


$query_sip=$ba->select("select * from sip");

while ($entry = $ba->fetch_array($query_sip)) {
	$sip_cont.="[".$entry['name']."]\n";
	$sip_cont.="address = ".$entry['server_address']."\n";
	$sip_cont.="register = ".$entry['type']."\n";
	$sip_cont.="user = ".$entry['user']."\n";
	$sip_cont.="secret = ".$entry['secret']."\n";
        if ($entry['type']=='1') $sip_cont.="regrefresh = ".$entry['regrefresh']."\n";
        //register_option interface is register_options
	if ($entry['type']=='1') $sip_cont.="register_options = ".$entry['register_option']."\n";
	if ($entry['type']=='1') $sip_cont.="hard_reregister = ".$entry['hard_reregister']."\n";
	$sip_cont.="externip = ".$entry['externip']."\n";
	//$sip_cont.="stunserver = ".$entry['stunserver']."\n";
	$sip_cont.="config = ".$entry['config']."\n";
}
if (!file_exists($file_sip)){
	$fp= fopen($file_sip,"w+");
	$write=fputs($fp,$sip_cont);
	fclose($fp);
}else if (file_exists($file_sip)){
	$fp= fopen($file_sip,"w+");
	$write=fputs($fp,$sip_cont);
	fclose($fp);
}

//isgw.isdn
$file_isdn="/usr/conf/isgw.isdn";
$isdn_cont.="[general]\n";
$isdn_cont.="time_from_isdn=".$bc->get("root", "time_from_isdn")."\n\n" ;
$query = $ba->select('SELECT DISTINCT gr FROM isdn WHERE gr!="NULL"');
while ($entry = $ba->fetch_array($query)) {
	$queryc = $ba->select('SELECT config FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$config="";
	while ($entryc = $ba->fetch_array($queryc)) {
		$config.=$entryc['config'];
	}
	$query_ch = $ba->select('SELECT chansel FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$chansel="";
	while ($entrych = $ba->fetch_array($query_ch)) {
		$chansel.=$entrych['chansel'];
	}
	$query_chd = $ba->select('SELECT chansel_dir FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$chansel_dir="";
	while ($entrychd = $ba->fetch_array($query_chd)) {
		$chansel_dir.=$entrychd['chansel_dir'];
	}
	$query_odt = $ba->select('SELECT odt FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$odt="";
	while ($entryodt = $ba->fetch_array($query_odt)) {
		$odt.=$entryodt['odt'];
	}
	$query_odt_e = $ba->select('SELECT odt_empty_dad FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$odt_empty_dad="";
	while ($entryodte = $ba->fetch_array($query_odt_e)) {
		$odt_empty_dad.=$entryodte['odt_empty_dad'];
	}
	$query_portpullup = $ba->select('SELECT portpullup FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$portpullup="";
	while ($entry_portpullup = $ba->fetch_array($query_portpullup)) {
		$portpullup.=$entry_portpullup['portpullup'];
	}
	$query_to = $ba->select('SELECT tones FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$tones="";
	while ($entryto = $ba->fetch_array($query_to)) {
		$tones.=$entryto['tones'];
	}
	$query_ovd = $ba->select('SELECT overlap_dialing FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$overlap_dialing="";
	while ($entryovd = $ba->fetch_array($query_ovd)) {
		$overlap_dialing.=$entryovd['overlap_dialing'];
	}
	$query_qsig = $ba->select('SELECT qsig FROM isdn WHERE gr="'.$entry['gr'].'" group by gr');
	$qsig="";
	while ($entryqsig = $ba->fetch_array($query_qsig)) {
		$qsig.=$entryqsig['qsig'];
	}
	$query_port = $ba->select('SELECT port FROM isdn WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
	$ports="";
	while ($entryp = $ba->fetch_array($query_port)) {
		if($ports!=""){
			$ports.=",".$entryp['port'];
		}else{
			$ports.=$entryp['port'];
		}
	}
	$isdn_cont.="[".$entry['gr']."]\n";
	$isdn_cont.="ports = ".$ports."\n";
	$isdn_cont.="config = ".$config."\n";
	$isdn_cont.="chansel = ".$chansel."\n";
	$isdn_cont.="chansel_direction = ".$chansel_dir."\n";
	$isdn_cont.="tones = ".$tones."\n";
	$isdn_cont.="interdigit_timeout = ".$odt."\n";
	$isdn_cont.="interdigit_timeout_initial = ".$odt_empty_dad."\n";
	$isdn_cont.="overlap_dialing = ".$overlap_dialing."\n";
	$isdn_cont.="qsig = ".$qsig."\n";
	$isdn_cont.="portpullup = ".$portpullup."\n";

}

if (!file_exists($file_isdn)){
	$fp= fopen($file_isdn,"w+");
	$write=fputs($fp,$isdn_cont);
	fclose($fp);
}else if (file_exists($file_isdn)){
	$fp= fopen($file_isdn,"w+");
	$write=fputs($fp,$isdn_cont);
	fclose($fp);
}

//hardware.conf
$file_isdnhw="/usr/conf/hardware.conf";
$query = $ba->select('SELECT type  FROM isdn WHERE lif="1" union SELECT type  FROM analog WHERE lif="1" union SELECT type  FROM gsm WHERE lif="1"');
$tmp=sqlite_fetch_single($query);
if ($tmp==""){
	$lif1="none";
}else{
	$lif1=$tmp;
}

$isdn_hw_cont="[lif0]\n";
if (sqlite_fetch_single($ba->select('SELECT type FROM isdn WHERE lif="0" union SELECT type FROM analog WHERE lif="0" union SELECT type FROM gsm WHERE lif="0"'))!=""){
	$type=sqlite_fetch_single($ba->select('SELECT type FROM isdn WHERE lif="0" union SELECT type FROM analog WHERE lif="0" union SELECT type FROM gsm WHERE lif="0"'));
	$isdn_hw_cont.="type=".$type."\n";

	$ntteSW="";
	$ntteHW="";
	$termination="";
	$crc="";
	$ptp="";
	$master="";
	$pin="";
	$smsc="";
	$clock="0";
	$i=0;
	$query = $ba->select('SELECT master, port, port_type FROM isdn WHERE lif="0" union SELECT master, port, port_type FROM analog WHERE lif="0" union SELECT master, port, port_type FROM gsm WHERE lif="0" order by port');
	while ($entry = $ba->fetch_array($query)) {
		$master=$entry['master'];
		if ($entry['port_type']=="FXS" || $entry['port_type']=="FXO" || $entry['port_type']=="GSM") continue;
		else{
			$q_isdn=$ba->select('SELECT * FROM isdn WHERE port="'.$entry['port'].'"');
			while ($entry_isdn = $ba->fetch_array($q_isdn)) {
				if ($ntteSW=="") $ntteSW=$entry_isdn['ntteSW'];
				else $ntteSW.=",".$entry_isdn['ntteSW'];

				if ($ntteHW=="") $ntteHW=$entry_isdn['ntteHW'];
				else $ntteHW.=",".$entry_isdn['ntteHW'];


				if ($termination=="") $termination=$entry_isdn['lt'];
				else $termination.=",".$entry_isdn['lt'];

				if ($crc=="") $crc=$entry_isdn['crc'];
				else $crc.=",".$entry_isdn['crc'];


				if ($ptp=="") $ptp=$entry_isdn['ptp'];
				else $ptp.=",".$entry_isdn['ptp'];
				if ($entry_isdn['clock']=="1") $clock=$entry_isdn['port'];
			}
		}

	}
	$isdn_hw_cont.="master=".$master."\n";
	if ($type!="bf4FXS" && $type!="bf4FXO" && $type!="bf2GSM"){
		if ($clock!="") $isdn_hw_cont.="clock=".$clock."\n";
		$isdn_hw_cont.="ntteSW=".$ntteSW."\n";
		$isdn_hw_cont.="ntteHW=".$ntteHW."\n";
		if ($type!="bf2E1" && $type!="bf1E1") $isdn_hw_cont.="termination=".$termination."\n";
		if ($type=="bf2E1" || $type=="bf1E1") $isdn_hw_cont.="crc=".$crc."\n";
		$revision=exec('sed -ne "s/.*revision=\(.*\)/\1/p" /proc/cmdline');
		if ($lif1=="bf2GSM") $isdn_hw_cont.="portswitch=1\n";
		if ($lif1=="none" && ($type=="bf4S0" || $type=="bf2S02FXS")  && $revision!="1.80" && $revision!="1.8" ) {
			$q=$ba->select('SELECT value FROM key_val WHERE key="portswitch"');
			$v=sqlite_fetch_single($q);
			$isdn_hw_cont.="portswitch=".$v."\n";
		}elseif (($type=="bf4S0" || $type=="bf2S02FXS") && $lif1!="bf2GSM"){
			$isdn_hw_cont.="portswitch=0\n";
		}
		if ($type=="bf2S02FXS"){
			$isdn_hw_cont.="country=".$ba->get('country')."\n";
			$isdn_hw_cont.=($ba->get('boostringer')=="") ? "boostringer=0\n" : "boostringer=".$ba->get('boostringer')."\n";
		}
		$isdn_hw_cont.="ptp=".$ptp."\n\n";
	}elseif ($type=="bf2GSM"){
		$query = $ba->select('SELECT pin,smsc FROM gsm WHERE lif="0" order by port');
		$j=0;
		while ($entry = $ba->fetch_array($query)) {
			if ($j==0) $pin=$entry['pin'];
			else $pin.=",".$entry['pin'];

			if ($j==0) $smsc=$entry['smsc'];
			else $smsc.=",".$entry['smsc'];
			$j++;
		}
		$isdn_hw_cont.="pin=".$pin."\n";
		$isdn_hw_cont.="smsc=".$smsc."\n";
	}else{
		if ($lif1=="none" && $revision!="1.80" && $revision!="1.8") {
			$q=$ba->select('SELECT value FROM key_val WHERE key="portswitch"');
			$v=sqlite_fetch_single($q);
			$isdn_hw_cont.="portswitch=".$v."\n";
		}elseif ($lif1=="bf2GSM"){
			 $isdn_hw_cont.="portswitch=1\n";
		}else{
			$isdn_hw_cont.="portswitch=0\n";
		}
		$isdn_hw_cont.="country=".$ba->get('country')."\n";
		if ($type=="bf4FXS") $isdn_hw_cont.=($ba->get('boostringer')=="") ? "boostringer=0\n" : "boostringer=".$ba->get('boostringer')."\n";
		$isdn_hw_cont.="\n";
	}
}else{
	$isdn_hw_cont.="type=none\n\n";
}

$query = $ba->select('SELECT type  FROM isdn WHERE lif="1" union SELECT type  FROM analog WHERE lif="1" union SELECT type  FROM gsm WHERE lif="1"');
$isdn_hw_cont.="[lif1]\n";
$type=sqlite_fetch_single($query);
if ($type!=""){
	$isdn_hw_cont.="type=".$type."\n";
	$ntteSW="";
	$ntteHW="";
	$termination="";
	$crc="";
	$ptp="";
	$master="";
	$pin="";
	$smsc="";
	$clock="0";
	$i=0;
	$query = $ba->select('SELECT master, port, port_type FROM isdn WHERE lif="1" union SELECT master, port, port_type FROM analog WHERE lif="1" union SELECT master, port, port_type FROM gsm WHERE lif="1" order by port');
	while ($entry = $ba->fetch_array($query)) {
		$master=$entry['master'];

		if ($entry['port_type']=="FXS" || $entry['port_type']=="FXO" || $entry['port_type']=="GSM") continue;
		else{
			$q_isdn=$ba->select('SELECT * FROM isdn WHERE port="'.$entry['port'].'"');
			while ($entry_isdn = $ba->fetch_array($q_isdn)) {
				if ($ntteSW=="") $ntteSW=$entry_isdn['ntteSW'];
				else $ntteSW.=",".$entry_isdn['ntteSW'];

				if ($ntteHW=="") $ntteHW=$entry_isdn['ntteHW'];
				else $ntteHW.=",".$entry_isdn['ntteHW'];


				if ($termination=="") $termination=$entry_isdn['lt'];
				else $termination.=",".$entry_isdn['lt'];

				if ($crc=="") $crc=$entry_isdn['crc'];
				else $crc.=",".$entry_isdn['crc'];

				if ($ptp=="") $ptp=$entry_isdn['ptp'];
				else $ptp.=",".$entry_isdn['ptp'];

				if ($entry_isdn['clock']=="1"){
					$q=$ba->select('SELECT * FROM isdn WHERE lif=0 order by port');
					$clock=$entry_isdn['port']-sqlite_num_rows($q);
				}
			}
		}
	}
	$isdn_hw_cont.="master=".$master."\n";
	if ($type!="bf4FXS" && $type!="bf4FXO" && $type!="bf2GSM"){
		if ($clock!="") $isdn_hw_cont.="clock=".$clock."\n";
		$isdn_hw_cont.="ntteSW=".$ntteSW."\n";
		$isdn_hw_cont.="ntteHW=".$ntteHW."\n";
		if ($type=="bf4S0"){
			$isdn_hw_cont.="portswitch=0\n";
		}
		if ($type!="bf2E1" && $type!="bf1E1") $isdn_hw_cont.="termination=".$termination."\n";
		if ($type=="bf2E1" || $type=="bf1E1") $isdn_hw_cont.="crc=".$crc."\n";
		if ($type=="bf2S02FXS"){
			$isdn_hw_cont.="country=".$ba->get('country')."\n";
			$isdn_hw_cont.=($ba->get('boostringer')=="") ? "boostringer=0\n" : "boostringer=".$ba->get('boostringer')."\n";
		}
			$isdn_hw_cont.="ptp=".$ptp."\n\n";
	}elseif ($type=="bf2GSM"){
		$query = $ba->select('SELECT pin,smsc FROM gsm WHERE lif="1" order by port');
		$j=0;
		while ($entry = $ba->fetch_array($query)) {
			if ($j==0) $pin=$entry['pin'];
			else $pin.=",".$entry['pin'];

			if ($j==0) $smsc=$entry['smsc'];
			else $smsc.=",".$entry['smsc'];
			$j++;
		}
		$isdn_hw_cont.="pin=".$pin."\n";
		$isdn_hw_cont.="smsc=".$smsc."\n";
	}else{
		$isdn_hw_cont.="portswitch=0\n";
		$isdn_hw_cont.="country=".$ba->get('country')."\n";
		if ($type=="bf4FXS") $isdn_hw_cont.=($ba->get('boostringer')=="") ? "boostringer=0\n" : "boostringer=".$ba->get('boostringer')."\n";
		$isdn_hw_cont.="\n";
	}
}else{
	$query = $ba->select('SELECT master FROM isdn union SELECT master FROM analog union SELECT master FROM gsm');
        $master=true;
        while ($entry = $ba->fetch_array($query)) {
                if ($entry['master']=='1') $master=false;
        }
        //Wenn PCM master=1 und kein lif1 gesteckt ist master=0
        if ($master) $isdn_hw_cont.="type=none\nmaster=0\n\n";
        else $isdn_hw_cont.="type=none\n\n";
}

$isdn_hw_cont.="[pcm]\ntype=pcm\n";
$query = $ba->select('SELECT master FROM isdn union SELECT master FROM analog union SELECT master FROM gsm');
        $master=true;
        while ($entry = $ba->fetch_array($query)) {
                if ($entry['master']=='1') $master=false;
        }
        if ($master) $isdn_hw_cont.="master=1\n";
        else $isdn_hw_cont.="master=0\n";
if (!file_exists($file_isdnhw)){
        $fp= fopen($file_isdnhw,"w+");
        $write=fputs($fp,$isdn_hw_cont);
        fclose($fp);
}else if (file_exists($file_isdnhw)){
        $fp= fopen($file_isdnhw,"w+");
        $write=fputs($fp,$isdn_hw_cont);
        fclose($fp);
}

//isgw.cpt
$file_isgwcpt="/usr/conf/isgw.cpt";
$cpt_cont="";
	$query = $ba->select('SELECT * FROM callprogress');
	while ($entry = $ba->fetch_array($query)) {
		if ($entry['direction']=="SIP-ISDN") $direction="s2i";
		else if ($entry['direction']=="ISDN-SIP") $direction="i2s";
		$cpt_cont.="[".$direction.":".$entry['name']."]\n";
		$q_ent=$ba->select('SELECT * FROM callprogress_entries WHERE id_name="'.$entry['id'].'"');
		while ($entry_ = $ba->fetch_array($q_ent)) {
			if ($entry['direction']=="SIP-ISDN") $cpt_cont.=$entry_['type']."\t".$entry_['chann_det']."\t".$entry_['isdn_event']."\t".$entry_['sip_resp']."\t".$entry_['resp_text']."\n";
			else if ($entry['direction']=="ISDN-SIP") $cpt_cont.=$entry_['type']."\t".$entry_['isdn_event']."\t".$entry_['resp_text']."\t".$entry_['sip_resp']."\n";
		}
	}

if (!file_exists($file_isgwcpt)){
	$fp= fopen($file_isgwcpt,"w+");
	$write=fputs($fp,$cpt_cont);
	fclose($fp);
}else if (file_exists($file_isgwcpt)){
	$fp= fopen($file_isgwcpt,"w+");
	$write=fputs($fp,$cpt_cont);
	fclose($fp);
}


//isgw.analog
$file_analog="/usr/conf/isgw.analog";
$analog_cont="[general]\n";
$query = $ba->select('SELECT DISTINCT gr FROM analog WHERE gr!="NULL"');
while ($entry = $ba->fetch_array($query)) {
	$queryc = $ba->select('SELECT config FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$config="";
	while ($entryc = $ba->fetch_array($queryc)) {
		$config.=$entryc['config'];
	}

	$query_port = $ba->select('SELECT port FROM analog WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
	$ports="";
	while ($entryp = $ba->fetch_array($query_port)) {
		if($ports!=""){
			$ports.=",".$entryp['port'];
		}else{
			$ports.=$entryp['port'];
		}
	}
	$query_to = $ba->select('SELECT tones FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$tones="";
	while ($entryto = $ba->fetch_array($query_to)) {
		$tones.=$entryto['tones'];
	}

	$query_odt_e = $ba->select('SELECT overlap_dial_timeout_empty_dad FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$odt_e="";
	while ($entry_odt_e = $ba->fetch_array($query_odt_e)) {
		$odt_e.=$entry_odt_e['overlap_dial_timeout_empty_dad'];
	}

	$query_odt = $ba->select('SELECT overlap_dial_timeout FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$odt="";
	while ($entry_odt = $ba->fetch_array($query_odt)) {
		$odt.=$entry_odt['overlap_dial_timeout'];
	}
	$query_overlap_dialing = $ba->select('SELECT overlap_dialing FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$overlap_dialing="";
	while ($entry_overlap_dialing = $ba->fetch_array($query_overlap_dialing)) {
		$overlap_dialing.=$entry_overlap_dialing['overlap_dialing'];
	}
	$query_connect = $ba->select('SELECT fxo_connect FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$connect="";
	while ($entry_connect = $ba->fetch_array($query_connect)) {
		$connect.=$entry_connect['fxo_connect'];
	}
	$query_wait_for_oad = $ba->select('SELECT fxo_wait_for_oad FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$wait_for_oad="";
	while ($entry_wait_for_oad = $ba->fetch_array($query_wait_for_oad)) {
		$wait_for_oad.=$entry_wait_for_oad['fxo_wait_for_oad'];
	}
	$query_dialtone_passthrough = $ba->select('SELECT fxo_dialtone_passthrough FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$dialtone_passthrough="";
	while ($entry_dialtone_passthrough = $ba->fetch_array($query_dialtone_passthrough)) {
		$dialtone_passthrough.=$entry_dialtone_passthrough['fxo_dialtone_passthrough'];
	}
	$query_analog_call_ending_signal = $ba->select('SELECT fxo_call_ending_signal FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$analog_call_ending_signal="";
	while ($entry_analog_call_ending_signal = $ba->fetch_array($query_analog_call_ending_signal)) {
		$analog_call_ending_signal.=$entry_analog_call_ending_signal['fxo_call_ending_signal'];
	}
	$query_clip = $ba->select('SELECT clip FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$clip="";
	while ($entry_clip = $ba->fetch_array($query_clip)) {
		$clip.=$entry_clip['clip'];
	}
	$query_cnip = $ba->select('SELECT cnip FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$cnip="";
	while ($entry_cnip = $ba->fetch_array($query_cnip)) {
		$cnip.=$entry_cnip['cnip'];
	}
	$query_port_type = $ba->select('SELECT port_type FROM analog WHERE gr="'.$entry['gr'].'" group by gr');
	$port_type="";
	while ($entry_port_type = $ba->fetch_array($query_port_type)) {
		$port_type=$entry_port_type['port_type'];
	}
	$analog_cont.="[".$entry['gr']."]\n";
	$analog_cont.="ports = ".$ports."\n";
	$analog_cont.="interdigit_timeout = ".$odt."\n";
	$analog_cont.="interdigit_timeout_initial = ".$odt_e."\n";
	$analog_cont.="overlap_dialing = ".$overlap_dialing."\n";
	$analog_cont.="tones = ".$tones."\n";
	$analog_cont.="clip = ".$clip."\n";
	$analog_cont.="cnip = ".$cnip."\n";
	if ($port_type=="FXO"){
		$analog_cont.="fxo_connect = ".$connect."\n";
		$analog_cont.="fxo_wait_for_oad = ".$wait_for_oad."\n";
		$analog_cont.="fxo_dialtone_passthrough = ".$dialtone_passthrough."\n";
		$analog_cont.="fxo_call_ending_signal = ".$analog_call_ending_signal."\n";
	}
	$analog_cont.="config = ".$config."\n";

}


if (!file_exists($file_analog)){
	$fp= fopen($file_analog,"w+");
	$write=fputs($fp,$analog_cont);
	fclose($fp);
}else if (file_exists($file_analog)){
	$fp= fopen($file_analog,"w+");
	$write=fputs($fp,$analog_cont);
	fclose($fp);
}

//isgw.gsm
$file_gsm="/usr/conf/isgw.gsm";
$gsm_cont="[general]\n";
$query = $ba->select('SELECT * FROM gsm ORDER BY port ASC');
$i=0;
while ($entry_gsm = $ba->fetch_array($query)) {
	if ($i==0) $pin=$entry_gsm['pin'];
	else $pin.=",".$entry_gsm['pin'];

	if ($i==0) $smsc=$entry_gsm['smsc'];
	else $smsc.=",".$entry_gsm['smsc'];

	$i++;
}
$gsm_cont.="pin=".$pin."\n";
$gsm_cont.="smsc=".$smsc."\n";
$query = $ba->select('SELECT DISTINCT gr FROM gsm WHERE gr!="NULL"');
while ($entry = $ba->fetch_array($query)) {
	$queryc = $ba->select('SELECT config, sms_extension, extension FROM gsm WHERE gr="'.$entry['gr'].'" group by gr');
	$config="";
	$sms_extension="";
	$extension="";
	while ($entryc = $ba->fetch_array($queryc)) {
		$config=$entryc['config'];
		$sms_extension=$entryc['sms_extension'];
		$extension=$entryc['extension'];
	}

	$query_port = $ba->select('SELECT port FROM gsm WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
	$ports="";
	while ($entryp = $ba->fetch_array($query_port)) {
		if($ports!=""){
			$ports.=",".$entryp['port'];
		}else{
			$ports.=$entryp['port'];
		}
	}

	$query_pin = $ba->select('SELECT pin FROM gsm WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
	$pin="";
	$i=0;
	while ($entryp = $ba->fetch_array($query_pin)) {
		if($i>0){
			$pin.=",".$entryp['pin'];
		}else{
			$pin.=$entryp['pin'];
		}
		$i++;
	}
	$query_smsc = $ba->select('SELECT smsc FROM gsm WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
	$smsc="";
	$i=0;
	while ($entrys = $ba->fetch_array($query_smsc)) {
		if($i>0){
			$smsc.=",".$entrys['smsc'];
		}else{
			$smsc.=$entrys['smsc'];
		}
		$i++;
	}


	$gsm_cont.="[".$entry['gr']."]\n";
	$gsm_cont.="ports = ".$ports."\n";
	$gsm_cont.="config = ".$config."\n";
	$gsm_cont.="sms_extensions = ".$sms_extension."\n";
	$gsm_cont.="extensions = ".$extension."\n";
	//$gsm_cont.="pin = ".$pin."\n";
	//$gsm_cont.="smsc = ".$smsc."\n";

}

if (!file_exists($file_gsm)){
	$fp= fopen($file_gsm,"w+");
	$write=fputs($fp,$gsm_cont);
	fclose($fp);
}else if (file_exists($file_gsm)){
	$fp= fopen($file_gsm,"w+");
	$write=fputs($fp,$gsm_cont);
	fclose($fp);
}



//misc.conf
$file_misc="/usr/conf/misc.conf";
$m_cont="";
$m_cont.="[general]\n";
$m_cont.="ntphost=".$bc->get("root", "ntphost")."\n";
$m_cont.="timezone=".$bc->get("root", "timezone")."\n";
$m_cont.="root-version=".$bc->get("root", "root-version")."\n";
$m_cont.="lan-iface=".$bc->get("root", "lan-iface")."\n";
$m_cont.="lan-ipaddr=".$bc->get("root", "lan-ipaddr")."\n";
$m_cont.="lan-netmask=".$bc->get("root", "lan-netmask")."\n";
$m_cont.="lan-gateway=".$bc->get("root", "lan-gateway")."\n";
$m_cont.="lan-nameserver=".$bc->get("root", "lan-nameserver")."\n";
$m_cont.="lan-dhcp=".$bc->get("root", "lan-dhcp")."\n";
$m_cont.="msp-iface=".$bc->get("root", "msp-iface")."\n";
$m_cont.="msp-ipaddr=".$bc->get("root", "msp-ipaddr")."\n";
$m_cont.="app-image=".$bc->get("root", "app-image")."\n";
$m_cont.="app-status=".$bc->get("root", "app-status")."\n";
$m_cont.="lan-mtu=".$bc->get("root", "lan-mtu")."\n";
$m_cont.="use-boot-prov=".$bc->get("root", "use-boot-prov")."\n";
$m_cont.="tftp-host=".$bc->get("root", "tftp-host")."\n";
$m_cont.="tftp-url=".$bc->get("root", "tftp-url")."\n";
$m_cont.="provisioning-option=".$bc->get("root", "provisioning-option")."\n";
$m_cont.="vlan-enable=".$bc->get("root", "vlan-enable")."\n";
$m_cont.="vlan-id=".$bc->get("root", "vlan-id")."\n";
$m_cont.="routing=".$bc->get("root", "routing")."\n";
$m_cont.="snmp-enabled=".$bc->get("root", "snmp-enabled")."\n";
$m_cont.="cloud_enable=".$bc->get("root", "cloud_enable")."\n";
$m_cont.="cloud_key=".$bc->get("root", "cloud_key")."\n";
$m_cont.="vlan-prio=".$bc->get("root", "vlan-prio")."\n";
$m_cont.="userapp-enabled=".$bc->get("root", "userapp-enabled")."\n";
$m_cont.="userapp-user-enabled=".$bc->get("root", "userapp-enabled")."\n";
$m_cont.="beroAvahi-enabled=".$bc->get("root", "beroAvahi-enabled")."\n";
$m_cont.="system-check-enabled=".$bc->get("root", "system-check-enabled")."\n";
$m_cont.="safeguard=".$bc->get("root", "safeguard")."\n";
//GSM SMS
$m_cont.="smgw-enabled=".$bc->get("root", "smgw-enabled")."\n";
$m_cont.="smtp_host=".$bc->get("root", "smtp_host")."\n";
$m_cont.="smtp_port=".$bc->get("root", "smtp_port")."\n";
$m_cont.="smtp_user=".$bc->get("root", "smtp_user")."\n";
$m_cont.="smtp_pass=".$bc->get("root", "smtp_pass")."\n";
$m_cont.="smtp_from=".$bc->get("root", "smtp_from")."\n";
$m_cont.="smtp_rcpt=".$bc->get("root", "smtp_rcpt")."\n";
$m_cont.="pop3_host=".$bc->get("root", "pop3_host")."\n";
$m_cont.="pop3_port=".$bc->get("root", "pop3_port")."\n";
$m_cont.="pop3_user=".$bc->get("root", "pop3_user")."\n";
$m_cont.="pop3_pass=".$bc->get("root", "pop3_pass")."\n";
$m_cont.="smgw_use_ssl=".$bc->get("root", "smgw_use_ssl")."\n";

$m_cont.="summer_winter_time=".$ba->get("summer_winter_time")."\n";
$m_cont.="bnfos-ipaddr=".$ba->get("ip_berofos")."\n";
$m_cont.="bnfos-mac=".$ba->get("mac_berofos")."\n";
$m_cont.="bnfos-intv=".$ba->get("interval_berofos")."\n";
$m_cont.="bnfos-hb-on-boot=".$bc->get("root", "use_boot_bnfoswdog")."\n";
$m_cont.="http_acl=".$ba->get("http_acl")."\n";
$m_cont.="sip_acl=".$ba->get("sip_acl")."\n";
$m_cont.="rtp_acl=".$ba->get("rtp_acl")."\n";
$m_cont.="bfdetect_acl=".$ba->get("bfdetect_acl")."\n";
$m_cont.="ssh_acl=".$ba->get("ssh_acl")."\n";
$query=$ba->select("select * from general");
while ($entry = $ba->fetch_array($query)) {
$m_cont.="telnet_acl=".$entry['telnet_acl']."\n";
}
$query = $ba->select('SELECT * FROM users where user!="beroAdmin"');
	while ($entry = $ba->fetch_array($query)) {
		$admin_user=$entry['user'];
		$admin_pass=$entry['password'];
	}
$query = $ba->select('SELECT show_app FROM set_app');
	while ($entry = $ba->fetch_array($query))
		$sessions_disable=$entry['show_app'];
$m_cont.="admin-pass=".$admin_pass."\n";
$m_cont.="admin-user=".$admin_user."\n";
$m_cont.="sessions-disable=".$sessions_disable."\n";

$wr=$m_cont;
if (!file_exists($file_misc)){
	$fp= fopen($file_misc,"w+");
	$write=fputs($fp,$wr);
	fclose($fp);
}else if (file_exists($file_misc)){
	$fp= fopen($file_misc,"w+");
	$write=fputs($fp,$wr);
	fclose($fp);
}

//isgw.causes
$file_causes="/usr/conf/isgw.causes";
$c_cont=";FROM\tTO\tINCAUSE\tOUTCAUSE\tOUTTEXT\n";
$query_d=$ba->select("select * from isgw_causes");
while ($entry = $ba->fetch_array($query_d)) {
	$dir=explode("-",$entry['direction']);
	$c_cont.=$dir[0]."\t".$dir[1]."\t";
	$c_cont.=$entry['to_cause']."\t".$entry['from_cause']."\t";
	$c_cont.=$entry['sip_response_message']."\t\n";
}
$wr=$c_cont;
if (!file_exists($file_causes)){
	$fp= fopen($file_causes,"w+");
	$write=fputs($fp,$wr);
	fclose($fp);
}else if (file_exists($file_causes)){
	$fp= fopen($file_causes,"w+");
	$write=fputs($fp,$wr);
	fclose($fp);
}


//isgw.dialplan
$file_dialplan="/usr/conf/isgw.dialplan";
$d_cont=";From\tTo\tFromID\tToID\tDestination\tNewDestination\tSource\tNewSource\tConfig\n";
$query_d=$ba->select("select * from dialplan order by posId, id asc");
while ($entry = $ba->fetch_array($query_d)) {
	if ($entry['activ']=="1"){
		if (($entry['fromDirection'] == 'isdn' || $entry['fromDirection'] == 'analog' || $entry['fromDirection'] == 'gsm') && ($entry['toDirection'] == 'isdn' || $entry['toDirection'] == 'analog' || $entry['toDirection'] == 'gsm')) {
			$d_cont.=$entry['fromDirection']."\t";
			//$d_cont.="isdn\t";
			$d_cont.="sip\t";
			$d_cont.=$entry['fromId']."\t";
			$d_cont.="127.0.0.1\t";
			$d_cont.=$entry['dest']."\t";
			if (strpos($entry['newDest'],"ISDNZUISDN")===false){
				$d_cont.=$entry['isdnid'].$entry['newDest']."\t";
			}else{
				$d_cont.=$entry['newDest']."\t";
			}
			$d_cont.=$entry['src']."\t";
			$d_cont.=$entry['newSrc']."\t";
			$d_cont.=$entry['config']."\n";

			$d_cont.="sip\t";
			$d_cont.=$entry['toDirection']."\t";
			$d_cont.="127.0.0.1\t";
			$d_cont.=$entry['toId']."\t";
			$d_cont.=$entry['isdnid']."(.*)"."\t";
			$d_cont.=stripslashes("\\\\1")."\t";
			$d_cont.=stripslashes("(.*)")."\t";
			$d_cont.=stripslashes("\\\\1")."\t";
			$d_cont.=$entry['config']."\n";
		}else{

			$d_cont.=$entry['fromDirection']."\t";
			$d_cont.=$entry['toDirection']."\t";
			$d_cont.=$entry['fromId']."\t";
			$d_cont.=$entry['toId']."\t";
			$d_cont.=$entry['dest']."\t";
			$d_cont.=$entry['newDest']."\t";
			$d_cont.=$entry['src']."\t";
			$d_cont.=$entry['newSrc']."\t";
			$d_cont.=$entry['config']."\n";
		}
	}
}
$wr=$d_cont;
if (!file_exists($file_dialplan)){
	$fp= fopen($file_dialplan,"w+");
	$write=fputs($fp,$wr);
	fclose($fp);
}else if (file_exists($file_dialplan)){
	$fp= fopen($file_dialplan,"w+");
	$write=fputs($fp,$wr);
	fclose($fp);
}


$query=$ba->select("select option from activate where id='activate'");
while($entry = $ba->fetch_array($query)){
	if ($entry['option']==3){
		$query=$ba->update("update activate SET option=0 where id='activate'");
		echo "<html>
			<body>
			<form class='beroari_form' action='../index.php?m=rebres' method='POST' name='reboot'>
			<input type='hidden' name='action' value='reboot'>
			<input type='hidden' name='v' value='Yes'>
			</form>
			<script>document.reboot.submit();</script>
			</body>
			</html>";
	}elseif ($entry['option']==2){
		exec('/usr/local/init/S50isgw restart',$ausgabe);
		exec('/usr/local/init/S03acl stop');
		exec('/usr/local/init/S03acl start');
		exec('/usr/local/init/S60cloud stop');
		exec('/usr/local/init/S60cloud start');
		exec('/usr/local/init/S90systemlog stop');
		exec('/usr/local/init/S90systemlog start');
		exec('/usr/local/init/S80snmpd restart');
		 exec('/usr/local/init/S55SMGW restart');
	}elseif ($entry['option']==1){
		require_once "./includes/isgwtelnet.php";
                $isgw = new isgwtelnet();
                $result=$isgw->isgw_login();
                $reload=$isgw->isgw_dialplanreload();
                $sip_reload=$isgw->isgw_sipreload();
                $isgw->isgw_rcpt();
                $isgw->isgw_rgc();

                exec('/usr/local/init/S60cloud stop');
                exec('/usr/local/init/S60cloud start');
                exec('/usr/local/init/S80snmpd restart');
                exec('/usr/local/init/S55SMGW restart');
                exec('/etc/init.d/S91userapp restart');
	}
}
if (!$ba->is_error()){
	$query=$ba->update("update activate SET option=0 where id='activate'");
}
if ($ba->is_error()){
	echo "<script>alert('".$ba->error()."');</script>";
}
echo "<script>window.location='../index.php?m=".$_GET['m']."'</script>";

