<?php

$ServerPassword = "oyster";


$fcid = $_GET['fcid'];                   //For example, "Gander A"
$pass = strtoupper(trim($_GET['pass'])); //Server password
$auth = $_GET['auth'];                   //Authorization
$data = $_GET['data'];                   //handoff data message
$user = $_GET['user'];                   //For example, "Garen Evans"
$ufds = $_GET['ufds'];                   //User Flight Data String


$logstr = sprintf("FCID:%s  USER:%s   AUTH:%s   PASS:%s",$fcid,$user,$auth,$pass);
logthis($logstr);

$fdat = file("atc.dat");

//housekeeping...clear timed-out connections
$newdat = $fdat;
$n = count($newdat);

for($i = 0; $i < $n; $i++){

	$sar = explode("||",$newdat[$i]);
	$difsec = time() - $sar[1];

	if($difsec > 120){ //two minutes!

		unset($newdat[$i]); //removes element but leaves an index hole...fix below

	}

}
$fdat= array_values($newdat); //reassign indices to remove hole, then assign the whole shebang to $fdat

$flag = "";
foreach($fdat as $s){
	if($s != ""){
		$sar = explode("||",$s);
		if($sar[0] == $fcid){
			$flag = $sar[2];  //auth code
			break;
		}
	}
}

if($flag != ""){  //user is attempting to RE-connect (check auth)

	if($auth == $flag){
		//update atc.dat with new timestamp 
		update_atcdata($fdat,$fcid,$auth);
		$ds = "AUTH||".$auth;
	}else{
		echo "ERROR||Facility ID (".$fcid.") is in use.";
		die();
	}

}else{  //user is attempting to connect for first time.

	if($pass == "OYSTER"){
		//create unique auth, update atc.dat file, 
		$auth = hash("crc32",$fcid.time());
		add_atcdata($fcid,$auth,$user);
		$ds = "AUTH||".$auth;
	}else{
		echo "ERROR||Invalid Password (".$pass.")";
		die();
	}
}
$ds = $ds."\n"."LIST||".list_atcdata($fdat)."CZQX-TEST||\n";

//collect handoff data
$hodar = get_handoffs($fcid);
if($hodar !== false){
	foreach($hodar as $str){
		$ds = $ds.$str."\n";
	}
}

printf("%s\n",$ds);


logthis($ufds);

logufds($fcid,$ufds);



exit;


//============================================================================== functions

function logufds($facility, $ufdata){

	$epoch = time();

	$fname = "ufds.txt";
	$fptr = fopen($fname,"a");
	
	
	$ufdrows = explode("::",$ufdata);
	$n = count($ufdrows);
	for($i = 0; $i < $n; $i++){
		if(trim($ufdrows[$i]) != ""){
			fprintf($fptr,"%s||%s||%s\n",$epoch,$facility,$ufdrows[$i]);
		}
	}

	fclose($fptr);
}


function logthis($message){
	$fname = "log.txt";
	$fptr = fopen($fname,"a");
	fprintf($fptr,"%s\n",$message);
	fclose($fptr);
}


function add_atcdata($fcid,$auth,$user){

	$row = sprintf("%s||%s||%s||%s",$fcid,time(),$auth,$user);
	

	$fname = "atc.dat";
	$fptr = fopen($fname,"a");
	fprintf($fptr,"%s\n",$row);
	fclose($fptr);
	//echo "<hr />R2=".$row."<hr />";
}


function update_atcdata($fdat,$fcid,$auth){

	$tm = time();


	$flag = 0;
	$n = count($fdat);
	for($i = 0; $i < $n; $i++){
		$sar = explode("||",$fdat[$i]);
		if($sar[0] == $fcid){
			$sar[1] = time();
			$sar[2] = $auth;
			$fdat[$i] = implode("||",$sar);
			$flag = 1;
			break;
		}

	}
	
	if($flag == 1){
		if(unlink("atc.dat")){
			$fptr = fopen("atc.dat","w");
			foreach($fdat as $row){
				if(strlen($row) > 10){
					fprintf($fptr,"%s",$row);
				}
			}
			fclose($fptr);
		}else{
			die("ERROR: unlink false");
		}
	}

}


function list_atcdata($fdat){

	$flist = "";
	foreach($fdat as $row){
		$sar = explode("||",$row);
		$flist .= $sar[0]."||";
	}
	return $flist;
}



function get_handoffs($fcid){


	$fdat = file("handoff.txt");
	$n = count($fdat);
	for($i = 0; $i < $n; $i++){
		$sar = explode("||",$fdat[$i]);
		if($sar[2] == $fcid){
			$har[] = $fdat[$i];
			$fdat[$i] = "";   //clears the entry
		}
	}

	//Update the handoff database
	unlink("handoff.txt");
	$fname = "handoff.txt";
	$fptr = fopen($fname,"w");
	for($i = 0; $i < $n; $i++){
		if($fdat[$i] != ""){
			fprintf($fptr,"%s",$fdat[$i]);
		}
	}
	fclose($fptr);

	//Return the handoffs, or false.	
	if(count($har) > 0){
		return $har;
	}else{
		return false;
	}
	
	



}


?>

