Completed
Push — master ( 7b298e...890247 )
by Yannick
43:19 queued 23s
created

Tracker::countOverallTrackerTypes()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 18
nop 3
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/class.Scheduler.php');
3
require_once(dirname(__FILE__).'/class.ACARS.php');
4
require_once(dirname(__FILE__).'/class.Image.php');
5
$global_query = "SELECT tracker_output.* FROM tracker_output";
6
7
class Tracker{
8
	public $db;
9
	
10
	public function __construct($dbc = null) {
11
		$Connection = new Connection($dbc);
12
		$this->db = $Connection->db();
13
	}
14
15
	/**
16
	* Get SQL query part for filter used
17
	* @param Array $filter the filter
18
	* @return Array the SQL part
19
	*/
20
	
21
	public function getFilter($filter = array(),$where = false,$and = false) {
22
		global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver;
23
		$filters = array();
24
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
25
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
26
				$filters = $globalStatsFilters[$globalFilterName];
27
			} else {
28
				$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]);
29
			}
30
		}
31
		if (isset($filter[0]['source'])) {
32
			$filters = array_merge($filters,$filter);
33
		}
34
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
35
		$filter_query_join = '';
36
		$filter_query_where = '';
37
		foreach($filters as $flt) {
38
			if (isset($flt['idents']) && !empty($flt['idents'])) {
39
				if (isset($flt['source'])) {
40
					$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_output WHERE tracker_output.ident IN ('".implode("','",$flt['idents'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) spfi ON spfi.famtrackid = tracker_output.famtrackid";
41
				} else {
42
					$filter_query_join .= " INNER JOIN (SELECT famtrackid FROM tracker_output WHERE tracker_output.ident IN ('".implode("','",$flt['idents'])."')) spfi ON spfi.famtrackid = tracker_output.famtrackid";
43
				}
44
			}
45
		}
46
		if (isset($filter['source']) && !empty($filter['source'])) {
47
			$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
48
		}
49
		if (isset($filter['ident']) && !empty($filter['ident'])) {
50
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
51
		}
52
		if (isset($filter['year']) && $filter['year'] != '') {
53
			if ($globalDBdriver == 'mysql') {
54
				$filter_query_where .= " AND YEAR(tracker_output.date) = '".$filter['year']."'";
55
			} else {
56
				$filter_query_where .= " AND EXTRACT(YEAR FROM tracker_output.date) = '".$filter['year']."'";
57
			}
58
		}
59
		if (isset($filter['month']) && $filter['month'] != '') {
60
			if ($globalDBdriver == 'mysql') {
61
				$filter_query_where .= " AND MONTH(tracker_output.date) = '".$filter['month']."'";
62
			} else {
63
				$filter_query_where .= " AND EXTRACT(MONTH FROM tracker_output.date) = '".$filter['month']."'";
64
			}
65
		}
66
		if (isset($filter['day']) && $filter['day'] != '') {
67
			if ($globalDBdriver == 'mysql') {
68
				$filter_query_where .= " AND DAY(tracker_output.date) = '".$filter['day']."'";
69
			} else {
70
				$filter_query_where .= " AND EXTRACT(DAY FROM tracker_output.date) = '".$filter['day']."'";
71
			}
72
		}
73
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
74
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
75
		if ($filter_query_where != '') {
76
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
77
		}
78
		$filter_query = $filter_query_join.$filter_query_where;
79
		return $filter_query;
80
	}
81
82
	/**
83
	* Executes the SQL statements to get the spotter information
84
	*
85
	* @param String $query the SQL query
86
	* @param Array $params parameter of the query
87
	* @param String $limitQuery the limit query
88
	* @return Array the spotter information
89
	*
90
	*/
91
	public function getDataFromDB($query, $params = array(), $limitQuery = '',$schedules = false)
0 ignored issues
show
Unused Code introduced by
The parameter $schedules 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...
92
	{
93
		date_default_timezone_set('UTC');
94
		if (!is_string($query))
95
		{
96
			return false;
97
		}
98
		
99
		if ($limitQuery != "")
100
		{
101
			if (!is_string($limitQuery))
102
			{
103
				return false;
104
			}
105
		}
106
107
		try {
108
			$sth = $this->db->prepare($query.$limitQuery);
109
			$sth->execute($params);
110
		} catch (PDOException $e) {
111
			printf("Invalid query : %s\nWhole query: %s\n",$e->getMessage(), $query.$limitQuery);
112
			exit();
113
		}
114
		
115
		$num_rows = 0;
116
		$spotter_array = array();
117
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
118
		{
119
			$num_rows++;
120
			$temp_array = array();
121
			if (isset($row['tracker_live_id'])) {
122
				$temp_array['tracker_id'] = $this->getTrackerIDBasedOnFamTrackID($row['famtrackid']);
123
			/*
124
			} elseif (isset($row['spotter_archive_id'])) {
125
				$temp_array['spotter_id'] = $row['spotter_archive_id'];
126
			} elseif (isset($row['spotter_archive_output_id'])) {
127
				$temp_array['spotter_id'] = $row['spotter_archive_output_id'];
128
			*/} 
129
			elseif (isset($row['trackerid'])) {
130
				$temp_array['trackerid'] = $row['trackerid'];
131
			} else {
132
				$temp_array['trackerid'] = '';
133
			}
134
			if (isset($row['famtrackid'])) $temp_array['famtrackid'] = $row['famtrackid'];
135
			if (isset($row['type'])) $temp_array['type'] = $row['type'];
136
			if (isset($row['comment'])) $temp_array['comment'] = $row['comment'];
137
			$temp_array['ident'] = $row['ident'];
138
			if (isset($row['latitude'])) $temp_array['latitude'] = $row['latitude'];
139
			if (isset($row['longitude'])) $temp_array['longitude'] = $row['longitude'];
140
			if (isset($row['format_source'])) $temp_array['format_source'] = $row['format_source'];
141
			if (isset($row['altitude'])) $temp_array['altitude'] = $row['altitude'];
142
			if (isset($row['heading'])) {
143
				$temp_array['heading'] = $row['heading'];
144
				$heading_direction = $this->parseDirection($row['heading']);
145
				if (isset($heading_direction[0]['direction_fullname'])) $temp_array['heading_name'] = $heading_direction[0]['direction_fullname'];
146
			}
147
			if (isset($row['ground_speed'])) $temp_array['ground_speed'] = $row['ground_speed'];
148
			
149
			if (isset($row['date'])) {
150
				$dateArray = $this->parseDateString($row['date']);
151
				if ($dateArray['seconds'] < 10)
152
				{
153
					$temp_array['date'] = "a few seconds ago";
154
				} elseif ($dateArray['seconds'] >= 5 && $dateArray['seconds'] < 30)
155
				{
156
					$temp_array['date'] = "half a minute ago";
157
				} elseif ($dateArray['seconds'] >= 30 && $dateArray['seconds'] < 60)
158
				{
159
					$temp_array['date'] = "about a minute ago";
160
				} elseif ($dateArray['minutes'] < 5)
161
				{
162
					$temp_array['date'] = "a few minutes ago";
163
				} elseif ($dateArray['minutes'] >= 5 && $dateArray['minutes'] < 60)
164
				{
165
					$temp_array['date'] = "about ".$dateArray['minutes']." minutes ago";
166
				} elseif ($dateArray['hours'] < 2)
167
				{
168
					$temp_array['date'] = "about an hour ago";
169
				} elseif ($dateArray['hours'] >= 2 && $dateArray['hours'] < 24)
170
				{
171
					$temp_array['date'] = "about ".$dateArray['hours']." hours ago";
172
				} else {
173
					$temp_array['date'] = date("M j Y, g:i a",strtotime($row['date']." UTC"));
174
				}
175
				$temp_array['date_minutes_past'] = $dateArray['minutes'];
176
				$temp_array['date_iso_8601'] = date("c",strtotime($row['date']." UTC"));
177
				$temp_array['date_rfc_2822'] = date("r",strtotime($row['date']." UTC"));
178
				$temp_array['date_unix'] = strtotime($row['date']." UTC");
179
				if (isset($row['last_seen']) && $row['last_seen'] != '') {
180
					if (strtotime($row['last_seen']) > strtotime($row['date'])) {
181
						$temp_array['duration'] = strtotime($row['last_seen']) - strtotime($row['date']);
182
						$temp_array['last_seen_date_iso_8601'] = date("c",strtotime($row['last_seen']." UTC"));
183
						$temp_array['last_seen_date_rfc_2822'] = date("r",strtotime($row['last_seen']." UTC"));
184
						$temp_array['last_seen_date_unix'] = strtotime($row['last_seen']." UTC");
185
					}
186
				}
187
			}
188
			
189
			$fromsource = NULL;
0 ignored issues
show
Unused Code introduced by
$fromsource 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...
190
			if (isset($row['source_name']) && $row['source_name'] != '') $temp_array['source_name'] = $row['source_name'];
191
			if (isset($row['over_country']) && $row['over_country'] != '') $temp_array['over_country'] = $row['over_country'];
192
			if (isset($row['distance']) && $row['distance'] != '') $temp_array['distance'] = $row['distance'];
193
			$temp_array['query_number_rows'] = $num_rows;
194
			$spotter_array[] = $temp_array;
195
		}
196
		if ($num_rows == 0) return array();
197
		$spotter_array[0]['query_number_rows'] = $num_rows;
198
		return $spotter_array;
199
	}	
