<?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'];
			
			
			//DNUMPLAN
			if (isset($_POST['dnumplan']))	$config.=",dnumplan=".$_POST['dnumplan'];
			
			//ONUMPLAN
			if (isset($_POST['onumplan']))	$config.=",onumplan=".$_POST['onumplan'];
			
			//BEARER
			if (isset($_POST['bearer_cap']))	$config.=",bearer_cap=".$_POST['bearer_cap'];
			
			//SCRENNING
			if (isset($_POST['screen'])){
				$config.=",screen=1";
			}elseif(!isset($_POST['screen'])){
				$config.=",screen=0";
			}
			
			//CALL DEFLECTION
			if (isset($_POST['cd'])){
				$config.=",cd=1";
			}elseif(!isset($_POST['cd'])){
				$config.=",cd=0";
			}
			
			//PRESENTATION
			if (isset($_POST['pres'])){
				$config.=",pres=1";
			}elseif(!isset($_POST['pres'])){
				$config.=",pres=0";
			}
			
			//ISDN_OAD_SETTING
			if (isset($_POST['isdn_oad_setting']) && $_POST['isdn_oad_setting']!='manual') $config.=",isdn_oad_setting=".$_POST['isdn_oad_setting'];
			else if (isset($_POST['isdn_oad_setting']) && $_POST['isdn_oad_setting']=='manual') $config.=",isdn_oad_setting=".$_POST['isdn_oad_setting_manual'];
			
			//ISDN_OAD2_SETTING
			if (isset($_POST['isdn_oad2_setting']) && $_POST['isdn_oad2_setting']!='manual') $config.=",isdn_oad2_setting=".$_POST['isdn_oad2_setting'];
			else if (isset($_POST['isdn_oad2_setting']) && $_POST['isdn_oad2_setting']=='manual') $config.=",isdn_oad2_setting=".$_POST['isdn_oad2_setting_manual'];
			
			//ISDN_QSIGNAME_SETTING
			if (isset($_POST['isdn_qsigname_setting']) && $_POST['isdn_qsigname_setting']!='manual') $config.=",isdn_qsigname_setting=".$_POST['isdn_qsigname_setting'];
			else if (isset($_POST['isdn_qsigname_setting']) && $_POST['isdn_qsigname_setting']=='manual') $config.=",isdn_qsigname_setting=".$_POST['isdn_qsigname_setting_manual'];
			
			//ISDN_REDIRECTED_NR_SETTING
			if (isset($_POST['isdn_redirected_nr_setting']) && $_POST['isdn_redirected_nr_setting']!='manual') $config.=",isdn_redirected_nr_setting=".$_POST['isdn_redirected_nr_setting'];
			else if (isset($_POST['isdn_redirected_nr_setting']) && $_POST['isdn_redirected_nr_setting']=='manual') $config.=",isdn_redirected_nr_setting=".$_POST['isdn_redirected_nr_setting_manual'];

			//PSTN_SRC_SETTING
			if (isset($_POST['from_pstn_src_setting_isdn'])) $config.=",from_pstn_src_setting=".$_POST['from_pstn_src_setting_isdn'];
			
			//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 isdn");
		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 isdn SET gr='NULL', config='' WHERE gr='".$_POST['gr_upd']."'");
		$gr=$_POST['group'];
		//$config=get_difference_from_defaults();
		$ports=$_POST['ports'];
		$new_ports=$_POST['new_ports'];
		$chansel=$_POST['chansel'];
		$chansel_dir=$_POST['chansel_dir'];
		$tones=$_POST['tones'];
		$portpullup=$_POST['portpullup'];
		$odt=$_POST['odt'];
		$odt_empty_dad=$_POST['odt_empty_dad'];
		$port="";
		if (isset($_POST['overlap_dialing'])){
			$overlap_dialing="1";
		}else{
			$overlap_dialing="0";
		}
		if (isset($_POST['qsig'])){
			$qsig="1";
		}else{
			$qsig="0";
		}
		$config=parameters();
		
		for ($i=0;$i<count($ports);$i++){
			$port = $ports[$i];
			$query_new = $ba->update("UPDATE isdn SET gr='$gr', config='$config', qsig='$qsig', portpullup='$portpullup', overlap_dialing='$overlap_dialing', odt='$odt', odt_empty_dad='$odt_empty_dad', chansel='$chansel', chansel_dir='$chansel_dir', tones='$tones' WHERE port='$port'");
		}
		$new_port="";
		for ($i=0;$i<count($new_ports);$i++){
			$new_port= $new_ports[$i];
			$query_new = $ba->update("UPDATE isdn SET gr='NULL', config='', qsig='no', overlap_dialing='no', odt='3', odt_empty_dad='15', chansel='standard', chansel_dir='ascending', portpullup='1', tones='[de]' WHERE port='$new_port'");
		}
		if (!$ba->is_error()){
			$query=$ba->update("UPDATE dialplan SET fromId='g:".$gr."' where fromDirection='isdn' and fromId='g:".$_POST['gr_upd']."'");
			$query=$ba->update("UPDATE dialplan SET toId='g:".$gr."' where toDirection='isdn' 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=isdn-".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 isdn");
			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'];
			//$config=get_difference_from_defaults();
			$ports=$_POST['ports'];
			$new_ports=$_POST['new_ports'];
			$chansel=$_POST['chansel'];
			$chansel_dir=$_POST['chansel_dir'];
			$tones=$_POST['tones'];
			$portpullup=$_POST['portpullup'];
			$odt=$_POST['odt'];
			$odt_empty_dad=$_POST['odt_empty_dad'];
			if (isset($_POST['overlap_dialing'])){
				$overlap_dialing="1";
			}else{
				$overlap_dialing="0";
			}
			if (isset($_POST['qsig'])){
				$qsig="1";
			}else{
				$qsig="0";
			}
			$port="";
			$config=parameters();

			for ($i=0;$i<count($ports);$i++){
				$port = $ports[$i];
				$query_new = $ba->update("UPDATE isdn SET gr='$gr', config='$config', qsig='$qsig', portpullup='$portpullup', overlap_dialing='$overlap_dialing', odt='$odt', odt_empty_dad='$odt_empty_dad', chansel='$chansel', chansel_dir='$chansel_dir', tones='$tones' WHERE port='$port'");
			}
			$new_port="";
			for ($i=0;$i<count($new_ports);$i++){
				$new_port= $new_ports[$i];
				$query_new = $ba->update("UPDATE isdn SET gr='NULL', config='', odt='3', qsig='0', portpullup='1', overlap_dialing='0', odt_empty_dad='15', chansel='standard', chansel_dir='ascending', tones='[de]' 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=isdn-".strtolower($_GET['opt'])."'</script>";
				echo "<script>this.window.close();</script>";

			}else{
				echo "<script>alert(".$ba->error().");</script>";
			}

		}else if ($_POST['submit']=="Save"){
			echo "<script> alert('You must fill all the fields'); window.history.back(); </script>";
		}
	}
}


?>
