Completed
Push — master ( 3a8aa2...f2121d )
by Yannick
31:04
created

MarineLive   D

Complexity

Total Complexity 168

Size/Duplication

Total Lines 988
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 988
rs 4.4444
c 0
b 0
f 0
wmc 168
lcom 1
cbo 4

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
F getFilter() 0 68 36
D getLiveMarineData() 0 40 9
B getMinLiveMarineData() 0 27 4
F getMinLastLiveMarineData() 0 68 22
B getLiveMarineCount() 0 22 4
A getLiveMarineDatabyCoord() 0 21 4
C getMinLiveMarineDatabyCoord() 0 66 8
C getLatestMarineForLayar() 0 54 11
A getLastLiveMarineDataByIdent() 0 12 1
A getDateLiveMarineDataByIdent() 0 13 1
A getLastLiveMarineDataById() 0 12 1
A getDateLiveMarineDataById() 0 12 1
B getAllLiveMarineDataById() 0 26 5
A getAllLiveMarineDataByIdent() 0 16 2
A deleteLiveMarineData() 0 21 3
C deleteLiveMarineDataNotUpdated() 0 91 11
A deleteLiveMarineDataByIdent() 0 15 2
A deleteLiveMarineDataById() 0 15 2
B getIdentFromLastHour() 0 26 3
B checkIdentRecent() 0 26 3
B checkIdRecent() 0 26 3
B checkMMSIRecent() 0 26 3
F addLiveMarineData() 0 98 26
A getOrderBy() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like MarineLive often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MarineLive, and based on these observations, apply Extract Interface, too.

1
<?php
2
//$global_query = "SELECT marine_live.* FROM marine_live";
3
4
class MarineLive {
5
	public $db;
6
	static $global_query = "SELECT marine_live.* FROM marine_live";
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.');
12
	}
13
14
15
	/**
16
	* Get SQL query part for filter used
17
	* @param Array $filter the filter
18
	* @return Array the SQL part
19
	*/
20
	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 marine_output.format_source IN ('".implode("','",$flt['source'])."')) spid ON spid.fammarine_id = marine_live.fammarine_id";
40
				} else {
41
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."')) spid ON spid.fammarine_id = marine_live.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['year']) && $filter['year'] != '') || (isset($filter['month']) && $filter['month'] != '') || (isset($filter['day']) && $filter['day'] != '')) {
52
			$filter_query_date = '';
53
			
54
			if (isset($filter['year']) && $filter['year'] != '') {
55
				if ($globalDBdriver == 'mysql') {
56
					$filter_query_date .= " AND YEAR(marine_output.date) = '".$filter['year']."'";
57
				} else {
58
					$filter_query_date .= " AND EXTRACT(YEAR FROM marine_output.date) = '".$filter['year']."'";
59
				}
60
			}
61
			if (isset($filter['month']) && $filter['month'] != '') {
62
				if ($globalDBdriver == 'mysql') {
63
					$filter_query_date .= " AND MONTH(marine_output.date) = '".$filter['month']."'";
64
				} else {
65
					$filter_query_date .= " AND EXTRACT(MONTH FROM marine_output.date) = '".$filter['month']."'";
66
				}
67
			}
68
			if (isset($filter['day']) && $filter['day'] != '') {
69
				if ($globalDBdriver == 'mysql') {
70
					$filter_query_date .= " AND DAY(marine_output.date) = '".$filter['day']."'";
71
				} else {
72
					$filter_query_date .= " AND EXTRACT(DAY FROM marine_output.date) = '".$filter['day']."'";
73
				}
74
			}
75
			$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output".preg_replace('/^ AND/',' WHERE',$filter_query_date).") sd ON sd.fammarine_id = marine_live.fammarine_id";
76
		}
77
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
78
			$filter_query_where .= " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
79
		}
80
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
81
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
82
		if ($filter_query_where != '') {
83
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
84
		}
85
		$filter_query = $filter_query_join.$filter_query_where;
86
		return $filter_query;
87
	}
88
89
	/**
90
	* Gets all the spotter information based on the latest data entry
91
	*
92
	* @return Array the spotter information
93
	*
94
	*/
95
	public function getLiveMarineData($limit = '', $sort = '', $filter = array())
96
	{
97
		global $globalDBdriver, $globalLiveInterval;
98
		$Marine = new Marine($this->db);
99
		date_default_timezone_set('UTC');
100
101
		$filter_query = $this->getFilter($filter);
102
		$limit_query = '';
103
		if ($limit != '')
104
		{
105
			$limit_array = explode(',', $limit);
106
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
107
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
108
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
109
			{
110
				$limit_query = ' LIMIT '.$limit_array[1].' OFFSET '.$limit_array[0];
111
			}
112
		}
113
		$orderby_query = '';
114
		if ($sort != '')
115
		{
116
			$search_orderby_array = $this->getOrderBy();
117
			if (isset($search_orderby_array[$sort]['sql'])) 
118
			{
119
				$orderby_query = ' '.$search_orderby_array[$sort]['sql'];
120
			}
121
		}
122
		if ($orderby_query == '') $orderby_query= ' ORDER BY date DESC';
123
124
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
125
		if ($globalDBdriver == 'mysql') {
126
			//$query  = "SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 SECOND) <= l.date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate";
127
			$query  = 'SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate'.$filter_query.$orderby_query;
128
		} else {
129
			$query  = "SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate".$filter_query.$orderby_query;
130
		}
131
		$spotter_array = $Marine->getDataFromDB($query.$limit_query,array(),'',true);
132
133
		return $spotter_array;
134
	}