200
	
201
	
202
	/**
203
	* Gets all the spotter information based on the latest data entry
204
	*
205
	* @return Array the spotter information
206
	*
207
	*/
208
	public function getLatestTrackerData($limit = '', $sort = '', $filter = array())
209
	{
210
		global $global_query;
211
		
212
		date_default_timezone_set('UTC');
213
214
		$filter_query = $this->getFilter($filter);
215
		
216
		if ($limit != "")
217
		{
218
			$limit_array = explode(",", $limit);
219
			
220
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
221
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
222
			
223
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
224
			{
225
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
226
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
227
			} else $limit_query = "";
228
		} else $limit_query = "";
229
		
230
		if ($sort != "")
231
		{
232
			$search_orderby_array = $this->getOrderBy();
233
			$orderby_query = $search_orderby_array[$sort]['sql'];
234
		} else {
235
			$orderby_query = " ORDER BY tracker_output.date DESC";
236
		}
237
238
		$query  = $global_query.$filter_query." ".$orderby_query;
239
240
		$spotter_array = $this->getDataFromDB($query, array(),$limit_query,true);
241
242
		return $spotter_array;
243
	}
244
    
245
	/*
246
	* Gets all the spotter information based on the spotter id
247
	*
248
	* @return Array the spotter information
249
	*
250
	*/
251
	public function getTrackerDataByID($id = '')
252
	{
253
		global $global_query;
254
		
255
		date_default_timezone_set('UTC');
256
		if ($id == '') return array();
257
		$additional_query = "tracker_output.famtrackid = :id";
258
		$query_values = array(':id' => $id);
259
		$query  = $global_query." WHERE ".$additional_query." ";
260
		$spotter_array = $this->getDataFromDB($query,$query_values);
261
		return $spotter_array;
262
	}
263
264
	/**
265
	* Gets all the spotter information based on the callsign
266
	*
267
	* @return Array the spotter information
268
	*
269
	*/
270
	public function getTrackerDataByIdent($ident = '', $limit = '', $sort = '', $filter = array())
271
	{
272
		global $global_query;
273
		
274
		date_default_timezone_set('UTC');
275
		
276
		$query_values = array();
277
		$limit_query = '';
278
		$additional_query = '';
279
		$filter_query = $this->getFilter($filter,true,true);
280
		if ($ident != "")
281
		{
282
			if (!is_string($ident))
283
			{
284
				return false;
285
			} else {
286
				$additional_query = " AND (tracker_output.ident = :ident)";
287
				$query_values = array(':ident' => $ident);
288
			}
289
		}
290
		
291
		if ($limit != "")
292
		{
293
			$limit_array = explode(",", $limit);
294
			
295
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
296
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
297
			
298
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
299
			{
300
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
301
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
302
			}
303
		}
304
305
		if ($sort != "")
306
		{
307
			$search_orderby_array = $this->getOrderBy();
308
			$orderby_query = $search_orderby_array[$sort]['sql'];
309
		} else {
310
			$orderby_query = " ORDER BY tracker_output.date DESC";
311
		}
312
313
		$query = $global_query.$filter_query." tracker_output.ident <> '' ".$additional_query." ".$orderby_query;
314
315
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
316
317
		return $spotter_array;
318
	}
319
	
320
	public function getTrackerDataByDate($date = '', $limit = '', $sort = '',$filter = array())
321
	{
322
		global $global_query, $globalTimezone, $globalDBdriver;
323
		
324
		$query_values = array();
325
		$limit_query = '';
326
		$additional_query = '';
327
328
		$filter_query = $this->getFilter($filter,true,true);
329
		
330
		if ($date != "")
331
		{
332
			if ($globalTimezone != '') {
333
				date_default_timezone_set($globalTimezone);
334
				$datetime = new DateTime($date);
335
				$offset = $datetime->format('P');
336
			} else {
337
				date_default_timezone_set('UTC');
338
				$datetime = new DateTime($date);
339
				$offset = '+00:00';
340
			}
341
			if ($globalDBdriver == 'mysql') {
342
				$additional_query = " AND DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) = :date ";
343
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':offset' => $offset);
344
			} elseif ($globalDBdriver == 'pgsql') {
345
				$additional_query = " AND to_char(tracker_output.date AT TIME ZONE :timezone,'YYYY-mm-dd') = :date ";
346
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':timezone' => $globalTimezone);
347
			}
348
		}
349
		
350
		if ($limit != "")
351
		{
352
			$limit_array = explode(",", $limit);
353
			
354
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
355
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
356
			
357
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
358
			{
359
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
360
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
361
			}
362
		}
363
364
		if ($sort != "")
365
		{
366
			$search_orderby_array = $this->getOrderBy();
367
			$orderby_query = $search_orderby_array[$sort]['sql'];
368
		} else {
369
			$orderby_query = " ORDER BY tracker_output.date DESC";
370
		}
371
372
		$query = $global_query.$filter_query." tracker_output.ident <> '' ".$additional_query.$orderby_query;
373
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
374
		return $spotter_array;
375
	}
376
377
378
379
	/**
380
	* Gets all source name
381
	*
382
	* @param String type format of source
383
	* @return Array list of source name
384
	*
385
	*/
386
	public function getAllSourceName($type = '',$filters = array())
387
	{
388
		$filter_query = $this->getFilter($filters,true,true);
389
		$query_values = array();
390
		$query  = "SELECT DISTINCT tracker_output.source_name 
391
				FROM tracker_output".$filter_query." tracker_output.source_name <> ''";
392
		if ($type != '') {
393
			$query_values = array(':type' => $type);
394
			$query .= " AND format_source = :type";
395
		}
396
		$query .= " ORDER BY tracker_output.source_name ASC";
397
398
		$sth = $this->db->prepare($query);
399
		if (!empty($query_values)) $sth->execute($query_values);
400
		else $sth->execute();
401
402
		$source_array = array();
403
		$temp_array = array();
404
		
405
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
406
		{
407
			$temp_array['source_name'] = $row['source_name'];
408
			$source_array[] = $temp_array;
409
		}
410
		return $source_array;
411
	}
412
413
414
	/**
415
	* Gets a list of all idents/callsigns
416
	*
417
	* @return Array list of ident/callsign names
418
	*
419
	*/
420
	public function getAllIdents($filters = array())
421
	{
422
		$filter_query = $this->getFilter($filters,true,true);
423
		$query  = "SELECT DISTINCT tracker_output.ident
424
								FROM tracker_output".$filter_query." tracker_output.ident <> '' 
425
								ORDER BY tracker_output.date ASC LIMIT 700 OFFSET 0";
426
427
		$sth = $this->db->prepare($query);
428
		$sth->execute();
429
    
430
		$ident_array = array();
431
		$temp_array = array();
432
		
433
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
434
		{
435
			$temp_array['ident'] = $row['ident'];
436
			$ident_array[] = $temp_array;
437
		}
438
439
		return $ident_array;
440
	}
441
442
	/*
443
	* Gets a list of all dates
444
	*
445
	* @return Array list of date names
446
	*
447
	*/
448
	public function getAllDates()
