Completed
Push — master ( 0a71c8...58eab4 )
by Yannick
38:44
created

TrackerLive   D

Complexity

Total Complexity 163

Size/Duplication

Total Lines 950
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 950
rs 4.4444
c 0
b 0
f 0
wmc 163
lcom 1
cbo 4

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
F getFilter() 0 68 36
C getLiveTrackerData() 0 39 8
B getMinLiveTrackerData() 0 31 4
F getMinLastLiveTrackerData() 0 69 22
B getLiveTrackerCount() 0 22 4
A getLiveTrackerDatabyCoord() 0 21 4
B getMinLiveTrackerDatabyCoord() 0 24 4
C getLatestTrackerForLayar() 0 54 11
A getLastLiveTrackerDataByIdent() 0 12 1
A getDateLiveTrackerDataByIdent() 0 13 1
A getLastLiveTrackerDataById() 0 12 1
A getDateLiveTrackerDataById() 0 12 1
A getAltitudeLiveTrackerDataByIdent() 0 20 2
B getAllLiveTrackerDataById() 0 26 5
A getAllLiveTrackerDataByIdent() 0 16 2
A deleteLiveTrackerData() 0 21 3
C deleteLiveTrackerDataNotUpdated() 0 91 11
A deleteLiveTrackerDataByIdent() 0 15 2
A deleteLiveTrackerDataById() 0 15 2
B getIdentFromLastHour() 0 26 3
B checkIdentRecent() 0 26 3
B checkIdRecent() 0 26 3
F addLiveTrackerData() 0 105 27
A getOrderBy() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like TrackerLive 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 TrackerLive, and based on these observations, apply Extract Interface, too.

1
<?php
2
//$global_query = "SELECT tracker_live.* FROM tracker_live";
3
4
class TrackerLive {
5
	public $db;
6
	static $global_query = "SELECT tracker_live.* FROM tracker_live";
7
8
	public function __construct($dbc = null) {
9
		$Connection = new Connection($dbc);
10
		$this->db = $Connection->db();
11
		if ($this->db === null) die('Error: No DB connection.');
12
	}
13
14
15
	/**
16
	* Get SQL query part for filter used
17
	* @param Array $filter the filter
18
	* @return Array the SQL part
19
	*/
20
	public function getFilter($filter = array(),$where = false,$and = false) {
21
		global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver;
22
		$filters = array();
23
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
24
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
25
				$filters = $globalStatsFilters[$globalFilterName];
26
			} else {
27
				$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]);
28
			}
29
		}
30
		if (isset($filter[0]['source'])) {
31
			$filters = array_merge($filters,$filter);
32
		}
33
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
34
		$filter_query_join = '';
35
		$filter_query_where = '';
36
		foreach($filters as $flt) {
37
			if (isset($flt['idents']) && !empty($flt['idents'])) {
38
				if (isset($flt['source'])) {
39
					$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_output WHERE tracker_output.ident IN ('".implode("','",$flt['idents'])."') AND tracker_output.format_source IN ('".implode("','",$flt['source'])."')) spid ON spid.famtrackid = tracker_live.famtrackid";
40
				} else {
41
					$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_output WHERE tracker_output.ident IN ('".implode("','",$flt['idents'])."')) spid ON spid.famtrackid = tracker_live.famtrackid";
42
				}
43
			}
44
		}
45
		if (isset($filter['source']) && !empty($filter['source'])) {
46
			$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
47
		}
48
		if (isset($filter['ident']) && !empty($filter['ident'])) {
49
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
50
		}
51
		if ((isset($filter['year']) && $filter['year'] != '') || (isset($filter['month']) && $filter['month'] != '') || (isset($filter['day']) && $filter['day'] != '')) {
52
			$filter_query_date = '';
53
			
54
			if (isset($filter['year']) && $filter['year'] != '') {
55
				if ($globalDBdriver == 'mysql') {
56
					$filter_query_date .= " AND YEAR(tracker_output.date) = '".$filter['year']."'";
57
				} else {
58
					$filter_query_date .= " AND EXTRACT(YEAR FROM tracker_output.date) = '".$filter['year']."'";
59
				}
60
			}
61
			if (isset($filter['month']) && $filter['month'] != '') {
62
				if ($globalDBdriver == 'mysql') {
63
					$filter_query_date .= " AND MONTH(tracker_output.date) = '".$filter['month']."'";
64
				} else {
65
					$filter_query_date .= " AND EXTRACT(MONTH FROM tracker_output.date) = '".$filter['month']."'";
66
				}
67
			}
68
			if (isset($filter['day']) && $filter['day'] != '') {
69
				if ($globalDBdriver == 'mysql') {
70
					$filter_query_date .= " AND DAY(tracker_output.date) = '".$filter['day']."'";
71
				} else {
72
					$filter_query_date .= " AND EXTRACT(DAY FROM tracker_output.date) = '".$filter['day']."'";
73
				}
74
			}
75
			$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_output".preg_replace('/^ AND/',' WHERE',$filter_query_date).") sd ON sd.famtrackid = tracker_live.famtrackid";
