Completed
Push — master ( e2bc06...df777b )
by Yannick
29:51
created

Marine::getMarineDataByCaptain()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 12
nop 4
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/class.Image.php');
3
$global_marine_query = "SELECT marine_output.* FROM marine_output";
4
5
class Marine{
6
	public $db;
7
	
8
	public function __construct($dbc = null) {
9
		$Connection = new Connection($dbc);
10
		$this->db = $Connection->db();
11
		if ($this->db === null) die('Error: No DB connection. (Marine)');
12
	}
13
14
	/**
15
	* Get SQL query part for filter used
16
	* @param Array $filter the filter
17
	* @return Array the SQL part
18
	*/
19
	
20
	public function getFilter($filter = array(),$where = false,$and = false) {
21
		global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver;
22
		$filters = array();
23
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
24
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
25
				$filters = $globalStatsFilters[$globalFilterName];
26
			} else {
27
				$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]);
28
			}
29
		}
30
		if (isset($filter[0]['source'])) {
31
			$filters = array_merge($filters,$filter);
32
		}
33
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
34
		$filter_query_join = '';
35
		$filter_query_where = '';
36
		foreach($filters as $flt) {
37
			if (isset($flt['idents']) && !empty($flt['idents'])) {
38
				if (isset($flt['source'])) {
39
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) spfi ON spfi.fammarine_id = marine_output.fammarine_id";
40
				} else {
41
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."')) spfi ON spfi.fammarine_id = marine_output.fammarine_id";
42
				}
43
			}
44
		}
45
		if (isset($filter['source']) && !empty($filter['source'])) {
46
			$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
47
		}
48
		if (isset($filter['ident']) && !empty($filter['ident'])) {
49
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
50
		}
51
		if (isset($filter['id']) && !empty($filter['id'])) {
52
			$filter_query_where .= " AND fammarine_id = '".$filter['id']."'";
53
		}
54
		if (isset($filter['mmsi']) && !empty($filter['mmsi'])) {
55
			$filter_query_where .= " AND mmsi = '".$filter['mmsi']."'";
56
		}
57
		if (isset($filter['race']) && !empty($filter['race'])) {
58
			$filter_query_where .= " AND race_id = '".$filter['race']."'";
59
		}
60
		if (isset($filter['year']) && $filter['year'] != '') {
61
			if ($globalDBdriver == 'mysql') {
62
				$filter_query_where .= " AND YEAR(marine_output.date) = '".$filter['year']."'";
63
			} else {
64
				$filter_query_where .= " AND EXTRACT(YEAR FROM marine_output.date) = '".$filter['year']."'";
65
			}
66
		}
67
		if (isset($filter['month']) && $filter['month'] != '') {
68
			if ($globalDBdriver == 'mysql') {
69
				$filter_query_where .= " AND MONTH(marine_output.date) = '".$filter['month']."'";
70
			} else {
71
				$filter_query_where .= " AND EXTRACT(MONTH FROM marine_output.date) = '".$filter['month']."'";
72
			}
73
		}
74
		if (isset($filter['day']) && $filter['day'] != '') {
75
			if ($globalDBdriver == 'mysql') {
76
				$filter_query_where .= " AND DAY(marine_output.date) = '".$filter['day']."'";
77
			} else {
78
				$filter_query_where .= " AND EXTRACT(DAY FROM marine_output.date) = '".$filter['day']."'";
79
			}
80
		}
81
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
82
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
83
		if ($filter_query_where != '') {
84
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
85
		}
86
		$filter_query = $filter_query_join.$filter_query_where;
87
		return $filter_query;
88
	}
89
90
	/**
91
	* Executes the SQL statements to get the spotter information
92
	*
93
	* @param String $query the SQL query
94
	* @param Array $params parameter of the query
95
	* @param String $limitQuery the limit query
96
	* @return Array the spotter information
97
	*
98
	*/
99
	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...
100
	{
101
		date_default_timezone_set('UTC');
102
		if (!is_string($query))
103
		{
104
			return false;
105
		}
106
		
107
		if ($limitQuery != "")
108
		{
109
			if (!is_string($limitQuery))
110
			{
111
				return false;
112
			}
113
		}
114
115
		try {
116
			$sth = $this->db->prepare($query.$limitQuery);
117
			$sth->execute($params);
118
		} catch (PDOException $e) {
119
			printf("Invalid query : %s\nWhole query: %s\n",$e->getMessage(), $query.$limitQuery);
120
			exit();
121
		}
122
		
123
		$num_rows = 0;
124
		$spotter_array = array();
125
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
126
		{
127
			$num_rows++;
128
			$temp_array = array();
129
			if (isset($row['marine_live_id'])) {
130
				$temp_array['marine_id'] = $this->getMarineIDBasedOnFamMarineID($row['fammarine_id']);
131
			/*
132
			} elseif (isset($row['spotter_archive_id'])) {
133
				$temp_array['spotter_id'] = $row['spotter_archive_id'];
134
			} elseif (isset($row['spotter_archive_output_id'])) {
135
				$temp_array['spotter_id'] = $row['spotter_archive_output_id'];
136
			*/} 
137
			elseif (isset($row['marineid'])) {
138
				$temp_array['marine_id'] = $row['marineid'];
139
			} else {
140
				$temp_array['marine_id'] = '';
141
			}
142
			if (isset($row['fammarine_id'])) $temp_array['fammarine_id'] = $row['fammarine_id'];
143
			if (isset($row['mmsi'])) $temp_array['mmsi'] = $row['mmsi'];
144
			if (isset($row['type'])) $temp_array['type'] = $row['type'];
145
			if (isset($row['type_id'])) $temp_array['type_id'] = $row['type_id'];
146
			if (isset($row['status'])) $temp_array['status'] = $row['status'];
147
			if (isset($row['status_id'])) $temp_array['status_id'] = $row['status_id'];
148
			if (isset($row['captain_id'])) $temp_array['captain_id'] = $row['captain_id'];
149
			if (isset($row['captain_name'])) $temp_array['captain_name'] = $row['captain_name'];
150
			if (isset($row['race_id'])) $temp_array['race_id'] = $row['race_id'];
151
			if (isset($row['race_name'])) $temp_array['race_name'] = $row['race_name'];
152
			if (isset($row['ident'])) $temp_array['ident'] = $row['ident'];
153
			if (isset($row['arrival_port_name'])) $temp_array['arrival_port_name'] = $row['arrival_port_name'];
154
			if (isset($row['latitude'])) $temp_array['latitude'] = $row['latitude'];
155
			if (isset($row['longitude'])) $temp_array['longitude'] = $row['longitude'];
156
			if (isset($row['format_source'])) $temp_array['format_source'] = $row['format_source'];
157
			if (isset($row['heading'])) {
158
				$temp_array['heading'] = $row['heading'];
159
				$heading_direction = $this->parseDirection($row['heading']);
160
				if (isset($heading_direction[0]['direction_fullname'])) $temp_array['heading_name'] = $heading_direction[0]['direction_fullname'];
161
			}
162
			if (isset($row['ground_speed'])) $temp_array['ground_speed'] = $row['ground_speed'];
163
164
			if(isset($temp_array['mmsi']) && $temp_array['mmsi'] != "")
165
			{
166
				$Image = new Image($this->db);
167
				if (isset($temp_array['ident']) && $temp_array['ident'] != '') $image_array = $Image->getMarineImage($temp_array['mmsi'],'',$temp_array['ident']);
168
				else $image_array = $Image->getMarineImage($temp_array['mmsi']);
169
				unset($Image);
170
				if (count($image_array) > 0) {
171
					$temp_array['image'] = $image_array[0]['image'];
172
					$temp_array['image_thumbnail'] = $image_array[0]['image_thumbnail'];
173
					$temp_array['image_source'] = $image_array[0]['image_source'];
174
					$temp_array['image_source_website'] = $image_array[0]['image_source_website'];
175
					$temp_array['image_copyright'] = $image_array[0]['image_copyright'];
176
				}
177
			}
178
			
179
			if (isset($row['date'])) {
180
				$dateArray = $this->parseDateString($row['date']);
181
				if ($dateArray['seconds'] < 10)
182
				{
183
					$temp_array['date'] = "a few seconds ago";
184
				} elseif ($dateArray['seconds'] >= 5 && $dateArray['seconds'] < 30)
185
				{
186
					$temp_array['date'] = "half a minute ago";
187
				} elseif ($dateArray['seconds'] >= 30 && $dateArray['seconds'] < 60)
188
				{
189
					$temp_array['date'] = "about a minute ago";
190
				} elseif ($dateArray['minutes'] < 5)
191
				{
192
					$temp_array['date'] = "a few minutes ago";
193
				} elseif ($dateArray['minutes'] >= 5 && $dateArray['minutes'] < 60)
194
				{
195
					$temp_array['date'] = "about ".$dateArray['minutes']." minutes ago";
196
				} elseif ($dateArray['hours'] < 2)
197
				{
198
					$temp_array['date'] = "about an hour ago";
199
				} elseif ($dateArray['hours'] >= 2 && $dateArray['hours'] < 24)
200
				{
201
					$temp_array['date'] = "about ".$dateArray['hours']." hours ago";
202
				} else {
203
					$temp_array['date'] = date("M j Y, g:i a",strtotime($row['date']." UTC"));
204
				}
205
				$temp_array['date_minutes_past'] = $dateArray['minutes'];
206
				$temp_array['date_iso_8601'] = date("c",strtotime($row['date']." UTC"));
207
				$temp_array['date_rfc_2822'] = date("r",strtotime($row['date']." UTC"));
208
				$temp_array['date_unix'] = strtotime($row['date']." UTC");
209
				if (isset($row['last_seen']) && $row['last_seen'] != '') {
210
					if (strtotime($row['last_seen']) > strtotime($row['date'])) {
211
						$temp_array['duration'] = strtotime($row['last_seen']) - strtotime($row['date']);
212
						$temp_array['last_seen_date_iso_8601'] = date("c",strtotime($row['last_seen']." UTC"));
213
						$temp_array['last_seen_date_rfc_2822'] = date("r",strtotime($row['last_seen']." UTC"));
214
						$temp_array['last_seen_date_unix'] = strtotime($row['last_seen']." UTC");
215
					}
216
				}
217
			}
218
			
219
			$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...
220
			if (isset($row['source_name']) && $row['source_name'] != '') $temp_array['source_name'] = $row['source_name'];
221
			if (isset($row['over_country']) && $row['over_country'] != '') $temp_array['over_country'] = $row['over_country'];
222
			if (isset($row['distance']) && $row['distance'] != '') $temp_array['distance'] = $row['distance'];
223
			$temp_array['query_number_rows'] = $num_rows;
224
			$spotter_array[] = $temp_array;
225
		}
226
		if ($num_rows == 0) return array();
227
		$spotter_array[0]['query_number_rows'] = $num_rows;
228
		return $spotter_array;
229
	}	
