Here is the whole code.
<?php
// MySQL database connection (a sample file is included)
@define ("MYSQL_CONNECT_INCLUDE", "../includes/connect_db.php");
// URL where the complete FSAcars reports will be stored
@define ("REPORT_FILE_URL", "http://www.domain.com/fsacars/logs/");
// Folder where the complete FSAcars reports will be stored
@define ("REPORT_FILE_PATH", "/home/domain/www/fsacars/logs/");
// Folder and filesname where the error log is located
@define ("ERROR_LOG_PATH", "/home/domain/www/fsacars/logs/error.log");
/*
* Error messages */
@define ("ERROR_OPENING_REPORT_FILE","Error opening report file");
@define ("ERROR_WRITING_REPORT_FILE","Error writing report file");
@define ("PILOT_NOT_FOUND","Pilot not found");
@define ("ERROR_IN_PILOT_QUERY","Pilot query error");
@define ("ERROR_INSERTING_PIREP","Error inserting report");
function CheckFSAcarsInfo() {
// Verify input
if (!isset($_GET['pilot'])) { return 0; }
// Request is not empty
return 1;
}
function GetFSAcarsInfo() {
/* ************************************************************************************************
@GetFSAcarsInfo
Receives inputs sent by FSAcars program and returns an array containing that information
Inputs: N/A
Outputs: string array
************************************************************************************************ */
// DO NOT EDIT THIS FUNCTION - THIS FIELDS ARE SENT BY FSACARS
$fsacars_pirep = array (
"pilot" => $_GET['pilot'],
"date" => $_GET['date'],
"time" => $_GET['time'],
"callsign" => $_GET['callsign'],
"reg" => $_GET['reg'],
"origin" => $_GET['origin'],
"dest" => $_GET['dest'],
"alt" => $_GET['alt'], //added
"equipment" => $_GET['equipment'],
"fuel" => $_GET['fuel'],
"duration" => $_GET['duration'],
"distance" => $_GET['distance'],
// "rep_url" => "Dummy", //removed
"version" => $_GET['version'], //added
"more" => $_GET['more'],
"fsacars_log" => $_GET['log'], // Get complete FSAcars log
"lat" => $_GET['lat'],
"long" => $_GET['long'],
"gs" => $_GET['gs'],
"iata" => $_GET['iata'],
"pnumber" => $_GET['pnumber'],
"depaptICAO" => $_GET['depaptICAO'],
"depapt" => $_GET['depapt'],
"disdepapt" => $_GET['disdepapt'],
"timedepapt" => $_GET['timedepapt'],
"destaptICAO" => $_GET['destaptICAO'],
"destapt" => $_GET['destapt'],
"disdestapt" => $_GET['disdestapt'],
"timedestapt" => $_GET['timedestapt'],
"Ph" => $_GET['Ph'],
"detailph" => $_GET['detailph'],
"cargo" => $_GET['cargo'],
"Regist" => $_GET['Regist'],
"purpose" => "GA",
"Online" => $_GET['Online']
);
/* DEBUG CODE - Write request to log file*/
$fe = fopen (ERROR_LOG_PATH, "a");
fwrite($fe, "[DEBUG ".date("d.m.y H:i:s")."] PILOT: ".$_GET['pilot']." DATE: ".$_GET['date']." TIME: ".$_GET['time']." CALLSIGN: ".$_GET['callsign']." REG: ".$_GET['reg']." ORIG: ".$_GET['origin']." DEST: ".$_GET['dest']." ALT: ".$_GET['alt']." EQUIP: ".$_GET['equipment']." FUEL: ".$_GET['fuel']." DURATION: ".$_GET['duration']." DIST: ".$_GET['distance']." MORE: ".$_GET['more']." VERSION: ".$_GET['version']." LAT: ".$_GET['lat']." LONG: ".$_GET['long']." GS: ".$_GET['gs']." IATA: ".$_GET['iata']." PNUMBER: ".$_GET['pnumber']." DEPAPTICAO: ".$_GET['depaptICAO']." DEPART: ".$_GET['depart']." DISTAPAPT: ".$_GET['disdepapt']." TIMEDEPAPT: ".$_GET['timedepapt']." DESTAPTICAO: ".$_GET['destaptICAO']." DESTAPT: ".$_GET['destapt']." DISDESTAPT: ".$_GET['disdestapt']." TIMEDESTAPT: ".$_GET['timedestapt']." PH: ".$_GET['ph']." DETAILPH: ".$_GET['detailph']." CARGO: ".$_GET['cargo']." REGIST: ".$_GET['Regist']." ONLINE: ".$_GET['Online']." PURPOSE: "."GA"." LOG: ".$_GET['log']."\n");
fclose($fe);
return $fsacars_pirep;
}
function SavePIREPFile($pirep_array) {
/* ************************************************************************************************
@SavePIREPFile
Receives a string array with FSAcars pireps and creates or appends information to pirep file
Inputs: string array
Outputs: 1 sucess, 0 error
************************************************************************************************ */
/* Build report filesname and URL */
$filesname=$pirep_array['pilot'].str_replace("/","",$pirep_array['date']).str_replace(":","",$pirep_array['time']).".txt";
$pirep_array['rep_url']=REPORT_FILE_URL.$pirep_array['pilot']."/".$filesname;
/* Parse FsAcars log */
$fsacars_log_lines_array = explode("*",$pirep_array['fsacars_log']);
/* Create or Append FSAcars report file */
$fp = fopen (REPORT_FILE_PATH.$pirep_array['pilot']."/".$filesname, "a");
if (!$fp) {
/* Error opening file */
$fe = fopen (ERROR_LOG_PATH, "a");
fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] PILOT: ".$pirep_array['pilot']." - ".ERROR_OPENING_REPORT_FILE." - ".$filesname."\n");
fclose($fe);
return 0;
}
/*
* Write all log lines received from FSAcars */
for($i=0;$i<count($fsacars_log_lines_array);$i++) {
if (!fwrite($fp, $fsacars_log_lines_array[$i] . "\n")) {
/* Error writing to file */
$fe = fopen (ERROR_LOG_PATH, "a");
fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] PILOT".$pirep_array['pilot']." - ".ERROR_WRITING_REPORT_FILE." - ".$filesname."\n");
fclose($fe);
return 0;
}
}
/* Close file */
fclose($fp);
return 1;
}
function InsertReportIntoDB($pirep_array) {
/* ************************************************************************************************
@InsertReportIntoDB
Receives a string array with FSAcars pireps and inserts summary into reports table
Inputs: string array
Outputs: 1 sucess, 0 error
************************************************************************************************ */
/* If this is the first chunk insert PIREP on database */
if ($pirep_array['more']=="0") {
/* connect to database */
include(MYSQL_CONNECT_INCLUDE);
/*
* Verify pilot identity (From VA Pilots table) */
$the_pilot = $pirep_array['pilot'];
//$id is an id you get from tblmember
// $pID = 'id';
// $nID = 'AN'.$pID;
// $stmt = "select id from pilots where $nID = '$the_pilot'";
$stmt = "select id from pilots where pilot_num='$the_pilot'";
$result = mysql_query($stmt);
/* mysql error */
if (!$result) {
$fe = fopen (ERROR_LOG_PATH, "a");
fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".ERROR_IN_PILOT_QUERY." - Pilot ".$pirep_array['pilot']." - ".mysql_error()." SQL: ".$stmt."\n");
fclose($fe);
return 0;
}
if (mysql_num_rows($result) == 0) {
/* Pilot not found */
$fe = fopen (ERROR_LOG_PATH, "a");
fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".PILOT_NOT_FOUND." - Pilot ".$pirep_array['pilot']."\n");
fclose($fe);
return 0;
} else {
/* Pilot found */
$id = mysql_result($result,0,"id");
/* Insert info on reports table */
$values = $id.",'".$pirep_array['date']."','".$pirep_array['time']."','".$pirep_array['callsign']."','".$pirep_array['origin']."','".$pirep_array['dest']."','".$pirep_array['reg']."','".$pirep_array['equipment']."','".$pirep_array['duration']."',".$pirep_array['fuel'].",".$pirep_array['distance'].",'".$pirep_array['rep_url']."',".$pirep_array['purpose'].";
$stmt = "INSERT INTO reports (id,date,time,callsign,origin_id,destination_id,registration,equipment,duration,fuel,distance,fsacars_rep_url,purpose) VALUES ($values)";
$result = mysql_query($stmt);
if (!$result) {
$fe = fopen (ERROR_LOG_PATH, "a");
fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".ERROR_INSERTING_PIREP." - Pilot ".$pirep_array['pilot']." - ".mysql_error()." SQL: ".$stmt."\n");
fclose($fe);
return 0;
}
/* Close the database connection */
mysql_close();
}
}
return 1;
}
function main() {
/* ************************************************************************************************
@main
Inputs: N/A
Outputs: "OK" sucess, "NOTOK" error
************************************************************************************************ */
$res = CheckFSAcarsInfo();
if ($res == 0) {
return include('FSACARS_OK.php');
}
$a = GetFSAcarsInfo();
$res = SavePIREPFile(&$a);
if ($res == 0) {
return "NOTOK";
}
$res = InsertReportIntoDB($a);
if ($res == 0) {
return "NOTOK";
}
// Report sucessfully received
return "OK";
}
/* receive_pirep.php return to FSACARS */
$out = main();
echo $out;
?>
When you said your last comment the penny dropped, but I still can't see it. There is only one ref to "$fsacars_pirep" which is a return value. Also my php skills aren't the best, I'm learning as I go and have lots more to learn.
But thanks for taking the time.
Cheers
Steph.