<?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']){

		$id=$_POST['id_upd'];
		$type=$_POST['type'];
		$resp=$_POST['resp'];
		if (isset($_POST['sdp']) && $resp!="none") $resp=$resp."_SDP";
		$resp_text=$_POST['resp_text'];
		$chann_det=$_POST['chann_det'];
		$events=$_POST['events'];
		$event="";
		if (count($events)==0){
			echo "<script>alert('".CALL_PROGRESS_ENT_EVENT_MESS."'); window.history.back(); </script>";
			return;
		}
		for ($i=count($events)-1;$i>=0;$i--){
			if ($event=="") {
				$event.=$events[$i];
				if (isset($_POST[$events[$i]."_sdp"])) $event.="_SDP";
			}else{
				$event.=":".$events[$i];
				if (isset($_POST[$events[$i]."_sdp"])) $event.="_SDP";
			}
		}

		$query_new = $ba->update("UPDATE callprogress_entries SET type='$type', sip_resp='$resp', resp_text='$resp_text', isdn_event='$event',chann_det='$chann_det' 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{
		$id_name=$_POST['id_name'];
		$type=$_POST['type'];
		$resp=$_POST['resp'];
		if (isset($_POST['sdp']) && $resp!="none") $resp=$resp."_SDP";
		$resp_text=$_POST['resp_text'];
		$chann_det=$_POST['chann_det'];
		$events=$_POST['events'];
		$event="";
		if (count($events)==0){
			echo "<script>alert('".CALL_PROGRESS_ENT_EVENT_MESS."'); window.history.back(); </script>";
			return;
		}
		for ($i=count($events)-1;$i>=0;$i--){
			if ($event=="") {
				$event.=$events[$i];
				if (isset($_POST[$events[$i]."_sdp"])) $event.="_SDP";
			}else{
				$event.=":".$events[$i];
				if (isset($_POST[$events[$i]."_sdp"])) $event.="_SDP";
			}
		}
		$query_new = $ba->insert_("INSERT INTO callprogress_entries (type, sip_resp, resp_text, isdn_event, chann_det, id_name)
								VALUES ('$type','$resp','$resp_text','$event','$chann_det','$id_name')");
			$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>";
			}

	}
}


?>
