Completed
Push — master ( 75e641...0485dc )
by Yannick
33:10
created

SpotterImport::delKey()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 17
nc 13
nop 1
dl 0
loc 22
rs 5.8579
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
			$SpotterLive = new SpotterLive($this->db);
212
			if ($this->all_flights[$key]['latitude'] != '' && $this->all_flights[$key]['longitude'] != '') {
213
				$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']);
214
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
215
				$this->all_flights[$key]['putinarchive'] = true;
216
				$result = $SpotterLive->addLiveSpotterData($this->all_flights[$key]['id'], $this->all_flights[$key]['ident'], $this->all_flights[$key]['aircraft_icao'], $this->all_flights[$key]['departure_airport'], $this->all_flights[$key]['arrival_airport'], $this->all_flights[$key]['latitude'], $this->all_flights[$key]['longitude'], $this->all_flights[$key]['waypoints'], $this->all_flights[$key]['altitude'],$this->all_flights[$key]['altitude_real'], $this->all_flights[$key]['heading'], $this->all_flights[$key]['speed'],$this->all_flights[$key]['datetime'], $this->all_flights[$key]['departure_airport_time'], $this->all_flights[$key]['arrival_airport_time'], $this->all_flights[$key]['squawk'],$this->all_flights[$key]['route_stop'],$this->all_flights[$key]['hex'],$this->all_flights[$key]['putinarchive'],$this->all_flights[$key]['registration'],$this->all_flights[$key]['pilot_id'],$this->all_flights[$key]['pilot_name'], $this->all_flights[$key]['verticalrate'], $this->all_flights[$key]['noarchive'], $this->all_flights[$key]['ground'],$this->all_flights[$key]['format_source'],$this->all_flights[$key]['source_name'],$this->all_flights[$key]['over_country']);
217
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
218
			}
219
			$Spotter->db = null;
220
			$SpotterLive->db = null;
221
		}
222
	}
223
	unset($this->all_flights[$key]);
224
    }
225
226
    public function add($line) {
227
	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;
228
	//if (!isset($globalDebugTimeElapsed) || $globalDebugTimeElapsed == '') $globalDebugTimeElapsed = FALSE;
229
	if (!isset($globalCoordMinChange) || $globalCoordMinChange == '') $globalCoordMinChange = '0.01';
230
	if (!isset($globalAircraftMaxUpdate) || $globalAircraftMaxUpdate == '') $globalAircraftMaxUpdate = 3000;
231
/*
232
	$Spotter = new Spotter();
233
	$dbc = $Spotter->db;
234
	$SpotterLive = new SpotterLive($dbc);
235
	$Common = new Common();
236
	$Schedule = new Schedule($dbc);
237
*/
238
	date_default_timezone_set('UTC');
239
	// signal handler - playing nice with sockets and dump1090
240
	// pcntl_signal_dispatch();
241
242
	// get the time (so we can figure the timeout)
243
	//$time = time();
244
245
	//pcntl_signal_dispatch();
246
	$dataFound = false;
247
	//$putinarchive = false;
248
	$send = false;
249
	
250
	// SBS format is CSV format
251
	if(is_array($line) && (isset($line['hex']) || isset($line['id']))) {
252
	    //print_r($line);
253
	    if (isset($line['hex'])) $line['hex'] = strtoupper($line['hex']);
254
  	    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)) {
255
256
		// Increment message number
257
		if (isset($line['sourcestats']) && $line['sourcestats'] === TRUE) {
258
		    $current_date = date('Y-m-d');
259
		    if (isset($line['source_name'])) $source = $line['source_name'];
260
		    else $source = '';
261
		    if ($source == '' || $line['format_source'] == 'aprs') $source = $line['format_source'];
262
		    if (!isset($this->stats[$current_date][$source]['msg'])) {
263
		    	$this->stats[$current_date][$source]['msg']['date'] = time();
264
		    	$this->stats[$current_date][$source]['msg']['nb'] = 1;
265
		    } else $this->stats[$current_date][$source]['msg']['nb'] += 1;
266
		}
267
		
268
		/*
269
		$dbc = $this->db;
270
		$Connection = new Connection($dbc);
271
		$Connection->connectionExists();
272
		$dbc = $Connection->db;
273
		*/
274
		//$Spotter = new Spotter($dbc);
275
		//$SpotterLive = new SpotterLive($dbc);
276
		$Common = new Common();
277
//		echo $this->nb++."\n";
278
		//$this->db = $dbc;
279
280
		//$hex = trim($line['hex']);
281
	        if (!isset($line['id'])) $id = trim($line['hex']);
282
	        else $id = trim($line['id']);
283
		
284
		if (!isset($this->all_flights[$id])) {
285
		    if ($globalDebug) echo 'New flight...'."\n";
286
		    $this->all_flights[$id] = array();
287
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
288
		    $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' => ''));
289
		    if (isset($globalDaemon) && $globalDaemon === FALSE) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('lastupdate' => time()));
290
		    if (!isset($line['id'])) {
291
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
292
//			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')));
293
//			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')));
294
			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')));
295
		        //else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
296
		     } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
297
		    if ($globalAllFlights !== FALSE) $dataFound = true;
298
		}
299
		if (isset($line['source_type']) && $line['source_type'] != '') {
300
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('source_type' => $line['source_type']));
301
		}
302
		
303
		//print_r($this->all_flights);
304
		if (isset($line['hex']) && !isset($this->all_flights[$id]['hex']) && ctype_xdigit($line['hex'])) {
305
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('hex' => trim($line['hex'])));
306
		    //if (isset($line['datetime']) && preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) {
307
			//$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => $line['datetime']));
308
		    //} else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => date('Y-m-d H:i:s')));
309
		    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') {
