TrackerImport   F
last analyzed

Complexity

Total Complexity 219

Size/Duplication

Total Lines 448
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 448
rs 2
c 0
b 0
f 0
wmc 219
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A checkAll() 0 11 4
B del() 0 28 11
F add() 0 372 203

How to fix   Complexity   

Complex Class

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

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

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

1
<?php
2
/**
3
 * This class is part of FlightAirmap. It's used to import trackers data
4
 *
5
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
6
 * Licensed under AGPL license.
7
 * For more information see: https://www.flightairmap.com/
8
*/
9
require_once(dirname(__FILE__).'/class.Connection.php');
10
require_once(dirname(__FILE__).'/class.Tracker.php');
11
require_once(dirname(__FILE__).'/class.TrackerLive.php');
12
require_once(dirname(__FILE__).'/class.TrackerArchive.php');
13
require_once(dirname(__FILE__).'/class.Stats.php');
14
require_once(dirname(__FILE__).'/class.Source.php');
15
16
class TrackerImport {
17
    private $all_tracked = array();
18
    private $last_delete_hourly = 0;
19
    private $last_delete = 0;
20
    private $stats = array();
21
    private $tmd = 0;
22
    private $source_location = array();
23
    public $db = null;
24
    public $nb = 0;
25
26
    public function __construct($dbc = null) {
27
	global $globalBeta;
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
    }
49
50
    public function checkAll() {
51
	global $globalDebug;
52
	if ($globalDebug) echo "Update last seen tracked data...\n";
53
	foreach ($this->all_tracked as $key => $flight) {
54
	    if (isset($this->all_tracked[$key]['id'])) {
55
		//echo $this->all_tracked[$key]['id'].' - '.$this->all_tracked[$key]['latitude'].'  '.$this->all_tracked[$key]['longitude']."\n";
56
    		$Tracker = new Tracker($this->db);
57
        	$Tracker->updateLatestTrackerData($this->all_tracked[$key]['id'],$this->all_tracked[$key]['ident'],$this->all_tracked[$key]['latitude'],$this->all_tracked[$key]['longitude'],$this->all_tracked[$key]['altitude'],$this->all_tracked[$key]['speed'],$this->all_tracked[$key]['datetime']);
58
            }
59
	}
60
    }
61
62
    public function del() {
63
	global $globalDebug;
64
	// Delete old infos
65
	if ($globalDebug) echo 'Delete old values and update latest data...'."\n";
66
	foreach ($this->all_tracked as $key => $flight) {
67
    	    if (isset($flight['lastupdate'])) {
68
        	if ($flight['lastupdate'] < (time()-3000)) {
69
            	    if (isset($this->all_tracked[$key]['id'])) {
70
            		if ($globalDebug) echo "--- Delete old values with id ".$this->all_tracked[$key]['id']."\n";
71
			/*
72
			$TrackerLive = new TrackerLive();
73
            		$TrackerLive->deleteLiveTrackerDataById($this->all_tracked[$key]['id']);
74
			$TrackerLive->db = null;
75
			*/
76
            		//$real_arrival = $this->arrival($key);
77
            		$Tracker = new Tracker($this->db);
78
            		if ($this->all_tracked[$key]['latitude'] != '' && $this->all_tracked[$key]['longitude'] != '') {
79
				$result = $Tracker->updateLatestTrackerData($this->all_tracked[$key]['id'],$this->all_tracked[$key]['ident'],$this->all_tracked[$key]['latitude'],$this->all_tracked[$key]['longitude'],$this->all_tracked[$key]['altitude'],$this->all_tracked[$key]['speed']);
80
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
81
			}
82
			// Put in archive
83
//			$Tracker->db = null;
84
            	    }
85
            	    unset($this->all_tracked[$key]);
86
    	        }
87
	    }
88
        }
89
    }
90
91
    public function add($line) {
92
	global $globalFork, $globalDistanceIgnore, $globalDaemon, $globalDebug, $globalCoordMinChangeTracker, $globalDebugTimeElapsed, $globalCenterLatitude, $globalCenterLongitude, $globalBeta, $globalSourcesupdate, $globalAllTracked;
93
	if (!isset($globalCoordMinChangeTracker) || $globalCoordMinChangeTracker == '') $globalCoordMinChangeTracker = '0.015';
94
	date_default_timezone_set('UTC');
95
	$dataFound = false;
96
	$send = false;
97
	
98
	// SBS format is CSV format
99
	if(is_array($line) && isset($line['ident'])) {
100
	    //print_r($line);
101
  	    if (isset($line['ident'])) {
102
103
		
104
		// Increment message number
105
		if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE) {
106
		    $current_date = date('Y-m-d');
107
		    if (isset($line['source_name'])) $source = $line['source_name'];
108
		    else $source = '';
109
		    if ($source == '' || $line['format_source'] == 'aprs') $source = $line['format_source'];
110
		    if (!isset($this->stats[$current_date][$source]['msg'])) {
111
		    	$this->stats[$current_date][$source]['msg']['date'] = time();
112
		    	$this->stats[$current_date][$source]['msg']['nb'] = 1;
113
		    } else $this->stats[$current_date][$source]['msg']['nb'] += 1;
114
		}
115
		
116
		
117
		$Common = new Common();
118
	        if (!isset($line['id'])) $id = trim($line['ident']);
119
	        else $id = trim($line['id']);
120
		
121
		if (!isset($this->all_tracked[$id])) {
122
		    $this->all_tracked[$id] = array();
123
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('addedTracker' => 0));
124
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('ident' => '','latitude' => '', 'longitude' => '', 'speed' => '', 'altitude' => '', 'heading' => '', 'format_source' => '','source_name' => '','comment'=> '','type' => '','noarchive' => false,'putinarchive' => true,'over_country' => ''));
125
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('lastupdate' => time()));
126
		    if (!isset($line['id'])) {
127
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
128
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $id.'-'.date('YmdHi')));
129
		     } else $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $line['id']));