449
	{
450
		global $globalTimezone, $globalDBdriver;
451
		if ($globalTimezone != '') {
452
			date_default_timezone_set($globalTimezone);
453
			$datetime = new DateTime();
454
			$offset = $datetime->format('P');
455
		} else $offset = '+00:00';
456
457
		if ($globalDBdriver == 'mysql') {
458
			$query  = "SELECT DISTINCT DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) as date
459
								FROM tracker_output
460
								WHERE tracker_output.date <> '' 
461
								ORDER BY tracker_output.date ASC LIMIT 0,200";
462
		} else {
463
			$query  = "SELECT DISTINCT to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') as date
464
								FROM tracker_output
465
								WHERE tracker_output.date <> '' 
466
								ORDER BY tracker_output.date ASC LIMIT 0,200";
467
		}
468
		
469
		$sth = $this->db->prepare($query);
470
		$sth->execute(array(':offset' => $offset));
471
    
472
		$date_array = array();
473
		$temp_array = array();
474
		
475
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
476
		{
477
			$temp_array['date'] = $row['date'];
478
479
			$date_array[] = $temp_array;
480
		}
481
482
		return $date_array;
483
	}
484
	
485
	
486
	/**
487
	* Update ident spotter data
488
	*
489
	* @param String $flightaware_id the ID from flightaware
0 ignored issues
show
Bug introduced by
There is no parameter named $flightaware_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
490
	* @param String $ident the flight ident
491
	* @return String success or false
492
	*
493
	*/	
494
	public function updateIdentTrackerData($famtrackid = '', $ident = '',$fromsource = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $fromsource 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...
495
	{
496
497
		$query = 'UPDATE tracker_output SET ident = :ident WHERE famtrackid = :famtrackid';
498
                $query_values = array(':famtrackid' => $famtrackid,':ident' => $ident);
499
500
		try {
501
			$sth = $this->db->prepare($query);
502
			$sth->execute($query_values);
503
		} catch (PDOException $e) {
504
			return "error : ".$e->getMessage();
505
		}
506
		
507
		return "success";
508
509
	}
510
	/**
511
	* Update latest spotter data
512
	*
513
	* @param String $flightaware_id the ID from flightaware
0 ignored issues
show
Bug introduced by
There is no parameter named $flightaware_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
514
	* @param String $ident the flight ident
515
	* @param String $arrival_airport_icao the arrival airport
0 ignored issues
show
Bug introduced by
There is no parameter named $arrival_airport_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
516
	* @return String success or false
517
	*
518
	*/	
519
	public function updateLatestTrackerData($famtrackid = '', $ident = '', $latitude = '', $longitude = '', $altitude = '', $groundspeed = NULL, $date = '')
520
	{
521
		$query = 'UPDATE tracker_output SET ident = :ident, last_latitude = :last_latitude, last_longitude = :last_longitude, last_altitude = :last_altitude, last_seen = :last_seen, last_ground_speed = :last_ground_speed WHERE famtrackid = :famtrackid';
522
                $query_values = array(':famtrackid' => $famtrackid,':last_latitude' => $latitude,':last_longitude' => $longitude, ':last_altitude' => $altitude,':last_ground_speed' => $groundspeed,':last_seen' => $date,':ident' => $ident);
523
524
		try {
525
			$sth = $this->db->prepare($query);
526
			$sth->execute($query_values);
527
		} catch (PDOException $e) {
528
			return "error : ".$e->getMessage();
529
		}
530
		
531
		return "success";
532
533
	}
534
535
	/**
536
	* Adds a new spotter data
537
	*
538
	* @param String $flightaware_id the ID from flightaware
0 ignored issues
show
Bug introduced by
There is no parameter named $flightaware_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
539
	* @param String $ident the flight ident
540
	* @param String $aircraft_icao the aircraft type
0 ignored issues
show
Bug introduced by
There is no parameter named $aircraft_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
541
	* @param String $departure_airport_icao the departure airport
0 ignored issues
show
Bug introduced by
There is no parameter named $departure_airport_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
542
	* @param String $arrival_airport_icao the arrival airport
0 ignored issues
show
Bug introduced by
There is no parameter named $arrival_airport_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
543
	* @param String $latitude latitude of flight
544
	* @param String $longitude latitude of flight
545
	* @param String $waypoints waypoints of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $waypoints. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
546
	* @param String $altitude altitude of flight
547
	* @param String $heading heading of flight
548
	* @param String $groundspeed speed of flight
549
	* @param String $date date of flight
550
	* @param String $departure_airport_time departure time of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $departure_airport_time. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
551
	* @param String $arrival_airport_time arrival time of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $arrival_airport_time. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
552
	* @param String $squawk squawk code of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $squawk. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
553
	* @param String $route_stop route stop of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $route_stop. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
554
	* @param String $highlight highlight or not
0 ignored issues
show
Bug introduced by
There is no parameter named $highlight. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
555
	* @param String $ModeS ModesS code of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $ModeS. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
556
	* @param String $registration registration code of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $registration. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
557
	* @param String $pilot_id pilot id of flight (for virtual airlines)
0 ignored issues
show
Bug introduced by
There is no parameter named $pilot_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
558
	* @param String $pilot_name pilot name of flight (for virtual airlines)
0 ignored issues
show
Bug introduced by
There is no parameter named $pilot_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
559
	* @param String $verticalrate vertival rate of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $verticalrate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
560
	* @return String success or false
561
	*/
562
	public function addTrackerData($famtrackid = '', $ident = '', $latitude = '', $longitude = '', $altitude = '', $heading = '', $groundspeed = '', $date = '', $comment = '', $type = '',$format_source = '', $source_name = '')
563
	{
564
		global $globalURL;
565
		
566
		//$Image = new Image($this->db);
567
		$Common = new Common();
568
		
569
		date_default_timezone_set('UTC');
570
		
571
		//getting the registration
572
		if ($famtrackid != "")
573
		{
574
			if (!is_string($famtrackid))
575
			{
576
				return false;
577
			}
578
		}
579
		$fromsource = NULL;
0 ignored issues
show
Unused Code introduced by
$fromsource 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...
580
		//getting the airline information
581
		if ($ident != "")
582
		{
583
			if (!is_string($ident))
584
			{
585
				return false;
586
			}
587
		}
588
589
		if ($latitude != "")
590
		{
591
			if (!is_numeric($latitude))
592
			{
593
				return false;
594
			}
595
		}
596
		
597
		if ($longitude != "")
598
		{
599
			if (!is_numeric($longitude))
600
			{
601
				return false;
602
			}
603
		}
604
		
605
		if ($altitude != "")
606
		{
607
			if (!is_numeric($altitude))
608
			{
609
				return false;
610
			}
611
		} else $altitude = 0;
612
		
613
		if ($heading != "")
614
		{
615
			if (!is_numeric($heading))
616
			{
617
				return false;
618
			}
619
		}
620
		
621
		if ($groundspeed != "")
622
		{
623
			if (!is_numeric($groundspeed))
624
			{
625
				return false;
626
			}
627
		}
628
629
    
630
		if ($date == "" || strtotime($date) < time()-20*60)
631
		{
632
			$date = date("Y-m-d H:i:s", time());
633
		}
634
635
		$famtrackid = filter_var($famtrackid,FILTER_SANITIZE_STRING);
636
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
637
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
638
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
639
		$altitude = filter_var($altitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
640
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
641
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
642
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
643
		$comment = filter_var($comment,FILTER_SANITIZE_STRING);
644
		$type = filter_var($type,FILTER_SANITIZE_STRING);
645
	
646
                if ($latitude == '' && $longitude == '') {
647
            		$latitude = 0;
648
            		$longitude = 0;
649
            	}
650
                if ($heading == '' || $Common->isInteger($heading) === false) $heading = 0;
651
                if ($groundspeed == '' || $Common->isInteger($groundspeed) === false) $groundspeed = 0;
652
                $query  = "INSERT INTO tracker_output (famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, comment, type) 
653
                VALUES (:famtrackid,:ident,:latitude,:longitude,:altitude,:heading,:speed,:date,:format_source, :source_name,:comment,:type)";
654
655
                $query_values = array(':famtrackid' => $famtrackid,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':altitude' => $altitude,':heading' => $heading,':speed' => $groundspeed,':date' => $date,':format_source' => $format_source, ':source_name' => $source_name,':comment' => $comment,':type' => $type);
656
657
		try {
658
		        
659
			$sth = $this->db->prepare($query);
660
			$sth->execute($query_values);
661
			$this->db = null;
662
		} catch (PDOException $e) {
663
		    return "error : ".$e->getMessage();
664
		}
665
		
666
		return "success";
667
668
	}
669
	
670
  
671
	/**
672
	* Gets the aircraft ident within the last hour
673
	*
674
	* @return String the ident
675
	*
676
	*/
677
	public function getIdentFromLastHour($ident)
678
	{
679
		global $globalDBdriver, $globalTimezone;
680
		if ($globalDBdriver == 'mysql') {
681
			$query  = "SELECT tracker_output.ident FROM tracker_output 
682
								WHERE tracker_output.ident = :ident 
683
								AND tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
684
								AND tracker_output.date < UTC_TIMESTAMP()";
685
			$query_data = array(':ident' => $ident);
686
		} else {
687
			$query  = "SELECT tracker_output.ident FROM tracker_output 
688
								WHERE tracker_output.ident = :ident 
689
								AND tracker_output.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
690
								AND tracker_output.date < now() AT TIME ZONE 'UTC'";
691
			$query_data = array(':ident' => $ident);
692
    		}
693
		
694
		$sth = $this->db->prepare($query);
695
		$sth->execute($query_data);
696
    		$ident_result='';
697
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
698
		{
699
			$ident_result = $row['ident'];
700
		}
701
702
		return $ident_result;
703
	}
704
	
705
	
706
	/**
707
	* Gets the aircraft data from the last 20 seconds
708
	*
709
	* @return Array the spotter data
710
	*
711
	*/
712
	public function getRealTimeData($q = '')
713
	{
714
		global $globalDBdriver;
715
		$additional_query = '';
716
		if ($q != "")
717
		{
718
			if (!is_string($q))
719
			{
720
				return false;
721
			} else {
722
				$q_array = explode(" ", $q);
723
				foreach ($q_array as $q_item){
724
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
725
					$additional_query .= " AND (";
726
					$additional_query .= "(tracker_output.ident like '%".$q_item."%')";
727
					$additional_query .= ")";
728
				}
729
			}
730
		}
731
		if ($globalDBdriver == 'mysql') {
732
			$query  = "SELECT tracker_output.* FROM tracker_output 
733
				WHERE tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 SECOND) ".$additional_query." 
734
				AND tracker_output.date < UTC_TIMESTAMP()";
735
		} else {
736
			$query  = "SELECT tracker_output.* FROM tracker_output 
737
				WHERE tracker_output.date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 SECONDS' ".$additional_query." 
738
				AND tracker_output.date::timestamp < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";
739
		}
740
		$spotter_array = $this->getDataFromDB($query, array());
741
742
		return $spotter_array;
743
	}
744
	
745
	
746
	
747
748
	/**
749
	* Gets all number of flight over countries
750
	*
751
	* @return Array the airline country list
752
	*
753
	*/
754
755
	public function countAllTrackerOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array())