310
			$timeelapsed = microtime(true);
311
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
312
			    $Spotter = new Spotter($this->db);
313
			    if (isset($this->all_flights[$id]['source_type'])) {
314
				$aircraft_icao = $Spotter->getAllAircraftType(trim($line['hex']),$this->all_flights[$id]['source_type']);
315
			    } else {
316
				$aircraft_icao = $Spotter->getAllAircraftType(trim($line['hex']));
317
			    }
318
			    $Spotter->db = null;
319
			    if ($globalDebugTimeElapsed) echo 'Time elapsed for update getallaircrattype : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
320
			    if ($aircraft_icao != '') $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
321
			}
322
		    }
323
		    if ($globalAllFlights !== FALSE) $dataFound = true;
324
		    if ($globalDebug) echo "*********** New aircraft hex : ".$line['hex']." ***********\n";
325
		}
326
	        if (isset($line['id']) && !isset($line['hex'])) {
327
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('hex' => ''));
328
	        }
329
		if (isset($line['aircraft_icao']) && $line['aircraft_icao'] != '') {
330
			$icao = $line['aircraft_icao'];
331
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
332
				$Spotter = new Spotter($this->db);
333
				if (isset($Spotter->aircraft_correct_icaotype[$icao])) $icao = $Spotter->aircraft_correct_icaotype[$icao];
334
				$Spotter->db = null;
335
			}
336
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $icao));
337
		} elseif (!isset($this->all_flights[$id]['aircraft_icao']) && isset($line['aircraft_name'])) {
338
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
339
				// Get aircraft ICAO from aircraft name
340
				$Spotter = new Spotter($this->db);
341
				$aircraft_icao = $Spotter->getAircraftIcao($line['aircraft_name']);
342
				$Spotter->db = null;
343
				if ($aircraft_icao != '') $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
344
			}
345
		}
346
		if (!isset($this->all_flights[$id]['aircraft_icao']) && isset($line['aircraft_type'])) {
347
			if ($line['aircraft_type'] == 'PARA_GLIDER') $aircraft_icao = 'GLID';
348
			elseif ($line['aircraft_type'] == 'HELICOPTER_ROTORCRAFT') $aircraft_icao = 'UHEL';
349
			elseif ($line['aircraft_type'] == 'TOW_PLANE') $aircraft_icao = 'TOWPLANE';
350
			elseif ($line['aircraft_type'] == 'POWERED_AIRCRAFT') $aircraft_icao = 'POWAIRC';
351
			if (isset($aircraft_icao)) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
352
		}
353
		if (!isset($this->all_flights[$id]['aircraft_icao'])) {
354
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => 'NA'));
355
		}
356
		//if (isset($line['datetime']) && preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) {
357
		if (isset($line['datetime']) && strtotime($line['datetime']) > time()-20*60 && strtotime($line['datetime']) < time()+20*60) {
358
		    if (!isset($this->all_flights[$id]['datetime']) || strtotime($line['datetime']) >= strtotime($this->all_flights[$id]['datetime'])) {
359
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => $line['datetime']));
360
		    } else {
361
				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";
362
				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";
363
				/*
364
				echo strtotime($line['datetime']).' > '.strtotime($this->all_flights[$id]['datetime']);
365
				print_r($this->all_flights[$id]);
366
				print_r($line);
367
				*/
368
				return '';
369
		    }
370
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) < time()-20*60) {
371
			if ($globalDebug) echo "!!! Date is too old ".$line['datetime']." for ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."!!!\n";
372
			return '';
373
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) > time()+20*60) {
374
			if ($globalDebug) echo "!!! Date is in the future ".$line['datetime']." for ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."!!!\n";
375
			return '';
376
		} elseif (!isset($line['datetime'])) {
377
			date_default_timezone_set('UTC');
378
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => date('Y-m-d H:i:s')));
379
		} else {
380
			if ($globalDebug) echo "!!! Unknow date error ".$this->all_flights[$id]['hex']." - format : ".$line['format_source']."!!!";
381
			return '';
382
		}
383
384
		if (isset($line['registration']) && $line['registration'] != '' && $line['registration'] != 'z.NO-REG' && $line['registration'] != 'NA') {
385
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('registration' => $line['registration']));
386
		}
387
		if (isset($line['waypoints']) && $line['waypoints'] != '') {
388
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('waypoints' => $line['waypoints']));
389
		}
390
		if (isset($line['pilot_id']) && $line['pilot_id'] != '') {
391
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('pilot_id' => trim($line['pilot_id'])));
392
		}
393
		if (isset($line['pilot_name']) && $line['pilot_name'] != '') {
394
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('pilot_name' => trim($line['pilot_name'])));
395
		}
396
 
