Completed
Push — master ( e699fe...2e25d0 )
by Yannick
08:35
created

MarineImport::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/class.Connection.php');
3
require_once(dirname(__FILE__).'/class.AIS.php');
4
require_once(dirname(__FILE__).'/class.Marine.php');
5
require_once(dirname(__FILE__).'/class.MarineLive.php');
6
//require_once(dirname(__FILE__).'/class.MarineArchive.php');
7
require_once(dirname(__FILE__).'/class.Scheduler.php');
8
require_once(dirname(__FILE__).'/class.Translation.php');
9
require_once(dirname(__FILE__).'/class.Stats.php');
10
require_once(dirname(__FILE__).'/class.Source.php');
11
if (isset($globalServerAPRS) && $globalServerAPRS) {
12
    require_once(dirname(__FILE__).'/class.APRS.php');
13
}
14
15
class MarineImport {
16
    private $all_tracked = array();
17
    private $last_delete_hourly = 0;
18
    private $last_delete = 0;
19
    private $stats = array();
0 ignored issues
show
Unused Code introduced by
The property $stats is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
    private $tmd = 0;
21
    private $source_location = array();
0 ignored issues
show
Unused Code introduced by
The property $source_location is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
    public $db = null;
23
    public $nb = 0;
24
25
    public function __construct($dbc = null) {
26
	global $globalBeta, $globalServerAPRS, $APRSSpotter, $globalNoDB;
27
	if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
28
	    $Connection = new Connection($dbc);
29
	    $this->db = $Connection->db();
30
	    date_default_timezone_set('UTC');
31
	}
32
	// Get previous source stats
33
	/*
34
	$Stats = new Stats($dbc);
35
	$currentdate = date('Y-m-d');
36
	$sourcestat = $Stats->getStatsSource($currentdate);
37
	if (!empty($sourcestat)) {
38
	    foreach($sourcestat as $srcst) {
39
	    	$type = $srcst['stats_type'];
40
		if ($type == 'polar' || $type == 'hist') {
41
		    $source = $srcst['source_name'];
42
		    $data = $srcst['source_data'];
43
		    $this->stats[$currentdate][$source][$type] = json_decode($data,true);
44
	        }
45
	    }
46
	}
47
	*/
48
	if (isset($globalServerAPRS) && $globalServerAPRS) {
49
	    $APRSMarine = new APRSMarine();
0 ignored issues
show
Unused Code introduced by
$APRSMarine is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
	    //$APRSSpotter->connect();
51
	}
52
    }
53
54
    public function checkAll() {
55
	global $globalDebug;
56
	if ($globalDebug) echo "Update last seen tracked data...\n";
57
	foreach ($this->all_tracked as $key => $flight) {
58
	    if (isset($this->all_tracked[$key]['id'])) {
59
		//echo $this->all_tracked[$key]['id'].' - '.$this->all_tracked[$key]['latitude'].'  '.$this->all_tracked[$key]['longitude']."\n";
60
    		$Marine = new Marine($this->db);
61
        	$Marine->updateLatestMarineData($this->all_tracked[$key]['id'],$this->all_tracked[$key]['ident'],$this->all_tracked[$key]['latitude'],$this->all_tracked[$key]['longitude'],$this->all_tracked[$key]['speed'],$this->all_tracked[$key]['datetime']);
62
            }
63
	}
64
    }
