Completed
Push — master ( cb13e2...855cef )
by Yannick
34:31
created

SpotterLive::getMinLiveSpotterDatabyCoord()   D

Complexity

Conditions 14
Paths 98

Size

Total Lines 88
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 45
nc 98
nop 3
dl 0
loc 88
rs 4.9516
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
//$global_query = "SELECT spotter_live.* FROM spotter_live";
3
4
class SpotterLive {
5
	public $db;
6
	static $global_query = "SELECT spotter_live.* FROM spotter_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. (SpotterLive)');
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['airlines']) && !empty($flt['airlines'])) {
38
				if ($flt['airlines'][0] != '' && $flt['airlines'][0] != 'all') {
39
					if (isset($flt['source'])) {
40
						$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$flt['airlines'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) saf ON saf.flightaware_id = spotter_live.flightaware_id";
41
					} else {
42
						$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$flt['airlines'])."')) saf ON saf.flightaware_id = spotter_live.flightaware_id";
43
					}
44
				}
45
			}
46
			if (isset($flt['pilots_id']) && !empty($flt['pilots_id'])) {
47
				if (isset($flt['source'])) {
48
					$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.pilot_id IN ('".implode("','",$flt['pilots_id'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) spi ON spi.flightaware_id = spotter_live.flightaware_id";
49
				} else {
50
					$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.pilot_id IN ('".implode("','",$flt['pilots_id'])."')) spi ON spi.flightaware_id = spotter_live.flightaware_id";
51
				}
52
			}
53
			if (isset($flt['idents']) && !empty($flt['idents'])) {
54
				if (isset($flt['source'])) {
55
					$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.ident IN ('".implode("','",$flt['idents'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) spid ON spid.flightaware_id = spotter_live.flightaware_id";
56
				} else {
57
					$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.ident IN ('".implode("','",$flt['idents'])."')) spid ON spid.flightaware_id = spotter_live.flightaware_id";
58
				}
59
			}
60
			if (isset($flt['registrations']) && !empty($flt['registrations'])) {
61
				if (isset($flt['source'])) {
62
					$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.registration IN ('".implode("','",$flt['registrations'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) sre ON sre.flightaware_id = spotter_live.flightaware_id";
63
				} else {
64
					$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.registration IN ('".implode("','",$flt['registrations'])."')) sre ON sre.flightaware_id = spotter_live.flightaware_id";
65
				}
66
			}
67
			if ((isset($flt['airlines']) && empty($flt['airlines']) && isset($flt['pilots_id']) && empty($flt['pilots_id']) && isset($flt['idents']) && empty($flt['idents'])) || (!isset($flt['airlines']) && !isset($flt['pilots_id']) && !isset($flt['idents']) && !isset($flt['registrations']))) {
68
				if (isset($flt['source'])) {
69
					$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.format_source IN ('".implode("','",$flt['source'])."')) ssf ON ssf.flightaware_id = spotter_live.flightaware_id";
70
				}
71
			}
72
		}
73
		if (isset($filter['airlines']) && !empty($filter['airlines'])) {
74
			if ($filter['airlines'][0] != '' && $filter['airlines'][0] != 'all') {
75
				$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) sai ON sai.flightaware_id = spotter_live.flightaware_id";
76
			}
77
		}
78
		if (isset($filter['alliance']) && !empty($filter['alliance'])) {
79
			$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN (SELECT icao FROM airlines WHERE alliance = '".$filter['alliance']."')) sal ON sal.flightaware_id = spotter_live.flightaware_id ";
80
		}
81
		if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
82
			$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_live.flightaware_id ";
83
		}
84
		if (isset($filter['pilots_id']) && !empty($filter['pilots_id'])) {
85
			$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.pilot_id IN ('".implode("','",$filter['pilots_id'])."')) sp ON sp.flightaware_id = spotter_live.flightaware_id";
86
		}
87
		if (isset($filter['blocked']) && $filter['blocked'] == true) {
88
			$filter_query_join .= " INNER JOIN (SELECT callsign FROM aircraft_block) cblk ON cblk.callsign = spotter_live.ident";
89
		}
90
		if (isset($filter['source']) && !empty($filter['source'])) {
91
			if (count($filter['source']) == 1) {
92
				$filter_query_where .= " AND format_source = '".$filter['source'][0]."'";
93
			} else {
94
				$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
95
			}
96
		}
97
		if (isset($filter['ident']) && !empty($filter['ident'])) {
98
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
99
		}
100
		if (isset($filter['id']) && !empty($filter['id'])) {
101
			$filter_query_where .= " AND flightaware_id = '".$filter['id']."'";
102
		}
103
		if ((isset($filter['year']) && $filter['year'] != '') || (isset($filter['month']) && $filter['month'] != '') || (isset($filter['day']) && $filter['day'] != '')) {
104
			$filter_query_date = '';
105
			
106
			if (isset($filter['year']) && $filter['year'] != '') {
107
				if ($globalDBdriver == 'mysql') {
108
					$filter_query_date .= " AND YEAR(spotter_output.date) = '".$filter['year']."'";
109
				} else {
110
					$filter_query_date .= " AND EXTRACT(YEAR FROM spotter_output.date) = '".$filter['year']."'";
111
				}
112
			}
113
			if (isset($filter['month']) && $filter['month'] != '') {
114
				if ($globalDBdriver == 'mysql') {
115
					$filter_query_date .= " AND MONTH(spotter_output.date) = '".$filter['month']."'";
116
				} else {
117
					$filter_query_date .= " AND EXTRACT(MONTH FROM spotter_output.date) = '".$filter['month']."'";
118
				}
119
			}
120
			if (isset($filter['day']) && $filter['day'] != '') {
121
				if ($globalDBdriver == 'mysql') {
122
					$filter_query_date .= " AND DAY(spotter_output.date) = '".$filter['day']."'";
123
				} else {
124
					$filter_query_date .= " AND EXTRACT(DAY FROM spotter_output.date) = '".$filter['day']."'";
125
				}
126
			}
127
			$filter_query_join .= " INNER JOIN (SELECT flightaware_id FROM spotter_output".preg_replace('/^ AND/',' WHERE',$filter_query_date).") sd ON sd.flightaware_id = spotter_live.flightaware_id";
128
		}
129
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
130
			$filter_query_where .= " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
131
		}
132
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
133
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
134
		if ($filter_query_where != '') {
135
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
136
		}
137
		$filter_query = $filter_query_join.$filter_query_where;
138
		return $filter_query;
139
	}
140
141
	/**
142
	* Gets all the spotter information based on the latest data entry
143
	*
144
	* @return Array the spotter information
145
	*
146
	*/
147
	public function getLiveSpotterData($limit = '', $sort = '', $filter = array())