230
	
231
	
232
	/**
233
	* Gets all the spotter information based on the latest data entry
234
	*
235
	* @return Array the spotter information
236
	*
237
	*/
238
	public function getLatestMarineData($limit = '', $sort = '', $filter = array())
239
	{
240
		global $global_marine_query;
241
		date_default_timezone_set('UTC');
242
		$filter_query = $this->getFilter($filter);
243
		if ($limit != "")
244
		{
245
			$limit_array = explode(",", $limit);
246
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
247
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
248
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
249
			{
250
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
251
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
252
			} else $limit_query = "";
253
		} else $limit_query = "";
254
		if ($sort != "")
255
		{
256
			$search_orderby_array = $this->getOrderBy();
257
			$orderby_query = $search_orderby_array[$sort]['sql'];
258
		} else {
259
			$orderby_query = " ORDER BY marine_output.date DESC";
260
		}
261
		$query  = $global_marine_query.$filter_query." ".$orderby_query;
262
		$spotter_array = $this->getDataFromDB($query, array(),$limit_query,true);
263
		return $spotter_array;
264
	}
265
    
266
	/*
267
	* Gets all the spotter information based on the spotter id
268
	*
269
	* @return Array the spotter information
270
	*
271
	*/
272
	public function getMarineDataByID($id = '')
273
	{
274
		global $global_marine_query;
275
		
276
		date_default_timezone_set('UTC');
277
		if ($id == '') return array();
278
		$additional_query = "marine_output.fammarine_id = :id";
279
		$query_values = array(':id' => $id);
280
		$query  = $global_marine_query." WHERE ".$additional_query." ";
281
		$spotter_array = $this->getDataFromDB($query,$query_values);
282
		return $spotter_array;
283
	}
284
285
	/**
286
	* Gets all the spotter information based on the callsign
287
	*
288
	* @return Array the spotter information
289
	*
290
	*/
291
	public function getMarineDataByIdent($ident = '', $limit = '', $sort = '', $filter = array())
292
	{
293
		global $global_marine_query;
294
		
295
		date_default_timezone_set('UTC');
296
		
297
		$query_values = array();
298
		$limit_query = '';
299
		$additional_query = '';
300
		$filter_query = $this->getFilter($filter,true,true);
301
		if ($ident != "")
302
		{
303
			if (!is_string($ident))
304
			{
305
				return false;
306
			} else {
307
				$additional_query = " AND (marine_output.ident = :ident)";
308
				$query_values = array(':ident' => $ident);
309
			}
310
		}
311
		
312
		if ($limit != "")
313
		{
314
			$limit_array = explode(",", $limit);
315
			
316
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
317
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
318
			
319
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
320
			{
321
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
322
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
323
			}
324
		}
325
326
		if ($sort != "")
327
		{
328
			$search_orderby_array = $this->getOrderBy();
329
			$orderby_query = $search_orderby_array[$sort]['sql'];
330
		} else {
331
			$orderby_query = " ORDER BY marine_output.date DESC";
332
		}
333
334
		$query = $global_marine_query.$filter_query." marine_output.ident <> '' ".$additional_query." ".$orderby_query;
335
		//echo $query."\n";
336
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
337
338
		return $spotter_array;
339
	}
340
341
	/**
342
	* Gets all the marine information based on the type
343
	*
344
	* @return Array the marine information
345
	*
346
	*/
347
	public function getMarineDataByType($type = '', $limit = '', $sort = '', $filter = array())
348
	{
349
		global $global_marine_query;
350
		
351
		date_default_timezone_set('UTC');
352
		
353
		$query_values = array();
0 ignored issues
show
Unused Code introduced by
$query_values 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...
354
		$limit_query = '';
355
		$additional_query = '';
0 ignored issues
show
Unused Code introduced by
$additional_query 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...
356
		$filter_query = $this->getFilter($filter,true,true);
357
		if (!is_string($type))
358
		{
359
			return false;
360
		} else {
361
			$additional_query = " AND marine_output.type_id = :type";
362
			$query_values = array(':type' => $type);
363
		}
364
		
365
		if ($limit != "")
366
		{
367
			$limit_array = explode(",", $limit);
368
			
369
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
370
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
371
			
372
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
373
			{
374
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
375
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
376
			}
377
		}
378
379
		if ($sort != "")
380
		{
381
			$search_orderby_array = $this->getOrderBy();
382
			$orderby_query = $search_orderby_array[$sort]['sql'];
383
		} else {
384
			$orderby_query = " ORDER BY marine_output.date DESC";
385
		}
386
387
		$query = $global_marine_query.$filter_query." marine_output.type <> '' ".$additional_query." ".$orderby_query;
388
		//echo $query."\n";
389
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
390
391
		return $spotter_array;
392
	}
393
	
394
	public function getMarineDataByDate($date = '', $limit = '', $sort = '',$filter = array())
395
	{
396
		global $global_marine_query, $globalTimezone, $globalDBdriver;
397
		
398
		$query_values = array();
399
		$limit_query = '';
400
		$additional_query = '';
401
402
		$filter_query = $this->getFilter($filter,true,true);
403
		
404
		if ($date != "")
405
		{
406
			if ($globalTimezone != '') {
407
				date_default_timezone_set($globalTimezone);
408
				$datetime = new DateTime($date);
409
				$offset = $datetime->format('P');
410
			} else {
411
				date_default_timezone_set('UTC');
412
				$datetime = new DateTime($date);
413
				$offset = '+00:00';
414
			}
415
			if ($globalDBdriver == 'mysql') {
416
				$additional_query = " AND DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date ";
417
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':offset' => $offset);
418
			} elseif ($globalDBdriver == 'pgsql') {
419
				$additional_query = " AND to_char(marine_output.date AT TIME ZONE :timezone,'YYYY-mm-dd') = :date ";
420
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':timezone' => $globalTimezone);
421
			}
422
		}
423
		
424
		if ($limit != "")
425
		{
426
			$limit_array = explode(",", $limit);
427
			
428
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
429
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
430
			
431
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
432
			{
433
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
434
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
435
			}
436
		}
437
438
		if ($sort != "")
439
		{
440
			$search_orderby_array = $this->getOrderBy();
441
			$orderby_query = $search_orderby_array[$sort]['sql'];
442
		} else {
443
			$orderby_query = " ORDER BY marine_output.date DESC";
444
		}
445
446
		$query = $global_marine_query.$filter_query." marine_output.ident <> '' ".$additional_query.$orderby_query;
447
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
448
		return $spotter_array;
449
	}
450
451
	/**
452
	* Gets all the marine information based on the captain
453
	*
454
	* @return Array the marine information
455
	*
456
	*/
457
	public function getMarineDataByCaptain($captain = '', $limit = '', $sort = '', $filter = array())
458
	{
459
		global $global_marine_query;
460
		date_default_timezone_set('UTC');
461
		$query_values = array();
462
		$limit_query = '';
463
		$additional_query = '';
464
		$filter_query = $this->getFilter($filter,true,true);
465
		if ($captain != "")
466
		{
467
			$additional_query = " AND (marine_output.captain_name = :captain OR marine_output.captain_id = :captain)";
468
			$query_values = array(':captain' => $captain);
469
		}
470
		if ($limit != "")
471
		{
472
			$limit_array = explode(",", $limit);
473
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
474
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
475
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
476
			{
477
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
478
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
479
			}
480
		}
481
		if ($sort != "")
482
		{
483
			$search_orderby_array = $this->getOrderBy();
484
			$orderby_query = $search_orderby_array[$sort]['sql'];
485
		} else {
486
			$orderby_query = " ORDER BY marine_output.date DESC";
487
		}
488
		$query = $global_marine_query.$filter_query." marine_output.captain_name <> '' ".$additional_query." ".$orderby_query;
489
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
490
		return $spotter_array;
491
	}
492
493
	/**
494
	* Gets all source name
495
	*
496
	* @param String type format of source
497
	* @return Array list of source name
498
	*
499
	*/
500
	public function getAllSourceName($type = '',$filters = array())
501
	{
502
		$filter_query = $this->getFilter($filters,true,true);
503
		$query_values = array();
504
		$query  = "SELECT DISTINCT marine_output.source_name 
505
				FROM marine_output".$filter_query." marine_output.source_name <> ''";
506
		if ($type != '') {
507
			$query_values = array(':type' => $type);
508
			$query .= " AND format_source = :type";
509
		}
510
		$query .= " ORDER BY marine_output.source_name ASC";
511
512
		$sth = $this->db->prepare($query);
513
		if (!empty($query_values)) $sth->execute($query_values);
514
		else $sth->execute();
515
516
		$source_array = array();
517
		$temp_array = array();
518
		
519
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
520
		{
521
			$temp_array['source_name'] = $row['source_name'];
522
			$source_array[] = $temp_array;
523
		}
524
		return $source_array;
525
	}
