Completed
Push — master ( 7bb360...c01d59 )
by Yannick
22:07
created

TrackerArchive::getTrackerDataByPilot()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
nc 12
nop 4
dl 0
loc 46
rs 8.4751
c 0
b 0
f 0
1
<?php
2
class TrackerArchive {
3
	public $global_query = "SELECT tracker_archive.* FROM tracker_archive";
4
	public $db;
5
6
	public function __construct($dbc = null) {
7
		$Connection = new Connection($dbc);
8
		$this->db = $Connection->db;
9
	}
10
11
	/**
12
	* Get SQL query part for filter used
13
	* @param Array $filter the filter
14
	* @return Array the SQL part
15
	*/
16
	public function getFilter($filter = array(),$where = false,$and = false) {
17
		global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver;
18
		$filters = array();
19
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
20
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
21
				$filters = $globalStatsFilters[$globalFilterName];
22
			} else {
23
				$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]);
24
			}
25
		}
26
		if (isset($filter[0]['source'])) {
27
			$filters = array_merge($filters,$filter);
28
		}
29
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
30
		$filter_query_join = '';
31
		$filter_query_where = '';
32
		foreach($filters as $flt) {
33
			if (isset($flt['idents']) && !empty($flt['idents'])) {
34
				if (isset($flt['source'])) {
35
					$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_archive_output WHERE tracker_archive_output.ident IN ('".implode("','",$flt['idents'])."') AND tracker_archive_output.format_source IN ('".implode("','",$flt['source'])."')) spid ON spid.famtrackid = tracker_archive.famtrackid";
36
				} else {
37
					$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_archive_output WHERE tracker_archive_output.ident IN ('".implode("','",$flt['idents'])."')) spid ON spid.famtrackid = tracker_archive.famtrackid";
38
				}
39
			}
40
		}
41
		if (isset($filter['source']) && !empty($filter['source'])) {
42
			$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
43
		}
44
		if (isset($filter['ident']) && !empty($filter['ident'])) {
45
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
46
		}
47
		if ((isset($filter['year']) && $filter['year'] != '') || (isset($filter['month']) && $filter['month'] != '') || (isset($filter['day']) && $filter['day'] != '')) {
48
			$filter_query_date = '';
49
			if (isset($filter['year']) && $filter['year'] != '') {
50
				if ($globalDBdriver == 'mysql') {
51
					$filter_query_date .= " AND YEAR(tracker_archive_output.date) = '".$filter['year']."'";
52
				} else {
53
					$filter_query_date .= " AND EXTRACT(YEAR FROM tracker_archive_output.date) = '".$filter['year']."'";
54
				}
55
			}
56
			if (isset($filter['month']) && $filter['month'] != '') {
57
				if ($globalDBdriver == 'mysql') {
58
					$filter_query_date .= " AND MONTH(tracker_archive_output.date) = '".$filter['month']."'";
59
				} else {
60
					$filter_query_date .= " AND EXTRACT(MONTH FROM tracker_archive_output.date) = '".$filter['month']."'";
61
				}
62
			}
63
			if (isset($filter['day']) && $filter['day'] != '') {
64
				if ($globalDBdriver == 'mysql') {
65
					$filter_query_date .= " AND DAY(tracker_archive_output.date) = '".$filter['day']."'";
66
				} else {
67
					$filter_query_date .= " AND EXTRACT(DAY FROM tracker_archive_output.date) = '".$filter['day']."'";
68
				}
69
			}
70
			$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_archive_output".preg_replace('/^ AND/',' WHERE',$filter_query_date).") sd ON sd.famtrackid = tracker_archive.famtrackid";
71
		}
72
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
73
			$filter_query_where .= " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
74
		}
75
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
76
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
77
		if ($filter_query_where != '') {
78
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
79
		}
80
		$filter_query = $filter_query_join.$filter_query_where;
81
		return $filter_query;
82
	}
83
84
	// tracker_archive
85
	public function addTrackerArchiveData($famtrackid = '', $ident = '', $latitude = '', $longitude = '', $altitude = '', $heading = '', $groundspeed = '', $date = '', $putinarchive = false, $comment = '', $type = '',$noarchive = false,$format_source = '', $source_name = '', $over_country = '') {
86
		require_once(dirname(__FILE__).'/class.Tracker.php');
87
		if ($over_country == '') {
88
			$Tracker = new Tracker($this->db);
89
			$data_country = $Tracker->getCountryFromLatitudeLongitude($latitude,$longitude);
90
			if (!empty($data_country)) $country = $data_country['iso2'];
0 ignored issues
show
Unused Code introduced by
$country is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
			else $country = '';
0 ignored issues
show
Unused Code introduced by
$country is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
		} else $country = $over_country;
0 ignored issues
show
Unused Code introduced by
$country is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
		// Route is not added in tracker_archive
94
		$query  = 'INSERT INTO tracker_archive (famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, over_country, comment, type) 
95
		    VALUES (:famtrackid,:ident,:latitude,:longitude,:altitude,:heading,:groundspeed,:date,:format_source, :source_name, :over_country,:comment,:type)';
96
		$query_values = array(':famtrackid' => $famtrackid,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':altitude' => $altitude,':heading' => $heading,':groundspeed' => $groundspeed,':date' => $date, ':format_source' => $format_source, ':source_name' => $source_name, ':over_country' => $over_country,':comment' => $comment,':type' => $type);
97
		try {
98
			$sth = $this->db->prepare($query);
99
			$sth->execute($query_values);
100
			$sth->closeCursor();
101
		} catch(PDOException $e) {
102
			return "error : ".$e->getMessage();
103
		}
104
		return "success";
105
	}
106
107
108
        /**
109
        * Gets all the spotter information based on a particular callsign
110
        *
111
        * @return Array the spotter information
112
        *
113
        */
114
        public function getLastArchiveTrackerDataByIdent($ident)
115
        {
116
		$Tracker = new Tracker($this->db);
117
                date_default_timezone_set('UTC');
118
119
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
120
                //$query  = "SELECT tracker_archive.* FROM tracker_archive INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE l.ident = :ident GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate LIMIT 1";
121
                $query  = "SELECT tracker_archive.* FROM tracker_archive WHERE ident = :ident ORDER BY date DESC LIMIT 1";
122
123
                $spotter_array = $Tracker->getDataFromDB($query,array(':ident' => $ident));
124
125
                return $spotter_array;
126
        }
127
128
129
        /**
130
        * Gets last the spotter information based on a particular id
131
        *
132
        * @return Array the spotter information
133
        *
134
        */
135
        public function getLastArchiveTrackerDataById($id)