148
	{
149
		global $globalDBdriver, $globalLiveInterval;
150
		$Spotter = new Spotter($this->db);
151
		date_default_timezone_set('UTC');
152
153
		$filter_query = $this->getFilter($filter);
154
		$limit_query = '';
155
		if ($limit != '')
156
		{
157
			$limit_array = explode(',', $limit);
158
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
159
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
160
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
161
			{
162
				$limit_query = ' LIMIT '.$limit_array[1].' OFFSET '.$limit_array[0];
163
			}
164
		}
165
		$orderby_query = '';
166
		if ($sort != '')
167
		{
168
			$search_orderby_array = $this->getOrderBy();
169
			if (isset($search_orderby_array[$sort]['sql'])) 
170
			{
171
				$orderby_query = ' '.$search_orderby_array[$sort]['sql'];
172
			}
173
		}
174
		if ($orderby_query == '') $orderby_query = ' ORDER BY date DESC';
175
176
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
177
		if ($globalDBdriver == 'mysql') {
178
			//$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate";
179
			$query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate'.$filter_query.$orderby_query;
180
		} else {
181
			$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate".$filter_query.$orderby_query;
182
		}
183
		$spotter_array = $Spotter->getDataFromDB($query.$limit_query,array(),'',true);
184
185
		return $spotter_array;
186
	}
187
188
	/**
189
	* Gets Minimal Live Spotter data
190
	*
191
	* @return Array the spotter information
192
	*
193
	*/
194
	public function getMinLiveSpotterData($limit = 0,$filter = array())
195
	{
196
		global $globalDBdriver, $globalLiveInterval, $globalArchive, $globalMap2DAircraftsLimit;
197
		date_default_timezone_set('UTC');
198
		$filter_query = $this->getFilter($filter,true,true);
199
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
200
		if ($globalDBdriver == 'mysql') {
201
			if (isset($globalArchive) && $globalArchive === TRUE) {
202
			//	$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
203
			//	FROM spotter_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= spotter_live.date AND'." spotter_live.latitude <> 0 AND spotter_live.longitude <> 0 ORDER BY date DESC";
204
				$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
205
				FROM spotter_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= spotter_live.date AND'." spotter_live.latitude <> 0 AND spotter_live.longitude <> 0";
206
			} else {
207
			//	$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
208
			//	FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate'.$filter_query." spotter_live.latitude <> 0 AND spotter_live.longitude <> 0 ORDER BY date DESC";
209
				$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
210
				FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate'.$filter_query." spotter_live.latitude <> 0 AND spotter_live.longitude <> 0";
211
			}
212
		} else {
213
			if (isset($globalArchive) && $globalArchive === TRUE) {
214
			//	$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
215
			//	FROM spotter_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date AND  spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' ORDER BY date DESC";
216
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
217
				FROM spotter_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date AND  spotter_live.latitude <> '0' AND spotter_live.longitude <> '0'";
218
			} else {
219
			//	$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
220
			//	FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate".$filter_query." spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' ORDER BY date DESC";
221
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
222
				FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate".$filter_query." spotter_live.latitude <> '0' AND spotter_live.longitude <> '0'";
223
			}
224
		}
225
226
		if ($limit == 0 && isset($globalMap2DAircraftsLimit) && $globalMap2DAircraftsLimit != '') {
227
			$limit = $globalMap2DAircraftsLimit;
228
		}
229
		if ($limit != 0 && filter_var($limit,FILTER_VALIDATE_INT)) {
230
			$query .= ' LIMIT '.$limit;
231
		}
232
233
		try {
234
			$sth = $this->db->prepare($query);
235
			$sth->execute();
236
		} catch(PDOException $e) {
237
			echo $e->getMessage();
238
			die;
239
		}
240
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
241
		return $spotter_array;
242
	}
243
244
	/**
245
	* Gets Minimal Live Spotter data since xx seconds
246
	*
247
	* @return Array the spotter information
248
	*
249
	*/
250
	public function getMinLastLiveSpotterData($coord = array(),$filter = array(), $limit = false, $id = '')
251
	{
252
		global $globalDBdriver, $globalLiveInterval, $globalArchive, $globalMap3DAircraftsLimit;
253
		date_default_timezone_set('UTC');
254
		$usecoord = false;
255
		if (is_array($coord) && !empty($coord)) {
256
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
257
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
258
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
259
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
260
			$usecoord = true;
261
		}
262
		$id = filter_var($id,FILTER_SANITIZE_STRING);
263
		$filter_query = $this->getFilter($filter,true,true);
264
265
		if (!isset($globalLiveInterval) || $globalLiveInterval == '') $globalLiveInterval = '200';
266
		if (!isset($globalMap3DAircraftsLimit) || $globalMap3DAircraftsLimit == '') $globalMap3DAircraftsLimit = '300';
267
		if ($globalDBdriver == 'mysql') {
268
			if (isset($globalArchive) && $globalArchive === TRUE) {
269
				/*
270
				$query  = 'SELECT spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk, spotter_archive.date, spotter_archive.format_source 
271
				FROM spotter_archive INNER JOIN (SELECT flightaware_id FROM spotter_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= spotter_live.date) l ON l.flightaware_id = spotter_archive.flightaware_id 
272
				WHERE spotter_archive.latitude <> '0' AND spotter_archive.longitude <> '0' 
273
				ORDER BY spotter_archive.flightaware_id, spotter_archive.date";
274
				*/
275
				$query  = 'SELECT * FROM (SELECT spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk, spotter_archive.date, spotter_archive.format_source, spotter_archive.registration 
276
				FROM spotter_archive INNER JOIN (SELECT flightaware_id FROM spotter_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= spotter_live.date) l ON l.flightaware_id = spotter_archive.flightaware_id ";
277
				if ($usecoord) $query .= "AND (spotter_archive.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_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 $maxlat 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...
278
				if ($id != '') $query .= "OR spotter_archive.flightaware_id = :id ";
279
				$query .= "UNION
280
				SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
281
				FROM spotter_live".$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= spotter_live.date";
282
				if ($usecoord) $query .= " AND (spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong.")";
283
				if ($id != '') $query .= " OR spotter_live.flightaware_id = :id";
284
				$query .= ") AS spotter 
285
				WHERE latitude <> '0' AND longitude <> '0' 
286
				ORDER BY flightaware_id, date";
287
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
288
			} else {
289
				$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
290
				FROM spotter_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= spotter_live.date ";
291
				if ($usecoord) $query .= "AND (spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong.") ";
292
				if ($id != '') $query .= "OR spotter_live.flightaware_id = :id ";
293
				$query .= "AND spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' 
294
				ORDER BY spotter_live.flightaware_id, spotter_live.date";
295
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
296
			}
297
		} else {
298
			if (isset($globalArchive) && $globalArchive === TRUE) {
299
				/*
300
				$query  = "SELECT spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk, spotter_archive.date, spotter_archive.format_source 
301
				FROM spotter_archive INNER JOIN (SELECT flightaware_id FROM spotter_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date) l ON l.flightaware_id = spotter_archive.flightaware_id 
302
				WHERE spotter_archive.latitude <> '0' AND spotter_archive.longitude <> '0' 
303
				ORDER BY spotter_archive.flightaware_id, spotter_archive.date";
304
                               */
305
				$query  = "SELECT * FROM (
306
				SELECT spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk, spotter_archive.date, spotter_archive.format_source, spotter_archive.registration 
307
				FROM spotter_archive 
308
				INNER JOIN (
309
				    SELECT flightaware_id 
310
				    FROM spotter_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date";
311
				$query.= ") l ON l.flightaware_id = spotter_archive.flightaware_id ";
312
				if ($usecoord) $query .= "AND (spotter_archive.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_archive.longitude BETWEEN ".$minlong." AND ".$maxlong.") ";
313
				if ($id != '') $query .= "OR spotter_archive.flightaware_id = :id ";
314
				$query .= "UNION
315
				    SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
316
				    FROM spotter_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date";
317
				if ($usecoord) $query .= " AND (spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong.")";
318
				if ($id != '') $query .= " OR spotter_live.flightaware_id = :id";
319
				$query .= ") AS spotter WHERE latitude <> '0' AND longitude <> '0' ";
320
				$query .= "ORDER BY flightaware_id, date";
321
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
322
			} else {
323
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
324
				FROM spotter_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date ";
325
				if ($usecoord) $query .= "AND (spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong.") ";
326
				if ($id != '') $query .= "OR spotter_live.flightaware_id = :id ";
327
				$query .= "AND spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' 
328
				ORDER BY spotter_live.flightaware_id, spotter_live.date";
329
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
330
			}
