Completed
Push — master ( 804bdc...b40ad5 )
by Yannick
26:28
created

SpotterImport   D

Complexity

Total Complexity 668

Size/Duplication

Total Lines 1055
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 1055
rs 4.4249
c 0
b 0
f 0
wmc 668
lcom 1
cbo 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 35 13
F get_Schedule() 0 72 20
B checkAll() 0 14 8
D arrival() 0 38 23
B del() 0 12 5
B delKey() 0 16 11
F add() 0 849 588

How to fix   Complexity   

Complex Class

Complex classes like SpotterImport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SpotterImport, and based on these observations, apply Extract Interface, too.

1
<?php
2
require_once(dirname(__FILE__).'/class.Connection.php');
3
require_once(dirname(__FILE__).'/class.Spotter.php');
4
require_once(dirname(__FILE__).'/class.SpotterLive.php');
5
require_once(dirname(__FILE__).'/class.SpotterArchive.php');
6
require_once(dirname(__FILE__).'/class.Scheduler.php');
7
require_once(dirname(__FILE__).'/class.Translation.php');
8
require_once(dirname(__FILE__).'/class.Stats.php');
9
require_once(dirname(__FILE__).'/class.Source.php');
10
require_once(dirname(__FILE__).'/class.GeoidHeight.php');
11
if (isset($globalServerAPRS) && $globalServerAPRS) {
12
    require_once(dirname(__FILE__).'/class.APRS.php');
13
}
14
15
class SpotterImport {
16
    private $all_flights = array();
17
    private $last_delete_hourly = 0;
18
    private $last_delete = 0;
19
    private $stats = array();
20
    private $tmd = 0;
21
    private $source_location = array();
22
    public $db = null;
23
    public $nb = 0;
24
25
    public function __construct($dbc = null) {
26
	global $globalBeta, $globalServerAPRS, $APRSSpotter, $globalNoDB, $GeoidClass, $globalDebug, $globalGeoid;
27
	if (!(isset($globalNoDB) && $globalNoDB)) {
28
		$Connection = new Connection($dbc);
29
		$this->db = $Connection->db();
30
		date_default_timezone_set('UTC');
31
32
		// Get previous source stats
33
		$Stats = new Stats($dbc);
34
		$currentdate = date('Y-m-d');
35
		$sourcestat = $Stats->getStatsSource($currentdate);
36
		if (!empty($sourcestat)) {
37
		    foreach($sourcestat as $srcst) {
38
		    	$type = $srcst['stats_type'];
39
			if ($type == 'polar' || $type == 'hist') {
40
			    $source = $srcst['source_name'];
41
			    $data = $srcst['source_data'];
42
			    $this->stats[$currentdate][$source][$type] = json_decode($data,true);
43
	    		}
44
		    }
45
		}
46
	}
47
	if (isset($globalServerAPRS) && $globalServerAPRS) {
48
		$APRSSpotter = new APRSSpotter();
49
		//$APRSSpotter->connect();
50
	}
51
	if (isset($globalGeoid) && $globalGeoid) {
52
		try {
53
			$GeoidClass = new GeoidHeight();
54
		} catch(Exception $e) {
55
			if ($globalDebug) echo "Can't calculate geoid, check that you downloaded it via update_db.php (".$e.")\n";
56
			$GeoidClass = FALSE;
57
		}
58
	}
59
    }
60
61
    public function get_Schedule($id,$ident) {
62
	global $globalDebug, $globalFork, $globalSchedulesFetch;
63
	// Get schedule here, so it's done only one time
64
	
65
	/*
66
	if ($globalFork) {
67
		$Connection = new Connection();
68
		$dbc = $Connection->db;
69
	} else $dbc = $this->db;
70
	*/
71
	$dbc = $this->db;
72
	$this->all_flights[$id]['schedule_check'] = true;
73
	if ($globalSchedulesFetch) {
74
	if ($globalDebug) echo 'Getting schedule info...'."\n";
75
	$Spotter = new Spotter($dbc);
76
	$Schedule = new Schedule($dbc);
77
	$Translation = new Translation($dbc);
78
	$operator = $Spotter->getOperator($ident);
79
	$scheduleexist = false;
80
	if ($Schedule->checkSchedule($operator) == 0) {
81
	    $operator = $Translation->checkTranslation($ident);
82
	    if ($Schedule->checkSchedule($operator) == 0) {
83
		$schedule = $Schedule->fetchSchedule($operator);
84
		if (count($schedule) > 0 && isset($schedule['DepartureTime']) && isset($schedule['ArrivalTime'])) {
85
		    if ($globalDebug) echo "-> Schedule info for ".$operator." (".$ident.")\n";
86
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport_time' => $schedule['DepartureTime']));
87
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('arrival_airport_time' => $schedule['ArrivalTime']));
88
		    // Should also check if route schedule = route from DB
89
		    if ($schedule['DepartureAirportIATA'] != '') {
90
			if ($this->all_flights[$id]['departure_airport'] != $Spotter->getAirportIcao($schedule['DepartureAirportIATA'])) {
91
			    $airport_icao = $Spotter->getAirportIcao($schedule['DepartureAirportIATA']);
92
			    if (trim($airport_icao) != '') {
93
				$this->all_flights[$id]['departure_airport'] = $airport_icao;
94
				if ($globalDebug) echo "-> Change departure airport to ".$airport_icao." for ".$ident."\n";
95
			    }
96
			}
97
		    }
98
		    if ($schedule['ArrivalAirportIATA'] != '') {
99
			if ($this->all_flights[$id]['arrival_airport'] != $Spotter->getAirportIcao($schedule['ArrivalAirportIATA'])) {
100
			    $airport_icao = $Spotter->getAirportIcao($schedule['ArrivalAirportIATA']);
101
			    if (trim($airport_icao) != '') {
102
				$this->all_flights[$id]['arrival_airport'] = $airport_icao;
103
				if ($globalDebug) echo "-> Change arrival airport to ".$airport_icao." for ".$ident."\n";
104
			    }
105
			}
106
		    }
107
		    $Schedule->addSchedule($operator,$this->all_flights[$id]['departure_airport'],$this->all_flights[$id]['departure_airport_time'],$this->all_flights[$id]['arrival_airport'],$this->all_flights[$id]['arrival_airport_time'],$schedule['Source']);
108
		}
109
	    } else $scheduleexist = true;
110
	} else $scheduleexist = true;
111
	// close connection, at least one way will work ?
112
       if ($scheduleexist) {
113
		if ($globalDebug) echo "-> get arrival/departure airport info for ".$ident."\n";
114
    		$sch = $Schedule->getSchedule($operator);
115
		$this->all_flights[$id] = array_merge($this->all_flights[$id],array('arrival_airport' => $sch['arrival_airport_icao'],'departure_airport' => $sch['departure_airport_icao'],'departure_airport_time' => $sch['departure_airport_time'],'arrival_airport_time' => $sch['arrival_airport_time']));
116
		if ($this->all_flights[$id]['addedSpotter'] == 1) $Spotter->updateLatestScheduleSpotterData($this->all_flights[$id]['id'],$sch['departure_airport_icao'],$sch['departure_airport_time'],$sch['arrival_airport_icao'],$sch['arrival_airport_time']);
117
       }
118
	$Spotter->db = null;
119
	$Schedule->db = null;
120
	$Translation->db = null;
121
	unset($Spotter->db);
122
	unset($Schedule->db);
123
	unset($Translation->db);
124
125
	/*
126
	if ($globalFork) {
127
	    $Connection->db = null;
128
	    unset($Connection->db);
129
	}
130
	  */
131
	}
132
    }
133
134
    public function checkAll() {
135
	global $globalDebug, $globalNoImport;
136
	if ($globalDebug) echo "Update last seen flights data...\n";
137
	if (!isset($globalNoImport) || $globalNoImport === FALSE) {
138
	    foreach ($this->all_flights as $key => $flight) {
139
		if (isset($this->all_flights[$key]['id'])) {
140
		    //echo $this->all_flights[$key]['id'].' - '.$this->all_flights[$key]['latitude'].'  '.$this->all_flights[$key]['longitude']."\n";
141
    		    $Spotter = new Spotter($this->db);
142
        	    $real_arrival = $this->arrival($key);
143
        	    if (isset($this->all_flights[$key]['altitude']) && isset($this->all_flights[$key]['datetime'])) $Spotter->updateLatestSpotterData($this->all_flights[$key]['id'],$this->all_flights[$key]['ident'],$this->all_flights[$key]['latitude'],$this->all_flights[$key]['longitude'],$this->all_flights[$key]['altitude'],$this->all_flights[$key]['ground'],$this->all_flights[$key]['speed'],$this->all_flights[$key]['datetime'],$real_arrival['airport_icao'],$real_arrival['airport_time']);
144
        	}
145
	    }
146
	}
147
    }