136
        {
137
    		$Tracker = new Tracker($this->db);
138
                date_default_timezone_set('UTC');
139
                $id = filter_var($id, FILTER_SANITIZE_STRING);
140
                //$query  = TrackerArchive->$global_query." WHERE tracker_archive.famtrackid = :id";
141
                //$query  = "SELECT tracker_archive.* FROM tracker_archive INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE l.famtrackid = :id GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate LIMIT 1";
142
                $query  = "SELECT * FROM tracker_archive WHERE famtrackid = :id ORDER BY date DESC LIMIT 1";
143
144
//              $spotter_array = Tracker->getDataFromDB($query,array(':id' => $id));
145
                  /*
146
                try {
147
                        $Connection = new Connection();
148
                        $sth = Connection->$db->prepare($query);
149
                        $sth->execute(array(':id' => $id));
150
                } catch(PDOException $e) {
151
                        return "error";
152
                }
153
                $spotter_array = $sth->fetchAll(PDO->FETCH_ASSOC);
154
                */
155
                $spotter_array = $Tracker->getDataFromDB($query,array(':id' => $id));
156
157
                return $spotter_array;
158
        }
159
160
        /**
161
        * Gets all the spotter information based on a particular id
162
        *
163
        * @return Array the spotter information
164
        *
165
        */
166
        public function getAllArchiveTrackerDataById($id)
167
	{
168
                date_default_timezone_set('UTC');
169
                $id = filter_var($id, FILTER_SANITIZE_STRING);
170
                $query  = $this->global_query." WHERE tracker_archive.famtrackid = :id ORDER BY date";
171
172
//              $spotter_array = Tracker->getDataFromDB($query,array(':id' => $id));
173
174
                try {
175
                        $sth = $this->db->prepare($query);
176
                        $sth->execute(array(':id' => $id));
177
                } catch(PDOException $e) {
178
                        echo $e->getMessage();
179
                        die;
180
                }
181
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
182
183
                return $spotter_array;
184
        }
185
186
        /**
187
        * Gets coordinate & time spotter information based on a particular id
188
        *
189
        * @return Array the spotter information
190
        *
191
        */
192
        public function getCoordArchiveTrackerDataById($id)
193
        {
194
                date_default_timezone_set('UTC');
195
                $id = filter_var($id, FILTER_SANITIZE_STRING);
196
                $query  = "SELECT tracker_archive.latitude, tracker_archive.longitude, tracker_archive.date FROM tracker_archive WHERE tracker_archive.famtrackid = :id";
197
198
//              $spotter_array = Tracker->getDataFromDB($query,array(':id' => $id));
199
200
                try {
201
                        $sth = $this->db->prepare($query);
202
                        $sth->execute(array(':id' => $id));
203
                } catch(PDOException $e) {
204
                        echo $e->getMessage();
205
                        die;
206
                }
207
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
208
209
                return $spotter_array;
210
        }
211
212
213
        /**
214
        * Gets altitude information based on a particular callsign
215
        *
216
        * @return Array the spotter information
217
        *
218
        */
219
        public function getAltitudeArchiveTrackerDataByIdent($ident)
220
        {
221
222
                date_default_timezone_set('UTC');
223
224
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
225
                $query  = "SELECT tracker_archive.altitude, tracker_archive.date FROM tracker_archive WHERE tracker_archive.ident = :ident AND tracker_archive.latitude <> 0 AND tracker_archive.longitude <> 0 ORDER BY date";
226
227
                try {
228
                        $sth = $this->db->prepare($query);
229
                        $sth->execute(array(':ident' => $ident));
230
                } catch(PDOException $e) {
231
                        echo $e->getMessage();
232
                        die;
233
                }
234
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
235
236
                return $spotter_array;
237
        }
238
239
        /**
240
        * Gets altitude information based on a particular id
241
        *
242
        * @return Array the spotter information
243
        *
244
        */
245
        public function getAltitudeArchiveTrackerDataById($id)
246
        {
247
248
                date_default_timezone_set('UTC');
249
250
                $id = filter_var($id, FILTER_SANITIZE_STRING);
251
                $query  = "SELECT tracker_archive.altitude, tracker_archive.date FROM tracker_archive WHERE tracker_archive.famtrackid = :id AND tracker_archive.latitude <> 0 AND tracker_archive.longitude <> 0 ORDER BY date";
252
253
                try {
254
                        $sth = $this->db->prepare($query);
255
                        $sth->execute(array(':id' => $id));
256
                } catch(PDOException $e) {
257
                        echo $e->getMessage();
258
                        die;
259
                }
260
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
261
262
                return $spotter_array;
263
        }
264
265
        /**
266
        * Gets altitude & speed information based on a particular id
267
        *
268
        * @return Array the spotter information
269
        *
270
        */
271
        public function getAltitudeSpeedArchiveTrackerDataById($id)
272
        {
273
274
                date_default_timezone_set('UTC');
275
276
                $id = filter_var($id, FILTER_SANITIZE_STRING);
277
                $query  = "SELECT tracker_archive.altitude, tracker_archive.ground_speed, tracker_archive.date FROM tracker_archive WHERE tracker_archive.famtrackid = :id ORDER BY date";
278
279
                try {
280
                        $sth = $this->db->prepare($query);
281
                        $sth->execute(array(':id' => $id));
282
                } catch(PDOException $e) {
283
                        echo $e->getMessage();
284
                        die;
285
                }
286
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
287
288
                return $spotter_array;
289
        }
290
291
292
        /**
293
        * Gets altitude information based on a particular callsign
294
        *
295
        * @return Array the spotter information
296
        *
297
        */
298
        public function getLastAltitudeArchiveTrackerDataByIdent($ident)
299
        {
300
301
                date_default_timezone_set('UTC');
302
303
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
304
                $query  = "SELECT tracker_archive.altitude, tracker_archive.date FROM tracker_archive INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE l.ident = :ident GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate LIMIT 1";
305
//                $query  = "SELECT tracker_archive.altitude, tracker_archive.date FROM tracker_archive WHERE tracker_archive.ident = :ident";
306
307
                try {
308
                        $sth = $this->db->prepare($query);
309
                        $sth->execute(array(':ident' => $ident));
310
                } catch(PDOException $e) {
311
                        echo $e->getMessage();
312
                        die;
313
                }
314
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
315
316
                return $spotter_array;
317
        }
318
319
320
321
       /**
322
        * Gets all the archive spotter information
323
        *
324
        * @return Array the spotter information
325
        *
326
        */
327
        public function getTrackerArchiveData($ident,$famtrackid,$date)
328
        {
329
    		$Tracker = new Tracker($this->db);
330
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
331
                $query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM spotter_live l WHERE l.ident = :ident AND l.famtrackid = :famtrackid AND l.date LIKE :date GROUP BY l.famtrackid) s on spotter_live.famtrackid = s.famtrackid AND spotter_live.date = s.maxdate";
332
333
                $spotter_array = $Tracker->getDataFromDB($query,array(':ident' => $ident,':famtrackid' => $famtrackid,':date' => $date.'%'));
334
335
                return $spotter_array;
336
        }
337
        
338
        public function deleteTrackerArchiveTrackData()
339
        {
340
		global $globalArchiveKeepTrackMonths;
341
                date_default_timezone_set('UTC');
342
		$query = 'DELETE FROM tracker_archive WHERE tracker_archive.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveKeepTrackMonths.' MONTH)';
