Completed
Push — master ( ba9f5e...7f1c9f )
by Yannick
14:25
created

SpotterLive::addLiveSpotterData()   F

Complexity

Conditions 37
Paths > 20000

Size

Total Lines 160
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 37
eloc 90
nc 230399
nop 27
dl 0
loc 160
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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