Completed
Push — master ( f7956b...8c6c54 )
by Yannick
43:22 queued 14:24
created

archive-geojson.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
require_once('require/class.Connection.php');
3
require_once('require/class.Common.php');
4
5
if (isset($globalProtect) && $globalProtect) {
6
	@session_start();
7
	if (!isset($_SESSION['protect']) || !isset($_SERVER['HTTP_REFERER'])) {
8
		echo 'You must access this page using the right way.';
9
		die();
10
	}
11
}
12
13
$tracker = false;
14
$marine = false;
15
16
if (isset($_GET['tracker'])) {
17
	$tracker = true;
18
}
19
if (isset($_GET['marine'])) {
20
	$marine = true;
21
}
22
if ($tracker) {
23
	require_once('require/class.Tracker.php');
24
	require_once('require/class.TrackerArchive.php');
25
}
26
elseif ($marine) {
27
	require_once('require/class.Marine.php');
28
	require_once('require/class.MarineArchive.php');
29
}
30
else {
31
	require_once('require/class.Spotter.php');
32
	require_once('require/class.SpotterArchive.php');
33
}
34
$begintime = microtime(true);
35
if ($tracker) {
36
	$Tracker = new Tracker();
37
	$TrackerArchive = new TrackerArchive();
38
}
39
elseif ($marine) {
40
	$Marine = new Marine();
41
	$MarineArchive = new MarineArchive();
42
}
43
else {
44
	$Spotter = new Spotter();
45
	$SpotterArchive = new SpotterArchive();
46
}
47
$Common = new Common();
48
49
if (isset($_GET['download'])) {
50
	if ($_GET['download'] == "true") {
51
		header('Content-disposition: attachment; filename="flightairmap.json"');
52
	}
53
}
54
header('Content-Type: text/javascript');
55
56
if (!isset($globalJsonCompress)) $compress = true;
57
else $compress = $globalJsonCompress;
58
59
$from_archive = false;
60
$min = false;
61
$allhistory = false;
62
$filter['source'] = array();
63
if ((!isset($globalMapVAchoose) || $globalMapVAchoose) && isset($globalVATSIM) && $globalVATSIM && isset($_COOKIE['filter_ShowVATSIM']) && $_COOKIE['filter_ShowVATSIM'] == 'true') $filter['source'] = array_merge($filter['source'],array('vatsimtxt'));
64
if ((!isset($globalMapVAchoose) || $globalMapVAchoose) && isset($globalIVAO) && $globalIVAO && isset($_COOKIE['filter_ShowIVAO']) && $_COOKIE['filter_ShowIVAO'] == 'true') $filter['source'] = array_merge($filter['source'],array('whazzup'));
65
if ((!isset($globalMapVAchoose) || $globalMapVAchoose) && isset($globalphpVMS) && $globalphpVMS && isset($_COOKIE['filter_ShowVMS']) && $_COOKIE['filter_ShowVMS'] == 'true') $filter['source'] = array_merge($filter['source'],array('phpvmacars'));
66
if ((!isset($globalMapchoose) || $globalMapchoose) && isset($globalSBS1) && $globalSBS1 && isset($_COOKIE['filter_ShowSBS1']) && $_COOKIE['filter_ShowSBS1'] == 'true') $filter['source'] = array_merge($filter['source'],array('sbs','famaprs'));
67
if ((!isset($globalMapchoose) || $globalMapchoose) && isset($globalAPRS) && $globalAPRS && isset($_COOKIE['filter_ShowAPRS']) && $_COOKIE['filter_ShowAPRS'] == 'true') $filter['source'] = array_merge($filter['source'],array('aprs'));
68
if (isset($_COOKIE['filter_ident']) && $_COOKIE['filter_ident'] != '') $filter['ident'] = filter_var($_COOKIE['filter_ident'],FILTER_SANITIZE_STRING);
69
if (isset($_COOKIE['filter_mmsi']) && $_COOKIE['filter_mmsi'] != '') $filter['mmsi'] = filter_var($_COOKIE['filter_mmsi'],FILTER_SANITIZE_STRING);
70
if (isset($_COOKIE['filter_Airlines']) && $_COOKIE['filter_Airlines'] != '') $filter['airlines'] = filter_var_array(explode(',',$_COOKIE['filter_Airlines']),FILTER_SANITIZE_STRING);
71
if (isset($_COOKIE['filter_Sources']) && $_COOKIE['filter_Sources'] != '') $filter['source_aprs'] = filter_var_array(explode(',',$_COOKIE['filter_Sources']),FILTER_SANITIZE_STRING);
72
if (isset($_COOKIE['filter_airlinestype']) && $_COOKIE['filter_airlinestype'] != 'all') $filter['airlinestype'] = filter_var($_COOKIE['filter_airlinestype'],FILTER_SANITIZE_STRING);
73
if (isset($_COOKIE['filter_alliance']) && $_COOKIE['filter_alliance'] != 'all') $filter['alliance'] = filter_var($_COOKIE['filter_alliance'],FILTER_SANITIZE_STRING);
74
75
if (isset($globalMapPopup) && !$globalMapPopup && !(isset($_COOKIE['flightpopup']) && $_COOKIE['flightpopup'] == 'true')) {
76
	$min = true;
77
} else $min = false;
78
79
if (isset($_GET['ident'])) {
80
	$ident = filter_input(INPUT_GET,'ident',FILTER_SANITIZE_STRING);
81
	$from_archive = true;
82
	if ($tracker) {
83
		$spotter_array = $TrackerArchive->getLastArchiveTrackerDataByIdent($ident);
84
	}
85
	elseif ($marine) {
86
		$spotter_array = $MarineArchive->getLastArchiveMarineDataByIdent($ident);
87
	}
88
	else {
89
		$spotter_array = $SpotterArchive->getLastArchiveSpotterDataByIdent($ident);
90
	}
91
	$allhistory = true;
92
}
93
elseif (isset($_GET['flightaware_id'])) {
94
	$flightaware_id = filter_input(INPUT_GET,'flightaware_id',FILTER_SANITIZE_STRING);
95
	$from_archive = true;
96
	if ($tracker) {
97
		$spotter_array = $TrackerArchive->getLastArchiveTrackerDataById($flightaware_id);
98
	}
99
	elseif ($marine) {
100
		$spotter_array = $MarineArchive->getLastArchiveMarineDataById($flightaware_id);
101
	}
102
	else {
103
		$spotter_array = $SpotterArchive->getLastArchiveSpotterDataById($flightaware_id);
104
	}
105
	$allhistory = true;
106
}
107
elseif (isset($_GET['archive']) && isset($_GET['begindate']) && isset($_GET['enddate']) && isset($_GET['speed'])) {
108
	$from_archive = true;
109
	$begindate = filter_input(INPUT_GET,'begindate',FILTER_SANITIZE_NUMBER_INT);
110
	if (isset($globalAircraftMaxUpdate)) $begindate = $begindate - $globalAircraftMaxUpdate;
111
	else $begindate = $begindate - 3000;
112
	$enddate = filter_input(INPUT_GET,'enddate',FILTER_SANITIZE_NUMBER_INT);
113
	$archivespeed = filter_input(INPUT_GET,'speed',FILTER_SANITIZE_NUMBER_INT);
114
	if ($begindate != '' && $enddate != '') {
115
		$begindate = date('Y-m-d H:i:s',$begindate);
116
		$enddate = date('Y-m-d H:i:s',$enddate);
117
		//$spotter_array = $SpotterArchive->getMinLiveSpotterDataPlayback($begindate,$enddate,$filter);
118
		if ($tracker) {
119
			$spotter_array = $TrackerArchive->getMinLiveTrackerData($begindate,$enddate,$filter);
120
		}
121
		elseif ($marine) {
122
			$spotter_array = $MarineArchive->getMinLiveMarineData($begindate,$enddate,$filter);
123
		}
124
		else {
125
			$spotter_array = $SpotterArchive->getMinLiveSpotterData($begindate,$enddate,$filter);
126
		}
127
	}
128
}
129
130
if (!empty($spotter_array)) {
131
	//$flightcnt = $SpotterArchive->getLiveSpotterCount($begindate,$enddate,$filter);
132
	$flightcnt = 0;
133
	if ($flightcnt == '') $flightcnt = 0;
134
} else $flightcnt = 0;
135
136
$sqltime = round(microtime(true)-$begintime,2);
137
138
$pfi = '';
139
//var_dump($spotter_array);
140
$j = 0;
141
$aircrafts_shadow = array();
142
$output = '{';
143
$output .= '"type": "FeatureCollection",';
144
if ($min) $output .= '"minimal": "true",';
145
else $output .= '"minimal": "false",';
146
$output .= '"fc": "'.$flightcnt.'",';
147
$output .= '"sqt": "'.$sqltime.'",';
148
149
if (!empty($spotter_array) && is_array($spotter_array)) {
150
	$output .= '"features": [';
151
	foreach($spotter_array as $spotter_item) {
152
		$j++;
153
		date_default_timezone_set('UTC');
154
		if ($tracker) {
155
			if ($pfi != $spotter_item['famtrackid']) {
156
				$spotter_history_array = $TrackerArchive->getCoordArchiveTrackerDataById($spotter_item['famtrackid']);
157
				$pfi = $spotter_item['famtrackid'];
158
			} else $spotter_history_array = 0;
159
		}
160
		elseif ($marine) {
161
			if ($pfi != $spotter_item['fammarine_d']) {
162
				$spotter_history_array = $MarineArchive->getCoordArchiveMarineDataById($spotter_item['fammarine_id']);
163
				$pfi = $spotter_item['fammarine_id'];
164
			} else $spotter_history_array = 0;
165
		}
166
		else {
167
			if ($pfi != $spotter_item['flightaware_id']) {
168
				//$spotter_history_array = $SpotterArchive->getCoordArchiveSpotterDataById($spotter_item['flightaware_id']);
169
				$spotter_history_array = $SpotterArchive->getCoordArchiveSpotterDataByIdDate($spotter_item['flightaware_id'],$begindate,$enddate);
170
				$pfi = $spotter_item['flightaware_id'];
171
			} else $spotter_history_array = 0;
172
		}
173
		if (count($spotter_history_array) > 1) {
174
			//location of aircraft
175
			$output .= '{';
176
			$output .= '"type": "Feature",';
177
			$output .= '"properties": {';
178
			$output .= '"fi": "'.$pfi.'",';
179
			if (isset($begindate)) $output .= '"archive_date": "'.$begindate.'",';
180
			if (isset($spotter_item['ident']) && $spotter_item['ident'] != '') {
181
				$output .= '"c": "'.$spotter_item['ident'].'",';
182
			} else {
183
				$output .= '"c": "NA",';
184
			}
185
			if (!isset($spotter_item['aircraft_shadow']) && !$tracker && !$marine) {
186
				if (!isset($spotter_item['aircraft_icao']) || $spotter_item['aircraft_icao'] == '') $spotter_item['aircraft_shadow'] = '';
187
				else {
188
					$aircraft_icao = $spotter_item['aircraft_icao'];
189
					$aircraft_info = $Spotter->getAllAircraftInfo($spotter_item['aircraft_icao']);
190
					if (count($aircraft_info) > 0) $spotter_item['aircraft_shadow'] = $aircraft_info[0]['aircraft_shadow'];
191
					elseif (isset($spotter_item['format_source']) && $spotter_item['format_source'] == 'aprs') $spotter_item['aircraft_shadow'] = 'PA18.png';
192
					elseif ($aircraft_icao == 'PARAGLIDER') $spotter_item['aircraft_shadow'] = 'PARAGLIDER.png';
193
					else $spotter_item['aircraft_shadow'] = '';
194
					$aircrafts_shadow[$aircraft_icao] = $spotter_item['aircraft_shadow'];
195
				}
196
			}
197
198
			if (!isset($spotter_item['aircraft_shadow']) || $spotter_item['aircraft_shadow'] == '') {
199
				if ($tracker) {
200
					if (isset($spotter_item['type']) && $spotter_item['type'] == 'Ambulance') {
201
						if ($compress) $output .= '"as": "ambulance.png",';
202
						else $output .= '"aircraft_shadow": "ambulance.png",';
203
					}
204
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Police') {
205
						if ($compress) $output .= '"as": "police.png",';
206
						else $output .= '"aircraft_shadow": "police.png",';
207
					}
208
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Yacht (Sail)') {
209
						if ($compress) $output .= '"as": "ship.png",';
210
						else $output .= '"aircraft_shadow": "ship.png",';
211
					}
212
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Ship (Power Boat)') {
213
						if ($compress) $output .= '"as": "ship.png",';
214
						else $output .= '"aircraft_shadow": "ship.png",';
215
					}
216
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Shuttle') {
217
						if ($compress) $output .= '"as": "ship.png",';
218
						else $output .= '"aircraft_shadow": "ship.png",';
219
					}
220
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Truck') {
221
						if ($compress) $output .= '"as": "truck.png",';
222
						else $output .= '"aircraft_shadow": "truck.png",';
223
					}