135
136
	/**
137
	* Gets Minimal Live Spotter data
138
	*
139
	* @return Array the spotter information
140
	*
141
	*/
142
	public function getMinLiveMarineData($filter = array())
143
	{
144
		global $globalDBdriver, $globalLiveInterval;
145
		date_default_timezone_set('UTC');
146
147
		$filter_query = $this->getFilter($filter,true,true);
148
149
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
150
		if ($globalDBdriver == 'mysql') {
151
			$query  = 'SELECT marine_live.mmsi, marine_live.ident, marine_live.type,marine_live.fammarine_id, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
152
			FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate'.$filter_query." marine_live.latitude <> 0 AND marine_live.longitude <> 0";
153
		} else {
154
			$query  = "SELECT marine_live.mmsi, marine_live.ident, marine_live.type,marine_live.fammarine_id, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
155
			FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate".$filter_query." marine_live.latitude <> '0' AND marine_live.longitude <> '0'";
156
		}
157
158
		try {
159
			$sth = $this->db->prepare($query);
160
			$sth->execute();
161
		} catch(PDOException $e) {
162
			echo $e->getMessage();
163
			die;
164
		}
165
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
166
167
		return $spotter_array;
168
	}
169
170
	/**
171
	* Gets Minimal Live Spotter data since xx seconds
172
	*
173
	* @return Array the spotter information
174
	*
175
	*/
176
	public function getMinLastLiveMarineData($coord = array(),$filter = array(), $limit = false)
177
	{
178
		global $globalDBdriver, $globalLiveInterval, $globalMap3DMarinesLimit, $globalArchive;
179
		date_default_timezone_set('UTC');
180
		$usecoord = false;
181
		if (is_array($coord) && !empty($coord)) {
182
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
183
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
184
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
185
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
186
			$usecoord = true;
187
		}
188
		$filter_query = $this->getFilter($filter,true,true);
189
190
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
191
		if (!isset($globalMap3DMarinesLimit) || $globalMap3DMarinesLimit == '') $globalMap3DMarinesLimit = '300';
192
		if ($globalDBdriver == 'mysql') {
193
			if (isset($globalArchive) && $globalArchive === TRUE) {
194
				$query  = 'SELECT * FROM (SELECT marine_archive.ident, marine_archive.fammarine_id,marine_archive.type, marine_archive.latitude, marine_archive.longitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.date, marine_archive.format_source 
195
				    FROM marine_archive INNER JOIN (SELECT fammarine_id FROM marine_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= marine_live.date) l ON l.flightaware_id = marine_archive.flightaware_id ";
196
				if ($usecoord) $query .= "AND marine_archive.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_archive.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
0 ignored issues
show
Bug introduced by
The variable $minlat does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $maxlat does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $minlong does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $maxlong does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
197
				$query .= "UNION
198
				    SELECT marine_live.ident, marine_live.fammarine_id,marine_live.type, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
199
				    FROM marine_live".$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= marine_live.date";
200
				if ($usecoord) $query .= " AND marine_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_live.longitude BETWEEN ".$minlong." AND ".$maxlong;
201
				$query .= ") AS marine 
202
				    WHERE latitude <> '0' AND longitude <> '0' 
203
				    ORDER BY fammarine_id, date";
204
				if ($limit) $query .= " LIMIT ".$globalMap3DMarinesLimit;
205
			} else {
206
				$query  = 'SELECT marine_live.ident, marine_live.fammarine_id,marine_live.type, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
207
				    FROM marine_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= marine_live.date ";
208
				if ($usecoord) $query .= "AND marine_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_live.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
209
				$query .= "AND marine_live.latitude <> '0' AND marine_live.longitude <> '0' 
210
				ORDER BY marine_live.fammarine_id, marine_live.date";
211
				if ($limit) $query .= " LIMIT ".$globalMap3DMarinesLimit;
212
			}
213
		} else {
214
			if (isset($globalArchive) && $globalArchive === TRUE) {
215
				$query  = "SELECT * FROM (SELECT marine_archive.ident, marine_archive.fammarine_id, marine_archive.type,marine_archive.latitude, marine_archive.longitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.date, marine_archive.format_source 
216
				    FROM marine_archive INNER JOIN (SELECT flightaware_id FROM marine_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= marine_live.date) l ON l.flightaware_id = marine_archive.flightaware_id ";
217
				if ($usecoord) $query .= "AND marine_archive.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_archive.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
218
				$query .= "UNION
219
				    SELECT marine_live.ident, marine_live.fammarine_id, marine_live.type,marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
220
				    FROM marine_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= marine_live.date";
221
				if ($usecoord) $query .= " AND marine_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_live.longitude BETWEEN ".$minlong." AND ".$maxlong;
222
				$query .= ") AS marine WHERE latitude <> '0' AND longitude <> '0' ";
223
				$query .= "ORDER BY fammarine_id, date";
224
				if ($limit) $query .= " LIMIT ".$globalMap3DMarinesLimit;
225
			} else {
226
				$query  = "SELECT marine_live.ident, marine_live.fammarine_id, marine_live.type,marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
227
				    FROM marine_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= marine_live.date ";
228
				if ($usecoord) $query .= "AND marine_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_live.longitude BETWEEN ".$minlong." AND ".$maxlong." ";
229
				$query .= "AND marine_live.latitude <> '0' AND marine_live.longitude <> '0' 
230
				ORDER BY marine_live.fammarine_id, marine_live.date";
231
				if ($limit) $query .= " LIMIT ".$globalMap3DMarinesLimit;
232
			}
233
		}
234
		try {
235
			$sth = $this->db->prepare($query);
236
			$sth->execute();
237
		} catch(PDOException $e) {
238
			echo $e->getMessage();
239
			die;
240
		}
241
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
242
		return $spotter_array;
243
	}