397
		if (isset($line['ident']) && trim($line['ident']) != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_flights[$id]['ident'] != trim($line['ident'])) && preg_match('/^[a-zA-Z0-9]+$/', $line['ident'])) {
398
399
		    if ($this->all_flights[$id]['addedSpotter'] == 1) {
400
			if ($globalVA !== TRUE && $globalIVAO !== TRUE && $globalVATSIM !== TRUE && $globalphpVMS !== TRUE && $globalVAM !== TRUE && $this->all_flights[$id]['lastupdate'] < time() - 1600) {
401
				if ($globalDebug) echo '---!!!! New ident, reset aircraft data...'."\n";
402
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
403
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 1));
404
				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')));
405
				elseif (isset($line['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
406
				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']));
407
			} else {
408
			    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ident' => trim($line['ident'])));
409
			    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
410
				$timeelapsed = microtime(true);
411
            			$Spotter = new Spotter($this->db);
412
            			$fromsource = NULL;
413
            			if (isset($globalAirlinesSource) && $globalAirlinesSource != '') $fromsource = $globalAirlinesSource;
414
            			elseif (isset($line['format_source']) && $line['format_source'] == 'vatsimtxt') $fromsource = 'vatsim';
415
				elseif (isset($line['format_source']) && $line['format_source'] == 'whazzup') $fromsource = 'ivao';
416
				elseif (isset($globalVATSIM) && $globalVATSIM) $fromsource = 'vatsim';
417
				elseif (isset($globalIVAO) && $globalIVAO) $fromsource = 'ivao';
418
            			$result = $Spotter->updateIdentSpotterData($this->all_flights[$id]['id'],$this->all_flights[$id]['ident'],$fromsource);
419
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
420
				$Spotter->db = null;
421
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update identspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
422
			    }
423
			}
424
		    } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ident' => trim($line['ident'])));
425
		    
426
/*
427
		    if (!isset($line['id'])) {
428
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
429
//			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')));
430
			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')));
431
		        else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
432
		     } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
433
  */
434
		    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']));
435
436
		    //$putinarchive = true;
437
		    if (isset($line['departure_airport_time']) && $line['departure_airport_time'] != 0) {
438
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport_time' => $line['departure_airport_time']));
439
		    }
440
		    if (isset($line['arrival_airport_time']) && $line['arrival_airport_time'] != 0) {
441
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('arrival_airport_time' => $line['arrival_airport_time']));
442
		    }
443
		    if (isset($line['departure_airport_icao']) && isset($line['arrival_airport_icao'])) {
444
		    		$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' => ''));
445
		    } elseif (isset($line['departure_airport_iata']) && isset($line['arrival_airport_iata'])) {
446
			$timeelapsed = microtime(true);
447
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
448
				$Spotter = new Spotter($this->db);
449
				$line['departure_airport_icao'] = $Spotter->getAirportIcao($line['departure_airport_iata']);
450
				$line['arrival_airport_icao'] = $Spotter->getAirportIcao($line['arrival_airport_iata']);
451
		    		$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' => ''));
452
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update getAirportICAO : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
453
                        }
454
		    } elseif (!isset($line['format_source']) || $line['format_source'] != 'aprs') {
455
			$timeelapsed = microtime(true);
456
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
457
			    $Spotter = new Spotter($this->db);
458
			    $route = $Spotter->getRouteInfo(trim($line['ident']));
459
			    if (!isset($route['fromairport_icao']) && !isset($route['toairport_icao'])) {
460
				$Translation = new Translation($this->db);
461
				$ident = $Translation->checkTranslation(trim($line['ident']));
462
				$route = $Spotter->getRouteInfo($ident);
463
				$Translation->db = null;
464
			    }
465
			    $Spotter->db = null;
466
			    if ($globalDebugTimeElapsed) echo 'Time elapsed for update getrouteinfo : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
467
                    	}
468
			if (isset($route['fromairport_icao']) && isset($route['toairport_icao'])) {
469
			    //if ($route['FromAirport_ICAO'] != $route['ToAirport_ICAO']) {
470
			    if ($route['fromairport_icao'] != $route['toairport_icao']) {
471
				//    $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']));
472
		    		$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']));
473
		    	    }
474
			}
475
			if (!isset($globalFork)) $globalFork = TRUE;
476
			if (!$globalVA && !$globalIVAO && !$globalVATSIM && !$globalphpVMS && !$globalVAM && (!isset($line['format_source']) || $line['format_source'] != 'aprs')) {
477
				if (!isset($this->all_flights[$id]['schedule_check']) || $this->all_flights[$id]['schedule_check'] === false) $this->get_Schedule($id,trim($line['ident']));
478
			}
479
		    }
480
		}
481
482
		if (isset($line['speed']) && $line['speed'] != '' && $line['speed'] != 0) {
483
		//    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => $line[12]));
484
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => round($line['speed'])));
485
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed_fromsrc' => true));
486
		    //$dataFound = true;
487
		} 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'])) {
488
		    $distance = $Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m');
489
		    if ($distance > 1000 && $distance < 10000) {
490
		    // use datetime
491
			$speed = $distance/(time() - $this->all_flights[$id]['time_last_coord']);
492
			$speed = $speed*3.6;
493
			if ($speed < 1000) {
494
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => round($speed)));
495
	  			if ($globalDebug) echo "ø Calculated Speed for ".$this->all_flights[$id]['hex']." : ".round($speed)." - distance : ".$distance."\n";
496
	  		} else {
497
	  			if ($globalDebug) echo "ø IGNORED : Calculated Speed for ".$this->all_flights[$id]['hex']." : ".round($speed)." - distance : ".$distance."\n";
498
	  		}
499
		    }
500
		}