331
		}
332
		$query_values = array();
333
		if ($id != '') $query_values = array(':id' => $id);
334
		try {
335
			$sth = $this->db->prepare($query);
336
			$sth->execute($query_values);
337
		} catch(PDOException $e) {
338
			echo $e->getMessage();
339
			die;
340
		}
341
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
342
		return $spotter_array;
343
	}
344
345
	/**
346
	* Gets Minimal Live Spotter data since xx seconds
347
	*
348
	* @return Array the spotter information
349
	*
350
	*/
351
	public function getMinLastLiveSpotterDataByID($id = '',$filter = array(), $limit = false)
352
	{
353
		global $globalDBdriver, $globalLiveInterval, $globalArchive, $globalMap3DAircraftsLimit;
354
		date_default_timezone_set('UTC');
355
		$id = filter_var($id,FILTER_SANITIZE_STRING);
356
		$filter_query = $this->getFilter($filter,true,true);
357
358
		if (!isset($globalLiveInterval) || $globalLiveInterval == '') $globalLiveInterval = '200';
359
		if (!isset($globalMap3DAircraftsLimit) || $globalMap3DAircraftsLimit == '') $globalMap3DAircraftsLimit = '300';
360
		if ($globalDBdriver == 'mysql') {
361
			if (isset($globalArchive) && $globalArchive === TRUE) {
362
				$query  = 'SELECT * FROM (SELECT spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk, spotter_archive.date, spotter_archive.format_source, spotter_archive.registration 
363
				FROM spotter_archive INNER JOIN (SELECT flightaware_id FROM spotter_live'.$filter_query.' spotter_live.flightaware_id = :id) l ON l.flightaware_id = spotter_archive.flightaware_id ';
364
				$query .= "UNION
365
				SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
366
				FROM spotter_live".$filter_query.' spotter_live.flightaware_id = :id';
367
				$query .= ") AS spotter 
368
				WHERE latitude <> '0' AND longitude <> '0' 
369
				ORDER BY flightaware_id, date";
370
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
371
			} else {
372
				$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
373
				FROM spotter_live'.$filter_query.' spotter_live.flightaware_id = :id ';
374
				$query .= "AND spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' 
375
				ORDER BY spotter_live.flightaware_id, spotter_live.date";
376
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
377
			}
378
		} else {
379
			if (isset($globalArchive) && $globalArchive === TRUE) {
380
				$query  = "SELECT * FROM (
381
				SELECT spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk, spotter_archive.date, spotter_archive.format_source, spotter_archive.registration 
382
				FROM spotter_archive 
383
				INNER JOIN (
384
				    SELECT flightaware_id 
385
				    FROM spotter_live".$filter_query." spotter_live.flightaware_id = :id";
386
				$query.= ") l ON l.flightaware_id = spotter_archive.flightaware_id ";
387
				$query .= "UNION
388
				    SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
389
				    FROM spotter_live".$filter_query." spotter_live.flightaware_id = :id";
390
				$query .= ") AS spotter WHERE latitude <> '0' AND longitude <> '0' ";
391
				$query .= "ORDER BY flightaware_id, date";
392
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
393
			} else {
394
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
395
				FROM spotter_live".$filter_query." spotter_live.flightaware_id = :id ";
396
				$query .= "AND spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' 
397
				ORDER BY spotter_live.flightaware_id, spotter_live.date";
398
				if ($limit) $query .= " LIMIT ".$globalMap3DAircraftsLimit;
399
			}
400
		}
401
		$query_values = array();
0 ignored issues
show
Unused Code introduced by
$query_values 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...
402
		$query_values = array(':id' => $id);
403
		try {
404
			$sth = $this->db->prepare($query);
405
			$sth->execute($query_values);
406
		} catch(PDOException $e) {
407
			echo $e->getMessage();
408
			die;
409
		}
410
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
411
		return $spotter_array;
412
	}
413
414
	/**
415
	* Gets number of latest data entry
416
	*
417
	* @return String number of entry
418
	*
419
	*/
420
	public function getLiveSpotterCount($filter = array())
421
	{
422
		global $globalDBdriver, $globalLiveInterval;
423
		$filter_query = $this->getFilter($filter,true,true);
424
425
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
426
		if ($globalDBdriver == 'mysql') {
427
			//$query  = 'SELECT COUNT(*) as nb FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate'.$filter_query;
428
			$query = 'SELECT COUNT(DISTINCT spotter_live.flightaware_id) as nb FROM spotter_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date';
429
		} else {
430
			//$query  = "SELECT COUNT(*) as nb FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE NOW() AT TIME ZONE 'UTC' - '".$globalLiveInterval." SECONDS'->INTERVAL <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate".$filter_query;
431
			$query = "SELECT COUNT(DISTINCT spotter_live.flightaware_id) as nb FROM spotter_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= date";
432
		}
433
		try {
434
			$sth = $this->db->prepare($query);
435
			$sth->execute();
436
		} catch(PDOException $e) {
437
			echo $e->getMessage();
438
			die;
439
		}
440
		$result = $sth->fetch(PDO::FETCH_ASSOC);
441
		$sth->closeCursor();
442
		return $result['nb'];
443
	}
444
445
	/**
446
	* Gets all the spotter information based on the latest data entry and coord
447
	*
448
	* @return Array the spotter information
449
	*
450
	*/
451
	public function getLiveSpotterDatabyCoord($coord, $filter = array())
452
	{
453
		global $globalDBdriver, $globalLiveInterval,$globalMap2DAircraftsLimit;
454
		$Spotter = new Spotter($this->db);
455
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
456
		$filter_query = $this->getFilter($filter);
457
458
		if (is_array($coord)) {
459
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
460
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
461
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
462
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
463
		} else return array();
464
		if ($globalDBdriver == 'mysql') {
465
			$query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate AND spotter_live.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND spotter_live.longitude BETWEEN '.$minlong.' AND '.$maxlong.' GROUP BY spotter_live.flightaware_id'.$filter_query;
466
		} else {
467
			$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE NOW() at time zone 'UTC'  - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate AND spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong." GROUP BY spotter_live.flightaware_id".$filter_query;
468
		}
469
		if (isset($globalMap2DAircraftsLimit) && $globalMap2DAircraftsLimit != '') {
470
			$query .= ' LIMIT '.$globalMap2DAircraftsLimit;
471
		}
472
473
		$spotter_array = $Spotter->getDataFromDB($query);
474
		return $spotter_array;
475
	}
476
477
	/**
478
	* Gets all the spotter information based on the latest data entry and coord
479
	*
480
	* @return Array the spotter information
481
	*
482
	*/
483
	public function getMinLiveSpotterDatabyCoord($coord,$limit = 0, $filter = array())
484
	{
485
		global $globalDBdriver, $globalLiveInterval, $globalArchive,$globalMap2DAircraftsLimit;
486
		$Spotter = new Spotter($this->db);
0 ignored issues
show
Unused Code introduced by
$Spotter 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...
487
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
488
		$filter_query = $this->getFilter($filter,true,true);
489
490
		if (is_array($coord)) {
491
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
492
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
493
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
494
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
495
		} else return array();
496
		if ($globalDBdriver == 'mysql') {
497
			if (isset($globalArchive) && $globalArchive === TRUE) {
498
				$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
499
				FROM spotter_live 
500
				'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= spotter_live.date 
501
				AND spotter_live.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND spotter_live.longitude BETWEEN '.$minlong.' AND '.$maxlong.'
502
				AND spotter_live.latitude <> 0 AND spotter_live.longitude <> 0 ORDER BY date DESC';
503
			} else {
504
				$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
505
				FROM spotter_live 
506
				INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate 
507
				    FROM spotter_live l 
508
				    WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date 
509
				    AND l.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND l.longitude BETWEEN '.$minlong.' AND '.$maxlong.'
510
				    GROUP BY l.flightaware_id
511
				) s on spotter_live.flightaware_id = s.flightaware_id 
512
				AND spotter_live.date = s.maxdate'.$filter_query.' spotter_live.latitude <> 0 AND spotter_live.longitude <> 0 ORDER BY date DESC';
513
			}
514
		} else {
515
			if (isset($globalArchive) && $globalArchive === TRUE) {
516
				/*
517
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
518
				FROM spotter_live 
519
				".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date 
520
				AND spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." 
521
				AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong." 
522
				AND spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' ORDER BY date DESC";
523
				*/
524
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
525
				FROM spotter_live 
526
				".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date 
527
				AND spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." 
528
				AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong." 
529
				AND spotter_live.latitude <> '0' AND spotter_live.longitude <> '0'";
530
			} else {
531
				/*
532
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
533
				FROM spotter_live 
534
				INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate 
535
				    FROM spotter_live l 
536
				    WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date 
537
				    AND l.latitude BETWEEN ".$minlat." AND ".$maxlat." 
538
				    AND l.longitude BETWEEN ".$minlong." AND ".$maxlong." 
539
				    GROUP BY l.flightaware_id
540
				) s on spotter_live.flightaware_id = s.flightaware_id 
541
				AND spotter_live.date = s.maxdate".$filter_query." spotter_live.latitude <> '0' AND spotter_live.longitude <> '0' ORDER BY date DESC";
542
				*/
543
				$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.real_altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source, spotter_live.registration 
544
				FROM spotter_live 
545
				INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate 
546
				    FROM spotter_live l 
547
				    WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date 
548
				    AND l.latitude BETWEEN ".$minlat." AND ".$maxlat." 
549
				    AND l.longitude BETWEEN ".$minlong." AND ".$maxlong." 
550
				    GROUP BY l.flightaware_id
551
				) s on spotter_live.flightaware_id = s.flightaware_id 
552
				AND spotter_live.date = s.maxdate".$filter_query." spotter_live.latitude <> '0' AND spotter_live.longitude <> '0'";
553
			}
554
		}