224
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Truck (18 Wheeler)') {
225
						if ($compress) $output .= '"as": "truck.png",';
226
						else $output .= '"aircraft_shadow": "truck.png",';
227
					}
228
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Aircraft (small)') {
229
						if ($compress) $output .= '"as": "aircraft.png",';
230
						else $output .= '"aircraft_shadow": "aircraft.png",';
231
					}
232
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Large Aircraft') {
233
						if ($compress) $output .= '"as": "aircraft.png",';
234
						else $output .= '"aircraft_shadow": "aircraft.png",';
235
					}
236
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Helicopter') {
237
						if ($compress) $output .= '"as": "helico.png",';
238
						else $output .= '"aircraft_shadow": "helico.png",';
239
					}
240
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Railroad Engine') {
241
						if ($compress) $output .= '"as": "rail.png",';
242
						else $output .= '"aircraft_shadow": "rail.png",';
243
					}
244
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Firetruck') {
245
						if ($compress) $output .= '"as": "firetruck.png",';
246
						else $output .= '"aircraft_shadow": "firetruck.png",';
247
					}
248
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Bus') {
249
						if ($compress) $output .= '"as": "bus.png",';
250
						else $output .= '"aircraft_shadow": "bus.png",';
251
					}
252
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Phone') {
253
						if ($compress) $output .= '"as": "phone.png",';