526
527
528
	/**
529
	* Gets a list of all idents/callsigns
530
	*
531
	* @return Array list of ident/callsign names
532
	*
533
	*/
534
	public function getAllIdents($filters = array())
535
	{
536
		$filter_query = $this->getFilter($filters,true,true);
537
		$query  = "SELECT DISTINCT marine_output.ident
538
								FROM marine_output".$filter_query." marine_output.ident <> '' 
539
								ORDER BY marine_output.date ASC LIMIT 700 OFFSET 0";
540
541
		$sth = $this->db->prepare($query);
542
		$sth->execute();
543
    
544
		$ident_array = array();
545
		$temp_array = array();
546
		
547
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
548
		{
549
			$temp_array['ident'] = $row['ident'];
550
			$ident_array[] = $temp_array;
551
		}
552
553
		return $ident_array;
554
	}
555
556
	/**
557
	* Gets all info from a mmsi
558
	*
559
	* @return Array ident
560
	*
561
	*/
562
	public function getIdentity($mmsi)
563
	{
564
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT);
565
		$query  = "SELECT * FROM marine_identity WHERE mmsi = :mmsi LIMIT 1";
566
		$sth = $this->db->prepare($query);
567
		$sth->execute(array(':mmsi' => $mmsi));
568
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
569
		if (isset($result[0])) return $result[0];
570
		else return array();
571
	}
572
573
	/**
574
	* Add identity
575
	*
576
	*/
577
	public function addIdentity($mmsi,$imo,$ident,$callsign,$type)
578
	{
579
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT);
580
		if ($mmsi != '') {
581
			$imo = filter_var($imo,FILTER_SANITIZE_NUMBER_INT);
582
			$ident = filter_var($ident,FILTER_SANITIZE_STRING);
583
			$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
584
			$type = filter_var($type,FILTER_SANITIZE_STRING);
585
			$identinfo = $this->getIdentity($mmsi);
586
			if (empty($identinfo)) {
587
				$query  = "INSERT INTO marine_identity (mmsi,imo,call_sign,ship_name,type) VALUES (:mmsi,:imo,:call_sign,:ship_name,:type)";
588
				$sth = $this->db->prepare($query);
589
				$sth->execute(array(':mmsi' => $mmsi,':imo' => $imo,':call_sign' => $callsign,':ship_name' => $ident,':type' => $type));
590
			} elseif ($ident != '' && $identinfo['ship_name'] != $ident) {
591
				$query  = "UPDATE marine_identity SET ship_name = :ship_name,type = :type WHERE mmsi = :mmsi";
592
				$sth = $this->db->prepare($query);
593
				$sth->execute(array(':mmsi' => $mmsi,':ship_name' => $ident,':type' => $type));
594
			}
595
		}
596
	}
597
598
	/*
599
	* Gets a list of all dates
600
	*
601
	* @return Array list of date names
602
	*
603
	*/
604
	public function getAllDates()
605
	{
606
		global $globalTimezone, $globalDBdriver;
607
		if ($globalTimezone != '') {
608
			date_default_timezone_set($globalTimezone);
609
			$datetime = new DateTime();
610
			$offset = $datetime->format('P');
611
		} else $offset = '+00:00';
612
613
		if ($globalDBdriver == 'mysql') {
614
			$query  = "SELECT DISTINCT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) as date
615
								FROM marine_output
616
								WHERE marine_output.date <> '' 
617
								ORDER BY marine_output.date ASC LIMIT 0,100";
618
		} else {
619
			$query  = "SELECT DISTINCT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') as date
620
								FROM marine_output
621
								WHERE marine_output.date <> '' 
622
								ORDER BY marine_output.date ASC LIMIT 0,100";
623
		}
624
		
625
		$sth = $this->db->prepare($query);
626
		$sth->execute(array(':offset' => $offset));
627
    
628
		$date_array = array();
629
		$temp_array = array();
630
		
631
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
632
		{
633
			$temp_array['date'] = $row['date'];
634
635
			$date_array[] = $temp_array;
636
		}
637
638
		return $date_array;
639
	}
640
641
	/**
642
	* Update ident tracker data
643
	*
644
	* @param String $fammarine_id the ID
645
	* @param String $ident the marine ident
646
	* @return String success or false
647
	*
648
	*/
649
	public function updateIdentMarineData($fammarine_id = '', $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...
650
	{
651
		$query = 'UPDATE marine_output SET ident = :ident WHERE fammarine_id = :fammarine_id';
652
		$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident);
653
		try {
654
			$sth = $this->db->prepare($query);
655
			$sth->execute($query_values);
656
		} catch (PDOException $e) {
657
			return "error : ".$e->getMessage();
658
		}
659
		return "success";
660
	}
661
662
	/**
663
	* Update arrival marine data
664
	*
665
	* @param String $fammarine_id the ID
666
	* @param String $arrival_code the marine ident
667
	* @return String success or false
668
	*
669
	*/
670
	public function updateArrivalPortNameMarineData($fammarine_id = '', $arrival_code = '',$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...
671
	{
672
		$query = 'UPDATE marine_output SET arrival_port_name = :arrival_code WHERE fammarine_id = :fammarine_id';
673
		$query_values = array(':fammarine_id' => $fammarine_id,':arrival_code' => $arrival_code);
674
		try {
675
			$sth = $this->db->prepare($query);
676
			$sth->execute($query_values);
677
		} catch (PDOException $e) {
678
			return "error : ".$e->getMessage();
679
		}
680
		return "success";
681
	}
682
683
	/**
684
	* Update Status data
685
	*
686
	* @param String $fammarine_id the ID
687
	* @param String $status_id the marine status id
688
	* @param String $status the marine status
689
	* @return String success or false
690
	*
691
	*/
692
	public function updateStatusMarineData($fammarine_id = '', $status_id = '',$status = '')
693
	{
694
695
		$query = 'UPDATE marine_output SET status = :status, status_id = :status_id WHERE fammarine_id = :fammarine_id';
696
                $query_values = array(':fammarine_id' => $fammarine_id,':status' => $status,':status_id' => $status_id);
697
698
		try {
699
			$sth = $this->db->prepare($query);
700
			$sth->execute($query_values);
701
		} catch (PDOException $e) {
702
			return "error : ".$e->getMessage();
703
		}
704
		
705
		return "success";
706
707
	}
708
	/**
709
	* Update latest marine data
710
	*
711
	* @param String $fammarine_id the ID
712
	* @param String $ident the marine ident
713
	* @return String success or false
714
	*
715
	*/	
716
	public function updateLatestMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $groundspeed = NULL, $date = '')
717
	{
718
		$query = 'UPDATE marine_output SET ident = :ident, last_latitude = :last_latitude, last_longitude = :last_longitude, last_seen = :last_seen, last_ground_speed = :last_ground_speed WHERE fammarine_id = :fammarine_id';
719
                $query_values = array(':fammarine_id' => $fammarine_id,':last_latitude' => $latitude,':last_longitude' => $longitude, ':last_ground_speed' => $groundspeed,':last_seen' => $date,':ident' => $ident);
720
721
		try {
722
			$sth = $this->db->prepare($query);
723
			$sth->execute($query_values);
724
		} catch (PDOException $e) {
725
			return "error : ".$e->getMessage();
726
		}
727
		
728
		return "success";
729
730
	}
731
732
	/**
733
	* Adds a new spotter data
734
	*
735
	* @param String $fammarine_id the ID
736
	* @param String $ident the marine ident
737
	* @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...
738
	* @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...
739
	* @param String $latitude latitude of flight
740
	* @param String $longitude latitude of flight
741
	* @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...
742
	* @param String $heading heading of flight
743
	* @param String $groundspeed speed of flight
744
	* @param String $date date of flight
745
	* @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...
746
	* @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...
747
	* @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...
748
	* @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...
749
	* @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...
750
	* @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...
751
	* @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...
752
	* @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...
753
	* @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...
754
	* @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...
755
	* @return String success or false
756
	*/
757
	public function addMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $heading = '', $groundspeed = '', $date = '', $mmsi = '',$type = '',$typeid = '',$imo = '',$callsign = '',$arrival_code = '',$arrival_date = '',$status = '',$statusid = '',$format_source = '', $source_name = '', $captain_id = '',$captain_name = '',$race_id = '', $race_name = '')
758
	{
759
		global $globalURL, $globalMarineImageFetch;
760
		
761
		//$Image = new Image($this->db);
762
		$Common = new Common();
763
		
764
		date_default_timezone_set('UTC');
765
		
766
		//getting the registration
767
		if ($fammarine_id != "")
768
		{
769
			if (!is_string($fammarine_id))
770
			{
771
				return false;
772
			}
773
		}
774
		$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...
775
		//getting the airline information
776
		if ($ident != "")
777
		{
778
			if (!is_string($ident))
779
			{
780
				return false;
781
			}
782
		}
783
784
		if ($latitude != "")
785
		{
786
			if (!is_numeric($latitude))
787
			{
788
				return false;
789
			}
790
		}
791
		
792
		if ($longitude != "")
793
		{
794
			if (!is_numeric($longitude))
795
			{
796
				return false;
797
			}
798
		}
799
		
800
		if ($heading != "")
801
		{
802
			if (!is_numeric($heading))
803
			{
804
				return false;
805
			}
806
		}
807
		if ($mmsi != "")
808
		{
809
			if (!is_numeric($mmsi))
810
			{
811
				return false;
812
			}
813
		}
814
		
815
		if ($groundspeed != "")
816
		{
817
			if (!is_numeric($groundspeed))
818
			{
819
				return false;
820
			}
821
		}
822
823
    
824
		if ($date == "" || strtotime($date) < time()-20*60)
825
		{
826
			$date = date("Y-m-d H:i:s", time());
827
		}
828
829
		$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING);
830
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
831
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
832
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
833
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
834
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
835
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
836
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
837
		$type = filter_var($type,FILTER_SANITIZE_STRING);