65
66
    public function del() {
67
	global $globalDebug;
68
	// Delete old infos
69
	if ($globalDebug) echo 'Delete old values and update latest data...'."\n";
70
	foreach ($this->all_tracked as $key => $flight) {
71
    	    if (isset($flight['lastupdate'])) {
72
        	if ($flight['lastupdate'] < (time()-3000)) {
73
            	    if (isset($this->all_tracked[$key]['id'])) {
74
            		if ($globalDebug) echo "--- Delete old values with id ".$this->all_tracked[$key]['id']."\n";
75
			/*
76
			$MarineLive = new MarineLive();
77
            		$MarineLive->deleteLiveMarineDataById($this->all_tracked[$key]['id']);
78
			$MarineLive->db = null;
79
			*/
80
            		//$real_arrival = $this->arrival($key);
81
            		$Marine = new Marine($this->db);
82
            		if ($this->all_tracked[$key]['latitude'] != '' && $this->all_tracked[$key]['longitude'] != '') {
83
				$result = $Marine->updateLatestMarineData($this->all_tracked[$key]['id'],$this->all_tracked[$key]['ident'],$this->all_tracked[$key]['latitude'],$this->all_tracked[$key]['longitude'],$this->all_tracked[$key]['speed']);
84
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
85
			}
86
			// Put in archive
87
//			$Marine->db = null;
88
            	    }
89
            	    unset($this->all_tracked[$key]);
90
    	        }
91
	    }
92
        }
93
    }
94
95
    public function add($line) {
96
	global $globalFork, $globalDistanceIgnore, $globalDaemon, $globalDebug, $globalCoordMinChange, $globalDebugTimeElapsed, $globalCenterLatitude, $globalCenterLongitude, $globalBeta, $globalSourcesupdate, $globalAllTracked, $globalNoImport, $globalNoDB;
97
	if (!isset($globalCoordMinChange) || $globalCoordMinChange == '') $globalCoordMinChange = '0.02';
98
	date_default_timezone_set('UTC');
99
	$dataFound = false;
100
	$send = false;
101
	
102
	// SBS format is CSV format
103
	if(is_array($line) && isset($line['mmsi'])) {
104
	    //print_r($line);
105
  	    if (isset($line['mmsi'])) {
106
107
		/*
108
		// Increment message number
109
		if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE) {
110
		    $current_date = date('Y-m-d');
111
		    $source = $line['source_name'];
112
		    if ($source == '' || $line['format_source'] == 'aprs') $source = $line['format_source'];
113
		    if (!isset($this->stats[$current_date][$source]['msg'])) {
114
		    	$this->stats[$current_date][$source]['msg']['date'] = time();
115
		    	$this->stats[$current_date][$source]['msg']['nb'] = 1;
116
		    } else $this->stats[$current_date][$source]['msg']['nb'] += 1;
117
		}
118
		*/
119
		
120
		$Common = new Common();
121
		$AIS = new AIS();
122
	        if (!isset($line['id'])) $id = trim($line['mmsi']);
123
	        else $id = trim($line['id']);
124
		
125
		if (!isset($this->all_tracked[$id])) {
126
		    $this->all_tracked[$id] = array();
127
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('addedMarine' => 0));
128
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('ident' => '','latitude' => '', 'longitude' => '', 'speed' => '', 'heading' => '', 'format_source' => '','source_name' => '','comment'=> '','type' => '','typeid' => '','noarchive' => false,'putinarchive' => true,'over_country' => '','mmsi' => '','status' => '','imo' => '','callsign' => '','arrival_code' => '','arrival_date' => ''));
129
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('lastupdate' => time()));
130
		    if (!isset($line['id'])) {
131
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
132
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $id.'-'.date('YmdHi')));
133
		     } else $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $line['id']));
134
		    if ($globalAllTracked !== FALSE) $dataFound = true;
135
		}
136
		
137
		if (isset($line['mmsi']) && $line['mmsi'] != '' && $line['mmsi'] != $this->all_tracked[$id]['mmsi']) {
138
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('mmsi' => $line['mmsi']));
139
		    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
140
			$Marine = new Marine($this->db);
141
			$identity = $Marine->getIdentity($line['mmsi']);
142
			if (!empty($identity)) {
143
			    $this->all_tracked[$id]['ident'] = $identity['ship_name'];
144
			    $this->all_tracked[$id]['type'] = $identity['type'];
145
			}
146
			//print_r($identity);
147
			unset($Marine);
148
			//$dataFound = true;
149
		    }
150
		}
151
		if (isset($line['type_id']) && $line['type_id'] != '') {
152
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('type' => $AIS->getShipType($line['type_id'])));
153
		}
154
		if (isset($line['type']) && $line['type'] != '') {
155
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('type' => $line['type']));
156
		}