244
245
	/**
246
	* Gets number of latest data entry
247
	*
248
	* @return String number of entry
249
	*
250
	*/
251
	public function getLiveMarineCount($filter = array())
252
	{
253
		global $globalDBdriver, $globalLiveInterval;
254
		$filter_query = $this->getFilter($filter,true,true);
255
256
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
257
		if ($globalDBdriver == 'mysql') {
258
			$query = 'SELECT COUNT(DISTINCT marine_live.fammarine_id) as nb FROM marine_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date';
259
		} else {
260
			$query = "SELECT COUNT(DISTINCT marine_live.fammarine_id) as nb FROM marine_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= date";
261
		}
262
		try {
263
			$sth = $this->db->prepare($query);
264
			$sth->execute();
265
		} catch(PDOException $e) {
266
			echo $e->getMessage();
267
			die;
268
		}
269
		$result = $sth->fetch(PDO::FETCH_ASSOC);
270
		$sth->closeCursor();
271
		return $result['nb'];
272
	}
273
274
	/**
275
	* Gets all the spotter information based on the latest data entry and coord
276
	*
277
	* @return Array the spotter information
278
	*
279
	*/
280
	public function getLiveMarineDatabyCoord($coord, $filter = array())
281
	{
282
		global $globalDBdriver, $globalLiveInterval;
283
		$Marine = new Marine($this->db);
284
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
285
		$filter_query = $this->getFilter($filter);
286
287
		if (is_array($coord)) {
288
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
289
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
290
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
291
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
292
		} else return array();
293
		if ($globalDBdriver == 'mysql') {
294
			$query  = 'SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate AND marine_live.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND marine_live.longitude BETWEEN '.$minlong.' AND '.$maxlong.' GROUP BY marine_live.fammarine_id'.$filter_query;
295
		} else {
296
			$query  = "SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE NOW() at time zone 'UTC'  - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate AND marine_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_live.longitude BETWEEN ".$minlong." AND ".$maxlong." GROUP BY marine_live.fammarine_id".$filter_query;
297
		}
298
		$spotter_array = $Marine->getDataFromDB($query);
299
		return $spotter_array;
300
	}
301
302
	/**
303
	* Gets all the spotter information based on the latest data entry and coord
304
	*
305
	* @return Array the spotter information
306
	*
307
	*/
308
	public function getMinLiveMarineDatabyCoord($coord, $filter = array())