555
		if ($limit == 0 && isset($globalMap2DAircraftsLimit) && $globalMap2DAircraftsLimit != '') {
556
			$limit = $globalMap2DAircraftsLimit;
557
		}
558
		if ($limit != 0 && filter_var($limit,FILTER_VALIDATE_INT)) {
559
			$query .= ' LIMIT '.$limit;
560
		}
561
		try {
562
			$sth = $this->db->prepare($query);
563
			$sth->execute();
564
		} catch(PDOException $e) {
565
			echo $e->getMessage();
566
			die;
567
		}
568
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
569
		return $spotter_array;
570
	}
571
572
	/**
573
	* Gets all the spotter information based on a user's latitude and longitude
574
	*
575
	* @return Array the spotter information
576
	*
577
	*/
578
	public function getLatestSpotterForLayar($lat, $lng, $radius, $interval)
579
	{
580
		$Spotter = new Spotter($this->db);
581
		date_default_timezone_set('UTC');
582
		if ($lat != '') {
583
			if (!is_numeric($lat)) {
584
				return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by SpotterLive::getLatestSpotterForLayar of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
585
			}
586
		}
587
        if ($lng != '')
588
                {
589
                        if (!is_numeric($lng))
590
                        {
591
                                return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by SpotterLive::getLatestSpotterForLayar of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
592
                        }
593
                }
594
595
                if ($radius != '')
596
                {
597
                        if (!is_numeric($radius))
598
                        {
599
                                return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by SpotterLive::getLatestSpotterForLayar of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
600
                        }
601
                }
602
		$additional_query = '';
603
        if ($interval != '')
604
                {
605
                        if (!is_string($interval))
606
                        {
607
                                //$additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= spotter_live.date ';
608
			        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by SpotterLive::getLatestSpotterForLayar of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
609
                        } else {
610
                if ($interval == '1m')
611
                {
612
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= spotter_live.date ';
613
                } else if ($interval == '15m'){
614
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 15 MINUTE) <= spotter_live.date ';
615
                } 
616
            }
617
                } else {
618
         $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= spotter_live.date ';   
619
        }
620
621
                $query  = "SELECT spotter_live.*, ( 6371 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lng) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM spotter_live 
622
                   WHERE spotter_live.latitude <> '' 
623
                                   AND spotter_live.longitude <> '' 
624
                   ".$additional_query."
625
                   HAVING distance < :radius  
626
                                   ORDER BY distance";
627
628
                $spotter_array = $Spotter->getDataFromDB($query, array(':lat' => $lat, ':lng' => $lng,':radius' => $radius));
629
630
                return $spotter_array;
631
        }
632
633
    
634
        /**
635
	* Gets all the spotter information based on a particular callsign
636
	*
637
	* @return Array the spotter information
638
	*
639
	*/
640
	public function getLastLiveSpotterDataByIdent($ident)
641
	{
642
		$Spotter = new Spotter($this->db);
643
		date_default_timezone_set('UTC');
644
645
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
646
                $query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.ident = :ident GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
647
648
		$spotter_array = $Spotter->getDataFromDB($query,array(':ident' => $ident),'',true);
649
650
		return $spotter_array;
651
	}
652
653
        /**
654
	* Gets all the spotter information based on a particular callsign
655
	*
656
	* @return Array the spotter information
657
	*
658
	*/
659
	public function getDateLiveSpotterDataByIdent($ident,$date)
660
	{
661
		$Spotter = new Spotter($this->db);
662
		date_default_timezone_set('UTC');
663
664
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
665
                $query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.ident = :ident AND l.date <= :date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
666
667
                $date = date('c',$date);
668
		$spotter_array = $Spotter->getDataFromDB($query,array(':ident' => $ident,':date' => $date));
669
670
		return $spotter_array;
671
	}