76
		}
77
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
78
			$filter_query_where .= " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
79
		}
80
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
81
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
82
		if ($filter_query_where != '') {
83
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
84
		}
85
		$filter_query = $filter_query_join.$filter_query_where;
86
		return $filter_query;
87
	}
88
89
	/**
90
	* Gets all the spotter information based on the latest data entry
91
	*
92
	* @return Array the spotter information
93
	*
94
	*/
95
	public function getLiveTrackerData($limit = '', $sort = '', $filter = array())
96
	{
97
		global $globalDBdriver, $globalLiveInterval;
98
		$Tracker = new Tracker($this->db);
99
		date_default_timezone_set('UTC');
100
101
		$filter_query = $this->getFilter($filter);
102
		$limit_query = '';
103
		if ($limit != '')
104
		{
105
			$limit_array = explode(',', $limit);
106
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
107
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
108
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
109
			{
110
				$limit_query = ' LIMIT '.$limit_array[1].' OFFSET '.$limit_array[0];
111
			}
112
		}
113
		$orderby_query = '';
114
		if ($sort != '')
115
		{
116
			$search_orderby_array = $this->getOrderBy();
117
			if (isset($search_orderby_array[$sort]['sql'])) 
118
			{
119
				$orderby_query = ' '.$search_orderby_array[$sort]['sql'];
120
			}
121
		}
122
123
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
124
		if ($globalDBdriver == 'mysql') {
125
			//$query  = "SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 SECOND) <= l.date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate";
126
			$query  = 'SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate'.$filter_query.$orderby_query;
127
		} else {
128
			$query  = "SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate".$filter_query.$orderby_query;
129
		}
130
		$spotter_array = $Tracker->getDataFromDB($query.$limit_query,array(),'',true);
131
132
		return $spotter_array;
133
	}
134
135
	/**
136
	* Gets Minimal Live Spotter data
137
	*
138
	* @return Array the spotter information
139
	*
140
	*/
141
	public function getMinLiveTrackerData($filter = array())
142
	{
143
		global $globalDBdriver, $globalLiveInterval;
144
		date_default_timezone_set('UTC');
145
146
		$filter_query = $this->getFilter($filter,true,true);
147
148
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
149
		if ($globalDBdriver == 'mysql') {
150
			$query  = 'SELECT tracker_live.ident, tracker_live.type,tracker_live.famtrackid, tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
151
			FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate'.$filter_query." tracker_live.latitude <> 0 AND tracker_live.longitude <> 0";
152
		} else {
153
			$query  = "SELECT tracker_live.ident, tracker_live.type,tracker_live.famtrackid, tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
154
			FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate".$filter_query." tracker_live.latitude <> '0' AND tracker_live.longitude <> '0'";
155
156
157
		}
158
//		$spotter_array = Spotter->getDataFromDB($query.$limit_query);
159
//		echo $query;
160
161
		try {
162
			$sth = $this->db->prepare($query);
163
			$sth->execute();
164
		} catch(PDOException $e) {
165
			echo $e->getMessage();
166
			die;
167
		}
168
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
169
170
		return $spotter_array;
171
	}
172
173
	/**
174
	* Gets Minimal Live Spotter data since xx seconds
175
	*
176
	* @return Array the spotter information
177
	*
178
	*/
179
	public function getMinLastLiveTrackerData($coord,$filter = array(),$limit = false)