343
                try {
344
                        $sth = $this->db->prepare($query);
345
                        $sth->execute();
346
                } catch(PDOException $e) {
347
                        echo $e->getMessage();
348
                        die;
349
                }
350
	}
351
352
	/**
353
        * Gets Minimal Live Tracker data
354
        *
355
        * @return Array the spotter information
356
        *
357
        */
358
        public function getMinLiveTrackerData($begindate,$enddate,$filter = array())
359
        {
360
                global $globalDBdriver, $globalLiveInterval;
361
                date_default_timezone_set('UTC');
362
363
                $filter_query = '';
364
                if (isset($filter['source']) && !empty($filter['source'])) {
365
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
366
                }
367
                // Use spotter_output also ?
368
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
369
                        $filter_query .= " INNER JOIN (SELECT famtrackid FROM tracker_archive_output WHERE tracker_archive_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.famtrackid = tracker_archive.famtrackid ";
370
                }
371
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
372
                        $filter_query .= " INNER JOIN (SELECT famtrackid FROM tracker_archive_output WHERE tracker_archive_output.airline_type = '".$filter['airlinestype']."') sa ON sa.famtrackid = tracker_archive.famtrackid ";
373
                }
374
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
375
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
376
                }
377
378
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
379
                if ($globalDBdriver == 'mysql') {
380
                        /*
381
                        $query  = 'SELECT a.aircraft_shadow, tracker_archive.ident, tracker_archive.famtrackid, tracker_archive.aircraft_icao, tracker_archive.departure_airport_icao as departure_airport, tracker_archive.arrival_airport_icao as arrival_airport, tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.squawk 
382
                    		    FROM tracker_archive 
383
                    		    INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE (l.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON tracker_archive.aircraft_icao = a.icao';
384
			*/
385
/*
386
			$query  = 'SELECT a.aircraft_shadow, tracker_archive.ident, tracker_archive.famtrackid, tracker_archive.aircraft_icao, tracker_archive.departure_airport_icao as departure_airport, tracker_archive.arrival_airport_icao as arrival_airport, tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.squawk 
387
				    FROM tracker_archive 
388
				    INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate 
389
						FROM tracker_archive l 
390
						WHERE (l.date BETWEEN DATE_SUB('."'".$begindate."'".',INTERVAL '.$globalLiveInterval.' SECOND) AND '."'".$begindate."'".') 
391
						GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid 
392
				    AND tracker_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON tracker_archive.aircraft_icao = a.icao';
393
*/
394
			$query  = 'SELECT tracker_archive.date,tracker_archive.famtrackid, tracker_archive.ident, tracker_archive.aircraft_icao, tracker_archive.departure_airport_icao as departure_airport, tracker_archive.arrival_airport_icao as arrival_airport, tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.squawk, a.aircraft_shadow,a.engine_type, a.engine_count, a.wake_category 
395
				    FROM tracker_archive 
396
				    INNER JOIN (SELECT * FROM aircraft) a on tracker_archive.aircraft_icao = a.icao
397
				    WHERE tracker_archive.date BETWEEN '."'".$begindate."'".' AND '."'".$begindate."'".' 
398
                        	    '.$filter_query.' ORDER BY famtrackid';
399
                } else {
400
                        //$query  = 'SELECT tracker_archive.ident, tracker_archive.famtrackid, tracker_archive.aircraft_icao, tracker_archive.departure_airport_icao as departure_airport, tracker_archive.arrival_airport_icao as arrival_airport, tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.squawk, a.aircraft_shadow FROM tracker_archive INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate '.$filter_query.'INNER JOIN (SELECT * FROM aircraft) a on tracker_archive.aircraft_icao = a.icao';
401
                        $query  = 'SELECT tracker_archive.date,tracker_archive.famtrackid, tracker_archive.ident, tracker_archive.aircraft_icao, tracker_archive.departure_airport_icao as departure_airport, tracker_archive.arrival_airport_icao as arrival_airport, tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.squawk, a.aircraft_shadow,a.engine_type, a.engine_count, a.wake_category 
402
                        	    FROM tracker_archive 
403
                        	    INNER JOIN (SELECT * FROM aircraft) a on tracker_archive.aircraft_icao = a.icao
404
                        	    WHERE tracker_archive.date >= '."'".$begindate."'".' AND tracker_archive.date <= '."'".$enddate."'".'
405
                        	    '.$filter_query.' ORDER BY famtrackid';
406
                }
407
                //echo $query;
408
                try {
409
                        $sth = $this->db->prepare($query);
410
                        $sth->execute();
411
                } catch(PDOException $e) {
412
                        echo $e->getMessage();
413
                        die;
414
                }
415
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
416
417
                return $spotter_array;
418
        }
419
420
	/**
421
        * Gets Minimal Live Tracker data
422
        *
423
        * @return Array the spotter information
424
        *
425
        */
426
        public function getMinLiveTrackerDataPlayback($begindate,$enddate,$filter = array())
427
        {
428
                global $globalDBdriver, $globalLiveInterval;
429
                date_default_timezone_set('UTC');
430
431
                $filter_query = '';
432
                if (isset($filter['source']) && !empty($filter['source'])) {
433
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
434
                }
435
                // Should use spotter_output also ?
436
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
437
                        $filter_query .= " INNER JOIN (SELECT famtrackid FROM tracker_archive_output WHERE tracker_archive_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.famtrackid = tracker_archive.famtrackid ";
438
                }
439
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
440
                        $filter_query .= " INNER JOIN (SELECT famtrackid FROM tracker_archive_output WHERE tracker_archive_output.airline_type = '".$filter['airlinestype']."') sa ON sa.famtrackid = tracker_archive.famtrackid ";
441
                }
442
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
443
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
444
                }
