Completed
Push — master ( db7024...3e0d90 )
by Yannick
07:40
created

SpotterLive::deleteLiveSpotterDataNotUpdated()   C

Complexity

Conditions 11
Paths 26

Size

Total Lines 91
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
933
	{
934
		global $globalURL, $globalArchive, $globalDebug;
935
		$Common = new Common();
936
		date_default_timezone_set('UTC');
937
938
		//getting the airline information
939
		if ($ident != '')
940
		{
941
			if (!is_string($ident))
942
			{
943
				return false;
944
			} 
945
		}
946
947
		//getting the aircraft information
948
		if ($aircraft_icao != '')
949
		{
950
			if (!is_string($aircraft_icao))
951
			{
952
				return false;
953
			} 
954
		} 
955
		//getting the departure airport information
956
		if ($departure_airport_icao != '')
957
		{
958
			if (!is_string($departure_airport_icao))
959
			{
960
				return false;
961
			} 
962
		}
963
964
		//getting the arrival airport information
965
		if ($arrival_airport_icao != '')
966
		{
967
			if (!is_string($arrival_airport_icao))
968
			{
969
				return false;
970
			}
971
		}
972
973
974
		if ($latitude != '')
975
		{
976
			if (!is_numeric($latitude))
977
			{
978
				return false;
979
			}
980
		} else return '';
981
982
		if ($longitude != '')
983
		{
984
			if (!is_numeric($longitude))
985
			{
986
				return false;
987
			}
988
		} else return '';
989
990
		if ($waypoints != '')
991
		{
992
			if (!is_string($waypoints))
993
			{
994
				return false;
995
			}
996
		}
997
998
		if ($altitude != '')
999
		{
1000
			if (!is_numeric($altitude))
1001
			{
1002
				return false;
1003
			}
1004
		} else $altitude = 0;
1005
1006
		if ($heading != '')
1007
		{
1008
			if (!is_numeric($heading))
1009
			{
1010
				return false;
1011
			}
1012
		} else $heading = 0;
1013
1014
		if ($groundspeed != '')
1015
		{
1016
			if (!is_numeric($groundspeed))
1017
			{
1018
				return false;
1019
			}
1020
		} else $groundspeed = 0;
1021
		date_default_timezone_set('UTC');
1022
		if ($date == '') $date = date("Y-m-d H:i:s", time());
1023
1024
        
1025
		$flightaware_id = filter_var($flightaware_id,FILTER_SANITIZE_STRING);
1026
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1027
		$aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING);
1028
		$departure_airport_icao = filter_var($departure_airport_icao,FILTER_SANITIZE_STRING);
1029
		$arrival_airport_icao = filter_var($arrival_airport_icao,FILTER_SANITIZE_STRING);
1030
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1031
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1032
		$waypoints = filter_var($waypoints,FILTER_SANITIZE_STRING);
1033
		$altitude = filter_var($altitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1034
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
1035
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1036
		$squawk = filter_var($squawk,FILTER_SANITIZE_NUMBER_INT);
1037
		$route_stop = filter_var($route_stop,FILTER_SANITIZE_STRING);
1038
		$ModeS = filter_var($ModeS,FILTER_SANITIZE_STRING);
1039
		$pilot_id = filter_var($pilot_id,FILTER_SANITIZE_STRING);
1040
		$pilot_name = filter_var($pilot_name,FILTER_SANITIZE_STRING);
1041
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
1042
		$source_name = filter_var($source_name,FILTER_SANITIZE_STRING);
1043
		$over_country = filter_var($over_country,FILTER_SANITIZE_STRING);
1044
		$verticalrate = filter_var($verticalrate,FILTER_SANITIZE_NUMBER_INT);
1045
1046
		$airline_name = '';
1047
		$airline_icao = '';
1048
		$airline_country = '';
1049
		$airline_type = '';
1050
		$aircraft_shadow = '';
1051
		$aircraft_type = '';
1052
		$aircraft_manufacturer = '';
1053
1054
1055
1056
		$aircraft_name = '';
1057
		$departure_airport_name = '';
1058
		$departure_airport_city = '';
1059
		$departure_airport_country = '';
1060
		
1061
		$arrival_airport_name = '';
1062
		$arrival_airport_city = '';
1063
		$arrival_airport_country = '';
1064
		
1065
            	
1066
            	if ($squawk == '' || $Common->isInteger($squawk) === false ) $squawk = NULL;
1067
            	if ($verticalrate == '' || $Common->isInteger($verticalrate) === false ) $verticalrate = NULL;
1068
            	if ($groundspeed == '' || $Common->isInteger($groundspeed) === false ) $groundspeed = 0;
1069
            	if ($heading == '' || $Common->isInteger($heading) === false ) $heading = 0;
1070
		
1071
		$query = '';
1072
		if ($globalArchive) {
1073
			if ($globalDebug) echo '-- Delete previous data -- ';
1074
			$query .= 'DELETE FROM spotter_live WHERE flightaware_id = :flightaware_id;';
1075
		}
1076
1077
		$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) 
1078
		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)';
1079
1080
		$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);
1081
		try {
1082
			
1083
			$sth = $this->db->prepare($query);
1084
			$sth->execute($query_values);
1085
			$sth->closeCursor();
1086
		} catch(PDOException $e) {
1087
			return "error : ".$e->getMessage();
1088
		}
1089
		if (isset($globalArchive) && $globalArchive && $putinarchive && $noarchive !== true) {
1090
		    if ($globalDebug) echo '(Add to SBS archive : ';
1091
		    $SpotterArchive = new SpotterArchive($this->db);
1092
		    $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, $heading, $groundspeed, $squawk, $ModeS, $pilot_id, $pilot_name,$verticalrate,$format_source,$source_name, $over_country);
1093
		    if ($globalDebug) echo $result.')';
1094
		}
1095
		return "success";
1096
1097
	}
1098
1099
	public function getOrderBy()
1100
	{
1101
		$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"));
1102
		return $orderby;
1103
	}
1104
1105
}
1106
1107
1108
?>
1109