Completed
Push — master ( cc50d0...0f8c33 )
by Yannick
09:46
created

Tracker::addTrackerData()   F

Complexity

Conditions 24
Paths 6271

Size

Total Lines 107
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 53
nc 6271
nop 12
dl 0
loc 107
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

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

There are several approaches to avoid long parameter lists:

1
<?php
2
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 getSpotterDataByDate($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 countAllTrackedOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array())
756
	{
757
		global $globalDBdriver;
758
		//$filter_query = $this->getFilter($filters,true,true);
759
		$Connection= new Connection($this->db);
760
		if (!$Connection->tableExists('countries')) return array();
761
		require_once('class.SpotterLive.php');
762
		$SpotterLive = new SpotterLive();
763
		$filter_query = $SpotterLive->getFilter($filters,true,true);
764
		$filter_query .= ' over_country IS NOT NULL';
765
                if ($olderthanmonths > 0) {
766
			if ($globalDBdriver == 'mysql') {
767
				$filter_query .= ' AND spotter_live.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
768
			} else {
769
				$filter_query .= " AND spotter_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
770
			}
771
		}
772
                if ($sincedate != '') {
773
            		if ($globalDBdriver == 'mysql') {
774
				$filter_query .= " AND spotter_live.date > '".$sincedate."' ";
775
			} else {
776
				$filter_query .= " AND spotter_live.date > CAST('".$sincedate."' AS TIMESTAMP)";
777
			}
778
		}
779
		$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT flightaware_id,over_country FROM spotter_live".$filter_query.") l ON c.iso2 = l.over_country ";
780
		$query .= "GROUP BY c.name,c.iso3,c.iso2 ORDER BY nb DESC";
781
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
782
      
783
		
784
		$sth = $this->db->prepare($query);
785
		$sth->execute();
786
 
787
		$flight_array = array();
788
		$temp_array = array();
789
        
790
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
791
		{
792
			$temp_array['flight_count'] = $row['nb'];
793
			$temp_array['flight_country'] = $row['name'];
794
			$temp_array['flight_country_iso3'] = $row['iso3'];
795
			$temp_array['flight_country_iso2'] = $row['iso2'];
796
			$flight_array[] = $temp_array;
797
		}
798
		return $flight_array;
799
	}
800
*/	
801
	
802
	
803
	/**
804
	* Gets all callsigns that have flown over
805
	*
806
	* @return Array the callsign list
807
	*
808
	*/
809
	public function countAllCallsigns($limit = true, $olderthanmonths = 0, $sincedate = '',$filters = array(),$year = '', $month = '', $day = '')
810
	{
811
		global $globalDBdriver;
812
		$filter_query = $this->getFilter($filters,true,true);
813
		$query  = "SELECT DISTINCT tracker_output.ident, COUNT(tracker_output.ident) AS callsign_icao_count 
814
                    FROM tracker_output".$filter_query." tracker_output.ident <> ''";
815
		 if ($olderthanmonths > 0) {
816
			if ($globalDBdriver == 'mysql') $query .= ' AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH)';
817
			else $query .= " AND tracker_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
818
		}
819
		if ($sincedate != '') {
820
			if ($globalDBdriver == 'mysql') $query .= " AND tracker_output.date > '".$sincedate."'";
821
			else $query .= " AND tracker_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
822
		}
823
		$query_values = array();
824
		if ($year != '') {
825
			if ($globalDBdriver == 'mysql') {
826
				$query .= " AND YEAR(tracker_output.date) = :year";
827
				$query_values = array_merge($query_values,array(':year' => $year));
828
			} else {
829
				$query .= " AND EXTRACT(YEAR FROM tracker_output.date) = :year";
830
				$query_values = array_merge($query_values,array(':year' => $year));
831
			}
832
		}
833
		if ($month != '') {
834
			if ($globalDBdriver == 'mysql') {
835
				$query .= " AND MONTH(tracker_output.date) = :month";
836
				$query_values = array_merge($query_values,array(':month' => $month));
837
			} else {
838
				$query .= " AND EXTRACT(MONTH FROM tracker_output.date) = :month";
839
				$query_values = array_merge($query_values,array(':month' => $month));
840
			}
841
		}
842
		if ($day != '') {
843
			if ($globalDBdriver == 'mysql') {
844
				$query .= " AND DAY(tracker_output.date) = :day";
845
				$query_values = array_merge($query_values,array(':day' => $day));
846
			} else {
847
				$query .= " AND EXTRACT(DAY FROM tracker_output.date) = :day";
848
				$query_values = array_merge($query_values,array(':day' => $day));
849
			}
850
		}
851
		$query .= " GROUP BY tracker_output.ident ORDER BY callsign_icao_count DESC";
852
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
853
      		
854
		$sth = $this->db->prepare($query);
855
		$sth->execute($query_values);
856
      
857
		$callsign_array = array();
858
		$temp_array = array();
859
        
860
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
861
		{
862
			$temp_array['callsign_icao'] = $row['ident'];
863
			$temp_array['airline_name'] = $row['airline_name'];
864
			$temp_array['airline_icao'] = $row['airline_icao'];
865
			$temp_array['callsign_icao_count'] = $row['callsign_icao_count'];
866
          
867
			$callsign_array[] = $temp_array;
868
		}
869
870
		return $callsign_array;
871
	}