756
	{
757
		global $globalDBdriver, $globalArchive;
758
		//$filter_query = $this->getFilter($filters,true,true);
759
		$Connection= new Connection($this->db);
760
		if (!$Connection->tableExists('countries')) return array();
761
		if (!isset($globalArchive) || $globalArchive !== TRUE) {
762
			require_once('class.TrackerLive.php');
763
			$TrackerLive = new TrackerLive();
764
			$filter_query = $TrackerLive->getFilter($filters,true,true);
765
			$filter_query .= ' over_country IS NOT NULL';
766
			if ($olderthanmonths > 0) {
767
				if ($globalDBdriver == 'mysql') {
768
					$filter_query .= ' AND tracker_live.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
769
				} else {
770
					$filter_query .= " AND tracker_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
771
				}
772
			}
773
			if ($sincedate != '') {
774
				if ($globalDBdriver == 'mysql') {
775
					$filter_query .= " AND tracker_live.date > '".$sincedate."' ";
776
				} else {
777
					$filter_query .= " AND tracker_live.date > CAST('".$sincedate."' AS TIMESTAMP)";
778
				}
779
			}
780
			$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT famtrackid,over_country FROM tracker_live".$filter_query.") l ON c.iso2 = l.over_country ";
781
		} else {
782
			require_once('class.TrackerArchive.php');
783
			$TrackerArchive = new TrackerArchive();
784
			$filter_query = $TrackerArchive->getFilter($filters,true,true);
785
			$filter_query .= ' over_country IS NOT NULL';
786
			if ($olderthanmonths > 0) {
787
				if ($globalDBdriver == 'mysql') {
788
					$filter_query .= ' AND tracker_archive.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
789
				} else {
790
					$filter_query .= " AND tracker_archive.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
791
				}
792
			}
793
			if ($sincedate != '') {
794
				if ($globalDBdriver == 'mysql') {
795
					$filter_query .= " AND tracker_archive.date > '".$sincedate."' ";
796
				} else {
797
					$filter_query .= " AND tracker_archive.date > CAST('".$sincedate."' AS TIMESTAMP)";
798
				}
799
			}
800
			$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT famtrackid,over_country FROM tracker_archive".$filter_query.") l ON c.iso2 = l.over_country ";
801
		}
802
		$query .= "GROUP BY c.name,c.iso3,c.iso2 ORDER BY nb DESC";
803
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
804
      
805
		
806
		$sth = $this->db->prepare($query);
807
		$sth->execute();
808
 
809
		$flight_array = array();
810
		$temp_array = array();
811
        
812
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
813
		{
814
			$temp_array['flight_count'] = $row['nb'];
815
			$temp_array['flight_country'] = $row['name'];
816
			$temp_array['flight_country_iso3'] = $row['iso3'];
817
			$temp_array['flight_country_iso2'] = $row['iso2'];
818
			$flight_array[] = $temp_array;
819
		}
820
		return $flight_array;
821
	}
822
	
823
	/**
824
	* Gets all callsigns that have flown over
825
	*
826
	* @return Array the callsign list
827
	*
828
	*/
829
	public function countAllCallsigns($limit = true, $olderthanmonths = 0, $sincedate = '',$filters = array(),$year = '', $month = '', $day = '')
830
	{
831
		global $globalDBdriver;
832
		$filter_query = $this->getFilter($filters,true,true);
833
		$query  = "SELECT DISTINCT tracker_output.ident, COUNT(tracker_output.ident) AS callsign_icao_count 
834
                    FROM tracker_output".$filter_query." tracker_output.ident <> ''";
835
		 if ($olderthanmonths > 0) {
836
			if ($globalDBdriver == 'mysql') $query .= ' AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH)';
837
			else $query .= " AND tracker_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
838
		}
839
		if ($sincedate != '') {
840
			if ($globalDBdriver == 'mysql') $query .= " AND tracker_output.date > '".$sincedate."'";
841
			else $query .= " AND tracker_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
842
		}
843
		$query_values = array();
844
		if ($year != '') {
845
			if ($globalDBdriver == 'mysql') {
846
				$query .= " AND YEAR(tracker_output.date) = :year";
847
				$query_values = array_merge($query_values,array(':year' => $year));
848
			} else {
849
				$query .= " AND EXTRACT(YEAR FROM tracker_output.date) = :year";
850
				$query_values = array_merge($query_values,array(':year' => $year));
851
			}
852
		}
853
		if ($month != '') {
854
			if ($globalDBdriver == 'mysql') {
855
				$query .= " AND MONTH(tracker_output.date) = :month";
856
				$query_values = array_merge($query_values,array(':month' => $month));
857
			} else {
858
				$query .= " AND EXTRACT(MONTH FROM tracker_output.date) = :month";
859
				$query_values = array_merge($query_values,array(':month' => $month));
860
			}
861
		}
862
		if ($day != '') {
863
			if ($globalDBdriver == 'mysql') {
864
				$query .= " AND DAY(tracker_output.date) = :day";
865
				$query_values = array_merge($query_values,array(':day' => $day));
866
			} else {
867
				$query .= " AND EXTRACT(DAY FROM tracker_output.date) = :day";
868
				$query_values = array_merge($query_values,array(':day' => $day));
869
			}
870
		}
871
		$query .= " GROUP BY tracker_output.ident ORDER BY callsign_icao_count DESC";
872
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
873
      		
874
		$sth = $this->db->prepare($query);
875
		$sth->execute($query_values);
876
      
877
		$callsign_array = array();
878
		$temp_array = array();
879
        
880
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
881
		{
882
			$temp_array['callsign_icao'] = $row['ident'];
883
			$temp_array['airline_name'] = $row['airline_name'];
884
			$temp_array['airline_icao'] = $row['airline_icao'];
885
			$temp_array['callsign_icao_count'] = $row['callsign_icao_count'];
886
          
887
			$callsign_array[] = $temp_array;
888
		}
889
890
		return $callsign_array;
891
	}
