<?php
//Isdn und analog Einträge speichern
@session_start();
if (!isset($_SESSION['beroari_time'])){
	 echo "<script>this.window.close();</script>";
}elseif((isset($_SESSION['beroari_time']))&&(($_SESSION['beroari_time']+1200)<time())){
	@session_unset();
	@session_destroy();
	echo "<script>window.opener.location='../index.php'</script>";
	echo "<script>this.window.close();</script>";
}else{
	$_SESSION['beroari_time']=time();

	chdir("..");

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

	function parameters($config=""){
		foreach ($_POST as $key=>$value){
			$_POST[$key]=str_replace("'","\"",$_POST[$key]);
		}

			//EC
			if (isset($_POST['ec']) && $config!=""){
				$config.=",ec=1";
			}elseif(isset($_POST['ec']) && $config==""){
				$config.="ec=1";
			}elseif(!isset($_POST['ec']) && $config!=""){
				$config.=",ec=0";
			}elseif(!isset($_POST['ec']) && $config==""){
				$config.=",ec=0";
			}

			//ECTL
			if (isset($_POST['ectl']))	$config.=",ectl=".$_POST['ectl'];

			//ANALOG_OAD_SETTING
			if (isset($_POST['analog_oad_setting']) && $_POST['analog_oad_setting']!='manual') $config.=",analog_oad_setting=".$_POST['analog_oad_setting'];
			else if (isset($_POST['analog_oad_setting']) && $_POST['analog_oad_setting']=='manual') $config.=",analog_oad_setting=".$_POST['analog_oad_setting_manual'];

			//ANALOG_DISPLAY_SETTING
			if (isset($_POST['analog_display_setting']) && $_POST['analog_display_setting']!='manual') $config.=",analog_display_setting=".$_POST['analog_display_setting'];
			else if (isset($_POST['analog_display_setting']) && $_POST['analog_display_setting']=='manual') $config.=",analog_display_setting=".$_POST['analog_display_setting_manual'];

			//PSTN_SRC_SETTING
			if (isset($_POST['from_pstn_src_setting_analog'])) $config.=",from_pstn_src_setting=".$_POST['from_pstn_src_setting_analog'];

			//CLIR on OAD
			if (isset($_POST['clir_on_oad']))	$config.=",clir_on_oad=".$_POST['clir_on_oad'];

			//Additional config string hinzufügen.
			if ($_POST['addit_options']!="") $config.=",".str_replace(array("\r\n", "\n", "\r"),",",$_POST['addit_options']);

			return $config;
	}

	if ($_POST['gr_upd']!=""){
		if ($ba->is_error()){
			die($ba->error());
		}
		if (strtolower($_POST['group'])=="general"){
			echo "<script>alert('Invalid Group Name');  window.history.back();</script>";
			return;
		}
		$query_comp=$ba->select("SELECT gr FROM analog");
		while ($entry = $ba->fetch_array($query_comp)) {
			if ($_POST['gr_upd']!=$entry['gr']){
				if ($_POST['group']==$entry['gr']){
					echo "<script>alert('Invalid Group Name');  window.history.back();</script>";
					return;
				}
			}
		}

		$query_new = $ba->update("UPDATE analog SET gr='NULL', config='' WHERE gr='".$_POST['gr_upd']."'");
		$gr=$_POST['group'];
		$ports=$_POST['ports'];
		$new_ports=$_POST['new_ports'];
		$overlap_dial_timeout_empty_dad=$_POST['overlap_dial_timeout_empty_dad'];
		$overlap_dial_timeout=$_POST['overlap_dial_timeout'];
		if (isset($_POST['overlap_dialing'])){
			$overlap_dialing="1";
		}else{
			$overlap_dialing="0";
		}
		$tones=$_POST['tones'];
		$connect=$_POST['fxo_connect'];
		$wait_for_oad=$_POST['fxo_wait_for_oad'];
		$clip=$_POST['clip'];
		$cnip=$_POST['cnip'];
		$analog_call_ending_signal=$_POST['fxo_call_ending_signal'];
		if ($analog_call_ending_signal){
	 		foreach ($analog_call_ending_signal as $value){$fxo_call_ending_signal.=",".$value;};
		}
		$fxo_call_ending_signal=substr($fxo_call_ending_signal,1,strlen($fxo_call_ending_signal)-1);
		if (isset($_POST['fxo_dialtone_passthrough']))	$dialtone_passthrough=$_POST['fxo_dialtone_passthrough'];
		else $dialtone_passthrough=0;



		$port="";
		$config=parameters();

		for ($i=0;$i<count($ports);$i++){
			$port = $ports[$i];
			$query_new = $ba->update("UPDATE analog SET gr='$gr', config='$config', clip='$clip', cnip='$cnip', overlap_dial_timeout_empty_dad='$overlap_dial_timeout_empty_dad', overlap_dial_timeout='$overlap_dial_timeout', tones='$tones', fxo_connect='$connect', fxo_wait_for_oad='$wait_for_oad', fxo_dialtone_passthrough='$dialtone_passthrough', fxo_call_ending_signal='$fxo_call_ending_signal', overlap_dialing='$overlap_dialing' WHERE port='$port'");
		}
		$new_port="";
		for ($i=0;$i<count($new_ports);$i++){
			$new_port= $new_ports[$i];
			$query_new = $ba->update("UPDATE analog SET gr='NULL', config='', clip='', cnip='', overlap_dial_timeout_empty_dad='15', overlap_dial_timeout='3', tones='[de]', fxo_connect='instant', fxo_wait_for_oad='wait', fxo_dialtone_passthrough='0', fxo_call_ending_signal='unobtainable', overlap_dialing='0'  WHERE port='$new_port'");
		}
		if (!$ba->is_error()){
			$query=$ba->update("UPDATE dialplan SET fromId='g:".$gr."' where fromDirection='analog' and fromId='g:".$_POST['gr_upd']."'");
			$query=$ba->update("UPDATE dialplan SET toId='g:".$gr."' where toDirection='analog' and toId='g:".$_POST['gr_upd']."'");

		}

		$query=$ba->select("select option from activate where id='activate'");
		while($entry = $ba->fetch_array($query)){
			if ($entry['option'] <= 2) $query =  $ba->update("UPDATE activate SET option=2 where id='activate'");
		}
		if (!$ba->is_error()){
				echo "<script>window.opener.location='../index.php?m=analog-".strtolower($_GET['opt'])."'</script>";
			echo "<script>this.window.close();</script>";

		}else{
			echo "<script>alert(".$ba->error().");</script>";
		}
	}else{
		if (($_POST['group'])&&($_POST['ports'])){
			if ($ba->is_error()){
				die($ba->error());
			}
			if (strtolower($_POST['group'])=="general"){
				echo "<script>  alert('Invalid Group Name'); window.history.back();</script>";
				return;
			}
			$query_comp=$ba->select("SELECT gr FROM analog");
			while ($entry = $ba->fetch_array($query_comp)) {
				if ($_POST['group']==$entry['gr'] ){
					echo "<script> alert('Invalid Group Name'); window.history.back();</script>";
					return;
				}
			}
			$gr=$_POST['group'];
			$ports=$_POST['ports'];
			$new_ports=$_POST['new_ports'];
			$port="";
			$config=parameters();
			$overlap_dial_timeout_empty_dad=$_POST['overlap_dial_timeout_empty_dad'];
			$overlap_dial_timeout=$_POST['overlap_dial_timeout'];
			if (isset($_POST['overlap_dialing'])){
				$overlap_dialing="1";
			}else{
				$overlap_dialing="0";
			}
			$tones=$_POST['tones'];
			$connect=$_POST['fxo_connect'];
			$wait_for_oad=$_POST['fxo_wait_for_oad'];
			$clip=$_POST['clip'];
			$cnip=$_POST['cnip'];
			$analog_call_ending_signal=$_POST['fxo_call_ending_signal'];
			if ($analog_call_ending_signal){
	 			foreach ($analog_call_ending_signal as $value){$fxo_call_ending_signal.=",".$value;};
			}
			$fxo_call_ending_signal=substr($fxo_call_ending_signal,1,strlen($fxo_call_ending_signal)-1);
			$analog_call_ending_signal=$_POST['fxo_call_ending_signal'];
			if (isset($_POST['fxo_dialtone_passthrough']))	$dialtone_passthrough=$_POST['fxo_dialtone_passthrough'];
			else $dialtone_passthrough=0;

			for ($i=0;$i<count($ports);$i++){
				$port = $ports[$i];
				$query_new = $ba->update("UPDATE analog SET gr='$gr', config='$config', clip='$clip', cnip='$cnip', overlap_dial_timeout_empty_dad='$overlap_dial_timeout_empty_dad', overlap_dial_timeout='$overlap_dial_timeout', tones='$tones', fxo_connect='$connect', fxo_wait_for_oad='$wait_for_oad', fxo_dialtone_passthrough='$dialtone_passthrough', fxo_call_ending_signal='$fxo_call_ending_signal', overlap_dialing='$overlap_dialing' WHERE port='$port'");
			}
			$new_port="";
			for ($i=0;$i<count($new_ports);$i++){
				$new_port= $new_ports[$i];
				$query_new = $ba->update("UPDATE analog SET gr='NULL', config='', clip='', cnip='', overlap_dial_timeout_empty_dad='15', overlap_dial_timeout='3', tones='[de]', fxo_connect='instant', fxo_wait_for_oad='wait', fxo_dialtone_passthrough='0', fxo_call_ending_signal='unobtainable', overlap_dialing='0'  WHERE port='$new_port'");
			}



			$query=$ba->select("select option from activate where id='activate'");
			while($entry = $ba->fetch_array($query)){
				if ($entry['option'] <= 2) $query =  $ba->update("UPDATE activate SET option=2 where id='activate'");
			}
			if (!$ba->is_error()){
				echo "<script>window.opener.location='../index.php?m=analog-".strtolower($_GET['opt'])."'</script>";
				echo "<script>this.window.close();</script>";
			}else{
				echo "<script>alert(".$ba->error().");</script>";
			}

		}else if ($_POST['submit']=="Save" && !isset($_POST['ports'])){
			echo "<script>  alert('You must at least select one Port'); window.history.back();</script>";
		}else if ($_POST['submit']=="Save" && (!$_POST['group'])){
			echo "<script>  alert('The group name can not be blank'); window.history.back();</script>";
		}
	}
}


?>