872
873
874
	/**
875
	* Counts all dates
876
	*
877
	* @return Array the date list
878
	*
879
	*/
880
	public function countAllDates($filters = array())
881
	{
882
		global $globalTimezone, $globalDBdriver;
883
		if ($globalTimezone != '') {
884
			date_default_timezone_set($globalTimezone);
885
			$datetime = new DateTime();
886
			$offset = $datetime->format('P');
887
		} else $offset = '+00:00';
888
889
		if ($globalDBdriver == 'mysql') {
890
			$query  = "SELECT DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
891
								FROM tracker_output";
892
			$query .= $this->getFilter($filters);
893
			$query .= " GROUP BY date_name 
894
								ORDER BY date_count DESC
895
								LIMIT 10 OFFSET 0";
896
		} else {
897
			$query  = "SELECT to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
898
								FROM tracker_output";
899
			$query .= $this->getFilter($filters);
900
			$query .= " GROUP BY date_name 
901
								ORDER BY date_count DESC
902
								LIMIT 10 OFFSET 0";
903
		}
904
      
905
		
906
		$sth = $this->db->prepare($query);
907
		$sth->execute(array(':offset' => $offset));
908
      
909
		$date_array = array();
910
		$temp_array = array();
911
        
912
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
913
		{
914
			$temp_array['date_name'] = $row['date_name'];
915
			$temp_array['date_count'] = $row['date_count'];
916
917
			$date_array[] = $temp_array;
918
		}
919
920
		return $date_array;
921
	}
922
	
923
	
924
	/**
925
	* Counts all dates during the last 7 days
926
	*
927
	* @return Array the date list
928
	*
929
	*/
930
	public function countAllDatesLast7Days($filters = array())
931
	{
932
		global $globalTimezone, $globalDBdriver;
933
		if ($globalTimezone != '') {
934
			date_default_timezone_set($globalTimezone);
935
			$datetime = new DateTime();
936
			$offset = $datetime->format('P');
937
		} else $offset = '+00:00';
938
		$filter_query = $this->getFilter($filters,true,true);
939
		if ($globalDBdriver == 'mysql') {
940
			$query  = "SELECT DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
941
								FROM tracker_output".$filter_query." tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY)";
942
			$query .= " GROUP BY date_name 
943
								ORDER BY tracker_output.date ASC";
944
			$query_data = array(':offset' => $offset);
945
		} else {
946
			$query  = "SELECT to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
947
								FROM tracker_output".$filter_query." tracker_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '7 DAYS'";
948
			$query .= " GROUP BY date_name 
949
								ORDER BY date_name ASC";
950
			$query_data = array(':offset' => $offset);
951
    		}
952
		
953
		$sth = $this->db->prepare($query);
954
		$sth->execute($query_data);
955
      
956
		$date_array = array();
957
		$temp_array = array();
958
        