180
	{
181
		global $globalDBdriver, $globalLiveInterval, $globalArchive, $globalMap3DTrackersLimit;
182
		date_default_timezone_set('UTC');
183
		if (is_array($coord) && !empty($coord)) {
184
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
185
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
186
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
187
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
188
		}
189
		$filter_query = $this->getFilter($filter,true,true);
190
191
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
192
		if (!isset($globalMap3DTrackersLimit) || $globalMap3DTrackersLimit == '') $globalMap3DTrackersLimit = '300';
193
		if ($globalDBdriver == 'mysql') {
194
			if (isset($globalArchive) && $globalArchive) {
195
				$query  = "SELECT * FROM (
196
					SELECT tracker_archive.ident, tracker_archive.famtrackid,tracker_archive.type,tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.date, tracker_archive.format_source 
197
					FROM tracker_archive INNER JOIN (SELECT famtrackid FROM tracker_live".$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= tracker_live.date) l ON l.famtrackid = tracker_archive.famtrackid ";
198
				if (isset($maxlat)) $query .= "AND tracker_archive.latitude BETWEEN ".$minlat." AND ".$maxlat." AND tracker_archive.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
0 ignored issues
show
Bug introduced by
The variable $minlat 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...
Bug introduced by
The variable $minlong 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...
Bug introduced by
The variable $maxlong 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...
199
				$query .= "UNION
200
					SELECT tracker_live.ident, tracker_live.famtrackid, tracker_live.type,tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
201
					FROM tracker_live".$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= tracker_live.date ";
202
				if (isset($maxlat)) $query .= "AND tracker_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND tracker_live.longitude BETWEEN ".$minlong." AND ".$maxlong;
203
				$query .= ") AS tracker
204
				    WHERE latitude <> '0' AND longitude <> '0' 
205
				    ORDER BY famtrackid, date";
206
				if ($limit) $query .= " LIMIT ".$globalMap3DTrackersLimit;
207
			} else {
208
				$query  = 'SELECT tracker_live.ident, tracker_live.famtrackid,tracker_live.type, tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
209
				    FROM tracker_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= tracker_live.date ";
210
				if (isset($maxlat)) $query .= "AND tracker_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND tracker_live.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
211
				$query .= "AND tracker_live.latitude <> '0' AND tracker_live.longitude <> '0' 
212
				    ORDER BY tracker_live.famtrackid, tracker_live.date";
213
				if ($limit) $query .= " LIMIT ".$globalMap3DTrackersLimit;
214
			}
215
		} else {
216
			if (isset($globalArchive) && $globalArchive) {
217
				$query  = "SELECT * FROM (
218
					SELECT tracker_archive.ident, tracker_archive.famtrackid,tracker_archive.type,tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.date, tracker_archive.format_source 
219
					FROM tracker_archive INNER JOIN (SELECT famtrackid FROM tracker_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= tracker_live.date) l ON l.famtrackid = tracker_archive.famtrackid ";
220
				if (isset($maxlat)) $query .= "AND tracker_archive.latitude BETWEEN ".$minlat." AND ".$maxlat." AND tracker_archive.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
221
				$query .= "UNION
222
					SELECT tracker_live.ident, tracker_live.famtrackid, tracker_live.type,tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
223
					FROM tracker_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= tracker_live.date";
224
				if (isset($maxlat)) $query .= " AND tracker_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND tracker_live.longitude BETWEEN ".$minlong." AND ".$maxlong;
225
				$query .= ") AS tracker
226
				    WHERE latitude <> '0' AND longitude <> '0' 
227
				    ORDER BY famtrackid, date";
228
				if ($limit) $query .= " LIMIT ".$globalMap3DTrackersLimit;
229
			} else {
230
				$query  = "SELECT tracker_live.ident, tracker_live.famtrackid, tracker_live.type,tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
231
				    FROM tracker_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= tracker_live.date AND tracker_live.latitude <> '0' AND tracker_live.longitude <> '0' ";
232
				if (isset($maxlat)) $query .= "AND tracker_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND tracker_live.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
233
				$query .= "ORDER BY tracker_live.famtrackid, tracker_live.date";
234
				if ($limit) $query .= " LIMIT ".$globalMap3DTrackersLimit;
235
			}
236
		}
237
238
		try {
239
			$sth = $this->db->prepare($query);
240
			$sth->execute();
241
		} catch(PDOException $e) {
242
			echo $e->getMessage();
243
			die;
244
		}
245
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
246
		return $spotter_array;
247
	}
248
249
	/**
250
	* Gets number of latest data entry
251
	*
252
	* @return String number of entry
253
	*
254
	*/
255
	public function getLiveTrackerCount($filter = array())
256
	{
257
		global $globalDBdriver, $globalLiveInterval;
258
		$filter_query = $this->getFilter($filter,true,true);
259
260
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
261
		if ($globalDBdriver == 'mysql') {
262
			$query = 'SELECT COUNT(DISTINCT tracker_live.famtrackid) as nb FROM tracker_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date';
263
		} else {
264
			$query = "SELECT COUNT(DISTINCT tracker_live.famtrackid) as nb FROM tracker_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= date";
265
		}
266
		try {
267
			$sth = $this->db->prepare($query);
268
			$sth->execute();
269
		} catch(PDOException $e) {
270
			echo $e->getMessage();
271
			die;
272
		}
273
		$result = $sth->fetch(PDO::FETCH_ASSOC);
274
		$sth->closeCursor();
275
		return $result['nb'];
276
	}
277
278
	/**
279
	* Gets all the spotter information based on the latest data entry and coord
280
	*
281
	* @return Array the spotter information
282
	*
283
	*/
284
	public function getLiveTrackerDatabyCoord($coord, $filter = array())
285
	{
286
		global $globalDBdriver, $globalLiveInterval;
287
		$Spotter = new Spotter($this->db);
288
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
289
		$filter_query = $this->getFilter($filter);
290
291
		if (is_array($coord)) {
292
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
293
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
294
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
295
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
296
		} else return array();
297
		if ($globalDBdriver == 'mysql') {
298
			$query  = 'SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate AND tracker_live.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND tracker_live.longitude BETWEEN '.$minlong.' AND '.$maxlong.' GROUP BY tracker_live.famtrackid'.$filter_query;
299
		} else {
300
			$query  = "SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE NOW() at time zone 'UTC'  - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate AND tracker_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND tracker_live.longitude BETWEEN ".$minlong." AND ".$maxlong." GROUP BY tracker_live.famtrackid".$filter_query;
301
		}
302
		$spotter_array = $Spotter->getDataFromDB($query);
303
		return $spotter_array;
304
	}
305
306
	/**
307
	* Gets all the spotter information based on the latest data entry and coord
308
	*
309
	* @return Array the spotter information
310
	*
311
	*/
312
	public function getMinLiveTrackerDatabyCoord($coord, $filter = array())
313
	{
314
		global $globalDBdriver, $globalLiveInterval;
315
		$Spotter = new Spotter($this->db);
316
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
317
		$filter_query = $this->getFilter($filter);
318
319
		if (is_array($coord)) {
320
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
321
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
322
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
323
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
324
		} else return array();
325
		if ($globalDBdriver == 'mysql') {
326
			$query  = 'SELECT tracker_live.ident, tracker_live.famtrackid,tracker_live.type, tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
327
			FROM tracker_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= tracker_live.date AND tracker_live.latitude <> '0' AND tracker_live.longitude <> '0' AND tracker_live.latitude BETWEEN ".$minlat.' AND '.$maxlat.' AND tracker_live.longitude BETWEEN '.$minlong.' AND '.$maxlong."
328
			ORDER BY tracker_live.famtrackid, tracker_live.date";
329
		} else {
330
			$query  = "SELECT tracker_live.ident, tracker_live.type,tracker_live.famtrackid, tracker_live.latitude, tracker_live.longitude, tracker_live.altitude, tracker_live.heading, tracker_live.ground_speed, tracker_live.date, tracker_live.format_source 
331
			FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date l.latitude BETWEEN ".$minlat." AND ".$maxlat." AND l.longitude BETWEEN ".$minlong." AND ".$maxlong." GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate".$filter_query." tracker_live.latitude <> '0' AND tracker_live.longitude <> '0'";
332
		}
333
		$spotter_array = $Spotter->getDataFromDB($query);
334
		return $spotter_array;
335
	}
336
337
	/**
338
	* Gets all the spotter information based on a user's latitude and longitude
339
	*
340
	* @return Array the spotter information
341
	*
342
	*/
343
	public function getLatestTrackerForLayar($lat, $lng, $radius, $interval)
344
	{
345
		$Tracker = new Tracker($this->db);
346
		date_default_timezone_set('UTC');
347
		if ($lat != '') {
348
			if (!is_numeric($lat)) {
349
				return false;
350
			}
351
		}
352
		if ($lng != '')
353
		{
354
			if (!is_numeric($lng))
355
                        {
356
                                return false;
357
                        }
358
                }
359
360
                if ($radius != '')
361
                {
362
                        if (!is_numeric($radius))
363
                        {
364
                                return false;
365
                        }
366
                }
367
		$additional_query = '';
368
		if ($interval != '')
369
                {
370
                        if (!is_string($interval))
371
                        {
372
                                //$additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= tracker_live.date ';
373
			        return false;
374
                        } else {
375
                if ($interval == '1m')
376
                {
377
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= tracker_live.date ';
378
                } else if ($interval == '15m'){
379
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 15 MINUTE) <= tracker_live.date ';
380
                } 
381
            }
382
                } else {
383
         $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= tracker_live.date ';   
384
        }
385
386
                $query  = "SELECT tracker_live.*, ( 6371 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lng) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM tracker_live 
387
                   WHERE tracker_live.latitude <> '' 
388
                                   AND tracker_live.longitude <> '' 
389
                   ".$additional_query."
390
                   HAVING distance < :radius  
391
                                   ORDER BY distance";
392
393
                $spotter_array = $Tracker->getDataFromDB($query, array(':lat' => $lat, ':lng' => $lng,':radius' => $radius));
394
395
                return $spotter_array;
396
        }
