<?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]);
		}


			//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 gsm");
		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 gsm SET gr='NULL', config='' WHERE gr='".$_POST['gr_upd']."'");
		$gr=$_POST['group'];
		$sms_extension=$_POST['sms_extension'];
		$extension=$_POST['extension'];
		$ports=$_POST['ports'];
		$new_ports=$_POST['new_ports'];

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

		for ($i=0;$i<count($ports);$i++){
			$port = $ports[$i];
			$query_new = $ba->update("UPDATE gsm SET gr='$gr', config='$config', sms_extension='$sms_extension', extension='$extension' 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='', sms_extension='', extension='' WHERE port='$new_port'");
		}
		if (!$ba->is_error()){
			$query=$ba->update("UPDATE dialplan SET fromId='g:".$gr."' where fromDirection='gsm' and fromId='g:".$_POST['gr_upd']."'");
			$query=$ba->update("UPDATE dialplan SET toId='g:".$gr."' where toDirection='gsm' 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=gsm'</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 gsm");
			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'];
			$sms_extension=$_POST['sms_extension'];
			$extension=$_POST['extension'];
			$ports=$_POST['ports'];
			$new_ports=$_POST['new_ports'];
			$port="";
			$config=parameters();

			for ($i=0;$i<count($ports);$i++){
				$port = $ports[$i];
				$query_new = $ba->update("UPDATE gsm SET gr='$gr', config='$config', sms_extension='$sms_extension', extension='$extension' WHERE port='$port'");
			}
			$new_port="";
			for ($i=0;$i<count($new_ports);$i++){
				$new_port= $new_ports[$i];
				$query_new = $ba->update("UPDATE gsm SET gr='NULL', config='', sms_extension='', extension=''  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=gsm'</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>";
		}
	}
}


?>