309
	{
310
		global $globalDBdriver, $globalLiveInterval;
311
		$Marine = new Marine($this->db);
312
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
313
		$filter_query = $this->getFilter($filter);
314
315
		if (is_array($coord)) {
316
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
317
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
318
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
319
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
320
		} else return array();
321
		/*
322
		if ($globalDBdriver == 'mysql') {
323
			$query  = 'SELECT marine_live.ident, marine_live.fammarine_id,marine_live.type, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
324
			FROM marine_live'.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval." SECOND) <= marine_live.date AND marine_live.latitude <> '0' AND marine_live.longitude <> '0' AND marine_live.latitude BETWEEN ".$minlat.' AND '.$maxlat.' AND marine_live.longitude BETWEEN '.$minlong.' AND '.$maxlong."
325
			ORDER BY marine_live.fammarine_id, marine_live.date";
326
		} else {
327
			$query  = "SELECT marine_live.ident, marine_live.fammarine_id, marine_live.type,marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
328
			FROM marine_live".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= marine_live.date AND marine_live.latitude <> '0' AND marine_live.longitude <> '0' AND marine_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND marine_live.longitude BETWEEN ".$minlong." AND ".$maxlong."
329
			ORDER BY marine_live.fammarine_id, marine_live.date";
330
		}
331
		*/
332
		if ($globalDBdriver == 'mysql') {
333
			if (isset($globalArchive) && $globalArchive === TRUE) {
0 ignored issues
show
Bug introduced by
The variable $globalArchive seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
334
				$query  = 'SELECT marine_live.ident, marine_live.fammarine_id,marine_live.type, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
335
				    FROM marine_live 
336
				    '.$filter_query.' DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= marine_live.date 
337
				    AND marine_live.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND marine_live.longitude BETWEEN '.$minlong.' AND '.$maxlong.'
338
				    AND marine_live.latitude <> 0 AND marine_live.longitude <> 0';
339
			} else {
340
				$query  = 'SELECT marine_live.ident, marine_live.fammarine_id,marine_live.type, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
341
				    FROM marine_live 
342
				    INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate 
343
				    FROM marine_live l 
344
				    WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date 
345
				    AND l.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND l.longitude BETWEEN '.$minlong.' AND '.$maxlong.'
346
				    GROUP BY l.fammarine_id
347
				    ) s on marine_live.fammarine_id = s.fammarine_id 
348
				    AND marine_live.date = s.maxdate'.$filter_query.' marine_live.latitude <> 0 AND marine_live.longitude <> 0';
349
			}
350
		} else {
351
			if (isset($globalArchive) && $globalArchive === TRUE) {
352
				$query  = "SELECT marine_live.ident, marine_live.fammarine_id,marine_live.type, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
353
				    FROM marine_live 
354
				    ".$filter_query." CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= marine_live.date 
355
				    AND marine_live.latitude BETWEEN ".$minlat." AND ".$maxlat." 
356
				    AND marine_live.longitude BETWEEN ".$minlong." AND ".$maxlong." 
357
				    AND marine_live.latitude <> '0' AND marine_live.longitude <> '0'";
358
			} else {
359
				$query  = "SELECT marine_live.ident, marine_live.fammarine_id,marine_live.type, marine_live.latitude, marine_live.longitude, marine_live.heading, marine_live.ground_speed, marine_live.date, marine_live.format_source 
360
				    FROM marine_live 
361
				    INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate 
362
				    FROM marine_live l 
363
				    WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date 
364
				    AND l.latitude BETWEEN ".$minlat." AND ".$maxlat." 
365
				    AND l.longitude BETWEEN ".$minlong." AND ".$maxlong." 
366
				    GROUP BY l.fammarine_id
367
				    ) s on marine_live.fammarine_id = s.fammarine_id 
368
				    AND marine_live.date = s.maxdate".$filter_query." marine_live.latitude <> '0' AND marine_live.longitude <> '0'";
369
			}
370
		}
371
		$spotter_array = $Marine->getDataFromDB($query);
372
		return $spotter_array;
373
	}
374
375
	/**
376
	* Gets all the spotter information based on a user's latitude and longitude
377
	*
378
	* @return Array the spotter information
379
	*
380
	*/
381
	public function getLatestMarineForLayar($lat, $lng, $radius, $interval)
382
	{
383
		$Marine = new Marine($this->db);
384
		date_default_timezone_set('UTC');
385
		if ($lat != '') {
386
			if (!is_numeric($lat)) {
387
				return false;
388
			}
389
		}
390
		if ($lng != '')
391
		{
392
			if (!is_numeric($lng))
393
                        {
394
                                return false;
395
                        }
396
                }
397
398
                if ($radius != '')
399
                {
400
                        if (!is_numeric($radius))
401
                        {
402
                                return false;
403
                        }
404
                }
405
		$additional_query = '';
406
		if ($interval != '')
407
                {
408
                        if (!is_string($interval))
409
                        {
410
                                //$additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= marine_live.date ';
411
			        return false;
412
                        } else {
413
                if ($interval == '1m')
414
                {
415
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= marine_live.date ';
416
                } else if ($interval == '15m'){
417
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 15 MINUTE) <= marine_live.date ';
418
                } 
419
            }
420
                } else {
421
         $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= marine_live.date ';   
422
        }
423
424
                $query  = "SELECT marine_live.*, ( 6371 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lng) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM marine_live 
425
                   WHERE marine_live.latitude <> '' 
426
                                   AND marine_live.longitude <> '' 
427
                   ".$additional_query."
428
                   HAVING distance < :radius  
429
                                   ORDER BY distance";
430
431
                $spotter_array = $Marine->getDataFromDB($query, array(':lat' => $lat, ':lng' => $lng,':radius' => $radius));
432
433
                return $spotter_array;
434
        }
435
436
    
437
        /**
438
	* Gets all the spotter information based on a particular callsign
439
	*
440
	* @return Array the spotter information
441
	*
442
	*/
443
	public function getLastLiveMarineDataByIdent($ident)
444
	{
445
		$Marine = new Marine($this->db);
446
		date_default_timezone_set('UTC');
447
448
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
449
                $query  = 'SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE l.ident = :ident GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate ORDER BY marine_live.date DESC';
450
451
		$spotter_array = $Marine->getDataFromDB($query,array(':ident' => $ident),'',true);
452
453
		return $spotter_array;
454
	}
455
456
        /**
457
	* Gets all the spotter information based on a particular callsign
458
	*
459
	* @return Array the spotter information
460
	*
461
	*/
462
	public function getDateLiveMarineDataByIdent($ident,$date)
463
	{
464
		$Marine = new Marine($this->db);
465
		date_default_timezone_set('UTC');
466
467
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
468
                $query  = 'SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE l.ident = :ident AND l.date <= :date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate ORDER BY marine_live.date DESC';
469
470
                $date = date('c',$date);
471
		$spotter_array = $Marine->getDataFromDB($query,array(':ident' => $ident,':date' => $date));
472
473
		return $spotter_array;
474
	}