838
		$status = filter_var($status,FILTER_SANITIZE_STRING);
839
		$type_id = filter_var($typeid,FILTER_SANITIZE_NUMBER_INT);
840
		$status_id = filter_var($statusid,FILTER_SANITIZE_NUMBER_INT);
841
		$imo = filter_var($imo,FILTER_SANITIZE_STRING);
842
		$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
0 ignored issues
show
Unused Code introduced by
$callsign 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...
843
		$arrival_code = filter_var($arrival_code,FILTER_SANITIZE_STRING);
844
		$arrival_date = filter_var($arrival_date,FILTER_SANITIZE_STRING);
845
		$captain_id = filter_var($captain_id,FILTER_SANITIZE_STRING);
846
		$captain_name = filter_var($captain_name,FILTER_SANITIZE_STRING);
847
		$race_id = filter_var($race_id,FILTER_SANITIZE_STRING);
848
		$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
849
		if (isset($globalMarineImageFetch) && $globalMarineImageFetch === TRUE) {
850
			$Image = new Image($this->db);
851
			$image_array = $Image->getMarineImage($mmsi,$imo,$ident);
852
			if (!isset($image_array[0]['mmsi'])) {
853
				$Image->addMarineImage($mmsi,$imo,$ident);
854
			}
855
			unset($Image);
856
		}
857
		if ($latitude == '' && $longitude == '') {
858
			$latitude = 0;
859
			$longitude = 0;
860
		}
861
		if ($type_id == '') $type_id = NULL;
862
		if ($status_id == '') $status_id = NULL;
863
		if ($heading == '' || $Common->isInteger($heading) === false) $heading = 0;
864
		//if ($groundspeed == '' || $Common->isInteger($groundspeed) === false) $groundspeed = 0;
865
		if ($arrival_date == '') $arrival_date = NULL;
866
		$query  = "INSERT INTO marine_output (fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, mmsi, type, type_id, status,status_id,imo,arrival_port_name,arrival_port_date,captain_id,captain_name,race_id,race_name) 
867
		    VALUES (:fammarine_id,:ident,:latitude,:longitude,:heading,:speed,:date,:format_source, :source_name,:mmsi,:type,:type_id,:status,:status_id,:imo,:arrival_port_name,:arrival_port_date,:captain_id,:captain_name,:race_id,:race_name)";
868
869
		$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':heading' => $heading,':speed' => $groundspeed,':date' => $date,':format_source' => $format_source, ':source_name' => $source_name,':mmsi' => $mmsi,':type' => $type,':type_id' => $type_id,':status' => $status,':status_id' => $status_id,':imo' => $imo,':arrival_port_name' => $arrival_code,':arrival_port_date' => $arrival_date,':captain_id' => $captain_id,':captain_name' => $captain_name,':race_id' => $race_id,':race_name' => $race_name);
870
		try {
871
			$sth = $this->db->prepare($query);
872
			$sth->execute($query_values);
873
			$this->db = null;
874
		} catch (PDOException $e) {
875
			return "error : ".$e->getMessage();
876
		}
877
		
878
		return "success";
879
880
	}
881
	
882
  
883
	/**
884
	* Gets the aircraft ident within the last hour
885
	*
886
	* @return String the ident
887
	*
888
	*/
889
	public function getIdentFromLastHour($ident)
890
	{
891
		global $globalDBdriver, $globalTimezone;
892
		if ($globalDBdriver == 'mysql') {
893
			$query  = "SELECT marine_output.ident FROM marine_output 
894
								WHERE marine_output.ident = :ident 
895
								AND marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
896
								AND marine_output.date < UTC_TIMESTAMP()";
897
			$query_data = array(':ident' => $ident);
898
		} else {
899
			$query  = "SELECT marine_output.ident FROM marine_output 
900
								WHERE marine_output.ident = :ident 
901
								AND marine_output.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
902
								AND marine_output.date < now() AT TIME ZONE 'UTC'";
903
			$query_data = array(':ident' => $ident);
904
    		}
905
		
906
		$sth = $this->db->prepare($query);
907
		$sth->execute($query_data);
908
    		$ident_result='';
909
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
910
		{
911
			$ident_result = $row['ident'];
912
		}
913
914
		return $ident_result;
915
	}
916
	
917
	
918
	/**
919
	* Gets the aircraft data from the last 20 seconds
920
	*
921
	* @return Array the spotter data
922
	*
923
	*/
924
	public function getRealTimeData($q = '')
925
	{
926
		global $globalDBdriver;
927
		$additional_query = '';
928
		if ($q != "")
929
		{
930
			if (!is_string($q))
931
			{
932
				return false;
933
			} else {
934
				$q_array = explode(" ", $q);
935
				foreach ($q_array as $q_item){
936
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
937
					$additional_query .= " AND (";
938
					$additional_query .= "(marine_output.ident like '%".$q_item."%')";
939
					$additional_query .= ")";
940
				}
941
			}
942
		}
943
		if ($globalDBdriver == 'mysql') {
944
			$query  = "SELECT marine_output.* FROM marine_output 
945
				WHERE marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 SECOND) ".$additional_query." 
946
				AND marine_output.date < UTC_TIMESTAMP()";
947
		} else {
948
			$query  = "SELECT marine_output.* FROM marine_output 
949
				WHERE marine_output.date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 SECONDS' ".$additional_query." 
950
				AND marine_output.date::timestamp < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";
951
		}
952
		$spotter_array = $this->getDataFromDB($query, array());
953
954
		return $spotter_array;
955
	}
956
	
957
	
958
	
959
960
	/**
961
	* Gets all number of flight over countries
962
	*
963
	* @return Array the airline country list
964
	*
965
	*/
966
967
	public function countAllMarineOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array())
968
	{
969
		global $globalDBdriver, $globalArchive;
970
		//$filter_query = $this->getFilter($filters,true,true);
971
		$Connection= new Connection($this->db);
972
		if (!$Connection->tableExists('countries')) return array();
973
		require_once('class.SpotterLive.php');
974
		if (!isset($globalArchive) || $globalArchive !== TRUE) {
975
			$MarineLive = new MarineLive($this->db);
976
			$filter_query = $MarineLive->getFilter($filters,true,true);
977
			$filter_query .= " over_country IS NOT NULL AND over_country <> ''";
978
			if ($olderthanmonths > 0) {
979
				if ($globalDBdriver == 'mysql') {
980
					$filter_query .= ' AND marine_live.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
981
				} else {
982
					$filter_query .= " AND marine_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
983
				}
984
			}
985
			if ($sincedate != '') {
986
				if ($globalDBdriver == 'mysql') {
987
					$filter_query .= " AND marine_live.date > '".$sincedate."' ";
988
				} else {
989
					$filter_query .= " AND marine_live.date > CAST('".$sincedate."' AS TIMESTAMP)";
990
				}
991
			}
992
			$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT fammarine_id,over_country FROM marine_live".$filter_query.") l ON c.iso2 = l.over_country ";
993
		} else {
994
			require_once(dirname(__FILE__)."/class.MarineArchive.php");
995
			$MarineArchive = new MarineArchive($this->db);
996
			$filter_query = $MarineArchive->getFilter($filters,true,true);
997
			$filter_query .= " over_country <> ''";
998
			if ($olderthanmonths > 0) {
999
				if ($globalDBdriver == 'mysql') {
1000
					$filter_query .= ' AND marine_archive.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1001
				} else {
1002
					$filter_query .= " AND marine_archive.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1003
				}
1004
			}
1005
			if ($sincedate != '') {
1006
				if ($globalDBdriver == 'mysql') {
1007
					$filter_query .= " AND marine_archive.date > '".$sincedate."' ";
1008
				} else {
1009
					$filter_query .= " AND marine_archive.date > CAST('".$sincedate."' AS TIMESTAMP)";
1010
				}
1011
			}
1012
			$filter_query .= " LIMIT 200 OFFSET 0";
1013
			$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT fammarine_id,over_country FROM marine_archive".$filter_query.") l ON c.iso2 = l.over_country ";
1014
		}
1015
		$query .= "GROUP BY c.name,c.iso3,c.iso2 ORDER BY nb DESC";
1016
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1017
1018
		$sth = $this->db->prepare($query);
1019
		$sth->execute();
1020
 
1021
		$flight_array = array();
1022
		$temp_array = array();
1023
        
1024
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1025
		{
1026
			$temp_array['marine_count'] = $row['nb'];
1027
			$temp_array['marine_country'] = $row['name'];
1028
			$temp_array['marine_country_iso3'] = $row['iso3'];
1029
			$temp_array['marine_country_iso2'] = $row['iso2'];
1030
			$flight_array[] = $temp_array;
1031
		}
1032
		return $flight_array;
1033
	}
1034
	
1035
	
1036
	
1037
	/**
1038
	* Gets all callsigns that have flown over
1039
	*
1040
	* @return Array the callsign list
1041
	*
1042
	*/
1043
	public function countAllCallsigns($limit = true, $olderthanmonths = 0, $sincedate = '',$filters = array(),$year = '', $month = '', $day = '')