148
149
    public function arrival($key) {
150
	global $globalClosestMinDist, $globalDebug;
151
	if ($globalDebug) echo 'Update arrival...'."\n";
152
	$Spotter = new Spotter($this->db);
153
        $airport_icao = '';
154
        $airport_time = '';
155
        if (!isset($globalClosestMinDist) || $globalClosestMinDist == '') $globalClosestMinDist = 50;
156
	if ($this->all_flights[$key]['latitude'] != '' && $this->all_flights[$key]['longitude'] != '') {
157
	    $closestAirports = $Spotter->closestAirports($this->all_flights[$key]['latitude'],$this->all_flights[$key]['longitude'],$globalClosestMinDist);
158
    	    if (isset($closestAirports[0])) {
159
        	if (isset($this->all_flights[$key]['arrival_airport']) && $this->all_flights[$key]['arrival_airport'] == $closestAirports[0]['icao']) {
160
        	    $airport_icao = $closestAirports[0]['icao'];
161
        	    $airport_time = $this->all_flights[$key]['datetime'];
162
        	    if ($globalDebug) echo "---++ Find arrival airport. airport_icao : ".$airport_icao."\n";
163
        	} elseif (count($closestAirports > 1) && isset($this->all_flights[$key]['arrival_airport']) && $this->all_flights[$key]['arrival_airport'] != '') {
164
        	    foreach ($closestAirports as $airport) {
165
        		if ($this->all_flights[$key]['arrival_airport'] == $airport['icao']) {
166
        		    $airport_icao = $airport['icao'];
167
        		    $airport_time = $this->all_flights[$key]['datetime'];
168
        		    if ($globalDebug) echo "---++ Find arrival airport. airport_icao : ".$airport_icao."\n";
169
        		    break;
170
        		}
171
        	    }
172
        	} elseif ($this->all_flights[$key]['altitude'] == 0 || ($this->all_flights[$key]['altitude_real'] != '' && ($closestAirports[0]['altitude'] < $this->all_flights[$key]['altitude_real'] && $this->all_flights[$key]['altitude_real'] < $closestAirports[0]['altitude']+5000))) {
173
        		$airport_icao = $closestAirports[0]['icao'];
174
        		$airport_time = $this->all_flights[$key]['datetime'];
175
        	} else {
176
        		if ($globalDebug) echo "----- Can't find arrival airport. Airport altitude : ".$closestAirports[0]['altitude'].' - flight altitude : '.$this->all_flights[$key]['altitude_real']."\n";
177
        	}
178
    	    } else {
179
    		    if ($globalDebug) echo "----- No Airport near last coord. Latitude : ".$this->all_flights[$key]['latitude'].' - Longitude : '.$this->all_flights[$key]['longitude'].' - MinDist : '.$globalClosestMinDist."\n";
180
    	    }
181
182
        } else {
183
        	if ($globalDebug) echo "---- No latitude or longitude. Ident : ".$this->all_flights[$key]['ident']."\n";
184
        }
185
        return array('airport_icao' => $airport_icao,'airport_time' => $airport_time);
186
    }
187
188
189
190
    public function del() {
191
	global $globalDebug, $globalNoImport, $globalNoDB;
192
	// Delete old infos
193
	if ($globalDebug) echo 'Delete old values and update latest data...'."\n";
194
	foreach ($this->all_flights as $key => $flight) {
195
	    if (isset($flight['lastupdate'])) {
196
		if ($flight['lastupdate'] < (time()-1800)) {
197
		    $this->delKey($key);
198
		}
199
	    }
200
	}
201
    }
202
203
    public function delKey($key) {
204
	global $globalDebug, $globalNoImport, $globalNoDB;
205
	// Delete old infos
206
	if (isset($this->all_flights[$key]['id'])) {
207
		if ($globalDebug) echo "--- Delete old values with id ".$this->all_flights[$key]['id']."\n";
208
		if ((!isset($globalNoImport) || $globalNoImport === FALSE) && (!isset($globalNoDB) || $globalNoDB !== TRUE)) {
209
			$real_arrival = $this->arrival($key);
210
			$Spotter = new Spotter($this->db);
211
			if ($this->all_flights[$key]['latitude'] != '' && $this->all_flights[$key]['longitude'] != '') {
212
				$result = $Spotter->updateLatestSpotterData($this->all_flights[$key]['id'],$this->all_flights[$key]['ident'],$this->all_flights[$key]['latitude'],$this->all_flights[$key]['longitude'],$this->all_flights[$key]['altitude'],$this->all_flights[$key]['ground'],$this->all_flights[$key]['speed'],$this->all_flights[$key]['datetime'],$real_arrival['airport_icao'],$real_arrival['airport_time']);
213
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
214
			}
215
		}
216
	}
217
	unset($this->all_flights[$key]);
218
    }
219
220
    public function add($line) {
221
	global $globalPilotIdAccept, $globalAirportAccept, $globalAirlineAccept, $globalAirlineIgnore, $globalAirportIgnore, $globalFork, $globalDistanceIgnore, $globalDaemon, $globalSBS1update, $globalDebug, $globalIVAO, $globalVATSIM, $globalphpVMS, $globalCoordMinChange, $globalDebugTimeElapsed, $globalCenterLatitude, $globalCenterLongitude, $globalBeta, $globalSourcesupdate, $globalAirlinesSource, $globalVAM, $globalAllFlights, $globalServerAPRS, $APRSSpotter, $globalNoImport, $globalNoDB, $globalVA, $globalAircraftMaxUpdate, $globalAircraftMinUpdate, $globalLiveInterval, $GeoidClass, $globalArchive;
222
	//if (!isset($globalDebugTimeElapsed) || $globalDebugTimeElapsed == '') $globalDebugTimeElapsed = FALSE;
223
	if (!isset($globalCoordMinChange) || $globalCoordMinChange == '') $globalCoordMinChange = '0.01';
224
	if (!isset($globalAircraftMaxUpdate) || $globalAircraftMaxUpdate == '') $globalAircraftMaxUpdate = 3000;
225
/*
226
	$Spotter = new Spotter();
227
	$dbc = $Spotter->db;
228
	$SpotterLive = new SpotterLive($dbc);
229
	$Common = new Common();
230
	$Schedule = new Schedule($dbc);
231
*/
232
	date_default_timezone_set('UTC');
233
	// signal handler - playing nice with sockets and dump1090
234
	// pcntl_signal_dispatch();
235
236
	// get the time (so we can figure the timeout)
237
	//$time = time();
238
239
	//pcntl_signal_dispatch();
240
	$dataFound = false;
241
	//$putinarchive = false;
242
	$send = false;
243
	
244
	// SBS format is CSV format
245
	if(is_array($line) && (isset($line['hex']) || isset($line['id']))) {
246
	    //print_r($line);
247
	    if (isset($line['hex'])) $line['hex'] = strtoupper($line['hex']);
248
  	    if (isset($line['id']) || (isset($line['hex']) && $line['hex'] != '' && substr($line['hex'],0,1) != '~' && $line['hex'] != '00000' && $line['hex'] != '000000' && $line['hex'] != '111111' && ctype_xdigit($line['hex']) && strlen($line['hex']) === 6)) {
249
250
		// Increment message number
251
		if (isset($line['sourcestats']) && $line['sourcestats'] === TRUE) {
252
		    $current_date = date('Y-m-d');
253
		    if (isset($line['source_name'])) $source = $line['source_name'];
254
		    else $source = '';
255
		    if ($source == '' || $line['format_source'] == 'aprs') $source = $line['format_source'];
256
		    if (!isset($this->stats[$current_date][$source]['msg'])) {
257
		    	$this->stats[$current_date][$source]['msg']['date'] = time();
258
		    	$this->stats[$current_date][$source]['msg']['nb'] = 1;
259
		    } else $this->stats[$current_date][$source]['msg']['nb'] += 1;
260
		}
261
		
262
		/*
263
		$dbc = $this->db;
264
		$Connection = new Connection($dbc);
265
		$Connection->connectionExists();
266
		$dbc = $Connection->db;
267
		*/
268
		//$Spotter = new Spotter($dbc);
269
		//$SpotterLive = new SpotterLive($dbc);
270
		$Common = new Common();
271
//		echo $this->nb++."\n";
272
		//$this->db = $dbc;
273
274
		//$hex = trim($line['hex']);
275
	        if (!isset($line['id'])) $id = trim($line['hex']);
276
	        else $id = trim($line['id']);
277
		
278
		if (!isset($this->all_flights[$id])) {
279
		    if ($globalDebug) echo 'New flight...'."\n";
280
		    $this->all_flights[$id] = array();
281
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
282
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ident' => '','departure_airport' => '', 'arrival_airport' => '','latitude' => '', 'longitude' => '', 'speed' => '', 'altitude' => '','altitude_real' => '','altitude_previous' => '', 'heading' => '','departure_airport_time' => '','arrival_airport_time' => '','squawk' => '','route_stop' => '','registration' => '','pilot_id' => '','pilot_name' => '','waypoints' => '','ground' => '0', 'format_source' => '','source_name' => '','over_country' => '','verticalrate' => '','noarchive' => false,'putinarchive' => true,'source_type' => ''));
283
		    if (isset($globalDaemon) && $globalDaemon === FALSE) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('lastupdate' => time()));
284
		    if (!isset($line['id'])) {
285
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
286
//			if (isset($line['format_source']) && ($line['format_source'] == 'sbs' || $line['format_source'] == 'tsv' || $line['format_source'] == 'raw') && $globalDaemon) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident'].'-'.date('YmdGi')));
287
//			if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'planeupdatefaa' || $line['format_source'] === 'aprs') && $globalDaemon) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.date('YmdHi')));
288
			if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'aircraftjson' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'planeupdatefaa' || $line['format_source'] === 'aprs' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'radarvirtueljson' || $line['format_source'] === 'famaprs')) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $id.'-'.date('YmdHi')));
289
		        //else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
290
		     } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
291
		    if ($globalAllFlights !== FALSE) $dataFound = true;
292
		}
293
		if (isset($line['source_type']) && $line['source_type'] != '') {
294
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('source_type' => $line['source_type']));
295
		}
296
		
297
		//print_r($this->all_flights);
298
		if (isset($line['hex']) && !isset($this->all_flights[$id]['hex']) && ctype_xdigit($line['hex'])) {
299
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('hex' => trim($line['hex'])));
300
		    //if (isset($line['datetime']) && preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) {
301
			//$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => $line['datetime']));
302
		    //} else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => date('Y-m-d H:i:s')));