157
		if (isset($line['imo']) && $line['imo'] != '') {
158
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('imo' => $line['imo']));
159
		}
160
		if (isset($line['callsign']) && $line['callsign'] != '') {
161
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('callsign' => $line['callsign']));
162
		}
163
		if (isset($line['arrival_code']) && $line['arrival_code'] != '') {
164
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('arrival_code' => $line['arrival_code']));
165
		}
166
		if (isset($line['arrival_date']) && $line['arrival_date'] != '') {
167
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('arrival_date' => $line['arrival_date']));
168
		}
169
170
171
		//if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_tracked[$id]['ident'] != trim($line['ident'])) && preg_match('/^[a-zA-Z0-9-]+$/', $line['ident'])) {
172
		if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_tracked[$id]['ident'] != trim($line['ident']))) {
173
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('ident' => trim($line['ident'])));
174
		    if ($this->all_tracked[$id]['addedMarine'] == 1) {
175
			if (!isset($globalNoImport) || $globalNoImport !== TRUE) {
176
			    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
177
				$timeelapsed = microtime(true);
178
				$Marine = new Marine($this->db);
179
				$fromsource = NULL;
180
				$result = $Marine->updateIdentMarineData($this->all_tracked[$id]['id'],$this->all_tracked[$id]['ident'],$fromsource);
181
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
182
				$Marine->db = null;
183
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update identspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
184
			    }
185
			}
186
		    }
187
		    if (!isset($this->all_tracked[$id]['id'])) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $this->all_tracked[$id]['ident']));
188
		}
189
190
		if (isset($line['speed']) && $line['speed'] != '') {
191
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($line['speed'])));
192
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed_fromsrc' => true));
193
		} else if (!isset($this->all_tracked[$id]['speed_fromsrc']) && isset($this->all_tracked[$id]['time_last_coord']) && $this->all_tracked[$id]['time_last_coord'] != time() && isset($line['latitude']) && isset($line['longitude'])) {
194
		    $distance = $Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m');
195
		    if ($distance > 1000 && $distance < 10000) {
196
			$speed = $distance/(time() - $this->all_tracked[$id]['time_last_coord']);
197
			$speed = $speed*3.6;
198
			if ($speed < 1000) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($speed)));
199
  			if ($globalDebug) echo "ø Calculated Speed for ".$this->all_tracked[$id]['hex']." : ".$speed." - distance : ".$distance."\n";
200
		    }
201
		}