397
398
    
399
        /**
400
	* Gets all the spotter information based on a particular callsign
401
	*
402
	* @return Array the spotter information
403
	*
404
	*/
405
	public function getLastLiveTrackerDataByIdent($ident)
406
	{
407
		$Tracker = new Tracker($this->db);
408
		date_default_timezone_set('UTC');
409
410
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
411
                $query  = 'SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE l.ident = :ident GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate ORDER BY tracker_live.date DESC';
412
413
		$spotter_array = $Tracker->getDataFromDB($query,array(':ident' => $ident),'',true);
414
415
		return $spotter_array;
416
	}
417
418
        /**
419
	* Gets all the spotter information based on a particular callsign
420
	*
421
	* @return Array the spotter information
422
	*
423
	*/
424
	public function getDateLiveTrackerDataByIdent($ident,$date)
425
	{
426
		$Tracker = new Tracker($this->db);
427
		date_default_timezone_set('UTC');
428
429
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
430
                $query  = 'SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE l.ident = :ident AND l.date <= :date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate ORDER BY tracker_live.date DESC';
431
432
                $date = date('c',$date);
433
		$spotter_array = $Tracker->getDataFromDB($query,array(':ident' => $ident,':date' => $date));
434
435
		return $spotter_array;
436
	}
