Completed
Push — master ( 6906ba...52314e )
by Yannick
21:00 queued 12:41
created

SpotterImport::__construct()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 6
nop 1
dl 0
loc 28
rs 4.909
c 0
b 0
f 0
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
if (isset($globalServerAPRS) && $globalServerAPRS) {
11
    require_once(dirname(__FILE__).'/class.APRS.php');
12
}
13
14
class SpotterImport {
15
    private $all_flights = array();
16
    private $last_delete_hourly = 0;
17
    private $last_delete = 0;
18
    private $stats = array();
19
    private $tmd = 0;
20
    private $source_location = array();
21
    public $db = null;
22
    public $nb = 0;
23
24
    public function __construct($dbc = null) {
25
	global $globalBeta, $globalServerAPRS, $APRSSpotter, $globalNoDB;
26
	if (!(isset($globalNoDB) && $globalNoDB)) {
27
		$Connection = new Connection($dbc);
28
		$this->db = $Connection->db();
29
		date_default_timezone_set('UTC');
30
31
		// Get previous source stats
32
		$Stats = new Stats($dbc);
33
		$currentdate = date('Y-m-d');
34
		$sourcestat = $Stats->getStatsSource($currentdate);
35
		if (!empty($sourcestat)) {
36
		    foreach($sourcestat as $srcst) {
37
		    	$type = $srcst['stats_type'];
38
			if ($type == 'polar' || $type == 'hist') {
39
			    $source = $srcst['source_name'];
40
			    $data = $srcst['source_data'];
41
			    $this->stats[$currentdate][$source][$type] = json_decode($data,true);
42
	    		}
43
		    }
44
		}
45
	}
46
	if (isset($globalServerAPRS) && $globalServerAPRS) {
47
		$APRSSpotter = new APRSSpotter();
48
		//$APRSSpotter->connect();
49
	}
50
51
    }
52
53
    public function get_Schedule($id,$ident) {
54
	global $globalDebug, $globalFork, $globalSchedulesFetch;
55
	// Get schedule here, so it's done only one time
56
	
57
	/*
58
	if ($globalFork) {
59
		$Connection = new Connection();
60
		$dbc = $Connection->db;
61
	} else $dbc = $this->db;
62
	*/
63
	$dbc = $this->db;
64
	$this->all_flights[$id]['schedule_check'] = true;
65
	if ($globalSchedulesFetch) {
66
	if ($globalDebug) echo 'Getting schedule info...'."\n";
67
	$Spotter = new Spotter($dbc);
68
	$Schedule = new Schedule($dbc);
69
	$Translation = new Translation($dbc);
70
	$operator = $Spotter->getOperator($ident);
71
	$scheduleexist = false;
72
	if ($Schedule->checkSchedule($operator) == 0) {
73
	    $operator = $Translation->checkTranslation($ident);
74
	    if ($Schedule->checkSchedule($operator) == 0) {
75
		$schedule = $Schedule->fetchSchedule($operator);
76
		if (count($schedule) > 0 && isset($schedule['DepartureTime']) && isset($schedule['ArrivalTime'])) {
77
		    if ($globalDebug) echo "-> Schedule info for ".$operator." (".$ident.")\n";
78
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport_time' => $schedule['DepartureTime']));
79
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('arrival_airport_time' => $schedule['ArrivalTime']));
80
		    // Should also check if route schedule = route from DB
81
		    if ($schedule['DepartureAirportIATA'] != '') {
82
			if ($this->all_flights[$id]['departure_airport'] != $Spotter->getAirportIcao($schedule['DepartureAirportIATA'])) {
83
			    $airport_icao = $Spotter->getAirportIcao($schedule['DepartureAirportIATA']);
84
			    if (trim($airport_icao) != '') {
85
				$this->all_flights[$id]['departure_airport'] = $airport_icao;
86
				if ($globalDebug) echo "-> Change departure airport to ".$airport_icao." for ".$ident."\n";
87
			    }
88
			}
89
		    }
90
		    if ($schedule['ArrivalAirportIATA'] != '') {
91
			if ($this->all_flights[$id]['arrival_airport'] != $Spotter->getAirportIcao($schedule['ArrivalAirportIATA'])) {
92
			    $airport_icao = $Spotter->getAirportIcao($schedule['ArrivalAirportIATA']);
93
			    if (trim($airport_icao) != '') {
94
				$this->all_flights[$id]['arrival_airport'] = $airport_icao;
95
				if ($globalDebug) echo "-> Change arrival airport to ".$airport_icao." for ".$ident."\n";
96
			    }
97
			}
98
		    }
99
		    $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']);
100
		}
101
	    } else $scheduleexist = true;
102
	} else $scheduleexist = true;
103
	// close connection, at least one way will work ?
104
       if ($scheduleexist) {
105
		if ($globalDebug) echo "-> get arrival/departure airport info for ".$ident."\n";
106
    		$sch = $Schedule->getSchedule($operator);
107
		$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']));
108
       }
109
	$Spotter->db = null;
110
	$Schedule->db = null;
111
	$Translation->db = null;
112
	unset($Spotter->db);
113
	unset($Schedule->db);
114
	unset($Translation->db);
115
116
	/*
117
	if ($globalFork) {
118
	    $Connection->db = null;
119
	    unset($Connection->db);
120
	}
121
	  */
122
	}
123
    }
124
125
    public function checkAll() {
126
	global $globalDebug, $globalNoImport;
127
	if ($globalDebug) echo "Update last seen flights data...\n";
128
	if (!isset($globalNoImport) || $globalNoImport === FALSE) {
129
	    foreach ($this->all_flights as $key => $flight) {
130
		if (isset($this->all_flights[$key]['id'])) {
131
		    //echo $this->all_flights[$key]['id'].' - '.$this->all_flights[$key]['latitude'].'  '.$this->all_flights[$key]['longitude']."\n";
132
    		    $Spotter = new Spotter($this->db);
133
        	    $real_arrival = $this->arrival($key);
134
        	    $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']);
135
        	}
136
	    }
137
	}
138
    }