672
673
        /**
674
	* Gets last spotter information based on a particular callsign
675
	*
676
	* @return Array the spotter information
677
	*
678
	*/
679
	public function getLastLiveSpotterDataById($id)
680
	{
681
		$Spotter = new Spotter($this->db);
682
		date_default_timezone_set('UTC');
683
		$id = filter_var($id, FILTER_SANITIZE_STRING);
684
		$query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.flightaware_id = :id GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
685
		$spotter_array = $Spotter->getDataFromDB($query,array(':id' => $id),'',true);
686
		return $spotter_array;
687
	}
688
689
        /**
690
	* Gets last spotter information based on a particular callsign
691
	*
692
	* @return Array the spotter information
693
	*
694
	*/
695
	public function getDateLiveSpotterDataById($id,$date)
696
	{
697
		$Spotter = new Spotter($this->db);
698
		date_default_timezone_set('UTC');
699
700
		$id = filter_var($id, FILTER_SANITIZE_STRING);
701
		$query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.flightaware_id = :id AND l.date <= :date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
702
		$date = date('c',$date);
703
		$spotter_array = $Spotter->getDataFromDB($query,array(':id' => $id,':date' => $date),'',true);
704
		return $spotter_array;
705
	}
706
707
        /**
708
	* Gets altitude information based on a particular callsign
709
	*
710
	* @return Array the spotter information
711
	*
712
	*/
713
	public function getAltitudeLiveSpotterDataByIdent($ident)
714
	{
715
716
		date_default_timezone_set('UTC');
717
718
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
719
                $query  = 'SELECT spotter_live.altitude, spotter_live.date FROM spotter_live WHERE spotter_live.ident = :ident';
720
721
    		try {
722
			
723
			$sth = $this->db->prepare($query);
724
			$sth->execute(array(':ident' => $ident));
725
		} catch(PDOException $e) {
726
			echo $e->getMessage();
727
			die;
728
		}
729
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
730
731
		return $spotter_array;
732
	}
733
734
        /**
735
	* Gets all the spotter information based on a particular id
736
	*
737
	* @return Array the spotter information
738
	*
739
	*/
740
	public function getAllLiveSpotterDataById($id,$liveinterval = false)
741
	{
742
		global $globalDBdriver, $globalLiveInterval;
743
		date_default_timezone_set('UTC');
744
		$id = filter_var($id, FILTER_SANITIZE_STRING);
745
		//$query  = self::$global_query.' WHERE spotter_live.flightaware_id = :id ORDER BY date';
746
		if ($globalDBdriver == 'mysql') {
747
			$query = 'SELECT spotter_live.* FROM spotter_live WHERE spotter_live.flightaware_id = :id';
748
			if ($liveinterval) $query .= ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date';
749
			$query .= ' ORDER BY date';
750
		} else {
751
			$query = 'SELECT spotter_live.* FROM spotter_live WHERE spotter_live.flightaware_id = :id';
752
			if ($liveinterval) $query .= " AND CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= date";
753
			$query .= ' ORDER BY date';
754
		}
755
756
		try {
757
			$sth = $this->db->prepare($query);
758
			$sth->execute(array(':id' => $id));
759
		} catch(PDOException $e) {
760
			echo $e->getMessage();
761
			die;
762
		}
763
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
764
		return $spotter_array;
765
	}
766
767
        /**
768
	* Gets all the spotter information based on a particular ident
769
	*
770
	* @return Array the spotter information
771
	*
772
	*/
773
	public function getAllLiveSpotterDataByIdent($ident)
774
	{
775
		date_default_timezone_set('UTC');
776
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
777
		$query  = self::$global_query.' WHERE spotter_live.ident = :ident';
778
    		try {
779
			
780
			$sth = $this->db->prepare($query);
781
			$sth->execute(array(':ident' => $ident));
782
		} catch(PDOException $e) {
783
			echo $e->getMessage();
784
			die;
785
		}
786
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
787
		return $spotter_array;
788
	}
789
790
791
	/**
792
	* Deletes all info in the table
793
	*
794
	* @return String success or false
795
	*
796
	*/
797
	public function deleteLiveSpotterData()
798
	{
799
		global $globalDBdriver;
800
		if ($globalDBdriver == 'mysql') {
801
			//$query  = "DELETE FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE) >= spotter_live.date";
802
			$query  = 'DELETE FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 9 HOUR) >= spotter_live.date';
803
            		//$query  = "DELETE FROM spotter_live WHERE spotter_live.id IN (SELECT spotter_live.id FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) >= spotter_live.date)";
804
		} else {
805
			$query  = "DELETE FROM spotter_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= spotter_live.date";
806
		}
807
        
808
    		try {
809
			
810
			$sth = $this->db->prepare($query);
811
			$sth->execute();
812
		} catch(PDOException $e) {
813
			return "error";
814
		}
815
816
		return "success";
817
	}
818
819
	/**
820
	* Deletes all info in the table for aircraft not seen since 2 HOUR
821
	*
822
	* @return String success or false
823
	*
824
	*/
825
	public function deleteLiveSpotterDataNotUpdated()
826
	{
827
		global $globalDBdriver, $globalDebug;
828
		if ($globalDBdriver == 'mysql') {
829
			//$query = 'SELECT flightaware_id FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) >= spotter_live.date AND spotter_live.flightaware_id NOT IN (SELECT flightaware_id FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) < spotter_live.date) LIMIT 800 OFFSET 0';
830
    			$query = "SELECT spotter_live.flightaware_id FROM spotter_live INNER JOIN (SELECT flightaware_id,MAX(date) as max_date FROM spotter_live GROUP BY flightaware_id) s ON s.flightaware_id = spotter_live.flightaware_id AND DATE_SUB(UTC_TIMESTAMP(), INTERVAL 2 HOUR) >= s.max_date LIMIT 2000 OFFSET 0";
831
    			try {
832
				
833
				$sth = $this->db->prepare($query);
834
				$sth->execute();
835
			} catch(PDOException $e) {
836
				return "error";
837
			}
838
			$query_delete = 'DELETE FROM spotter_live WHERE flightaware_id IN (';
839
                        $i = 0;
840
                        $j =0;
841
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
842
			foreach($all as $row)
843
			{
844
				$i++;
845
				$j++;
846
				if ($j == 30) {
847
					if ($globalDebug) echo ".";
848
				    	try {
849
						
850
						$sth = $this->db->prepare(substr($query_delete,0,-1).")");
851
						$sth->execute();
852
					} catch(PDOException $e) {
853
						return "error";
854
					}
855
                                	$query_delete = 'DELETE FROM spotter_live WHERE flightaware_id IN (';
856
                                	$j = 0;
857
				}
858
				$query_delete .= "'".$row['flightaware_id']."',";
859
			}
860
			if ($i > 0) {
861
    				try {
862
					
863
					$sth = $this->db->prepare(substr($query_delete,0,-1).")");
864
					$sth->execute();
865
				} catch(PDOException $e) {
866
					return "error";
867
				}
868
			}
869
			return "success";
870
		} elseif ($globalDBdriver == 'pgsql') {
871
			//$query = "SELECT flightaware_id FROM spotter_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= spotter_live.date AND spotter_live.flightaware_id NOT IN (SELECT flightaware_id FROM spotter_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' < spotter_live.date) LIMIT 800 OFFSET 0";
872
    			//$query = "SELECT spotter_live.flightaware_id FROM spotter_live INNER JOIN (SELECT flightaware_id,MAX(date) as max_date FROM spotter_live GROUP BY flightaware_id) s ON s.flightaware_id = spotter_live.flightaware_id AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 800 OFFSET 0";
873
    			$query = "DELETE FROM spotter_live WHERE flightaware_id IN (SELECT spotter_live.flightaware_id FROM spotter_live INNER JOIN (SELECT flightaware_id,MAX(date) as max_date FROM spotter_live GROUP BY flightaware_id) s ON s.flightaware_id = spotter_live.flightaware_id AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 2000 OFFSET 0)";
874
    			try {
875
				
876
				$sth = $this->db->prepare($query);
877
				$sth->execute();
878
			} catch(PDOException $e) {
879
				return "error";
880
			}
881
/*			$query_delete = "DELETE FROM spotter_live WHERE flightaware_id IN (";
882
                        $i = 0;
883
                        $j =0;
884
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
885
			foreach($all as $row)
886
			{
887
				$i++;
888
				$j++;
889
				if ($j == 100) {
890
					if ($globalDebug) echo ".";
891
				    	try {
892
						
893
						$sth = $this->db->query(substr($query_delete,0,-1).")");
894
						//$sth->execute();
895
					} catch(PDOException $e) {
896
						return "error";
897
					}
898
                                	$query_delete = "DELETE FROM spotter_live WHERE flightaware_id IN (";
899
                                	$j = 0;
900
				}
901
				$query_delete .= "'".$row['flightaware_id']."',";
902
			}
903
			if ($i > 0) {
904
    				try {
905
					
906
					$sth = $this->db->query(substr($query_delete,0,-1).")");
907
					//$sth->execute();
908
				} catch(PDOException $e) {
909
					return "error";
910
				}
911
			}
912
*/
913
			return "success";
914
		}
915
	}
