Completed
Push — master ( 8a4155...eb4f96 )
by Yannick
07:53
created

SpotterLive::checkIdentRecent()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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