139
140
    public function arrival($key) {
141
	global $globalClosestMinDist, $globalDebug;
142
	if ($globalDebug) echo 'Update arrival...'."\n";
143
	$Spotter = new Spotter($this->db);
144
        $airport_icao = '';
145
        $airport_time = '';
146
        if (!isset($globalClosestMinDist) || $globalClosestMinDist == '') $globalClosestMinDist = 50;
147
	if ($this->all_flights[$key]['latitude'] != '' && $this->all_flights[$key]['longitude'] != '') {
148
	    $closestAirports = $Spotter->closestAirports($this->all_flights[$key]['latitude'],$this->all_flights[$key]['longitude'],$globalClosestMinDist);
149
    	    if (isset($closestAirports[0])) {
150
        	if (isset($this->all_flights[$key]['arrival_airport']) && $this->all_flights[$key]['arrival_airport'] == $closestAirports[0]['icao']) {
151
        	    $airport_icao = $closestAirports[0]['icao'];
152
        	    $airport_time = $this->all_flights[$key]['datetime'];
153
        	    if ($globalDebug) echo "---++ Find arrival airport. airport_icao : ".$airport_icao."\n";
154
        	} elseif (count($closestAirports > 1) && isset($this->all_flights[$key]['arrival_airport']) && $this->all_flights[$key]['arrival_airport'] != '') {
155
        	    foreach ($closestAirports as $airport) {
156
        		if ($this->all_flights[$key]['arrival_airport'] == $airport['icao']) {
157
        		    $airport_icao = $airport['icao'];
158
        		    $airport_time = $this->all_flights[$key]['datetime'];
159
        		    if ($globalDebug) echo "---++ Find arrival airport. airport_icao : ".$airport_icao."\n";
160
        		    break;
161
        		}
162
        	    }
163
        	} 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))) {
164
        		$airport_icao = $closestAirports[0]['icao'];
165
        		$airport_time = $this->all_flights[$key]['datetime'];
166
        	} else {
167
        		if ($globalDebug) echo "----- Can't find arrival airport. Airport altitude : ".$closestAirports[0]['altitude'].' - flight altitude : '.$this->all_flights[$key]['altitude_real']."\n";
168
        	}
169
    	    } else {
170
    		    if ($globalDebug) echo "----- No Airport near last coord. Latitude : ".$this->all_flights[$key]['latitude'].' - Longitude : '.$this->all_flights[$key]['longitude'].' - MinDist : '.$globalClosestMinDist."\n";
171
    	    }
172
173
        } else {
174
        	if ($globalDebug) echo "---- No latitude or longitude. Ident : ".$this->all_flights[$key]['ident']."\n";
175
        }
176
        return array('airport_icao' => $airport_icao,'airport_time' => $airport_time);
177
    }
178
179
180
181
    public function del() {
182
	global $globalDebug, $globalNoImport, $globalNoDB;
183
	// Delete old infos
184
	if ($globalDebug) echo 'Delete old values and update latest data...'."\n";
185
	foreach ($this->all_flights as $key => $flight) {
186
    	    if (isset($flight['lastupdate'])) {
187
        	if ($flight['lastupdate'] < (time()-3000)) {
188
            	    if (isset($this->all_flights[$key]['id'])) {
189
            		if ($globalDebug) echo "--- Delete old values with id ".$this->all_flights[$key]['id']."\n";
190
			/*
191
			$SpotterLive = new SpotterLive();
192
            		$SpotterLive->deleteLiveSpotterDataById($this->all_flights[$key]['id']);
193
			$SpotterLive->db = null;
194
			*/
195
			if ((!isset($globalNoImport) || $globalNoImport === FALSE) && (!isset($globalNoDB) || $globalNoDB !== TRUE)) {
196
            		    $real_arrival = $this->arrival($key);
197
            		    $Spotter = new Spotter($this->db);
198
            	    	    if ($this->all_flights[$key]['latitude'] != '' && $this->all_flights[$key]['longitude'] != '') {
199
				$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']);
200
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
201
			    }
202
			// Put in archive
203
//				$Spotter->db = null;
204
			}
205
            	    }
206
            	    unset($this->all_flights[$key]);
207
    	        }
208
	    }
209
        }
210
    }
211
212
    public function add($line) {
213
	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;
214
	//if (!isset($globalDebugTimeElapsed) || $globalDebugTimeElapsed == '') $globalDebugTimeElapsed = FALSE;
215
	if (!isset($globalCoordMinChange) || $globalCoordMinChange == '') $globalCoordMinChange = '0.02';
216
/*
217
	$Spotter = new Spotter();
218
	$dbc = $Spotter->db;
219
	$SpotterLive = new SpotterLive($dbc);
220
	$Common = new Common();
221
	$Schedule = new Schedule($dbc);
222
*/
223
	date_default_timezone_set('UTC');
224
	// signal handler - playing nice with sockets and dump1090
225
	// pcntl_signal_dispatch();
226
227
	// get the time (so we can figure the timeout)
228
	//$time = time();
229
230
	//pcntl_signal_dispatch();
231
	$dataFound = false;
232
	//$putinarchive = false;
233
	$send = false;
234
	
235
	// SBS format is CSV format
236
	if(is_array($line) && (isset($line['hex']) || isset($line['id']))) {
237
	    //print_r($line);
238
  	    if (isset($line['id']) || (isset($line['hex']) && $line['hex'] != '' && $line['hex'] != '00000' && $line['hex'] != '000000' && $line['hex'] != '111111' && ctype_xdigit($line['hex']) && strlen($line['hex']) === 6)) {
239
240
		// Increment message number
241
		if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE) {
242
		    $current_date = date('Y-m-d');
243
		    $source = $line['source_name'];
244
		    if ($source == '' || $line['format_source'] == 'aprs') $source = $line['format_source'];
245
		    if (!isset($this->stats[$current_date][$source]['msg'])) {
246
		    	$this->stats[$current_date][$source]['msg']['date'] = time();
247
		    	$this->stats[$current_date][$source]['msg']['nb'] = 1;
248
		    } else $this->stats[$current_date][$source]['msg']['nb'] += 1;
249
		}
250
		
251
		/*
252
		$dbc = $this->db;
253
		$Connection = new Connection($dbc);
254
		$Connection->connectionExists();
255
		$dbc = $Connection->db;
256
		*/
257
		//$Spotter = new Spotter($dbc);
258
		//$SpotterLive = new SpotterLive($dbc);
259
		$Common = new Common();
260
//		echo $this->nb++."\n";
261
		//$this->db = $dbc;
262
263
		//$hex = trim($line['hex']);
264
	        if (!isset($line['id'])) $id = trim($line['hex']);
265
	        else $id = trim($line['id']);
266
		
267
		if (!isset($this->all_flights[$id])) {
268
		    $this->all_flights[$id] = array();
269
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
270
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ident' => '','departure_airport' => '', 'arrival_airport' => '','latitude' => '', 'longitude' => '', 'speed' => '', 'altitude' => '','altitude_real' => '', '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' => ''));
271
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('lastupdate' => time()));
272
		    if (!isset($line['id'])) {
273
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
274
//			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')));
275
//			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')));
276
			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' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'radarvirtueljson')) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $id.'-'.date('YmdHi')));
277
		        //else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
278
		     } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
279
		    if ($globalAllFlights !== FALSE) $dataFound = true;
280
		}
281
		if (isset($line['source_type']) && $line['source_type'] != '') {
282
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('source_type' => $line['source_type']));
283
		}