916
917
	/**
918
	* Deletes all info in the table for an ident
919
	*
920
	* @return String success or false
921
	*
922
	*/
923
	public function deleteLiveSpotterDataByIdent($ident)
924
	{
925
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
926
		$query  = 'DELETE FROM spotter_live WHERE ident = :ident';
927
        
928
    		try {
929
			
930
			$sth = $this->db->prepare($query);
931
			$sth->execute(array(':ident' => $ident));
932
		} catch(PDOException $e) {
933
			return "error";
934
		}
935
936
		return "success";
937
	}
938
939
	/**
940
	* Deletes all info in the table for an id
941
	*
942
	* @return String success or false
943
	*
944
	*/
945
	public function deleteLiveSpotterDataById($id)
946
	{
947
		$id = filter_var($id, FILTER_SANITIZE_STRING);
948
		$query  = 'DELETE FROM spotter_live WHERE flightaware_id = :id';
949
        
950
    		try {
951
			
952
			$sth = $this->db->prepare($query);
953
			$sth->execute(array(':id' => $id));
954
		} catch(PDOException $e) {
955
			return "error";
956
		}
957
958
		return "success";
959
	}
960
961
962
	/**
963
	* Gets the aircraft ident within the last hour
964
	*
965
	* @return String the ident
966
	*
967
	*/
968
	public function getIdentFromLastHour($ident)
969
	{
970
		global $globalDBdriver, $globalTimezone;
971
		if ($globalDBdriver == 'mysql') {
972
			$query  = 'SELECT spotter_live.ident FROM spotter_live 
973
				WHERE spotter_live.ident = :ident 
974
				AND spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
975
				AND spotter_live.date < UTC_TIMESTAMP()';
976
			$query_data = array(':ident' => $ident);
977
		} else {
978
			$query  = "SELECT spotter_live.ident FROM spotter_live 
979
				WHERE spotter_live.ident = :ident 
980
				AND spotter_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
981
				AND spotter_live.date < now() AT TIME ZONE 'UTC'";
982
			$query_data = array(':ident' => $ident);
983
		}
984
		
985
		$sth = $this->db->prepare($query);
986
		$sth->execute($query_data);
987
		$ident_result='';
988
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
989
		{
990
			$ident_result = $row['ident'];
991
		}
992
		return $ident_result;
993
        }
994
995
	/**
996
	* Check recent aircraft
997
	*
998
	* @return String the ident
999
	*
1000
	*/
1001
	public function checkIdentRecent($ident)
1002
	{
1003
		global $globalDBdriver, $globalTimezone;
1004
		if ($globalDBdriver == 'mysql') {
1005
			$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id FROM spotter_live 
1006
				WHERE spotter_live.ident = :ident 
1007
				AND spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 MINUTE)'; 
1008
//				AND spotter_live.date < UTC_TIMESTAMP()";
1009
			$query_data = array(':ident' => $ident);
1010
		} else {
1011
			$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id FROM spotter_live 
1012
				WHERE spotter_live.ident = :ident 
1013
				AND spotter_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '20 MINUTES'";
1014
//				AND spotter_live.date < now() AT TIME ZONE 'UTC'";
1015
			$query_data = array(':ident' => $ident);
1016
		}
1017
		
1018
		$sth = $this->db->prepare($query);
1019
		$sth->execute($query_data);
1020
		$ident_result='';
1021
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1022
		{
1023
			$ident_result = $row['flightaware_id'];
1024
		}
1025
		return $ident_result;
1026
        }
1027
1028
	/**
1029
	* Check recent aircraft by id
1030
	*
1031
	* @return String the ident
1032
	*
1033
	*/
1034
	public function checkIdRecent($id)
1035
	{
1036
		global $globalDBdriver, $globalTimezone;
1037
		if ($globalDBdriver == 'mysql') {
1038
			$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id FROM spotter_live 
1039
				WHERE spotter_live.flightaware_id = :id 
1040
				AND spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 10 HOUR)'; 
1041
//				AND spotter_live.date < UTC_TIMESTAMP()";
1042
			$query_data = array(':id' => $id);
1043
		} else {
1044
			$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id FROM spotter_live 
1045
				WHERE spotter_live.flightaware_id = :id 
1046
				AND spotter_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '10 HOUR'";
1047
//				AND spotter_live.date < now() AT TIME ZONE 'UTC'";
1048
			$query_data = array(':id' => $id);
1049
		}
1050
		
1051
		$sth = $this->db->prepare($query);
1052
		$sth->execute($query_data);
1053
		$ident_result='';
1054
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1055
		{
1056
			$ident_result = $row['flightaware_id'];
1057
		}
1058
		return $ident_result;
1059
        }
1060
1061
	/**
1062
	* Check recent aircraft by ModeS
1063
	*
1064
	* @return String the ModeS
1065
	*
1066
	*/
1067
	public function checkModeSRecent($modes)