1044
	{
1045
		global $globalDBdriver;
1046
		$filter_query = $this->getFilter($filters,true,true);
1047
		$query  = "SELECT DISTINCT marine_output.ident, COUNT(marine_output.ident) AS callsign_icao_count 
1048
                    FROM marine_output".$filter_query." marine_output.ident <> ''";
1049
		 if ($olderthanmonths > 0) {
1050
			if ($globalDBdriver == 'mysql') $query .= ' AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH)';
1051
			else $query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1052
		}
1053
		if ($sincedate != '') {
1054
			if ($globalDBdriver == 'mysql') $query .= " AND marine_output.date > '".$sincedate."'";
1055
			else $query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
1056
		}
1057
		$query_values = array();
1058
		if ($year != '') {
1059
			if ($globalDBdriver == 'mysql') {
1060
				$query .= " AND YEAR(marine_output.date) = :year";
1061
				$query_values = array_merge($query_values,array(':year' => $year));
1062
			} else {
1063
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1064
				$query_values = array_merge($query_values,array(':year' => $year));
1065
			}
1066
		}
1067
		if ($month != '') {
1068
			if ($globalDBdriver == 'mysql') {
1069
				$query .= " AND MONTH(marine_output.date) = :month";
1070
				$query_values = array_merge($query_values,array(':month' => $month));
1071
			} else {
1072
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1073
				$query_values = array_merge($query_values,array(':month' => $month));
1074
			}
1075
		}
1076
		if ($day != '') {
1077
			if ($globalDBdriver == 'mysql') {
1078
				$query .= " AND DAY(marine_output.date) = :day";
1079
				$query_values = array_merge($query_values,array(':day' => $day));
1080
			} else {
1081
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
1082
				$query_values = array_merge($query_values,array(':day' => $day));
1083
			}
1084
		}
1085
		$query .= " GROUP BY marine_output.ident ORDER BY callsign_icao_count DESC";
1086
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1087
      		
1088
		$sth = $this->db->prepare($query);
1089
		$sth->execute($query_values);
1090
      
1091
		$callsign_array = array();
1092
		$temp_array = array();
1093
        
1094
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1095
		{
1096
			$temp_array['callsign_icao'] = $row['ident'];
1097
			$temp_array['airline_name'] = $row['airline_name'];
1098
			$temp_array['airline_icao'] = $row['airline_icao'];
1099
			$temp_array['callsign_icao_count'] = $row['callsign_icao_count'];
1100
          
1101
			$callsign_array[] = $temp_array;
1102
		}
1103
1104
		return $callsign_array;
1105
	}
1106
1107
1108
	/**
1109
	* Counts all dates
1110
	*
1111
	* @return Array the date list
1112
	*
1113
	*/
1114
	public function countAllDates($filters = array())
1115
	{
1116
		global $globalTimezone, $globalDBdriver;
1117
		if ($globalTimezone != '') {
1118
			date_default_timezone_set($globalTimezone);
1119
			$datetime = new DateTime();
1120
			$offset = $datetime->format('P');
1121
		} else $offset = '+00:00';
1122
1123
		if ($globalDBdriver == 'mysql') {
1124
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1125
								FROM marine_output";
1126
			$query .= $this->getFilter($filters);
1127
			$query .= " GROUP BY date_name 
1128
								ORDER BY date_count DESC
1129
								LIMIT 10 OFFSET 0";
1130
		} else {
1131
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1132
								FROM marine_output";
1133
			$query .= $this->getFilter($filters);
1134
			$query .= " GROUP BY date_name 
1135
								ORDER BY date_count DESC
1136
								LIMIT 10 OFFSET 0";
1137
		}
1138
      
1139
		
1140
		$sth = $this->db->prepare($query);
1141
		$sth->execute(array(':offset' => $offset));
1142
      
1143
		$date_array = array();
1144
		$temp_array = array();
1145
        
1146
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1147
		{
1148
			$temp_array['date_name'] = $row['date_name'];
1149
			$temp_array['date_count'] = $row['date_count'];
1150
1151
			$date_array[] = $temp_array;
1152
		}
1153
1154
		return $date_array;
1155
	}
1156
	
1157
	
1158
	/**
1159
	* Counts all dates during the last 7 days
1160
	*
1161
	* @return Array the date list
1162
	*
1163
	*/
1164
	public function countAllDatesLast7Days($filters = array())
1165
	{
1166
		global $globalTimezone, $globalDBdriver;
1167
		if ($globalTimezone != '') {
1168
			date_default_timezone_set($globalTimezone);
1169
			$datetime = new DateTime();
1170
			$offset = $datetime->format('P');
1171
		} else $offset = '+00:00';
1172
		$filter_query = $this->getFilter($filters,true,true);
1173
		if ($globalDBdriver == 'mysql') {
1174
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1175
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY)";
1176
			$query .= " GROUP BY date_name 
1177
								ORDER BY marine_output.date ASC";
1178
			$query_data = array(':offset' => $offset);
1179
		} else {
1180
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1181
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '7 DAYS'";
1182
			$query .= " GROUP BY date_name 
1183
								ORDER BY date_name ASC";
1184
			$query_data = array(':offset' => $offset);
1185
    		}
1186
		
1187
		$sth = $this->db->prepare($query);
1188
		$sth->execute($query_data);
1189
      
1190
		$date_array = array();
1191
		$temp_array = array();
1192
        
1193
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1194
		{
1195
			$temp_array['date_name'] = $row['date_name'];
1196
			$temp_array['date_count'] = $row['date_count'];
1197
          
1198
			$date_array[] = $temp_array;
1199
		}
1200
1201
		return $date_array;
1202
	}
1203
1204
	/**
1205
	* Counts all dates during the last month
1206
	*
1207
	* @return Array the date list
1208
	*
1209
	*/
1210
	public function countAllDatesLastMonth($filters = array())
1211
	{
1212
		global $globalTimezone, $globalDBdriver;
1213
		if ($globalTimezone != '') {
1214
			date_default_timezone_set($globalTimezone);
1215
			$datetime = new DateTime();
1216
			$offset = $datetime->format('P');
1217
		} else $offset = '+00:00';
1218
		$filter_query = $this->getFilter($filters,true,true);
1219
		if ($globalDBdriver == 'mysql') {
1220
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1221
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MONTH)";
1222
			$query .= " GROUP BY date_name 
1223
								ORDER BY marine_output.date ASC";
1224
			$query_data = array(':offset' => $offset);
1225
		} else {
1226
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1227
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 MONTHS'";
1228
			$query .= " GROUP BY date_name 
1229
								ORDER BY date_name ASC";
1230
			$query_data = array(':offset' => $offset);
1231
    		}
1232
		
1233
		$sth = $this->db->prepare($query);
1234
		$sth->execute($query_data);
1235
      
1236
		$date_array = array();
1237
		$temp_array = array();
1238
        
1239
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1240
		{
1241
			$temp_array['date_name'] = $row['date_name'];
1242
			$temp_array['date_count'] = $row['date_count'];
1243
          
1244
			$date_array[] = $temp_array;
1245
		}
1246
1247
		return $date_array;
1248
	}
1249
1250
1251
1252
	/**
1253
	* Counts all month
1254
	*
1255
	* @return Array the month list
1256
	*
1257
	*/
1258
	public function countAllMonths($filters = array())
1259
	{
1260
		global $globalTimezone, $globalDBdriver;
1261
		if ($globalTimezone != '') {
1262
			date_default_timezone_set($globalTimezone);
1263
			$datetime = new DateTime();
1264
			$offset = $datetime->format('P');
1265
		} else $offset = '+00:00';
1266
1267
		if ($globalDBdriver == 'mysql') {
1268
			$query  = "SELECT YEAR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS year_name,MONTH(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS month_name, count(*) as date_count
1269
								FROM marine_output";
1270
			$query .= $this->getFilter($filters);
1271
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1272
		} else {
1273
			$query  = "SELECT EXTRACT(YEAR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS year_name,EXTRACT(MONTH FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS month_name, count(*) as date_count
1274
								FROM marine_output";
1275
			$query .= $this->getFilter($filters);
1276
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1277
		}
1278
      
1279
		
1280
		$sth = $this->db->prepare($query);
1281
		$sth->execute(array(':offset' => $offset));
1282
      
1283
		$date_array = array();
1284
		$temp_array = array();
1285
        
1286
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1287
		{
1288
			$temp_array['month_name'] = $row['month_name'];
1289
			$temp_array['year_name'] = $row['year_name'];
1290
			$temp_array['date_count'] = $row['date_count'];
1291
1292
			$date_array[] = $temp_array;
1293
		}
1294
1295
		return $date_array;
1296
	}
1297
1298
	
1299
	
1300
1301
	/**
1302
	* Counts all dates during the last year
1303
	*
1304
	* @return Array the date list
1305
	*
1306
	*/
1307
	public function countAllMonthsLastYear($filters)
1308
	{
1309
		global $globalTimezone, $globalDBdriver;
1310
		if ($globalTimezone != '') {
1311
			date_default_timezone_set($globalTimezone);
1312
			$datetime = new DateTime();
1313
			$offset = $datetime->format('P');
1314
		} else $offset = '+00:00';
1315
		$filter_query = $this->getFilter($filters,true,true);
1316
		if ($globalDBdriver == 'mysql') {
1317
			$query  = "SELECT MONTH(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS month_name, YEAR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS year_name, count(*) as date_count
1318
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)";
1319
			$query .= " GROUP BY year_name, month_name
1320
								ORDER BY year_name, month_name ASC";
1321
			$query_data = array(':offset' => $offset);
1322
		} else {
1323
			$query  = "SELECT EXTRACT(MONTH FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS month_name, EXTRACT(YEAR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS year_name, count(*) as date_count
1324
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'";
1325
			$query .= " GROUP BY year_name, month_name
1326
								ORDER BY year_name, month_name ASC";
1327
			$query_data = array(':offset' => $offset);
1328
    		}
1329
		
1330
		$sth = $this->db->prepare($query);
1331
		$sth->execute($query_data);
1332
      
1333
		$date_array = array();
1334
		$temp_array = array();
1335
        
1336
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1337
		{
1338
			$temp_array['year_name'] = $row['year_name'];
1339
			$temp_array['month_name'] = $row['month_name'];
1340
			$temp_array['date_count'] = $row['date_count'];
1341
          
1342
			$date_array[] = $temp_array;
1343
		}
1344
1345
		return $date_array;
1346
	}
1347
	
1348
	
1349
	
1350
	/**
1351
	* Counts all hours
1352
	*
1353
	* @return Array the hour list
1354
	*
1355
	*/
1356
	public function countAllHours($orderby,$filters = array())
