<?php
@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();
	require_once("./includes/lang.php");

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

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



			//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['wait_for_cancel'])){
				$config.=",wait_for_cancel=1";
			}elseif(!isset($_POST['wait_for_cancel'])){
				$config.=",wait_for_cancel=0";
			}


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

			//S2I CPT
			if (isset($_POST['s2i_cpt'])) $config.=",s2i_cpt=".$_POST['s2i_cpt'];
			//I2S CPT
			if (isset($_POST['i2s_cpt'])) $config.=",i2s_cpt=".$_POST['i2s_cpt'];

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

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

			//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['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['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'];

			//CODECS
			$c="";
			for ($i=0;$i<count($_POST['codecs']);$i++){
				if ($c!="") $c.=":";
				$c.= $_POST['codecs'][$i];
			}
			if ($c!="")	$config.=",codecs=".$c;


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

			return $config;
	}

	if ($_POST['id_upd']){
		if ($ba->is_error()){
			die($ba->error());
		}
		if (strtolower($_POST['name'])=="general"){
			echo "<script>alert('".SIP_INVALID_NAME_DIS."'); window.history.back();</script>";
					return;
		}
		$query_comp=$ba->select("SELECT * FROM sip");
		while ($entry = $ba->fetch_array($query_comp)) {
			if ($_POST['id_upd']!=$entry['id']){
				if ($_POST['name']==$entry['name']){
					echo "<script>alert('".SIP_INVALID_NAME_DIS."'); window.history.back(); </script>";
					return;
				}
			}
		}
		$name=$_POST['name'];
		$ser_addr=$_POST['server_address'];
		$user=$_POST['user'];
		$secret=$_POST['secret'];
		$type=$_POST['register'];
		$stunserver=$_POST['stunserver'];
		if ($type==1) {
			$regrefresh=$_POST['regrefresh'];
			$register_option=$_POST['register_option'];
			$hard_reregister=$_POST['hard_reregister'];
		}else{
			$regrefresh="";
			$register_option="validate";
			$hard_reregister="0";
		}
		$externip=$_POST['externip'];
		$config=parameters();
		$id=$_POST['id_upd'];
		$query_new = $ba->update("UPDATE sip SET name='$name', server_address='$ser_addr',
						 user='$user', secret='$secret', type='$type', regrefresh='$regrefresh',
						 register_option='$register_option', config='$config', hard_reregister='$hard_reregister',
						 externip='$externip' WHERE id='$id'");
		if (!$ba->is_error()){
			$query=$ba->update("UPDATE dialplan SET fromId='p:".$name."'
						where fromDirection='sip' and fromId='p:".$_POST['alt_name']."'");
			$query=$ba->update("UPDATE dialplan SET toId='p:".$name."'
						where toDirection='sip' and toId='p:".$_POST['alt_name']."'");
			$query=$ba->update("UPDATE dialplan SET fromId='d:".$name."'
						where fromDirection='sip' and fromId='d:".$_POST['alt_name']."'");
		}
		$query=$ba->select("select option from activate where id='activate'");
		while($entry = $ba->fetch_array($query)){
			if ($entry['option'] <= 1) $query =  $ba->update("UPDATE activate SET option=1 where id='activate'");
		}
		if (!$ba->is_error()){
			echo "<script>window.opener.location='../index.php?m=sip'</script>";
			echo "<script>this.window.close();</script>";

		}else{
			echo "<script>alert('".$ba->error()."');</script>";
		}
	}else{
		if (($_POST['name'])&&($_POST['server_address']))
		{
			if ($ba->is_error()){
				die($ba->error());
			}
			if (strtolower($_POST['name'])=="general"){
				echo "<script>alert('".SIP_INVALID_NAME_DIS."'); window.history.back();</script>";
				return;
			}
			$query_comp=$ba->select("SELECT name FROM sip");
			while ($entry = $ba->fetch_array($query_comp)) {
				if ($_POST['name']==$entry['name']){
					echo "<script>alert('".SIP_INVALID_NAME_DIS."'); window.history.back(); </script>";
					return;
				}
			}
			$name=$_POST['name'];
			$ser_addr=$_POST['server_address'];
			$user=$_POST['user'];
			$secret=$_POST['secret'];
			$type=$_POST['register'];
			if ($type==1){
				$regrefresh=$_POST['regrefresh'];
				$register_option=$_POST['register_option'];
				$hard_reregister=$_POST['hard_reregister'];
			}else{
				$regrefresh="";
				$register_option="validate";
				$hard_reregister="0";
			}
			$stunserver=$_POST['stunserver'];
			$externip=$_POST['externip'];
			$config=parameters();
			$query_new = $ba->insert_("INSERT INTO sip (name, server_address, user, secret, type,  config,externip,regrefresh,register_option,hard_reregister)
									VALUES ('$name','$ser_addr', '$user', '$secret', '$type','$config','$externip','$regrefresh','$register_option','$hard_reregister')");
			$query=$ba->select("select option from activate where id='activate'");
			while($entry = $ba->fetch_array($query)){
				if ($entry['option'] <= 1) $query =  $ba->update("UPDATE activate SET option=1 where id='activate'");
			}
			if (!$ba->is_error()){
				echo "<script>window.opener.location='../index.php?m=sip&start=" . $page . "'</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>";
		}
	}
}

?>
