-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatedisplay_xml.php
More file actions
102 lines (96 loc) · 4.41 KB
/
gatedisplay_xml.php
File metadata and controls
102 lines (96 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
header('Access-Control-Allow-Origin: *');
// Fetch datafeed, if needed
$refreshInterval = 15; // How many seconds between requests to pull data from VATSIM data service
$feed = array();
$localPath = fetch_my_url();
// Harvest timestamp from cached network data to determine if update is needed
$vatsim_stats_url = $localPath . "data/vatsim.json";
$cu = curl_init();
curl_setopt($cu,CURLOPT_URL,$vatsim_stats_url);
curl_setopt($cu,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cu,CURLOPT_CONNECTTIMEOUT,3);
curl_setopt($cu, CURLOPT_ENCODING, "gzip");
curl_setopt($cu,CURLOPT_SSL_VERIFYPEER,false); // There is no reason to verify the SSL certificate, skip this
$cached_stats = json_decode(curl_exec($cu), true); // Execute CURL and decode JSON
curl_close($cu);
$refresh = false;
if(strtotime($cached_stats['general']['update_timestamp']) < (time()-$refreshInterval)) {
$refresh = true;
// Fetch VATSIM status JSON to get appropriate feed URLs for data and metars
$cu = curl_init();
curl_setopt($cu,CURLOPT_URL,"https://status.vatsim.net/status.json");
curl_setopt($cu,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cu,CURLOPT_CONNECTTIMEOUT,3);
curl_setopt($cu,CURLOPT_TIMEOUT,10); // Added to prevent execution errors when VATSIM JSON hangs
curl_setopt($cu, CURLOPT_ENCODING, "gzip");
curl_setopt($cu,CURLOPT_SSL_VERIFYPEER,false); // There is no reason to verify the SSL certificate, skip this
$curl_raw = curl_exec($cu); // Execute CURL
$status_array = json_decode($curl_raw, true); // decode JSON
curl_close($cu);
// Check to make sure that the data exists
if(isset($status_array['data']['v3'][0])) {
$vatsim_stats_url = $status_array['data']['v3'][0];
}
else {
$vatsim_stats_url = "https://data.vatsim.net/v3/vatsim-data.json";
}
// CURL to ingest JSON from VATSIM data service
$cu = curl_init();
curl_setopt($cu,CURLOPT_URL,$vatsim_stats_url);
curl_setopt($cu,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cu,CURLOPT_CONNECTTIMEOUT,3);
curl_setopt($cu,CURLOPT_TIMEOUT,10); // Added to prevent execution errors when VATSIM JSON hangs
curl_setopt($cu, CURLOPT_ENCODING, "gzip");
curl_setopt($cu,CURLOPT_SSL_VERIFYPEER,false); // There is no reason to verify the SSL certificate, skip this
$curl_raw = curl_exec($cu); // Execute CURL
curl_close($cu);
$stats_array = array(); // Error prevention in the case the the CURL does not return in the allotted time
if(isJSON($curl_raw)) { // Note: In some instances, this CURL request to VATSIM has a tendancy to last more than 30 seconds and time-out. When it times out, a non-JSON is returned. This allows us to catch the error.
$stats_array = json_decode($curl_raw, true); // Execute CURL and decode JSON
}
$feed = $stats_array;
}
else {
$feed = $cached_stats;
}
$xml = new SimpleXMLElement("<markers></markers>");
$arrival = $xml->addChild('marker');
$arrival->addAttribute('name','DATEMODED');
$update = date_create_from_format('YmdHis', $feed['general']['update']);
$arrival->addAttribute('dest',date_format($update, 'm/d/y H:i') . ' UTC');
foreach($feed['pilots'] as $sortie) {
if(($sortie['flight_plan']['departure'] == $_REQUEST['afld'])||($sortie['flight_plan']['arrival'] == $_REQUEST['afld'])) {
$arrival = $xml->addChild('marker');
$arrival->addAttribute('name',$sortie['callsign']);
$arrival->addAttribute('dest',$sortie['flight_plan']['arrival']);
$arrival->addAttribute('lat',$sortie['latitude']);
$arrival->addAttribute('lng',$sortie['longitude']);
$arrival->addAttribute('flightdata',$sortie['groundspeed'] . ' kts ' . $sortie['altitude'] . ' ft');
$arrival->addAttribute('type',0);
if($sortie['flight_plan'] == 'null') {
$PColor = '#FF00FF';
}
elseif($sortie['flight_plan']['departure'] == $_REQUEST['afld']) {
$PColor = '#00FF00';
}
elseif($sortie['flight_plan']['arrival'] == $_REQUEST['afld']) {
$PColor = 'red'; //'#FFFF00';
}
else {
$PColor = '#FF0000';
}
$arrival->addAttribute('PColor',$PColor);
$arrival->addAttribute('HDG',$sortie['heading']);
$arrival->addAttribute('ACType',$sortie['flight_plan']['aircraft_faa']);
}
}
Header('Content-type: text/xml');
print($xml->asXML());
function fetch_my_url() { // Returns server URL
$ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http';
return $ssl."://".$_SERVER['SERVER_NAME'].dirname($_SERVER["REQUEST_URI"].'?').'/';
}
function isJSON($string){ // Check to verify that string is a JSON
return is_string($string) && is_array(json_decode($string, true)) ? true : false;
}