Completed
Push — master ( aa8319...e96b22 )
by Yannick
07:18
created

TrackerArchive::countAllFlightOverCountries()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 24
nop 3
dl 0
loc 39
rs 8.439
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 = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $putinarchive is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $noarchive is not used and could be removed.

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

Loading history...
86
		/*
87
		require_once(dirname(__FILE__).'/class.Tracker.php');
88
		if ($over_country == '') {
89
			$Tracker = new Tracker($this->db);
90
			$data_country = $Tracker->getCountryFromLatitudeLongitude($latitude,$longitude);
91
			if (!empty($data_country)) $country = $data_country['iso2'];
92
			else $country = '';
93
		} else $country = $over_country;
94
		*/
95
		$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...
96
		// Route is not added in tracker_archive
97
		$query  .= 'INSERT INTO tracker_archive (famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, over_country, comment, type) 
0 ignored issues
show
Bug introduced by
The variable $query does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

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