Completed
Push — master ( 50fd5f...ef9e9e )
by Yannick
50:20 queued 20:34
created

archive-geojson.php (3 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();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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
				$pfi = $spotter_item['flightaware_id'];
170
			} else $spotter_history_array = 0;
171
		}
172
		if (count($spotter_history_array) > 1) {
173
			//location of aircraft
174
			$output .= '{';
175
			$output .= '"type": "Feature",';
176
			$output .= '"properties": {';
177
			$output .= '"fi": "'.$pfi.'",';
178
			if (isset($begindate)) $output .= '"archive_date": "'.$begindate.'",';
179
			if (isset($spotter_item['ident']) && $spotter_item['ident'] != '') {
180
				$output .= '"c": "'.$spotter_item['ident'].'",';
181
			} else {
182
				$output .= '"c": "NA",';
183
			}
184
			if (!isset($spotter_item['aircraft_shadow']) && !$tracker && !$marine) {
185
				if (!isset($spotter_item['aircraft_icao']) || $spotter_item['aircraft_icao'] == '') $spotter_item['aircraft_shadow'] = '';
186
				else {
187
					$aircraft_icao = $spotter_item['aircraft_icao'];
188
					$aircraft_info = $Spotter->getAllAircraftInfo($spotter_item['aircraft_icao']);
189
					if (count($aircraft_info) > 0) $spotter_item['aircraft_shadow'] = $aircraft_info[0]['aircraft_shadow'];
190
					elseif (isset($spotter_item['format_source']) && $spotter_item['format_source'] == 'aprs') $spotter_item['aircraft_shadow'] = 'PA18.png';
191
					elseif ($aircraft_icao == 'PARAGLIDER') $spotter_item['aircraft_shadow'] = 'PARAGLIDER.png';
192
					else $spotter_item['aircraft_shadow'] = '';
193
					$aircrafts_shadow[$aircraft_icao] = $spotter_item['aircraft_shadow'];
194
				}
195
			}
196
197
			if (!isset($spotter_item['aircraft_shadow']) || $spotter_item['aircraft_shadow'] == '') {
198
				if ($tracker) {
199
					if (isset($spotter_item['type']) && $spotter_item['type'] == 'Ambulance') {
200
						if ($compress) $output .= '"as": "ambulance.png",';
201
						else $output .= '"aircraft_shadow": "ambulance.png",';
202
					}
203
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Police') {
204
						if ($compress) $output .= '"as": "police.png",';
205
						else $output .= '"aircraft_shadow": "police.png",';
206
					}
207
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Yacht (Sail)') {
208
						if ($compress) $output .= '"as": "ship.png",';
209
						else $output .= '"aircraft_shadow": "ship.png",';
210
					}
211
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Ship (Power Boat)') {
212
						if ($compress) $output .= '"as": "ship.png",';
213
						else $output .= '"aircraft_shadow": "ship.png",';
214
					}
215
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Shuttle') {
216
						if ($compress) $output .= '"as": "ship.png",';
217
						else $output .= '"aircraft_shadow": "ship.png",';
218
					}
219
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Truck') {
220
						if ($compress) $output .= '"as": "truck.png",';
221
						else $output .= '"aircraft_shadow": "truck.png",';
222
					}
223
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Truck (18 Wheeler)') {
224
						if ($compress) $output .= '"as": "truck.png",';
225
						else $output .= '"aircraft_shadow": "truck.png",';
226
					}
227
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Aircraft (small)') {
228
						if ($compress) $output .= '"as": "aircraft.png",';
229
						else $output .= '"aircraft_shadow": "aircraft.png",';
230
					}
231
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Large Aircraft') {
232
						if ($compress) $output .= '"as": "aircraft.png",';
233
						else $output .= '"aircraft_shadow": "aircraft.png",';
234
					}
235
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Helicopter') {
236
						if ($compress) $output .= '"as": "helico.png",';
237
						else $output .= '"aircraft_shadow": "helico.png",';
238
					}
239
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Railroad Engine') {
240
						if ($compress) $output .= '"as": "rail.png",';
241
						else $output .= '"aircraft_shadow": "rail.png",';
242
					}
243
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Firetruck') {
244
						if ($compress) $output .= '"as": "firetruck.png",';
245
						else $output .= '"aircraft_shadow": "firetruck.png",';
246
					}
247
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Bus') {
248
						if ($compress) $output .= '"as": "bus.png",';
249
						else $output .= '"aircraft_shadow": "bus.png",';
250
					}
251
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Phone') {
252
						if ($compress) $output .= '"as": "phone.png",';
253
						else $output .= '"aircraft_shadow": "phone.png",';
254
					}
255
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Jogger') {
256
						if ($compress) $output .= '"as": "jogger.png",';