1357
	{
1358
		global $globalTimezone, $globalDBdriver;
1359
		if ($globalTimezone != '') {
1360
			date_default_timezone_set($globalTimezone);
1361
			$datetime = new DateTime();
1362
			$offset = $datetime->format('P');
1363
		} else $offset = '+00:00';
1364
1365
		$orderby_sql = '';
1366
		if ($orderby == "hour")
1367
		{
1368
			$orderby_sql = "ORDER BY hour_name ASC";
1369
		}
1370
		if ($orderby == "count")
1371
		{
1372
			$orderby_sql = "ORDER BY hour_count DESC";
1373
		}
1374
		
1375
		if ($globalDBdriver == 'mysql') {
1376
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1377
								FROM marine_output";
1378
			$query .= $this->getFilter($filters);
1379
			$query .= " GROUP BY hour_name 
1380
								".$orderby_sql;
1381
1382
/*		$query  = "SELECT HOUR(marine_output.date) AS hour_name, count(*) as hour_count
1383
								FROM marine_output 
1384
								GROUP BY hour_name 
1385
								".$orderby_sql."
1386
								LIMIT 10 OFFSET 00";
1387
  */    
1388
		$query_data = array(':offset' => $offset);
1389
		} else {
1390
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1391
								FROM marine_output";
1392
			$query .= $this->getFilter($filters);
1393
			$query .= " GROUP BY hour_name 
1394
								".$orderby_sql;
1395
			$query_data = array(':offset' => $offset);
1396
		}
1397
		
1398
		$sth = $this->db->prepare($query);
1399
		$sth->execute($query_data);
1400
      
1401
		$hour_array = array();
1402
		$temp_array = array();
1403
        
1404
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1405
		{
1406
			$temp_array['hour_name'] = $row['hour_name'];
1407
			$temp_array['hour_count'] = $row['hour_count'];
1408
          
1409
			$hour_array[] = $temp_array;
1410
		}
1411
1412
		return $hour_array;
1413
	}
1414
	
1415
	
1416
	
1417
	/**
1418
	* Counts all hours by date
1419
	*
1420
	* @return Array the hour list
1421
	*
1422
	*/
1423
	public function countAllHoursByDate($date, $filters = array())
1424
	{
1425
		global $globalTimezone, $globalDBdriver;
1426
		$filter_query = $this->getFilter($filters,true,true);
1427
		$date = filter_var($date,FILTER_SANITIZE_STRING);
1428
		if ($globalTimezone != '') {
1429
			date_default_timezone_set($globalTimezone);
1430
			$datetime = new DateTime($date);
1431
			$offset = $datetime->format('P');
1432
		} else $offset = '+00:00';
1433
1434
		if ($globalDBdriver == 'mysql') {
1435
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1436
								FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date
1437
								GROUP BY hour_name 
1438
								ORDER BY hour_name ASC";
1439
		} else {
1440
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1441
								FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset, 'YYYY-mm-dd') = :date
1442
								GROUP BY hour_name 
1443
								ORDER BY hour_name ASC";
1444
		}
1445
		
1446
		$sth = $this->db->prepare($query);
1447
		$sth->execute(array(':date' => $date, ':offset' => $offset));
1448
      
1449
		$hour_array = array();
1450
		$temp_array = array();
1451
        
1452
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1453
		{
1454
			$temp_array['hour_name'] = $row['hour_name'];
1455
			$temp_array['hour_count'] = $row['hour_count'];
1456
          
1457
			$hour_array[] = $temp_array;
1458
		}
1459
1460
		return $hour_array;
1461
	}
1462
	
1463
	
1464
	
1465
	/**
1466
	* Counts all hours by a ident/callsign
1467
	*
1468
	* @return Array the hour list
1469
	*
1470
	*/
1471
	public function countAllHoursByIdent($ident, $filters = array())
1472
	{
1473
		global $globalTimezone, $globalDBdriver;
1474
		$filter_query = $this->getFilter($filters,true,true);
1475
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1476
		if ($globalTimezone != '') {
1477
			date_default_timezone_set($globalTimezone);
1478
			$datetime = new DateTime();
1479
			$offset = $datetime->format('P');
1480
		} else $offset = '+00:00';
1481
1482
		if ($globalDBdriver == 'mysql') {
1483
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1484
								FROM marine_output".$filter_query." marine_output.ident = :ident 
1485
								GROUP BY hour_name 
1486
								ORDER BY hour_name ASC";
1487
		} else {
1488
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1489
								FROM marine_output".$filter_query." marine_output.ident = :ident 
1490
								GROUP BY hour_name 
1491
								ORDER BY hour_name ASC";
1492
		}
1493
      
1494
		
1495
		$sth = $this->db->prepare($query);
1496
		$sth->execute(array(':ident' => $ident,':offset' => $offset));
1497
      
1498
		$hour_array = array();
1499
		$temp_array = array();
1500
        
1501
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1502
		{
1503
			$temp_array['hour_name'] = $row['hour_name'];
1504
			$temp_array['hour_count'] = $row['hour_count'];
1505
          
1506
			$hour_array[] = $temp_array;
1507
		}
1508
1509
		return $hour_array;
1510
	}
1511
	
1512
	
1513
	
1514
	/**
1515
	* Counts all vessels
1516
	*
1517
	* @return Integer the number of vessels
1518
	*
1519
	*/
1520
	public function countOverallMarine($filters = array(),$year = '',$month = '')
1521
	{
1522
		global $globalDBdriver;
1523
		//$queryi  = "SELECT COUNT(marine_output.marine_id) AS flight_count FROM marine_output";
1524
		$queryi  = "SELECT COUNT(DISTINCT marine_output.mmsi) AS flight_count FROM marine_output";
1525
		$query_values = array();
1526
		$query = '';
1527
		if ($year != '') {
1528
			if ($globalDBdriver == 'mysql') {
1529
				$query .= " AND YEAR(marine_output.date) = :year";
1530
				$query_values = array_merge($query_values,array(':year' => $year));
1531
			} else {
1532
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1533
				$query_values = array_merge($query_values,array(':year' => $year));
1534
			}
1535
		}
1536
		if ($month != '') {
1537
			if ($globalDBdriver == 'mysql') {
1538
				$query .= " AND MONTH(marine_output.date) = :month";
1539
				$query_values = array_merge($query_values,array(':month' => $month));
1540
			} else {
1541
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1542
				$query_values = array_merge($query_values,array(':month' => $month));
1543
			}
1544
		}
1545
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1546
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1547
		
1548
		$sth = $this->db->prepare($queryi);
1549
		$sth->execute($query_values);
1550
		return $sth->fetchColumn();
1551
	}
1552
	
1553
	/**
1554
	* Counts all vessel type
1555
	*
1556
	* @return Integer the number of vessels
1557
	*
1558
	*/
1559
	public function countOverallMarineTypes($filters = array(),$year = '',$month = '')
1560
	{
1561
		global $globalDBdriver;
1562
		$queryi  = "SELECT COUNT(DISTINCT marine_output.type) AS marine_count FROM marine_output";
1563
		$query_values = array();
1564
		$query = '';
1565
		if ($year != '') {
1566
			if ($globalDBdriver == 'mysql') {
1567
				$query .= " AND YEAR(marine_output.date) = :year";
1568
				$query_values = array_merge($query_values,array(':year' => $year));
1569
			} else {
1570
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1571
				$query_values = array_merge($query_values,array(':year' => $year));
1572
			}
1573
		}
1574
		if ($month != '') {
1575
			if ($globalDBdriver == 'mysql') {
1576
				$query .= " AND MONTH(marine_output.date) = :month";
1577
				$query_values = array_merge($query_values,array(':month' => $month));
1578
			} else {
1579
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1580
				$query_values = array_merge($query_values,array(':month' => $month));
1581
			}
1582
		}
1583
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1584
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1585
		
1586
		$sth = $this->db->prepare($queryi);
1587
		$sth->execute($query_values);
1588
		return $sth->fetchColumn();
1589
	}
1590
	
1591
  
1592
	/**
1593
	* Counts all hours of today
1594
	*
1595
	* @return Array the hour list
1596
	*
1597
	*/
1598
	public function countAllHoursFromToday($filters = array())
1599
	{
1600
		global $globalTimezone, $globalDBdriver;
1601
		$filter_query = $this->getFilter($filters,true,true);
1602
		if ($globalTimezone != '') {
1603
			date_default_timezone_set($globalTimezone);
1604
			$datetime = new DateTime();
1605
			$offset = $datetime->format('P');
1606
		} else $offset = '+00:00';
1607
1608
		if ($globalDBdriver == 'mysql') {
1609
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1610
								FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = CURDATE()
1611
								GROUP BY hour_name 
1612
								ORDER BY hour_name ASC";
1613
		} else {
1614
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1615
								FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') = CAST(NOW() AS date)
1616
								GROUP BY hour_name 
1617
								ORDER BY hour_name ASC";
1618
		}
1619
		
1620
		$sth = $this->db->prepare($query);
1621
		$sth->execute(array(':offset' => $offset));
1622
      
1623
		$hour_array = array();
1624
		$temp_array = array();
1625
        
1626
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1627
		{
1628
			$temp_array['hour_name'] = $row['hour_name'];
1629
			$temp_array['hour_count'] = $row['hour_count'];
1630
			$hour_array[] = $temp_array;
1631
		}
1632
1633
		return $hour_array;
1634
	}
1635
    
1636
    
1637
     /**
1638
	* Gets the Barrie Spotter ID based on the FlightAware ID
1639
	*
1640
	* @return Integer the Barrie Spotter ID
1641
q	*
1642
	*/
1643
	public function getMarineIDBasedOnFamMarineID($fammarine_id)
1644
	{
1645
		$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING);
1646
1647
		$query  = "SELECT marine_output.marine_id
1648
				FROM marine_output 
1649
				WHERE marine_output.fammarine_id = '".$fammarine_id."'";
1650
        
1651
		
1652
		$sth = $this->db->prepare($query);