475
476
        /**
477
	* Gets last spotter information based on a particular callsign
478
	*
479
	* @return Array the spotter information
480
	*
481
	*/
482
	public function getLastLiveMarineDataById($id)
483
	{
484
		$Marine = new Marine($this->db);
485
		date_default_timezone_set('UTC');
486
487
		$id = filter_var($id, FILTER_SANITIZE_STRING);
488
                $query  = 'SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE l.fammarine_id = :id GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate ORDER BY marine_live.date DESC';
489
490
		$spotter_array = $Marine->getDataFromDB($query,array(':id' => $id),'',true);
491
492
		return $spotter_array;
493
	}
494
495
        /**
496
	* Gets last spotter information based on a particular callsign
497
	*
498
	* @return Array the spotter information
499
	*
500
	*/
501
	public function getDateLiveMarineDataById($id,$date)
502
	{
503
		$Marine = new Marine($this->db);
504
		date_default_timezone_set('UTC');
505
506
		$id = filter_var($id, FILTER_SANITIZE_STRING);
507
                $query  = 'SELECT marine_live.* FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l WHERE l.fammarine_id = :id AND l.date <= :date GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate ORDER BY marine_live.date DESC';
508
                $date = date('c',$date);
509
		$spotter_array = $Marine->getDataFromDB($query,array(':id' => $id,':date' => $date),'',true);
510
511
		return $spotter_array;
512
	}
513
514
515
        /**
516
	* Gets all the spotter information based on a particular id
517
	*
518
	* @return Array the spotter information
519
	*
520
	*/
521
	public function getAllLiveMarineDataById($id,$liveinterval = false)
522
	{
523
		global $globalDBdriver, $globalLiveInterval;
524
		date_default_timezone_set('UTC');
525
		$id = filter_var($id, FILTER_SANITIZE_STRING);
526
		//$query  = self::$global_query.' WHERE marine_live.fammarine_id = :id ORDER BY date';
527
		if ($globalDBdriver == 'mysql') {
528
			$query = 'SELECT marine_live.* FROM marine_live WHERE marine_live.fammarine_id = :id';
529
			if ($liveinterval) $query .= ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date';
530
			$query .= ' ORDER BY date';
531
		} else {
532
			$query = 'SELECT marine_live.* FROM marine_live WHERE marine_live.fammarine_id = :id';
533
			if ($liveinterval) $query .= " AND CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= date";
534
			$query .= ' ORDER BY date';
535
		}
536
537
		try {
538
			$sth = $this->db->prepare($query);
539
			$sth->execute(array(':id' => $id));
540
		} catch(PDOException $e) {
541
			echo $e->getMessage();
542
			die;
543
		}
544
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
545
		return $spotter_array;
546
	}
547
548
        /**
549
	* Gets all the spotter information based on a particular ident
550
	*
551
	* @return Array the spotter information
552
	*
553
	*/
554
	public function getAllLiveMarineDataByIdent($ident)
555
	{
556
		date_default_timezone_set('UTC');
557
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
558
		$query  = self::$global_query.' WHERE marine_live.ident = :ident';
559
    		try {
560
			
561
			$sth = $this->db->prepare($query);
562
			$sth->execute(array(':ident' => $ident));
563
		} catch(PDOException $e) {
564
			echo $e->getMessage();
565
			die;
566
		}
567
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
568
		return $spotter_array;
569
	}
570
571
572
	/**
573
	* Deletes all info in the table
574
	*
575
	* @return String success or false
576
	*
577
	*/
578
	public function deleteLiveMarineData()
579
	{
580
		global $globalDBdriver;
581
		if ($globalDBdriver == 'mysql') {
582
			//$query  = "DELETE FROM marine_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE) >= marine_live.date";
583
			$query  = 'DELETE FROM marine_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 9 HOUR) >= marine_live.date';
584
            		//$query  = "DELETE FROM marine_live WHERE marine_live.id IN (SELECT marine_live.id FROM marine_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_live l GROUP BY l.fammarine_id) s on marine_live.fammarine_id = s.fammarine_id AND marine_live.date = s.maxdate AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) >= marine_live.date)";
585
		} else {
586
			$query  = "DELETE FROM marine_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= marine_live.date";
587
		}
588
        
589
    		try {
590
			
591
			$sth = $this->db->prepare($query);
592
			$sth->execute();
593
		} catch(PDOException $e) {
594
			return "error";
595
		}
596
597
		return "success";
598
	}
599
600
	/**
601
	* Deletes all info in the table for aircraft not seen since 2 HOUR
602
	*
603
	* @return String success or false
604
	*
605
	*/
606
	public function deleteLiveMarineDataNotUpdated()