254
						else $output .= '"aircraft_shadow": "phone.png",';
255
					}
256
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Jogger') {
257
						if ($compress) $output .= '"as": "jogger.png",';
258
						else $output .= '"aircraft_shadow": "jogger.png",';
259
					}
260
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Bike') {
261
						if ($compress) $output .= '"as": "bike.png",';
262
						else $output .= '"aircraft_shadow": "bike.png",';
263
					}
264
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Motorcycle') {
265
						if ($compress) $output .= '"as": "motorcycle.png",';
266
						else $output .= '"aircraft_shadow": "motorcycle.png",';
267
					}
268
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Balloon') {
269
						if ($compress) $output .= '"as": "balloon.png",';
270
						else $output .= '"aircraft_shadow": "balloon.png",';
271
					}
272
					else {
273
						if ($compress) $output .= '"as": "car.png",';
274
						else $output .= '"aircraft_shadow": "car.png",';
275
					}
276
				}
277
				elseif ($marine) {
278
					if ($compress) $output .= '"as": "ship.png",';
279
					else $output .= '"aircraft_shadow": "ship.png",';
280
				}
281
				else {
282
					if ($compress) $output .= '"as": "default.png",';
283
					else $output .= '"aircraft_shadow": "default.png",';
284
				}
285
			} else {
286
				if ($compress) $output .= '"as": "'.$spotter_item['aircraft_shadow'].'",';
287
				else $output .= '"aircraft_shadow": "'.$spotter_item['aircraft_shadow'].'",';
288
			}