445
446
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
447
                if ($globalDBdriver == 'mysql') {
448
                        /*
449
                        $query  = 'SELECT a.aircraft_shadow, tracker_archive.ident, tracker_archive.famtrackid, tracker_archive.aircraft_icao, tracker_archive.departure_airport_icao as departure_airport, tracker_archive.arrival_airport_icao as arrival_airport, tracker_archive.latitude, tracker_archive.longitude, tracker_archive.altitude, tracker_archive.heading, tracker_archive.ground_speed, tracker_archive.squawk 
450
                    		    FROM tracker_archive 
451
                    		    INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE (l.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON tracker_archive.aircraft_icao = a.icao';
452
			*/
453
			$query  = 'SELECT a.aircraft_shadow, tracker_archive_output.ident, tracker_archive_output.famtrackid, tracker_archive_output.aircraft_icao, tracker_archive_output.departure_airport_icao as departure_airport, tracker_archive_output.arrival_airport_icao as arrival_airport, tracker_archive_output.latitude, tracker_archive_output.longitude, tracker_archive_output.altitude, tracker_archive_output.heading, tracker_archive_output.ground_speed, tracker_archive_output.squawk 
454
				    FROM tracker_archive_output 
455
				    LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON tracker_archive_output.aircraft_icao = a.icao 
456
				    WHERE (tracker_archive_output.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') 
457
                        	    '.$filter_query.' GROUP BY tracker_archive_output.famtrackid, tracker_archive_output.ident, tracker_archive_output.aircraft_icao, tracker_archive_output.departure_airport_icao, tracker_archive_output.arrival_airport_icao, tracker_archive_output.latitude, tracker_archive_output.longitude, tracker_archive_output.altitude, tracker_archive_output.heading, tracker_archive_output.ground_speed, tracker_archive_output.squawk, a.aircraft_shadow';
458
459
                } else {
460
                        //$query  = 'SELECT tracker_archive_output.ident, tracker_archive_output.famtrackid, tracker_archive_output.aircraft_icao, tracker_archive_output.departure_airport_icao as departure_airport, tracker_archive_output.arrival_airport_icao as arrival_airport, tracker_archive_output.latitude, tracker_archive_output.longitude, tracker_archive_output.altitude, tracker_archive_output.heading, tracker_archive_output.ground_speed, tracker_archive_output.squawk, a.aircraft_shadow FROM tracker_archive_output INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive_output l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.famtrackid) s on tracker_archive_output.famtrackid = s.famtrackid AND tracker_archive_output.date = s.maxdate '.$filter_query.'INNER JOIN (SELECT * FROM aircraft) a on tracker_archive_output.aircraft_icao = a.icao';
461
                       /*
462
                        $query  = 'SELECT tracker_archive_output.ident, tracker_archive_output.famtrackid, tracker_archive_output.aircraft_icao, tracker_archive_output.departure_airport_icao as departure_airport, tracker_archive_output.arrival_airport_icao as arrival_airport, tracker_archive_output.latitude, tracker_archive_output.longitude, tracker_archive_output.altitude, tracker_archive_output.heading, tracker_archive_output.ground_speed, tracker_archive_output.squawk, a.aircraft_shadow
463
                        	    FROM tracker_archive_output 
464
                        	    INNER JOIN (SELECT * FROM aircraft) a on tracker_archive_output.aircraft_icao = a.icao
465
                        	    WHERE tracker_archive_output.date >= '."'".$begindate."'".' AND tracker_archive_output.date <= '."'".$enddate."'".'
466
                        	    '.$filter_query.' GROUP BY tracker_archive_output.famtrackid, tracker_archive_output.ident, tracker_archive_output.aircraft_icao, tracker_archive_output.departure_airport_icao, tracker_archive_output.arrival_airport_icao, tracker_archive_output.latitude, tracker_archive_output.longitude, tracker_archive_output.altitude, tracker_archive_output.heading, tracker_archive_output.ground_speed, tracker_archive_output.squawk, a.aircraft_shadow';
467
                        */
468
                        $query  = 'SELECT DISTINCT tracker_archive_output.famtrackid, tracker_archive_output.ident, tracker_archive_output.aircraft_icao, tracker_archive_output.departure_airport_icao as departure_airport, tracker_archive_output.arrival_airport_icao as arrival_airport, tracker_archive_output.latitude, tracker_archive_output.longitude, tracker_archive_output.altitude, tracker_archive_output.heading, tracker_archive_output.ground_speed, tracker_archive_output.squawk, a.aircraft_shadow
469
                        	    FROM tracker_archive_output 
470
                        	    INNER JOIN (SELECT * FROM aircraft) a on tracker_archive_output.aircraft_icao = a.icao
471
                        	    WHERE tracker_archive_output.date >= '."'".$begindate."'".' AND tracker_archive_output.date <= '."'".$enddate."'".'
472
                        	    '.$filter_query.' LIMIT 200 OFFSET 0';
473
//                        	    .' GROUP BY spotter_output.famtrackid, spotter_output.ident, spotter_output.aircraft_icao, spotter_output.departure_airport_icao, spotter_output.arrival_airport_icao, spotter_output.latitude, spotter_output.longitude, spotter_output.altitude, spotter_output.heading, spotter_output.ground_speed, spotter_output.squawk, a.aircraft_shadow';
474
                        	    
475
                }
476
                //echo $query;
477
                try {
478
                        $sth = $this->db->prepare($query);
479
                        $sth->execute();
480
                } catch(PDOException $e) {
481
                        echo $e->getMessage();
482
                        die;
483
                }
484
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
485
486
                return $spotter_array;
487
        }
488
489
	 /**
490
        * Gets count Live Tracker data
491
        *
492
        * @return Array the spotter information
493
        *
494
        */
495
        public function getLiveTrackerCount($begindate,$enddate,$filter = array())
496
        {
497
                global $globalDBdriver, $globalLiveInterval;
498
                date_default_timezone_set('UTC');
499
500
                $filter_query = '';
501
                if (isset($filter['source']) && !empty($filter['source'])) {
502
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
503
                }
504
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
505
                        $filter_query .= " INNER JOIN (SELECT famtrackid FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.famtrackid = tracker_archive.famtrackid ";
506
                }
507
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
508
                        $filter_query .= " INNER JOIN (SELECT famtrackid FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.famtrackid = tracker_archive.famtrackid ";
509
                }
510
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
511
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
512
                }
513
514
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
515
                if ($globalDBdriver == 'mysql') {
516
			$query = 'SELECT COUNT(DISTINCT famtrackid) as nb 
517
			FROM tracker_archive l 
518
			WHERE (l.date BETWEEN DATE_SUB('."'".$begindate."'".',INTERVAL '.$globalLiveInterval.' SECOND) AND '."'".$begindate."'".')'.$filter_query;
519
                } else {
520
			$query = 'SELECT COUNT(DISTINCT famtrackid) as nb FROM tracker_archive l WHERE (l.date BETWEEN '."'".$begindate."' - INTERVAL '".$globalLiveInterval." SECONDS' AND "."'".$enddate."'".')'.$filter_query;
521
                }
522
                //echo $query;
523
                try {
524
                        $sth = $this->db->prepare($query);
525
                        $sth->execute();
526
                } catch(PDOException $e) {
527
                        echo $e->getMessage();
528
                        die;
529
                }
530
		$result = $sth->fetch(PDO::FETCH_ASSOC);
531
		$sth->closeCursor();
532
                return $result['nb'];
533
534
        }
535
536
537
538
	// tracker_archive_output
539
	
540
    /**
541
    * Gets all the spotter information
542
    *
543
    * @return Array the spotter information
544
    *
545
    */
546
    public function searchTrackerData($q = '', $registration = '', $aircraft_icao = '', $aircraft_manufacturer = '', $highlights = '', $airline_icao = '', $airline_country = '', $airline_type = '', $airport = '', $airport_country = '', $callsign = '', $departure_airport_route = '', $arrival_airport_route = '', $owner = '',$pilot_id = '',$pilot_name = '',$altitude = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '', $filters=array())
