<?php
chdir("..");
chdir("..");

include "./includes/variables.php";
include "./misc/database.php";
$ip = exec('/sbin/ifconfig eth0 | sed -ne "s/.*inet addr:\(.*\) Bcast.*/\1/p"');
define ("ip_address", $ip);
if (show=="yes"){
	$ba= new beroAri();

	if (isset($_POST['gr_upd'])){
		$name=$_POST['name'];
		$ser_addr=$_POST['ip'];
		$user=$_POST['name'];
		$secret=$_POST['name'];
		$type="proxy";
		$query_new = $ba->update("UPDATE sip SET name='$name', server_address='$ser_addr', user='$user', secret='$secret', type='$type', def='0' WHERE name='".$_POST['gr_upd']."-".ip_address.""."'");

		$direction="sip-isdn";
		$fromid=$_POST['name'];
		$toid=$_POST['name'];
		$dest=$_POST['name']."(.*)";
		$newdest="\\\\1";
		$src="(.*)";
		$newsrc="\\\\1";
		$qu = $ba->update("UPDATE dialplan SET direction='$direction', fromId='$fromid', toId='$toid', dest='$dest', newDest='$newdest', src='$src', newSrc='$newsrc', config='' WHERE fromId='".$_POST['gr_upd']."-".ip_address.""."' and direction='$direction'");

		$direction="isdn-sip";
		$fromid=$_POST['name'];
		$toid=$_POST['name'];
		$dest="(.*)";
		$newdest=$_POST['name']."\\\\1";
		$src="(.*)";
		$newsrc="\\\\1";
		$qu = $ba->update("UPDATE dialplan SET direction='$direction', fromId='$fromid', toId='$toid', dest='$dest', newDest='$newdest', src='$src', newSrc='$newsrc', config='' WHERE fromId='".$_POST['gr_upd']."-".ip_address.""."' and direction='$direction'");

	}else{
		$name=$_POST['name'];
		$ser_addr=$_POST['ip'];
		$user=$_POST['name'];
		$secret=$_POST['name'];
		$type="proxy";
		$query_new = $ba->insert_("INSERT INTO sip (name, server_address, user, secret, type, def) VALUES ('$name','$ser_addr', '$user', '$secret', '$type','$default')");

		$direction="sip-isdn";
		$fromid=$_POST['name'];
		$toid=$_POST['name'];
		$dest=$_POST['name']."(.*)";
		$newdest="\\\\1";
		$src="(.*)";
		$newsrc="\\\\1";
		$query_new = $ba->insert_("INSERT INTO dialplan (direction, fromId, toId, dest, newDest, src, newSrc, config) VALUES ('$direction','$fromid','$toid', '$dest', '$newdest', '$src', '$newsrc', '')");

		$direction="isdn-sip";
		$fromid=$_POST['name'];
		$toid=$_POST['name'];
		$dest="(.*)";
		$newdest=$_POST['name']."\\\\1";
		$src="(.*)";
		$newsrc="\\\\1";
		$query_new = $ba->insert_("INSERT INTO dialplan (direction, fromId, toId, dest, newDest, src, newSrc, config) VALUES ('$direction','$fromid','$toid', '$dest', '$newdest', '$src', '$newsrc', '')");
	}

}else{
	echo "<h2>Access Denied</h2>";
}
?>