607
	{
608
		global $globalDBdriver, $globalDebug;
609
		if ($globalDBdriver == 'mysql') {
610
			//$query = 'SELECT fammarine_id FROM marine_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) >= marine_live.date AND marine_live.fammarine_id NOT IN (SELECT fammarine_id FROM marine_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) < marine_live.date) LIMIT 800 OFFSET 0';
611
    			$query = "SELECT marine_live.fammarine_id FROM marine_live INNER JOIN (SELECT fammarine_id,MAX(date) as max_date FROM marine_live GROUP BY fammarine_id) s ON s.fammarine_id = marine_live.fammarine_id AND DATE_SUB(UTC_TIMESTAMP(), INTERVAL 2 HOUR) >= s.max_date LIMIT 1200 OFFSET 0";
612
    			try {
613
				
614
				$sth = $this->db->prepare($query);
615
				$sth->execute();
616
			} catch(PDOException $e) {
617
				return "error";
618
			}
619
			$query_delete = 'DELETE FROM marine_live WHERE fammarine_id IN (';
620
                        $i = 0;
621
                        $j =0;
622
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
623
			foreach($all as $row)
624
			{
625
				$i++;
626
				$j++;
627
				if ($j == 30) {
628
					if ($globalDebug) echo ".";
629
				    	try {
630
						
631
						$sth = $this->db->prepare(substr($query_delete,0,-1).")");
632
						$sth->execute();
633
					} catch(PDOException $e) {
634
						return "error";
635
					}
636
                                	$query_delete = 'DELETE FROM marine_live WHERE fammarine_id IN (';
637
                                	$j = 0;
638
				}
639
				$query_delete .= "'".$row['fammarine_id']."',";
640
			}
641
			if ($i > 0) {
642
    				try {
643
					
644
					$sth = $this->db->prepare(substr($query_delete,0,-1).")");
645
					$sth->execute();
646
				} catch(PDOException $e) {
647
					return "error";
648
				}
649
			}
650
			return "success";
651
		} elseif ($globalDBdriver == 'pgsql') {
652
			//$query = "SELECT fammarine_id FROM marine_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= marine_live.date AND marine_live.fammarine_id NOT IN (SELECT fammarine_id FROM marine_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' < marine_live.date) LIMIT 800 OFFSET 0";
653
    			//$query = "SELECT marine_live.fammarine_id FROM marine_live INNER JOIN (SELECT fammarine_id,MAX(date) as max_date FROM marine_live GROUP BY fammarine_id) s ON s.fammarine_id = marine_live.fammarine_id AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 800 OFFSET 0";
654
    			$query = "DELETE FROM marine_live WHERE fammarine_id IN (SELECT marine_live.fammarine_id FROM marine_live INNER JOIN (SELECT fammarine_id,MAX(date) as max_date FROM marine_live GROUP BY fammarine_id) s ON s.fammarine_id = marine_live.fammarine_id AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 800 OFFSET 0)";
655
    			try {
656
				
657
				$sth = $this->db->prepare($query);
658
				$sth->execute();
659
			} catch(PDOException $e) {
660
				return "error";
661
			}
662
/*			$query_delete = "DELETE FROM marine_live WHERE fammarine_id IN (";
663
                        $i = 0;
664
                        $j =0;
665
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
666
			foreach($all as $row)
667
			{
668
				$i++;
669
				$j++;
670
				if ($j == 100) {
671
					if ($globalDebug) echo ".";
672
				    	try {
673
						
674
						$sth = $this->db->query(substr($query_delete,0,-1).")");
675
						//$sth->execute();
676
					} catch(PDOException $e) {
677
						return "error";
678
					}
679
                                	$query_delete = "DELETE FROM marine_live WHERE fammarine_id IN (";
680
                                	$j = 0;
681
				}
682
				$query_delete .= "'".$row['fammarine_id']."',";
683
			}
684
			if ($i > 0) {
685
    				try {
686
					
687
					$sth = $this->db->query(substr($query_delete,0,-1).")");
688
					//$sth->execute();
689
				} catch(PDOException $e) {
690
					return "error";
691
				}
692
			}
693
*/
694
			return "success";
695
		}
696
	}
697
698
	/**
699
	* Deletes all info in the table for an ident
700
	*
701
	* @return String success or false
702
	*
703
	*/
704
	public function deleteLiveMarineDataByIdent($ident)
705
	{
706
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
707
		$query  = 'DELETE FROM marine_live WHERE ident = :ident';
708
        
709
    		try {
710
			
711
			$sth = $this->db->prepare($query);
712
			$sth->execute(array(':ident' => $ident));
713
		} catch(PDOException $e) {
714
			return "error";
715
		}
716
717
		return "success";
718
	}
719
720
	/**
721
	* Deletes all info in the table for an id
722
	*
723
	* @return String success or false
724
	*
725
	*/
726
	public function deleteLiveMarineDataById($id)
727
	{
728
		$id = filter_var($id, FILTER_SANITIZE_STRING);
729
		$query  = 'DELETE FROM marine_live WHERE fammarine_id = :id';
730
        
731
    		try {
732
			
733
			$sth = $this->db->prepare($query);
734
			$sth->execute(array(':id' => $id));
735
		} catch(PDOException $e) {
736
			return "error";
737
		}
738
739
		return "success";
740
	}
741
742
743
	/**
744
	* Gets the aircraft ident within the last hour
745
	*
746
	* @return String the ident
747
	*
748
	*/
749
	public function getIdentFromLastHour($ident)