892
893
894
	/**
895
	* Counts all dates
896
	*
897
	* @return Array the date list
898
	*
899
	*/
900
	public function countAllDates($filters = array())
901
	{
902
		global $globalTimezone, $globalDBdriver;
903
		if ($globalTimezone != '') {
904
			date_default_timezone_set($globalTimezone);
905
			$datetime = new DateTime();
906
			$offset = $datetime->format('P');
907
		} else $offset = '+00:00';
908
909
		if ($globalDBdriver == 'mysql') {
910
			$query  = "SELECT DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
911
								FROM tracker_output";
912
			$query .= $this->getFilter($filters);
913
			$query .= " GROUP BY date_name 
914
								ORDER BY date_count DESC
915
								LIMIT 10 OFFSET 0";
916
		} else {
917
			$query  = "SELECT to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
918
								FROM tracker_output";
919
			$query .= $this->getFilter($filters);
920
			$query .= " GROUP BY date_name 
921
								ORDER BY date_count DESC
922
								LIMIT 10 OFFSET 0";
923
		}
924
      
925
		
926
		$sth = $this->db->prepare($query);
927
		$sth->execute(array(':offset' => $offset));
928
      
929
		$date_array = array();
930
		$temp_array = array();
931
        
932
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
933
		{
934
			$temp_array['date_name'] = $row['date_name'];
935
			$temp_array['date_count'] = $row['date_count'];
936
937
			$date_array[] = $temp_array;
938
		}
939
940
		return $date_array;
941
	}
942
	
943
	
944
	/**
945
	* Counts all dates during the last 7 days
946
	*
947
	* @return Array the date list
948
	*
949
	*/
950
	public function countAllDatesLast7Days($filters = array())
951
	{
952
		global $globalTimezone, $globalDBdriver;
953
		if ($globalTimezone != '') {
954
			date_default_timezone_set($globalTimezone);
955
			$datetime = new DateTime();
956
			$offset = $datetime->format('P');
957
		} else $offset = '+00:00';
958
		$filter_query = $this->getFilter($filters,true,true);
959
		if ($globalDBdriver == 'mysql') {
960
			$query  = "SELECT DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
961
								FROM tracker_output".$filter_query." tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY)";
962
			$query .= " GROUP BY date_name 
963
								ORDER BY tracker_output.date ASC";
964
			$query_data = array(':offset' => $offset);
965
		} else {
966
			$query  = "SELECT to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
967
								FROM tracker_output".$filter_query." tracker_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '7 DAYS'";
968
			$query .= " GROUP BY date_name 
969
								ORDER BY date_name ASC";
970
			$query_data = array(':offset' => $offset);
971
    		}
972
		
973
		$sth = $this->db->prepare($query);
974
		$sth->execute($query_data);
975
      
976
		$date_array = array();
977
		$temp_array = array();
978
        
979
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
980
		{
981
			$temp_array['date_name'] = $row['date_name'];
982
			$temp_array['date_count'] = $row['date_count'];
983
          
984
			$date_array[] = $temp_array;
985
		}
986
987
		return $date_array;
988
	}
989
990
	/**
991
	* Counts all dates during the last month
992
	*
993
	* @return Array the date list
994
	*
995
	*/
996
	public function countAllDatesLastMonth($filters = array())
997
	{
998
		global $globalTimezone, $globalDBdriver;
999
		if ($globalTimezone != '') {
1000
			date_default_timezone_set($globalTimezone);
1001
			$datetime = new DateTime();
1002
			$offset = $datetime->format('P');
1003
		} else $offset = '+00:00';
1004
		$filter_query = $this->getFilter($filters,true,true);
1005
		if ($globalDBdriver == 'mysql') {
1006
			$query  = "SELECT DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1007
								FROM tracker_output".$filter_query." tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MONTH)";
1008
			$query .= " GROUP BY date_name 
1009
								ORDER BY tracker_output.date ASC";
1010
			$query_data = array(':offset' => $offset);
1011
		} else {
1012
			$query  = "SELECT to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1013
								FROM tracker_output".$filter_query." tracker_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 MONTHS'";
1014
			$query .= " GROUP BY date_name 
1015
								ORDER BY date_name ASC";
1016
			$query_data = array(':offset' => $offset);
1017
    		}
1018
		
1019
		$sth = $this->db->prepare($query);
1020
		$sth->execute($query_data);
1021
      
1022
		$date_array = array();
1023
		$temp_array = array();
1024
        
1025
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1026
		{
1027
			$temp_array['date_name'] = $row['date_name'];
1028
			$temp_array['date_count'] = $row['date_count'];
1029
          
1030
			$date_array[] = $temp_array;
1031
		}
1032
1033
		return $date_array;
1034
	}
1035
1036
1037
1038
	/**
1039
	* Counts all month
1040
	*
1041
	* @return Array the month list
1042
	*
1043
	*/
1044
	public function countAllMonths($filters = array())
1045
	{
1046
		global $globalTimezone, $globalDBdriver;
1047
		if ($globalTimezone != '') {
1048
			date_default_timezone_set($globalTimezone);
1049
			$datetime = new DateTime();
1050
			$offset = $datetime->format('P');
1051
		} else $offset = '+00:00';
1052
1053
		if ($globalDBdriver == 'mysql') {
1054
			$query  = "SELECT YEAR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS year_name,MONTH(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS month_name, count(*) as date_count
1055
								FROM tracker_output";
1056
			$query .= $this->getFilter($filters);
1057
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1058
		} else {
1059
			$query  = "SELECT EXTRACT(YEAR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS year_name,EXTRACT(MONTH FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS month_name, count(*) as date_count
1060
								FROM tracker_output";
1061
			$query .= $this->getFilter($filters);
1062
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1063
		}
1064
      
1065
		
1066
		$sth = $this->db->prepare($query);
1067
		$sth->execute(array(':offset' => $offset));
1068
      
1069
		$date_array = array();
1070
		$temp_array = array();
1071
        
1072
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1073
		{
1074
			$temp_array['month_name'] = $row['month_name'];
1075
			$temp_array['year_name'] = $row['year_name'];
1076
			$temp_array['date_count'] = $row['date_count'];
1077
1078
			$date_array[] = $temp_array;
1079
		}
1080
1081
		return $date_array;
1082
	}
1083
1084
	
1085
	
1086
1087
	/**
1088
	* Counts all dates during the last year
1089
	*
1090
	* @return Array the date list
1091
	*
1092
	*/
1093
	public function countAllMonthsLastYear($filters)
1094
	{
1095
		global $globalTimezone, $globalDBdriver;
1096
		if ($globalTimezone != '') {
1097
			date_default_timezone_set($globalTimezone);
1098
			$datetime = new DateTime();
1099
			$offset = $datetime->format('P');
1100
		} else $offset = '+00:00';
1101
		$filter_query = $this->getFilter($filters,true,true);
1102
		if ($globalDBdriver == 'mysql') {
1103
			$query  = "SELECT MONTH(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS month_name, YEAR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS year_name, count(*) as date_count
1104
								FROM tracker_output".$filter_query." tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)";
1105
			$query .= " GROUP BY year_name, month_name
1106
								ORDER BY year_name, month_name ASC";
1107
			$query_data = array(':offset' => $offset);
1108
		} else {
1109
			$query  = "SELECT EXTRACT(MONTH FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS month_name, EXTRACT(YEAR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS year_name, count(*) as date_count
1110
								FROM tracker_output".$filter_query." tracker_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'";
1111
			$query .= " GROUP BY year_name, month_name
1112
								ORDER BY year_name, month_name ASC";
1113
			$query_data = array(':offset' => $offset);
1114
    		}
1115
		
1116
		$sth = $this->db->prepare($query);
1117
		$sth->execute($query_data);
1118
      
1119
		$date_array = array();
1120
		$temp_array = array();
1121
        
1122
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1123
		{
1124
			$temp_array['year_name'] = $row['year_name'];
1125
			$temp_array['month_name'] = $row['month_name'];
1126
			$temp_array['date_count'] = $row['date_count'];
1127
          
1128
			$date_array[] = $temp_array;
1129
		}