547
    {
548
	global $globalTimezone, $globalDBdriver;
549
	require_once(dirname(__FILE__).'/class.Translation.php');
550
	$Translation = new Translation();
551
	$Tracker = new Tracker($this->db);
552
553
	date_default_timezone_set('UTC');
554
	
555
	$query_values = array();
556
	$additional_query = '';
557
	$limit_query = '';
558
	$filter_query = $this->getFilter($filters);
559
	if ($q != "")
560
	{
561
	    if (!is_string($q))
562
	    {
563
		return false;
564
	    } else {
565
	        
566
		$q_array = explode(" ", $q);
567
		
568
		foreach ($q_array as $q_item){
569
		    $additional_query .= " AND (";
570
		    $additional_query .= "(tracker_archive_output.spotter_id like '%".$q_item."%') OR ";
571
		    $additional_query .= "(tracker_archive_output.aircraft_icao like '%".$q_item."%') OR ";
572
		    $additional_query .= "(tracker_archive_output.aircraft_name like '%".$q_item."%') OR ";
573
		    $additional_query .= "(tracker_archive_output.aircraft_manufacturer like '%".$q_item."%') OR ";
574
		    $additional_query .= "(tracker_archive_output.airline_icao like '%".$q_item."%') OR ";
575
		    $additional_query .= "(tracker_archive_output.airline_name like '%".$q_item."%') OR ";
576
		    $additional_query .= "(tracker_archive_output.airline_country like '%".$q_item."%') OR ";
577
		    $additional_query .= "(tracker_archive_output.departure_airport_icao like '%".$q_item."%') OR ";
578
		    $additional_query .= "(tracker_archive_output.departure_airport_name like '%".$q_item."%') OR ";
579
		    $additional_query .= "(tracker_archive_output.departure_airport_city like '%".$q_item."%') OR ";
580
		    $additional_query .= "(tracker_archive_output.departure_airport_country like '%".$q_item."%') OR ";
581
		    $additional_query .= "(tracker_archive_output.arrival_airport_icao like '%".$q_item."%') OR ";
582
		    $additional_query .= "(tracker_archive_output.arrival_airport_name like '%".$q_item."%') OR ";
583
		    $additional_query .= "(tracker_archive_output.arrival_airport_city like '%".$q_item."%') OR ";
584
		    $additional_query .= "(tracker_archive_output.arrival_airport_country like '%".$q_item."%') OR ";
585
		    $additional_query .= "(tracker_archive_output.registration like '%".$q_item."%') OR ";
586
		    $additional_query .= "(tracker_archive_output.owner_name like '%".$q_item."%') OR ";
587
		    $additional_query .= "(tracker_archive_output.pilot_id like '%".$q_item."%') OR ";
588
		    $additional_query .= "(tracker_archive_output.pilot_name like '%".$q_item."%') OR ";
589
		    $additional_query .= "(tracker_archive_output.ident like '%".$q_item."%') OR ";
590
		    $translate = $Translation->ident2icao($q_item);
591
		    if ($translate != $q_item) $additional_query .= "(tracker_archive_output.ident like '%".$translate."%') OR ";
592
		    $additional_query .= "(tracker_archive_output.highlight like '%".$q_item."%')";
593
		    $additional_query .= ")";
594
		}
595
	    }
596
	}
597
	
598
	if ($registration != "")
599
	{
600
	    $registration = filter_var($registration,FILTER_SANITIZE_STRING);
601
	    if (!is_string($registration))
602
	    {
603
		return false;
604
	    } else {
605
		$additional_query .= " AND (tracker_archive_output.registration = '".$registration."')";
606
	    }
607
	}
608
	
609
	if ($aircraft_icao != "")
610
	{
611
	    $aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING);
612
	    if (!is_string($aircraft_icao))
613
	    {
614
		return false;
615
	    } else {
616
		$additional_query .= " AND (tracker_archive_output.aircraft_icao = '".$aircraft_icao."')";
617
	    }
618
	}
619
	
620
	if ($aircraft_manufacturer != "")
621
	{
622
	    $aircraft_manufacturer = filter_var($aircraft_manufacturer,FILTER_SANITIZE_STRING);
623
	    if (!is_string($aircraft_manufacturer))
624
	    {
625
		return false;
626
	    } else {
627
		$additional_query .= " AND (tracker_archive_output.aircraft_manufacturer = '".$aircraft_manufacturer."')";
628
	    }
629
	}
630
	
631
	if ($highlights == "true")
632
	{
633
	    if (!is_string($highlights))
634
	    {
635
		return false;
636
	    } else {
637
		$additional_query .= " AND (tracker_archive_output.highlight <> '')";
638
	    }
639
	}
640
	
641
	if ($airline_icao != "")
642
	{
643
	    $airline_icao = filter_var($airline_icao,FILTER_SANITIZE_STRING);
644
	    if (!is_string($airline_icao))
645
	    {
646
		return false;
647
	    } else {
648
		$additional_query .= " AND (tracker_archive_output.airline_icao = '".$airline_icao."')";
649
	    }
650
	}
651
	
652
	if ($airline_country != "")
653
	{
654
	    $airline_country = filter_var($airline_country,FILTER_SANITIZE_STRING);
655
	    if (!is_string($airline_country))
656
	    {
657
		return false;
658
	    } else {
659
		$additional_query .= " AND (tracker_archive_output.airline_country = '".$airline_country."')";
660
	    }
661
	}
662
	
663
	if ($airline_type != "")
664
	{
665
	    $airline_type = filter_var($airline_type,FILTER_SANITIZE_STRING);
666
	    if (!is_string($airline_type))
667
	    {
668
		return false;
669
	    } else {
670
		if ($airline_type == "passenger")
671
		{
672
		    $additional_query .= " AND (tracker_archive_output.airline_type = 'passenger')";
673
		}
674
		if ($airline_type == "cargo")
675
		{
676
		    $additional_query .= " AND (tracker_archive_output.airline_type = 'cargo')";
677
		}
678
		if ($airline_type == "military")
679
		{
680
		    $additional_query .= " AND (tracker_archive_output.airline_type = 'military')";
681
		}
682
	    }
683
	}
684
	
685
	if ($airport != "")
686
	{
687
	    $airport = filter_var($airport,FILTER_SANITIZE_STRING);
688
	    if (!is_string($airport))
689
	    {
690
		return false;
691
	    } else {
692
		$additional_query .= " AND ((tracker_archive_output.departure_airport_icao = '".$airport."') OR (tracker_archive_output.arrival_airport_icao = '".$airport."'))";
693
	    }
694
	}
695
	
696
	if ($airport_country != "")
697
	{
698
	    $airport_country = filter_var($airport_country,FILTER_SANITIZE_STRING);
699
	    if (!is_string($airport_country))
700
	    {
701
		return false;
702
	    } else {
703
		$additional_query .= " AND ((tracker_archive_output.departure_airport_country = '".$airport_country."') OR (tracker_archive_output.arrival_airport_country = '".$airport_country."'))";
704
	    }
705
	}
706
    