750
	{
751
		global $globalDBdriver, $globalTimezone;
752
		if ($globalDBdriver == 'mysql') {
753
			$query  = 'SELECT marine_live.ident FROM marine_live 
754
				WHERE marine_live.ident = :ident 
755
				AND marine_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
756
				AND marine_live.date < UTC_TIMESTAMP()';
757
			$query_data = array(':ident' => $ident);
758
		} else {
759
			$query  = "SELECT marine_live.ident FROM marine_live 
760
				WHERE marine_live.ident = :ident 
761
				AND marine_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
762
				AND marine_live.date < now() AT TIME ZONE 'UTC'";
763
			$query_data = array(':ident' => $ident);
764
		}
765
		
766
		$sth = $this->db->prepare($query);
767
		$sth->execute($query_data);
768
		$ident_result='';
769
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
770
		{
771
			$ident_result = $row['ident'];
772
		}
773
		return $ident_result;
774
        }
775
776
	/**
777
	* Check recent aircraft
778
	*
779
	* @return String the ident
780
	*
781
	*/
782
	public function checkIdentRecent($ident)
783
	{
784
		global $globalDBdriver, $globalTimezone;
785
		if ($globalDBdriver == 'mysql') {
786
			$query  = 'SELECT marine_live.ident, marine_live.fammarine_id FROM marine_live 
787
				WHERE marine_live.ident = :ident 
788
				AND marine_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE)'; 
789
//				AND marine_live.date < UTC_TIMESTAMP()";
790
			$query_data = array(':ident' => $ident);
791
		} else {
792
			$query  = "SELECT marine_live.ident, marine_live.fammarine_id FROM marine_live 
793
				WHERE marine_live.ident = :ident 
794
				AND marine_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '30 MINUTES'";
795
//				AND marine_live.date < now() AT TIME ZONE 'UTC'";
796
			$query_data = array(':ident' => $ident);
797
		}
798
		
799
		$sth = $this->db->prepare($query);
800
		$sth->execute($query_data);
801
		$ident_result='';
802
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
803
		{
804
			$ident_result = $row['fammarine_id'];
805
		}
806
		return $ident_result;
807
        }
808
809
	/**
810
	* Check recent aircraft by id
811
	*
812
	* @return String the ident
813
	*
814
	*/
815
	public function checkIdRecent($id)
816
	{
817
		global $globalDBdriver, $globalTimezone;
818
		if ($globalDBdriver == 'mysql') {
819
			$query  = 'SELECT marine_live.ident, marine_live.fammarine_id FROM marine_live 
820
				WHERE marine_live.fammarine_id = :id 
821
				AND marine_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 10 HOUR)'; 
822
//				AND marine_live.date < UTC_TIMESTAMP()";
823
			$query_data = array(':id' => $id);
824
		} else {
825
			$query  = "SELECT marine_live.ident, marine_live.fammarine_id FROM marine_live 
826
				WHERE marine_live.fammarine_id = :id 
827
				AND marine_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '10 HOUR'";
828
//				AND marine_live.date < now() AT TIME ZONE 'UTC'";
829
			$query_data = array(':id' => $id);
830
		}
831
		
832
		$sth = $this->db->prepare($query);
833
		$sth->execute($query_data);
834
		$ident_result='';
835
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
836
		{
837
			$ident_result = $row['fammarine_id'];
838
		}
839
		return $ident_result;
840
        }
841
842
	/**
843
	* Check recent aircraft by mmsi
844
	*
845
	* @return String the ident
846
	*
847
	*/
848
	public function checkMMSIRecent($mmsi)
849
	{
850
		global $globalDBdriver, $globalTimezone;
851
		if ($globalDBdriver == 'mysql') {
852
			$query  = 'SELECT marine_live.fammarine_id FROM marine_live 
853
				WHERE marine_live.mmsi = :mmsi 
854
				AND marine_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 10 HOUR)'; 
855
//				AND marine_live.date < UTC_TIMESTAMP()";
856
			$query_data = array(':mmsi' => $mmsi);
857
		} else {
858
			$query  = "SELECT marine_live.fammarine_id FROM marine_live 
859
				WHERE marine_live.mmsi = :mmsi 
860
				AND marine_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '10 HOUR'";
861
//				AND marine_live.date < now() AT TIME ZONE 'UTC'";
862
			$query_data = array(':mmsi' => $mmsi);
863
		}
864
		
865
		$sth = $this->db->prepare($query);
866
		$sth->execute($query_data);
867
		$ident_result='';
868
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
869
		{
870
			$ident_result = $row['fammarine_id'];
871
		}
872
		return $ident_result;
873
        }
874
875
	/**
876
	* Adds a new spotter data
877
	*
878
	* @param String $fammarine_id the ID from flightaware
879
	* @param String $ident the flight ident
880
	* @param String $aircraft_icao the aircraft type
0 ignored issues
show
Bug introduced by
There is no parameter named $aircraft_icao. Was it maybe removed?

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

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

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

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

Loading history...
881
	* @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...
882
	* @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...
883
	* @return String success or false
884
	*
885
	*/