1130
1131
		return $date_array;
1132
	}
1133
	
1134
	
1135
	
1136
	/**
1137
	* Counts all hours
1138
	*
1139
	* @return Array the hour list
1140
	*
1141
	*/
1142
	public function countAllHours($orderby,$filters = array())
1143
	{
1144
		global $globalTimezone, $globalDBdriver;
1145
		if ($globalTimezone != '') {
1146
			date_default_timezone_set($globalTimezone);
1147
			$datetime = new DateTime();
1148
			$offset = $datetime->format('P');
1149
		} else $offset = '+00:00';
1150
1151
		$orderby_sql = '';
1152
		if ($orderby == "hour")
1153
		{
1154
			$orderby_sql = "ORDER BY hour_name ASC";
1155
		}
1156
		if ($orderby == "count")
1157
		{
1158
			$orderby_sql = "ORDER BY hour_count DESC";
1159
		}
1160
		
1161
		if ($globalDBdriver == 'mysql') {
1162
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1163
								FROM tracker_output";
1164
			$query .= $this->getFilter($filters);
1165
			$query .= " GROUP BY hour_name 
1166
								".$orderby_sql;
1167
1168
/*		$query  = "SELECT HOUR(tracker_output.date) AS hour_name, count(*) as hour_count
1169
								FROM tracker_output 
1170
								GROUP BY hour_name 
1171
								".$orderby_sql."
1172
								LIMIT 10 OFFSET 00";
1173
  */    
1174
		$query_data = array(':offset' => $offset);
1175
		} else {
1176
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1177
								FROM tracker_output";
1178
			$query .= $this->getFilter($filters);
1179
			$query .= " GROUP BY hour_name 
1180
								".$orderby_sql;
1181
			$query_data = array(':offset' => $offset);
1182
		}
1183
		
1184
		$sth = $this->db->prepare($query);
1185
		$sth->execute($query_data);
1186
      
1187
		$hour_array = array();
1188
		$temp_array = array();
1189
        
1190
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1191
		{
1192
			$temp_array['hour_name'] = $row['hour_name'];
1193
			$temp_array['hour_count'] = $row['hour_count'];
1194
          
1195
			$hour_array[] = $temp_array;
1196
		}
1197
1198
		return $hour_array;
1199
	}
1200
	
1201
	
1202
	
1203
	/**
1204
	* Counts all hours by date
1205
	*
1206
	* @return Array the hour list
1207
	*
1208
	*/
1209
	public function countAllHoursByDate($date, $filters = array())
1210
	{
1211
		global $globalTimezone, $globalDBdriver;
1212
		$filter_query = $this->getFilter($filters,true,true);
1213
		$date = filter_var($date,FILTER_SANITIZE_STRING);
1214
		if ($globalTimezone != '') {
1215
			date_default_timezone_set($globalTimezone);
1216
			$datetime = new DateTime($date);
1217
			$offset = $datetime->format('P');
1218
		} else $offset = '+00:00';
1219
1220
		if ($globalDBdriver == 'mysql') {
1221
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1222
								FROM tracker_output".$filter_query." DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) = :date
1223
								GROUP BY hour_name 
1224
								ORDER BY hour_name ASC";
1225
		} else {
1226
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1227
								FROM tracker_output".$filter_query." to_char(tracker_output.date AT TIME ZONE INTERVAL :offset, 'YYYY-mm-dd') = :date
1228
								GROUP BY hour_name 
1229
								ORDER BY hour_name ASC";
1230
		}
1231
		
1232
		$sth = $this->db->prepare($query);
1233
		$sth->execute(array(':date' => $date, ':offset' => $offset));
1234
      
1235
		$hour_array = array();
1236
		$temp_array = array();
1237
        
1238
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1239
		{
1240
			$temp_array['hour_name'] = $row['hour_name'];
1241
			$temp_array['hour_count'] = $row['hour_count'];
1242
          
1243
			$hour_array[] = $temp_array;
1244
		}
1245
1246
		return $hour_array;
1247
	}
1248
	
1249
	
1250
	
1251
	/**
1252
	* Counts all hours by a ident/callsign
1253
	*
1254
	* @return Array the hour list
1255
	*
1256
	*/
1257
	public function countAllHoursByIdent($ident, $filters = array())
1258
	{
1259
		global $globalTimezone, $globalDBdriver;
1260
		$filter_query = $this->getFilter($filters,true,true);
1261
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1262
		if ($globalTimezone != '') {
1263
			date_default_timezone_set($globalTimezone);
1264
			$datetime = new DateTime();
1265
			$offset = $datetime->format('P');
1266
		} else $offset = '+00:00';
1267
1268
		if ($globalDBdriver == 'mysql') {
1269
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1270
								FROM tracker_output".$filter_query." tracker_output.ident = :ident 
1271
								GROUP BY hour_name 
1272
								ORDER BY hour_name ASC";
1273
		} else {
1274
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1275
								FROM tracker_output".$filter_query." tracker_output.ident = :ident 
1276
								GROUP BY hour_name 
1277
								ORDER BY hour_name ASC";
1278
		}
1279
      
1280
		
1281
		$sth = $this->db->prepare($query);
1282
		$sth->execute(array(':ident' => $ident,':offset' => $offset));
1283
      
1284
		$hour_array = array();
1285
		$temp_array = array();
1286
        
1287
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1288
		{
1289
			$temp_array['hour_name'] = $row['hour_name'];
1290
			$temp_array['hour_count'] = $row['hour_count'];
1291
          
1292
			$hour_array[] = $temp_array;
1293
		}
1294
1295
		return $hour_array;
1296
	}
1297
	
1298
	
1299
	
1300
	/**
1301
	* Counts all trackers that have flown over
1302
	*
1303
	* @return Integer the number of trackers
1304
	*
1305
	*/
1306
	public function countOverallTracker($filters = array(),$year = '',$month = '')
1307
	{
1308
		global $globalDBdriver;
1309
		//$queryi  = "SELECT COUNT(tracker_output.tracker_id) AS flight_count FROM tracker_output";
1310
		$queryi  = "SELECT COUNT(DISTINCT tracker_output.ident) AS tracker_count FROM tracker_output";
1311
		$query_values = array();
1312
		$query = '';
1313
		if ($year != '') {
1314
			if ($globalDBdriver == 'mysql') {
1315
				$query .= " AND YEAR(tracker_output.date) = :year";
1316
				$query_values = array_merge($query_values,array(':year' => $year));
1317
			} else {
1318
				$query .= " AND EXTRACT(YEAR FROM tracker_output.date) = :year";
1319
				$query_values = array_merge($query_values,array(':year' => $year));
1320
			}
1321
		}
1322
		if ($month != '') {
1323
			if ($globalDBdriver == 'mysql') {
1324
				$query .= " AND MONTH(tracker_output.date) = :month";
1325
				$query_values = array_merge($query_values,array(':month' => $month));
1326
			} else {
1327
				$query .= " AND EXTRACT(MONTH FROM tracker_output.date) = :month";
1328
				$query_values = array_merge($query_values,array(':month' => $month));
1329
			}
1330
		}
1331
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1332
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1333
		
1334
		$sth = $this->db->prepare($queryi);
1335
		$sth->execute($query_values);
1336
		return $sth->fetchColumn();
1337
	}
1338
	
1339
	/**
1340
	* Counts all trackers type that have flown over
1341
	*
1342
	* @return Integer the number of flights
1343
	*
1344
	*/
1345
	public function countOverallTrackerTypes($filters = array(),$year = '',$month = '')
1346
	{
1347
		global $globalDBdriver;
1348
		$queryi  = "SELECT COUNT(DISTINCT tracker_output.type) AS tracker_count FROM tracker_output";
1349
		$query_values = array();
1350
		$query = '';
1351
		if ($year != '') {
1352
			if ($globalDBdriver == 'mysql') {
1353
				$query .= " AND YEAR(tracker_output.date) = :year";
1354
				$query_values = array_merge($query_values,array(':year' => $year));
1355
			} else {
1356
				$query .= " AND EXTRACT(YEAR FROM tracker_output.date) = :year";
1357
				$query_values = array_merge($query_values,array(':year' => $year));
1358
			}
1359
		}
1360
		if ($month != '') {
1361
			if ($globalDBdriver == 'mysql') {
1362
				$query .= " AND MONTH(tracker_output.date) = :month";
1363
				$query_values = array_merge($query_values,array(':month' => $month));
1364
			} else {
1365
				$query .= " AND EXTRACT(MONTH FROM tracker_output.date) = :month";
1366
				$query_values = array_merge($query_values,array(':month' => $month));
1367
			}
1368
		}
1369
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1370
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1371
		
1372
		$sth = $this->db->prepare($queryi);
1373
		$sth->execute($query_values);
1374
		return $sth->fetchColumn();
1375
	}