257
						else $output .= '"aircraft_shadow": "jogger.png",';
258
					}
259
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Bike') {
260
						if ($compress) $output .= '"as": "bike.png",';
261
						else $output .= '"aircraft_shadow": "bike.png",';
262
					}
263
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Motorcycle') {
264
						if ($compress) $output .= '"as": "motorcycle.png",';
265
						else $output .= '"aircraft_shadow": "motorcycle.png",';
266
					}
267
					elseif (isset($spotter_item['type']) && $spotter_item['type'] == 'Balloon') {
268
						if ($compress) $output .= '"as": "balloon.png",';
269
						else $output .= '"aircraft_shadow": "balloon.png",';
270
					}
271
					else {
272
						if ($compress) $output .= '"as": "car.png",';
273
						else $output .= '"aircraft_shadow": "car.png",';
274
					}
275
				}
276
				elseif ($marine) {
277
					if ($compress) $output .= '"as": "ship.png",';
278
					else $output .= '"aircraft_shadow": "ship.png",';
279
				}
280
				else {
281
					if ($compress) $output .= '"as": "default.png",';
282
					else $output .= '"aircraft_shadow": "default.png",';
283
				}
284
			} else {
285
				if ($compress) $output .= '"as": "'.$spotter_item['aircraft_shadow'].'",';
286
				else $output .= '"aircraft_shadow": "'.$spotter_item['aircraft_shadow'].'",';
287
			}
288
289
			if (isset($spotter_item['date_iso_8601'])) {
290
				$output .= '"date_update": "'.date("M j, Y, g:i a T", strtotime($spotter_item['date_iso_8601'])).'",';
291
			}
292
			if (isset($spotter_item['date'])) {
293
				$output .= '"lu": "'.strtotime($spotter_item['date']).'",';
294
			}
295
			if (isset($spotter_item['squawk'])) {
296
				$output .= '"sq": "'.$spotter_item['squawk'].'",';
297
			}
298
			if (isset($spotter_item['squawk_usage'])) {
299
				$output .= '"squawk_usage": "'.$spotter_item['squawk_usage'].'",';
300
			}
301
			//$spotter_history_array = $SpotterArchive->getCoordArchiveSpotterDataById($spotter_item['flightaware_id']);
302
			//$spotter_history_array = array();
303
			$output_history = '';
304
			$output_time = '';
305
			$output_timediff = '';
306
			$previousts = 0;
307
			$end = false;
308
			$k = 0;
309
			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...
310
				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...
311
					$k++;
312
					$output_history .= '['.$spotter_history['longitude'].', '.$spotter_history['latitude'].'],';
313
					$output_time .= (strtotime($spotter_history['date'])*1000).',';
314
					if ($previousts != 0) $output_timediff .= (strtotime($spotter_history['date'])-$previousts).',';
315
					$previousts = strtotime($spotter_history['date']);
316
					if ($k > 1 && (strtotime($spotter_history['date'])*1000 > $enddate)) $end = true;
317
				}
318
			}
319
			if (isset($output_time)) {
320
				$output_time  = substr($output_time, 0, -1);
321
				$output .= '"time": ['.$output_time.'],';
322
			}
323
			if (isset($output_timediff)) {
324
				$output_timediff  = substr($output_timediff, 0, -1);
325
				$output .= '"timediff": ['.$output_timediff.'],';
326
			}
327
			// FIXME : type when not aircraft ?
328
			if ($compress) $output .= '"t": "aircraft"';
329
			else $output .= '"type": "aircraft"';
330
			$output .= '},';
331
			$output .= '"geometry": {';
332
			//$output .= '"type": "MultiPoint",';
333
			$output .= '"type": "LineString",';
334
			$output .= '"coordinates": [';
335
336
			if (isset($output_history)) {
337
				$output_history  = substr($output_history, 0, -1);
338
				$output .= $output_history;
339
			}
340
			$output .= ']';
341
			$output .= '}';
342
			$output .= '},';
343
		}
344
	}
345
	$output  = substr($output, 0, -1);
346
	$output .= ']';
347
	$output .= ',"initial_sqltime": "'.$sqltime.'",';
348
	$output .= '"totaltime": "'.round(microtime(true)-$begintime,2).'",';
349
	if (isset($begindate)) $output .= '"archive_date": "'.$begindate.'",';
350
	$output .= '"fc": "'.$flightcnt.'"';
351
} else {
352
	$output .= '"features": ';
353
	$output .= '{';
354
	$output .= '"type": "Feature",';
355
	$output .= '"properties": {';
356
	$output .= '"fc": "'.$flightcnt.'"}}';
357
}
358
$output .= '}';
359
print $output;
360
361
?>
362