707
	if ($callsign != "")
708
	{
709
	    $callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
710
	    if (!is_string($callsign))
711
	    {
712
		return false;
713
	    } else {
714
		$translate = $Translation->ident2icao($callsign);
715
		if ($translate != $callsign) {
716
			$additional_query .= " AND (tracker_archive_output.ident = :callsign OR tracker_archive_output.ident = :translate)";
717
			$query_values = array_merge($query_values,array(':callsign' => $callsign,':translate' => $translate));
718
		} else {
719
			$additional_query .= " AND (tracker_archive_output.ident = '".$callsign."')";
720
		}
721
	    }
722
	}
723
724
	if ($owner != "")
725
	{
726
	    $owner = filter_var($owner,FILTER_SANITIZE_STRING);
727
	    if (!is_string($owner))
728
	    {
729
		return false;
730
	    } else {
731
		$additional_query .= " AND (tracker_archive_output.owner_name = '".$owner."')";
732
	    }
733
	}
734
735
	if ($pilot_name != "")
736
	{
737
	    $pilot_name = filter_var($pilot_name,FILTER_SANITIZE_STRING);
738
	    if (!is_string($pilot_name))
739
	    {
740
		return false;
741
	    } else {
742
		$additional_query .= " AND (tracker_archive_output.pilot_name = '".$pilot_name."')";
743
	    }
744
	}
745
	
746
	if ($pilot_id != "")
747
	{
748
	    $pilot_id = filter_var($pilot_id,FILTER_SANITIZE_NUMBER_INT);
749
	    if (!is_string($pilot_id))
750
	    {
751
		return false;
752
	    } else {
753
		$additional_query .= " AND (tracker_archive_output.pilot_id = '".$pilot_id."')";
754
	    }
755
	}
756
	
757
	if ($departure_airport_route != "")
758
	{
759
	    $departure_airport_route = filter_var($departure_airport_route,FILTER_SANITIZE_STRING);
760
	    if (!is_string($departure_airport_route))
761
	    {
762
		return false;
763
	    } else {
764
		$additional_query .= " AND (tracker_archive_output.departure_airport_icao = '".$departure_airport_route."')";
765
	    }
766
	}
767
	
768
	if ($arrival_airport_route != "")
769
	{
770
	    $arrival_airport_route = filter_var($arrival_airport_route,FILTER_SANITIZE_STRING);
771
	    if (!is_string($arrival_airport_route))
772
	    {
773
		return false;
774
	    } else {
775
		$additional_query .= " AND (tracker_archive_output.arrival_airport_icao = '".$arrival_airport_route."')";
776
	    }
777
	}
778
	
779
	if ($altitude != "")
780
	{
781
	    $altitude_array = explode(",", $altitude);
782
	    
783
	    $altitude_array[0] = filter_var($altitude_array[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
784
	    $altitude_array[1] = filter_var($altitude_array[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
785
	    
786
787
	    if ($altitude_array[1] != "")
788
	    {                
789
		$altitude_array[0] = substr($altitude_array[0], 0, -2);
790
		$altitude_array[1] = substr($altitude_array[1], 0, -2);
791
		$additional_query .= " AND altitude BETWEEN '".$altitude_array[0]."' AND '".$altitude_array[1]."' ";
792
	    } else {
793
		$altitude_array[0] = substr($altitude_array[0], 0, -2);
794
		$additional_query .= " AND altitude <= '".$altitude_array[0]."' ";
795
	    }
796
	}
797
	
798
	if ($date_posted != "")
799
	{
800
	    $date_array = explode(",", $date_posted);
801
	    
802
	    $date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING);
803
	    $date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING);
804
	    
805
	    if ($globalTimezone != '') {
806
		date_default_timezone_set($globalTimezone);
807
		$datetime = new DateTime();
808
		$offset = $datetime->format('P');
809
	    } else $offset = '+00:00';
810
811
812
	    if ($date_array[1] != "")
813
	    {                
814
		$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
815
		$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1]));
816
		if ($globalDBdriver == 'mysql') {
817
			$additional_query .= " AND TIMESTAMP(CONVERT_TZ(tracker_archive_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' AND TIMESTAMP(CONVERT_TZ(tracker_archive_output.date,'+00:00', '".$offset."')) <= '".$date_array[1]."' ";
818
		} else {
819
			$additional_query .= " AND tracker_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." >= CAST('".$date_array[0]."' AS TIMESTAMP) AND tracker_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." <= CAST('".$date_array[1]."' AS TIMESTAMP) ";
820
		}
821
	    } else {
822
		$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
823
                if ($globalDBdriver == 'mysql') {
824
			$additional_query .= " AND TIMESTAMP(CONVERT_TZ(tracker_archive_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' ";
825
		} else {
826
			$additional_query .= " AND tracker_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." >= CAST('".$date_array[0]."' AS TIMESTAMP) ";
827
		}
828
	    }
829
	}
830
	
831
	if ($limit != "")
832
	{
833
	    $limit_array = explode(",", $limit);
834
	    
835
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
836
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
837
	    
838
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
839
	    {
840
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
841
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
842
	    }
843
	}
844
	
845
846
	if ($origLat != "" && $origLon != "" && $dist != "") {
847
		$dist = number_format($dist*0.621371,2,'.','');
848
		$query="SELECT tracker_archive_output.*, 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - ABS(CAST(tracker_archive.latitude as double precision)))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(ABS(CAST(tracker_archive.latitude as double precision))*pi()/180)*POWER(SIN(($origLon-CAST(tracker_archive.longitude as double precision))*pi()/180/2),2))) as distance 
849
                          FROM tracker_archive_output, tracker_archive WHERE spotter_output_archive.famtrackid = tracker_archive.famtrackid AND spotter_output.ident <> '' ".$additional_query."AND CAST(tracker_archive.longitude as double precision) between ($origLon-$dist/ABS(cos(radians($origLat))*69)) and ($origLon+$dist/ABS(cos(radians($origLat))*69)) and CAST(tracker_archive.latitude as double precision) between ($origLat-($dist/69)) and ($origLat+($dist/69)) 
850
                          AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - ABS(CAST(tracker_archive.latitude as double precision)))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(ABS(CAST(tracker_archive.latitude as double precision))*pi()/180)*POWER(SIN(($origLon-CAST(tracker_archive.longitude as double precision))*pi()/180/2),2)))) < $dist".$filter_query." ORDER BY distance";
851
	} else {
852
		if ($sort != "")
853
		{
854
			$search_orderby_array = $Tracker->getOrderBy();
855
			$orderby_query = $search_orderby_array[$sort]['sql'];
856
		} else {
857
			$orderby_query = " ORDER BY tracker_archive_output.date DESC";
858
		}
859
	
860
		if ($includegeodata == "true")
861
		{
862
			$additional_query .= " AND (tracker_archive_output.waypoints <> '')";
863
		}
864
865
		$query  = "SELECT tracker_archive_output.* FROM tracker_archive_output 
866
		    WHERE tracker_archive_output.ident <> '' 
867
		    ".$additional_query."
868
		    ".$filter_query.$orderby_query;
869
	}
