Merge pull request #456 from Struart88/patch-2
[FlightAirMap.git] / archive-geojson.php
blobf4694cca7803cc59922fc605d806e1d1ef073ff0
1 <?php
2 // This script can be slow...
3 set_time_limit(0);
4 ini_set('max_execution_time',6000);
5 require_once('require/class.Connection.php');
6 require_once('require/class.Common.php');
8 if (isset($globalProtect) && $globalProtect) {
9 @session_start();
10 if (!isset($_SESSION['protect']) || !isset($_SERVER['HTTP_REFERER'])) {
11 echo 'You must access this page using the right way.';
12 die();
16 $tracker = false;
17 $marine = false;
19 if (isset($_GET['tracker'])) {
20 $tracker = true;
22 if (isset($_GET['marine'])) {
23 $marine = true;
25 if ($tracker) {
26 require_once('require/class.Tracker.php');
27 require_once('require/class.TrackerArchive.php');
29 elseif ($marine) {
30 require_once('require/class.Marine.php');
31 require_once('require/class.MarineArchive.php');
33 else {
34 require_once('require/class.Spotter.php');
35 require_once('require/class.SpotterArchive.php');
37 $begintime = microtime(true);
38 if ($tracker) {
39 $Tracker = new Tracker();
40 $TrackerArchive = new TrackerArchive();
42 elseif ($marine) {
43 $Marine = new Marine();
44 $MarineArchive = new MarineArchive();
46 else {
47 $Spotter = new Spotter();
48 $SpotterArchive = new SpotterArchive();
50 $Common = new Common();
52 if (isset($_GET['download'])) {
53 if ($_GET['download'] == "true") {
54 header('Content-disposition: attachment; filename="flightairmap-archive.json"');
57 header('Content-Type: text/javascript');
59 if (!isset($globalJsonCompress)) $compress = true;
60 else $compress = $globalJsonCompress;
62 $from_archive = false;
63 $min = false;
64 $allhistory = false;
65 $filter['source'] = array();
66 if ((!isset($globalMapVAchoose) || $globalMapVAchoose) && isset($globalVATSIM) && $globalVATSIM && isset($_COOKIE['filter_ShowVATSIM']) && $_COOKIE['filter_ShowVATSIM'] == 'true') $filter['source'] = array_merge($filter['source'],array('vatsimtxt'));
67 if ((!isset($globalMapVAchoose) || $globalMapVAchoose) && isset($globalIVAO) && $globalIVAO && isset($_COOKIE['filter_ShowIVAO']) && $_COOKIE['filter_ShowIVAO'] == 'true') $filter['source'] = array_merge($filter['source'],array('whazzup'));
68 if ((!isset($globalMapVAchoose) || $globalMapVAchoose) && isset($globalphpVMS) && $globalphpVMS && isset($_COOKIE['filter_ShowVMS']) && $_COOKIE['filter_ShowVMS'] == 'true') $filter['source'] = array_merge($filter['source'],array('phpvmacars'));
69 if ((!isset($globalMapchoose) || $globalMapchoose) && isset($globalSBS1) && $globalSBS1 && isset($_COOKIE['filter_ShowSBS1']) && $_COOKIE['filter_ShowSBS1'] == 'true') $filter['source'] = array_merge($filter['source'],array('sbs','famaprs'));
70 if ((!isset($globalMapchoose) || $globalMapchoose) && isset($globalAPRS) && $globalAPRS && isset($_COOKIE['filter_ShowAPRS']) && $_COOKIE['filter_ShowAPRS'] == 'true') $filter['source'] = array_merge($filter['source'],array('aprs'));
71 if (isset($_COOKIE['filter_ident']) && $_COOKIE['filter_ident'] != '') $filter['ident'] = filter_var($_COOKIE['filter_ident'],FILTER_SANITIZE_STRING);
72 if (isset($_COOKIE['filter_mmsi']) && $_COOKIE['filter_mmsi'] != '') $filter['mmsi'] = filter_var($_COOKIE['filter_mmsi'],FILTER_SANITIZE_STRING);
73 if (isset($_COOKIE['filter_Airlines']) && $_COOKIE['filter_Airlines'] != '') $filter['airlines'] = filter_var_array(explode(',',$_COOKIE['filter_Airlines']),FILTER_SANITIZE_STRING);
74 if (isset($_COOKIE['filter_Sources']) && $_COOKIE['filter_Sources'] != '') $filter['source_aprs'] = filter_var_array(explode(',',$_COOKIE['filter_Sources']),FILTER_SANITIZE_STRING);
75 if (isset($_COOKIE['filter_airlinestype']) && $_COOKIE['filter_airlinestype'] != 'all') $filter['airlinestype'] = filter_var($_COOKIE['filter_airlinestype'],FILTER_SANITIZE_STRING);
76 if (isset($_COOKIE['filter_alliance']) && $_COOKIE['filter_alliance'] != 'all') $filter['alliance'] = filter_var($_COOKIE['filter_alliance'],FILTER_SANITIZE_STRING);
78 if (isset($globalMapPopup) && !$globalMapPopup && !(isset($_COOKIE['flightpopup']) && $_COOKIE['flightpopup'] == 'true')) {
79 $min = true;
80 } else $min = false;
82 if (isset($_GET['ident'])) {
83 $ident = filter_input(INPUT_GET,'ident',FILTER_SANITIZE_STRING);
84 $from_archive = true;
85 if ($tracker) {
86 $spotter_array = $TrackerArchive->getLastArchiveTrackerDataByIdent($ident);
88 elseif ($marine) {
89 $spotter_array = $MarineArchive->getLastArchiveMarineDataByIdent($ident);
91 else {
92 $spotter_array = $SpotterArchive->getLastArchiveSpotterDataByIdent($ident);
94 $allhistory = true;
96 elseif (isset($_GET['flightaware_id'])) {
97 $flightaware_id = filter_input(INPUT_GET,'flightaware_id',FILTER_SANITIZE_STRING);
98 $from_archive = true;
99 if ($tracker) {
100 $spotter_array = $TrackerArchive->getLastArchiveTrackerDataById($flightaware_id);
102 elseif ($marine) {
103 $spotter_array = $MarineArchive->getLastArchiveMarineDataById($flightaware_id);
105 else {
106 $spotter_array = $SpotterArchive->getLastArchiveSpotterDataById($flightaware_id);
108 $allhistory = true;
110 elseif (isset($_GET['archive']) && isset($_GET['begindate']) && isset($_GET['enddate']) && isset($_GET['speed'])) {
111 $from_archive = true;
112 $begindate = filter_input(INPUT_GET,'begindate',FILTER_SANITIZE_NUMBER_INT);
113 if (isset($globalAircraftMaxUpdate)) $begindate = $begindate - $globalAircraftMaxUpdate;
114 else $begindate = $begindate - 3000;
115 $enddate = filter_input(INPUT_GET,'enddate',FILTER_SANITIZE_NUMBER_INT);
116 $archivespeed = filter_input(INPUT_GET,'speed',FILTER_SANITIZE_NUMBER_INT);
117 $part = filter_input(INPUT_GET,'part',FILTER_SANITIZE_NUMBER_INT);
118 if ($part == '') $part = 0;
120 if ($begindate != '' && $enddate != '') {
121 $begindate = date('Y-m-d H:i:s',$begindate);
122 $enddate = date('Y-m-d H:i:s',$enddate);
123 //$spotter_array = $SpotterArchive->getMinLiveSpotterDataPlayback($begindate,$enddate,$filter);
124 if ($tracker) {
125 $spotter_array = $TrackerArchive->getMinLiveTrackerData($begindate,$enddate,$filter);
127 elseif ($marine) {
128 $spotter_array = $MarineArchive->getMinLiveMarineData($begindate,$enddate,$filter);
130 else {
131 $spotter_array = $SpotterArchive->getMinLiveSpotterData($begindate,$enddate,$filter,$part);
136 if (!empty($spotter_array)) {
137 //$flightcnt = $SpotterArchive->getLiveSpotterCount($begindate,$enddate,$filter);
138 $flightcnt = 0;
139 if ($flightcnt == '') $flightcnt = 0;
140 } else $flightcnt = 0;
142 $sqltime = round(microtime(true)-$begintime,2);
144 $pfi = '';
145 //var_dump($spotter_array);
146 $j = 0;
147 $aircrafts_shadow = array();
148 $output = '{';
149 $output .= '"type": "FeatureCollection",';
150 if ($min) $output .= '"minimal": "true",';
151 else $output .= '"minimal": "false",';
152 $output .= '"fc": "'.$flightcnt.'",';
153 $output .= '"sqt": "'.$sqltime.'",';
154 $begin = true;
155 if (!empty($spotter_array) && is_array($spotter_array)) {
156 $output .= '"features": [';
157 foreach($spotter_array as $spotter_item) {
158 $j++;
159 date_default_timezone_set('UTC');
160 if ($tracker) {
161 if ($pfi != $spotter_item['famtrackid']) {
162 $pfi = $spotter_item['famtrackid'];
163 $begin = true;
164 } else $spotter_history_array = 0;
166 elseif ($marine) {
167 if ($pfi != $spotter_item['fammarine_d']) {
168 $pfi = $spotter_item['fammarine_id'];
169 $begin = true;
170 } else $spotter_history_array = 0;
172 else {
173 if ($pfi != $spotter_item['flightaware_id']) {
174 $pfi = $spotter_item['flightaware_id'];
175 $begin = true;
178 if ($begin) {
179 if ($j > 1) {
180 if (isset($output_time)) {
181 $output_time = substr($output_time, 0, -1);
182 $output .= '"time": ['.$output_time.']';
184 $output .= '},';
185 $output .= '"geometry": {';
186 //$output .= '"type": "MultiPoint",';
187 $output .= '"type": "LineString",';
188 $output .= '"coordinates": [';
189 if (isset($output_history)) {
190 $output_history = substr($output_history, 0, -1);
191 $output .= $output_history;
193 $output .= ']}},';
195 $pfi = $spotter_item['flightaware_id'];
196 $output_history = '';
197 $output_time = '';
198 $output_timediff = '';
199 $previousts = 0;
200 $end = false;
201 $k = 0;
204 if ($end === false) {
205 $k++;
206 $output_history .= '['.$spotter_item['longitude'].', '.$spotter_item['latitude'].'],';
207 $output_time .= (strtotime($spotter_item['date'])*1000).',';
208 $previousts = strtotime($spotter_item['date']);
209 if ($k > 1 && (strtotime($spotter_item['date'])*1000 > $enddate)) $end = true;
212 if ($begin) {
213 $begin = false;
214 //location of aircraft
215 $output .= '{';
216 $output .= '"type": "Feature",';
217 $output .= '"properties": {';
218 $output .= '"fi": "'.$pfi.'",';
219 if (isset($begindate)) $output .= '"archive_date": "'.$begindate.'",';
220 if (isset($spotter_item['ident']) && $spotter_item['ident'] != '') {
221 $output .= '"c": "'.str_replace('\\','',$spotter_item['ident']).'",';
223 } else {
224 $output .= '"c": "NA",';
226 if (!isset($spotter_item['aircraft_shadow']) && !$tracker && !$marine) {
227 if (!isset($spotter_item['aircraft_icao']) || $spotter_item['aircraft_icao'] == '') $spotter_item['aircraft_shadow'] = '';
228 else {
229 $aircraft_icao = $spotter_item['aircraft_icao'];
230 $aircraft_info = $Spotter->getAllAircraftInfo($spotter_item['aircraft_icao']);
231 if (count($aircraft_info) > 0) $spotter_item['aircraft_shadow'] = $aircraft_info[0]['aircraft_shadow'];
232 elseif (isset($spotter_item['format_source']) && $spotter_item['format_source'] == 'aprs') $spotter_item['aircraft_shadow'] = 'PA18.png';
233 elseif ($aircraft_icao == 'PARAGLIDER') $spotter_item['aircraft_shadow'] = 'PARAGLIDER.png';
234 else $spotter_item['aircraft_shadow'] = '';
235 $aircrafts_shadow[$aircraft_icao] = $spotter_item['aircraft_shadow'];
239 if (!isset($spotter_item['aircraft_shadow']) || $spotter_item['aircraft_shadow'] == '') {
240 if ($tracker) {
241 if (isset($spotter_item['type']) && $spotter_item['type'] == 'Ambulance') {
242 if ($compress) $output .= '"as": "ambulance.png",';
243 else $output .= '"aircraft_shadow": "ambulance.png",';
245 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Police') {
246 if ($compress) $output .= '"as": "police.png",';
247 else $output .= '"aircraft_shadow": "police.png",';
249 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Yacht (Sail)') {
250 if ($compress) $output .= '"as": "ship.png",';
251 else $output .= '"aircraft_shadow": "ship.png",';
253 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Ship (Power Boat)') {
254 if ($compress) $output .= '"as": "ship.png",';
255 else $output .= '"aircraft_shadow": "ship.png",';
257 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Shuttle') {
258 if ($compress) $output .= '"as": "ship.png",';
259 else $output .= '"aircraft_shadow": "ship.png",';
261 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Truck') {
262 if ($compress) $output .= '"as": "truck.png",';
263 else $output .= '"aircraft_shadow": "truck.png",';
265 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Truck (18 Wheeler)') {
266 if ($compress) $output .= '"as": "truck.png",';
267 else $output .= '"aircraft_shadow": "truck.png",';
269 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Aircraft (small)') {
270 if ($compress) $output .= '"as": "aircraft.png",';
271 else $output .= '"aircraft_shadow": "aircraft.png",';
273 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Large Aircraft') {
274 if ($compress) $output .= '"as": "aircraft.png",';
275 else $output .= '"aircraft_shadow": "aircraft.png",';
277 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Helicopter') {
278 if ($compress) $output .= '"as": "helico.png",';
279 else $output .= '"aircraft_shadow": "helico.png",';
281 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Railroad Engine') {
282 if ($compress) $output .= '"as": "rail.png",';
283 else $output .= '"aircraft_shadow": "rail.png",';
285 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Firetruck') {
286 if ($compress) $output .= '"as": "firetruck.png",';
287 else $output .= '"aircraft_shadow": "firetruck.png",';
289 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Bus') {
290 if ($compress) $output .= '"as": "bus.png",';
291 else $output .= '"aircraft_shadow": "bus.png",';
293 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Phone') {
294 if ($compress) $output .= '"as": "phone.png",';
295 else $output .= '"aircraft_shadow": "phone.png",';
297 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Jogger') {
298 if ($compress) $output .= '"as": "jogger.png",';
299 else $output .= '"aircraft_shadow": "jogger.png",';
301 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Bike') {
302 if ($compress) $output .= '"as": "bike.png",';
303 else $output .= '"aircraft_shadow": "bike.png",';
305 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Motorcycle') {
306 if ($compress) $output .= '"as": "motorcycle.png",';
307 else $output .= '"aircraft_shadow": "motorcycle.png",';
309 elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Balloon') {
310 if ($compress) $output .= '"as": "balloon.png",';
311 else $output .= '"aircraft_shadow": "balloon.png",';
313 else {
314 if ($compress) $output .= '"as": "car.png",';
315 else $output .= '"aircraft_shadow": "car.png",';
318 elseif ($marine) {
319 if ($compress) $output .= '"as": "ship.png",';
320 else $output .= '"aircraft_shadow": "ship.png",';
322 else {
323 if ($compress) $output .= '"as": "default.png",';
324 else $output .= '"aircraft_shadow": "default.png",';
326 } else {
327 if ($compress) $output .= '"as": "'.$spotter_item['aircraft_shadow'].'",';
328 else $output .= '"aircraft_shadow": "'.$spotter_item['aircraft_shadow'].'",';
331 if (isset($spotter_item['date_iso_8601'])) {
332 $output .= '"date_update": "'.date("M j, Y, g:i a T", strtotime($spotter_item['date_iso_8601'])).'",';
334 if (isset($spotter_item['date'])) {
335 $output .= '"lu": "'.strtotime($spotter_item['date']).'",';
337 if (isset($spotter_item['squawk'])) {
338 $output .= '"sq": "'.$spotter_item['squawk'].'",';
340 if (isset($spotter_item['squawk_usage'])) {
341 $output .= '"squawk_usage": "'.$spotter_item['squawk_usage'].'",';
343 if (isset($spotter_item['type'])) {
344 $output .= '"t": "'.$spotter_item['type'].'",';
345 } elseif ($marine) {
346 $output .= '"t": "ship",';
347 } else {
348 $output .= '"t": "aircraft",';
353 if ($j > 1) {
354 if (isset($output_time)) {
355 $output_time = substr($output_time, 0, -1);
356 $output .= '"time": ['.$output_time.']';
358 $output .= '},';
359 $output .= '"geometry": {';
360 //$output .= '"type": "MultiPoint",';
361 $output .= '"type": "LineString",';
362 $output .= '"coordinates": [';
363 if (isset($output_history)) {
364 $output_history = substr($output_history, 0, -1);
365 $output .= $output_history;
367 $output .= ']';
368 $output .= '}';
369 $output .= '},';
372 $output = substr($output, 0, -1);
373 $output .= ']';
374 $output .= ',"initial_sqltime": "'.$sqltime.'",';
375 $output .= '"totaltime": "'.round(microtime(true)-$begintime,2).'",';
376 if (isset($begindate)) $output .= '"archive_date": "'.$begindate.'",';
377 $output .= '"fc": "'.$flightcnt.'"';
378 } else {
379 $output .= '"features": ';
380 $output .= '{';
381 $output .= '"type": "Feature",';
382 $output .= '"properties": {';
383 $output .= '"fc": "'.$flightcnt.'"}}';
385 $output .= '}';
386 print $output;