284
		
285
		//print_r($this->all_flights);
286
		if (isset($line['hex']) && !isset($this->all_flights[$id]['hex']) && ctype_xdigit($line['hex'])) {
287
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('hex' => trim($line['hex'])));
288
		    //if (isset($line['datetime']) && preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) {
289
			//$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => $line['datetime']));
290
		    //} else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => date('Y-m-d H:i:s')));
291
		    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') {
292
			$timeelapsed = microtime(true);
293
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
294
			    $Spotter = new Spotter($this->db);
295
			    if (isset($this->all_flights[$id]['source_type'])) {
296
				$aircraft_icao = $Spotter->getAllAircraftType(trim($line['hex']),$this->all_flights[$id]['source_type']);
297
			    } else {
298
				$aircraft_icao = $Spotter->getAllAircraftType(trim($line['hex']));
299
			    }
300
			    $Spotter->db = null;
301
			    if ($globalDebugTimeElapsed) echo 'Time elapsed for update getallaircrattype : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
302
			    if ($aircraft_icao != '') $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
303
			}
304
		    }
305
		    if ($globalAllFlights !== FALSE) $dataFound = true;
306
		    if ($globalDebug) echo "*********** New aircraft hex : ".$line['hex']." ***********\n";
307
		}
308
		if (isset($line['aircraft_icao']) && $line['aircraft_icao'] != '') {
309
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $line['aircraft_icao']));
310
		}
311
		if (!isset($this->all_flights[$id]['aircraft_icao']) && isset($line['aircraft_name'])) {
312
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
313
				// Get aircraft ICAO from aircraft name
314
				$Spotter = new Spotter($this->db);
315
				$aircraft_icao = $Spotter->getAircraftIcao($line['aircraft_name']);
316
				$Spotter->db = null;
317
				if ($aircraft_icao != '') $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
318
			}
319
		}
320
		if (!isset($this->all_flights[$id]['aircraft_icao']) && isset($line['aircraft_type'])) {
321
			if ($line['aircraft_type'] == 'PARA_GLIDER') $aircraft_icao = 'GLID';
322
			elseif ($line['aircraft_type'] == 'HELICOPTER_ROTORCRAFT') $aircraft_icao = 'UHEL';
323
			elseif ($line['aircraft_type'] == 'TOW_PLANE') $aircraft_icao = 'TOWPLANE';
324
			elseif ($line['aircraft_type'] == 'POWERED_AIRCRAFT') $aircraft_icao = 'POWAIRC';
325
			if (isset($aircraft_icao)) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => $aircraft_icao));
326
		}
327
		if (!isset($this->all_flights[$id]['aircraft_icao'])) {
328
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('aircraft_icao' => 'NA'));
329
		}
330
		//if (isset($line['datetime']) && preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) {
331
		if (isset($line['datetime']) && strtotime($line['datetime']) > time()-20*60) {
332
		    if (!isset($this->all_flights[$id]['datetime']) || strtotime($line['datetime']) >= strtotime($this->all_flights[$id]['datetime'])) {
333
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => $line['datetime']));
334
		    } else {
335
				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";
336
				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";
337
				/*
338
				echo strtotime($line['datetime']).' > '.strtotime($this->all_flights[$id]['datetime']);
339
				print_r($this->all_flights[$id]);
340
				print_r($line);
341
				*/
342
				return '';
343
		    }
344
		} else {
345
			date_default_timezone_set('UTC');
346
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('datetime' => date('Y-m-d H:i:s')));
347
		}
348
349
		if (isset($line['registration']) && $line['registration'] != '' && $line['registration'] != 'z.NO-REG') {
350
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('registration' => $line['registration']));
351
		}
352
		if (isset($line['waypoints']) && $line['waypoints'] != '') {
353
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('waypoints' => $line['waypoints']));
354
		}
355
		if (isset($line['pilot_id']) && $line['pilot_id'] != '') {
356
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('pilot_id' => $line['pilot_id']));
357
		}
358
		if (isset($line['pilot_name']) && $line['pilot_name'] != '') {
359
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('pilot_name' => $line['pilot_name']));
360
		}
361
 
362
		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'])) {
363
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ident' => trim($line['ident'])));
364
		    if ($this->all_flights[$id]['addedSpotter'] == 1) {
365
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
366
			    $timeelapsed = microtime(true);
367
            		    $Spotter = new Spotter($this->db);
368
            		    $fromsource = NULL;
369
            		    if (isset($globalAirlinesSource) && $globalAirlinesSource != '') $fromsource = $globalAirlinesSource;
370
            		    elseif (isset($line['format_source']) && $line['format_source'] == 'vatsimtxt') $fromsource = 'vatsim';
371
			    elseif (isset($line['format_source']) && $line['format_source'] == 'whazzup') $fromsource = 'ivao';
372
			    elseif (isset($globalVATSIM) && $globalVATSIM) $fromsource = 'vatsim';
373
			    elseif (isset($globalIVAO) && $globalIVAO) $fromsource = 'ivao';
374
            		    $result = $Spotter->updateIdentSpotterData($this->all_flights[$id]['id'],$this->all_flights[$id]['ident'],$fromsource);
375
			    if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
376
			    $Spotter->db = null;
377
			    if ($globalDebugTimeElapsed) echo 'Time elapsed for update identspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
378
			}