870
	$spotter_array = $Tracker->getDataFromDB($query, $query_values,$limit_query);
871
872
	return $spotter_array;
873
    }
874
875
    public function deleteTrackerArchiveData()
876
    {
877
		global $globalArchiveKeepMonths, $globalDBdriver;
878
                date_default_timezone_set('UTC');
879
                if ($globalDBdriver == 'mysql') {
880
			$query = 'DELETE FROM tracker_archive_output WHERE tracker_archive_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveKeepMonths.' MONTH)';
881
		} else {
882
			$query = "DELETE FROM tracker_archive_output WHERE tracker_archive_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveKeepMonths." MONTH'";
883
		}
884
                try {
885
                        $sth = $this->db->prepare($query);
886
                        $sth->execute();
887
                } catch(PDOException $e) {
888
                        return "error";
889
                }
890
	}
891
892
    /**
893
    * Gets all the spotter information based on the callsign
894
    *
895
    * @return Array the spotter information
896
    *
897
    */
898
    public function getTrackerDataByIdent($ident = '', $limit = '', $sort = '')
899
    {
900
	$global_query = "SELECT tracker_archive_output.* FROM tracker_archive_output";
901
	
902
	date_default_timezone_set('UTC');
903
	$Tracker = new Tracker($this->db);
904
	
905
	$query_values = array();
906
	$limit_query = '';
907
	$additional_query = '';
908
	
909
	if ($ident != "")
910
	{
911
	    if (!is_string($ident))
912
	    {
913
		return false;
914
	    } else {
915
		$additional_query = " AND (tracker_archive_output.ident = :ident)";
916
		$query_values = array(':ident' => $ident);
917
	    }
918
	}
919
	
920
	if ($limit != "")
921
	{
922
	    $limit_array = explode(",", $limit);
923
	    
924
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
925
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
926
	    
927
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
928
	    {
929
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
930
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
931
	    }
932
	}
933
934
	if ($sort != "")
935
	{
936
	    $search_orderby_array = $Tracker->getOrderBy();
937
	    $orderby_query = $search_orderby_array[$sort]['sql'];
938
	} else {
939
	    $orderby_query = " ORDER BY tracker_archive_output.date DESC";
940
	}
941
942
	$query = $global_query." WHERE tracker_archive_output.ident <> '' ".$additional_query." ".$orderby_query;
943
944
	$spotter_array = $Tracker->getDataFromDB($query, $query_values, $limit_query);
945
946
	return $spotter_array;
947
    }
948
949
950
    /**
951
    * Gets all the spotter information based on the owner
952
    *
953
    * @return Array the spotter information
954
    *
955
    */
956
    public function getTrackerDataByOwner($owner = '', $limit = '', $sort = '', $filter = array())
957
    {
958
	$global_query = "SELECT tracker_archive_output.* FROM tracker_archive_output";
959
	
960
	date_default_timezone_set('UTC');
961
	$Tracker = new Tracker($this->db);
962
	
963
	$query_values = array();
964
	$limit_query = '';
965
	$additional_query = '';
966
	$filter_query = $this->getFilter($filter,true,true);
967
	
968
	if ($owner != "")
969
	{
970
	    if (!is_string($owner))
971
	    {
972
		return false;
973
	    } else {
974
		$additional_query = " AND (tracker_archive_output.owner_name = :owner)";
975
		$query_values = array(':owner' => $owner);
976
	    }
977
	}
978
	
979
	if ($limit != "")
980
	{
981
	    $limit_array = explode(",", $limit);
982
	    
983
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
984
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
985
	    
986
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
987
	    {
988
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
989
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
990
	    }
991
	}
992
993
	if ($sort != "")
994
	{
995
	    $search_orderby_array = $Tracker->getOrderBy();
996
	    $orderby_query = $search_orderby_array[$sort]['sql'];
997
	} else {
998
	    $orderby_query = " ORDER BY tracker_archive_output.date DESC";
999
	}
1000
1001
	$query = $global_query.$filter_query." tracker_archive_output.owner_name <> '' ".$additional_query." ".$orderby_query;
1002
1003
	$spotter_array = $Tracker->getDataFromDB($query, $query_values, $limit_query);
1004
1005
	return $spotter_array;
1006
    }
1007
1008
    /**
1009
    * Gets all the spotter information based on the pilot
1010
    *
1011
    * @return Array the spotter information
1012
    *
1013
    */
1014
    public function getTrackerDataByPilot($pilot = '', $limit = '', $sort = '', $filter = array())
1015
    {
1016
	$global_query = "SELECT tracker_archive_output.* FROM tracker_archive_output";
1017
	
1018
	date_default_timezone_set('UTC');
1019
	$Tracker = new Tracker($this->db);
1020
	
1021
	$query_values = array();
1022
	$limit_query = '';
1023
	$additional_query = '';
1024
	$filter_query = $this->getFilter($filter,true,true);
1025
	
1026
	if ($pilot != "")
1027
	{
1028
		$additional_query = " AND (tracker_archive_output.pilot_id = :pilot OR tracker_archive_output.pilot_name = :pilot)";
1029
		$query_values = array(':pilot' => $pilot);
1030
	}
1031
	
1032
	if ($limit != "")
1033
	{
1034
	    $limit_array = explode(",", $limit);
1035
	    
1036
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
1037
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
1038
	    
1039
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
1040
	    {
1041
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
1042
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
1043
	    }
1044
	}
1045
1046
	if ($sort != "")
1047
	{
1048
	    $search_orderby_array = $Tracker->getOrderBy();
1049
	    $orderby_query = $search_orderby_array[$sort]['sql'];
1050
	} else {
1051
	    $orderby_query = " ORDER BY tracker_archive_output.date DESC";
1052
	}
1053
1054
	$query = $global_query.$filter_query." tracker_archive_output.pilot_name <> '' ".$additional_query." ".$orderby_query;
1055
1056
	$spotter_array = $Tracker->getDataFromDB($query, $query_values, $limit_query);
1057
1058
	return $spotter_array;
1059
    }
1060
1061
    /**
1062
    * Gets all number of flight over countries
1063
    *
1064
    * @return Array the airline country list
1065
    *
1066
    */
1067
    public function countAllFlightOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '')