959
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
960
		{
961
			$temp_array['date_name'] = $row['date_name'];
962
			$temp_array['date_count'] = $row['date_count'];
963
          
964
			$date_array[] = $temp_array;
965
		}
966
967
		return $date_array;
968
	}
969
970
	/**
971
	* Counts all dates during the last month
972
	*
973
	* @return Array the date list
974
	*
975
	*/
976
	public function countAllDatesLastMonth($filters = array())
977
	{
978
		global $globalTimezone, $globalDBdriver;
979
		if ($globalTimezone != '') {
980
			date_default_timezone_set($globalTimezone);
981
			$datetime = new DateTime();
982
			$offset = $datetime->format('P');
983
		} else $offset = '+00:00';
984
		$filter_query = $this->getFilter($filters,true,true);
985
		if ($globalDBdriver == 'mysql') {
986
			$query  = "SELECT DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
987
								FROM tracker_output".$filter_query." tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MONTH)";
988
			$query .= " GROUP BY date_name 
989
								ORDER BY tracker_output.date ASC";
990
			$query_data = array(':offset' => $offset);
991
		} else {
992
			$query  = "SELECT to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
993
								FROM tracker_output".$filter_query." tracker_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 MONTHS'";
994
			$query .= " GROUP BY date_name 
995
								ORDER BY date_name ASC";
996
			$query_data = array(':offset' => $offset);
997
    		}
998
		
999
		$sth = $this->db->prepare($query);
1000
		$sth->execute($query_data);
1001
      
1002
		$date_array = array();
1003
		$temp_array = array();
1004
        
1005
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1006
		{
1007
			$temp_array['date_name'] = $row['date_name'];
1008
			$temp_array['date_count'] = $row['date_count'];
1009
          
1010
			$date_array[] = $temp_array;
1011
		}
1012
1013
		return $date_array;
1014
	}
1015
1016
1017
1018
	/**
1019
	* Counts all month
1020
	*
1021
	* @return Array the month list
1022
	*
1023
	*/
1024
	public function countAllMonths($filters = array())
1025
	{
1026
		global $globalTimezone, $globalDBdriver;
1027
		if ($globalTimezone != '') {
1028
			date_default_timezone_set($globalTimezone);
1029
			$datetime = new DateTime();
1030
			$offset = $datetime->format('P');
1031
		} else $offset = '+00:00';
1032
1033
		if ($globalDBdriver == 'mysql') {
1034
			$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
1035
								FROM tracker_output";
1036
			$query .= $this->getFilter($filters);
1037
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1038
		} else {
1039
			$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
1040
								FROM tracker_output";
1041
			$query .= $this->getFilter($filters);
1042
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1043
		}
1044
      
1045
		
1046
		$sth = $this->db->prepare($query);
1047
		$sth->execute(array(':offset' => $offset));
1048
      
1049
		$date_array = array();
1050
		$temp_array = array();
1051
        
1052
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1053
		{
1054
			$temp_array['month_name'] = $row['month_name'];
1055
			$temp_array['year_name'] = $row['year_name'];
1056
			$temp_array['date_count'] = $row['date_count'];
1057
1058
			$date_array[] = $temp_array;
1059
		}
1060
1061
		return $date_array;
1062
	}
1063
1064
	
1065
	
1066
1067
	/**
1068
	* Counts all dates during the last year
1069
	*
1070
	* @return Array the date list
1071
	*
1072
	*/
1073
	public function countAllMonthsLastYear($filters)
1074
	{
1075
		global $globalTimezone, $globalDBdriver;
1076
		if ($globalTimezone != '') {
1077
			date_default_timezone_set($globalTimezone);
1078
			$datetime = new DateTime();
1079
			$offset = $datetime->format('P');
1080
		} else $offset = '+00:00';
1081
		$filter_query = $this->getFilter($filters,true,true);
1082
		if ($globalDBdriver == 'mysql') {
1083
			$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
1084
								FROM tracker_output".$filter_query." tracker_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)";
1085
			$query .= " GROUP BY year_name, month_name
1086
								ORDER BY year_name, month_name ASC";
1087
			$query_data = array(':offset' => $offset);
1088
		} else {
1089
			$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
1090
								FROM tracker_output".$filter_query." tracker_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'";
1091
			$query .= " GROUP BY year_name, month_name
1092
								ORDER BY year_name, month_name ASC";
1093
			$query_data = array(':offset' => $offset);
1094
    		}
