Completed
Push — master ( 460c94...2c5525 )
by Yannick
33:06
created

Marine::checkId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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