-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvatsim.php
More file actions
184 lines (159 loc) · 5.77 KB
/
Copy pathvatsim.php
File metadata and controls
184 lines (159 loc) · 5.77 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
class VATSIM {
private $f_orig;
private $f_bin;
private $v_status;
private $v_map_data;
private $is_debug;
public $vdata_upd;
function __construct($debug = false) {
$this->f_orig = '/data/';
$this->f_bin = '/bin/';
$this->v_status = 'https://status.vatsim.net/status.json';
$this->v_map_data = 'https://api.vatsim.net/api/map_data/';
if ($debug) {
$this->is_debug = true;
error_reporting(E_ALL);
echo realpath(__FILE__).'<br>';
} else {
$this->is_debug = false;
error_reporting(0);
}
$this->vdata_upd = false;
}
function get_filetype($datatype) {
$v_status = basename($this->v_status);
$vatspy_status = 'VATSpy.json';
switch ($datatype) {
case 'vatsim-status':
$copy = $this->curl_copy($this->v_status,$v_status);
break;
case 'vatsim-data':
// check if file exists and is already current
if ($data = $this->get_cache_file('vdata.bin')) {
if (strtotime($data['general']['update_timestamp'].' +1 minute') > time()) {
break;
}
}
if (!file_exists(__DIR__.$this->f_orig.$v_status)) {
$f = new VATSIM();
$f->get_filetype('vatsim-status');
} else {
// get url from status file
$vdata = json_decode(file_get_contents(__DIR__.$this->f_orig.$v_status),true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
} else if (isset($vdata['data']['v3'])) {
foreach ($vdata['data']['v3'] as $url) {
if ($copy = $this->curl_copy($url,basename($url))) {
$write = $this->write_cache_file(json_decode(file_get_contents($copy),true),'vdata.bin');
$this->vdata_upd = true;
break;
}
}
}
}
break;
case 'vatspy':
$resp = $this->curl_resp($this->v_map_data, ['Accept: application/json']);
$vatspy_remote = json_decode($resp, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
} else if (file_exists(__DIR__.$this->f_orig.$vatspy_status)) {
$vatspy = json_decode(file_get_contents(__DIR__.$this->f_orig.$vatspy_status),true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
} else if ($vatspy['current_commit_hash'] == $vatspy_remote['current_commit_hash']) {
break; // hash on local file is already up-to-date
} else {
// trigger VATSpy data file updates
$f = new VATSIM();
$f->get_filetype('vatspy-data');
$f->get_filetype('vatspy-geojson');
}
}
$copy = $this->curl_copy($this->v_map_data,$vatspy_status);
break;
case 'vatspy-data':
$vatspy = $this->get_json($this->v_map_data,$vatspy_status);
if ($copy = $this->curl_copy($vatspy['vatspy_dat_url'],'VATSpy.dat')) {
foreach (file($copy) as $apt) {
if (preg_match('/^([A-Z]{4})(?:\|([^\|]*)){4}\|([^\1]{4})\2?/i',$apt,$m)) {
$vatspy['FIR'][] = strtolower($m[3]);
$vatspy['ICAO'][] = strtolower($m[1]);
if (!empty($m[2]) && $m[2]!=$m[3]) $vatspy['IATA'][] = strtolower($m[2]);
else $vatspy['IATA'][] = '';
}
}
$write = $this->write_cache_file($vatspy,'vatspy.bin');
}
break;
case 'vatspy-geojson':
$vatspy = $this->get_json($this->v_map_data,$vatspy_status);
if ($copy = $this->curl_copy($vatspy['fir_boundaries_geojson_url'],'Boundaries.geojson')) {
}
break;
}
if (isset($copy) && !$copy) echo "Error while copying file '".$datatype."' to local server.";
if (isset($write) && !$write) echo "Error while writing bin file '".$datatype."'.";
}
private function curl_resp($orig, $httpheader = []) {
$curl = curl_init($orig);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
if (!empty($httpheader)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);
}
$resp = curl_exec($curl);
if ($resp === false) {
throw new RuntimeException('cURL error: ' . curl_error($ch));
}
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {
throw new RuntimeException('HTTP error: ' . $httpCode);
}
return $resp;
}
private function curl_copy($orig, $file = '') {
$dest = __DIR__.$this->f_orig.$file;
if (!($fp = fopen($dest,'w'))) return false;
if (!($curl = curl_init($orig))) return false;
if (!curl_setopt($curl, CURLOPT_FILE, $fp)) return false;
if (!curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true)) return false;
if (!curl_setopt($curl, CURLOPT_TIMEOUT, 10)) return false;
if (!curl_exec($curl)) return false;
curl_close($curl);
if (!fclose($fp)) return false;
return $dest;
}
private function get_json($orig, $file = '', $folder = '') {
$dest = __DIR__.($folder == '' ? $this->f_orig : $folder).($file == '' ? (basename($origin) == '' ? 'result.json' : basename($origin)) : $file);
if (!file_exists($dest)) {
$copy = $this->curl_copy($orig,$file);
}
$json = json_decode(file_get_contents($dest), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
}
return $json;
}
private function write_cache_file($data,$file) {
//if ($this->is_debug) echo var_dump($data);
if (!file_put_contents(__DIR__.$this->f_bin.$file, serialize($data))) return false;
return true;
}
private function get_cache_file($file) {
if ($cache = unserialize(file_get_contents(__DIR__.$this->f_bin.$file))) return $cache;
return false;
}
function get_vdata() {
$this->get_filetype('vatsim-data');
return $this->get_cache_file('vdata.bin');
}
}
if (array_key_exists('r',$_GET)) {
$file = new VATSIM(array_key_exists('debug',$_GET));
$file->get_filetype($_GET['r']);
}
?>