1095
		
1096
		$sth = $this->db->prepare($query);
1097
		$sth->execute($query_data);
1098
      
1099
		$date_array = array();
1100
		$temp_array = array();
1101
        
1102
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1103
		{
1104
			$temp_array['year_name'] = $row['year_name'];
1105
			$temp_array['month_name'] = $row['month_name'];
1106
			$temp_array['date_count'] = $row['date_count'];
1107
          
1108
			$date_array[] = $temp_array;
1109
		}
1110
1111
		return $date_array;
1112
	}
1113
	
1114
	
1115
	
1116
	/**
1117
	* Counts all hours
1118
	*
1119
	* @return Array the hour list
1120
	*
1121
	*/
1122
	public function countAllHours($orderby,$filters = array())
1123
	{
1124
		global $globalTimezone, $globalDBdriver;
1125
		if ($globalTimezone != '') {
1126
			date_default_timezone_set($globalTimezone);
1127
			$datetime = new DateTime();
1128
			$offset = $datetime->format('P');
1129
		} else $offset = '+00:00';
1130
1131
		$orderby_sql = '';
1132
		if ($orderby == "hour")
1133
		{
1134
			$orderby_sql = "ORDER BY hour_name ASC";
1135
		}
1136
		if ($orderby == "count")
1137
		{
1138
			$orderby_sql = "ORDER BY hour_count DESC";
1139
		}
1140
		
1141
		if ($globalDBdriver == 'mysql') {
1142
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1143
								FROM tracker_output";
1144
			$query .= $this->getFilter($filters);
1145
			$query .= " GROUP BY hour_name 
1146
								".$orderby_sql;
1147
1148
/*		$query  = "SELECT HOUR(tracker_output.date) AS hour_name, count(*) as hour_count
1149
								FROM tracker_output 
1150
								GROUP BY hour_name 
1151
								".$orderby_sql."
1152
								LIMIT 10 OFFSET 00";
1153
  */    
1154
		$query_data = array(':offset' => $offset);
1155
		} else {
1156
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1157
								FROM tracker_output";
1158
			$query .= $this->getFilter($filters);
1159
			$query .= " GROUP BY hour_name 
1160
								".$orderby_sql;
1161
			$query_data = array(':offset' => $offset);
1162
		}
1163
		
1164
		$sth = $this->db->prepare($query);
1165
		$sth->execute($query_data);
1166
      
1167
		$hour_array = array();
1168
		$temp_array = array();
1169
        
1170
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1171
		{
1172
			$temp_array['hour_name'] = $row['hour_name'];
1173
			$temp_array['hour_count'] = $row['hour_count'];
1174
          
1175
			$hour_array[] = $temp_array;
1176
		}
1177
1178
		return $hour_array;
1179
	}
1180
	
1181
	
1182
	
1183
	/**
1184
	* Counts all hours by date
1185
	*
1186
	* @return Array the hour list
1187
	*
1188
	*/
1189
	public function countAllHoursByDate($date, $filters = array())
1190
	{
1191
		global $globalTimezone, $globalDBdriver;
1192
		$filter_query = $this->getFilter($filters,true,true);
1193
		$date = filter_var($date,FILTER_SANITIZE_STRING);
1194
		if ($globalTimezone != '') {
1195
			date_default_timezone_set($globalTimezone);
1196
			$datetime = new DateTime($date);
1197
			$offset = $datetime->format('P');
1198
		} else $offset = '+00:00';
1199
1200
		if ($globalDBdriver == 'mysql') {
1201
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1202
								FROM tracker_output".$filter_query." DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) = :date
1203
								GROUP BY hour_name 
1204
								ORDER BY hour_name ASC";
1205
		} else {
1206
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1207
								FROM tracker_output".$filter_query." to_char(tracker_output.date AT TIME ZONE INTERVAL :offset, 'YYYY-mm-dd') = :date
1208
								GROUP BY hour_name 
1209
								ORDER BY hour_name ASC";
1210
		}