289
290
			if (isset($spotter_item['date_iso_8601'])) {
291
				$output .= '"date_update": "'.date("M j, Y, g:i a T", strtotime($spotter_item['date_iso_8601'])).'",';
292
			}
293
			if (isset($spotter_item['date'])) {
294
				$output .= '"lu": "'.strtotime($spotter_item['date']).'",';
295
			}
296
			if (isset($spotter_item['squawk'])) {
297
				$output .= '"sq": "'.$spotter_item['squawk'].'",';
298
			}
299
			if (isset($spotter_item['squawk_usage'])) {
300
				$output .= '"squawk_usage": "'.$spotter_item['squawk_usage'].'",';
301
			}
302
			//$spotter_history_array = $SpotterArchive->getCoordArchiveSpotterDataById($spotter_item['flightaware_id']);
303
			//$spotter_history_array = array();
304
			$output_history = '';
305
			$output_time = '';
306
			$output_timediff = '';
307
			$previousts = 0;
308
			$end = false;
309
			$k = 0;
310
			foreach ($spotter_history_array as $key => $spotter_history) {
0 ignored issues
show
The expression $spotter_history_array of type array|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
311
				if ($end == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
312
					$k++;
313
					$output_history .= '['.$spotter_history['longitude'].', '.$spotter_history['latitude'].'],';
314
					$output_time .= (strtotime($spotter_history['date'])*1000).',';
315
					//if ($previousts != 0) $output_timediff .= (strtotime($spotter_history['date'])-$previousts).',';
316
					$previousts = strtotime($spotter_history['date']);
317
					if ($k > 1 && (strtotime($spotter_history['date'])*1000 > $enddate)) $end = true;
318
				}
319
			}
