<?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');
	//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>";

	

	$page=$_POST['start'];
	$dir=$_POST['dir'];
	$search=$_POST['search'];
	//Wenn einen Eintrag bearbeitet wurde
	if ($_POST['id_upd']) {
		
		//Wenn es richtung sip zu pstn geht
		if ($_POST['fromDirection'] == 'sip') {
			$fromDirection=$_POST['fromDirection'];
			$toDirection=$_POST['toDirection'];
			$id=$_POST['id_upd'];
			$fromid=$_POST['fromIdSip'];
			$match_type=$_POST['matchType'];
			$fromManual=trim($_POST['manualsipfrom']);
			if ($toDirection=="isdn") $toid=$_POST['toIsdn'];
			if ($toDirection=="analog") $toid=$_POST['toAnalog'];
			if ($toDirection=="gsm") $toid=$_POST['toGsm'];
			$dest=trim($_POST['dest']);
			$newdest=trim($_POST['newDest']);
			$src=trim($_POST['src']);
			$newsrc=trim($_POST['newSrc']);
			$comment=trim($_POST['comment']);
			(isset($_POST['activ'])) ? $activ=1 : $activ=0;
			
			if ($match_type!="manual") $fromid=$match_type.$fromid;
			
			//Wenn manual ausgewählt ist, ist from ID ein Pflichtfeld
			if ($match_type=="manual" && $fromManual==""){
				echo "<script>alert('From ID can not be empty!'); window.history.back();</script>";
					return;
			}
			if ($match_type!="manual"){
				$query_new = $ba->update("UPDATE dialplan SET fromDirection='$fromDirection', toDirection='$toDirection', fromId='$fromid', toId='$toid', dest='$dest', newDest='$newdest', src='$src', newSrc='$newsrc', comment='$comment', activ='$activ', isdnid='' WHERE id='$id'");
			}else{
				$query_new = $ba->update("UPDATE dialplan SET fromDirection='$fromDirection', toDirection='$toDirection', fromId='$fromManual', toId='$toid', dest='$dest', newDest='$newdest', src='$src', newSrc='$newsrc', comment='$comment', activ='$activ', isdnid='' WHERE id='$id'");
			}
		}
		
		//Wenn es richtung pstn zu sip geht
		if (($_POST['fromDirection'] == 'isdn' || $_POST['fromDirection'] == 'analog' || $_POST['fromDirection'] == 'gsm') && $_POST['toDirection'] == 'sip') {
			$fromDirection=$_POST['fromDirection'];
			$toDirection=$_POST['toDirection'];
			$id=$_POST['id_upd'];
			if ($fromDirection=="isdn") $fromid=$_POST['fromIdIsdn'];
			if ($fromDirection=="analog") $fromid=$_POST['fromIdAnalog'];
			if ($fromDirection=="gsm") $fromid=$_POST['fromIdGsm'];
			$toid=$_POST['toSip'];	
			
			$toManual=trim($_POST['manualsip']);
			$dest=trim($_POST['dest']);
			$newdest=trim($_POST['newDest']);
			$src=trim($_POST['src']);
			$newsrc=trim($_POST['newSrc']);
			$comment=trim($_POST['comment']);
			(isset($_POST['activ'])) ? $activ=1 : $activ=0;
			
			//Wenn Manual ausgewählt ist, musst To ID ausgefüllt sein
			if ($toid=="manual" && $toManual==""){
				echo "<script>alert('To ID can not be empty!'); window.history.back();</script>";
					return;
			}
			
			if ($toid!="manual"){
				$qu = $ba->update("UPDATE dialplan SET fromDirection='$fromDirection', toDirection='$toDirection', fromId='$fromid', toId='$toid', dest='$dest', newDest='$newdest', src='$src', newSrc='$newsrc', comment='$comment', activ='$activ', isdnid='' WHERE id='$id'");
			}else{
				$q= $ba->update("UPDATE dialplan SET fromDirection='$fromDirection', toDirection='$toDirection', fromId='$fromid', toId='$toManual', dest='$dest', newDest='$newdest', src='$src', newSrc='$newsrc', comment='$comment', activ='$activ', isdnid='' WHERE id='$id'");
			}
		}
		
		//Wenn es richtung pstn zu pstn geht
		if (($_POST['fromDirection'] == 'isdn' || $_POST['fromDirection'] == 'analog' || $_POST['fromDirection'] == 'gsm') && ($_POST['toDirection'] == 'isdn' || $_POST['toDirection'] == 'analog' || $_POST['toDirection'] == 'gsm')) {
			$fromDirection=$_POST['fromDirection'];
			$toDirection=$_POST['toDirection'];
			$id=$_POST['id_upd'];
			if ($fromDirection=="isdn") $fromid=$_POST['fromIdIsdn'];
			if ($fromDirection=="analog") $fromid=$_POST['fromIdAnalog'];
			if ($fromDirection=="gsm") $fromid=$_POST['fromIdGsm'];
			if ($toDirection=="isdn") $toid=$_POST['toIsdn'];
			if ($toDirection=="analog") $toid=$_POST['toAnalog'];		
			if ($toDirection=="gsm") $toid=$_POST['toGsm'];		
			$dest=trim($_POST['dest']);
			$newdest=trim($_POST['newDest']);
			$src=trim($_POST['src']);
			$newsrc=trim($_POST['newSrc']);
			$comment=trim($_POST['comment']);
			(isset($_POST['activ'])) ? $activ=1 : $activ=0;

			if ($_POST['isdnid']!=""){
				$qu=$ba->update("UPDATE dialplan SET fromDirection='$fromDirection', toDirection='$toDirection', fromId='$fromid', toId='$toid', dest='$dest', newDest='".$newdest."', src='$src', newSrc='$newsrc', comment='$comment', activ='$activ'  WHERE isdnid='".$_POST['isdnid']."'");
			}else{
				//Unique id erzeuge als isdnid
				$isdnid=uniqid("ISDNZUISDN");
				$qu=$ba->update("UPDATE dialplan SET fromDirection='$fromDirection', toDirection='$toDirection', fromId='$fromid', toId='$toid', dest='$dest', newDest='".$newdest."', src='$src', newSrc='$newsrc', isdnid='$isdnid', comment='$comment', activ='$activ'  WHERE id='".$id."'");
			}
		}
		$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");
		}
		
		if (!$ba->is_error()) {
			$page=$_POST['start'];
			$dir=$_POST['dir'];
			$search=$_POST['search'];
			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>";
		}
	
	//Einen neuen dialplan Eintrag erzeugen
	} else {
		$q = $ba->select("select max(posId) from dialplan");
		$posId=sqlite_fetch_single($q)+1;
		
		//SIP zu PSTN
		if ($_POST['fromDirection'] == 'sip') {
			$config=$_POST['config'];
			$fromDirection=$_POST['fromDirection'];
			$toDirection=$_POST['toDirection'];
			$fromid=$_POST['fromIdSip'];
			$match_type=$_POST['matchType'];
			$fromManual=trim($_POST['manualsipfrom']);
			if ($toDirection=="isdn") $toid=$_POST['toIsdn'];
			if ($toDirection=="analog") $toid=$_POST['toAnalog'];	
			if ($toDirection=="gsm") $toid=$_POST['toGsm'];	
			$dest=trim($_POST['dest']);
			$newdest=trim($_POST['newDest']);
			$src=trim($_POST['src']);
			$newsrc=trim($_POST['newSrc']);
			$comment=trim($_POST['comment']);
			(isset($_POST['activ'])) ? $activ=1 : $activ=0;
			if ($fromid=="manual" && $fromManual=="")  $fromManual="(.*)";
			if ($dest=="") $dest="(.*)";
			if ($newdest=="") $newdest="\\\\1";
			if ($src=="") $src="(.*)";
			if ($newsrc=="") $newsrc="\\\\1";
			if ($match_type!="manual") $fromid=$match_type.$fromid;
			
			//Wenn manual ausgewählt ist, ist from ID ein Pflichtfeld
			if ($match_type=="manual" && $fromManual==""){
				echo "<script>alert('From ID can not be empty!'); window.history.back();</script>";
					return;
			}

			if ($match_type!="manual"){
				$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc, posId, comment, config, activ) VALUES ('$fromDirection','$toDirection','$fromid','$toid', '$dest', '$newdest', '$src', '$newsrc',  '$posId', '$comment', '$config', '$activ')");
			}else{
				$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc, posId, comment, config) VALUES ('$fromDirection','$toDirection','$fromManual','$toid', '$dest', '$newdest', '$src', '$newsrc', '$posId', '$comment', '$config')");
			}
		}
		//PSTN zu SIP
		if (($_POST['fromDirection'] == 'isdn' || $_POST['fromDirection'] == 'analog' || $_POST['fromDirection'] == 'gsm') && $_POST['toDirection'] == 'sip') {
			$config=$_POST['config'];
			$fromDirection=$_POST['fromDirection'];
			$toDirection=$_POST['toDirection'];
			if ($fromDirection=="isdn") $fromid=$_POST['fromIdIsdn'];
			if ($fromDirection=="analog") $fromid=$_POST['fromIdAnalog'];
			if ($fromDirection=="gsm") $fromid=$_POST['fromIdGsm'];
			$toid=$_POST['toSip'];	
			$toManual=trim($_POST['manualsip']);
			$dest=trim($_POST['dest']);
			$newdest=trim($_POST['newDest']);
			$src=trim($_POST['src']);
			$newsrc=trim($_POST['newSrc']);
			$comment=trim($_POST['comment']);
			(isset($_POST['activ'])) ? $activ=1 : $activ=0;
			if ($dest=="") $dest="(.*)";
			if ($newdest=="") $newdest="\\\\1";
			if ($src=="") $src="(.*)";
			if ($newsrc=="") $newsrc="\\\\1";
			
			//Wenn Manual ausgewählt ist, musst To ID ausgefüllt sein
			if ($toid=="manual" && $toManual==""){
				echo "<script>alert('To ID can not be empty!'); window.history.back(); </script>";
					return;
			}
			
			if ($toid!="manual"){
				$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc, posId, comment, config, activ) VALUES ('$fromDirection','$toDirection','$fromid','$toid', '$dest', '$newdest', '$src', '$newsrc', '$posId', '$comment', '$config', '$activ')");
			}else{
				$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc, posId, comment, config, activ) VALUES ('$fromDirection','$toDirection','$fromid','$toManual', '$dest', '$newdest', '$src', '$newsrc', '$posId', '$comment', '$config', '$activ')");

			}
		}
		//PSTN zu PSTN
		if (($_POST['fromDirection'] == 'isdn' || $_POST['fromDirection'] == 'analog' || $_POST['fromDirection'] == 'gsm') && ($_POST['toDirection'] == 'isdn' || $_POST['toDirection'] == 'analog' || $_POST['toDirection'] == 'gsm')) {
			$config=$_POST['config'];
			$fromDirection=$_POST['fromDirection'];
			$toDirection=$_POST['toDirection'];
			if ($fromDirection=="isdn") $fromid=$_POST['fromIdIsdn'];
			if ($fromDirection=="analog") $fromid=$_POST['fromIdAnalog'];
			if ($fromDirection=="gsm") $fromid=$_POST['fromIdGsm'];
			if ($toDirection=="isdn") $toid=$_POST['toIsdn'];
			if ($toDirection=="analog") $toid=$_POST['toAnalog'];
			if ($toDirection=="gsm") $toid=$_POST['toGsm'];
			if ($fromDirection=="isdn" && $toDirection=="isdn") $config.="ie_on_sip=1,ec=0,t38=0,unknownprefix=,internationalprefix=,nationalprefix=,privateprefix=,localprefix=";		
			$dest=trim($_POST['dest']);
			$newdest=trim($_POST['newDest']);
			$src=trim($_POST['src']);
			$newsrc=trim($_POST['newSrc']);
			$comment=trim($_POST['comment']);
			(isset($_POST['activ'])) ? $activ=1 : $activ=0;
			if ($dest=="") $dest="(.*)";
			if ($newdest=="") $newdest="\\\\1";
			if ($src=="") $src="(.*)";
			if ($newsrc=="") $newsrc="\\\\1";

			$isdnid=uniqid("ISDNZUISDN");
			$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId, comment, config, activ) VALUES ('$fromDirection', '$toDirection', '$fromid', '$toid', '$dest', '".$newdest."', '$src', '".$newsrc."', '$isdnid','$posId', '$comment', '$config', '$activ')");

		}
		
		$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 (isset($_POST['first']) && !$ba->is_error()){
			$q = $ba->select("select max(id) from dialplan");
			$id=sqlite_fetch_single($q);
			echo "<script>window.location='./change_pos.php?posid=1&id=".$id."&first=1';</script>";
		}
		if (!$ba->is_error()) {
			$page=$_POST['start'];
			$dir=$_POST['dir'];
			$search=$_POST['search'];
			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>";
		}
	}
}
?>