303
		    if (!isset($line['aircraft_name']) && (!isset($line['aircraft_icao']) || $line['aircraft_icao'] == '????') && $line['format_source'] != 'whazzup' && $line['format_source'] != 'vatsimtxt' && $line['format_source'] != 'pireps' && $line['format_source'] != 'phpvmacars' && $line['format_source'] != 'vam' && $line['format_source'] != 'flightgearsp' && $line['format_source'] != 'flightgearmp') {
304
			$timeelapsed = microtime(true);
305
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
306
			    $Spotter = new Spotter($this->db);
307
			    if (isset($this->all_flights[$id]['source_type'])) {
308
				$aircraft_icao = $Spotter->getAllAircraftType(trim($line['hex']),$this->all_flights[$id]['source_type']);
309
			    } else {
310
				$aircraft_icao = $Spotter->getAllAircraftType(trim($line['hex']));
311
			    }
312
			    $Spotter->db = null;
313
			    if ($globalDebugTimeElapsed) echo 'Time elapsed for update getallaircrattype : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
314
			    if ($aircraft_icao != '') $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
315
			}
316
		    }
317
		    if ($globalAllFlights !== FALSE) $dataFound = true;
318
		    if ($globalDebug) echo "*********** New aircraft hex : ".$line['hex']." ***********\n";
319
		}
320
	        if (isset($line['id']) && !isset($line['hex'])) {
321
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('hex' => ''));
322
	        }
323
		if (isset($line['aircraft_icao']) && $line['aircraft_icao'] != '') {
324
			$icao = $line['aircraft_icao'];
325
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
326
				$Spotter = new Spotter($this->db);
327
				if (isset($Spotter->aircraft_correct_icaotype[$icao])) $icao = $Spotter->aircraft_correct_icaotype[$icao];
328
				$Spotter->db = null;
329
			}
330
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $icao));
331
		} elseif (!isset($this->all_flights[$id]['aircraft_icao']) && isset($line['aircraft_name'])) {
332
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
333
				// Get aircraft ICAO from aircraft name
334
				$Spotter = new Spotter($this->db);
335
				$aircraft_icao = $Spotter->getAircraftIcao($line['aircraft_name']);
336
				$Spotter->db = null;
337
				if ($aircraft_icao != '') $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
338
			}
339
		}
340
		if (!isset($this->all_flights[$id]['aircraft_icao']) && isset($line['aircraft_type'])) {
341
			if ($line['aircraft_type'] == 'PARA_GLIDER') $aircraft_icao = 'GLID';
342
			elseif ($line['aircraft_type'] == 'HELICOPTER_ROTORCRAFT') $aircraft_icao = 'UHEL';
343
			elseif ($line['aircraft_type'] == 'TOW_PLANE') $aircraft_icao = 'TOWPLANE';
344
			elseif ($line['aircraft_type'] == 'POWERED_AIRCRAFT') $aircraft_icao = 'POWAIRC';
345
			if (isset($aircraft_icao)) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
346
		}
347
		if (!isset($this->all_flights[$id]['aircraft_icao'])) {
348
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => 'NA'));
349
		}
350
		//if (isset($line['datetime']) && preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) {
351
		if (isset($line['datetime']) && strtotime($line['datetime']) > time()-20*60 && strtotime($line['datetime']) < time()+20*60) {
352
		    if (!isset($this->all_flights[$id]['datetime']) || strtotime($line['datetime']) >= strtotime($this->all_flights[$id]['datetime'])) {
353
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => $line['datetime']));
354
		    } else {
355
				if (strtotime($line['datetime']) == strtotime($this->all_flights[$id]['datetime']) && $globalDebug) echo "!!! Date is the same as previous data for ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."\n";
356
				elseif (strtotime($line['datetime']) > strtotime($this->all_flights[$id]['datetime']) && $globalDebug) echo "!!! Date previous latest data (".$line['datetime']." > ".$this->all_flights[$id]['datetime'].") !!! for ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."\n";
357
				/*
358
				echo strtotime($line['datetime']).' > '.strtotime($this->all_flights[$id]['datetime']);
359
				print_r($this->all_flights[$id]);
360
				print_r($line);
361
				*/
362
				return '';
363
		    }
364
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) < time()-20*60) {
365
			if ($globalDebug) echo "!!! Date is too old ".$line['datetime']." for ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."!!!\n";
366
			return '';
367
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) > time()+20*60) {
368
			if ($globalDebug) echo "!!! Date is in the future ".$line['datetime']." for ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."!!!\n";
369
			return '';
370
		} elseif (!isset($line['datetime'])) {
371
			date_default_timezone_set('UTC');
372
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => date('Y-m-d H:i:s')));
373
		} else {
374
			if ($globalDebug) echo "!!! Unknow date error ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."!!!";
375
			return '';
376
		}
377
378
		if (isset($line['registration']) && $line['registration'] != '' && $line['registration'] != 'z.NO-REG' && $line['registration'] != 'NA') {
379
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('registration' => $line['registration']));
380
		}
381
		if (isset($line['waypoints']) && $line['waypoints'] != '') {
382
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('waypoints' => $line['waypoints']));
383
		}
384
		if (isset($line['pilot_id']) && $line['pilot_id'] != '') {
385
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('pilot_id' => trim($line['pilot_id'])));
386
		}
387
		if (isset($line['pilot_name']) && $line['pilot_name'] != '') {
388
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('pilot_name' => trim($line['pilot_name'])));
389
		}
390
 
391
		if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_flights[$id]['ident'] != trim($line['ident'])) && preg_match('/^[a-zA-Z0-9]+$/', $line['ident'])) {
392
393
		    if ($this->all_flights[$id]['addedSpotter'] == 1) {
394
			if ($globalVA !== TRUE && $globalIVAO !== TRUE && $globalVATSIM !== TRUE && $globalphpVMS !== TRUE && $globalVAM !== TRUE && $this->all_flights[$id]['lastupdate'] < time() - 1600) {
395
				if ($globalDebug) echo '---!!!! New ident, reset aircraft data...'."\n";
396
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
397
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 1));
398
				if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'aircraftjson' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'planeupdatefaa' || $line['format_source'] === 'aprs' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'radarvirtueljson' || $line['format_source'] === 'famaprs')) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $id.'-'.date('YmdHi')));
399
				elseif (isset($line['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
400
				elseif (isset($this->all_flights[$id]['ident'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
401
			} else {
402
			    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ident' => trim($line['ident'])));
403
			    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
404
				$timeelapsed = microtime(true);
405
            			$Spotter = new Spotter($this->db);
406
            			$fromsource = NULL;
407
            			if (isset($globalAirlinesSource) && $globalAirlinesSource != '') $fromsource = $globalAirlinesSource;
408
            			elseif (isset($line['format_source']) && $line['format_source'] == 'vatsimtxt') $fromsource = 'vatsim';
409
				elseif (isset($line['format_source']) && $line['format_source'] == 'whazzup') $fromsource = 'ivao';
410
				elseif (isset($globalVATSIM) && $globalVATSIM) $fromsource = 'vatsim';
411
				elseif (isset($globalIVAO) && $globalIVAO) $fromsource = 'ivao';
412
            			$result = $Spotter->updateIdentSpotterData($this->all_flights[$id]['id'],$this->all_flights[$id]['ident'],$fromsource);
413
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
414
				$Spotter->db = null;
415
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update identspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
416
			    }
417
			}
418
		    } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ident' => trim($line['ident'])));
419
		    
420
/*
421
		    if (!isset($line['id'])) {
422
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
423
//			if (isset($line['format_source']) && ($line['format_source'] == 'sbs' || $line['format_source'] == 'tsv' || $line['format_source'] == 'raw') && $globalDaemon) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident'].'-'.date('YmdGi')));
424
			if (isset($line['format_source']) && ($line['format_source'] == 'sbs' || $line['format_source'] == 'tsv' || $line['format_source'] == 'raw') && $globalDaemon) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.date('YmdGi')));
425
		        else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
426
		     } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
427
  */
428
		    if (!isset($this->all_flights[$id]['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
429
430
		    //$putinarchive = true;
431
		    if (isset($line['departure_airport_time']) && $line['departure_airport_time'] != 0) {
432
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport_time' => $line['departure_airport_time']));
433
		    }
434
		    if (isset($line['arrival_airport_time']) && $line['arrival_airport_time'] != 0) {
435
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('arrival_airport_time' => $line['arrival_airport_time']));
436
		    }
437
		    if (isset($line['departure_airport_icao']) && isset($line['arrival_airport_icao'])) {
438
		    		$this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport' => $line['departure_airport_icao'],'arrival_airport' => $line['arrival_airport_icao'],'route_stop' => ''));
439
		    } elseif (isset($line['departure_airport_iata']) && isset($line['arrival_airport_iata'])) {
440
			$timeelapsed = microtime(true);
441
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
442
				$Spotter = new Spotter($this->db);
443
				$line['departure_airport_icao'] = $Spotter->getAirportIcao($line['departure_airport_iata']);
444
				$line['arrival_airport_icao'] = $Spotter->getAirportIcao($line['arrival_airport_iata']);
445
		    		$this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport' => $line['departure_airport_icao'],'arrival_airport' => $line['arrival_airport_icao'],'route_stop' => ''));
446
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update getAirportICAO : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
447
                        }
448
		    } elseif (!isset($line['format_source']) || $line['format_source'] != 'aprs') {
449
			$timeelapsed = microtime(true);
450
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
451
			    $Spotter = new Spotter($this->db);
452
			    $route = $Spotter->getRouteInfo(trim($line['ident']));
453
			    if (!isset($route['fromairport_icao']) && !isset($route['toairport_icao'])) {
454
				$Translation = new Translation($this->db);
455
				$ident = $Translation->checkTranslation(trim($line['ident']));
456
				$route = $Spotter->getRouteInfo($ident);
457
				$Translation->db = null;
458
			    }
459
			    $Spotter->db = null;
460
			    if ($globalDebugTimeElapsed) echo 'Time elapsed for update getrouteinfo : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
461
                    	}
462
			if (isset($route['fromairport_icao']) && isset($route['toairport_icao'])) {
463
			    //if ($route['FromAirport_ICAO'] != $route['ToAirport_ICAO']) {
464
			    if ($route['fromairport_icao'] != $route['toairport_icao']) {
465
				//    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport' => $route['FromAirport_ICAO'],'arrival_airport' => $route['ToAirport_ICAO'],'route_stop' => $route['RouteStop']));
466
		    		$this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport' => $route['fromairport_icao'],'arrival_airport' => $route['toairport_icao'],'route_stop' => $route['routestop']));
467
		    	    }
468
			}
469
			if (!isset($globalFork)) $globalFork = TRUE;
470
			if (!$globalVA && !$globalIVAO && !$globalVATSIM && !$globalphpVMS && !$globalVAM && (!isset($line['format_source']) || $line['format_source'] != 'aprs')) {
471
				if (!isset($this->all_flights[$id]['schedule_check']) || $this->all_flights[$id]['schedule_check'] === false) $this->get_Schedule($id,trim($line['ident']));
472
			}
473
		    }
474
		}
475
476
		if (isset($line['speed']) && $line['speed'] != '' && $line['speed'] != 0) {
477
		//    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => $line[12]));
478
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => round($line['speed'])));
479
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed_fromsrc' => true));
480
		    //$dataFound = true;
481
		} else if (!isset($this->all_flights[$id]['speed_fromsrc']) && isset($this->all_flights[$id]['time_last_coord']) && $this->all_flights[$id]['time_last_coord'] != time() && isset($line['latitude']) && isset($line['longitude'])) {
482
		    $distance = $Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m');
483
		    if ($distance > 1000 && $distance < 10000) {
484
		    // use datetime
485
			$speed = $distance/(time() - $this->all_flights[$id]['time_last_coord']);
486
			$speed = $speed*3.6;
487
			if ($speed < 1000) {
488
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => round($speed)));
489
	  			if ($globalDebug) echo "ø Calculated Speed for ".$this->all_flights[$id]['hex']." : ".round($speed)." - distance : ".$distance."\n";
490
	  		} else {
491
	  			if ($globalDebug) echo "ø IGNORED : Calculated Speed for ".$this->all_flights[$id]['hex']." : ".round($speed)." - distance : ".$distance."\n";
492
	  		}