130
		    if ($globalAllTracked !== FALSE) $dataFound = true;
131
		}
132
		
133
		if (isset($line['datetime']) && strtotime($line['datetime']) > time()-20*60 && strtotime($line['datetime']) < time()+20*60) {
134
		    if (!isset($this->all_tracked[$id]['datetime']) || strtotime($line['datetime']) >= strtotime($this->all_tracked[$id]['datetime'])) {
135
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => $line['datetime']));
136
		    } else {
137
				if (strtotime($line['datetime']) == strtotime($this->all_tracked[$id]['datetime']) && $globalDebug) echo "!!! Date is the same as previous data for ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."\n";
138
				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]['ident']." - format : ".$line['format_source']."\n";
139
				return '';
140
		    }
141
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) < time()-20*60) {
142
			if ($globalDebug) echo "!!! Date is too old ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."!!!\n";
143
			return '';
144
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) > time()+20*60) {
145
			if ($globalDebug) echo "!!! Date is in the future ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."!!!\n";
146
			return '';
147
		} elseif (!isset($line['datetime'])) {
148
			date_default_timezone_set('UTC');
149
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => date('Y-m-d H:i:s')));
150
		} else {
151
			if ($globalDebug) echo "!!! Unknow date error ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."!!!\n";
152
			return '';
153
		}
154
		
155
		//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'])) {
156
		if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_tracked[$id]['ident'] != trim($line['ident']))) {
157
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('ident' => trim($line['ident'])));
158
		    if ($this->all_tracked[$id]['addedTracker'] == 1) {
159
			$timeelapsed = microtime(true);
160
            		$Tracker = new Tracker($this->db);
161
            		$fromsource = NULL;
162
            		$result = $Tracker->updateIdentTrackerData($this->all_tracked[$id]['id'],$this->all_tracked[$id]['ident'],$fromsource);
163
			if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
164
			$Tracker->db = null;
165
			if ($globalDebugTimeElapsed) echo 'Time elapsed for update identspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
166
		    }
167
		    if (!isset($this->all_tracked[$id]['id'])) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $this->all_tracked[$id]['ident']));
168
		}
169
170
		if (isset($line['speed']) && $line['speed'] != '') {
171
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($line['speed'])));
172
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed_fromsrc' => true));
173
		} 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'])) {
174
		    $distance = $Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m');
175
		    if ($distance > 100 && $distance < 10000) {
176
			$speed = $distance/(time() - $this->all_tracked[$id]['time_last_coord']);
177
			$speed = $speed*3.6;
178
			if ($speed < 1000) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($speed)));