1376
	
1377
  
1378
	/**
1379
	* Counts all hours of today
1380
	*
1381
	* @return Array the hour list
1382
	*
1383
	*/
1384
	public function countAllHoursFromToday($filters = array())
1385
	{
1386
		global $globalTimezone, $globalDBdriver;
1387
		$filter_query = $this->getFilter($filters,true,true);
1388
		if ($globalTimezone != '') {
1389
			date_default_timezone_set($globalTimezone);
1390
			$datetime = new DateTime();
1391
			$offset = $datetime->format('P');
1392
		} else $offset = '+00:00';
1393
1394
		if ($globalDBdriver == 'mysql') {
1395
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1396
								FROM tracker_output".$filter_query." DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) = CURDATE()
1397
								GROUP BY hour_name 
1398
								ORDER BY hour_name ASC";
1399
		} else {
1400
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1401
								FROM tracker_output".$filter_query." to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') = CAST(NOW() AS date)
1402
								GROUP BY hour_name 
1403
								ORDER BY hour_name ASC";
1404
		}
1405
		
1406
		$sth = $this->db->prepare($query);
1407
		$sth->execute(array(':offset' => $offset));
1408
      
1409
		$hour_array = array();
1410
		$temp_array = array();
1411
        
1412
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1413
		{
1414
			$temp_array['hour_name'] = $row['hour_name'];
1415
			$temp_array['hour_count'] = $row['hour_count'];
1416
			$hour_array[] = $temp_array;
1417
		}
1418
1419
		return $hour_array;
1420
	}
1421
    
1422
    
1423
     /**
1424
	* Gets the Barrie Spotter ID based on the FlightAware ID
1425
	*
1426
	* @return Integer the Barrie Spotter ID
1427
q	*
1428
	*/
1429
	public function getTrackerIDBasedOnFamTrackID($famtrackid)
1430
	{
1431
		$famtrackid = filter_var($famtrackid,FILTER_SANITIZE_STRING);
1432
1433
		$query  = "SELECT tracker_output.tracker_id
1434
				FROM tracker_output 
1435
				WHERE tracker_output.famtrackid = '".$famtrackid."'";
1436
        
1437
		
1438
		$sth = $this->db->prepare($query);
1439
		$sth->execute();
1440
1441
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1442
		{
1443
			return $row['tracker_id'];
1444
		}
1445
	}
1446
  
1447
 
1448
	/**
1449
	* Parses a date string
1450
	*
1451
	* @param String $dateString the date string
1452
	* @param String $timezone the timezone of a user
1453
	* @return Array the time information
1454
	*
1455
	*/
1456
	public function parseDateString($dateString, $timezone = '')