379
		    }
380
381
/*
382
		    if (!isset($line['id'])) {
383
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
384
//			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')));
385
			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')));
386
		        else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $this->all_flights[$id]['hex'].'-'.$this->all_flights[$id]['ident']));
387
		     } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
388
  */
389
		    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']));
390
391
		    //$putinarchive = true;
392
		    if (isset($line['departure_airport_time']) && $line['departure_airport_time'] != 0) {
393
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('departure_airport_time' => $line['departure_airport_time']));
394
		    }
395
		    if (isset($line['arrival_airport_time']) && $line['arrival_airport_time'] != 0) {
396
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('arrival_airport_time' => $line['arrival_airport_time']));
397
		    }
398
		    if (isset($line['departure_airport_icao']) && isset($line['arrival_airport_icao'])) {
399
		    		$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' => ''));
400
		    } elseif (isset($line['departure_airport_iata']) && isset($line['arrival_airport_iata'])) {
401
			$timeelapsed = microtime(true);
402
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
403
				$Spotter = new Spotter($this->db);
404
				$line['departure_airport_icao'] = $Spotter->getAirportIcao($line['departure_airport_iata']);
405
				$line['arrival_airport_icao'] = $Spotter->getAirportIcao($line['arrival_airport_iata']);
406
		    		$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' => ''));
407
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update getAirportICAO : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
408
                        }
409
		    } elseif (!isset($line['format_source']) || $line['format_source'] != 'aprs') {
410
			$timeelapsed = microtime(true);
411
			if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
412
			    $Spotter = new Spotter($this->db);
413
			    $route = $Spotter->getRouteInfo(trim($line['ident']));
414
			    if (!isset($route['fromairport_icao']) && !isset($route['toairport_icao'])) {
415
				$Translation = new Translation($this->db);
416
				$ident = $Translation->checkTranslation(trim($line['ident']));
417
				$route = $Spotter->getRouteInfo($ident);
418
				$Translation->db = null;
419
			    }
420
			    $Spotter->db = null;
421
			    if ($globalDebugTimeElapsed) echo 'Time elapsed for update getrouteinfo : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
422
                    	}
423
			if (isset($route['fromairport_icao']) && isset($route['toairport_icao'])) {
424
			    //if ($route['FromAirport_ICAO'] != $route['ToAirport_ICAO']) {
425
			    if ($route['fromairport_icao'] != $route['toairport_icao']) {
426
				//    $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']));
427
		    		$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']));
428
		    	    }
429
			}
430
			if (!isset($globalFork)) $globalFork = TRUE;
431
			if (!$globalIVAO && !$globalVATSIM && !$globalphpVMS && !$globalVAM && (!isset($line['format_source']) || $line['format_source'] != 'aprs')) {
432
				if (!isset($this->all_flights[$id]['schedule_check']) || $this->all_flights[$id]['schedule_check'] === false) $this->get_Schedule($id,trim($line['ident']));
433
			}
434
		    }
435
		}
436
437
		if (isset($line['speed']) && $line['speed'] != '') {
438
		//    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => $line[12]));
439
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => round($line['speed'])));
440
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed_fromsrc' => true));
441
		    //$dataFound = true;
442
		} 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'])) {
443
		    $distance = $Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m');
444
		    if ($distance > 1000 && $distance < 10000) {
445
		    // use datetime
446
			$speed = $distance/(time() - $this->all_flights[$id]['time_last_coord']);
447
			$speed = $speed*3.6;
448
			if ($speed < 1000) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('speed' => round($speed)));
449
  			if ($globalDebug) echo "ø Calculated Speed for ".$this->all_flights[$id]['hex']." : ".$speed." - distance : ".$distance."\n";
450
		    }
451
		}
452
453
454
455
	        if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '' && is_numeric($line['latitude']) && is_numeric($line['longitude'])) {
456
	    	    if (isset($this->all_flights[$id]['time_last_coord'])) $timediff = round(time()-$this->all_flights[$id]['time_last_coord']);
457
	    	    else unset($timediff);
458
	    	    if ($this->tmd > 5 || (isset($globalIVAO) && $globalIVAO) || (isset($globalVATSIM) && $globalVATSIM) || (isset($globalphpVMS) && $globalphpVMS) || (isset($globalVAM) && $globalVAM) || !isset($timediff) || $timediff > 2000 || ($timediff > 30 && isset($this->all_flights[$id]['latitude']) && isset($this->all_flights[$id]['longitude']) && $Common->withinThreshold($timediff,$Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m')))) {
459
			if (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'])) {
460
			    if (!$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'])) {
461
				$this->all_flights[$id]['archive_latitude'] = $line['latitude'];
462
				$this->all_flights[$id]['archive_longitude'] = $line['longitude'];
463
				$this->all_flights[$id]['putinarchive'] = true;
464
				$this->tmd = 0;
465
				if (!isset($globalNoImport) || $globalNoImport === FALSE) {
466
				    if ($globalDebug) echo "\n".' ------- Check Country for '.$this->all_flights[$id]['ident'].' with latitude : '.$line['latitude'].' and longitude : '.$line['longitude'].'.... ';
467
				    $timeelapsed = microtime(true);
468
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
469
					$Spotter = new Spotter($this->db);
470
					$all_country = $Spotter->getCountryFromLatitudeLongitude($line['latitude'],$line['longitude']);
471
					if (!empty($all_country)) $this->all_flights[$id]['over_country'] = $all_country['iso2'];
472
					$Spotter->db = null;
473
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update getCountryFromlatitudeLongitude : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
474
					if ($globalDebug) echo 'FOUND : '.$this->all_flights[$id]['over_country'].' ---------------'."\n";
475
				    }
476
				}
477
			    }
478
			}
479
480
			if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
481
			    //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) {
482
				if (!isset($this->all_flights[$id]['archive_latitude'])) $this->all_flights[$id]['archive_latitude'] = $line['latitude'];
483
				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') {
484
				    $this->all_flights[$id]['livedb_latitude'] = $line['latitude'];
485
				    $dataFound = true;
486
				    $this->all_flights[$id]['time_last_coord'] = time();
487
				}
488
				// elseif ($globalDebug) echo '!*!*! Ignore data, too close to previous one'."\n";
489
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('latitude' => $line['latitude']));
490
				/*
491
				if (abs($this->all_flights[$id]['archive_latitude']-$this->all_flights[$id]['latitude']) > 0.3) {
492
				    $this->all_flights[$id]['archive_latitude'] = $line['latitude'];
493
				    $this->all_flights[$id]['putinarchive'] = true;
494
				    //$putinarchive = true;
495
				}
496
				*/
497
			    /*
498
			    } elseif (isset($this->all_flights[$id]['latitude'])) {
499
				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";
500
			    }
501
			    */
502
			}