179
  			if ($globalDebug) echo "ø Calculated Speed for ".$this->all_tracked[$id]['ident']." : ".$speed." - distance : ".$distance."\n";
180
		    }
181
		}
182
183
	        if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '' && is_numeric($line['latitude']) && is_numeric($line['longitude'])) {
184
	    	    if (isset($this->all_tracked[$id]['time_last_coord'])) $timediff = round(time()-$this->all_tracked[$id]['time_last_coord']);
185
	    	    else unset($timediff);
186
	    	    if ($this->tmd > 5 || !isset($timediff) || $timediff > 90 || ($timediff > 60 && 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')))) {
187
			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'])) {
188
			    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'],0.08)) {
189
				$this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
190
				$this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
191
				$this->all_tracked[$id]['putinarchive'] = true;
192
				
193
				if ($globalDebug) echo "\n".' ------- Check Country for '.$this->all_tracked[$id]['ident'].' with latitude : '.$line['latitude'].' and longitude : '.$line['longitude'].'.... ';
194
				$timeelapsed = microtime(true);
195
				$Tracker = new Tracker($this->db);
196
				$all_country = $Tracker->getCountryFromLatitudeLongitude($line['latitude'],$line['longitude']);
197
				if (!empty($all_country)) $this->all_tracked[$id]['over_country'] = $all_country['iso2'];
198
				$Tracker->db = null;
199
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update getCountryFromlatitudeLongitude : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
200
				$this->tmd = 0;
201
				if ($globalDebug) echo 'FOUND : '.$this->all_tracked[$id]['over_country'].' ---------------'."\n";
202
			    }
203
			}
204
205
			if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
206
				if (!isset($this->all_tracked[$id]['archive_latitude'])) $this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
207
				if (!isset($this->all_tracked[$id]['livedb_latitude']) || abs($this->all_tracked[$id]['livedb_latitude']-$line['latitude']) > $globalCoordMinChangeTracker || $this->all_tracked[$id]['format_source'] == 'aprs') {
208
				    $this->all_tracked[$id]['livedb_latitude'] = $line['latitude'];
209
				    $dataFound = true;
210
				    $this->all_tracked[$id]['time_last_coord'] = time();
211
				}
212
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('latitude' => $line['latitude']));
213
			}
214
			if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
215
			    if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
216
				if (!isset($this->all_tracked[$id]['archive_longitude'])) $this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
217
				if (!isset($this->all_tracked[$id]['livedb_longitude']) || abs($this->all_tracked[$id]['livedb_longitude']-$line['longitude']) > $globalCoordMinChangeTracker || $this->all_tracked[$id]['format_source'] == 'aprs') {
218
				    $this->all_tracked[$id]['livedb_longitude'] = $line['longitude'];
219
				    $dataFound = true;
220
				    $this->all_tracked[$id]['time_last_coord'] = time();
221
				}
222
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('longitude' => $line['longitude']));
223
			}
224
225
		    } else if ($globalDebug && $timediff > 20) {
226
			$this->tmd = $this->tmd + 1;
227
			if ($line['latitude'] != $this->all_tracked[$id]['latitude'] && $line['longitude'] != $this->all_tracked[$id]['longitude']) {
228
				echo '!!! Too much distance in short time... for '.$this->all_tracked[$id]['ident']."\n";
229
				echo 'Time : '.$timediff.'s - Distance : '.$Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')."m -";
230
				echo 'Speed : '.(($Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')/$timediff)*3.6)." km/h - ";
231
				echo 'Lat : '.$line['latitude'].' - long : '.$line['longitude'].' - prev lat : '.$this->all_tracked[$id]['latitude'].' - prev long : '.$this->all_tracked[$id]['longitude']." \n";
232
			}
233
		    }
234
		}
235
		if (isset($line['last_update']) && $line['last_update'] != '') {
236
		    if (isset($this->all_tracked[$id]['last_update']) && $this->all_tracked[$id]['last_update'] != $line['last_update']) $dataFound = true;
237
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('last_update' => $line['last_update']));
238
		}
239
		if (isset($line['format_source']) && $line['format_source'] != '') {
240
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('format_source' => $line['format_source']));
241
		}
242
		if (isset($line['source_name']) && $line['source_name'] != '') {
243
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('source_name' => $line['source_name']));
244
		}