501
502
503
504
	        if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '' && is_numeric($line['latitude']) && is_numeric($line['longitude'])) {
505
	    	    if (ctype_digit(strval($line['latitude'])) || ctype_digit(strval($line['longitude']))) {
506
	    	    	if ($globalDebug) echo "/!\ Invalid latitude or/and longitude data : lat: ".$line['latitude']." - lng: ".$line['longitude']."\n";
507
	    	    	return false;
508
	    	    }
509
	    	    if (isset($this->all_flights[$id]['time_last_coord'])) $timediff = round(time()-$this->all_flights[$id]['time_last_coord']);
510
	    	    else unset($timediff);
511
	    	    if (isset($this->all_flights[$id]['time_last_archive_coord'])) $timediff_archive = round(time()-$this->all_flights[$id]['time_last_archive_coord']);
512
	    	    else unset($timediff_archive);
513
	    	    if ($this->tmd > 5
514
	    	        || (isset($line['format_source']) 
515
	    	    	    && $line['format_source'] == 'airwhere' 
516
	    	    	    && ((!isset($this->all_flights[$id]['latitude']) 
517
	    	    		|| !isset($this->all_flights[$id]['longitude'])) 
518
	    	    		|| (isset($this->all_flights[$id]['latitude']) 
519
	    	    		    && isset($this->all_flights[$id]['longitude']) 
520
	    	    		    && $this->all_flights[$id]['latitude'] != $line['latitude'] 
521
	    	    		    && $this->all_flights[$id]['longitude'] != $line['longitude']
522
	    	    		)
523
	    	    	    )
524
	    	    	)
525
	    		|| (isset($globalVA) && $globalVA) 
526
	    	    	|| (isset($globalIVAO) && $globalIVAO)
527
	    	    	|| (isset($globalVATSIM) && $globalVATSIM)
528
	    	    	|| (isset($globalphpVMS) && $globalphpVMS)
529
	    	    	|| (isset($globalVAM) && $globalVAM)
530
	    	    	|| !isset($timediff)
531
	    	    	|| $timediff > $globalLiveInterval
532
	    	    	|| $globalArchive
533
	    	    	|| ($timediff > 30 
534
	    	    	    && isset($this->all_flights[$id]['latitude']) 
535
	    	    	    && isset($this->all_flights[$id]['longitude']) 
536
	    	    	    && $Common->withinThreshold($timediff,$Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m'))
537
	    	    	    )
538
	    	    	) {
539
540
			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']))) {
541
			    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...
542
				|| (isset($line['format_source']) && $line['format_source'] == 'airwhere') 
543
				|| !$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'])) {
544
				$this->all_flights[$id]['archive_latitude'] = $line['latitude'];
545
				$this->all_flights[$id]['archive_longitude'] = $line['longitude'];
546
				$this->all_flights[$id]['putinarchive'] = true;
547
				$this->tmd = 0;
548
				if (!isset($globalNoImport) || $globalNoImport === FALSE) {
549
				    if ($globalDebug) echo "\n".' ------- Check Country for '.$this->all_flights[$id]['ident'].' with latitude : '.$line['latitude'].' and longitude : '.$line['longitude'].'.... ';
550
				    $timeelapsed = microtime(true);
551
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
552
					$Spotter = new Spotter($this->db);
553
					$all_country = $Spotter->getCountryFromLatitudeLongitude($line['latitude'],$line['longitude']);
554
					if (!empty($all_country)) $this->all_flights[$id]['over_country'] = $all_country['iso2'];
555
					else $this->all_flights[$id]['over_country'] = '';
556
					$Spotter->db = null;
557
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update getCountryFromlatitudeLongitude : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
558
					if ($globalDebug) echo 'FOUND : '.$this->all_flights[$id]['over_country'].' ---------------'."\n";
559
				    }
560
				}
561
				$this->all_flights[$id]['time_last_archive_coord'] = time();
562
			    } 
563
			    /*
564
			    else {
565
				if (!isset($timediff)) echo 'NO TIMEDIFF';
566
				else {
567
				echo 'timediff : '.$timediff.' - maxupdate : '.$globalAircraftMaxUpdate."\n";
568
				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']));
569
				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";
570
				}
571
				echo 'NOT OK HERE';
572
			    }
573
			    */
574
			}
575
576
			if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
577
			    //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) {
578
				if (!isset($this->all_flights[$id]['archive_latitude'])) {
579
					$this->all_flights[$id]['archive_latitude'] = $line['latitude'];
580
					$this->all_flights[$id]['time_last_coord'] = time();
581
				}
582
				//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)) {
583
				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)) {
584
				    $this->all_flights[$id]['livedb_latitude'] = $line['latitude'];
585
				    $dataFound = true;
586
				    $this->all_flights[$id]['time_last_coord'] = time();
587
				}
588
				// elseif ($globalDebug) echo '!*!*! Ignore data, too close to previous one'."\n";
589
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('latitude' => $line['latitude']));
590
				/*
591
				if (abs($this->all_flights[$id]['archive_latitude']-$this->all_flights[$id]['latitude']) > 0.3) {
592
				    $this->all_flights[$id]['archive_latitude'] = $line['latitude'];
593
				    $this->all_flights[$id]['putinarchive'] = true;
594
				    //$putinarchive = true;
595
				}
596
				*/
597
			    /*
598
			    } elseif (isset($this->all_flights[$id]['latitude'])) {
599
				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";
600
			    }
601
			    */
602
			}
603
			if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
604
			    if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
605
			    //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) {
606
				if (!isset($this->all_flights[$id]['archive_longitude'])) {
607
					$this->all_flights[$id]['archive_longitude'] = $line['longitude'];
608
					$this->all_flights[$id]['time_last_coord'] = time();
609
				}
610
				//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)) {
611
				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)) {
612
				    $this->all_flights[$id]['livedb_longitude'] = $line['longitude'];
613
				    $dataFound = true;
614
				    $this->all_flights[$id]['time_last_coord'] = time();
615
				}
616
				// elseif ($globalDebug) echo '!*!*! Ignore data, too close to previous one'."\n";
617
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('longitude' => $line['longitude']));
618
				/*
619
				if (abs($this->all_flights[$id]['archive_longitude']-$this->all_flights[$id]['longitude']) > 0.3) {
620
				    $this->all_flights[$id]['archive_longitude'] = $line['longitude'];
621
				    $this->all_flights[$id]['putinarchive'] = true;
622
				    //$putinarchive = true;
623
				}
624
				*/
625
			/*
626
			    } elseif (isset($this->all_flights[$id]['longitude'])) {
627
				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";
628
			    }
629
			    */
630
			}