202
203
	        if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '' && is_numeric($line['latitude']) && is_numeric($line['longitude'])) {
204
	    	    if (isset($this->all_tracked[$id]['time_last_coord'])) $timediff = round(time()-$this->all_tracked[$id]['time_last_coord']);
205
	    	    else unset($timediff);
206
	    	    if ($this->tmd > 5 || !isset($timediff) || $timediff > 2000 || ($timediff > 30 && isset($this->all_tracked[$id]['latitude']) && isset($this->all_tracked[$id]['longitude']) && $Common->withinThreshold($timediff,$Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')))) {
207
			if (isset($this->all_tracked[$id]['archive_latitude']) && isset($this->all_tracked[$id]['archive_longitude']) && isset($this->all_tracked[$id]['livedb_latitude']) && isset($this->all_tracked[$id]['livedb_longitude'])) {
208
			    if (!$Common->checkLine($this->all_tracked[$id]['archive_latitude'],$this->all_tracked[$id]['archive_longitude'],$this->all_tracked[$id]['livedb_latitude'],$this->all_tracked[$id]['livedb_longitude'],$line['latitude'],$line['longitude'])) {
209
				$this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
210
				$this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
211
				$this->all_tracked[$id]['putinarchive'] = true;
212
				
213
				if ($globalDebug) echo "\n".' ------- Check Country for '.$this->all_tracked[$id]['ident'].' with latitude : '.$line['latitude'].' and longitude : '.$line['longitude'].'.... ';
214
				$timeelapsed = microtime(true);
215
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
216
				    $Marine = new Marine($this->db);
217
				    $all_country = $Marine->getCountryFromLatitudeLongitude($line['latitude'],$line['longitude']);
218
				    if (!empty($all_country)) $this->all_tracked[$id]['over_country'] = $all_country['iso2'];
219
				    $Marine->db = null;
220
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update getCountryFromlatitudeLongitude : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
221
				}
222
				$this->tmd = 0;
223
				if ($globalDebug) echo 'FOUND : '.$this->all_tracked[$id]['over_country'].' ---------------'."\n";
224
			    }
225
			}
226
227
			if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
228
				if (!isset($this->all_tracked[$id]['archive_latitude'])) $this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
229
				if (!isset($this->all_tracked[$id]['livedb_latitude']) || abs($this->all_tracked[$id]['livedb_latitude']-$line['latitude']) > $globalCoordMinChange || $this->all_tracked[$id]['format_source'] == 'aprs') {
230
				    $this->all_tracked[$id]['livedb_latitude'] = $line['latitude'];
231
				    $dataFound = true;
232
				    $this->all_tracked[$id]['time_last_coord'] = time();
233
				}
234
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('latitude' => $line['latitude']));
235
			}
236
			if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
237
			    if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
238
				if (!isset($this->all_tracked[$id]['archive_longitude'])) $this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
239
				if (!isset($this->all_tracked[$id]['livedb_longitude']) || abs($this->all_tracked[$id]['livedb_longitude']-$line['longitude']) > $globalCoordMinChange || $this->all_tracked[$id]['format_source'] == 'aprs') {
240
				    $this->all_tracked[$id]['livedb_longitude'] = $line['longitude'];
241
				    $dataFound = true;
242
				    $this->all_tracked[$id]['time_last_coord'] = time();
243
				}
244
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('longitude' => $line['longitude']));
245
			}
246
247
		    } else if ($globalDebug && $timediff > 20) {
248
			$this->tmd = $this->tmd + 1;
249
			echo '!!! Too much distance in short time... for '.$this->all_tracked[$id]['ident']."\n";
250
			echo 'Time : '.$timediff.'s - Distance : '.$Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')."m -";
251
			echo 'Speed : '.(($Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')/$timediff)*3.6)." km/h - ";
252
			echo 'Lat : '.$line['latitude'].' - long : '.$line['longitude'].' - prev lat : '.$this->all_tracked[$id]['latitude'].' - prev long : '.$this->all_tracked[$id]['longitude']." \n";
253
		    }
254
		}
255
		if (isset($line['last_update']) && $line['last_update'] != '') {
256
		    if (isset($this->all_tracked[$id]['last_update']) && $this->all_tracked[$id]['last_update'] != $line['last_update']) $dataFound = true;
257
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('last_update' => $line['last_update']));
258
		}
259
		if (isset($line['format_source']) && $line['format_source'] != '') {
260
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('format_source' => $line['format_source']));
261
		}
262
		if (isset($line['source_name']) && $line['source_name'] != '') {
263
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('source_name' => $line['source_name']));
264
		}
265
		if (isset($line['status']) && $line['status'] != '') {
266
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('status' => $line['status']));
267
		}
268
269
		if (isset($line['noarchive']) && $line['noarchive'] === true) {
270
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('noarchive' => true));
271
		}
272
		
273
		if (isset($line['heading']) && $line['heading'] != '') {
274
		    if (is_int($this->all_tracked[$id]['heading']) && abs($this->all_tracked[$id]['heading']-round($line['heading'])) > 10) $this->all_tracked[$id]['putinarchive'] = true;
275
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($line['heading'])));
276
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading_fromsrc' => true));
277
		    //$dataFound = true;
278
  		} elseif (!isset($this->all_tracked[$id]['heading_fromsrc']) && isset($this->all_tracked[$id]['archive_latitude']) && $this->all_tracked[$id]['archive_latitude'] != $this->all_tracked[$id]['latitude'] && isset($this->all_tracked[$id]['archive_longitude']) && $this->all_tracked[$id]['archive_longitude'] != $this->all_tracked[$id]['longitude']) {
279
  		    $heading = $Common->getHeading($this->all_tracked[$id]['archive_latitude'],$this->all_tracked[$id]['archive_longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
280
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($heading)));
281
		    if (abs($this->all_tracked[$id]['heading']-round($heading)) > 10) $this->all_tracked[$id]['putinarchive'] = true;
282
  		    if ($globalDebug) echo "ø Calculated Heading for ".$this->all_tracked[$id]['ident']." : ".$heading."\n";
283
  		}