245
		if (isset($line['comment']) && $line['comment'] != '') {
246
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('comment' => $line['comment']));
247
		    //$dataFound = true;
248
		}
249
		if (isset($line['type']) && $line['type'] != '') {
250
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('type' => $line['type']));
251
		    //$dataFound = true;
252
		}
253
254
		if (isset($line['altitude']) && $line['altitude'] != '') {
255
		    //if (!isset($this->all_tracked[$id]['altitude']) || $this->all_tracked[$id]['altitude'] == '' || ($this->all_tracked[$id]['altitude'] > 0 && $line['altitude'] != 0)) {
256
			if (is_int($this->all_tracked[$id]['altitude']) && abs(round($line['altitude']/100)-$this->all_tracked[$id]['altitude']) > 3) $this->all_tracked[$id]['putinarchive'] = true;
257
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('altitude' => $line['altitude']));
258
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('altitude_real' => $line['altitude']));
259
			//$dataFound = true;
260
		    //} elseif ($globalDebug) echo "!!! Strange altitude data... not added.\n";
261
  		}
262
263
		if (isset($line['noarchive']) && $line['noarchive'] === true) {
264
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('noarchive' => true));
265
		}
266
		
267
		if (isset($line['heading']) && $line['heading'] != '') {
268
		    if (is_int($this->all_tracked[$id]['heading']) && abs($this->all_tracked[$id]['heading']-round($line['heading'])) > 10) $this->all_tracked[$id]['putinarchive'] = true;
269
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($line['heading'])));
270
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading_fromsrc' => true));
271
		    //$dataFound = true;
272
  		} 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']) {
273
  		    $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']);
274
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($heading)));
275
		    if (abs($this->all_tracked[$id]['heading']-round($heading)) > 10) $this->all_tracked[$id]['putinarchive'] = true;
276
  		    if ($globalDebug) echo "ø Calculated Heading for ".$this->all_tracked[$id]['ident']." : ".$heading."\n";
277
  		}
278
		//if (isset($globalSourcesupdate) && $globalSourcesupdate != '' && isset($this->all_tracked[$id]['lastupdate']) && time()-$this->all_tracked[$id]['lastupdate'] < $globalSourcesupdate) $dataFound = false;
279
280
		if ($dataFound === true && isset($this->all_tracked[$id]['ident'])) {
281
		    $this->all_tracked[$id]['lastupdate'] = time();
282
		    if ($this->all_tracked[$id]['addedTracker'] == 0) {
283
		        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'])) {
284
			    if (!isset($this->all_tracked[$id]['forcenew']) || $this->all_tracked[$id]['forcenew'] == 0) {
285
				if ($globalDebug) echo "Check if aircraft is already in DB...";
286
				$timeelapsed = microtime(true);
287
				$TrackerLive = new TrackerLive($this->db);
288
				if (isset($line['id'])) {
289
				    $recent_ident = $TrackerLive->checkIdRecent($line['id']);
290
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
291
				} elseif (isset($this->all_tracked[$id]['ident']) && $this->all_tracked[$id]['ident'] != '') {
292
				    $recent_ident = $TrackerLive->checkIdentRecent($this->all_tracked[$id]['ident']);
293
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
294
				} else $recent_ident = '';
295
				$TrackerLive->db=null;
296
297
				if ($globalDebug && $recent_ident == '') echo " Not in DB.\n";
298
				elseif ($globalDebug && $recent_ident != '') echo " Already in DB.\n";
299
			    } else {
300
				$recent_ident = '';
301
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('forcenew' => 0));
302
			    }
303
			    //if there was no aircraft with the same callsign within the last hour and go post it into the archive
304
			    if($recent_ident == "")
305
			    {
306
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['ident']." in archive DB : ";
307
				//adds the spotter data for the archive
308
				    $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...
309
				    if (!isset($this->all_tracked[$id]['id'])) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $this->all_tracked[$id]['ident'].'-'.date('YmdHi')));
310
				    $timeelapsed = microtime(true);
311
				    $Tracker = new Tracker($this->db);