631
632
		    } else if ($globalDebug && $timediff > 30) {
633
			$this->tmd = $this->tmd + 1;
634
			echo '!!! Too much distance in short time... for '.$this->all_flights[$id]['ident']."\n";
635
			echo 'Time : '.$timediff.'s - Distance : '.$Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m')."m -";
636
			echo 'Speed : '.(($Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m')/$timediff)*3.6)." km/h - ";
637
			echo 'Lat : '.$line['latitude'].' - long : '.$line['longitude'].' - prev lat : '.$this->all_flights[$id]['latitude'].' - prev long : '.$this->all_flights[$id]['longitude']." \n";
638
		    }
639
		}
640
		if (isset($line['last_update']) && $line['last_update'] != '') {
641
		    if (isset($this->all_flights[$id]['last_update']) && $this->all_flights[$id]['last_update'] != $line['last_update']) $dataFound = true;
642
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('last_update' => $line['last_update']));
643
		}
644
		if (isset($line['verticalrate']) && $line['verticalrate'] != '') {
645
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('verticalrate' => $line['verticalrate']));
646
		    //$dataFound = true;
647
		}
648
		if (isset($line['format_source']) && $line['format_source'] != '') {
649
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('format_source' => $line['format_source']));
650
		}
651
		if (isset($line['source_name']) && $line['source_name'] != '') {
652
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('source_name' => $line['source_name']));
653
		}
654
		if (isset($line['emergency']) && $line['emergency'] != '') {
655
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('emergency' => $line['emergency']));
656
		    //$dataFound = true;
657
		}
658
		if (isset($line['ground']) && $line['ground'] != '') {
659
		    if (isset($this->all_flights[$id]['ground']) && $this->all_flights[$id]['ground'] == 1 && $line['ground'] == 0) {
660
			// Here we force archive of flight because after ground it's a new one (or should be)
661
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
662
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 1));
663
			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')));
664
		        elseif (isset($line['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
665
			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']));
666
		    }
667
		    if ($line['ground'] != 1) $line['ground'] = 0;
668
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ground' => $line['ground']));
669
		    //$dataFound = true;
670
		}
671
		if (isset($line['squawk']) && $line['squawk'] != '') {
672
		    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'])) {
673
			    if ($this->all_flights[$id]['squawk'] != $line['squawk']) $this->all_flights[$id]['putinarchive'] = true;
674
			    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('squawk' => $line['squawk']));
675
			    $highlight = '';
676
			    if ($this->all_flights[$id]['squawk'] == '7500') $highlight = 'Squawk 7500 : Hijack at '.date('Y-m-d G:i').' UTC';
677
			    if ($this->all_flights[$id]['squawk'] == '7600') $highlight = 'Squawk 7600 : Lost Comm (radio failure) at '.date('Y-m-d G:i').' UTC';
678
			    if ($this->all_flights[$id]['squawk'] == '7700') $highlight = 'Squawk 7700 : Emergency at '.date('Y-m-d G:i').' UTC';
679
			    if ($highlight != '') {
680
				$timeelapsed = microtime(true);
681
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
682
				    $Spotter = new Spotter($this->db);
683
				    $Spotter->setHighlightFlight($this->all_flights[$id]['id'],$highlight);
684
				    $Spotter->db = null;
685
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update sethighlightflight : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
686
				}
687
				//$putinarchive = true;
688
				//$highlight = '';
689
			    }
690
			    
691
		    } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('squawk' => $line['squawk']));
692
		    //$dataFound = true;
693
		}
694
695
		if (isset($line['altitude']) && $line['altitude'] != '') {
696
			if (isset($line['altitude_relative']) && isset($GeoidClass) && is_object($GeoidClass)) {
697
				if ($line['altitude_relative'] == 'AMSL' || $line['altitude_relative'] == 'MSL') {
698
					$geoid = round($GeoidClass->get($this->all_flights[$id]['livedb_latitude'],$this->all_flights[$id]['livedb_longitude'])*3.28084,2);
699
					//if ($globalDebug) echo '=> Set altitude to WGS84 Ellipsoid, add '.$geoid.' to '.$line['altitude']."\n";
700
					$line['altitude'] = $line['altitude'] - $geoid;
701
				}
702
			}
703
		    //if (!isset($this->all_flights[$id]['altitude']) || $this->all_flights[$id]['altitude'] == '' || ($this->all_flights[$id]['altitude'] > 0 && $line['altitude'] != 0)) {
704
			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;
705
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude' => round($line['altitude']/100)));
706
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude_real' => $line['altitude']));
707
			//$dataFound = true;
708
		    //} elseif ($globalDebug) echo "!!! Strange altitude data... not added.\n";
709
		    if ($globalVA !== TRUE && $globalIVAO !== TRUE && $globalVATSIM !== TRUE && $globalphpVMS !== TRUE && $globalVAM !== TRUE) {
710
			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) {
711
				if ($globalDebug) echo '--- Reset because of altitude'."\n";
712
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
713
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 1));
714
				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')));
715
				elseif (isset($line['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
716
				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']));
717
			}
718
		    }
719
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude_previous' => $line['altitude']));
720
		}
721
722
		if (isset($line['noarchive']) && $line['noarchive'] === true) {
723
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('noarchive' => true));
724
		}
725
		
726
		if (isset($line['heading']) && $line['heading'] != '') {
727
		    if (is_int($this->all_flights[$id]['heading']) && abs($this->all_flights[$id]['heading']-round($line['heading'])) > 10) $this->all_flights[$id]['putinarchive'] = true;
728
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => round($line['heading'])));
729
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading_fromsrc' => true));
730
		    //$dataFound = true;
731
  		} 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']) {
732
  		    $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']);
733
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => round($heading)));
734
		    if (abs($this->all_flights[$id]['heading']-round($heading)) > 10) $this->all_flights[$id]['putinarchive'] = true;
