<?php
//Einträge der Datenbank löschen
@session_start();
if((isset($_SESSION['beroari_time']))&&(($_SESSION['beroari_time']+1200)<time())){
	@session_unset();
	@session_destroy();
	echo "<script>window.opener.location='../index.php?m=sip'</script>";
	echo "<script>this.window.close();</script>";
}else{
	$_SESSION['beroari_time']=time();


	chdir("..");
	include "./includes/variables.php";
	include './misc/database.php';

	$ba = new beroAri();

	if ($ba->is_error()){
		die($ba->error());
	}
	$id=$_GET['id'];
	$table=$_GET['table'];
	if (isset($_POST['gr']) || isset($_POST['ip'])){
		$query = $ba->delete("DELETE FROM sip WHERE name='".$_POST['gr']."-".$_POST['ip']."'");
		$query = $ba->select("SELECT isdnid FROM dialplan WHERE fromId='".$_POST['gr']."-".$_POST['ip']."'");
		while($entry = $ba->fetch_array($query)){
			if ($entry['isdnid']!="") $query = $ba->delete("DELETE FROM dialplan WHERE isdnid='".$entry['isdnid']."'");
		}
		$query = $ba->delete("DELETE FROM dialplan WHERE fromId='".$_POST['gr']."-".$_POST['ip']."'");
		
	}

	if ($table=="isdn"){
		$query = $ba->update("UPDATE isdn SET gr='NULL', config='' WHERE gr='".$_GET['gr']."'");	
		$q = $ba->select("SELECT isdnid FROM dialplan WHERE fromId='g:".$_GET['gr']."' or toId='g:".$_GET['gr']."'");
		while($entry = $ba->fetch_array($q)){
			if ($entry['isdnid']!="") $query = $ba->delete("DELETE FROM dialplan WHERE isdnid='".$entry['isdnid']."'");
		}
		$query = $ba->delete("DELETE FROM dialplan where (fromId='g:".$_GET['gr']."' or toId='g:".$_GET['gr']."') and (fromDirection='isdn' or toDirection='isdn')");
	}elseif ($table=="analog"){
		$query = $ba->update("UPDATE analog SET gr='NULL', config='' WHERE gr='".$_GET['gr']."'");	
		$q = $ba->select("SELECT isdnid FROM dialplan WHERE fromId='g:".$_GET['gr']."' or toId='g:".$_GET['gr']."'");
		while($entry = $ba->fetch_array($q)){
			if ($entry['isdnid']!="") $query = $ba->delete("DELETE FROM dialplan WHERE isdnid='".$entry['isdnid']."'");
		}
		$query = $ba->delete("DELETE FROM dialplan where (fromId='g:".$_GET['gr']."' or toId='g:".$_GET['gr']."') and (fromDirection='analog' or toDirection='analog')");
	}elseif ($table=="gsm"){
		$query = $ba->update("UPDATE gsm SET gr='NULL', config='' WHERE gr='".$_GET['gr']."'");	
		$q = $ba->select("SELECT isdnid FROM dialplan WHERE fromId='g:".$_GET['gr']."' or toId='g:".$_GET['gr']."'");
		while($entry = $ba->fetch_array($q)){
			if ($entry['isdnid']!="") $query = $ba->delete("DELETE FROM dialplan WHERE isdnid='".$entry['isdnid']."'");
		}
		$query = $ba->delete("DELETE FROM dialplan where (fromId='g:".$_GET['gr']."' or toId='g:".$_GET['gr']."') and (fromDirection='gsm' or toDirection='gsm')");
	}elseif ($table=="sip"){
		$query = $ba->delete("DELETE FROM dialplan where fromId='p:".$_GET['name']."' or toId='p:".$_GET['name']."' or toId='d:".$_GET['name']."' or fromId='d:".$_GET['name']."'");
		$query = $ba->delete("DELETE FROM ".$table." WHERE id='$id'");
	}elseif ($table=="callprogress"){
		$query = $ba->delete("DELETE FROM callprogress_entries where id_name='$id';");
		$query = $ba->delete("DELETE FROM ".$table." WHERE id='$id'");
	}elseif ($table=="dialplan" && isset($_GET['isdnid'])){
		$query = $ba->delete("DELETE from dialplan WHERE isdnid='".$_GET['isdnid']."'");
	}else{
		$query = $ba->delete("DELETE FROM ".$table." WHERE id='$id'");
	}
	if ($table=="isdn" || $table=="firewall" || $table=="analog" || $table=="gsm" || $table=="isgw_causes"){
		$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 (isset($_GET['table'])){
		$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()){
		if ($table=="dialplan")
		{
			$start=$_GET['start'];
			echo "<script>window.location='../index.php?m=".$_GET['table']. "&start=" . $start . "';</script>";
		}elseif ($table=="isdn"){
			echo "<script>window.location='../index.php?m=".$_GET['opt']."';</script>";
		}elseif ($table=="callprogress_entries"){
			echo "<script>window.location='../index.php?m=callprogress';</script>";
		}elseif ($table=="isgw_causes"){
			echo "<script>window.location='../index.php?m=causes';</script>";
		}elseif ($table=="analog"){
			echo "<script>window.location='../index.php?m=".$_GET['opt']."';</script>";
		}else{
			echo "<script>window.location='../index.php?m=".$_GET['table']."';</script>";
		}
	}else{
		echo "<script>alert(".$ba->error().");</script>";
	}
}