1653
		$sth->execute();
1654
1655
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1656
		{
1657
			return $row['marine_id'];
1658
		}
1659
	}
1660
  
1661
 
1662
	/**
1663
	* Parses a date string
1664
	*
1665
	* @param String $dateString the date string
1666
	* @param String $timezone the timezone of a user
1667
	* @return Array the time information
1668
	*
1669
	*/
1670
	public function parseDateString($dateString, $timezone = '')
1671
	{
1672
		$time_array = array();
1673
	
1674
		if ($timezone != "")
1675
		{
1676
			date_default_timezone_set($timezone);
1677
		}
1678
		
1679
		$current_date = date("Y-m-d H:i:s");
1680
		$date = date("Y-m-d H:i:s",strtotime($dateString." UTC"));
1681
		
1682
		$diff = abs(strtotime($current_date) - strtotime($date));
1683
1684
		$time_array['years'] = floor($diff / (365*60*60*24)); 
1685
		$years = $time_array['years'];
1686
		
1687
		$time_array['months'] = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
1688
		$months = $time_array['months'];
1689
		
1690
		$time_array['days'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
1691
		$days = $time_array['days'];
1692
		$time_array['hours'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
1693
		$hours = $time_array['hours'];
1694
		$time_array['minutes'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
1695
		$minutes = $time_array['minutes'];
1696
		$time_array['seconds'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));  
1697
		
1698
		return $time_array;
1699
	}
1700
	
1701
	/**
1702
	* Parses the direction degrees to working
1703
	*
1704
	* @param Float $direction the direction in degrees
1705
	* @return Array the direction information
1706
	*
1707
	*/
1708
	public function parseDirection($direction = 0)
1709
	{
1710
		if ($direction == '') $direction = 0;
1711
		$direction_array = array();
1712
		$temp_array = array();
1713
1714
		if ($direction == 360 || ($direction >= 0 && $direction < 22.5))
1715
		{
1716
			$temp_array['direction_degree'] = $direction;
1717
			$temp_array['direction_shortname'] = "N";
1718
			$temp_array['direction_fullname'] = "North";
1719
		} elseif ($direction >= 22.5 && $direction < 45){
1720
			$temp_array['direction_degree'] = $direction;
1721
			$temp_array['direction_shortname'] = "NNE";
1722
			$temp_array['direction_fullname'] = "North-Northeast";
1723
		} elseif ($direction >= 45 && $direction < 67.5){
1724
			$temp_array['direction_degree'] = $direction;
1725
			$temp_array['direction_shortname'] = "NE";
1726
			$temp_array['direction_fullname'] = "Northeast";
1727
		} elseif ($direction >= 67.5 && $direction < 90){
1728
			$temp_array['direction_degree'] = $direction;
1729
			$temp_array['direction_shortname'] = "ENE";
1730
			$temp_array['direction_fullname'] = "East-Northeast";
1731
		} elseif ($direction >= 90 && $direction < 112.5){
1732
			$temp_array['direction_degree'] = $direction;
1733
			$temp_array['direction_shortname'] = "E";
1734
			$temp_array['direction_fullname'] = "East";
1735
		} elseif ($direction >= 112.5 && $direction < 135){
1736
			$temp_array['direction_degree'] = $direction;
1737
			$temp_array['direction_shortname'] = "ESE";
1738
			$temp_array['direction_fullname'] = "East-Southeast";
1739
		} elseif ($direction >= 135 && $direction < 157.5){
1740
			$temp_array['direction_degree'] = $direction;
1741
			$temp_array['direction_shortname'] = "SE";
1742
			$temp_array['direction_fullname'] = "Southeast";
1743
		} elseif ($direction >= 157.5 && $direction < 180){
1744
			$temp_array['direction_degree'] = $direction;
1745
			$temp_array['direction_shortname'] = "SSE";
1746
			$temp_array['direction_fullname'] = "South-Southeast";
1747
		} elseif ($direction >= 180 && $direction < 202.5){
1748
			$temp_array['direction_degree'] = $direction;
1749
			$temp_array['direction_shortname'] = "S";
1750
			$temp_array['direction_fullname'] = "South";
1751
		} elseif ($direction >= 202.5 && $direction < 225){
1752
			$temp_array['direction_degree'] = $direction;
1753
			$temp_array['direction_shortname'] = "SSW";
1754
			$temp_array['direction_fullname'] = "South-Southwest";
1755
		} elseif ($direction >= 225 && $direction < 247.5){
1756
			$temp_array['direction_degree'] = $direction;
1757
			$temp_array['direction_shortname'] = "SW";
1758
			$temp_array['direction_fullname'] = "Southwest";
1759
		} elseif ($direction >= 247.5 && $direction < 270){
1760
			$temp_array['direction_degree'] = $direction;
1761
			$temp_array['direction_shortname'] = "WSW";
1762
			$temp_array['direction_fullname'] = "West-Southwest";
1763
		} elseif ($direction >= 270 && $direction < 292.5){
1764
			$temp_array['direction_degree'] = $direction;
1765
			$temp_array['direction_shortname'] = "W";
1766
			$temp_array['direction_fullname'] = "West";
1767
		} elseif ($direction >= 292.5 && $direction < 315){
1768
			$temp_array['direction_degree'] = $direction;
1769
			$temp_array['direction_shortname'] = "WNW";
1770
			$temp_array['direction_fullname'] = "West-Northwest";
1771
		} elseif ($direction >= 315 && $direction < 337.5){
1772
			$temp_array['direction_degree'] = $direction;
1773
			$temp_array['direction_shortname'] = "NW";
1774
			$temp_array['direction_fullname'] = "Northwest";
1775
		} elseif ($direction >= 337.5 && $direction < 360){
1776
			$temp_array['direction_degree'] = $direction;
1777
			$temp_array['direction_shortname'] = "NNW";
1778
			$temp_array['direction_fullname'] = "North-Northwest";
1779
		}
1780
		$direction_array[] = $temp_array;
1781
		return $direction_array;
1782
	}
1783
	
1784
	
1785
	/**
1786
	* Gets Country from latitude/longitude
1787
	*
1788
	* @param Float $latitude latitute of the flight
1789
	* @param Float $longitude longitute of the flight
1790
	* @return String the countrie
1791
	*/
1792
	public function getCountryFromLatitudeLongitude($latitude,$longitude)
1793
	{
1794
		global $globalDBdriver, $globalDebug;
1795
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1796
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1797
	
1798
		$Connection = new Connection($this->db);
1799
		if (!$Connection->tableExists('countries')) return '';
1800
	
1801
		try {
1802
			/*
1803
			if ($globalDBdriver == 'mysql') {
1804
				//$query  = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(:latitude :longitude)'), ogc_geom) LIMIT 1";
1805
				$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(".$longitude.' '.$latitude.")'), ogc_geom) LIMIT 1";
1806
			}
1807
			*/
1808
			// This query seems to work both for MariaDB and PostgreSQL
1809
			$query = "SELECT name,iso2,iso3 FROM countries WHERE ST_Within(ST_GeomFromText('POINT(".$longitude." ".$latitude.")',4326), ogc_geom) LIMIT 1";
1810
		
1811
			$sth = $this->db->prepare($query);
1812
			//$sth->execute(array(':latitude' => $latitude,':longitude' => $longitude));
1813
			$sth->execute();
1814
    
1815
			$row = $sth->fetch(PDO::FETCH_ASSOC);
1816
			$sth->closeCursor();
1817
			if (count($row) > 0) {
1818
				return $row;
1819
			} else return '';
1820
		} catch (PDOException $e) {
1821
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
1822
			return '';
1823
		}
1824
	
1825
	}
1826
1827
	/**
1828
	* Gets Country from iso2
1829
	*
1830
	* @param String $iso2 ISO2 country code
1831
	* @return String the countrie
1832
	*/
1833
	public function getCountryFromISO2($iso2)
1834
	{
1835
		global $globalDBdriver, $globalDebug;
1836
		$iso2 = filter_var($iso2,FILTER_SANITIZE_STRING);
1837
	
1838
		$Connection = new Connection($this->db);
1839
		if (!$Connection->tableExists('countries')) return '';
1840
	
1841
		try {
1842
			$query = "SELECT name,iso2,iso3 FROM countries WHERE iso2 = :iso2 LIMIT 1";
1843
		
1844
			$sth = $this->db->prepare($query);
1845
			$sth->execute(array(':iso2' => $iso2));
1846
    
1847
			$row = $sth->fetch(PDO::FETCH_ASSOC);
1848
			$sth->closeCursor();
1849
			if (count($row) > 0) {
1850
				return $row;
1851
			} else return '';
1852
		} catch (PDOException $e) {
1853
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
1854
			return '';
1855
		}
1856
	
1857
	}
1858
1859
	
1860
	/**
1861
	* Gets the short url from bit.ly
1862
	*
1863
	* @param String $url the full url
1864
	* @return String the bit.ly url
1865
	*
1866
	*/
1867
	public function getBitlyURL($url)
1868
	{
1869
		global $globalBitlyAccessToken;
1870
		
1871
		if ($globalBitlyAccessToken == '') return $url;
1872
        
1873
		$google_url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$globalBitlyAccessToken.'&longUrl='.$url;
1874
		
1875
		$ch = curl_init();
1876
		curl_setopt($ch, CURLOPT_HEADER, 0);
1877
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1878
		curl_setopt($ch, CURLOPT_URL, $google_url);
1879
		$bitly_data = curl_exec($ch);
1880
		curl_close($ch);
1881
		
1882
		$bitly_data = json_decode($bitly_data);
1883
		$bitly_url = '';
1884
		if ($bitly_data->status_txt = "OK"){
1885
			$bitly_url = $bitly_data->data->url;
1886
		}
1887
1888
		return $bitly_url;
1889
	}
1890
1891
	
1892
	/**
1893
	* Gets all vessels types that have flown over
1894
	*
1895
	* @return Array the vessel type list
1896
	*
1897
	*/
1898
	public function countAllMarineTypes($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array(),$year = '',$month = '',$day = '')
1899
	{
1900
		global $globalDBdriver;
1901
		$filter_query = $this->getFilter($filters,true,true);
1902
		$query  = "SELECT marine_output.type AS marine_type, COUNT(marine_output.type) AS marine_type_count, marine_output.type_id AS marine_type_id 
1903
		    FROM marine_output ".$filter_query." marine_output.type <> '' AND marine_output.type_id IS NOT NULL";
1904
		if ($olderthanmonths > 0) {
1905
			if ($globalDBdriver == 'mysql') {
1906
				$query .= ' AND marine_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$olderthanmonths.' MONTH)';
1907
			} else {
1908
				$query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1909
			}
1910
		}
1911
		if ($sincedate != '') {
1912
			if ($globalDBdriver == 'mysql') {
1913
				$query .= " AND marine_output.date > '".$sincedate."'";
1914
			} else {
1915
				$query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
1916
			}
1917
		}
1918
		$query_values = array();
1919
		if ($year != '') {
1920
			if ($globalDBdriver == 'mysql') {
1921
				$query .= " AND YEAR(marine_output.date) = :year";
1922
				$query_values = array_merge($query_values,array(':year' => $year));
1923
			} else {
1924
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1925
				$query_values = array_merge($query_values,array(':year' => $year));
1926
			}
1927
		}
1928
		if ($month != '') {
1929
			if ($globalDBdriver == 'mysql') {
1930
				$query .= " AND MONTH(marine_output.date) = :month";
1931
				$query_values = array_merge($query_values,array(':month' => $month));
1932
			} else {
1933
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1934
				$query_values = array_merge($query_values,array(':month' => $month));
1935
			}
1936
		}
1937
		if ($day != '') {
1938
			if ($globalDBdriver == 'mysql') {
1939
				$query .= " AND DAY(marine_output.date) = :day";
1940
				$query_values = array_merge($query_values,array(':day' => $day));
1941
			} else {
1942
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
1943
				$query_values = array_merge($query_values,array(':day' => $day));
1944
			}
1945
		}
1946
		$query .= " GROUP BY marine_output.type, marine_output.type_id ORDER BY marine_type_count DESC";
1947
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1948
		$sth = $this->db->prepare($query);
1949
		$sth->execute($query_values);
1950
		$marine_array = array();
1951
		$temp_array = array();
1952
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1953
		{
1954
			$temp_array['marine_type'] = $row['marine_type'];
1955
			$temp_array['marine_type_id'] = $row['marine_type_id'];
1956
			$temp_array['marine_type_count'] = $row['marine_type_count'];
1957
			$marine_array[] = $temp_array;
1958
		}
1959
		return $marine_array;
1960
	}
1961
1962
	/**
1963
	* Gets all the tracker information
1964
	*
1965
	* @return Array the tracker information
1966
	*
1967
	*/
1968
	public function searchMarineData($q = '', $callsign = '',$mmsi = '', $imo = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '',$filters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $includegeodata 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...
1969
	{
1970
		global $globalTimezone, $globalDBdriver;
1971
		date_default_timezone_set('UTC');
1972
		$query_values = array();
1973
		$additional_query = '';
1974
		$filter_query = $this->getFilter($filters,true,true);
1975
		if ($q != "")
1976
		{
1977
			if (!is_string($q))
1978
			{
1979
				return false;
1980
			} else {
1981
				$q_array = explode(" ", $q);
1982
				foreach ($q_array as $q_item){
1983
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
1984
					$additional_query .= " AND (";
1985
					if (is_int($q_item)) $additional_query .= "(marine_output.marine_id = '".$q_item."') OR ";
1986
					if (is_int($q_item)) $additional_query .= "(marine_output.mmsi = '".$q_item."') OR ";
1987
					if (is_int($q_item)) $additional_query .= "(marine_output.imo = '".$q_item."') OR ";
1988
					$additional_query .= "(marine_output.ident like '%".$q_item."%') OR ";
1989
					$additional_query .= ")";
1990
				}
1991
			}
1992
		}
1993
		if ($callsign != "")
1994
		{
1995
			$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
1996
			if (!is_string($callsign))
1997
			{
1998
				return false;
1999
			} else {
2000
				$additional_query .= " AND marine_output.ident = :callsign";
2001
				$query_values = array_merge($query_values,array(':callsign' => $callsign));
2002
			}
2003
		}
2004
		if ($mmsi != "")
2005
		{
2006
			$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
2007
			if (!is_numeric($mmsi))
2008
			{
2009
				return false;
2010
			} else {
2011
				$additional_query .= " AND marine_output.mmsi = :mmsi";
2012
				$query_values = array_merge($query_values,array(':mmsi' => $mmsi));
2013
			}
2014
		}
2015
		if ($imo != "")
2016
		{
2017
			$imo = filter_var($imo,FILTER_SANITIZE_STRING);
2018
			if (!is_numeric($imo))
2019
			{
2020
				return false;
2021
			} else {
2022
				$additional_query .= " AND marine_output.imo = :imo";
2023
				$query_values = array_merge($query_values,array(':imo' => $imo));
2024
			}
2025
		}
2026
		if ($date_posted != "")
2027
		{
2028
			$date_array = explode(",", $date_posted);
2029
			$date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING);
2030
			$date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING);
2031
			if ($globalTimezone != '') {
2032
				date_default_timezone_set($globalTimezone);
2033
				$datetime = new DateTime();
2034
				$offset = $datetime->format('P');
2035
			} else $offset = '+00:00';
2036
			if ($date_array[1] != "")
2037
			{
2038
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2039
				$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1]));