312
				    $result = $Tracker->addTrackerData($this->all_tracked[$id]['id'], $this->all_tracked[$id]['ident'], $this->all_tracked[$id]['latitude'], $this->all_tracked[$id]['longitude'], $this->all_tracked[$id]['altitude'], $this->all_tracked[$id]['heading'], $this->all_tracked[$id]['speed'], $this->all_tracked[$id]['datetime'], $this->all_tracked[$id]['comment'],$this->all_tracked[$id]['type'],$this->all_tracked[$id]['format_source'],$this->all_tracked[$id]['source_name']);
313
				    $Tracker->db = null;
314
				    if ($globalDebug && isset($result)) echo $result."\n";
315
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update addspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
316
				    
317
				    
318
				    // Add source stat in DB
319
				    $Stats = new Stats($this->db);
320
				    if (!empty($this->stats)) {
321
					if ($globalDebug) echo 'Add source stats : ';
322
				        foreach($this->stats as $date => $data) {
323
					    foreach($data as $source => $sourced) {
324
					        //print_r($sourced);
325
				    	        if (isset($sourced['polar'])) echo $Stats->addStatSource(json_encode($sourced['polar']),$source,'polar_tracker',$date);
326
				    	        if (isset($sourced['hist'])) echo $Stats->addStatSource(json_encode($sourced['hist']),$source,'hist_tracker',$date);
327
				    		if (isset($sourced['msg'])) {
328
				    		    if (time() - $sourced['msg']['date'] > 10) {
329
				    		        $nbmsg = round($sourced['msg']['nb']/(time() - $sourced['msg']['date']));
330
				    		        echo $Stats->addStatSource($nbmsg,$source,'msg_tracker',$date);
331
			    			        unset($this->stats[$date][$source]['msg']);
332
			    			    }
333
			    			}
334
			    		    }
335
			    		    if ($date != date('Y-m-d')) {
336
			    			unset($this->stats[$date]);
337
			    		    }
338
				    	}
339
				    	if ($globalDebug) echo 'Done'."\n";
340
341
				    }
342
				    $Stats->db = null;
343
				    
344
				    $this->del();
345
				//$ignoreImport = false;
346
				$this->all_tracked[$id]['addedTracker'] = 1;
347
				//print_r($this->all_tracked[$id]);
348
				if ($this->last_delete == 0 || time() - $this->last_delete > 1800) {
349
				    if ($globalDebug) echo "---- Deleting Live Tracker data older than 9 hours...";
350
				    //TrackerLive->deleteLiveTrackerDataNotUpdated();
351
				    $TrackerLive = new TrackerLive($this->db);
352
				    $TrackerLive->deleteLiveTrackerData();
353
				    $TrackerLive->db=null;
354
				    if ($globalDebug) echo " Done\n";
355
				    $this->last_delete = time();
356
				}
357
			    } else {
358
				$this->all_tracked[$id]['id'] = $recent_ident;
359
				$this->all_tracked[$id]['addedTracker'] = 1;
360
				if (isset($globalDaemon) && !$globalDaemon) {
361
					$Tracker = new Tracker($this->db);
362
					$Tracker->updateLatestTrackerData($this->all_tracked[$id]['id'],$this->all_tracked[$id]['ident'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$this->all_tracked[$id]['altitude'],$this->all_tracked[$id]['speed'],$this->all_tracked[$id]['datetime']);
363
					$Tracker->db = null;
364
				}
365
				
366
			    }
367
			}
368
		    }
369
		    //adds the spotter LIVE data
370
		    if ($globalDebug) {
371
			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'].' - Altitude : '.$this->all_tracked[$id]['altitude'].' - Heading : '.$this->all_tracked[$id]['heading'].' - Speed : '.$this->all_tracked[$id]['speed']."\n";
372
		    }
373
		    $ignoreImport = false;