886
	public function addLiveMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $heading = '', $groundspeed = '', $date = '', $putinarchive = false, $mmsi = '',$type = '',$typeid = '',$imo = '', $callsign = '',$arrival_code = '',$arrival_date = '',$status = '',$statusid = '',$noarchive = false,$format_source = '', $source_name = '', $over_country = '')
0 ignored issues
show
Unused Code introduced by
The parameter $statusid 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...
887
	{
888
		global $globalURL, $globalArchive, $globalDebug;
889
		$Common = new Common();
890
		date_default_timezone_set('UTC');
891
892
		//getting the airline information
893
		if ($ident != '')
894
		{
895
			if (!is_string($ident))
896
			{
897
				return false;
898
			} 
899
		}
900
901
902
		if ($latitude != '')
903
		{
904
			if (!is_numeric($latitude))
905
			{
906
				return false;
907
			}
908
		} else return '';
909
910
		if ($longitude != '')
911
		{
912
			if (!is_numeric($longitude))
913
			{
914
				return false;
915
			}
916
		} else return '';
917
918
919
		if ($heading != '')
920
		{
921
			if (!is_numeric($heading))
922
			{
923
				return false;
924
			}
925
		} else $heading = 0;
926
927
		if ($groundspeed != '')
928
		{
929
			if (!is_numeric($groundspeed))
930
			{
931
				return false;
932
			}
933
		} else $groundspeed = 0;
934
		date_default_timezone_set('UTC');
935
		if ($date == '') $date = date("Y-m-d H:i:s", time());
936
937
        
938
		$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING);
939
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
940
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
941
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
942
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
943
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
944
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
945
		$source_name = filter_var($source_name,FILTER_SANITIZE_STRING);
946
		$over_country = filter_var($over_country,FILTER_SANITIZE_STRING);
947
		$type = filter_var($type,FILTER_SANITIZE_STRING);
948
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT);
949
		$status = filter_var($status,FILTER_SANITIZE_STRING);
950
		$imo = filter_var($imo,FILTER_SANITIZE_STRING);
951
		$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
952
		$arrival_code = filter_var($arrival_code,FILTER_SANITIZE_STRING);
953
		$arrival_date = filter_var($arrival_date,FILTER_SANITIZE_STRING);
954
		
955
956
            	if ($groundspeed == '' || $Common->isInteger($groundspeed) === false ) $groundspeed = 0;
957
            	if ($heading == '' || $Common->isInteger($heading) === false ) $heading = 0;
958
            	if ($arrival_date == '') $arrival_date = NULL;
959
            	$query = '';
960
		if ($globalArchive) {
961
			if ($globalDebug) echo '-- Delete previous data -- ';
962
			$query .= 'DELETE FROM marine_live WHERE fammarine_id = :fammarine_id;';
963
		}
964
		$query .= 'INSERT INTO marine_live (fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, over_country, mmsi, type,status,imo,arrival_port_name,arrival_port_date) 
965
		    VALUES (:fammarine_id,:ident,:latitude,:longitude,:heading,:groundspeed,:date,:format_source, :source_name, :over_country,:mmsi,:type,:status,:imo,:arrival_port_name,:arrival_port_date)';
966
967
		$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':heading' => $heading,':groundspeed' => $groundspeed,':date' => $date, ':format_source' => $format_source, ':source_name' => $source_name, ':over_country' => $over_country,':mmsi' => $mmsi,':type' => $type,':status' => $status,':imo' => $imo,':arrival_port_name' => $arrival_code,':arrival_port_date' => $arrival_date);
968
		try {
969
			$sth = $this->db->prepare($query);
970
			$sth->execute($query_values);
971
			$sth->closeCursor();
972
		} catch(PDOException $e) {
973
			return "error : ".$e->getMessage();
974
		}
975
		
976
		if (isset($globalArchive) && $globalArchive && $putinarchive && $noarchive !== true) {
977
			if ($globalDebug) echo '(Add to Marine archive : ';
978
			$MarineArchive = new MarineArchive($this->db);
979
			$result =  $MarineArchive->addMarineArchiveData($fammarine_id, $ident, $latitude, $longitude, $heading, $groundspeed, $date, $putinarchive, $mmsi,$type,$typeid,$imo, $callsign,$arrival_code,$arrival_date,$status,$noarchive,$format_source, $source_name, $over_country);
0 ignored issues
show
Documentation introduced by
$noarchive is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
980
			if ($globalDebug) echo $result.')';
981
		}
982
		return "success";
983
	}
984
985
	public function getOrderBy()
986
	{
987
		$orderby = array("aircraft_asc" => array("key" => "aircraft_asc", "value" => "Aircraft Type - ASC", "sql" => "ORDER BY marine_live.aircraft_icao ASC"), "aircraft_desc" => array("key" => "aircraft_desc", "value" => "Aircraft Type - DESC", "sql" => "ORDER BY marine_live.aircraft_icao DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY marine_live.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY marine_live.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY marine_live.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY marine_live.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY marine_live.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY marine_live.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure Airport - ASC", "sql" => "ORDER BY marine_live.departure_airport_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY marine_live.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY marine_live.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY marine_live.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY marine_live.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY marine_live.date DESC"));
988
		return $orderby;
989
	}
990
991
}
992
993
994
?>
995