437
438
        /**
439
	* Gets last spotter information based on a particular callsign
440
	*
441
	* @return Array the spotter information
442
	*
443
	*/
444
	public function getLastLiveTrackerDataById($id)
445
	{
446
		$Tracker = new Tracker($this->db);
447
		date_default_timezone_set('UTC');
448
449
		$id = filter_var($id, FILTER_SANITIZE_STRING);
450
                $query  = 'SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE l.famtrackid = :id GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate ORDER BY tracker_live.date DESC';
451
452
		$spotter_array = $Tracker->getDataFromDB($query,array(':id' => $id),'',true);
453
454
		return $spotter_array;
455
	}
456
457
        /**
458
	* Gets last spotter information based on a particular callsign
459
	*
460
	* @return Array the spotter information
461
	*
462
	*/
463
	public function getDateLiveTrackerDataById($id,$date)
464
	{
465
		$Tracker = new Tracker($this->db);
466
		date_default_timezone_set('UTC');
467
468
		$id = filter_var($id, FILTER_SANITIZE_STRING);
469
                $query  = 'SELECT tracker_live.* FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l WHERE l.famtrackid = :id AND l.date <= :date GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate ORDER BY tracker_live.date DESC';
470
                $date = date('c',$date);
471
		$spotter_array = $Tracker->getDataFromDB($query,array(':id' => $id,':date' => $date),'',true);
472
473
		return $spotter_array;
474
	}
475
476
        /**
477
	* Gets altitude information based on a particular callsign
478
	*
479
	* @return Array the spotter information
480
	*
481
	*/
482
	public function getAltitudeLiveTrackerDataByIdent($ident)
483
	{
484
485
		date_default_timezone_set('UTC');
486
487
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
488
                $query  = 'SELECT tracker_live.altitude, tracker_live.date FROM tracker_live WHERE tracker_live.ident = :ident';
489
490
    		try {
491
			
492
			$sth = $this->db->prepare($query);
493
			$sth->execute(array(':ident' => $ident));
494
		} catch(PDOException $e) {
495
			echo $e->getMessage();
496
			die;
497
		}
498
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
499
500
		return $spotter_array;
501
	}
502
503
        /**
504
	* Gets all the spotter information based on a particular id
505
	*
506
	* @return Array the spotter information
507
	*
508
	*/
509
	public function getAllLiveTrackerDataById($id,$liveinterval = false)
510
	{
511
		global $globalDBdriver, $globalLiveInterval;
512
		date_default_timezone_set('UTC');
513
		$id = filter_var($id, FILTER_SANITIZE_STRING);
514
		//$query  = self::$global_query.' WHERE tracker_live.famtrackid = :id ORDER BY date';
515
		if ($globalDBdriver == 'mysql') {
516
			$query = 'SELECT tracker_live.* FROM tracker_live WHERE tracker_live.famtrackid = :id';
517
			if ($liveinterval) $query .= ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date';
518
			$query .= ' ORDER BY date';
519
		} else {
520
			$query = 'SELECT tracker_live.* FROM tracker_live WHERE tracker_live.famtrackid = :id';
521
			if ($liveinterval) $query .= " AND CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= date";
522
			$query .= ' ORDER BY date';
523
		}
524
525
		try {
526
			$sth = $this->db->prepare($query);
527
			$sth->execute(array(':id' => $id));
528
		} catch(PDOException $e) {
529
			echo $e->getMessage();
530
			die;
531
		}
532
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
533
		return $spotter_array;
534
	}
535
536
        /**
537
	* Gets all the spotter information based on a particular ident
538
	*
539
	* @return Array the spotter information
540
	*
541
	*/
542
	public function getAllLiveTrackerDataByIdent($ident)
543
	{
544
		date_default_timezone_set('UTC');
545
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
546
		$query  = self::$global_query.' WHERE tracker_live.ident = :ident';
547
    		try {
548
			
549
			$sth = $this->db->prepare($query);
550
			$sth->execute(array(':ident' => $ident));
551
		} catch(PDOException $e) {
552
			echo $e->getMessage();
553
			die;
554
		}
555
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
556
		return $spotter_array;
557
	}
558
559
560
	/**
561
	* Deletes all info in the table
562
	*
563
	* @return String success or false
564
	*
565
	*/
566
	public function deleteLiveTrackerData()
567
	{
568
		global $globalDBdriver;
569
		if ($globalDBdriver == 'mysql') {
570
			//$query  = "DELETE FROM tracker_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE) >= tracker_live.date";
571
			$query  = 'DELETE FROM tracker_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 9 HOUR) >= tracker_live.date';
572
            		//$query  = "DELETE FROM tracker_live WHERE tracker_live.id IN (SELECT tracker_live.id FROM tracker_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_live l GROUP BY l.famtrackid) s on tracker_live.famtrackid = s.famtrackid AND tracker_live.date = s.maxdate AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) >= tracker_live.date)";
573
		} else {
574
			$query  = "DELETE FROM tracker_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= tracker_live.date";
575
		}
576
        
577
    		try {
578
			
579
			$sth = $this->db->prepare($query);
580
			$sth->execute();
581
		} catch(PDOException $e) {
582
			return "error";
583
		}
584
585
		return "success";
586
	}