493
		    }
494
		}
495
496
497
498
	        if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '' && is_numeric($line['latitude']) && is_numeric($line['longitude'])) {
499
	    	    if (ctype_digit(strval($line['latitude'])) || ctype_digit(strval($line['longitude']))) {
500
	    	    	if ($globalDebug) echo "/!\ Invalid latitude or/and longitude data : lat: ".$line['latitude']." - lng: ".$line['longitude']."\n";
501
	    	    	return false;
502
	    	    }
503
	    	    if (isset($this->all_flights[$id]['time_last_coord'])) $timediff = round(time()-$this->all_flights[$id]['time_last_coord']);
504
	    	    else unset($timediff);
505
	    	    if (isset($this->all_flights[$id]['time_last_archive_coord'])) $timediff_archive = round(time()-$this->all_flights[$id]['time_last_archive_coord']);
506
	    	    else unset($timediff_archive);
507
	    	    if ($this->tmd > 5
508
	    	        || (isset($line['format_source']) 
509
	    	    	    && $line['format_source'] == 'airwhere' 
510
	    	    	    && ((!isset($this->all_flights[$id]['latitude']) 
511
	    	    		|| !isset($this->all_flights[$id]['longitude'])) 
512
	    	    		|| (isset($this->all_flights[$id]['latitude']) 
513
	    	    		    && isset($this->all_flights[$id]['longitude']) 
514
	    	    		    && $this->all_flights[$id]['latitude'] != $line['latitude'] 
515
	    	    		    && $this->all_flights[$id]['longitude'] != $line['longitude']
516
	    	    		)
517
	    	    	    )
518
	    	    	)
519
	    		|| (isset($globalVA) && $globalVA) 
520
	    	    	|| (isset($globalIVAO) && $globalIVAO)
521
	    	    	|| (isset($globalVATSIM) && $globalVATSIM)
522
	    	    	|| (isset($globalphpVMS) && $globalphpVMS)
523
	    	    	|| (isset($globalVAM) && $globalVAM)
524
	    	    	|| !isset($timediff)
525
	    	    	|| $timediff > $globalLiveInterval
526
	    	    	|| $globalArchive
527
	    	    	|| ($timediff > 30 
528
	    	    	    && isset($this->all_flights[$id]['latitude']) 
529
	    	    	    && isset($this->all_flights[$id]['longitude']) 
530
	    	    	    && $Common->withinThreshold($timediff,$Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m'))
531
	    	    	    )
532
	    	    	) {
533
534
			if ((isset($timediff) && !isset($timediff_archive)) || (isset($this->all_flights[$id]['archive_latitude']) && isset($this->all_flights[$id]['archive_longitude']) && isset($this->all_flights[$id]['livedb_latitude']) && isset($this->all_flights[$id]['livedb_longitude']))) {
535
			    if ((isset($timediff_archive) && $timediff_archive > $globalAircraftMaxUpdate)
0 ignored issues
show
Bug introduced by
The variable $timediff_archive does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
536
				|| (isset($line['format_source']) && $line['format_source'] == 'airwhere') 
537
				|| !$Common->checkLine($this->all_flights[$id]['archive_latitude'],$this->all_flights[$id]['archive_longitude'],$this->all_flights[$id]['livedb_latitude'],$this->all_flights[$id]['livedb_longitude'],$line['latitude'],$line['longitude'])) {
538
				$this->all_flights[$id]['archive_latitude'] = $line['latitude'];
539
				$this->all_flights[$id]['archive_longitude'] = $line['longitude'];
540
				$this->all_flights[$id]['putinarchive'] = true;
541
				$this->tmd = 0;
542
				if (!isset($globalNoImport) || $globalNoImport === FALSE) {
543
				    if ($globalDebug) echo "\n".' ------- Check Country for '.$this->all_flights[$id]['ident'].' with latitude : '.$line['latitude'].' and longitude : '.$line['longitude'].'.... ';
544
				    $timeelapsed = microtime(true);
545
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
546
					$Spotter = new Spotter($this->db);
547
					$all_country = $Spotter->getCountryFromLatitudeLongitude($line['latitude'],$line['longitude']);
548
					if (!empty($all_country)) $this->all_flights[$id]['over_country'] = $all_country['iso2'];
549
					else $this->all_flights[$id]['over_country'] = '';
550
					$Spotter->db = null;
551
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update getCountryFromlatitudeLongitude : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
552
					if ($globalDebug) echo 'FOUND : '.$this->all_flights[$id]['over_country'].' ---------------'."\n";
553
				    }
554
				}
555
				$this->all_flights[$id]['time_last_archive_coord'] = time();
556
			    } 
557
			    /*
558
			    else {
559
				if (!isset($timediff)) echo 'NO TIMEDIFF';
560
				else {
561
				echo 'timediff : '.$timediff.' - maxupdate : '.$globalAircraftMaxUpdate."\n";
562
				var_dump($Common->checkLine($this->all_flights[$id]['archive_latitude'],$this->all_flights[$id]['archive_longitude'],$this->all_flights[$id]['livedb_latitude'],$this->all_flights[$id]['livedb_longitude'],$line['latitude'],$line['longitude']));
563
				echo 'lat1: '.$this->all_flights[$id]['archive_latitude'].' lon1: '.$this->all_flights[$id]['archive_longitude'].' - lat2: '.$this->all_flights[$id]['livedb_latitude'].' lon2: '.$this->all_flights[$id]['livedb_longitude'].' - lat3: '.$line['latitude'].' lon3: '.$line['longitude']."\n";
564
				}
565
				echo 'NOT OK HERE';
566
			    }
567
			    */
568
			}
569
570
			if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
571
			    //if (!isset($this->all_flights[$id]['latitude']) || $this->all_flights[$id]['latitude'] == '' || abs($this->all_flights[$id]['latitude']-$line['latitude']) < 3 || $line['format_source'] != 'sbs' || time() - $this->all_flights[$id]['lastupdate'] > 30) {
572
				if (!isset($this->all_flights[$id]['archive_latitude'])) {
573
					$this->all_flights[$id]['archive_latitude'] = $line['latitude'];
574
					$this->all_flights[$id]['time_last_coord'] = time();
575
				}
576
				//if (!isset($this->all_flights[$id]['livedb_latitude']) || abs($this->all_flights[$id]['livedb_latitude']-$line['latitude']) > $globalCoordMinChange || $this->all_flights[$id]['format_source'] == 'aprs' || ($this->all_flights[$id]['format_source'] == 'airwhere' && abs($this->all_flights[$id]['livedb_latitude']-$line['latitude']) > 0.0001)) {
577
				if (!isset($this->all_flights[$id]['livedb_latitude']) || abs($this->all_flights[$id]['livedb_latitude']-$line['latitude']) > $globalCoordMinChange || ($this->all_flights[$id]['format_source'] == 'airwhere' && abs($this->all_flights[$id]['livedb_latitude']-$line['latitude']) > 0.0001)) {
578
				    $this->all_flights[$id]['livedb_latitude'] = $line['latitude'];
579
				    $dataFound = true;
580
				    $this->all_flights[$id]['time_last_coord'] = time();
581
				}
582
				// elseif ($globalDebug) echo '!*!*! Ignore data, too close to previous one'."\n";
583
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('latitude' => $line['latitude']));
584
				/*
585
				if (abs($this->all_flights[$id]['archive_latitude']-$this->all_flights[$id]['latitude']) > 0.3) {
586
				    $this->all_flights[$id]['archive_latitude'] = $line['latitude'];
587
				    $this->all_flights[$id]['putinarchive'] = true;
588
				    //$putinarchive = true;
589
				}
590
				*/
591
			    /*
592
			    } elseif (isset($this->all_flights[$id]['latitude'])) {
593
				if ($globalDebug) echo '!!! Strange latitude value - diff : '.abs($this->all_flights[$id]['latitude']-$line['latitude']).'- previous lat : '.$this->all_flights[$id]['latitude'].'- new lat : '.$line['latitude']."\n";
594
			    }
595
			    */
596
			}
