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


	if ($_POST['id_upd']){

		//$direction=$_POST['call_direction'];
		$id=$_POST['id_upd'];
		$name=$_POST['name'];
		$query_comp=$ba->select("SELECT * FROM callprogress");
		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;
				}
			}
		}
		$query_new = $ba->update("UPDATE callprogress SET name='$name' 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 where id='activate'");
		}
		if (!$ba->is_error()){
			echo "<script>window.opener.location='../index.php?m=callprogress'</script>";
			echo "<script>this.window.close();</script>";

		}else{
			echo "<script>alert(".$ba->error().");</script>";
		}
	}else{
		$direction=$_POST['call_direction'];
		$name=$_POST['name'];
		$query_comp=$ba->select("SELECT * FROM callprogress");
		while ($entry = $ba->fetch_array($query_comp)) {
			if ($_POST['name']==$entry['name']){
				echo "<script>alert('".SIP_INVALID_NAME_DIS."'); window.history.back(); </script>";
				return;
			}
		}
		$query_new = $ba->insert_("INSERT INTO callprogress (direction, name)
								VALUES ('$direction','$name')");
		$id = $ba->rowid();
		if ($_POST['callforward_rules']==1 && $direction=="SIP-ISDN"){
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, sip_resp, resp_text, isdn_event, chann_det, id_name)
								VALUES ('te','none','','PROCEEDING','any_state','$id')");
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, sip_resp, resp_text, isdn_event, chann_det, id_name)
								VALUES ('te','none','','PROGRESS','any_state','$id')");
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, sip_resp, resp_text, isdn_event, chann_det, id_name)
								VALUES ('te','183','Ringing','ALERTING','any_state','$id')");
		}
		if ($_POST['ea_outbound_rules']==1 && $direction=="SIP-ISDN"){
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, sip_resp, resp_text, isdn_event, chann_det, id_name)
								VALUES ('nt','183_SDP','Progress','PROGRESS','any_state','$id')");
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, sip_resp, resp_text, isdn_event, chann_det, id_name)
								VALUES ('nt','183_SDP','Proceeding','PROCEEDING','any_state','$id')");
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, sip_resp, resp_text, isdn_event, chann_det, id_name)
								VALUES ('nt','183_SDP','Alerting','ALERTING','any_state','$id')");
		}
		if ($_POST['183_to_alerting_rules']==1 && $direction=="ISDN-SIP"){
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, isdn_event, resp_text, sip_resp, chann_det, id_name)
								VALUES ('te','183','*','ALERTING','','$id')");
			$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, isdn_event, resp_text, sip_resp, chann_det, id_name)
								VALUES ('te','183_SDP','*','ALERTING','','$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 where id='activate'");
			}
			if (!$ba->is_error()){
				echo "<script>window.opener.location='../index.php?m=callprogress'</script>";
				echo "<script>this.window.close();</script>";

			}else{
				echo "<script>alert(".$ba->error().");</script>";
			}

	}
}


?>