2040
				if ($globalDBdriver == 'mysql') {
2041
					$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) <= '".$date_array[1]."' ";
2042
				} else {
2043
					$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) <= '".$date_array[1]."' ";
2044
				}
2045
			} else {
2046
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2047
				if ($globalDBdriver == 'mysql') {
2048
					$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' ";
2049
				} else {
2050
					$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' ";
2051
				}
2052
			}
2053
		}
2054
		if ($limit != "")
2055
		{
2056
			$limit_array = explode(",", $limit);
2057
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
2058
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
2059
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
2060
			{
2061
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
2062
			} else $limit_query = "";
2063
		} else $limit_query = "";
2064
		if ($sort != "")
2065
		{
2066
			$search_orderby_array = $this->getOrderBy();
2067
			$orderby_query = $search_orderby_array[$sort]['sql'];
2068
		} else {
2069
			if ($origLat != "" && $origLon != "" && $dist != "") {
2070
				$orderby_query = " ORDER BY distance ASC";
2071
			} else {
2072
				$orderby_query = " ORDER BY marine_output.date DESC";
2073
			}
2074
		}
2075
		if ($origLat != "" && $origLon != "" && $dist != "") {
2076
			$dist = number_format($dist*0.621371,2,'.',''); // convert km to mile
2077
			if ($globalDBdriver == 'mysql') {
2078
				$query="SELECT marine_output.*, 1.60935*3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - marine_archive.latitude)*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(marine_archive.latitude*pi()/180)*POWER(SIN(($origLon-marine_archive.longitude)*pi()/180/2),2))) as distance 
2079
				    FROM marine_archive,marine_output".$filter_query." marine_output.fammarine_id = marine_archive.fammarine_id AND marine_output.ident <> '' ".$additional_query."AND marine_archive.longitude between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon+$dist/cos(radians($origLat)*69)) and marine_archive.latitude between ($origLat-($dist/69)) and ($origLat+($dist/69)) 
2080
				    AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - marine_archive.latitude)*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(marine_archive.latitude*pi()/180)*POWER(SIN(($origLon-marine_archive.longitude)*pi()/180/2),2)))) < $dist".$orderby_query;
2081
			} else {
2082
				$query="SELECT marine_output.*, 1.60935 * 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - CAST(marine_archive.latitude as double precision))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(CAST(marine_archive.latitude as double precision)*pi()/180)*POWER(SIN(($origLon-CAST(marine_archive.longitude as double precision))*pi()/180/2),2))) as distance 
2083
				    FROM marine_archive,marine_output".$filter_query." marine_output.fammarine_id = marine_archive.fammarine_id AND marine_output.ident <> '' ".$additional_query."AND CAST(marine_archive.longitude as double precision) between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon+$dist/cos(radians($origLat))*69) and CAST(marine_archive.latitude as double precision) between ($origLat-($dist/69)) and ($origLat+($dist/69)) 
2084
				    AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - CAST(marine_archive.latitude as double precision))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(CAST(marine_archive.latitude as double precision)*pi()/180)*POWER(SIN(($origLon-CAST(marine_archive.longitude as double precision))*pi()/180/2),2)))) < $dist".$filter_query.$orderby_query;
2085
			}
2086
		} else {
2087
			$query  = "SELECT marine_output.* FROM marine_output".$filter_query." marine_output.ident <> '' 
2088
			    ".$additional_query."
2089
			    ".$orderby_query;
2090
		}
2091
		$marine_array = $this->getDataFromDB($query, $query_values,$limit_query);
2092
		return $marine_array;
2093
	}
2094
2095
	public function getOrderBy()
2096
	{
2097
		$orderby = array("type_asc" => array("key" => "type_asc", "value" => "Type - ASC", "sql" => "ORDER BY marine_output.type ASC"), "type_desc" => array("key" => "type_desc", "value" => "Type - DESC", "sql" => "ORDER BY marine_output.type DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY marine_output.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY marine_output.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY marine_output.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY marine_output.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY marine_output.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY marine_output.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure Airport - ASC", "sql" => "ORDER BY marine_output.departure_airport_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY marine_output.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY marine_output.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY marine_output.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY marine_output.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY marine_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"));
2098
		
2099
		return $orderby;
2100
		
2101
	}
2102
    
2103
}
2104
?>