587
588
	/**
589
	* Deletes all info in the table for aircraft not seen since 2 HOUR
590
	*
591
	* @return String success or false
592
	*
593
	*/
594
	public function deleteLiveTrackerDataNotUpdated()
595
	{
596
		global $globalDBdriver, $globalDebug;
597
		if ($globalDBdriver == 'mysql') {
598
			//$query = 'SELECT famtrackid FROM tracker_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) >= tracker_live.date AND tracker_live.famtrackid NOT IN (SELECT famtrackid FROM tracker_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) < tracker_live.date) LIMIT 800 OFFSET 0';
599
    			$query = "SELECT tracker_live.famtrackid FROM tracker_live INNER JOIN (SELECT famtrackid,MAX(date) as max_date FROM tracker_live GROUP BY famtrackid) s ON s.famtrackid = tracker_live.famtrackid AND DATE_SUB(UTC_TIMESTAMP(), INTERVAL 2 HOUR) >= s.max_date LIMIT 1200 OFFSET 0";
600
    			try {
601
				
602
				$sth = $this->db->prepare($query);
603
				$sth->execute();
604
			} catch(PDOException $e) {
605
				return "error";
606
			}
607
			$query_delete = 'DELETE FROM tracker_live WHERE famtrackid IN (';
608
                        $i = 0;
609
                        $j =0;
610
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
611
			foreach($all as $row)
612
			{
613
				$i++;
614
				$j++;
615
				if ($j == 30) {
616
					if ($globalDebug) echo ".";
617
				    	try {
618
						
619
						$sth = $this->db->prepare(substr($query_delete,0,-1).")");
620
						$sth->execute();
621
					} catch(PDOException $e) {
622
						return "error";
623
					}
624
                                	$query_delete = 'DELETE FROM tracker_live WHERE famtrackid IN (';
625
                                	$j = 0;
626
				}
627
				$query_delete .= "'".$row['famtrackid']."',";
628
			}
629
			if ($i > 0) {
630
    				try {
631
					
632
					$sth = $this->db->prepare(substr($query_delete,0,-1).")");
633
					$sth->execute();
634
				} catch(PDOException $e) {
635
					return "error";
636
				}
637
			}
638
			return "success";
639
		} elseif ($globalDBdriver == 'pgsql') {
640
			//$query = "SELECT famtrackid FROM tracker_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= tracker_live.date AND tracker_live.famtrackid NOT IN (SELECT famtrackid FROM tracker_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' < tracker_live.date) LIMIT 800 OFFSET 0";
641
    			//$query = "SELECT tracker_live.famtrackid FROM tracker_live INNER JOIN (SELECT famtrackid,MAX(date) as max_date FROM tracker_live GROUP BY famtrackid) s ON s.famtrackid = tracker_live.famtrackid AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 800 OFFSET 0";
642
    			$query = "DELETE FROM tracker_live WHERE famtrackid IN (SELECT tracker_live.famtrackid FROM tracker_live INNER JOIN (SELECT famtrackid,MAX(date) as max_date FROM tracker_live GROUP BY famtrackid) s ON s.famtrackid = tracker_live.famtrackid AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 800 OFFSET 0)";
643
    			try {
644
				
645
				$sth = $this->db->prepare($query);
646
				$sth->execute();
647
			} catch(PDOException $e) {
648
				return "error";
649
			}
650
/*			$query_delete = "DELETE FROM tracker_live WHERE famtrackid IN (";
651
                        $i = 0;
652
                        $j =0;
653
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
654
			foreach($all as $row)
655
			{
656
				$i++;
657
				$j++;
658
				if ($j == 100) {
659
					if ($globalDebug) echo ".";
660
				    	try {
661
						
662
						$sth = $this->db->query(substr($query_delete,0,-1).")");
663
						//$sth->execute();
664
					} catch(PDOException $e) {
665
						return "error";
666
					}
667
                                	$query_delete = "DELETE FROM tracker_live WHERE famtrackid IN (";
668
                                	$j = 0;
669
				}
670
				$query_delete .= "'".$row['famtrackid']."',";
671
			}
672
			if ($i > 0) {
673
    				try {
674
					
675
					$sth = $this->db->query(substr($query_delete,0,-1).")");
676
					//$sth->execute();
677
				} catch(PDOException $e) {
678
					return "error";
679
				}
680
			}
681
*/
682
			return "success";
683
		}
684
	}
685
686
	/**
687
	* Deletes all info in the table for an ident
688
	*
689
	* @return String success or false
690
	*
691
	*/
692
	public function deleteLiveTrackerDataByIdent($ident)
693
	{
694
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
695
		$query  = 'DELETE FROM tracker_live WHERE ident = :ident';
696
        
697
    		try {
698
			
699
			$sth = $this->db->prepare($query);
700
			$sth->execute(array(':ident' => $ident));
701
		} catch(PDOException $e) {
702
			return "error";
703
		}
704
705
		return "success";
706
	}