1068
    {
1069
	global $globalDBdriver;
1070
	/*
1071
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb 
1072
		    FROM countries c, tracker_archive s
1073
		    WHERE Within(GeomFromText(CONCAT('POINT(',s.longitude,' ',s.latitude,')')), ogc_geom) ";
1074
	*/
1075
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb
1076
		    FROM countries c, tracker_archive s
1077
		    WHERE c.iso2 = s.over_country ";
1078
                if ($olderthanmonths > 0) {
1079
            		if ($globalDBdriver == 'mysql') {
1080
				$query .= 'AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1081
			} else {
1082
				$query .= "AND date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1083
			}
1084
		}
1085
                if ($sincedate != '') $query .= "AND date > '".$sincedate."' ";
1086
	$query .= "GROUP BY c.name, c.iso3, c.iso2 ORDER BY nb DESC";
1087
	if ($limit) $query .= " LIMIT 0,10";
1088
      
1089
	
1090
	$sth = $this->db->prepare($query);
1091
	$sth->execute();
1092
 
1093
	$flight_array = array();
1094
	$temp_array = array();
1095
        
1096
	while($row = $sth->fetch(PDO::FETCH_ASSOC))
1097
	{
1098
	    $temp_array['flight_count'] = $row['nb'];
1099
	    $temp_array['flight_country'] = $row['name'];
1100
	    $temp_array['flight_country_iso3'] = $row['iso3'];
1101
	    $temp_array['flight_country_iso2'] = $row['iso2'];
1102
	    $flight_array[] = $temp_array;
1103
	}
1104
	return $flight_array;
1105
    }
1106
1107
    /**
1108
    * Gets all number of flight over countries
1109
    *
1110
    * @return Array the airline country list
1111
    *
1112
    */
1113
    public function countAllFlightOverCountriesByAirlines($limit = true,$olderthanmonths = 0,$sincedate = '')
1114
    {
1115
	global $globalDBdriver;
1116
	/*
1117
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb 
1118
		    FROM countries c, tracker_archive s
1119
		    WHERE Within(GeomFromText(CONCAT('POINT(',s.longitude,' ',s.latitude,')')), ogc_geom) ";
1120
	*/
1121
	$query = "SELECT o.airline_icao,c.name, c.iso3, c.iso2, count(c.name) as nb
1122
		    FROM countries c, tracker_archive s, spotter_output o
1123
		    WHERE c.iso2 = s.over_country AND o.airline_icao <> '' AND o.famtrackid = s.famtrackid ";
1124
                if ($olderthanmonths > 0) {
1125
            		if ($globalDBdriver == 'mysql') {
1126
				$query .= 'AND s.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1127
			} else {
1128
				$query .= "AND s.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1129
			}
1130
		}
1131
                if ($sincedate != '') $query .= "AND s.date > '".$sincedate."' ";
1132
	$query .= "GROUP BY o.airline_icao,c.name, c.iso3, c.iso2 ORDER BY nb DESC";
1133
	if ($limit) $query .= " LIMIT 0,10";
1134
      
1135
	
1136
	$sth = $this->db->prepare($query);
1137
	$sth->execute();
1138
 
1139
	$flight_array = array();
1140
	$temp_array = array();
1141
        
1142
	while($row = $sth->fetch(PDO::FETCH_ASSOC))
1143
	{
1144
	    $temp_array['airline_icao'] = $row['airline_icao'];
1145
	    $temp_array['flight_count'] = $row['nb'];
1146
	    $temp_array['flight_country'] = $row['name'];
1147
	    $temp_array['flight_country_iso3'] = $row['iso3'];
1148
	    $temp_array['flight_country_iso2'] = $row['iso2'];
1149
	    $flight_array[] = $temp_array;
1150
	}
1151
	return $flight_array;
1152
    }
1153
1154
    /**
1155
    * Gets last spotter information based on a particular callsign
1156
    *
1157
    * @return Array the spotter information
1158
    *
1159
    */
1160
    public function getDateArchiveTrackerDataById($id,$date)
1161
    {
1162
	$Tracker = new Tracker($this->db);
1163
	date_default_timezone_set('UTC');
1164
	$id = filter_var($id, FILTER_SANITIZE_STRING);
1165
	$query  = 'SELECT tracker_archive.* FROM tracker_archive INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE l.famtrackid = :id AND l.date <= :date GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate ORDER BY tracker_archive.date DESC';
1166
	$date = date('c',$date);
1167
	$spotter_array = $Tracker->getDataFromDB($query,array(':id' => $id,':date' => $date));
1168
	return $spotter_array;
1169
    }
1170
1171
    /**
1172
    * Gets all the spotter information based on a particular callsign
1173
    *
1174
    * @return Array the spotter information
1175
    *
1176
    */
1177
    public function getDateArchiveTrackerDataByIdent($ident,$date)
1178
    {
1179
	$Tracker = new Tracker($this->db);
1180
	date_default_timezone_set('UTC');
1181
	$ident = filter_var($ident, FILTER_SANITIZE_STRING);
1182
	$query  = 'SELECT tracker_archive.* FROM tracker_archive INNER JOIN (SELECT l.famtrackid, max(l.date) as maxdate FROM tracker_archive l WHERE l.ident = :ident AND l.date <= :date GROUP BY l.famtrackid) s on tracker_archive.famtrackid = s.famtrackid AND tracker_archive.date = s.maxdate ORDER BY tracker_archive.date DESC';
1183
	$date = date('c',$date);
1184
	$spotter_array = $Tracker->getDataFromDB($query,array(':ident' => $ident,':date' => $date));
1185
	return $spotter_array;
1186
    }
1187
1188
    /**
1189
    * Gets all the spotter information based on the airport
1190
    *
1191
    * @return Array the spotter information
1192
    *
1193
    */
1194
    public function getTrackerDataByAirport($airport = '', $limit = '', $sort = '',$filters = array())
1195
    {
1196
	global $global_query;
1197
	$Tracker = new Tracker();
1198
	date_default_timezone_set('UTC');
1199
	$query_values = array();
1200
	$limit_query = '';
1201
	$additional_query = '';
1202
	$filter_query = $this->getFilter($filters,true,true);
1203
	
1204
	if ($airport != "")
1205
	{
1206
	    if (!is_string($airport))
1207
	    {
1208
		return false;
1209
	    } else {
1210
		$additional_query .= " AND ((tracker_archive_output.departure_airport_icao = :airport) OR (tracker_archive_output.arrival_airport_icao = :airport))";
1211
		$query_values = array(':airport' => $airport);
1212
	    }
1213
	}
1214
	
1215
	if ($limit != "")
1216
	{
1217
	    $limit_array = explode(",", $limit);
1218
	    
1219
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
1220
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
1221
	    
1222
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
1223
	    {
1224
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
1225
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
1226
	    }
1227
	}
1228
	
1229
	if ($sort != "")
1230
	{
1231
	    $search_orderby_array = $Tracker->getOrderBy();
1232
	    $orderby_query = $search_orderby_array[$sort]['sql'];
1233
	} else {
1234
	    $orderby_query = " ORDER BY tracker_archive_output.date DESC";
1235
	}
1236
1237
	$query = $global_query.$filter_query." tracker_archive_output.ident <> '' ".$additional_query." AND ((tracker_archive_output.departure_airport_icao <> 'NA') AND (tracker_archive_output.arrival_airport_icao <> 'NA')) ".$orderby_query;
1238
1239
	$spotter_array = $Tracker->getDataFromDB($query, $query_values, $limit_query);
1240
1241
	return $spotter_array;
1242
    }
1243
}
1244
?>