320
			if (isset($output_time)) {
321
				$output_time  = substr($output_time, 0, -1);
322
				$output .= '"time": ['.$output_time.'],';
323
			}
324
			/*
325
			if (isset($output_timediff)) {
326
				$output_timediff  = substr($output_timediff, 0, -1);
327
				$output .= '"timediff": ['.$output_timediff.'],';
328
			}
329
			*/
330
			// FIXME : type when not aircraft ?
331
			if ($compress) $output .= '"t": "aircraft"';
332
			else $output .= '"type": "aircraft"';
333
			$output .= '},';
334
			$output .= '"geometry": {';
335
			//$output .= '"type": "MultiPoint",';
336
			$output .= '"type": "LineString",';
337
			$output .= '"coordinates": [';
338
339
			if (isset($output_history)) {
340
				$output_history  = substr($output_history, 0, -1);
341
				$output .= $output_history;
342
			}
343
			$output .= ']';
344
			$output .= '}';
345
			$output .= '},';
346
		}
347
	}
348
	$output  = substr($output, 0, -1);
349
	$output .= ']';
350
	$output .= ',"initial_sqltime": "'.$sqltime.'",';
351
	$output .= '"totaltime": "'.round(microtime(true)-$begintime,2).'",';
352
	if (isset($begindate)) $output .= '"archive_date": "'.$begindate.'",';
353
	$output .= '"fc": "'.$flightcnt.'"';
354
} else {
355
	$output .= '"features": ';
356
	$output .= '{';
357
	$output .= '"type": "Feature",';
358
	$output .= '"properties": {';
359
	$output .= '"fc": "'.$flightcnt.'"}}';
360
}
361
$output .= '}';
362
print $output;
363
364
?>
365