707
708
	/**
709
	* Deletes all info in the table for an id
710
	*
711
	* @return String success or false
712
	*
713
	*/
714
	public function deleteLiveTrackerDataById($id)
715
	{
716
		$id = filter_var($id, FILTER_SANITIZE_STRING);
717
		$query  = 'DELETE FROM tracker_live WHERE famtrackid = :id';
718
        
719
    		try {
720
			
721
			$sth = $this->db->prepare($query);
722
			$sth->execute(array(':id' => $id));
723
		} catch(PDOException $e) {
724
			return "error";
725
		}
726
727
		return "success";
728
	}
729
730
731
	/**
732
	* Gets the aircraft ident within the last hour
733
	*
734
	* @return String the ident
735
	*
736
	*/
737
	public function getIdentFromLastHour($ident)
738
	{
739
		global $globalDBdriver, $globalTimezone;
740
		if ($globalDBdriver == 'mysql') {
741
			$query  = 'SELECT tracker_live.ident FROM tracker_live 
742
				WHERE tracker_live.ident = :ident 
743
				AND tracker_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
744
				AND tracker_live.date < UTC_TIMESTAMP()';
745
			$query_data = array(':ident' => $ident);
746
		} else {
747
			$query  = "SELECT tracker_live.ident FROM tracker_live 
748
				WHERE tracker_live.ident = :ident 
749
				AND tracker_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
750
				AND tracker_live.date < now() AT TIME ZONE 'UTC'";
751
			$query_data = array(':ident' => $ident);
752
		}
753
		
754
		$sth = $this->db->prepare($query);
755
		$sth->execute($query_data);
756
		$ident_result='';
757
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
758
		{
759
			$ident_result = $row['ident'];
760
		}
761
		return $ident_result;
762
        }
763
764
	/**
765
	* Check recent aircraft
766
	*
767
	* @return String the ident
768
	*
769
	*/
770
	public function checkIdentRecent($ident)
771
	{
772
		global $globalDBdriver, $globalTimezone;
773
		if ($globalDBdriver == 'mysql') {
774
			$query  = 'SELECT tracker_live.ident, tracker_live.famtrackid FROM tracker_live 
775
				WHERE tracker_live.ident = :ident 
776
				AND tracker_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE)'; 
777
//				AND tracker_live.date < UTC_TIMESTAMP()";
778
			$query_data = array(':ident' => $ident);
779
		} else {
780
			$query  = "SELECT tracker_live.ident, tracker_live.famtrackid FROM tracker_live 
781
				WHERE tracker_live.ident = :ident 
782
				AND tracker_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '30 MINUTES'";
783
//				AND tracker_live.date < now() AT TIME ZONE 'UTC'";
784
			$query_data = array(':ident' => $ident);
785
		}
786
		
787
		$sth = $this->db->prepare($query);
788
		$sth->execute($query_data);
789
		$ident_result='';
790
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
791
		{
792
			$ident_result = $row['famtrackid'];
793
		}
794
		return $ident_result;
795
        }
796
797
	/**
798
	* Check recent aircraft by id
799
	*
800
	* @return String the ident
801
	*
802
	*/
803
	public function checkIdRecent($id)
804
	{
805
		global $globalDBdriver, $globalTimezone;
806
		if ($globalDBdriver == 'mysql') {
807
			$query  = 'SELECT tracker_live.ident, tracker_live.famtrackid FROM tracker_live 
808
				WHERE tracker_live.famtrackid = :id 
809
				AND tracker_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 10 HOUR)'; 
810
//				AND tracker_live.date < UTC_TIMESTAMP()";
811
			$query_data = array(':id' => $id);
812
		} else {
813
			$query  = "SELECT tracker_live.ident, tracker_live.famtrackid FROM tracker_live 
814
				WHERE tracker_live.famtrackid = :id 
815
				AND tracker_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '10 HOUR'";
816
//				AND tracker_live.date < now() AT TIME ZONE 'UTC'";
817
			$query_data = array(':id' => $id);
818
		}
819
		
820
		$sth = $this->db->prepare($query);
821
		$sth->execute($query_data);
822
		$ident_result='';
823
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
824
		{
825
			$ident_result = $row['famtrackid'];
826
		}
827
		return $ident_result;
828
        }
829
830
	/**
831
	* Adds a new spotter data
832
	*
833
	* @param String $famtrackid the ID from flightaware
834
	* @param String $ident the flight ident
835
	* @param String $aircraft_icao the aircraft type
0 ignored issues
show
Bug introduced by
There is no parameter named $aircraft_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
836
	* @param String $departure_airport_icao the departure airport
0 ignored issues
show
Bug introduced by
There is no parameter named $departure_airport_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
837
	* @param String $arrival_airport_icao the arrival airport
0 ignored issues
show
Bug introduced by
There is no parameter named $arrival_airport_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
838
	* @return String success or false
839
	*
840
	*/