597
			if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
598
			    if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
599
			    //if (!isset($this->all_flights[$id]['longitude']) || $this->all_flights[$id]['longitude'] == ''  || abs($this->all_flights[$id]['longitude']-$line['longitude']) < 2 || $line['format_source'] != 'sbs' || time() - $this->all_flights[$id]['lastupdate'] > 30) {
600
				if (!isset($this->all_flights[$id]['archive_longitude'])) {
601
					$this->all_flights[$id]['archive_longitude'] = $line['longitude'];
602
					$this->all_flights[$id]['time_last_coord'] = time();
603
				}
604
				//if (!isset($this->all_flights[$id]['livedb_longitude']) || abs($this->all_flights[$id]['livedb_longitude']-$line['longitude']) > $globalCoordMinChange || $this->all_flights[$id]['format_source'] == 'aprs' || ($this->all_flights[$id]['format_source'] == 'airwhere' && abs($this->all_flights[$id]['livedb_longitude']-$line['longitude']) > 0.0001)) {
605
				if (!isset($this->all_flights[$id]['livedb_longitude']) || abs($this->all_flights[$id]['livedb_longitude']-$line['longitude']) > $globalCoordMinChange || ($this->all_flights[$id]['format_source'] == 'airwhere' && abs($this->all_flights[$id]['livedb_longitude']-$line['longitude']) > 0.0001)) {
606
				    $this->all_flights[$id]['livedb_longitude'] = $line['longitude'];
607
				    $dataFound = true;
608
				    $this->all_flights[$id]['time_last_coord'] = time();
609
				}
610
				// elseif ($globalDebug) echo '!*!*! Ignore data, too close to previous one'."\n";
611
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('longitude' => $line['longitude']));
612
				/*
613
				if (abs($this->all_flights[$id]['archive_longitude']-$this->all_flights[$id]['longitude']) > 0.3) {
614
				    $this->all_flights[$id]['archive_longitude'] = $line['longitude'];
615
				    $this->all_flights[$id]['putinarchive'] = true;
616
				    //$putinarchive = true;
617
				}
618
				*/
619
			/*
620
			    } elseif (isset($this->all_flights[$id]['longitude'])) {
621
				if ($globalDebug) echo '!!! Strange longitude value - diff : '.abs($this->all_flights[$id]['longitude']-$line['longitude']).'- previous lat : '.$this->all_flights[$id]['longitude'].'- new lat : '.$line['longitude']."\n";
622
			    }
623
			    */
624
			}
625
626
		    } else if ($globalDebug && $timediff > 30) {
627
			$this->tmd = $this->tmd + 1;
628
			echo '!!! Too much distance in short time... for '.$this->all_flights[$id]['ident']."\n";
629
			echo 'Time : '.$timediff.'s - Distance : '.$Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m')."m -";
630
			echo 'Speed : '.(($Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m')/$timediff)*3.6)." km/h - ";
631
			echo 'Lat : '.$line['latitude'].' - long : '.$line['longitude'].' - prev lat : '.$this->all_flights[$id]['latitude'].' - prev long : '.$this->all_flights[$id]['longitude']." \n";
632
		    }
633
		}
634
		if (isset($line['last_update']) && $line['last_update'] != '') {
635
		    if (isset($this->all_flights[$id]['last_update']) && $this->all_flights[$id]['last_update'] != $line['last_update']) $dataFound = true;
636
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('last_update' => $line['last_update']));
637
		}
638
		if (isset($line['verticalrate']) && $line['verticalrate'] != '') {
639
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('verticalrate' => $line['verticalrate']));
640
		    //$dataFound = true;
641
		}
642
		if (isset($line['format_source']) && $line['format_source'] != '') {
643
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('format_source' => $line['format_source']));
644
		}
645
		if (isset($line['source_name']) && $line['source_name'] != '') {
646
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('source_name' => $line['source_name']));
647
		}
648
		if (isset($line['emergency']) && $line['emergency'] != '') {
649
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('emergency' => $line['emergency']));
650
		    //$dataFound = true;
651
		}
652
		if (isset($line['ground']) && $line['ground'] != '') {
653
		    if (isset($this->all_flights[$id]['ground']) && $this->all_flights[$id]['ground'] == 1 && $line['ground'] == 0) {
654
			// Here we force archive of flight because after ground it's a new one (or should be)
655
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
656
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 1));
657
			if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'aircraftjson' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'planeupdatefaa' || $line['format_source'] === 'aprs' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'radarvirtueljson' || $line['format_source'] === 'famaprs')) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $id.'-'.date('YmdHi')));
658
		        elseif (isset($line['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
659
			elseif (isset($this->all_flights[$id]['ident'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
660
		    }
661
		    if ($line['ground'] != 1) $line['ground'] = 0;
662
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ground' => $line['ground']));
663
		    //$dataFound = true;
664
		}
665
		if (isset($line['squawk']) && $line['squawk'] != '') {
666
		    if (isset($this->all_flights[$id]['squawk']) && $this->all_flights[$id]['squawk'] != '7500' && $this->all_flights[$id]['squawk'] != '7600' && $this->all_flights[$id]['squawk'] != '7700' && isset($this->all_flights[$id]['id'])) {
667
			    if ($this->all_flights[$id]['squawk'] != $line['squawk']) $this->all_flights[$id]['putinarchive'] = true;
668
			    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('squawk' => $line['squawk']));
669
			    $highlight = '';
670
			    if ($this->all_flights[$id]['squawk'] == '7500') $highlight = 'Squawk 7500 : Hijack at '.date('Y-m-d G:i').' UTC';
671
			    if ($this->all_flights[$id]['squawk'] == '7600') $highlight = 'Squawk 7600 : Lost Comm (radio failure) at '.date('Y-m-d G:i').' UTC';
672
			    if ($this->all_flights[$id]['squawk'] == '7700') $highlight = 'Squawk 7700 : Emergency at '.date('Y-m-d G:i').' UTC';
673
			    if ($highlight != '') {
674
				$timeelapsed = microtime(true);
675
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
676
				    $Spotter = new Spotter($this->db);
677
				    $Spotter->setHighlightFlight($this->all_flights[$id]['id'],$highlight);
678
				    $Spotter->db = null;
679
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update sethighlightflight : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
680
				}
681
				//$putinarchive = true;
682
				//$highlight = '';
683
			    }
684
			    
685
		    } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('squawk' => $line['squawk']));
686
		    //$dataFound = true;
687
		}
688
689
		if (isset($line['altitude']) && $line['altitude'] != '') {
690
			if (isset($line['altitude_relative']) && isset($GeoidClass) && is_object($GeoidClass)) {
691
				if ($line['altitude_relative'] == 'AMSL' || $line['altitude_relative'] == 'MSL') {
692
					$geoid = round($GeoidClass->get($this->all_flights[$id]['livedb_latitude'],$this->all_flights[$id]['livedb_longitude'])*3.28084,2);
693
					//if ($globalDebug) echo '=> Set altitude to WGS84 Ellipsoid, add '.$geoid.' to '.$line['altitude']."\n";
694
					$line['altitude'] = $line['altitude'] - $geoid;
695
				}
696
			}
697
		    //if (!isset($this->all_flights[$id]['altitude']) || $this->all_flights[$id]['altitude'] == '' || ($this->all_flights[$id]['altitude'] > 0 && $line['altitude'] != 0)) {
698
			if (is_int($this->all_flights[$id]['altitude']) && abs(round($line['altitude']/100)-$this->all_flights[$id]['altitude']) > 3) $this->all_flights[$id]['putinarchive'] = true;
699
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude' => round($line['altitude']/100)));
700
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude_real' => $line['altitude']));
701
			//$dataFound = true;
702
		    //} elseif ($globalDebug) echo "!!! Strange altitude data... not added.\n";
703
		    if ($globalVA !== TRUE && $globalIVAO !== TRUE && $globalVATSIM !== TRUE && $globalphpVMS !== TRUE && $globalVAM !== TRUE) {
704
			if (isset($this->all_flights[$id]['over_country']) && $this->all_flights[$id]['over_country'] != '' && isset($this->all_flights[$id]['altitude_previous']) && $this->all_flights[$id]['altitude_previous'] != '' && $this->all_flights[$id]['altitude_previous'] < $this->all_flights[$id]['altitude_real'] && isset($this->all_flights[$id]['lastupdate']) && $this->all_flights[$id]['lastupdate'] < time() - 1600) {
705
				if ($globalDebug) echo '--- Reset because of altitude'."\n";
706
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
707
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 1));
708
				if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'aircraftjson' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'planeupdatefaa' || $line['format_source'] === 'aprs' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'radarvirtueljson' || $line['format_source'] === 'famaprs')) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $id.'-'.date('YmdHi')));
709
				elseif (isset($line['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
710
				elseif (isset($this->all_flights[$id]['ident'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
711
			}
712
		    }
713
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude_previous' => $line['altitude']));
714
		}
715
716
		if (isset($line['noarchive']) && $line['noarchive'] === true) {
717
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('noarchive' => true));
718
		}
719
		
720
		if (isset($line['heading']) && $line['heading'] != '') {
721
		    if (is_int($this->all_flights[$id]['heading']) && abs($this->all_flights[$id]['heading']-round($line['heading'])) > 10) $this->all_flights[$id]['putinarchive'] = true;
722
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => round($line['heading'])));
723
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading_fromsrc' => true));
724
		    //$dataFound = true;
725
  		} elseif (!isset($this->all_flights[$id]['heading_fromsrc']) && isset($this->all_flights[$id]['archive_latitude']) && $this->all_flights[$id]['archive_latitude'] != $this->all_flights[$id]['latitude'] && isset($this->all_flights[$id]['archive_longitude']) && $this->all_flights[$id]['archive_longitude'] != $this->all_flights[$id]['longitude']) {
726
  		    $heading = $Common->getHeading($this->all_flights[$id]['archive_latitude'],$this->all_flights[$id]['archive_longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude']);
727
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => round($heading)));
728
		    if (abs($this->all_flights[$id]['heading']-round($heading)) > 10) $this->all_flights[$id]['putinarchive'] = true;