503
			if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
504
			    if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
505
			    //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) {
506
				if (!isset($this->all_flights[$id]['archive_longitude'])) $this->all_flights[$id]['archive_longitude'] = $line['longitude'];
507
				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') {
508
				    $this->all_flights[$id]['livedb_longitude'] = $line['longitude'];
509
				    $dataFound = true;
510
				    $this->all_flights[$id]['time_last_coord'] = time();
511
				}
512
				// elseif ($globalDebug) echo '!*!*! Ignore data, too close to previous one'."\n";
513
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('longitude' => $line['longitude']));
514
				/*
515
				if (abs($this->all_flights[$id]['archive_longitude']-$this->all_flights[$id]['longitude']) > 0.3) {
516
				    $this->all_flights[$id]['archive_longitude'] = $line['longitude'];
517
				    $this->all_flights[$id]['putinarchive'] = true;
518
				    //$putinarchive = true;
519
				}
520
				*/
521
			/*
522
			    } elseif (isset($this->all_flights[$id]['longitude'])) {
523
				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";
524
			    }
525
			    */
526
			}
527
528
		    } else if ($globalDebug && $timediff > 30) {
529
			$this->tmd = $this->tmd + 1;
530
			echo '!!! Too much distance in short time... for '.$this->all_flights[$id]['ident']."\n";
531
			echo 'Time : '.$timediff.'s - Distance : '.$Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m')."m -";
532
			echo 'Speed : '.(($Common->distance($line['latitude'],$line['longitude'],$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],'m')/$timediff)*3.6)." km/h - ";
533
			echo 'Lat : '.$line['latitude'].' - long : '.$line['longitude'].' - prev lat : '.$this->all_flights[$id]['latitude'].' - prev long : '.$this->all_flights[$id]['longitude']." \n";
534
		    }
535
		}
536
		if (isset($line['last_update']) && $line['last_update'] != '') {
537
		    if (isset($this->all_flights[$id]['last_update']) && $this->all_flights[$id]['last_update'] != $line['last_update']) $dataFound = true;
538
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('last_update' => $line['last_update']));
539
		}
540
		if (isset($line['verticalrate']) && $line['verticalrate'] != '') {
541
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('verticalrate' => $line['verticalrate']));
542
		    //$dataFound = true;
543
		}
544
		if (isset($line['format_source']) && $line['format_source'] != '') {
545
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('format_source' => $line['format_source']));
546
		}
547
		if (isset($line['source_name']) && $line['source_name'] != '') {
548
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('source_name' => $line['source_name']));
549
		}
550
		if (isset($line['emergency']) && $line['emergency'] != '') {
551
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('emergency' => $line['emergency']));
552
		    //$dataFound = true;
553
		}
554
		if (isset($line['ground']) && $line['ground'] != '') {
555
		    if (isset($this->all_flights[$id]['ground']) && $this->all_flights[$id]['ground'] == 1 && $line['ground'] == 0) {
556
			// Here we force archive of flight because after ground it's a new one (or should be)
557
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('addedSpotter' => 0));
558
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 1));
559
			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')));
560
		        elseif (isset($line['id'])) $this->all_flights[$id] = array_merge($this->all_flights[$id],array('id' => $line['id']));
561
			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']));
562
		    }
563
		    if ($line['ground'] != 1) $line['ground'] = 0;
564
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('ground' => $line['ground']));
565
		    //$dataFound = true;
566
		}
567
		if (isset($line['squawk']) && $line['squawk'] != '') {
568
		    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'])) {
569
			    if ($this->all_flights[$id]['squawk'] != $line['squawk']) $this->all_flights[$id]['putinarchive'] = true;
570
			    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('squawk' => $line['squawk']));
571
			    $highlight = '';
572
			    if ($this->all_flights[$id]['squawk'] == '7500') $highlight = 'Squawk 7500 : Hijack at '.date('Y-m-d G:i').' UTC';
573
			    if ($this->all_flights[$id]['squawk'] == '7600') $highlight = 'Squawk 7600 : Lost Comm (radio failure) at '.date('Y-m-d G:i').' UTC';
574
			    if ($this->all_flights[$id]['squawk'] == '7700') $highlight = 'Squawk 7700 : Emergency at '.date('Y-m-d G:i').' UTC';
575
			    if ($highlight != '') {
576
				$timeelapsed = microtime(true);
577
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
578
				    $Spotter = new Spotter($this->db);
579
				    $Spotter->setHighlightFlight($this->all_flights[$id]['id'],$highlight);
580
				    $Spotter->db = null;
581
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update sethighlightflight : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
582
				}
583
				//$putinarchive = true;
584
				//$highlight = '';
585
			    }
586
			    
587
		    } else $this->all_flights[$id] = array_merge($this->all_flights[$id],array('squawk' => $line['squawk']));
588
		    //$dataFound = true;
589
		}
590
591
		if (isset($line['altitude']) && $line['altitude'] != '') {
592
		    //if (!isset($this->all_flights[$id]['altitude']) || $this->all_flights[$id]['altitude'] == '' || ($this->all_flights[$id]['altitude'] > 0 && $line['altitude'] != 0)) {
593
			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;
594
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude' => round($line['altitude']/100)));
595
			$this->all_flights[$id] = array_merge($this->all_flights[$id],array('altitude_real' => $line['altitude']));
596
			//$dataFound = true;
597
		    //} elseif ($globalDebug) echo "!!! Strange altitude data... not added.\n";
598
  		}