841
	public function addLiveTrackerData($famtrackid = '', $ident = '', $latitude = '', $longitude = '', $altitude = '', $heading = '', $groundspeed = '', $date = '', $putinarchive = false, $comment = '', $type = '',$noarchive = false,$format_source = '', $source_name = '', $over_country = '')
842
	{
843
		global $globalURL, $globalArchive, $globalDebug;
844
		$Common = new Common();
845
		date_default_timezone_set('UTC');
846
847
		//getting the airline information
848
		if ($ident != '')
849
		{
850
			if (!is_string($ident))
851
			{
852
				return false;
853
			} 
854
		}
855
856
857
		if ($latitude != '')
858
		{
859
			if (!is_numeric($latitude))
860
			{
861
				return false;
862
			}
863
		} else return '';
864
865
		if ($longitude != '')
866
		{
867
			if (!is_numeric($longitude))
868
			{
869
				return false;
870
			}
871
		} else return '';
872
873
		if ($altitude != '')
874
		{
875
			if (!is_numeric($altitude))
876
			{
877
				return false;
878
			}
879
		} else $altitude = 0;
880
881
		if ($heading != '')
882
		{
883
			if (!is_numeric($heading))
884
			{
885
				return false;
886
			}
887
		} else $heading = 0;
888
889
		if ($groundspeed != '')
890
		{
891
			if (!is_numeric($groundspeed))
892
			{
893
				return false;
894
			}
895
		} else $groundspeed = 0;
896
		date_default_timezone_set('UTC');
897
		if ($date == '') $date = date("Y-m-d H:i:s", time());
898
899
        
900
		$famtrackid = filter_var($famtrackid,FILTER_SANITIZE_STRING);
901
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
902
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
903
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
904
		$altitude = filter_var($altitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
905
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
906
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
907
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
908
		$source_name = filter_var($source_name,FILTER_SANITIZE_STRING);
909
		$over_country = filter_var($over_country,FILTER_SANITIZE_STRING);
910
		$comment = filter_var($comment,FILTER_SANITIZE_STRING);
911
		$type = filter_var($type,FILTER_SANITIZE_STRING);
912
913
            	if ($groundspeed == '' || $Common->isInteger($groundspeed) === false ) $groundspeed = 0;
914
            	if ($heading == '' || $Common->isInteger($heading) === false ) $heading = 0;
915
            	
916
		$query = '';
917
		if ($globalArchive) {
918
			if ($globalDebug) echo '-- Delete previous data -- ';
919
			$query .= 'DELETE FROM tracker_live WHERE famtrackid = :famtrackid;';
920
		}
921
		$query  .= 'INSERT INTO tracker_live (famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, over_country, comment, type) 
922
		VALUES (:famtrackid,:ident,:latitude,:longitude,:altitude,:heading,:groundspeed,:date,:format_source, :source_name, :over_country,:comment,:type)';
923
924
		$query_values = array(':famtrackid' => $famtrackid,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':altitude' => $altitude,':heading' => $heading,':groundspeed' => $groundspeed,':date' => $date, ':format_source' => $format_source, ':source_name' => $source_name, ':over_country' => $over_country,':comment' => $comment,':type' => $type);
925
		try {
926
			
927
			$sth = $this->db->prepare($query);
928
			$sth->execute($query_values);
929
                } catch(PDOException $e) {
930
                	return "error : ".$e->getMessage();
931
                }
932
                /*
933
                echo 'putinarchive : '.$putinarchive."\n";
934
                echo 'noarchive : '.$noarchive."\n";
935
                */
936
		if (isset($globalArchive) && $globalArchive && $putinarchive && $noarchive !== true) {
937
		    if ($globalDebug) echo '(Add to Tracker archive '.$famtrackid.' : ';
938
		    $TrackerArchive = new TrackerArchive($this->db);
939
		    $result =  $TrackerArchive->addTrackerArchiveData($famtrackid, $ident,$latitude, $longitude, $altitude, $heading, $groundspeed, $date, $putinarchive, $comment, $type,$noarchive,$format_source, $source_name, $over_country);
940
		    if ($globalDebug) echo $result.')';
941
		}
942
943
		return "success";
944
945
	}
946
947
	public function getOrderBy()
948
	{
949
		$orderby = array("aircraft_asc" => array("key" => "aircraft_asc", "value" => "Aircraft Type - ASC", "sql" => "ORDER BY tracker_live.aircraft_icao ASC"), "aircraft_desc" => array("key" => "aircraft_desc", "value" => "Aircraft Type - DESC", "sql" => "ORDER BY tracker_live.aircraft_icao DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY tracker_live.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY tracker_live.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY tracker_live.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY tracker_live.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY tracker_live.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY tracker_live.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure Airport - ASC", "sql" => "ORDER BY tracker_live.departure_airport_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY tracker_live.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY tracker_live.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY tracker_live.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY tracker_live.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY tracker_live.date DESC"));
950
		return $orderby;
951
	}
952
953
}
954
955
956
?>
957