729
  		    if ($globalDebug) echo "ø Calculated Heading for ".$this->all_flights[$id]['hex']." : ".$heading."\n";
730
  		} elseif (isset($this->all_flights[$id]['format_source']) && $this->all_flights[$id]['format_source'] == 'ACARS') {
731
  		    // If not enough messages and ACARS set heading to 0
732
  		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => 0));
733
  		}
734
		if ($globalDaemon === TRUE && isset($globalSourcesupdate) && $globalSourcesupdate != '' && isset($this->all_flights[$id]['lastupdate']) && time()-$this->all_flights[$id]['lastupdate'] < $globalSourcesupdate) $dataFound = false;
735
		elseif ($globalDaemon === TRUE && isset($globalSBS1update) && $globalSBS1update != '' && isset($this->all_flights[$id]['lastupdate']) && time()-$this->all_flights[$id]['lastupdate'] < $globalSBS1update) $dataFound = false;
736
		elseif ($globalDaemon === TRUE && isset($globalAircraftMinUpdate) && $globalAircraftMinUpdate != '' && isset($this->all_flights[$id]['lastupdate']) && time()-$this->all_flights[$id]['lastupdate'] < $globalAircraftMinupdate) $dataFound = false;
0 ignored issues
show
Bug introduced by
The variable $globalAircraftMinupdate does not exist. Did you mean $globalAircraftMinUpdate?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
737
738
//		print_r($this->all_flights[$id]);
739
		//gets the callsign from the last hour
740
		//if (time()-$this->all_flights[$id]['lastupdate'] > 30 && $dataFound == true && $this->all_flights[$id]['ident'] != '' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
741
		//if ($dataFound == true && isset($this->all_flights[$id]['hex']) && $this->all_flights[$id]['ident'] != '' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
742
		//if ($dataFound === true && isset($this->all_flights[$id]['hex']) && $this->all_flights[$id]['heading'] != '' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
743
		//if ($dataFound === true && isset($this->all_flights[$id]['hex'])) {
744
		if ($dataFound === true && isset($this->all_flights[$id]['id'])) {
745
		    $this->all_flights[$id]['lastupdate'] = time();
746
		    if ((!isset($globalNoImport) || $globalNoImport === FALSE) && $this->all_flights[$id]['addedSpotter'] == 0) {
747
		        if (!isset($globalDistanceIgnore['latitude']) || $this->all_flights[$id]['longitude'] == ''  || $this->all_flights[$id]['latitude'] == '' || (isset($globalDistanceIgnore['latitude']) && $Common->distance($this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude']) < $globalDistanceIgnore['distance'])) {
748
			    //print_r($this->all_flights);
749
			    //echo $this->all_flights[$id]['id'].' - '.$this->all_flights[$id]['addedSpotter']."\n";
750
			    //$last_hour_ident = Spotter->getIdentFromLastHour($this->all_flights[$id]['ident']);
751
			    if (!isset($this->all_flights[$id]['forcenew']) || $this->all_flights[$id]['forcenew'] == 0) {
752
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
753
				    if ($globalDebug) echo "Check if aircraft is already in DB...";
754
				    $timeelapsed = microtime(true);
755
				    $SpotterLive = new SpotterLive($this->db);
756
				    if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'aircraftjson' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'planeupdatefaa' || $line['format_source'] === 'aprs' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'radarvirtueljson' || $line['format_source'] === 'famaprs')) {
757
					$recent_ident = $SpotterLive->checkModeSRecent($this->all_flights[$id]['hex']);
758
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkModeSRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
759
				    } elseif (isset($line['id'])) {
760
					$recent_ident = $SpotterLive->checkIdRecent($line['id']);
761
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
762
				    } elseif (isset($this->all_flights[$id]['ident']) && $this->all_flights[$id]['ident'] != '') {
763
					$recent_ident = $SpotterLive->checkIdentRecent($this->all_flights[$id]['ident']);
764
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
765
				    } else $recent_ident = '';
766
				    $SpotterLive->db=null;
767
				    if ($globalDebug && $recent_ident == '') echo " Not in DB.\n";
768
				    elseif ($globalDebug && $recent_ident != '') echo " Already in DB.\n";
769
				} else $recent_ident = '';
770
			    } else {
771
				$recent_ident = '';
772
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 0));
773
			    }
774
			    //if there was no aircraft with the same callsign within the last hour and go post it into the archive
775
			    if($recent_ident == "")
776
			    {
777
				if ($globalDebug) echo "\o/ Add ".$this->all_flights[$id]['ident']." in archive DB : ";
778
				if ($this->all_flights[$id]['departure_airport'] == "") { $this->all_flights[$id]['departure_airport'] = "NA"; }
779
				if ($this->all_flights[$id]['arrival_airport'] == "") { $this->all_flights[$id]['arrival_airport'] = "NA"; }
780
				//adds the spotter data for the archive
781
				$ignoreImport = false;
782
				foreach($globalAirportIgnore as $airportIgnore) {
783
				    if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
784
					$ignoreImport = true;
785
				    }
786
				}
787
				if (count($globalAirportAccept) > 0) {
788
				    $ignoreImport = true;
789
				    foreach($globalAirportIgnore as $airportIgnore) {
790
					if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
791
					    $ignoreImport = false;
792
					}
793
				    }
794
				}
795
				if (isset($globalAirlineIgnore) && is_array($globalAirlineIgnore)) {
796
				    foreach($globalAirlineIgnore as $airlineIgnore) {
797
					if ((is_numeric(substr(substr($this->all_flights[$id]['ident'],0,4),-1,1)) && substr($this->all_flights[$id]['ident'],0,3) == $airlineIgnore) || (is_numeric(substr(substr($this->all_flights[$id]['ident'],0,3),-1,1)) && substr($this->all_flights[$id]['ident'],0,2) == $airlineIgnore)) {
798
					    $ignoreImport = true;
799
					}
800
				    }
801
				}
802
				if (isset($globalAirlineAccept) && count($globalAirlineAccept) > 0) {
803
				    $ignoreImport = true;
804
				    foreach($globalAirlineAccept as $airlineAccept) {
805
					if ((is_numeric(substr(substr($this->all_flights[$id]['ident'],0,4),-1,1)) && substr($this->all_flights[$id]['ident'],0,3) == $airlineAccept) || (is_numeric(substr(substr($this->all_flights[$id]['ident'],0,3),-1,1)) && substr($this->all_flights[$id]['ident'],0,2) == $airlineAccept)) {
806
					    $ignoreImport = false;
807
					}
808
				    }
809
				}
810
				if (isset($globalPilotIdAccept) && count($globalPilotIdAccept) > 0) {
811
				    $ignoreImport = true;
812
				    foreach($globalPilotIdAccept as $pilotIdAccept) {
813
					if ($this->all_flights[$id]['pilot_id'] == $pilotIdAccept) {
814
					    $ignoreImport = false;
815
					}
816
				    }
817
				}
818
				
819
				if (!$ignoreImport) {
820
				    $highlight = '';
821
				    if ($this->all_flights[$id]['squawk'] == '7500') $highlight = 'Squawk 7500 : Hijack';
822
				    if ($this->all_flights[$id]['squawk'] == '7600') $highlight = 'Squawk 7600 : Lost Comm (radio failure)';
823
				    if ($this->all_flights[$id]['squawk'] == '7700') $highlight = 'Squawk 7700 : Emergency';
824
				    if (!isset($this->all_flights[$id]['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.date('YmdHi')));
825
				    $timeelapsed = microtime(true);
826
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
827
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
828
					    $Spotter = new Spotter($this->db);
829
					    $result = $Spotter->addSpotterData($this->all_flights[$id]['id'], $this->all_flights[$id]['ident'], $this->all_flights[$id]['aircraft_icao'], $this->all_flights[$id]['departure_airport'], $this->all_flights[$id]['arrival_airport'], $this->all_flights[$id]['latitude'], $this->all_flights[$id]['longitude'], $this->all_flights[$id]['waypoints'], $this->all_flights[$id]['altitude'],$this->all_flights[$id]['altitude_real'], $this->all_flights[$id]['heading'], $this->all_flights[$id]['speed'], $this->all_flights[$id]['datetime'], $this->all_flights[$id]['departure_airport_time'], $this->all_flights[$id]['arrival_airport_time'],$this->all_flights[$id]['squawk'],$this->all_flights[$id]['route_stop'],$highlight,$this->all_flights[$id]['hex'],$this->all_flights[$id]['registration'],$this->all_flights[$id]['pilot_id'],$this->all_flights[$id]['pilot_name'],$this->all_flights[$id]['verticalrate'],$this->all_flights[$id]['ground'],$this->all_flights[$id]['format_source'],$this->all_flights[$id]['source_name'],$this->all_flights[$id]['source_type']);
830
					    $Spotter->db = null;
831
					    if ($globalDebug && isset($result)) echo $result."\n";
832
					}
833
				    }
834
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update addspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
835
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
836
837
				    // Add source stat in DB
838
				    $Stats = new Stats($this->db);
839
				    if (!empty($this->stats)) {
840
					if ($globalDebug) echo 'Add source stats : ';
841
				        foreach($this->stats as $date => $data) {
842
					    foreach($data as $source => $sourced) {
843
					        //print_r($sourced);
844
				    	        if (isset($sourced['polar'])) echo $Stats->addStatSource(json_encode($sourced['polar']),$source,'polar',$date);
845
				    	        if (isset($sourced['hist'])) echo $Stats->addStatSource(json_encode($sourced['hist']),$source,'hist',$date);
846
				    		if (isset($sourced['msg'])) {
847
				    		    if (time() - $sourced['msg']['date'] > 10) {
848
				    		        $nbmsg = round($sourced['msg']['nb']/(time() - $sourced['msg']['date']));
849
				    		        echo $Stats->addStatSource($nbmsg,$source,'msg',$date);
850
			    			        unset($this->stats[$date][$source]['msg']);
851
			    			    }
852
			    			}
853
			    		    }
854
			    		    if ($date != date('Y-m-d')) {
855
			    			unset($this->stats[$date]);
856
			    		    }
857
				    	}
858
				    	if ($globalDebug) echo 'Done'."\n";
859
860
				    }
861
				    $Stats->db = null;
862
				    }
863
				    $this->del();
864
				} elseif ($globalDebug) echo 'Ignore data'."\n";