599
600
		if (isset($line['noarchive']) && $line['noarchive'] === true) {
601
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('noarchive' => true));
602
		}
603
		
604
		if (isset($line['heading']) && $line['heading'] != '') {
605
		    if (is_int($this->all_flights[$id]['heading']) && abs($this->all_flights[$id]['heading']-round($line['heading'])) > 10) $this->all_flights[$id]['putinarchive'] = true;
606
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => round($line['heading'])));
607
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading_fromsrc' => true));
608
		    //$dataFound = true;
609
  		} 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']) {
610
  		    $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']);
611
		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => round($heading)));
612
		    if (abs($this->all_flights[$id]['heading']-round($heading)) > 10) $this->all_flights[$id]['putinarchive'] = true;
613
  		    if ($globalDebug) echo "ø Calculated Heading for ".$this->all_flights[$id]['hex']." : ".$heading."\n";
614
  		} elseif (isset($this->all_flights[$id]['format_source']) && $this->all_flights[$id]['format_source'] == 'ACARS') {
615
  		    // If not enough messages and ACARS set heading to 0
616
  		    $this->all_flights[$id] = array_merge($this->all_flights[$id],array('heading' => 0));
617
  		}
618
		if (isset($globalSourcesupdate) && $globalSourcesupdate != '' && isset($this->all_flights[$id]['lastupdate']) && time()-$this->all_flights[$id]['lastupdate'] < $globalSourcesupdate) $dataFound = false;
619
		elseif (isset($globalSBS1update) && $globalSBS1update != '' && isset($this->all_flights[$id]['lastupdate']) && time()-$this->all_flights[$id]['lastupdate'] < $globalSBS1update) $dataFound = false;
620
621
//		print_r($this->all_flights[$id]);
622
		//gets the callsign from the last hour
623
		//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'] != '') {
624
		//if ($dataFound == true && isset($this->all_flights[$id]['hex']) && $this->all_flights[$id]['ident'] != '' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
625
		//if ($dataFound === true && isset($this->all_flights[$id]['hex']) && $this->all_flights[$id]['heading'] != '' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
626
		if ($dataFound === true && isset($this->all_flights[$id]['hex'])) {
627
		    $this->all_flights[$id]['lastupdate'] = time();
628
		    if ((!isset($globalNoImport) || $globalNoImport === FALSE) && $this->all_flights[$id]['addedSpotter'] == 0) {
629
		        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'])) {
630
			    //print_r($this->all_flights);
631
			    //echo $this->all_flights[$id]['id'].' - '.$this->all_flights[$id]['addedSpotter']."\n";
632
			    //$last_hour_ident = Spotter->getIdentFromLastHour($this->all_flights[$id]['ident']);
633
			    if (!isset($this->all_flights[$id]['forcenew']) || $this->all_flights[$id]['forcenew'] == 0) {
634
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
635
				    if ($globalDebug) echo "Check if aircraft is already in DB...";
636
				    $timeelapsed = microtime(true);
637
				    $SpotterLive = new SpotterLive($this->db);
638
				    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' || $line['format_source'] === 'aircraftlistjson' || $line['format_source'] === 'radarvirtueljson')) {
639
					$recent_ident = $SpotterLive->checkModeSRecent($this->all_flights[$id]['hex']);
640
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkModeSRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
641
				    } elseif (isset($line['id'])) {
642
					$recent_ident = $SpotterLive->checkIdRecent($line['id']);
643
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
644
				    } elseif (isset($this->all_flights[$id]['ident']) && $this->all_flights[$id]['ident'] != '') {
645
					$recent_ident = $SpotterLive->checkIdentRecent($this->all_flights[$id]['ident']);
646
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
647
				    } else $recent_ident = '';
648
				    $SpotterLive->db=null;
649
				    if ($globalDebug && $recent_ident == '') echo " Not in DB.\n";
650
				    elseif ($globalDebug && $recent_ident != '') echo " Already in DB.\n";
651
				} else $recent_ident = '';
652
			    } else {
653
				$recent_ident = '';
654
				$this->all_flights[$id] = array_merge($this->all_flights[$id],array('forcenew' => 0));
655
			    }
656
			    //if there was no aircraft with the same callsign within the last hour and go post it into the archive
657
			    if($recent_ident == "")
658
			    {
659
				if ($globalDebug) echo "\o/ Add ".$this->all_flights[$id]['ident']." in archive DB : ";
660
				if ($this->all_flights[$id]['departure_airport'] == "") { $this->all_flights[$id]['departure_airport'] = "NA"; }
661
				if ($this->all_flights[$id]['arrival_airport'] == "") { $this->all_flights[$id]['arrival_airport'] = "NA"; }
662
				//adds the spotter data for the archive
663
				$ignoreImport = false;
664
				foreach($globalAirportIgnore as $airportIgnore) {
665
				    if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
666
					$ignoreImport = true;
667
				    }
668
				}
669
				if (count($globalAirportAccept) > 0) {
670
				    $ignoreImport = true;
671
				    foreach($globalAirportIgnore as $airportIgnore) {
672
					if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
673
					    $ignoreImport = false;
674
					}
675
				    }
676
				}
677
				if (isset($globalAirlineIgnore) && is_array($globalAirlineIgnore)) {
678
				    foreach($globalAirlineIgnore as $airlineIgnore) {
679
					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)) {
680
					    $ignoreImport = true;
681
					}
682
				    }
683
				}
684
				if (isset($globalAirlineAccept) && count($globalAirlineAccept) > 0) {
685
				    $ignoreImport = true;
686
				    foreach($globalAirlineAccept as $airlineAccept) {
687
					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)) {
688
					    $ignoreImport = false;
689
					}
690
				    }
691
				}
692
				if (isset($globalPilotIdAccept) && count($globalPilotIdAccept) > 0) {
693
				    $ignoreImport = true;
694
				    foreach($globalPilotIdAccept as $pilotIdAccept) {
695
					if ($this->all_flights[$id]['pilot_id'] == $pilotIdAccept) {
696
					    $ignoreImport = false;
697
					}
698
				    }
699
				}