284
		//if (isset($globalSourcesupdate) && $globalSourcesupdate != '' && isset($this->all_tracked[$id]['lastupdate']) && time()-$this->all_tracked[$id]['lastupdate'] < $globalSourcesupdate) $dataFound = false;
285
286
		if (isset($line['datetime'])) {
287
		    if (!isset($this->all_tracked[$id]['datetime']) || strtotime($line['datetime']) > strtotime($this->all_tracked[$id]['datetime'])) {
288
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => $line['datetime']));
289
		    } else {
290
				if (strtotime($line['datetime']) == strtotime($this->all_tracked[$id]['datetime']) && $globalDebug) echo "!!! Date is the same as previous data for ".$this->all_tracked[$id]['mmsi']."\n";
291
				elseif (strtotime($line['datetime']) > strtotime($this->all_tracked[$id]['datetime']) && $globalDebug) echo "!!! Date previous latest data (".$line['datetime']." > ".$this->all_tracked[$id]['datetime'].") !!! for ".$this->all_tracked[$id]['hex']." - format : ".$line['format_source']."\n";
292
				return '';
293
		    }
294
		} else {
295
			date_default_timezone_set('UTC');
296
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => date('Y-m-d H:i:s')));
297
		}
298
299
300
		if ($dataFound === true && isset($this->all_tracked[$id]['mmsi'])) {
301
		    $this->all_tracked[$id]['lastupdate'] = time();
302
		    if ($this->all_tracked[$id]['addedMarine'] == 0) {
303
		        if (!isset($globalDistanceIgnore['latitude']) || $this->all_tracked[$id]['longitude'] == ''  || $this->all_tracked[$id]['latitude'] == '' || (isset($globalDistanceIgnore['latitude']) && $Common->distance($this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude']) < $globalDistanceIgnore['distance'])) {
304
			    if (!isset($this->all_tracked[$id]['forcenew']) || $this->all_tracked[$id]['forcenew'] == 0) {
305
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
306
				    if ($globalDebug) echo "Check if aircraft is already in DB...";
307
				    $timeelapsed = microtime(true);
308
				    $MarineLive = new MarineLive($this->db);
309
				    if (isset($line['id'])) {
310
					$recent_ident = $MarineLive->checkIdRecent($line['id']);
311
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
312
				    } elseif (isset($this->all_tracked[$id]['mmsi']) && $this->all_tracked[$id]['mmsi'] != '') {
313
					$recent_ident = $MarineLive->checkMMSIRecent($this->all_tracked[$id]['mmsi']);
314
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
315
				    } elseif (isset($this->all_tracked[$id]['ident']) && $this->all_tracked[$id]['ident'] != '') {
316
					$recent_ident = $MarineLive->checkIdentRecent($this->all_tracked[$id]['ident']);
317
					if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
318
				    } else $recent_ident = '';
319
				    $MarineLive->db=null;
320
				    if ($globalDebug && $recent_ident == '') echo " Not in DB.\n";
321
				    elseif ($globalDebug && $recent_ident != '') echo " Already in DB.\n";
322
				} else $recent_ident = '';
323
			    } else {
324
				$recent_ident = '';
325
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('forcenew' => 0));
326
			    }
327
			    //if there was no aircraft with the same callsign within the last hour and go post it into the archive
328
			    if($recent_ident == "" && $this->all_tracked[$id]['latitude'] != '' && $this->all_tracked[$id]['longitude'] != '')
329
			    {
330
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['mmsi']." in archive DB : ";
331
				//adds the spotter data for the archive
332
				    $highlight = '';
0 ignored issues
show
Unused Code introduced by
$highlight is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
333
				    if (!isset($this->all_tracked[$id]['id'])) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $this->all_tracked[$id]['mmsi'].'-'.date('YmdHi')));
334
				    if (!isset($globalNoImport) || $globalNoImport !== TRUE) {
335
					if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
336
					    $timeelapsed = microtime(true);
337
					    $Marine = new Marine($this->db);
338
					    $result = $Marine->addMarineData($this->all_tracked[$id]['id'], $this->all_tracked[$id]['ident'], $this->all_tracked[$id]['latitude'], $this->all_tracked[$id]['longitude'], $this->all_tracked[$id]['heading'], $this->all_tracked[$id]['speed'], $this->all_tracked[$id]['datetime'], $this->all_tracked[$id]['mmsi'], $this->all_tracked[$id]['type'],$this->all_tracked[$id]['typeid'],$this->all_tracked[$id]['imo'],$this->all_tracked[$id]['callsign'],$this->all_tracked[$id]['arrival_code'],$this->all_tracked[$id]['arrival_date'], $this->all_tracked[$id]['status'],$this->all_tracked[$id]['format_source'],$this->all_tracked[$id]['source_name']);
339
					    $Marine->db = null;
340
					    if ($globalDebug && isset($result)) echo $result."\n";
341
					    if ($globalDebugTimeElapsed) echo 'Time elapsed for update addspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
342
					}
343
				    }
344
				    /*
345
				    // Add source stat in DB
346
				    $Stats = new Stats($this->db);
347
				    if (!empty($this->stats)) {
348
					if ($globalDebug) echo 'Add source stats : ';
349
				        foreach($this->stats as $date => $data) {
350
					    foreach($data as $source => $sourced) {
351
					        //print_r($sourced);
352
				    	        if (isset($sourced['polar'])) echo $Stats->addStatSource(json_encode($sourced['polar']),$source,'polar',$date);
353
				    	        if (isset($sourced['hist'])) echo $Stats->addStatSource(json_encode($sourced['hist']),$source,'hist',$date);
354
				    		if (isset($sourced['msg'])) {
355
				    		    if (time() - $sourced['msg']['date'] > 10) {
356
				    		        $nbmsg = round($sourced['msg']['nb']/(time() - $sourced['msg']['date']));
357
				    		        echo $Stats->addStatSource($nbmsg,$source,'msg',$date);
358
			    			        unset($this->stats[$date][$source]['msg']);
359
			    			    }
360
			    			}
361
			    		    }
362
			    		    if ($date != date('Y-m-d')) {
363
			    			unset($this->stats[$date]);
364
			    		    }
365
				    	}
366
				    	if ($globalDebug) echo 'Done'."\n";
367
368
				    }
369
				    $Stats->db = null;
370
				    */
371
				    $this->del();
372
				//$ignoreImport = false;
373
				$this->all_tracked[$id]['addedMarine'] = 1;
374
				//print_r($this->all_tracked[$id]);
375
				if ($this->last_delete == 0 || time() - $this->last_delete > 1800) {
376
				    if ($globalDebug) echo "---- Deleting Live Marine data older than 9 hours...";
377
				    //MarineLive->deleteLiveMarineDataNotUpdated();
378
				    if (!isset($globaNoDB) || $globalNoDB !== TRUE) {
0 ignored issues
show
Bug introduced by
The variable $globaNoDB does not exist. Did you mean $globalNoDB?

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...
379
					$MarineLive = new MarineLive($this->db);
380
					$MarineLive->deleteLiveMarineData();
381
					$MarineLive->db=null;
382
					if ($globalDebug) echo " Done\n";
383
				    }
384
				    $this->last_delete = time();
385
				}
386
			    } elseif ($recent_ident != '') {
387
				$this->all_tracked[$id]['id'] = $recent_ident;
388
				$this->all_tracked[$id]['addedMarine'] = 1;
389
				if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
390
				    if (isset($globalDaemon) && !$globalDaemon) {
391
					$Marine = new Marine($this->db);
392
					$Marine->updateLatestMarineData($this->all_tracked[$id]['id'],$this->all_tracked[$id]['ident'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$this->all_tracked[$id]['speed'],$this->all_tracked[$id]['datetime']);
393
					$Marine->db = null;
394
				    }
395
				}
396
				
397
			    }