865
				//$ignoreImport = false;
866
				$this->all_flights[$id]['addedSpotter'] = 1;
867
				//print_r($this->all_flights[$id]);
868
			/*
869
			if (isset($globalArchive) && $globalArchive) {
870
			    $archives_ident = SpotterLive->getAllLiveSpotterDataByIdent($this->all_flights[$id]['ident']);
871
			    foreach ($archives_ident as $archive) {
872
				SpotterArchive->addSpotterArchiveData($archive['flightaware_id'], $archive['ident'], $archive['registration'],$archive['airline_name'],$archive['airline_icao'],$archive['airline_country'],$archive['airline_type'],$archive['aircraft_icao'],$archive['aircraft_shadow'],$archive['aircraft_name'],$archive['aircraft_manufacturer'], $archive['departure_airport_icao'],$archive['departure_airport_name'],$archive['departure_airport_city'],$archive['departure_airport_country'],$archive['departure_airport_time'],
873
				$archive['arrival_airport_icao'],$archive['arrival_airport_name'],$archive['arrival_airport_city'],$archive['arrival_airport_country'],$archive['arrival_airport_time'],
874
				$archive['route_stop'],$archive['date'],$archive['latitude'], $archive['longitude'], $archive['waypoints'], $archive['altitude'], $archive['heading'], $archive['ground_speed'],
875
				$archive['squawk'],$archive['ModeS']);
876
			    }
877
			}
878
			*/
879
			//SpotterLive->deleteLiveSpotterDataByIdent($this->all_flights[$id]['ident']);
880
				if ($this->last_delete == 0 || time() - $this->last_delete > 1800) {
881
				    if ($globalDebug) echo "---- Deleting Live Spotter data older than 9 hours...";
882
				    //SpotterLive->deleteLiveSpotterDataNotUpdated();
883
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
884
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
885
					    $SpotterLive = new SpotterLive($this->db);
886
					    $SpotterLive->deleteLiveSpotterData();
887
					    $SpotterLive->db=null;
888
					}
889
				    }
890
				    if ($globalDebug) echo " Done\n";
891
				    $this->last_delete = time();
892
				}
893
			    } else {
894
				if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'aircraftjson' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt'|| $line['format_source'] === 'planeupdatefaa'  || $line['format_source'] === 'aprs' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'famaprs' || $line['format_source'] === 'airwhere')) {
895
				    $this->all_flights[$id]['id'] = $recent_ident;
896
				    $this->all_flights[$id]['addedSpotter'] = 1;
897
				}
898
				if (isset($globalDaemon) && !$globalDaemon) {
899
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
900
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
901
					    $Spotter = new Spotter($this->db);
902
					    $Spotter->updateLatestSpotterData($this->all_flights[$id]['id'],$this->all_flights[$id]['ident'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],$this->all_flights[$id]['altitude'],$this->all_flights[$id]['altitude_real'],$this->all_flights[$id]['ground'],$this->all_flights[$id]['speed'],$this->all_flights[$id]['datetime'],$this->all_flights[$id]['arrival_airport'],$this->all_flights[$id]['arrival_airport_time']);
903
					    $Spotter->db = null;
904
					}
905
				    }
906
				}
907
				
908
			    }
909
			}
910
		    }
911
		    //adds the spotter LIVE data
912
		    //SpotterLive->addLiveSpotterData($flightaware_id, $ident, $aircraft_type, $departure_airport, $arrival_airport, $latitude, $longitude, $waypoints, $altitude, $heading, $groundspeed);
913
		    //echo "\nAdd in Live !! \n";
914
		    //echo "{$line[8]} {$line[7]} - MODES:{$line[4]}  CALLSIGN:{$line[10]}   ALT:{$line[11]}   VEL:{$line[12]}   HDG:{$line[13]}   LAT:{$line[14]}   LON:{$line[15]}   VR:{$line[16]}   SQUAWK:{$line[17]}\n";
915
		    if ($globalDebug) {
916
			if ((isset($globalVA) && $globalVA) || (isset($globalIVAO) && $globalIVAO) || (isset($globalVATSIM) && $globalVATSIM) || (isset($globalphpVMS) && $globalphpVMS) || (isset($globalVAM) && $globalVAM)) {
917
				if (isset($this->all_flights[$id]['source_name'])) echo 'DATA : hex : '.$this->all_flights[$id]['hex'].' - ident : '.$this->all_flights[$id]['ident'].' - ICAO : '.$this->all_flights[$id]['aircraft_icao'].' - Departure Airport : '.$this->all_flights[$id]['departure_airport'].' - Arrival Airport : '.$this->all_flights[$id]['arrival_airport'].' - Latitude : '.$this->all_flights[$id]['latitude'].' - Longitude : '.$this->all_flights[$id]['longitude'].' - waypoints : '.$this->all_flights[$id]['waypoints'].' - Altitude : '.$this->all_flights[$id]['altitude'].' - Heading : '.$this->all_flights[$id]['heading'].' - Speed : '.$this->all_flights[$id]['speed'].' - Departure Airport Time : '.$this->all_flights[$id]['departure_airport_time'].' - Arrival Airport time : '.$this->all_flights[$id]['arrival_airport_time'].' - Pilot : '.$this->all_flights[$id]['pilot_name'].' - Source name : '.$this->all_flights[$id]['source_name']."\n";
918
				else echo 'DATA : hex : '.$this->all_flights[$id]['hex'].' - ident : '.$this->all_flights[$id]['ident'].' - ICAO : '.$this->all_flights[$id]['aircraft_icao'].' - Departure Airport : '.$this->all_flights[$id]['departure_airport'].' - Arrival Airport : '.$this->all_flights[$id]['arrival_airport'].' - Latitude : '.$this->all_flights[$id]['latitude'].' - Longitude : '.$this->all_flights[$id]['longitude'].' - waypoints : '.$this->all_flights[$id]['waypoints'].' - Altitude : '.$this->all_flights[$id]['altitude'].' - Heading : '.$this->all_flights[$id]['heading'].' - Speed : '.$this->all_flights[$id]['speed'].' - Departure Airport Time : '.$this->all_flights[$id]['departure_airport_time'].' - Arrival Airport time : '.$this->all_flights[$id]['arrival_airport_time'].' - Pilot : '.$this->all_flights[$id]['pilot_name']."\n";
919
			} else {
920
				if (isset($this->all_flights[$id]['source_name'])) echo 'DATA : hex : '.$this->all_flights[$id]['hex'].' - ident : '.$this->all_flights[$id]['ident'].' - ICAO : '.$this->all_flights[$id]['aircraft_icao'].' - Departure Airport : '.$this->all_flights[$id]['departure_airport'].' - Arrival Airport : '.$this->all_flights[$id]['arrival_airport'].' - Latitude : '.$this->all_flights[$id]['latitude'].' - Longitude : '.$this->all_flights[$id]['longitude'].' - waypoints : '.$this->all_flights[$id]['waypoints'].' - Altitude : '.$this->all_flights[$id]['altitude'].' - Heading : '.$this->all_flights[$id]['heading'].' - Speed : '.$this->all_flights[$id]['speed'].' - Departure Airport Time : '.$this->all_flights[$id]['departure_airport_time'].' - Arrival Airport time : '.$this->all_flights[$id]['arrival_airport_time'].' - Source Name : '.$this->all_flights[$id]['source_name']."\n";
921
				else echo 'DATA : hex : '.$this->all_flights[$id]['hex'].' - ident : '.$this->all_flights[$id]['ident'].' - ICAO : '.$this->all_flights[$id]['aircraft_icao'].' - Departure Airport : '.$this->all_flights[$id]['departure_airport'].' - Arrival Airport : '.$this->all_flights[$id]['arrival_airport'].' - Latitude : '.$this->all_flights[$id]['latitude'].' - Longitude : '.$this->all_flights[$id]['longitude'].' - waypoints : '.$this->all_flights[$id]['waypoints'].' - Altitude : '.$this->all_flights[$id]['altitude'].' - Heading : '.$this->all_flights[$id]['heading'].' - Speed : '.$this->all_flights[$id]['speed'].' - Departure Airport Time : '.$this->all_flights[$id]['departure_airport_time'].' - Arrival Airport time : '.$this->all_flights[$id]['arrival_airport_time']."\n";
922
			}
923
		    }
924
		    $ignoreImport = false;
925
		    if ($this->all_flights[$id]['departure_airport'] == "") { $this->all_flights[$id]['departure_airport'] = "NA"; }
926
		    if ($this->all_flights[$id]['arrival_airport'] == "") { $this->all_flights[$id]['arrival_airport'] = "NA"; }
927
928
		    foreach($globalAirportIgnore as $airportIgnore) {
929
		        if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
930
			    $ignoreImport = true;
931
			}
932
		    }