700
				
701
				if (!$ignoreImport) {
702
				    $highlight = '';
703
				    if ($this->all_flights[$id]['squawk'] == '7500') $highlight = 'Squawk 7500 : Hijack';
704
				    if ($this->all_flights[$id]['squawk'] == '7600') $highlight = 'Squawk 7600 : Lost Comm (radio failure)';
705
				    if ($this->all_flights[$id]['squawk'] == '7700') $highlight = 'Squawk 7700 : Emergency';
706
				    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')));
707
				    $timeelapsed = microtime(true);
708
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
709
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
710
					    $Spotter = new Spotter($this->db);
711
					    $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']);
712
					    $Spotter->db = null;
713
					    if ($globalDebug && isset($result)) echo $result."\n";
714
					}
715
				    }
716
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update addspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
717
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
718
719
				    // Add source stat in DB
720
				    $Stats = new Stats($this->db);
721
				    if (!empty($this->stats)) {
722
					if ($globalDebug) echo 'Add source stats : ';
723
				        foreach($this->stats as $date => $data) {
724
					    foreach($data as $source => $sourced) {
725
					        //print_r($sourced);
726
				    	        if (isset($sourced['polar'])) echo $Stats->addStatSource(json_encode($sourced['polar']),$source,'polar',$date);
727
				    	        if (isset($sourced['hist'])) echo $Stats->addStatSource(json_encode($sourced['hist']),$source,'hist',$date);
728
				    		if (isset($sourced['msg'])) {
729
				    		    if (time() - $sourced['msg']['date'] > 10) {
730
				    		        $nbmsg = round($sourced['msg']['nb']/(time() - $sourced['msg']['date']));
731
				    		        echo $Stats->addStatSource($nbmsg,$source,'msg',$date);
732
			    			        unset($this->stats[$date][$source]['msg']);
733
			    			    }
734
			    			}
735
			    		    }
736
			    		    if ($date != date('Y-m-d')) {
737
			    			unset($this->stats[$date]);
738
			    		    }
739
				    	}
740
				    	if ($globalDebug) echo 'Done'."\n";
741
742
				    }
743
				    $Stats->db = null;
744
				    }
745
				    $this->del();
746
				} elseif ($globalDebug) echo 'Ignore data'."\n";
747
				//$ignoreImport = false;
748
				$this->all_flights[$id]['addedSpotter'] = 1;
749
				//print_r($this->all_flights[$id]);
750
			/*
751
			if (isset($globalArchive) && $globalArchive) {
752
			    $archives_ident = SpotterLive->getAllLiveSpotterDataByIdent($this->all_flights[$id]['ident']);
753
			    foreach ($archives_ident as $archive) {
754
				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'],
755
				$archive['arrival_airport_icao'],$archive['arrival_airport_name'],$archive['arrival_airport_city'],$archive['arrival_airport_country'],$archive['arrival_airport_time'],
756
				$archive['route_stop'],$archive['date'],$archive['latitude'], $archive['longitude'], $archive['waypoints'], $archive['altitude'], $archive['heading'], $archive['ground_speed'],
757
				$archive['squawk'],$archive['ModeS']);
758
			    }
759
			}
760
			*/
761
			//SpotterLive->deleteLiveSpotterDataByIdent($this->all_flights[$id]['ident']);
762
				if ($this->last_delete == 0 || time() - $this->last_delete > 1800) {
763
				    if ($globalDebug) echo "---- Deleting Live Spotter data older than 9 hours...";
764
				    //SpotterLive->deleteLiveSpotterDataNotUpdated();
765
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
766
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
767
					    $SpotterLive = new SpotterLive($this->db);
768
					    $SpotterLive->deleteLiveSpotterData();
769
					    $SpotterLive->db=null;
770
					}
771
				    }
772
				    if ($globalDebug) echo " Done\n";
773
				    $this->last_delete = time();
774
				}
775
			    } else {
776
				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' || $line['format_source'] === 'aircraftlistjson')) {
777
				    $this->all_flights[$id]['id'] = $recent_ident;
778
				    $this->all_flights[$id]['addedSpotter'] = 1;
779
				}
780
				if (isset($globalDaemon) && !$globalDaemon) {
781
				    if (!isset($globalNoImport) || $globalNoImport === FALSE) {
782
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
783
					    $Spotter = new Spotter($this->db);
784
					    $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']);
0 ignored issues
show
Unused Code introduced by
The call to Spotter::updateLatestSpotterData() has too many arguments starting with $this->all_flights[$id]['arrival_airport_time'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
785
					    $Spotter->db = null;
786
					}
787
				    }
788
				}
789
				
790
			    }
791
			}
792
		    }
793
		    //adds the spotter LIVE data
794
		    //SpotterLive->addLiveSpotterData($flightaware_id, $ident, $aircraft_type, $departure_airport, $arrival_airport, $latitude, $longitude, $waypoints, $altitude, $heading, $groundspeed);
795
		    //echo "\nAdd in Live !! \n";
796
		    //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";
797
		    if ($globalDebug) {
798
			if ((isset($globalIVAO) && $globalIVAO) || (isset($globalVATSIM) && $globalVATSIM) || (isset($globalphpVMS) && $globalphpVMS) || (isset($globalVAM) && $globalVAM)) {
799
				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";
800
				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";
801
			} else {
802
				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";
803
				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";
804
			}
805
		    }
806
		    $ignoreImport = false;
807
		    if ($this->all_flights[$id]['departure_airport'] == "") { $this->all_flights[$id]['departure_airport'] = "NA"; }
808
		    if ($this->all_flights[$id]['arrival_airport'] == "") { $this->all_flights[$id]['arrival_airport'] = "NA"; }
809
810
		    foreach($globalAirportIgnore as $airportIgnore) {
811
		        if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
812
			    $ignoreImport = true;
813
			}
814
		    }
815
		    if (count($globalAirportAccept) > 0) {
816
		        $ignoreImport = true;
817
		        foreach($globalAirportIgnore as $airportIgnore) {
818
			    if (($this->all_flights[$id]['departure_airport'] == $airportIgnore) || ($this->all_flights[$id]['arrival_airport'] == $airportIgnore)) {
819
				$ignoreImport = false;
820
			    }
821
			}
822
		    }