735
  		    if ($globalDebug) echo "ø Calculated Heading for ".$this->all_flights[$id]['hex']." : ".$heading."\n";
736
  		} elseif (isset($this->all_flights[$id]['format_source']) && $this->all_flights[$id]['format_source'] == 'ACARS') {
737
  		    // If not enough messages and ACARS set heading to 0
738
  		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => 0));
739
  		}
740
		if ($globalDaemon === TRUE && isset($globalSourcesupdate) && $globalSourcesupdate != '' && isset($this->all_flights[$id]['lastupdate']) && time()-$this->all_flights[$id]['lastupdate'] < $globalSourcesupdate) $dataFound = false;
741
		elseif ($globalDaemon === TRUE && isset($globalSBS1update) && $globalSBS1update != '' && isset($this->all_flights[$id]['lastupdate']) && time()-$this->all_flights[$id]['lastupdate'] < $globalSBS1update) $dataFound = false;
742
		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...
743
744
//		print_r($this->all_flights[$id]);
745
		//gets the callsign from the last hour
746
		//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'] != '') {
747
		//if ($dataFound == true && isset($this->all_flights[$id]['hex']) && $this->all_flights[$id]['ident'] != '' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
748
		//if ($dataFound === true && isset($this->all_flights[$id]['hex']) && $this->all_flights[$id]['heading'] != '' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
749
		//if ($dataFound === true && isset($this->all_flights[$id]['hex'])) {
750
		if ($dataFound === true && isset($this->all_flights[$id]['id'])) {
751
		    $this->all_flights[$id]['lastupdate'] = time();
752
		    if ((!isset($globalNoImport) || $globalNoImport === FALSE) && $this->all_flights[$id]['addedSpotter'] == 0) {
753
		        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'])) {
754
			    //print_r($this->all_flights);
755
			    //echo $this->all_flights[$id]['id'].' - '.$this->all_flights[$id]['addedSpotter']."\n";
756
			    //$last_hour_ident = Spotter->getIdentFromLastHour($this->all_flights[$id]['ident']);
757
			    if (!isset($this->all_flights[$id]['forcenew']) || $this->all_flights[$id]['forcenew'] == 0) {
758
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
759
				    if ($globalDebug) echo "Check if aircraft is already in DB...";
760
				    $timeelapsed = microtime(true);
761
				    $SpotterLive = new SpotterLive($this->db);
762
				    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')) {
763
					$recent_ident = $SpotterLive->checkModeSRecent($this->all_flights[$id]['hex']);
764
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkModeSRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
765
				    } elseif (isset($line['id'])) {
766
					$recent_ident = $SpotterLive->checkIdRecent($line['id']);
767
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
768
				    } elseif (isset($this->all_flights[$id]['ident']) && $this->all_flights[$id]['ident'] != '') {
769
					$recent_ident = $SpotterLive->checkIdentRecent($this->all_flights[$id]['ident']);
770
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
771
				    } else $recent_ident = '';
772
				    $SpotterLive->db=null;
773
				    if ($globalDebug && $recent_ident == '') echo " Not in DB.\n";
774
				    elseif ($globalDebug && $recent_ident != '') echo " Already in DB.\n";
775
				} else $recent_ident = '';
776
			    } else {
777
				$recent_ident = '';
778
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 0));
779
			    }
780
			    //if there was no aircraft with the same callsign within the last hour and go post it into the archive
781
			    if($recent_ident == "")
782
			    {
783
				if ($globalDebug) echo "\o/ Add ".$this->all_flights[$id]['ident']." in archive DB : ";
784
				if ($this->all_flights[$id]['departure_airport'] == "") { $this->all_flights[$id]['departure_airport'] = "NA"; }
785
				if ($this->all_flights[$id]['arrival_airport'] == "") { $this->all_flights[$id]['arrival_airport'] = "NA"; }
786
				//adds the spotter data for the archive
787
				$ignoreImport = false;
788
				foreach($globalAirportIgnore as $airportIgnore) {
789
				    if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
790
					$ignoreImport = true;
791
				    }
792
				}
793
				if (count($globalAirportAccept) > 0) {
794
				    $ignoreImport = true;
795
				    foreach($globalAirportIgnore as $airportIgnore) {
796
					if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
797
					    $ignoreImport = false;
798
					}
799
				    }
800
				}
801
				if (isset($globalAirlineIgnore) && is_array($globalAirlineIgnore)) {
802
				    foreach($globalAirlineIgnore as $airlineIgnore) {
803
					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)) {
804
					    $ignoreImport = true;
805
					}
806
				    }
807
				}
808
				if (isset($globalAirlineAccept) && count($globalAirlineAccept) > 0) {
809
				    $ignoreImport = true;
810
				    foreach($globalAirlineAccept as $airlineAccept) {
811
					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)) {
812
					    $ignoreImport = false;
813
					}
814
				    }
815
				}
816
				if (isset($globalPilotIdAccept) && count($globalPilotIdAccept) > 0) {
817
				    $ignoreImport = true;
818
				    foreach($globalPilotIdAccept as $pilotIdAccept) {
819
					if ($this->all_flights[$id]['pilot_id'] == $pilotIdAccept) {
820
					    $ignoreImport = false;
821
					}
822
				    }
823
				}
824
				
825
				if (!$ignoreImport) {
826
				    $highlight = '';
827
				    if ($this->all_flights[$id]['squawk'] == '7500') $highlight = 'Squawk 7500 : Hijack';
828
				    if ($this->all_flights[$id]['squawk'] == '7600') $highlight = 'Squawk 7600 : Lost Comm (radio failure)';
829
				    if ($this->all_flights[$id]['squawk'] == '7700') $highlight = 'Squawk 7700 : Emergency';
830
				    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')));
831
				    $timeelapsed = microtime(true);
832
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
833
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
834
					    $Spotter = new Spotter($this->db);
835
					    $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']);
836
					    $Spotter->db = null;
837
					    if ($globalDebug && isset($result)) echo $result."\n";
838
					}