374
375
		    if (!$ignoreImport) {
376
			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'])) {
377
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['ident']." from ".$this->all_tracked[$id]['format_source']." in Live DB : ";
378
				$timeelapsed = microtime(true);
379
				$TrackerLive = new TrackerLive($this->db);
380
				$result = $TrackerLive->addLiveTrackerData($this->all_tracked[$id]['id'], $this->all_tracked[$id]['ident'], $this->all_tracked[$id]['latitude'], $this->all_tracked[$id]['longitude'], $this->all_tracked[$id]['altitude'], $this->all_tracked[$id]['heading'], $this->all_tracked[$id]['speed'],$this->all_tracked[$id]['datetime'], $this->all_tracked[$id]['putinarchive'],$this->all_tracked[$id]['comment'],$this->all_tracked[$id]['type'],$this->all_tracked[$id]['noarchive'],$this->all_tracked[$id]['format_source'],$this->all_tracked[$id]['source_name'],$this->all_tracked[$id]['over_country']);
381
				$TrackerLive->db = null;
382
				$this->all_tracked[$id]['putinarchive'] = false;
383
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update addlivespotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
384
385
				// Put statistics in $this->stats variable
386
				
387
				if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE && $this->all_tracked[$id]['latitude'] != '' && $this->all_tracked[$id]['longitude'] != '') {
388
					$source = $this->all_tracked[$id]['source_name'];
389
					if ($source == '') $source = $this->all_tracked[$id]['format_source'];
390
					if (!isset($this->source_location[$source])) {
391
						$Location = new Source($this->db);
392
						$coord = $Location->getLocationInfobySourceName($source);
393
						if (count($coord) > 0) {
394
							$latitude = $coord[0]['latitude'];
395
							$longitude = $coord[0]['longitude'];
396
						} else {
397
							$latitude = $globalCenterLatitude;
398
							$longitude = $globalCenterLongitude;
399
						}
400
						$this->source_location[$source] = array('latitude' => $latitude,'longitude' => $longitude);
401
					} else {
402
						$latitude = $this->source_location[$source]['latitude'];
403
						$longitude = $this->source_location[$source]['longitude'];
404
					}
405
					$stats_heading = $Common->getHeading($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
406
					//$stats_heading = $stats_heading%22.5;
407
					$stats_heading = round($stats_heading/22.5);
408
					$stats_distance = $Common->distance($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
409
					$current_date = date('Y-m-d');
410
					if ($stats_heading == 16) $stats_heading = 0;
411
					if (!isset($this->stats[$current_date][$source]['polar'][1])) {
412
						for ($i=0;$i<=15;$i++) {
413
						    $this->stats[$current_date][$source]['polar'][$i] = 0;
414
						}
415
						$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
416
					} else {
417
						if ($this->stats[$current_date][$source]['polar'][$stats_heading] < $stats_distance) {
418
							$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
419
						}
420
					}
421
					$distance = (round($stats_distance/10)*10);
422
					//echo '$$$$$$$$$$ DISTANCE : '.$distance.' - '.$source."\n";
423
					//var_dump($this->stats);
424
					if (!isset($this->stats[$current_date][$source]['hist'][$distance])) {
425
						if (isset($this->stats[$current_date][$source]['hist'][0])) {
426
						    end($this->stats[$current_date][$source]['hist']);
427
						    $mini = key($this->stats[$current_date][$source]['hist'])+10;
428
						} else $mini = 0;
429
						for ($i=$mini;$i<=$distance;$i+=10) {
430
						    $this->stats[$current_date][$source]['hist'][$i] = 0;
431
						}
432
						$this->stats[$current_date][$source]['hist'][$distance] = 1;
433
					} else {
434
						$this->stats[$current_date][$source]['hist'][$distance] += 1;
435
					}
436
				}
437
438
				$this->all_tracked[$id]['lastupdate'] = time();
439
				if ($this->all_tracked[$id]['putinarchive']) $send = true;
440
				if ($globalDebug) echo $result."\n";
441
			} 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";
442
			//$this->del();
443
			
444
			
445
			if ($this->last_delete_hourly == 0 || time() - $this->last_delete_hourly > 900) {
446
			    if ($globalDebug) echo "---- Deleting Live Tracker data Not updated since 2 hour...";
447
			    $TrackerLive = new TrackerLive($this->db);
448
			    $TrackerLive->deleteLiveTrackerDataNotUpdated();
449
			    $TrackerLive->db = null;
450
			    //TrackerLive->deleteLiveTrackerData();
451
			    if ($globalDebug) echo " Done\n";
452
			    $this->last_delete_hourly = time();
453
			}
454
			
455
		    }
456
		    //$ignoreImport = false;
457
		}
458
		//if (function_exists('pcntl_fork') && $globalFork) pcntl_signal(SIGCHLD, SIG_IGN);
459
		if ($send) return $this->all_tracked[$id];
460
	    }
461
	}
462
    }
463
}
464
?>
465