1457
	{
1458
		$time_array = array();
1459
	
1460
		if ($timezone != "")
1461
		{
1462
			date_default_timezone_set($timezone);
1463
		}
1464
		
1465
		$current_date = date("Y-m-d H:i:s");
1466
		$date = date("Y-m-d H:i:s",strtotime($dateString." UTC"));
1467
		
1468
		$diff = abs(strtotime($current_date) - strtotime($date));
1469
1470
		$time_array['years'] = floor($diff / (365*60*60*24)); 
1471
		$years = $time_array['years'];
1472
		
1473
		$time_array['months'] = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
1474
		$months = $time_array['months'];
1475
		
1476
		$time_array['days'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
1477
		$days = $time_array['days'];
1478
		$time_array['hours'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
1479
		$hours = $time_array['hours'];
1480
		$time_array['minutes'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
1481
		$minutes = $time_array['minutes'];
1482
		$time_array['seconds'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));  
1483
		
1484
		return $time_array;
1485
	}
1486
	
1487
	/**
1488
	* Parses the direction degrees to working
1489
	*
1490
	* @param Float $direction the direction in degrees
1491
	* @return Array the direction information
1492
	*
1493
	*/
1494
	public function parseDirection($direction = 0)
1495
	{
1496
		if ($direction == '') $direction = 0;
1497
		$direction_array = array();
1498
		$temp_array = array();
1499
1500
		if ($direction == 360 || ($direction >= 0 && $direction < 22.5))
1501
		{
1502
			$temp_array['direction_degree'] = $direction;
1503
			$temp_array['direction_shortname'] = "N";
1504
			$temp_array['direction_fullname'] = "North";
1505
		} elseif ($direction >= 22.5 && $direction < 45){
1506
			$temp_array['direction_degree'] = $direction;
1507
			$temp_array['direction_shortname'] = "NNE";
1508
			$temp_array['direction_fullname'] = "North-Northeast";
1509
		} elseif ($direction >= 45 && $direction < 67.5){
1510
			$temp_array['direction_degree'] = $direction;
1511
			$temp_array['direction_shortname'] = "NE";
1512
			$temp_array['direction_fullname'] = "Northeast";
1513
		} elseif ($direction >= 67.5 && $direction < 90){
1514
			$temp_array['direction_degree'] = $direction;
1515
			$temp_array['direction_shortname'] = "ENE";
1516
			$temp_array['direction_fullname'] = "East-Northeast";
1517
		} elseif ($direction >= 90 && $direction < 112.5){
1518
			$temp_array['direction_degree'] = $direction;
1519
			$temp_array['direction_shortname'] = "E";
1520
			$temp_array['direction_fullname'] = "East";
1521
		} elseif ($direction >= 112.5 && $direction < 135){
1522
			$temp_array['direction_degree'] = $direction;
1523
			$temp_array['direction_shortname'] = "ESE";
1524
			$temp_array['direction_fullname'] = "East-Southeast";
1525
		} elseif ($direction >= 135 && $direction < 157.5){
1526
			$temp_array['direction_degree'] = $direction;
1527
			$temp_array['direction_shortname'] = "SE";
1528
			$temp_array['direction_fullname'] = "Southeast";
1529
		} elseif ($direction >= 157.5 && $direction < 180){
1530
			$temp_array['direction_degree'] = $direction;
1531
			$temp_array['direction_shortname'] = "SSE";
1532
			$temp_array['direction_fullname'] = "South-Southeast";
1533
		} elseif ($direction >= 180 && $direction < 202.5){
1534
			$temp_array['direction_degree'] = $direction;
1535
			$temp_array['direction_shortname'] = "S";
1536
			$temp_array['direction_fullname'] = "South";
1537
		} elseif ($direction >= 202.5 && $direction < 225){
1538
			$temp_array['direction_degree'] = $direction;
1539
			$temp_array['direction_shortname'] = "SSW";
1540
			$temp_array['direction_fullname'] = "South-Southwest";
1541
		} elseif ($direction >= 225 && $direction < 247.5){
1542
			$temp_array['direction_degree'] = $direction;
1543
			$temp_array['direction_shortname'] = "SW";
1544
			$temp_array['direction_fullname'] = "Southwest";
1545
		} elseif ($direction >= 247.5 && $direction < 270){
1546
			$temp_array['direction_degree'] = $direction;
1547
			$temp_array['direction_shortname'] = "WSW";
1548
			$temp_array['direction_fullname'] = "West-Southwest";
1549
		} elseif ($direction >= 270 && $direction < 292.5){
1550
			$temp_array['direction_degree'] = $direction;
1551
			$temp_array['direction_shortname'] = "W";
1552
			$temp_array['direction_fullname'] = "West";
1553
		} elseif ($direction >= 292.5 && $direction < 315){
1554
			$temp_array['direction_degree'] = $direction;
1555
			$temp_array['direction_shortname'] = "WNW";
1556
			$temp_array['direction_fullname'] = "West-Northwest";
1557
		} elseif ($direction >= 315 && $direction < 337.5){
1558
			$temp_array['direction_degree'] = $direction;
1559
			$temp_array['direction_shortname'] = "NW";
1560
			$temp_array['direction_fullname'] = "Northwest";
1561
		} elseif ($direction >= 337.5 && $direction < 360){
1562
			$temp_array['direction_degree'] = $direction;
1563
			$temp_array['direction_shortname'] = "NNW";
1564
			$temp_array['direction_fullname'] = "North-Northwest";
1565
		}
1566
		$direction_array[] = $temp_array;
1567
		return $direction_array;
1568
	}
1569
	
1570
	
1571
	/**
1572
	* Gets Country from latitude/longitude
1573
	*
1574
	* @param Float $latitude latitute of the flight
1575
	* @param Float $longitude longitute of the flight
1576
	* @return String the countrie
1577
	*/
1578
	public function getCountryFromLatitudeLongitude($latitude,$longitude)
1579
	{
1580
		global $globalDBdriver, $globalDebug;
1581
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1582
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1583
	
1584
		$Connection = new Connection($this->db);
1585
		if (!$Connection->tableExists('countries')) return '';
1586
	
1587
		try {
1588
			/*
1589
			if ($globalDBdriver == 'mysql') {
1590
				//$query  = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(:latitude :longitude)'), ogc_geom) LIMIT 1";
1591
				$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(".$longitude.' '.$latitude.")'), ogc_geom) LIMIT 1";
1592
			}
1593
			*/
1594
			// This query seems to work both for MariaDB and PostgreSQL
1595
			$query = "SELECT name,iso2,iso3 FROM countries WHERE ST_Within(ST_GeomFromText('POINT(".$longitude." ".$latitude.")',4326), ogc_geom) LIMIT 1";
1596
		
1597
			$sth = $this->db->prepare($query);
1598
			//$sth->execute(array(':latitude' => $latitude,':longitude' => $longitude));
1599
			$sth->execute();
1600
    
1601
			$row = $sth->fetch(PDO::FETCH_ASSOC);
1602
			$sth->closeCursor();
1603
			if (count($row) > 0) {
1604
				return $row;
1605
			} else return '';
1606
		} catch (PDOException $e) {
1607
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
1608
			return '';
1609
		}
1610
	
1611
	}
1612
1613
	/**
1614
	* Gets Country from iso2
1615
	*
1616
	* @param String $iso2 ISO2 country code
1617
	* @return String the countrie
1618
	*/
1619
	public function getCountryFromISO2($iso2)
1620
	{
1621
		global $globalDBdriver, $globalDebug;
1622
		$iso2 = filter_var($iso2,FILTER_SANITIZE_STRING);
1623
	
1624
		$Connection = new Connection($this->db);
1625
		if (!$Connection->tableExists('countries')) return '';
1626
	
1627
		try {
1628
			$query = "SELECT name,iso2,iso3 FROM countries WHERE iso2 = :iso2 LIMIT 1";
1629
		
1630
			$sth = $this->db->prepare($query);
1631
			$sth->execute(array(':iso2' => $iso2));
1632
    
1633
			$row = $sth->fetch(PDO::FETCH_ASSOC);
1634
			$sth->closeCursor();
1635
			if (count($row) > 0) {
1636
				return $row;
1637
			} else return '';
1638
		} catch (PDOException $e) {
1639
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
1640
			return '';
1641
		}
1642
	
1643
	}
1644
1645
	/**
1646
	* Gets all vessels types that have flown over
1647
	*
1648
	* @return Array the vessel type list
1649
	*
1650
	*/
1651
	public function countAllTrackerTypes($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array(),$year = '',$month = '',$day = '')
1652
	{
1653
		global $globalDBdriver;
1654
		$filter_query = $this->getFilter($filters,true,true);
1655
		$query  = "SELECT tracker_output.type AS tracker_type, COUNT(tracker_output.type) AS tracker_type_count 
1656
		    FROM tracker_output ".$filter_query." tracker_output.type  <> ''";
1657
		if ($olderthanmonths > 0) {
1658
			if ($globalDBdriver == 'mysql') {
1659
				$query .= ' AND tracker_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$olderthanmonths.' MONTH)';
1660
			} else {
1661
				$query .= " AND tracker_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1662
			}
1663
		}
1664
		if ($sincedate != '') {
1665
			if ($globalDBdriver == 'mysql') {
1666
				$query .= " AND tracker_output.date > '".$sincedate."'";
1667
			} else {
1668
				$query .= " AND tracker_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
1669
			}
1670
		}
1671
		$query_values = array();
1672
		if ($year != '') {
1673
			if ($globalDBdriver == 'mysql') {
1674
				$query .= " AND YEAR(tracker_output.date) = :year";
1675
				$query_values = array_merge($query_values,array(':year' => $year));
1676
			} else {
1677
				$query .= " AND EXTRACT(YEAR FROM tracker_output.date) = :year";
1678
				$query_values = array_merge($query_values,array(':year' => $year));
1679
			}
1680
		}
1681
		if ($month != '') {
1682
			if ($globalDBdriver == 'mysql') {
1683
				$query .= " AND MONTH(tracker_output.date) = :month";
1684
				$query_values = array_merge($query_values,array(':month' => $month));
1685
			} else {
1686
				$query .= " AND EXTRACT(MONTH FROM tracker_output.date) = :month";
1687
				$query_values = array_merge($query_values,array(':month' => $month));
1688
			}
1689
		}
1690
		if ($day != '') {
1691
			if ($globalDBdriver == 'mysql') {
1692
				$query .= " AND DAY(tracker_output.date) = :day";
1693
				$query_values = array_merge($query_values,array(':day' => $day));
1694
			} else {
1695
				$query .= " AND EXTRACT(DAY FROM tracker_output.date) = :day";
1696
				$query_values = array_merge($query_values,array(':day' => $day));
1697
			}
1698
		}
1699
		$query .= " GROUP BY tracker_output.type ORDER BY tracker_type_count DESC";
1700
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1701
		$sth = $this->db->prepare($query);
1702
		$sth->execute($query_values);
1703
		$tracker_array = array();
1704
		$temp_array = array();
1705
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1706
		{
1707
			$temp_array['tracker_type'] = $row['tracker_type'];
1708
			$temp_array['tracker_type_count'] = $row['tracker_type_count'];
1709
			$tracker_array[] = $temp_array;
1710
		}
1711
		return $tracker_array;
1712
	}
1713
1714
1715
	
1716
	/**
1717
	* Gets the short url from bit.ly
1718
	*
1719
	* @param String $url the full url
1720
	* @return String the bit.ly url
1721
	*
1722
	*/
1723
	public function getBitlyURL($url)
1724
	{
1725
		global $globalBitlyAccessToken;
1726
		
1727
		if ($globalBitlyAccessToken == '') return $url;
1728
        
1729
		$google_url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$globalBitlyAccessToken.'&longUrl='.$url;
1730
		
1731
		$ch = curl_init();
1732
		curl_setopt($ch, CURLOPT_HEADER, 0);
1733
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1734
		curl_setopt($ch, CURLOPT_URL, $google_url);
1735
		$bitly_data = curl_exec($ch);
1736
		curl_close($ch);
1737
		
1738
		$bitly_data = json_decode($bitly_data);
1739
		$bitly_url = '';
1740
		if ($bitly_data->status_txt = "OK"){
1741
			$bitly_url = $bitly_data->data->url;
1742
		}
1743
1744
		return $bitly_url;
1745
	}
1746
1747
1748
	public function getOrderBy()
1749
	{
1750
		$orderby = array("aircraft_asc" => array("key" => "aircraft_asc", "value" => "Aircraft Type - ASC", "sql" => "ORDER BY tracker_output.aircraft_icao ASC"), "aircraft_desc" => array("key" => "aircraft_desc", "value" => "Aircraft Type - DESC", "sql" => "ORDER BY tracker_output.aircraft_icao DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY tracker_output.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY tracker_output.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY tracker_output.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY tracker_output.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY tracker_output.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY tracker_output.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure Airport - ASC", "sql" => "ORDER BY tracker_output.departure_airport_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY tracker_output.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY tracker_output.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY tracker_output.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY tracker_output.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY tracker_output.date DESC"),"distance_asc" => array("key" => "distance_asc","value" => "Distance - ASC","sql" => "ORDER BY distance ASC"),"distance_desc" => array("key" => "distance_desc","value" => "Distance - DESC","sql" => "ORDER BY distance DESC"));
1751
		
1752
		return $orderby;
1753
		
1754
	}
1755
    
1756
}
1757
?>