<?php
//Überprüfung on die SESSION noch gültig ist
@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';
	$ba = new beroAri();
	require_once ('./includes/lang.php');
	require_once ('./includes/isgwtelnet.php');
	//AJAX um die Haupseite zu aktualisieren
	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>";




	//Liefert das endgültige Configstring
	function parameters($config,$from,$to){
		foreach ($_POST as $key=>$value){
			$_POST[$key]=str_replace("'","\"",$_POST[$key]);
		}
			//EC
			if (!isset($_POST['def_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['def_ectl'])){
				if (isset($_POST['ectl']))
					$config.=",ectl=".trim($_POST['ectl']);
			}
			//DNUMPLAN
			if (!isset($_POST['def_dnumplan'])){
				if (isset($_POST['dnumplan']))
					$config.=",dnumplan=".trim($_POST['dnumplan']);
			}

			//ONUMPLAN
			if (!isset($_POST['def_onumplan'])){
				if (isset($_POST['onumplan']))
					$config.=",onumplan=".trim($_POST['onumplan']);
			}

			//BEARER
			if (!isset($_POST['def_bearer_cap'])){
			if (isset($_POST['bearer_cap']))
				$config.=",bearer_cap=".trim($_POST['bearer_cap']);
			}
			//SCRENNING
			if (!isset($_POST['def_screen'])){
				if (isset($_POST['screen'])){
					$config.=",screen=1";
				}elseif(!isset($_POST['screen'])){
					$config.=",screen=0";
				}
			}
			//PRESENTATION
			if (!isset($_POST['def_pres'])){
				if (isset($_POST['pres'])){
					$config.=",pres=1";
				}elseif(!isset($_POST['pres'])){
					$config.=",pres=0";
				}
			}
			//CALL DEFLECTION
			if (!isset($_POST['def_cd'])){
				if (isset($_POST['cd'])){
					$config.=",cd=1";
				}elseif(!isset($_POST['cd'])){
					$config.=",cd=0";
				}
			}

			//T.38
			if (!isset($_POST['def_t38'])){
				if (isset($_POST['t38'])){
					$config.=",t38=1";
				}elseif(!isset($_POST['t38'])){
					$config.=",t38=0";
				}
			}
			//IE on SIP
			if (!isset($_POST['def_ie_on_sip'])){
				if (isset($_POST['ie_on_sip'])){
					$config.=",ie_on_sip=1";
				}elseif(!isset($_POST['ie_on_sip'])){
					$config.=",ie_on_sip=0";
				}
			}

			//WAIT FOR CANCEL
			if (!isset($_POST['def_wait_for_cancel'])){
				if (isset($_POST['wait_for_cancel'])){
					$config.=",wait_for_cancel=1";
				}elseif(!isset($_POST['wait_for_cancel'])){
					$config.=",wait_for_cancel=0";
				}
			}
			//DTMF
			if (!isset($_POST['def_dtmfmode'])){
				if (isset($_POST['dtmfmode']))
					$config.=",dtmfmode=".trim($_POST['dtmfmode']);
			}
			//DTMF
			if (!isset($_POST['def_s2i_cpt'])){
				if (isset($_POST['s2i_cpt']))
					$config.=",s2i_cpt=".trim($_POST['s2i_cpt']);
			}
			//DTMF
			if (!isset($_POST['def_i2s_cpt'])){
				if (isset($_POST['i2s_cpt']))
					$config.=",i2s_cpt=".trim($_POST['i2s_cpt']);
			}
			//CODECS
			if (!isset($_POST['def_codecs'])){
				$c="";
				for ($i=0;$i<count($_POST['codecs']);$i++){
					if ($c!="") $c.=":";
					$c.= $_POST['codecs'][$i];
				}
				if (isset($_POST['codecs']) && $defc[$_POST['fromIdSip']]['codecs']!=$c)
					$config.=",codecs=".$c;
			}

			//ANALOG_OAD_SETTING
			if (!isset($_POST['def_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['def_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'];
			}

			//FROM_PSTN_SRC_SETTING
			if ($from=="isdn" && $to=="analog"){
				if (!isset($_POST['def_from_pstn_src_setting_isdn'])) $config.=",from_pstn_src_setting=".$_POST['from_pstn_src_setting_isdn'];
				if (!isset($_POST['def_from_pstn_src_setting_analog']) && strpos($config,"from_pstn_src_setting")===false) $config.=",from_pstn_src_setting=".$_POST['from_pstn_src_setting_analog'];
			}else{
				if (!isset($_POST['def_from_pstn_src_setting_analog'])) $config.=",from_pstn_src_setting=".$_POST['from_pstn_src_setting_analog'];
				if (!isset($_POST['def_from_pstn_src_setting_isdn']) && strpos($config,"from_pstn_src_setting")===false) $config.=",from_pstn_src_setting=".$_POST['from_pstn_src_setting_isdn'];
			}
			//ISDN_OAD_SETTING
			if (!isset($_POST['def_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['def_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['def_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['def_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'];
			}

			//FROM_SIP_SRC_SETTING
			if (!isset($_POST['def_from_sip_src_setting'])){
				if (isset($_POST['from_sip_src_setting'])) $config.=",from_sip_src_setting=".$_POST['from_sip_src_setting'];
			}

			//SIP_FROM_USER_SETTING
			if (!isset($_POST['def_sip_from_user_setting'])){
				if (isset($_POST['sip_from_user_setting']) && $_POST['sip_from_user_setting']!='manual') $config.=",sip_from_user_setting=".$_POST['sip_from_user_setting'];
				else if (isset($_POST['sip_from_user_setting']) && $_POST['sip_from_user_setting']=='manual') $config.=",sip_from_user_setting=".$_POST['sip_from_user_setting_manual'];
			}

			//SIP_FROM_DISPLAY_SETTING
			if (!isset($_POST['def_sip_from_display_setting'])){
				if (isset($_POST['sip_from_display_setting']) && $_POST['sip_from_display_setting']!='manual') $config.=",sip_from_display_setting=".$_POST['sip_from_display_setting'];
				else if (isset($_POST['sip_from_display_setting']) && $_POST['sip_from_display_setting']=='manual') $config.=",sip_from_display_setting=".$_POST['sip_from_display_setting_manual'];
			}


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

		//Wenn der Configstring mit einem "," anfängt wird dieses gelöscht
		if (stripos($config,",")==0 && stripos($config,",")!==false)
		$config=substr_replace($config,'',0,1);
		return $config;

	}

	$page=$_POST['page'];
	$dir=$_POST['dir'];
	$search=$_POST['search'];
	//Wenn einen Eintrag bearbeitet wurde
	if ($_POST['id_upd']) {
			$id=$_POST['id_upd'];
			$config=parameters($config,$_POST['from'],$_POST['to']);
			$query_new = $ba->update("UPDATE dialplan SET config='$config' WHERE id='$id'");

	}


		$query = $ba->select("select option from activate");
		while ($entry = $ba->fetch_array($query)) {
			if ($entry['option'] <= 1)
				$query = $ba->update("UPDATE activate SET option=1");
		}
		if (!$ba->is_error()) {
			echo "<script>window.opener.location='../index.php?m=dialplan&start=" . $page . "&dir=" . $dir . "&search=" . $search . "';</script>";
			echo "<script>this.window.close();</script>";
		} else {
			echo "<script>alert(\"" . $ba->error() . "\");</script>";
		}
	}

?>