1068
	{
1069
		global $globalDBdriver, $globalTimezone;
1070
		if ($globalDBdriver == 'mysql') {
1071
			$query  = 'SELECT spotter_live.ModeS, spotter_live.flightaware_id FROM spotter_live 
1072
				WHERE spotter_live.ModeS = :modes 
1073
				AND spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 MINUTE)'; 
1074
//				AND spotter_live.date < UTC_TIMESTAMP()";
1075
			$query_data = array(':modes' => $modes);
1076
		} else {
1077
			$query  = "SELECT spotter_live.ModeS, spotter_live.flightaware_id FROM spotter_live 
1078
				WHERE spotter_live.ModeS = :modes 
1079
				AND spotter_live.date >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 MINUTE'";
1080
//			//	AND spotter_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";
1081
			$query_data = array(':modes' => $modes);
1082
		}
1083
		
1084
		$sth = $this->db->prepare($query);
1085
		$sth->execute($query_data);
1086
		$ident_result='';
1087
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1088
		{
1089
			//$ident_result = $row['spotter_live_id'];
1090
			$ident_result = $row['flightaware_id'];
1091
		}
1092
		return $ident_result;
1093
	}
1094
1095
	/**
1096
	* Gets the aircraft data from the last 20 seconds
1097
	*
1098
	* @return Array the spotter data
1099
	*
1100
	*/
1101
	public function getRealTimeData($q = '')
1102
	{
1103
		global $globalDBdriver;
1104
		$additional_query = '';
1105
		if ($q != "")
1106
		{
1107
			if (!is_string($q))
1108
			{
1109
				return array();
1110
			} else {
1111
				$q_array = explode(" ", $q);
1112
				foreach ($q_array as $q_item){
1113
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
1114
					$additional_query .= " AND (";
1115
					$additional_query .= "(spotter_live.aircraft_icao like '%".$q_item."%') OR ";
1116
					$additional_query .= "(spotter_live.aircraft_name like '%".$q_item."%') OR ";
1117
					$additional_query .= "(spotter_live.aircraft_manufacturer like '%".$q_item."%') OR ";
1118
					$additional_query .= "(spotter_live.airline_icao like '%".$q_item."%') OR ";
1119
					$additional_query .= "(spotter_live.departure_airport_icao like '%".$q_item."%') OR ";
1120
					$additional_query .= "(spotter_live.arrival_airport_icao like '%".$q_item."%') OR ";
1121
					$additional_query .= "(spotter_live.registration like '%".$q_item."%') OR ";
1122
					$additional_query .= "(spotter_live.ident like '%".$q_item."%')";
1123
					$additional_query .= ")";
1124
				}
1125
			}
1126
		}
1127
		if ($globalDBdriver == 'mysql') {
1128
			$query  = "SELECT spotter_live.* FROM spotter_live 
1129
			    WHERE spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 SECOND) ".$additional_query." 
1130
			    AND spotter_live.date < UTC_TIMESTAMP()";
1131
		} else {
1132
			$query  = "SELECT spotter_live.* FROM spotter_live 
1133
			    WHERE spotter_live.date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 SECONDS' ".$additional_query." 
1134
			    AND spotter_live.date::timestamp < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";
1135
		}
1136
		$Spotter = new Spotter();
1137
		$spotter_array = $Spotter->getDataFromDB($query, array());
1138
		return $spotter_array;
1139
	}
1140
1141
	/**
1142
	* Adds a new spotter data
1143
	*
1144
	* @param String $flightaware_id the ID from flightaware
1145
	* @param String $ident the flight ident
1146
	* @param String $aircraft_icao the aircraft type
1147
	* @param String $departure_airport_icao the departure airport
1148
	* @param String $arrival_airport_icao the arrival airport
1149
	* @return String success or false
1150
	*
1151
	*/
1152
	public function addLiveSpotterData($flightaware_id = '', $ident = '', $aircraft_icao = '', $departure_airport_icao = '', $arrival_airport_icao = '', $latitude = '', $longitude = '', $waypoints = '', $altitude = '', $altitude_real = '',$heading = '', $groundspeed = '', $date = '',$departure_airport_time = '', $arrival_airport_time = '', $squawk = '', $route_stop = '', $ModeS = '', $putinarchive = false,$registration = '',$pilot_id = '', $pilot_name = '', $verticalrate = '', $noarchive = false, $ground = false,$format_source = '', $source_name = '', $over_country = '')
1153
	{
1154
		global $globalURL, $globalArchive, $globalDebug;
1155
		$Common = new Common();
1156
		date_default_timezone_set('UTC');
1157
1158
		//getting the airline information
1159
		if ($ident != '')
1160
		{
1161
			if (!is_string($ident))
1162
			{
1163
				return false;
1164
			} 
1165
		}
1166
1167
		//getting the aircraft information
1168
		if ($aircraft_icao != '')
1169
		{
1170
			if (!is_string($aircraft_icao))
1171
			{
1172
				return false;
1173
			} 
1174
		} 
1175
		//getting the departure airport information
1176
		if ($departure_airport_icao != '')
1177
		{
1178
			if (!is_string($departure_airport_icao))
1179
			{
1180
				return false;
1181
			} 
1182
		}
1183
1184
		//getting the arrival airport information
1185
		if ($arrival_airport_icao != '')
1186
		{
1187
			if (!is_string($arrival_airport_icao))
1188
			{
1189
				return false;
1190
			}
1191
		}
1192
1193
1194
		if ($latitude != '')
1195
		{
1196
			if (!is_numeric($latitude))
1197
			{
1198
				return false;
1199
			}
1200
		} else return '';
1201
1202
		if ($longitude != '')
1203
		{
1204
			if (!is_numeric($longitude))
1205
			{
1206
				return false;
1207
			}
1208
		} else return '';
1209
1210
		if ($waypoints != '')
1211
		{
1212
			if (!is_string($waypoints))
1213
			{
1214
				return false;
1215
			}
1216
		}
1217
1218
		if ($altitude != '')
1219
		{
1220
			if (!is_numeric($altitude))
1221
			{
1222
				return false;
1223
			}
1224
		} else $altitude = 0;
1225
		if ($altitude_real != '')
1226
		{
1227
			if (!is_numeric($altitude_real))
1228
			{
1229
				return false;
1230
			}
1231
		} else $altitude_real = 0;
1232
1233
		if ($heading != '')
1234
		{
1235
			if (!is_numeric($heading))
1236
			{
1237
				return false;
1238
			}
1239
		} else $heading = 0;
1240
1241
		if ($groundspeed != '')
1242
		{
1243
			if (!is_numeric($groundspeed))
1244
			{
1245
				return false;
1246
			}
1247
		} else $groundspeed = 0;
1248
		date_default_timezone_set('UTC');
1249
		if ($date == '') $date = date("Y-m-d H:i:s", time());
1250
1251
        
1252
		$flightaware_id = filter_var($flightaware_id,FILTER_SANITIZE_STRING);
1253
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1254
		$aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING);
1255
		$departure_airport_icao = filter_var($departure_airport_icao,FILTER_SANITIZE_STRING);
1256
		$arrival_airport_icao = filter_var($arrival_airport_icao,FILTER_SANITIZE_STRING);
1257
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1258
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1259
		$waypoints = filter_var($waypoints,FILTER_SANITIZE_STRING);