933
		    if (count($globalAirportAccept) > 0) {
934
		        $ignoreImport = true;
935
		        foreach($globalAirportIgnore as $airportIgnore) {
936
			    if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
937
				$ignoreImport = false;
938
			    }
939
			}
940
		    }
941
		    if (isset($globalAirlineIgnore) && is_array($globalAirlineIgnore)) {
942
			foreach($globalAirlineIgnore as $airlineIgnore) {
943
			    if ((is_numeric(substr(substr($this->all_flights[$id]['ident'],0,4),-1,1)) && substr($this->all_flights[$id]['ident'],0,3) == $airlineIgnore) || (is_numeric(substr(substr($this->all_flights[$id]['ident'],0,3),-1,1)) && substr($this->all_flights[$id]['ident'],0,2) == $airlineIgnore)) {
944
				$ignoreImport = true;
945
			    }
946
			}
947
		    }
948
		    if (isset($globalAirlineAccept) && count($globalAirlineAccept) > 0) {
949
			$ignoreImport = true;
950
			foreach($globalAirlineAccept as $airlineAccept) {
951
			    if ((is_numeric(substr(substr($this->all_flights[$id]['ident'],0,4),-1,1)) && substr($this->all_flights[$id]['ident'],0,3) == $airlineAccept) || (is_numeric(substr(substr($this->all_flights[$id]['ident'],0,3),-1,1)) && substr($this->all_flights[$id]['ident'],0,2) == $airlineAccept)) {
952
				$ignoreImport = false;
953
			    }
954
			}
955
		    }
956
		    if (isset($globalPilotIdAccept) && count($globalPilotIdAccept) > 0) {
957
			$ignoreImport = true;
958
			foreach($globalPilotIdAccept as $pilotIdAccept) {
959
			    if ($this->all_flights[$id]['pilot_id'] == $pilotIdAccept) {
960
			        $ignoreImport = false;
961
			    }
962
			}
963
		    }
964
965
		    if (!$ignoreImport) {
966
			if (!isset($globalDistanceIgnore['latitude']) || (isset($globalDistanceIgnore['latitude']) && $Common->distance($this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude']) < $globalDistanceIgnore['distance'])) {
967
				if (!isset($this->all_flights[$id]['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.date('YmdHi')));
968
				$timeelapsed = microtime(true);
969
				if (!isset($globalNoImport) || $globalNoImport === FALSE) {
970
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
971
					if ($globalDebug) echo "\o/ Add ".$this->all_flights[$id]['ident']." from ".$this->all_flights[$id]['format_source']." in Live DB : ";
972
					$SpotterLive = new SpotterLive($this->db);
973
					$result = $SpotterLive->addLiveSpotterData($this->all_flights[$id]['id'], $this->all_flights[$id]['ident'], $this->all_flights[$id]['aircraft_icao'], $this->all_flights[$id]['departure_airport'], $this->all_flights[$id]['arrival_airport'], $this->all_flights[$id]['latitude'], $this->all_flights[$id]['longitude'], $this->all_flights[$id]['waypoints'], $this->all_flights[$id]['altitude'],$this->all_flights[$id]['altitude_real'], $this->all_flights[$id]['heading'], $this->all_flights[$id]['speed'],$this->all_flights[$id]['datetime'], $this->all_flights[$id]['departure_airport_time'], $this->all_flights[$id]['arrival_airport_time'], $this->all_flights[$id]['squawk'],$this->all_flights[$id]['route_stop'],$this->all_flights[$id]['hex'],$this->all_flights[$id]['putinarchive'],$this->all_flights[$id]['registration'],$this->all_flights[$id]['pilot_id'],$this->all_flights[$id]['pilot_name'], $this->all_flights[$id]['verticalrate'], $this->all_flights[$id]['noarchive'], $this->all_flights[$id]['ground'],$this->all_flights[$id]['format_source'],$this->all_flights[$id]['source_name'],$this->all_flights[$id]['over_country']);
974
					$SpotterLive->db = null;
975
					if ($globalDebug) echo $result."\n";
976
				    }
977
				}
978
				if (isset($globalServerAPRS) && $globalServerAPRS && $this->all_flights[$id]['putinarchive']) {
979
					$APRSSpotter->addLiveSpotterData($this->all_flights[$id]['id'], $this->all_flights[$id]['ident'], $this->all_flights[$id]['aircraft_icao'], $this->all_flights[$id]['departure_airport'], $this->all_flights[$id]['arrival_airport'], $this->all_flights[$id]['latitude'], $this->all_flights[$id]['longitude'], $this->all_flights[$id]['waypoints'], $this->all_flights[$id]['altitude'], $this->all_flights[$id]['altitude_real'], $this->all_flights[$id]['heading'], $this->all_flights[$id]['speed'],$this->all_flights[$id]['datetime'], $this->all_flights[$id]['departure_airport_time'], $this->all_flights[$id]['arrival_airport_time'], $this->all_flights[$id]['squawk'],$this->all_flights[$id]['route_stop'],$this->all_flights[$id]['hex'],$this->all_flights[$id]['putinarchive'],$this->all_flights[$id]['registration'],$this->all_flights[$id]['pilot_id'],$this->all_flights[$id]['pilot_name'], $this->all_flights[$id]['verticalrate'], $this->all_flights[$id]['noarchive'], $this->all_flights[$id]['ground'],$this->all_flights[$id]['format_source'],$this->all_flights[$id]['source_name'],$this->all_flights[$id]['over_country']);
980
				}
981
				$this->all_flights[$id]['putinarchive'] = false;
982
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update addlivespotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
983
984
				// Put statistics in $this->stats variable
985
				//if ($line['format_source'] != 'aprs') {
986
				//if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt')) {
987
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
988
				    if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
989
					$source = $this->all_flights[$id]['source_name'];
990
					if ($source == '') $source = $this->all_flights[$id]['format_source'];
991
					if (!isset($this->source_location[$source])) {
992
						$Location = new Source($this->db);
993
						$coord = $Location->getLocationInfobySourceName($source);
994
						if (count($coord) > 0) {
995
							$latitude = $coord[0]['latitude'];
996
							$longitude = $coord[0]['longitude'];
997
						} else {
998
							$latitude = $globalCenterLatitude;
999
							$longitude = $globalCenterLongitude;
1000
						}
1001
						$this->source_location[$source] = array('latitude' => $latitude,'longitude' => $longitude);
1002
					} else {
1003
						$latitude = $this->source_location[$source]['latitude'];
1004
						$longitude = $this->source_location[$source]['longitude'];
1005
					}
1006
					$stats_heading = $Common->getHeading($latitude,$longitude,$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude']);
1007
					//$stats_heading = $stats_heading%22.5;
1008
					$stats_heading = round($stats_heading/22.5);
1009
					$stats_distance = $Common->distance($latitude,$longitude,$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude']);
1010
					$current_date = date('Y-m-d');
1011
					if ($stats_heading == 16) $stats_heading = 0;
1012
					if (!isset($this->stats[$current_date][$source]['polar'][1])) {
1013
						for ($i=0;$i<=15;$i++) {
1014
						    $this->stats[$current_date][$source]['polar'][$i] = 0;
1015
						}
1016
						$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
1017
					} else {
1018
						if ($this->stats[$current_date][$source]['polar'][$stats_heading] < $stats_distance) {
1019
							$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
1020
						}
1021
					}
1022
					$distance = (round($stats_distance/10)*10);
1023
					//echo '$$$$$$$$$$ DISTANCE : '.$distance.' - '.$source."\n";
1024
					//var_dump($this->stats);
1025
					if (!isset($this->stats[$current_date][$source]['hist'][$distance])) {
1026
						if (isset($this->stats[$current_date][$source]['hist'][0])) {
1027
						    end($this->stats[$current_date][$source]['hist']);
1028
						    $mini = key($this->stats[$current_date][$source]['hist'])+10;
1029
						} else $mini = 0;
1030
						for ($i=$mini;$i<=$distance;$i+=10) {
1031
						    $this->stats[$current_date][$source]['hist'][$i] = 0;
1032
						}
1033
						$this->stats[$current_date][$source]['hist'][$distance] = 1;
1034
					} else {
1035
						$this->stats[$current_date][$source]['hist'][$distance] += 1;
1036
					}
1037
				    }
1038
				}
1039
1040
				$this->all_flights[$id]['lastupdate'] = time();
1041
				if ($this->all_flights[$id]['putinarchive']) $send = true;
1042
				//if ($globalDebug) echo "Distance : ".Common->distance($this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude'])."\n";
1043
			} elseif (isset($this->all_flights[$id]['latitude']) && isset($globalDistanceIgnore['latitude']) && $globalDebug) echo "!! Too far -> Distance : ".$Common->distance($this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude'])."\n";
1044
			//$this->del();
1045
			
1046
			if ($this->last_delete_hourly == 0 || time() - $this->last_delete_hourly > 900) {
1047
			    if ((!isset($globalNoImport) || $globalNoImport === FALSE) && (!isset($globalNoDB) || $globalNoDB !== TRUE)) {
1048
				if ($globalDebug) echo "---- Deleting Live Spotter data Not updated since 2 hour...";
1049
				$SpotterLive = new SpotterLive($this->db);
1050
				$SpotterLive->deleteLiveSpotterDataNotUpdated();
1051
				$SpotterLive->db = null;
1052
				//SpotterLive->deleteLiveSpotterData();
1053
				if ($globalDebug) echo " Done\n";
1054
				$this->last_delete_hourly = time();
1055
			    } else {
1056
				$this->del();
1057
				$this->last_delete_hourly = time();
1058
			    }
1059
			}
1060
			
1061
		    }
1062
		    //$ignoreImport = false;
1063
		}
1064
		//if (function_exists('pcntl_fork') && $globalFork) pcntl_signal(SIGCHLD, SIG_IGN);
1065
		if ($send) return $this->all_flights[$id];
1066
	    }
1067
	}
1068
    }
1069
}
1070
?>
1071