398
			}
399
		    }
400
		    //adds the spotter LIVE data
401
		    if ($globalDebug) {
402
			echo 'DATA : ident : '.$this->all_tracked[$id]['ident'].' - type : '.$this->all_tracked[$id]['type'].' - Latitude : '.$this->all_tracked[$id]['latitude'].' - Longitude : '.$this->all_tracked[$id]['longitude'].' - Heading : '.$this->all_tracked[$id]['heading'].' - Speed : '.$this->all_tracked[$id]['speed']."\n";
403
		    }
404
		    $ignoreImport = false;
405
406
		    if (!$ignoreImport) {
407
			if (!isset($globalDistanceIgnore['latitude']) || (isset($globalDistanceIgnore['latitude']) && $Common->distance($this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude']) < $globalDistanceIgnore['distance'])) {
408
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['ident']." from ".$this->all_tracked[$id]['format_source']." in Live DB : ";
409
				if (!isset($globalNoImport) || $globalNoImport !== TRUE) {
410
				    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
411
					$timeelapsed = microtime(true);
412
					$MarineLive = new MarineLive($this->db);
413
					$result = $MarineLive->addLiveMarineData($this->all_tracked[$id]['id'], $this->all_tracked[$id]['ident'], $this->all_tracked[$id]['latitude'], $this->all_tracked[$id]['longitude'], $this->all_tracked[$id]['heading'], $this->all_tracked[$id]['speed'],$this->all_tracked[$id]['datetime'], $this->all_tracked[$id]['putinarchive'],$this->all_tracked[$id]['mmsi'],$this->all_tracked[$id]['type'],$this->all_tracked[$id]['typeid'],$this->all_tracked[$id]['imo'],$this->all_tracked[$id]['callsign'],$this->all_tracked[$id]['arrival_code'],$this->all_tracked[$id]['arrival_date'],$this->all_tracked[$id]['status'],$this->all_tracked[$id]['noarchive'],$this->all_tracked[$id]['format_source'],$this->all_tracked[$id]['source_name'],$this->all_tracked[$id]['over_country']);
0 ignored issues
show
Documentation introduced by
$this->all_tracked[$id]['putinarchive'] is of type integer|double|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->all_tracked[$id]['noarchive'] is of type integer|double|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
414
					$MarineLive->db = null;
415
					if ($globalDebug) echo $result."\n";
416
				    }
417
				}
418
				if (isset($globalServerAPRS) && $globalServerAPRS && $this->all_flights[$id]['putinarchive']) {
0 ignored issues
show
Bug introduced by
The property all_flights does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $globalServerAPRS seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
419
				    $APRSMarine->addLiveMarineData($this->all_tracked[$id]['id'], $this->all_tracked[$id]['ident'], $this->all_tracked[$id]['latitude'], $this->all_tracked[$id]['longitude'], $this->all_tracked[$id]['heading'], $this->all_tracked[$id]['speed'],$this->all_tracked[$id]['datetime'], $this->all_tracked[$id]['putinarchive'],$this->all_tracked[$id]['mmsi'],$this->all_tracked[$id]['type'],$this->all_tracked[$id]['typeid'],$this->all_tracked[$id]['imo'],$this->all_tracked[$id]['callsign'],$this->all_tracked[$id]['arrival_code'],$this->all_tracked[$id]['arrival_date'],$this->all_tracked[$id]['status'],$this->all_tracked[$id]['noarchive'],$this->all_tracked[$id]['format_source'],$this->all_tracked[$id]['source_name'],$this->all_tracked[$id]['over_country']);
0 ignored issues
show
Bug introduced by
The variable $APRSMarine does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
420
				}
421
				$this->all_tracked[$id]['putinarchive'] = false;
422
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update addlivespotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
0 ignored issues
show
Bug introduced by
The variable $timeelapsed 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...
423
424
				// Put statistics in $this->stats variable
425
				/*
426
				if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE && $line['format_source'] != 'aprs' && $this->all_tracked[$id]['latitude'] != '' && $this->all_tracked[$id]['longitude'] != '') {
427
					$source = $this->all_tracked[$id]['source_name'];
428
					if ($source == '') $source = $this->all_tracked[$id]['format_source'];
429
					if (!isset($this->source_location[$source])) {
430
						$Location = new Source();
431
						$coord = $Location->getLocationInfobySourceName($source);
432
						if (count($coord) > 0) {
433
							$latitude = $coord[0]['latitude'];
434
							$longitude = $coord[0]['longitude'];
435
						} else {
436
							$latitude = $globalCenterLatitude;
437
							$longitude = $globalCenterLongitude;
438
						}
439
						$this->source_location[$source] = array('latitude' => $latitude,'longitude' => $longitude);
440
					} else {
441
						$latitude = $this->source_location[$source]['latitude'];
442
						$longitude = $this->source_location[$source]['longitude'];
443
					}
444
					$stats_heading = $Common->getHeading($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
445
					//$stats_heading = $stats_heading%22.5;
446
					$stats_heading = round($stats_heading/22.5);
447
					$stats_distance = $Common->distance($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
448
					$current_date = date('Y-m-d');
449
					if ($stats_heading == 16) $stats_heading = 0;
450
					if (!isset($this->stats[$current_date][$source]['polar'][1])) {
451
						for ($i=0;$i<=15;$i++) {
452
						    $this->stats[$current_date][$source]['polar'][$i] = 0;
453
						}
454
						$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
455
					} else {
456
						if ($this->stats[$current_date][$source]['polar'][$stats_heading] < $stats_distance) {
457
							$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
458
						}
459
					}
460
					$distance = (round($stats_distance/10)*10);
461
					//echo '$$$$$$$$$$ DISTANCE : '.$distance.' - '.$source."\n";
462
					//var_dump($this->stats);
463
					if (!isset($this->stats[$current_date][$source]['hist'][$distance])) {
464
						if (isset($this->stats[$current_date][$source]['hist'][0])) {
465
						    end($this->stats[$current_date][$source]['hist']);
466
						    $mini = key($this->stats[$current_date][$source]['hist'])+10;
467
						} else $mini = 0;
468
						for ($i=$mini;$i<=$distance;$i+=10) {
469
						    $this->stats[$current_date][$source]['hist'][$i] = 0;
470
						}
471
						$this->stats[$current_date][$source]['hist'][$distance] = 1;
472
					} else {
473
						$this->stats[$current_date][$source]['hist'][$distance] += 1;
474
					}
475
				}
476
				*/
477
478
				$this->all_tracked[$id]['lastupdate'] = time();
479
				if ($this->all_tracked[$id]['putinarchive']) $send = true;
480
			} elseif (isset($this->all_tracked[$id]['latitude']) && isset($globalDistanceIgnore['latitude']) && $globalDebug) echo "!! Too far -> Distance : ".$Common->distance($this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude'])."\n";
481
			//$this->del();
482
			
483
			
484
			if ($this->last_delete_hourly == 0 || time() - $this->last_delete_hourly > 900) {
485
			    if (!isset($globalNoDB) || $globalNoDB !== TRUE) {
486
				if ($globalDebug) echo "---- Deleting Live Marine data Not updated since 2 hour...";
487
				$MarineLive = new MarineLive($this->db);
488
				$MarineLive->deleteLiveMarineDataNotUpdated();
489
				$MarineLive->db = null;
490
				//MarineLive->deleteLiveMarineData();
491
				if ($globalDebug) echo " Done\n";
492
			    }
493
			    $this->last_delete_hourly = time();
494
			}
495
			
496
		    }
497
		    //$ignoreImport = false;
498
		}
499
		//if (function_exists('pcntl_fork') && $globalFork) pcntl_signal(SIGCHLD, SIG_IGN);
500
		if ($send) return $this->all_tracked[$id];
501
	    }
502
	}
503
    }
504
}
505
?>
506