1260
		$altitude = filter_var($altitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1261
		$altitude_real = filter_var($altitude_real,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1262
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
1263
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1264
		$squawk = filter_var($squawk,FILTER_SANITIZE_NUMBER_INT);
1265
		$route_stop = filter_var($route_stop,FILTER_SANITIZE_STRING);
1266
		$ModeS = filter_var($ModeS,FILTER_SANITIZE_STRING);
1267
		$pilot_id = filter_var($pilot_id,FILTER_SANITIZE_STRING);
1268
		$pilot_name = filter_var($pilot_name,FILTER_SANITIZE_STRING);
1269
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
1270
		$source_name = filter_var($source_name,FILTER_SANITIZE_STRING);
1271
		$over_country = filter_var($over_country,FILTER_SANITIZE_STRING);
1272
		$verticalrate = filter_var($verticalrate,FILTER_SANITIZE_NUMBER_INT);
1273
1274
		$airline_name = '';
1275
		$airline_icao = '';
1276
		$airline_country = '';
1277
		$airline_type = '';
1278
		$aircraft_shadow = '';
1279
		$aircraft_type = '';
1280
		$aircraft_manufacturer = '';
1281
1282
1283
1284
		$aircraft_name = '';
1285
		$departure_airport_name = '';
1286
		$departure_airport_city = '';
1287
		$departure_airport_country = '';
1288
		
1289
		$arrival_airport_name = '';
1290
		$arrival_airport_city = '';
1291
		$arrival_airport_country = '';
1292
		
1293
            	
1294
            	if ($squawk == '' || $Common->isInteger($squawk) === false ) $squawk = NULL;
1295
            	if ($verticalrate == '' || $Common->isInteger($verticalrate) === false ) $verticalrate = NULL;
1296
            	if ($groundspeed == '' || $Common->isInteger($groundspeed) === false ) $groundspeed = 0;
1297
            	if ($heading == '' || $Common->isInteger($heading) === false ) $heading = 0;
1298
		
1299
		$query = '';
1300
		if ($globalArchive) {
1301
			if ($globalDebug) echo '-- Delete previous data -- ';
1302
			$query .= 'DELETE FROM spotter_live WHERE flightaware_id = :flightaware_id;';
1303
		}
1304
1305
		$query  .= 'INSERT INTO spotter_live (flightaware_id, ident, registration, airline_name, airline_icao, airline_country, airline_type, aircraft_icao, aircraft_shadow, aircraft_name, aircraft_manufacturer, departure_airport_icao, departure_airport_name, departure_airport_city, departure_airport_country, arrival_airport_icao, arrival_airport_name, arrival_airport_city, arrival_airport_country, latitude, longitude, waypoints, altitude, heading, ground_speed, date, departure_airport_time, arrival_airport_time, squawk, route_stop, ModeS, pilot_id, pilot_name, verticalrate, ground, format_source, source_name, over_country, real_altitude) 
1306
		VALUES (:flightaware_id,:ident,:registration,:airline_name,:airline_icao,:airline_country,:airline_type,:aircraft_icao,:aircraft_shadow,:aircraft_type,:aircraft_manufacturer,:departure_airport_icao,:departure_airport_name, :departure_airport_city, :departure_airport_country, :arrival_airport_icao, :arrival_airport_name, :arrival_airport_city, :arrival_airport_country, :latitude,:longitude,:waypoints,:altitude,:heading,:groundspeed,:date,:departure_airport_time,:arrival_airport_time,:squawk,:route_stop,:ModeS, :pilot_id, :pilot_name, :verticalrate, :ground, :format_source, :source_name, :over_country, :real_altitude)';
1307
1308
		$query_values = array(':flightaware_id' => $flightaware_id,':ident' => $ident, ':registration' => $registration,':airline_name' => $airline_name,':airline_icao' => $airline_icao,':airline_country' => $airline_country,':airline_type' => $airline_type,':aircraft_icao' => $aircraft_icao,':aircraft_shadow' => $aircraft_shadow,':aircraft_type' => $aircraft_type,':aircraft_manufacturer' => $aircraft_manufacturer,':departure_airport_icao' => $departure_airport_icao,':departure_airport_name' => $departure_airport_name,':departure_airport_city' => $departure_airport_city,':departure_airport_country' => $departure_airport_country,':arrival_airport_icao' => $arrival_airport_icao,':arrival_airport_name' => $arrival_airport_name,':arrival_airport_city' => $arrival_airport_city,':arrival_airport_country' => $arrival_airport_country,':latitude' => $latitude,':longitude' => $longitude, ':waypoints' => $waypoints,':altitude' => $altitude,':heading' => $heading,':groundspeed' => $groundspeed,':date' => $date, ':departure_airport_time' => $departure_airport_time,':arrival_airport_time' => $arrival_airport_time, ':squawk' => $squawk,':route_stop' => $route_stop,':ModeS' => $ModeS, ':pilot_id' => $pilot_id, ':pilot_name' => $pilot_name, ':verticalrate' => $verticalrate, ':format_source' => $format_source,':ground' => $ground, ':source_name' => $source_name, ':over_country' => $over_country,':real_altitude' => $altitude_real);
1309
		try {
1310
			
1311
			$sth = $this->db->prepare($query);
1312
			$sth->execute($query_values);
1313
			$sth->closeCursor();
1314
		} catch(PDOException $e) {
1315
			return "error : ".$e->getMessage();
1316
		}
1317
		if (isset($globalArchive) && $globalArchive && $putinarchive && $noarchive !== true) {
1318
		    if ($globalDebug) echo '(Add to SBS archive : ';
1319
		    $SpotterArchive = new SpotterArchive($this->db);
1320
		    $result =  $SpotterArchive->addSpotterArchiveData($flightaware_id, $ident, $registration, $airline_name, $airline_icao, $airline_country, $airline_type, $aircraft_icao, $aircraft_shadow, $aircraft_name, $aircraft_manufacturer, $departure_airport_icao, $departure_airport_name, $departure_airport_city, $departure_airport_country, $departure_airport_time,$arrival_airport_icao, $arrival_airport_name, $arrival_airport_city, $arrival_airport_country, $arrival_airport_time, $route_stop, $date,$latitude, $longitude, $waypoints, $altitude, $altitude_real,$heading, $groundspeed, $squawk, $ModeS, $pilot_id, $pilot_name,$verticalrate,$format_source,$source_name, $over_country);
1321
		    if ($globalDebug) echo $result.')';
1322
		} elseif ($globalDebug && $putinarchive !== true) {
1323
			echo '(Not adding to archive)';
1324
		} elseif ($globalDebug && $noarchive === true) {
1325
			echo '(No archive)';
1326
		}
1327
		return "success";
1328
1329
	}
1330
1331
	public function getOrderBy()
1332
	{
1333
		$orderby = array("aircraft_asc" => array("key" => "aircraft_asc", "value" => "Aircraft Type - ASC", "sql" => "ORDER BY spotter_live.aircraft_icao ASC"), "aircraft_desc" => array("key" => "aircraft_desc", "value" => "Aircraft Type - DESC", "sql" => "ORDER BY spotter_live.aircraft_icao DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY spotter_live.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY spotter_live.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY spotter_live.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY spotter_live.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY spotter_live.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY spotter_live.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure Airport - ASC", "sql" => "ORDER BY spotter_live.departure_airport_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY spotter_live.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY spotter_live.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY spotter_live.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY spotter_live.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY spotter_live.date DESC"));
1334
		return $orderby;
1335
	}
1336
1337
}
1338
1339
1340
?>
1341