839
				    }
840
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update addspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
841
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
842
843
				    // Add source stat in DB
844
				    $Stats = new Stats($this->db);
845
				    if (!empty($this->stats)) {
846
					if ($globalDebug) echo 'Add source stats : ';
847
				        foreach($this->stats as $date => $data) {
848
					    foreach($data as $source => $sourced) {
849
					        //print_r($sourced);
850
				    	        if (isset($sourced['polar'])) echo $Stats->addStatSource(json_encode($sourced['polar']),$source,'polar',$date);
851
				    	        if (isset($sourced['hist'])) echo $Stats->addStatSource(json_encode($sourced['hist']),$source,'hist',$date);
852
				    		if (isset($sourced['msg'])) {
853
				    		    if (time() - $sourced['msg']['date'] > 10) {
854
				    		        $nbmsg = round($sourced['msg']['nb']/(time() - $sourced['msg']['date']));
855
				    		        echo $Stats->addStatSource($nbmsg,$source,'msg',$date);
856
			    			        unset($this->stats[$date][$source]['msg']);
857
			    			    }
858
			    			}
859
			    		    }
860
			    		    if ($date != date('Y-m-d')) {
861
			    			unset($this->stats[$date]);
862
			    		    }
863
				    	}
864
				    	if ($globalDebug) echo 'Done'."\n";
865
866
				    }
867
				    $Stats->db = null;
868
				    }
869
				    $this->del();
870
				} elseif ($globalDebug) echo 'Ignore data'."\n";
871
				//$ignoreImport = false;
872
				$this->all_flights[$id]['addedSpotter'] = 1;
873
				//print_r($this->all_flights[$id]);
874
			/*
875
			if (isset($globalArchive) && $globalArchive) {
876
			    $archives_ident = SpotterLive->getAllLiveSpotterDataByIdent($this->all_flights[$id]['ident']);
877
			    foreach ($archives_ident as $archive) {
878
				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'],
879
				$archive['arrival_airport_icao'],$archive['arrival_airport_name'],$archive['arrival_airport_city'],$archive['arrival_airport_country'],$archive['arrival_airport_time'],
880
				$archive['route_stop'],$archive['date'],$archive['latitude'], $archive['longitude'], $archive['waypoints'], $archive['altitude'], $archive['heading'], $archive['ground_speed'],
881
				$archive['squawk'],$archive['ModeS']);
882
			    }
883
			}
884
			*/
885
			//SpotterLive->deleteLiveSpotterDataByIdent($this->all_flights[$id]['ident']);
886
				if ($this->last_delete == 0 || time() - $this->last_delete > 1800) {
887
				    if ($globalDebug) echo "---- Deleting Live Spotter data older than 9 hours...";
888
				    //SpotterLive->deleteLiveSpotterDataNotUpdated();
889
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
890
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
891
					    $SpotterLive = new SpotterLive($this->db);
892
					    $SpotterLive->deleteLiveSpotterData();
893
					    $SpotterLive->db=null;
894
					}
895
				    }
896
				    if ($globalDebug) echo " Done\n";
897
				    $this->last_delete = time();
898
				}
899
			    } else {
900
				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')) {
901
				    $this->all_flights[$id]['id'] = $recent_ident;
902
				    $this->all_flights[$id]['addedSpotter'] = 1;
903
				}
904
				if (isset($globalDaemon) && !$globalDaemon) {
905
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
906
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
907
					    $Spotter = new Spotter($this->db);
908
					    $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']);
909
					    $Spotter->db = null;
910
					}
911
				    }
912
				}
913
				
914
			    }
915
			}
916
		    }
917
		    //adds the spotter LIVE data
918
		    //SpotterLive->addLiveSpotterData($flightaware_id, $ident, $aircraft_type, $departure_airport, $arrival_airport, $latitude, $longitude, $waypoints, $altitude, $heading, $groundspeed);
919
		    //echo "\nAdd in Live !! \n";
920
		    //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";
921
		    if ($globalDebug) {
922
			if ((isset($globalVA) && $globalVA) || (isset($globalIVAO) && $globalIVAO) || (isset($globalVATSIM) && $globalVATSIM) || (isset($globalphpVMS) && $globalphpVMS) || (isset($globalVAM) && $globalVAM)) {
923
				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";
924
				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";
925
			} else {
926
				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";
927
				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";
928
			}
929
		    }
930
		    $ignoreImport = false;
931
		    if ($this->all_flights[$id]['departure_airport'] == "") { $this->all_flights[$id]['departure_airport'] = "NA"; }
932
		    if ($this->all_flights[$id]['arrival_airport'] == "") { $this->all_flights[$id]['arrival_airport'] = "NA"; }
933
934
		    foreach($globalAirportIgnore as $airportIgnore) {
935
		        if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
936
			    $ignoreImport = true;
937
			}
938
		    }
939
		    if (count($globalAirportAccept) > 0) {
940
		        $ignoreImport = true;
941
		        foreach($globalAirportIgnore as $airportIgnore) {
942
			    if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
943
				$ignoreImport = false;
944
			    }
945
			}
946
		    }
947
		    if (isset($globalAirlineIgnore) && is_array($globalAirlineIgnore)) {
948
			foreach($globalAirlineIgnore as $airlineIgnore) {
949
			    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)) {
950
				$ignoreImport = true;
951
			    }
952
			}
953
		    }