823
		    if (isset($globalAirlineIgnore) && is_array($globalAirlineIgnore)) {
824
			foreach($globalAirlineIgnore as $airlineIgnore) {
825
			    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)) {
826
				$ignoreImport = true;
827
			    }
828
			}
829
		    }
830
		    if (isset($globalAirlineAccept) && count($globalAirlineAccept) > 0) {
831
			$ignoreImport = true;
832
			foreach($globalAirlineAccept as $airlineAccept) {
833
			    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)) {
834
				$ignoreImport = false;
835
			    }
836
			}
837
		    }
838
		    if (isset($globalPilotIdAccept) && count($globalPilotIdAccept) > 0) {
839
			$ignoreImport = true;
840
			foreach($globalPilotIdAccept as $pilotIdAccept) {
841
			    if ($this->all_flights[$id]['pilot_id'] == $pilotIdAccept) {
842
			        $ignoreImport = false;
843
			    }
844
			}
845
		    }
846
847
		    if (!$ignoreImport) {
848
			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'])) {
849
				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')));
850
				$timeelapsed = microtime(true);
851
				if (!isset($globalNoImport) || $globalNoImport === FALSE) {
852
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
853
					if ($globalDebug) echo "\o/ Add ".$this->all_flights[$id]['ident']." from ".$this->all_flights[$id]['format_source']." in Live DB : ";
854
					$SpotterLive = new SpotterLive($this->db);
855
					$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']);
856
					$SpotterLive->db = null;
857
					if ($globalDebug) echo $result."\n";
858
				    }
859
				}
860
				if (isset($globalServerAPRS) && $globalServerAPRS) {
861
					$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']);
862
				}
863
				$this->all_flights[$id]['putinarchive'] = false;
864
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update addlivespotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
865
866
				// Put statistics in $this->stats variable
867
				//if ($line['format_source'] != 'aprs') {
868
				//if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt')) {
869
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
870
				    if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE && $line['format_source'] != 'aprs' && $this->all_flights[$id]['latitude'] != '' && $this->all_flights[$id]['longitude'] != '') {
871
					$source = $this->all_flights[$id]['source_name'];
872
					if ($source == '') $source = $this->all_flights[$id]['format_source'];
873
					if (!isset($this->source_location[$source])) {
874
						$Location = new Source();
875
						$coord = $Location->getLocationInfobySourceName($source);
876
						if (count($coord) > 0) {
877
							$latitude = $coord[0]['latitude'];
878
							$longitude = $coord[0]['longitude'];
879
						} else {
880
							$latitude = $globalCenterLatitude;
881
							$longitude = $globalCenterLongitude;
882
						}
883
						$this->source_location[$source] = array('latitude' => $latitude,'longitude' => $longitude);
884
					} else {
885
						$latitude = $this->source_location[$source]['latitude'];
886
						$longitude = $this->source_location[$source]['longitude'];
887
					}
888
					$stats_heading = $Common->getHeading($latitude,$longitude,$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude']);
889
					//$stats_heading = $stats_heading%22.5;
890
					$stats_heading = round($stats_heading/22.5);
891
					$stats_distance = $Common->distance($latitude,$longitude,$this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude']);
892
					$current_date = date('Y-m-d');
893
					if ($stats_heading == 16) $stats_heading = 0;
894
					if (!isset($this->stats[$current_date][$source]['polar'][1])) {
895
						for ($i=0;$i<=15;$i++) {
896
						    $this->stats[$current_date][$source]['polar'][$i] = 0;
897
						}
898
						$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
899
					} else {
900
						if ($this->stats[$current_date][$source]['polar'][$stats_heading] < $stats_distance) {
901
							$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
902
						}
903
					}
904
					$distance = (round($stats_distance/10)*10);
905
					//echo '$$$$$$$$$$ DISTANCE : '.$distance.' - '.$source."\n";
906
					//var_dump($this->stats);
907
					if (!isset($this->stats[$current_date][$source]['hist'][$distance])) {
908
						if (isset($this->stats[$current_date][$source]['hist'][0])) {
909
						    end($this->stats[$current_date][$source]['hist']);
910
						    $mini = key($this->stats[$current_date][$source]['hist'])+10;
911
						} else $mini = 0;
912
						for ($i=$mini;$i<=$distance;$i+=10) {
913
						    $this->stats[$current_date][$source]['hist'][$i] = 0;
914
						}
915
						$this->stats[$current_date][$source]['hist'][$distance] = 1;
916
					} else {
917
						$this->stats[$current_date][$source]['hist'][$distance] += 1;
918
					}
919
				    }
920
				}
921
922
				$this->all_flights[$id]['lastupdate'] = time();
923
				if ($this->all_flights[$id]['putinarchive']) $send = true;
924
				//if ($globalDebug) echo "Distance : ".Common->distance($this->all_flights[$id]['latitude'],$this->all_flights[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude'])."\n";
925
			} 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";
926
			//$this->del();
927
			
928
			if ($this->last_delete_hourly == 0 || time() - $this->last_delete_hourly > 900) {
929
			    if ((!isset($globalNoImport) || $globalNoImport === FALSE) && (!isset($globalNoDB) || $globalNoDB !== TRUE)) {
930
				if ($globalDebug) echo "---- Deleting Live Spotter data Not updated since 2 hour...";
931
				$SpotterLive = new SpotterLive($this->db);
932
				$SpotterLive->deleteLiveSpotterDataNotUpdated();
933
				$SpotterLive->db = null;
934
				//SpotterLive->deleteLiveSpotterData();
935
				if ($globalDebug) echo " Done\n";
936
				$this->last_delete_hourly = time();
937
			    } else {
938
				$this->del();
939
				$this->last_delete_hourly = time();
940
			    }
941
			}
942
			
943
		    }
944
		    //$ignoreImport = false;
945
		}
946
		//if (function_exists('pcntl_fork') && $globalFork) pcntl_signal(SIGCHLD, SIG_IGN);
947
		if ($send) return $this->all_flights[$id];
948
	    }
949
	}
950
    }
951
}
952
?>
953