1211
		
1212
		$sth = $this->db->prepare($query);
1213
		$sth->execute(array(':date' => $date, ':offset' => $offset));
1214
      
1215
		$hour_array = array();
1216
		$temp_array = array();
1217
        
1218
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1219
		{
1220
			$temp_array['hour_name'] = $row['hour_name'];
1221
			$temp_array['hour_count'] = $row['hour_count'];
1222
          
1223
			$hour_array[] = $temp_array;
1224
		}
1225
1226
		return $hour_array;
1227
	}
1228
	
1229
	
1230
	
1231
	/**
1232
	* Counts all hours by a ident/callsign
1233
	*
1234
	* @return Array the hour list
1235
	*
1236
	*/
1237
	public function countAllHoursByIdent($ident, $filters = array())
1238
	{
1239
		global $globalTimezone, $globalDBdriver;
1240
		$filter_query = $this->getFilter($filters,true,true);
1241
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1242
		if ($globalTimezone != '') {
1243
			date_default_timezone_set($globalTimezone);
1244
			$datetime = new DateTime();
1245
			$offset = $datetime->format('P');
1246
		} else $offset = '+00:00';
1247
1248
		if ($globalDBdriver == 'mysql') {
1249
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1250
								FROM tracker_output".$filter_query." tracker_output.ident = :ident 
1251
								GROUP BY hour_name 
1252
								ORDER BY hour_name ASC";
1253
		} else {
1254
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1255
								FROM tracker_output".$filter_query." tracker_output.ident = :ident 
1256
								GROUP BY hour_name 
1257
								ORDER BY hour_name ASC";
1258
		}
1259
      
1260
		
1261
		$sth = $this->db->prepare($query);
1262
		$sth->execute(array(':ident' => $ident,':offset' => $offset));
1263
      
1264
		$hour_array = array();
1265
		$temp_array = array();
1266
        
1267
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1268
		{
1269
			$temp_array['hour_name'] = $row['hour_name'];
1270
			$temp_array['hour_count'] = $row['hour_count'];
1271
          
1272
			$hour_array[] = $temp_array;
1273
		}
1274
1275
		return $hour_array;
1276
	}
1277
	
1278
	
1279
	
1280
	/**
1281
	* Counts all flights that have flown over
1282
	*
1283
	* @return Integer the number of flights
1284
	*
1285
	*/
1286
	public function countOverallTracked($filters = array(),$year = '',$month = '')
1287
	{
1288
		global $globalDBdriver;
1289
		$queryi  = "SELECT COUNT(tracker_output.tracker_id) AS flight_count FROM tracker_output";
1290
		$query_values = array();
1291
		$query = '';
1292
		if ($year != '') {
1293
			if ($globalDBdriver == 'mysql') {
1294
				$query .= " AND YEAR(tracker_output.date) = :year";
1295
				$query_values = array_merge($query_values,array(':year' => $year));
1296
			} else {
1297
				$query .= " AND EXTRACT(YEAR FROM tracker_output.date) = :year";
1298
				$query_values = array_merge($query_values,array(':year' => $year));
1299
			}
1300
		}
1301
		if ($month != '') {
1302
			if ($globalDBdriver == 'mysql') {
1303
				$query .= " AND MONTH(tracker_output.date) = :month";
1304
				$query_values = array_merge($query_values,array(':month' => $month));
1305
			} else {
1306
				$query .= " AND EXTRACT(MONTH FROM tracker_output.date) = :month";
1307
				$query_values = array_merge($query_values,array(':month' => $month));
1308
			}
1309
		}
1310
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1311
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1312
		
1313
		$sth = $this->db->prepare($queryi);
1314
		$sth->execute($query_values);
1315
		return $sth->fetchColumn();
1316
	}
1317
	
1318
  
1319
	/**
1320
	* Counts all hours of today
1321
	*
1322
	* @return Array the hour list
1323
	*
1324
	*/
1325
	public function countAllHoursFromToday($filters = array())