954
		    if (isset($globalAirlineAccept) && count($globalAirlineAccept) > 0) {
955
			$ignoreImport = true;
956
			foreach($globalAirlineAccept as $airlineAccept) {
957
			    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)) {
958
				$ignoreImport = false;
959
			    }
960
			}
961
		    }
962
		    if (isset($globalPilotIdAccept) && count($globalPilotIdAccept) > 0) {
963
			$ignoreImport = true;
964
			foreach($globalPilotIdAccept as $pilotIdAccept) {
965
			    if ($this->all_flights[$id]['pilot_id'] == $pilotIdAccept) {
966
			        $ignoreImport = false;
967
			    }
968
			}
969
		    }
970
971
		    if (!$ignoreImport) {
972
			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'])) {
973
				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')));
974
				$timeelapsed = microtime(true);
975
				if (!isset($globalNoImport) || $globalNoImport === FALSE) {
976
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
977
					if ($globalDebug) echo "\o/ Add ".$this->all_flights[$id]['ident']." from ".$this->all_flights[$id]['format_source']." in Live DB : ";
978
					$SpotterLive = new SpotterLive($this->db);
979
					$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']);
980
					$SpotterLive->db = null;
981
					if ($globalDebug) echo $result."\n";
982
				    }
983
				}
984
				if (isset($globalServerAPRS) && $globalServerAPRS && $this->all_flights[$id]['putinarchive']) {
985
					$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']);
986
				}
987
				$this->all_flights[$id]['putinarchive'] = false;
988
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update addlivespotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
989
990
				// Put statistics in $this->stats variable
991
				//if ($line['format_source'] != 'aprs') {
992
				//if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt')) {
993
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
994
				    if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
995
					$source = $this->all_flights[$id]['source_name'];
996
					if ($source == '') $source = $this->all_flights[$id]['format_source'];
997
					if (!isset($this->source_location[$source])) {
998
						$Location = new Source($this->db);
999
						$coord = $Location->getLocationInfobySourceName($source);
1000
						if (count($coord) > 0) {
1001
							$latitude = $coord[0]['latitude'];
1002
							$longitude = $coord[0]['longitude'];
1003
						} else {
1004
							$latitude = $globalCenterLatitude;
1005
							$longitude = $globalCenterLongitude;
1006
						}
1007
						$this->source_location[$source] = array('latitude' => $latitude,'longitude' => $longitude);
1008
					} else {
1009
						$latitude = $this->source_location[$source]['latitude'];
1010
						$longitude = $this->source_location[$source]['longitude'];
1011
					}
1012
					$stats_heading = $Common->getHeading($latitude,$longitude,$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude']);
1013
					//$stats_heading = $stats_heading%22.5;
1014
					$stats_heading = round($stats_heading/22.5);
1015
					$stats_distance = $Common->distance($latitude,$longitude,$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude']);
1016
					$current_date = date('Y-m-d');
1017
					if ($stats_heading == 16) $stats_heading = 0;
1018
					if (!isset($this->stats[$current_date][$source]['polar'][1])) {
1019
						for ($i=0;$i<=15;$i++) {
1020
						    $this->stats[$current_date][$source]['polar'][$i] = 0;
1021
						}
1022
						$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
1023
					} else {
1024
						if ($this->stats[$current_date][$source]['polar'][$stats_heading] < $stats_distance) {
1025
							$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
1026
						}
1027
					}
1028
					$distance = (round($stats_distance/10)*10);
1029
					//echo '$$$$$$$$$$ DISTANCE : '.$distance.' - '.$source."\n";
1030
					//var_dump($this->stats);
1031
					if (!isset($this->stats[$current_date][$source]['hist'][$distance])) {
1032
						if (isset($this->stats[$current_date][$source]['hist'][0])) {
1033
						    end($this->stats[$current_date][$source]['hist']);
1034
						    $mini = key($this->stats[$current_date][$source]['hist'])+10;
1035
						} else $mini = 0;
1036
						for ($i=$mini;$i<=$distance;$i+=10) {
1037
						    $this->stats[$current_date][$source]['hist'][$i] = 0;
1038
						}
1039
						$this->stats[$current_date][$source]['hist'][$distance] = 1;
1040
					} else {
1041
						$this->stats[$current_date][$source]['hist'][$distance] += 1;
1042
					}
1043
				    }
1044
				}
1045
1046
				$this->all_flights[$id]['lastupdate'] = time();
1047
				if ($this->all_flights[$id]['putinarchive']) $send = true;
1048
				//if ($globalDebug) echo "Distance : ".Common->distance($this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude'])."\n";
1049
			} 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";
1050
			//$this->del();
1051
			
1052
			if ($this->last_delete_hourly == 0 || time() - $this->last_delete_hourly > 900) {
1053
			    if ((!isset($globalNoImport) || $globalNoImport === FALSE) && (!isset($globalNoDB) || $globalNoDB !== TRUE)) {
1054
				if ($globalDebug) echo "---- Deleting Live Spotter data Not updated since 2 hour...";
1055
				$SpotterLive = new SpotterLive($this->db);
1056
				$SpotterLive->deleteLiveSpotterDataNotUpdated();
1057
				$SpotterLive->db = null;
1058
				//SpotterLive->deleteLiveSpotterData();
1059
				if ($globalDebug) echo " Done\n";
1060
				$this->last_delete_hourly = time();
1061
			    } else {
1062
				$this->del();
1063
				$this->last_delete_hourly = time();
1064
			    }
1065
			}
1066
			
1067
		    }
1068
		    //$ignoreImport = false;
1069
		}
1070
		//if (function_exists('pcntl_fork') && $globalFork) pcntl_signal(SIGCHLD, SIG_IGN);
1071
		if ($send) return $this->all_flights[$id];
1072
	    }
1073
	}
1074
    }
1075
}
1076
?>
1077