1326
	{
1327
		global $globalTimezone, $globalDBdriver;
1328
		$filter_query = $this->getFilter($filters,true,true);
1329
		if ($globalTimezone != '') {
1330
			date_default_timezone_set($globalTimezone);
1331
			$datetime = new DateTime();
1332
			$offset = $datetime->format('P');
1333
		} else $offset = '+00:00';
1334
1335
		if ($globalDBdriver == 'mysql') {
1336
			$query  = "SELECT HOUR(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1337
								FROM tracker_output".$filter_query." DATE(CONVERT_TZ(tracker_output.date,'+00:00', :offset)) = CURDATE()
1338
								GROUP BY hour_name 
1339
								ORDER BY hour_name ASC";
1340
		} else {
1341
			$query  = "SELECT EXTRACT(HOUR FROM tracker_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1342
								FROM tracker_output".$filter_query." to_char(tracker_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') = CAST(NOW() AS date)
1343
								GROUP BY hour_name 
1344
								ORDER BY hour_name ASC";
1345
		}
1346
		
1347
		$sth = $this->db->prepare($query);
1348
		$sth->execute(array(':offset' => $offset));
1349
      
1350
		$hour_array = array();
1351
		$temp_array = array();
1352
        
1353
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1354
		{
1355
			$temp_array['hour_name'] = $row['hour_name'];
1356
			$temp_array['hour_count'] = $row['hour_count'];
1357
			$hour_array[] = $temp_array;
1358
		}
1359
1360
		return $hour_array;
1361
	}
1362
    
1363
    
1364
     /**
1365
	* Gets the Barrie Spotter ID based on the FlightAware ID
1366
	*
1367
	* @return Integer the Barrie Spotter ID
1368
q	*
1369
	*/
1370
	public function getTrackerIDBasedOnFamTrackID($famtrackid)
1371
	{
1372
		$famtrackid = filter_var($famtrackid,FILTER_SANITIZE_STRING);
1373
1374
		$query  = "SELECT tracker_output.tracker_id
1375
				FROM tracker_output 
1376
				WHERE tracker_output.famtrackid = '".$famtrackid."'";
1377
        
1378
		
1379
		$sth = $this->db->prepare($query);
1380
		$sth->execute();
1381
1382
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1383
		{
1384
			return $row['tracker_id'];
1385
		}
1386
	}
1387
  
1388
 
1389
	/**
1390
	* Parses a date string
1391
	*
1392
	* @param String $dateString the date string
1393
	* @param String $timezone the timezone of a user
1394
	* @return Array the time information
1395
	*
1396
	*/
1397
	public function parseDateString($dateString, $timezone = '')
1398
	{
1399
		$time_array = array();
1400
	
1401
		if ($timezone != "")
1402
		{
1403
			date_default_timezone_set($timezone);
1404
		}
1405
		
1406
		$current_date = date("Y-m-d H:i:s");
1407
		$date = date("Y-m-d H:i:s",strtotime($dateString." UTC"));
1408
		
1409
		$diff = abs(strtotime($current_date) - strtotime($date));
1410
1411
		$time_array['years'] = floor($diff / (365*60*60*24)); 
1412
		$years = $time_array['years'];
1413
		
1414
		$time_array['months'] = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
1415
		$months = $time_array['months'];
1416
		
1417
		$time_array['days'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
1418
		$days = $time_array['days'];
1419
		$time_array['hours'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
1420
		$hours = $time_array['hours'];
1421
		$time_array['minutes'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
1422
		$minutes = $time_array['minutes'];
1423
		$time_array['seconds'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));  
1424
		
1425
		return $time_array;
1426
	}
1427
	
1428
	/**
1429
	* Parses the direction degrees to working
1430
	*
1431
	* @param Float $direction the direction in degrees
1432
	* @return Array the direction information
1433
	*
1434
	*/
1435
	public function parseDirection($direction = 0)
1436
	{
1437
		if ($direction == '') $direction = 0;
1438
		$direction_array = array();
1439
		$temp_array = array();
1440
1441
		if ($direction == 360 || ($direction >= 0 && $direction < 22.5))
1442
		{
1443
			$temp_array['direction_degree'] = $direction;
1444
			$temp_array['direction_shortname'] = "N";
1445
			$temp_array['direction_fullname'] = "North";
1446
		} elseif ($direction >= 22.5 && $direction < 45){
1447
			$temp_array['direction_degree'] = $direction;
1448
			$temp_array['direction_shortname'] = "NNE";
1449
			$temp_array['direction_fullname'] = "North-Northeast";
1450
		} elseif ($direction >= 45 && $direction < 67.5){
1451
			$temp_array['direction_degree'] = $direction;
1452
			$temp_array['direction_shortname'] = "NE";
1453
			$temp_array['direction_fullname'] = "Northeast";
1454
		} elseif ($direction >= 67.5 && $direction < 90){
1455
			$temp_array['direction_degree'] = $direction;
1456
			$temp_array['direction_shortname'] = "ENE";
1457
			$temp_array['direction_fullname'] = "East-Northeast";
1458
		} elseif ($direction >= 90 && $direction < 112.5){
1459
			$temp_array['direction_degree'] = $direction;
1460
			$temp_array['direction_shortname'] = "E";
1461
			$temp_array['direction_fullname'] = "East";
1462
		} elseif ($direction >= 112.5 && $direction < 135){
1463
			$temp_array['direction_degree'] = $direction;
1464
			$temp_array['direction_shortname'] = "ESE";
1465
			$temp_array['direction_fullname'] = "East-Southeast";
1466
		} elseif ($direction >= 135 && $direction < 157.5){
1467
			$temp_array['direction_degree'] = $direction;
1468
			$temp_array['direction_shortname'] = "SE";
1469
			$temp_array['direction_fullname'] = "Southeast";
1470
		} elseif ($direction >= 157.5 && $direction < 180){
1471
			$temp_array['direction_degree'] = $direction;
1472
			$temp_array['direction_shortname'] = "SSE";
1473
			$temp_array['direction_fullname'] = "South-Southeast";
1474
		} elseif ($direction >= 180 && $direction < 202.5){
1475
			$temp_array['direction_degree'] = $direction;
1476
			$temp_array['direction_shortname'] = "S";
1477
			$temp_array['direction_fullname'] = "South";
1478
		} elseif ($direction >= 202.5 && $direction < 225){
1479
			$temp_array['direction_degree'] = $direction;
1480
			$temp_array['direction_shortname'] = "SSW";
1481
			$temp_array['direction_fullname'] = "South-Southwest";
1482
		} elseif ($direction >= 225 && $direction < 247.5){
1483
			$temp_array['direction_degree'] = $direction;
1484
			$temp_array['direction_shortname'] = "SW";
1485
			$temp_array['direction_fullname'] = "Southwest";
1486
		} elseif ($direction >= 247.5 && $direction < 270){
1487
			$temp_array['direction_degree'] = $direction;
1488
			$temp_array['direction_shortname'] = "WSW";
1489
			$temp_array['direction_fullname'] = "West-Southwest";
1490
		} elseif ($direction >= 270 && $direction < 292.5){
1491
			$temp_array['direction_degree'] = $direction;
1492
			$temp_array['direction_shortname'] = "W";
1493
			$temp_array['direction_fullname'] = "West";
1494
		} elseif ($direction >= 292.5 && $direction < 315){
1495
			$temp_array['direction_degree'] = $direction;
1496
			$temp_array['direction_shortname'] = "WNW";
1497
			$temp_array['direction_fullname'] = "West-Northwest";
1498
		} elseif ($direction >= 315 && $direction < 337.5){
1499
			$temp_array['direction_degree'] = $direction;
1500
			$temp_array['direction_shortname'] = "NW";
1501
			$temp_array['direction_fullname'] = "Northwest";
1502
		} elseif ($direction >= 337.5 && $direction < 360){
1503
			$temp_array['direction_degree'] = $direction;
1504
			$temp_array['direction_shortname'] = "NNW";
1505
			$temp_array['direction_fullname'] = "North-Northwest";
1506
		}
1507
		$direction_array[] = $temp_array;
1508
		return $direction_array;
1509
	}
1510
	
1511
	
1512
	/**
1513
	* Gets Country from latitude/longitude
1514
	*
1515
	* @param Float $latitude latitute of the flight
1516
	* @param Float $longitude longitute of the flight
1517
	* @return String the countrie
1518
	*/
1519
	public function getCountryFromLatitudeLongitude($latitude,$longitude)
1520
	{
1521
		global $globalDBdriver, $globalDebug;
1522
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1523
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1524
	
1525
		$Connection = new Connection($this->db);
1526
		if (!$Connection->tableExists('countries')) return '';
1527
	
1528
		try {
1529
			/*
1530
			if ($globalDBdriver == 'mysql') {
1531
				//$query  = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(:latitude :longitude)'), ogc_geom) LIMIT 1";
1532
				$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(".$longitude.' '.$latitude.")'), ogc_geom) LIMIT 1";
1533
			}
1534
			*/
1535
			// This query seems to work both for MariaDB and PostgreSQL
1536
			$query = "SELECT name,iso2,iso3 FROM countries WHERE ST_Within(ST_GeomFromText('POINT(".$longitude." ".$latitude.")',4326), ogc_geom) LIMIT 1";
1537
		
1538
			$sth = $this->db->prepare($query);
1539
			//$sth->execute(array(':latitude' => $latitude,':longitude' => $longitude));
1540
			$sth->execute();
1541
    
1542
			$row = $sth->fetch(PDO::FETCH_ASSOC);
1543
			$sth->closeCursor();
1544
			if (count($row) > 0) {
1545
				return $row;
1546
			} else return '';
1547
		} catch (PDOException $e) {
1548
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
1549
			return '';
1550
		}
1551
	
1552
	}
1553
1554
	/**
1555
	* Gets Country from iso2
1556
	*
1557
	* @param String $iso2 ISO2 country code
1558
	* @return String the countrie
1559
	*/
1560
	public function getCountryFromISO2($iso2)
1561
	{
1562
		global $globalDBdriver, $globalDebug;
1563
		$iso2 = filter_var($iso2,FILTER_SANITIZE_STRING);
1564
	
1565
		$Connection = new Connection($this->db);
1566
		if (!$Connection->tableExists('countries')) return '';
1567
	
1568
		try {
1569
			$query = "SELECT name,iso2,iso3 FROM countries WHERE iso2 = :iso2 LIMIT 1";
1570
		
1571
			$sth = $this->db->prepare($query);
1572
			$sth->execute(array(':iso2' => $iso2));
1573
    
1574
			$row = $sth->fetch(PDO::FETCH_ASSOC);
1575
			$sth->closeCursor();
1576
			if (count($row) > 0) {
1577
				return $row;
1578
			} else return '';
1579
		} catch (PDOException $e) {
1580
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
1581
			return '';
1582
		}
1583
	
1584
	}
1585
1586
	
1587
	/**
1588
	* Gets the short url from bit.ly
1589
	*
1590
	* @param String $url the full url
1591
	* @return String the bit.ly url
1592
	*
1593
	*/
1594
	public function getBitlyURL($url)
1595
	{
1596
		global $globalBitlyAccessToken;
1597
		
1598
		if ($globalBitlyAccessToken == '') return $url;
1599
        
1600
		$google_url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$globalBitlyAccessToken.'&longUrl='.$url;
1601
		
1602
		$ch = curl_init();
1603
		curl_setopt($ch, CURLOPT_HEADER, 0);
1604
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1605
		curl_setopt($ch, CURLOPT_URL, $google_url);
1606
		$bitly_data = curl_exec($ch);
1607
		curl_close($ch);
1608
		
1609
		$bitly_data = json_decode($bitly_data);
1610
		$bitly_url = '';
1611
		if ($bitly_data->status_txt = "OK"){
1612
			$bitly_url = $bitly_data->data->url;
1613
		}
1614
1615
		return $bitly_url;
1616
	}
1617
1618
1619
	public function getOrderBy()
1620
	{
1621
		$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"));
1622
		
1623
		return $orderby;
1624
		
1625
	}
1626
    
1627
}
1628
?>