Completed
Push — master ( 9a1a14...9882b2 )
by Yannick
05:46
created

SpotterLive::getLiveSpotterData()   F

Complexity

Conditions 15
Paths 384

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 31
nc 384
nop 3
dl 0
loc 49
rs 3.8226
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
//$global_query = "SELECT spotter_live.* FROM spotter_live";
3
4
class SpotterLive {
5
	public $db;
6
	static $global_query = "SELECT spotter_live.* FROM spotter_live";
7
8
	public function __construct($dbc = null) {
9
		$Connection = new Connection($dbc);
10
		$this->db = $Connection->db();
11
	}
12
	    
13
	/**
14
	* Gets all the spotter information based on the latest data entry
15
	*
16
	* @return Array the spotter information
17
	*
18
	*/
19
	public function getLiveSpotterData($limit = '', $sort = '', $filter = array())
20
	{
21
		global $globalDBdriver, $globalLiveInterval;
22
		$Spotter = new Spotter($this->db);
23
		date_default_timezone_set('UTC');
24
25
		$filter_query = '';
26
		if (isset($filter['source']) && !empty($filter['source'])) {
27
			$filter_query = " AND format_source IN ('".implode("','",$filter['source'])."')";
28
		}
29
		if (isset($filter['airlines']) && !empty($filter['airlines'])) {
30
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.flightaware_id = spotter_live.flightaware_id";
31
		}
32
		if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
33
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_live.flightaware_id ";
34
		}
35
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
36
			$filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
37
		}
38
		
39
		$limit_query = '';
40
		if ($limit != '')
41
		{
42
			$limit_array = explode(',', $limit);
43
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
44
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
45
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
46
			{
47
				$limit_query = ' LIMIT '.$limit_array[1].' OFFSET '.$limit_array[0];
48
			}
49
		}
50
		$orderby_query = '';
51
		if ($sort != '')
52
		{
53
			$search_orderby_array = $this->getOrderBy();
54
			$orderby_query = ' '.$search_orderby_array[$sort]['sql'];
55
		}
56
57
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
58
		if ($globalDBdriver == 'mysql') {
59
			//$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate";
60
			$query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate'.$filter_query.$orderby_query;
61
                } else {
62
			$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate".$filter_query.$orderby_query;
63
		}
64
		$spotter_array = $Spotter->getDataFromDB($query.$limit_query);
65
66
		return $spotter_array;
67
	}
68
69
	/**
70
	* Gets Minimal Live Spotter data
71
	*
72
	* @return Array the spotter information
73
	*
74
	*/
75
	public function getMinLiveSpotterData($filter = array())
76
	{
77
		global $globalDBdriver, $globalLiveInterval;
78
		date_default_timezone_set('UTC');
79
80
		$filter_query = '';
81
		if (isset($filter['source']) && !empty($filter['source'])) {
82
			$filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
83
		}
84
		if (isset($filter['airlines']) && !empty($filter['airlines'])) {
85
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.flightaware_id = spotter_live.flightaware_id ";
86
		}
87
		if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
88
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_live.flightaware_id ";
89
		}
90
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
91
			$filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
92
		}
93
94
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
95
		if ($globalDBdriver == 'mysql') {
96
//			$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL ".$globalLiveInterval." SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate$orderby_query";
97
//			$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, a.aircraft_shadow FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate INNER JOIN (SELECT * FROM aircraft) a on spotter_live.aircraft_icao = a.icao';
98
//			$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate'.$filter_query;
99
100
			$query  = 'SELECT a.aircraft_shadow, spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source 
101
			FROM spotter_live 
102
			INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_live.aircraft_icao = a.icao';
103
104
//			$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= spotter_live.date ORDER BY spotter_live.date GROUP BY spotter_live.flightaware_id'.$filter_query;
105
106
			//$query = 'SELECT a.aircraft_shadow, spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk  FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_live.aircraft_icao = a.icao';
107
                } else {
108
            		//$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE NOW() AT TIME ZONE 'UTC' - '30 SECONDS'->INTERVAL <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate";
109
            		//$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE NOW() AT TIME ZONE 'UTC' - '".$globalLiveInterval." SECONDS'->INTERVAL <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate$orderby_query";
110
			//$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, a.aircraft_shadow FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate '.$filter_query.'INNER JOIN (SELECT * FROM aircraft) a on spotter_live.aircraft_icao = a.icao';
111
			$query  = "SELECT a.aircraft_shadow, spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source 
112
			FROM spotter_live
113
			INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ".$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_live.aircraft_icao = a.icao';
114
		}
115
//		$spotter_array = Spotter->getDataFromDB($query.$limit_query);
116
		//echo $query;
117
118
    		try {
119
			$sth = $this->db->prepare($query);
120
			$sth->execute();
121
		} catch(PDOException $e) {
122
			echo $e->getMessage();
123
			die;
124
		}
125
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
126
127
		return $spotter_array;
128
	}
129
130
	/**
131
	* Gets Minimal Live Spotter data since xx seconds
132
	*
133
	* @return Array the spotter information
134
	*
135
	*/
136
	public function getMinLastLiveSpotterData($filter = array())
137
	{
138
		global $globalDBdriver, $globalLiveInterval;
139
		date_default_timezone_set('UTC');
140
141
		$filter_query = '';
142
		if (isset($filter['source']) && !empty($filter['source'])) {
143
			$filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
144
		}
145
		if (isset($filter['airlines']) && !empty($filter['airlines'])) {
146
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.flightaware_id = spotter_live.flightaware_id ";
147
		}
148
		if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
149
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_live.flightaware_id ";
150
		}
151
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
152
			$filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
153
		}
154
155
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
156
		if ($globalDBdriver == 'mysql') {
157
158
			$query  = 'SELECT a.aircraft_shadow, a.engine_type, a.engine_count, a.wake_category, spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source 
159
			FROM spotter_live LEFT JOIN (SELECT aircraft_shadow,engine_type, engine_count, wake_category,icao FROM aircraft) a ON spotter_live.aircraft_icao = a.icao WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= spotter_live.date
160
			'.$filter_query.'ORDER BY spotter_live.flightaware_id, spotter_live.date';
161
                } else if ($globalDBdriver == 'pgsql') {
162
/*
163
			$query  = "SELECT a.aircraft_shadow, spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source 
164
			FROM spotter_live WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date 
165
			".$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_live.aircraft_icao = a.icao ORDER BY spotter_live.flightaware_id, spotter_live.date';
166
*/
167
			$query  = "SELECT a.aircraft_shadow, a.engine_type, a.engine_count, a.wake_category, spotter_live.ident, spotter_live.flightaware_id, spotter_live.aircraft_icao, spotter_live.departure_airport_icao as departure_airport, spotter_live.arrival_airport_icao as arrival_airport, spotter_live.latitude, spotter_live.longitude, spotter_live.altitude, spotter_live.heading, spotter_live.ground_speed, spotter_live.squawk, spotter_live.date, spotter_live.format_source 
168
			FROM spotter_live LEFT JOIN (SELECT aircraft_shadow,engine_type, engine_count, wake_category, icao FROM aircraft) a ON spotter_live.aircraft_icao = a.icao WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= spotter_live.date 
169
			".$filter_query.'ORDER BY spotter_live.flightaware_id, spotter_live.date';
170
//			echo $query;
171
		}
172
173
    		try {
174
			$sth = $this->db->prepare($query);
0 ignored issues
show
Bug introduced by
The variable $query 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...
175
			$sth->execute();
176
		} catch(PDOException $e) {
177
			echo $e->getMessage();
178
			die;
179
180
		}
181
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
182
183
		return $spotter_array;
184
	}
185
186
	/**
187
	* Gets number of latest data entry
188
	*
189
	* @return String number of entry
190
	*
191
	*/
192
	public function getLiveSpotterCount($filter = array())
193
	{
194
		global $globalDBdriver, $globalLiveInterval;
195
		$filter_query = '';
196
		if (isset($filter['source']) && !empty($filter['source'])) {
197
			$filter_query = " AND format_source IN ('".implode("','",$filter['source'])."')";
198
		}
199
		if (isset($filter['airlines']) && !empty($filter['airlines'])) {
200
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.flightaware_id = spotter_live.flightaware_id";
201
		}
202
		if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
203
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_live.flightaware_id ";
204
		}
205
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
206
			$filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
207
		}
208
209
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
210
		if ($globalDBdriver == 'mysql') {
211
            		//$query  = 'SELECT COUNT(*) as nb FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate'.$filter_query;
212
			$query = 'SELECT COUNT(DISTINCT flightaware_id) as nb FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= date'.$filter_query;
213
            	} elseif ($globalDBdriver == 'pgsql') {
214
	                //$query  = "SELECT COUNT(*) as nb FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE NOW() AT TIME ZONE 'UTC' - '".$globalLiveInterval." SECONDS'->INTERVAL <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate".$filter_query;
215
			$query = "SELECT COUNT(DISTINCT flightaware_id) as nb FROM spotter_live WHERE CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalLiveInterval." SECONDS' <= date".$filter_query;
216
                }
217
    		try {
218
			$sth = $this->db->prepare($query);
0 ignored issues
show
Bug introduced by
The variable $query 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...
219
			$sth->execute();
220
		} catch(PDOException $e) {
221
			echo $e->getMessage();
222
			die;
223
		}
224
		$result = $sth->fetch(PDO::FETCH_ASSOC);
225
		return $result['nb'];
226
	}
227
228
	/**
229
	* Gets all the spotter information based on the latest data entry and coord
230
	*
231
	* @return Array the spotter information
232
	*
233
	*/
234
	public function getLiveSpotterDatabyCoord($coord, $filter = array())
235
	{
236
		global $globalDBdriver, $globalLiveInterval;
237
		$Spotter = new Spotter($this->db);
238
		if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
239
		$filter_query = '';
240
		if (isset($filter['source'])) {
241
			$filter_query = " AND format_source IN ('".implode(',',$filter['source'])."')";
242
		}
243
		if (isset($filter['airlines'])) {
244
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.flightaware_id = spotter_live.flightaware_id";
245
		}
246
		if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
247
			$filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_live.flightaware_id ";
248
		}
249
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
250
			$filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
251
		}
252
253
		if (is_array($coord)) {
254
                        $minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
255
                        $minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
256
                        $maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
257
                        $maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
258
                } else return array();
259
                if ($globalDBdriver == 'mysql') {
260
        		//$query  = "SELECT spotter_output.* FROM spotter_output WHERE spotter_output.flightaware_id IN (SELECT spotter_live.flightaware_id FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL ".$globalLiveInterval." SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate AND spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong.")";
261
        		$query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate AND spotter_live.latitude BETWEEN '.$minlat.' AND '.$maxlat.' AND spotter_live.longitude BETWEEN '.$minlong.' AND '.$maxlong.' GROUP BY spotter_live.flightaware_id'.$filter_query;
262
        	} else if ($globalDBdriver == 'pgsql') {
263
            		$query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE NOW() at time zone 'UTC'  - INTERVAL '".$globalLiveInterval." SECONDS' <= l.date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate AND spotter_live.latitude BETWEEN ".$minlat." AND ".$maxlat." AND spotter_live.longitude BETWEEN ".$minlong." AND ".$maxlong." GROUP BY spotter_live.flightaware_id".$filter_query;
264
                }
265
                $spotter_array = $Spotter->getDataFromDB($query);
0 ignored issues
show
Bug introduced by
The variable $query 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...
266
                return $spotter_array;
267
        }
268
269
	/**
270
        * Gets all the spotter information based on a user's latitude and longitude
271
        *
272
        * @return Array the spotter information
273
        *
274
        */
275
        public function getLatestSpotterForLayar($lat, $lng, $radius, $interval)
276
        {
277
    		$Spotter = new Spotter($this->db);
278
                date_default_timezone_set('UTC');
279
280
        if ($lat != '')
281
                {
282
                        if (!is_numeric($lat))
283
                        {
284
                                return false;
285
                        }
286
                }
287
        
288
        if ($lng != '')
289
                {
290
                        if (!is_numeric($lng))
291
                        {
292
                                return false;
293
                        }
294
                }
295
296
                if ($radius != '')
297
                {
298
                        if (!is_numeric($radius))
299
                        {
300
                                return false;
301
                        }
302
                }
303
        
304
        if ($interval != '')
305
                {
306
                        if (!is_string($interval))
307
                        {
308
                                $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= spotter_live.date ';
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...
309
                return false;
310
                        } else {
311
                if ($interval == '1m')
312
                {
313
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= spotter_live.date ';
314
                } else if ($interval == '15m'){
315
                    $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 15 MINUTE) <= spotter_live.date ';
316
                } 
317
            }
318
                } else {
319
         $additional_query = ' AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MINUTE) <= spotter_live.date ';   
320
        }
321
322
                $query  = "SELECT spotter_live.*, ( 6371 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lng) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM spotter_live 
323
                   WHERE spotter_live.latitude <> '' 
324
                                   AND spotter_live.longitude <> '' 
325
                   ".$additional_query."
0 ignored issues
show
Bug introduced by
The variable $additional_query 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...
326
                   HAVING distance < :radius  
327
                                   ORDER BY distance";
328
329
                $spotter_array = $Spotter->getDataFromDB($query, array(':lat' => $lat, ':lng' => $lng,':radius' => $radius),$limit_query);
0 ignored issues
show
Bug introduced by
The variable $limit_query does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
330
331
                return $spotter_array;
332
        }
333
334
    
335
        /**
336
	* Gets all the spotter information based on a particular callsign
337
	*
338
	* @return Array the spotter information
339
	*
340
	*/
341
	public function getLastLiveSpotterDataByIdent($ident)
342
	{
343
		$Spotter = new Spotter($this->db);
344
		date_default_timezone_set('UTC');
345
346
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
347
                $query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.ident = :ident GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
348
349
		$spotter_array = $Spotter->getDataFromDB($query,array(':ident' => $ident));
350
351
		return $spotter_array;
352
	}
353
354
        /**
355
	* Gets all the spotter information based on a particular callsign
356
	*
357
	* @return Array the spotter information
358
	*
359
	*/
360
	public function getDateLiveSpotterDataByIdent($ident,$date)
361
	{
362
		$Spotter = new Spotter($this->db);
363
		date_default_timezone_set('UTC');
364
365
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
366
                $query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.ident = :ident AND l.date <= :date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
367
368
                $date = date('c',$date);
369
		$spotter_array = $Spotter->getDataFromDB($query,array(':ident' => $ident,':date' => $date));
370
371
		return $spotter_array;
372
	}
373
374
        /**
375
	* Gets last spotter information based on a particular callsign
376
	*
377
	* @return Array the spotter information
378
	*
379
	*/
380
	public function getLastLiveSpotterDataById($id)
381
	{
382
		$Spotter = new Spotter($this->db);
383
		date_default_timezone_set('UTC');
384
385
		$id = filter_var($id, FILTER_SANITIZE_STRING);
386
                $query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.flightaware_id = :id GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
387
388
		$spotter_array = $Spotter->getDataFromDB($query,array(':id' => $id));
389
390
		return $spotter_array;
391
	}
392
393
        /**
394
	* Gets last spotter information based on a particular callsign
395
	*
396
	* @return Array the spotter information
397
	*
398
	*/
399
	public function getDateLiveSpotterDataById($id,$date)
400
	{
401
		$Spotter = new Spotter($this->db);
402
		date_default_timezone_set('UTC');
403
404
		$id = filter_var($id, FILTER_SANITIZE_STRING);
405
                $query  = 'SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l WHERE l.flightaware_id = :id AND l.date <= :date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate ORDER BY spotter_live.date DESC';
406
                $date = date('c',$date);
407
		$spotter_array = $Spotter->getDataFromDB($query,array(':id' => $id,':date' => $date));
408
409
		return $spotter_array;
410
	}
411
412
        /**
413
	* Gets altitude information based on a particular callsign
414
	*
415
	* @return Array the spotter information
416
	*
417
	*/
418
	public function getAltitudeLiveSpotterDataByIdent($ident)
419
	{
420
421
		date_default_timezone_set('UTC');
422
423
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
424
                $query  = 'SELECT spotter_live.altitude, spotter_live.date FROM spotter_live WHERE spotter_live.ident = :ident';
425
426
    		try {
427
			
428
			$sth = $this->db->prepare($query);
429
			$sth->execute(array(':ident' => $ident));
430
		} catch(PDOException $e) {
431
			return "error";
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'error'; (string) is incompatible with the return type documented by SpotterLive::getAltitudeLiveSpotterDataByIdent of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
432
		}
433
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
434
435
		return $spotter_array;
436
	}
437
438
        /**
439
	* Gets all the spotter information based on a particular id
440
	*
441
	* @return Array the spotter information
442
	*
443
	*/
444
	public function getAllLiveSpotterDataById($id)
445
	{
446
		date_default_timezone_set('UTC');
447
		$id = filter_var($id, FILTER_SANITIZE_STRING);
448
		$query  = self::$global_query.' WHERE spotter_live.flightaware_id = :id ORDER BY date';
449
//		$spotter_array = Spotter->getDataFromDB($query,array(':id' => $id));
450
451
    		try {
452
			
453
			$sth = $this->db->prepare($query);
454
			$sth->execute(array(':id' => $id));
455
		} catch(PDOException $e) {
456
			return "error";
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'error'; (string) is incompatible with the return type documented by SpotterLive::getAllLiveSpotterDataById of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
457
		}
458
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
459
460
		return $spotter_array;
461
	}
462
463
        /**
464
	* Gets all the spotter information based on a particular ident
465
	*
466
	* @return Array the spotter information
467
	*
468
	*/
469
	public function getAllLiveSpotterDataByIdent($ident)
470
	{
471
		date_default_timezone_set('UTC');
472
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
473
		$query  = self::$global_query.' WHERE spotter_live.ident = :ident';
474
    		try {
475
			
476
			$sth = $this->db->prepare($query);
477
			$sth->execute(array(':ident' => $ident));
478
		} catch(PDOException $e) {
479
			echo $e->getMessage();
480
			die;
481
		}
482
		$spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
483
		return $spotter_array;
484
	}
485
486
487
	/**
488
	* Deletes all info in the table
489
	*
490
	* @return String success or false
491
	*
492
	*/
493
	public function deleteLiveSpotterData()
494
	{
495
		global $globalDBdriver;
496
		if ($globalDBdriver == 'mysql') {
497
			//$query  = "DELETE FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE) >= spotter_live.date";
498
			$query  = 'DELETE FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 9 HOUR) >= spotter_live.date';
499
            		//$query  = "DELETE FROM spotter_live WHERE spotter_live.id IN (SELECT spotter_live.id FROM spotter_live INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_live l GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate AND DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) >= spotter_live.date)";
500
501
		} elseif ($globalDBdriver == 'pgsql') {
502
			$query  = "DELETE FROM spotter_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= spotter_live.date";
503
		}
504
        
505
    		try {
506
			
507
			$sth = $this->db->prepare($query);
0 ignored issues
show
Bug introduced by
The variable $query 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...
508
			$sth->execute();
509
		} catch(PDOException $e) {
510
			return "error";
511
		}
512
513
		return "success";
514
	}
515
516
	/**
517
	* Deletes all info in the table for aircraft not seen since 2 HOUR
518
	*
519
	* @return String success or false
520
	*
521
	*/
522
	public function deleteLiveSpotterDataNotUpdated()
523
	{
524
		global $globalDBdriver, $globalDebug;
525
		if ($globalDBdriver == 'mysql') {
526
			//$query = 'SELECT flightaware_id FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) >= spotter_live.date AND spotter_live.flightaware_id NOT IN (SELECT flightaware_id FROM spotter_live WHERE DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR) < spotter_live.date) LIMIT 800 OFFSET 0';
527
    			$query = "SELECT spotter_live.flightaware_id FROM spotter_live INNER JOIN (SELECT flightaware_id,MAX(date) as max_date FROM spotter_live GROUP BY flightaware_id) s ON s.flightaware_id = spotter_live.flightaware_id AND DATE_SUB(UTC_TIMESTAMP(), INTERVAL 2 HOUR) >= s.max_date LIMIT 1200 OFFSET 0";
528
    			try {
529
				
530
				$sth = $this->db->prepare($query);
531
				$sth->execute();
532
			} catch(PDOException $e) {
533
				return "error";
534
			}
535
			$query_delete = 'DELETE FROM spotter_live WHERE flightaware_id IN (';
536
                        $i = 0;
537
                        $j =0;
538
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
539
			foreach($all as $row)
540
			{
541
				$i++;
542
				$j++;
543
				if ($j == 30) {
544
					if ($globalDebug) echo ".";
545
				    	try {
546
						
547
						$sth = $this->db->prepare(substr($query_delete,0,-1).")");
548
						$sth->execute();
549
					} catch(PDOException $e) {
550
						return "error";
551
					}
552
                                	$query_delete = 'DELETE FROM spotter_live WHERE flightaware_id IN (';
553
                                	$j = 0;
554
				}
555
				$query_delete .= "'".$row['flightaware_id']."',";
556
			}
557
			if ($i > 0) {
558
    				try {
559
					
560
					$sth = $this->db->prepare(substr($query_delete,0,-1).")");
561
					$sth->execute();
562
				} catch(PDOException $e) {
563
					return "error";
564
				}
565
			}
566
			return "success";
567
		} elseif ($globalDBdriver == 'pgsql') {
568
			//$query = "SELECT flightaware_id FROM spotter_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' >= spotter_live.date AND spotter_live.flightaware_id NOT IN (SELECT flightaware_id FROM spotter_live WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '9 HOURS' < spotter_live.date) LIMIT 800 OFFSET 0";
569
    			//$query = "SELECT spotter_live.flightaware_id FROM spotter_live INNER JOIN (SELECT flightaware_id,MAX(date) as max_date FROM spotter_live GROUP BY flightaware_id) s ON s.flightaware_id = spotter_live.flightaware_id AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 800 OFFSET 0";
570
    			$query = "DELETE FROM spotter_live WHERE flightaware_id IN (SELECT spotter_live.flightaware_id FROM spotter_live INNER JOIN (SELECT flightaware_id,MAX(date) as max_date FROM spotter_live GROUP BY flightaware_id) s ON s.flightaware_id = spotter_live.flightaware_id AND NOW() AT TIME ZONE 'UTC' - INTERVAL '2 HOURS' >= s.max_date LIMIT 800 OFFSET 0)";
571
    			try {
572
				
573
				$sth = $this->db->prepare($query);
574
				$sth->execute();
575
			} catch(PDOException $e) {
576
				return "error";
577
			}
578
/*			$query_delete = "DELETE FROM spotter_live WHERE flightaware_id IN (";
579
                        $i = 0;
580
                        $j =0;
581
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
582
			foreach($all as $row)
583
			{
584
				$i++;
585
				$j++;
586
				if ($j == 100) {
587
					if ($globalDebug) echo ".";
588
				    	try {
589
						
590
						$sth = $this->db->query(substr($query_delete,0,-1).")");
591
						//$sth->execute();
592
					} catch(PDOException $e) {
593
						return "error";
594
					}
595
                                	$query_delete = "DELETE FROM spotter_live WHERE flightaware_id IN (";
596
                                	$j = 0;
597
				}
598
				$query_delete .= "'".$row['flightaware_id']."',";
599
			}
600
			if ($i > 0) {
601
    				try {
602
					
603
					$sth = $this->db->query(substr($query_delete,0,-1).")");
604
					//$sth->execute();
605
				} catch(PDOException $e) {
606
					return "error";
607
				}
608
			}
609
*/
610
			return "success";
611
		}
612
	}
613
614
	/**
615
	* Deletes all info in the table for an ident
616
	*
617
	* @return String success or false
618
	*
619
	*/
620
	public function deleteLiveSpotterDataByIdent($ident)
621
	{
622
		$ident = filter_var($ident, FILTER_SANITIZE_STRING);
623
		$query  = 'DELETE FROM spotter_live WHERE ident = :ident';
624
        
625
    		try {
626
			
627
			$sth = $this->db->prepare($query);
628
			$sth->execute(array(':ident' => $ident));
629
		} catch(PDOException $e) {
630
			return "error";
631
		}
632
633
		return "success";
634
	}
635
636
	/**
637
	* Deletes all info in the table for an id
638
	*
639
	* @return String success or false
640
	*
641
	*/
642
	public function deleteLiveSpotterDataById($id)
643
	{
644
		$id = filter_var($id, FILTER_SANITIZE_STRING);
645
		$query  = 'DELETE FROM spotter_live WHERE flightaware_id = :id';
646
        
647
    		try {
648
			
649
			$sth = $this->db->prepare($query);
650
			$sth->execute(array(':id' => $id));
651
		} catch(PDOException $e) {
652
			return "error";
653
		}
654
655
		return "success";
656
	}
657
658
659
	/**
660
	* Gets the aircraft ident within the last hour
661
	*
662
	* @return String the ident
663
	*
664
	*/
665
	public function getIdentFromLastHour($ident)
666
	{
667
		global $globalDBdriver, $globalTimezone;
668
		if ($globalDBdriver == 'mysql') {
669
			$query  = 'SELECT spotter_live.ident FROM spotter_live 
670
				WHERE spotter_live.ident = :ident 
671
				AND spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
672
				AND spotter_live.date < UTC_TIMESTAMP()';
673
			$query_data = array(':ident' => $ident);
674
		} elseif ($globalDBdriver == 'pgsql') {
675
			$query  = "SELECT spotter_live.ident FROM spotter_live 
676
				WHERE spotter_live.ident = :ident 
677
				AND spotter_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
678
				AND spotter_live.date < now() AT TIME ZONE 'UTC'";
679
			$query_data = array(':ident' => $ident);
680
		}
681
		
682
		$sth = $this->db->prepare($query);
0 ignored issues
show
Bug introduced by
The variable $query 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...
683
		$sth->execute($query_data);
0 ignored issues
show
Bug introduced by
The variable $query_data 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...
684
		$ident_result='';
685
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
686
		{
687
			$ident_result = $row['ident'];
688
		}
689
		return $ident_result;
690
        }
691
692
	/**
693
	* Check recent aircraft
694
	*
695
	* @return String the ident
696
	*
697
	*/
698
	public function checkIdentRecent($ident)
699
	{
700
		global $globalDBdriver, $globalTimezone;
701
		if ($globalDBdriver == 'mysql') {
702
			$query  = 'SELECT spotter_live.ident, spotter_live.flightaware_id FROM spotter_live 
703
				WHERE spotter_live.ident = :ident 
704
				AND spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE)'; 
705
//				AND spotter_live.date < UTC_TIMESTAMP()";
706
			$query_data = array(':ident' => $ident);
707
		} elseif ($globalDBdriver == 'pgsql') {
708
			$query  = "SELECT spotter_live.ident, spotter_live.flightaware_id FROM spotter_live 
709
				WHERE spotter_live.ident = :ident 
710
				AND spotter_live.date >= now() AT TIME ZONE 'UTC' - INTERVAL '30 MINUTES'";
711
//				AND spotter_live.date < now() AT TIME ZONE 'UTC'";
712
			$query_data = array(':ident' => $ident);
713
		}
714
		
715
		$sth = $this->db->prepare($query);
0 ignored issues
show
Bug introduced by
The variable $query 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...
716
		$sth->execute($query_data);
0 ignored issues
show
Bug introduced by
The variable $query_data 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...
717
		$ident_result='';
718
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
719
		{
720
			$ident_result = $row['flightaware_id'];
721
		}
722
		return $ident_result;
723
        }
724
725
	/**
726
	* Check recent aircraft by ModeS
727
	*
728
	* @return String the ModeS
729
	*
730
	*/
731
	public function checkModeSRecent($modes)
732
	{
733
		global $globalDBdriver, $globalTimezone;
734
		if ($globalDBdriver == 'mysql') {
735
			$query  = 'SELECT spotter_live.ModeS, spotter_live.flightaware_id FROM spotter_live 
736
				WHERE spotter_live.ModeS = :modes 
737
				AND spotter_live.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 MINUTE)'; 
738
//				AND spotter_live.date < UTC_TIMESTAMP()";
739
			$query_data = array(':modes' => $modes);
740
		} elseif ($globalDBdriver == 'pgsql') {
741
			$query  = "SELECT spotter_live.ModeS, spotter_live.flightaware_id FROM spotter_live 
742
				WHERE spotter_live.ModeS = :modes 
743
				AND spotter_live.date >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '30 MINUTE'";
744
//			//	AND spotter_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";
745
			$query_data = array(':modes' => $modes);
746
		}
747
		
748
		$sth = $this->db->prepare($query);
0 ignored issues
show
Bug introduced by
The variable $query 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...
749
		$sth->execute($query_data);
0 ignored issues
show
Bug introduced by
The variable $query_data 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...
750
		$ident_result='';
751
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
752
		{
753
			//$ident_result = $row['spotter_live_id'];
754
			$ident_result = $row['flightaware_id'];
755
		}
756
		return $ident_result;
757
        }
758
759
	/**
760
	* Adds a new spotter data
761
	*
762
	* @param String $flightaware_id the ID from flightaware
763
	* @param String $ident the flight ident
764
	* @param String $aircraft_icao the aircraft type
765
	* @param String $departure_airport_icao the departure airport
766
	* @param String $arrival_airport_icao the arrival airport
767
	* @return String success or false
768
	*
769
	*/
770
	public function addLiveSpotterData($flightaware_id = '', $ident = '', $aircraft_icao = '', $departure_airport_icao = '', $arrival_airport_icao = '', $latitude = '', $longitude = '', $waypoints = '', $altitude = '', $heading = '', $groundspeed = '', $date = '',$departure_airport_time = '', $arrival_airport_time = '', $squawk = '', $route_stop = '', $ModeS = '', $putinarchive = false,$registration = '',$pilot_id = '', $pilot_name = '', $verticalrate = '', $noarchive = false, $ground = false,$format_source = '', $source_name = '', $over_country = '')
0 ignored issues
show
Unused Code introduced by
The parameter $registration 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...
771
	{
772
		global $globalURL, $globalArchive, $globalDebug;
773
		$Common = new Common();
774
		$SpotterArchive = new SpotterArchive($this->db);
775
		date_default_timezone_set('UTC');
776
777
		$registration = '';
778
		//getting the registration
779
		
780
781
//		if ($ModeS != '') $registration = Spotter->getAircraftRegistrationBymodeS($ModeS);
782
		
783
784
		//getting the airline information
785
		if ($ident != '')
786
		{
787
			if (!is_string($ident))
788
			{
789
				return false;
790
			} 
791
			/*
792
			else {
793
				//if (!is_numeric(substr($ident, -1, 1)))
794
				if (!is_numeric(substr($ident, 0, 3)))
795
				{
796
					if (is_numeric(substr(substr($ident, 0, 3), -1, 1))) {
797
						$airline_array = Spotter->getAllAirlineInfo(substr($ident, 0, 2));
798
					} elseif (is_numeric(substr(substr($ident, 0, 4), -1, 1))) {
799
						$airline_array = Spotter->getAllAirlineInfo(substr($ident, 0, 3));
800
					} else {
801
						$airline_array = Spotter->getAllAirlineInfo("NA");
802
					}
803
					//print_r($airline_array);
804
					if (count($airline_array) == 0) {
805
					    $airline_array = Spotter->getAllAirlineInfo("NA");
806
					} elseif ($airline_array[0]['icao'] == ''){
807
					    $airline_array = Spotter->getAllAirlineInfo("NA");
808
					}
809
810
				} else {
811
					//echo "\n arg numeric : ".substr($ident, -1, 1)." - ".substr($ident, 0, 3)."\n";
812
					$airline_array = Spotter->getAllAirlineInfo("NA");
813
				}
814
			}
815
		*/
816
		}
817
818
		//getting the aircraft information
819
		if ($aircraft_icao != '')
820
		{
821
			if (!is_string($aircraft_icao))
822
			{
823
				return false;
824
			} 
825
			/*
826
			else {
827
				if ($aircraft_icao == '' || $aircraft_icao == "XXXX")
828
				{
829
					$aircraft_array = Spotter->getAllAircraftInfo("NA");
830
				} else {
831
					$aircraft_array = Spotter->getAllAircraftInfo($aircraft_icao);
832
				}
833
			}
834
			*/
835
		} 
836
		//getting the departure airport information
837
		if ($departure_airport_icao != '')
838
		{
839
			if (!is_string($departure_airport_icao))
840
			{
841
				return false;
842
			} 
843
			/*
844
			else {
845
				$departure_airport_array = Spotter->getAllAirportInfo($departure_airport_icao);
846
			}
847
			*/
848
		}
849
850
		//getting the arrival airport information
851
		if ($arrival_airport_icao != '')
852
		{
853
			if (!is_string($arrival_airport_icao))
854
			{
855
				return false;
856
			}
857
			/*
858
			
859
			 else {
860
				$arrival_airport_array = Spotter->getAllAirportInfo($arrival_airport_icao);
861
			}
862
			*/
863
		}
864
865
866
		if ($latitude != '')
867
		{
868
			if (!is_numeric($latitude))
869
			{
870
				return false;
871
			}
872
		}
873
874
		if ($longitude != '')
875
		{
876
			if (!is_numeric($longitude))
877
			{
878
				return false;
879
			}
880
		}
881
882
		if ($waypoints != '')
883
		{
884
			if (!is_string($waypoints))
885
			{
886
				return false;
887
			}
888
		}
889
890
		if ($altitude != '')
891
		{
892
			if (!is_numeric($altitude))
893
			{
894
				return false;
895
			}
896
		} else $altitude = 0;
897
898
		if ($heading != '')
899
		{
900
			if (!is_numeric($heading))
901
			{
902
				return false;
903
			}
904
		}
905
906
		if ($groundspeed != '')
907
		{
908
			if (!is_numeric($groundspeed))
909
			{
910
				return false;
911
			}
912
		}
913
		date_default_timezone_set('UTC');
914
		if ($date == '') $date = date("Y-m-d H:i:s", time());
915
916
/*
917
		//getting the aircraft image
918
		if ($registration != '')
919
		{
920
			$image_array = Image->getSpotterImage($registration);
921
			if (!isset($image_array[0]['registration']))
922
			{
923
				Image->addSpotterImage($registration);
924
			}
925
		}
926
  */
927
        
928
		$flightaware_id = filter_var($flightaware_id,FILTER_SANITIZE_STRING);
929
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
930
		$aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING);
931
		$departure_airport_icao = filter_var($departure_airport_icao,FILTER_SANITIZE_STRING);
932
		$arrival_airport_icao = filter_var($arrival_airport_icao,FILTER_SANITIZE_STRING);
933
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
934
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
935
		$waypoints = filter_var($waypoints,FILTER_SANITIZE_STRING);
936
		$altitude = filter_var($altitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
937
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
938
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
939
		$squawk = filter_var($squawk,FILTER_SANITIZE_NUMBER_INT);
940
		$route_stop = filter_var($route_stop,FILTER_SANITIZE_STRING);
941
		$ModeS = filter_var($ModeS,FILTER_SANITIZE_STRING);
942
		$pilot_id = filter_var($pilot_id,FILTER_SANITIZE_STRING);
943
		$pilot_name = filter_var($pilot_name,FILTER_SANITIZE_STRING);
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
		$verticalrate = filter_var($verticalrate,FILTER_SANITIZE_NUMBER_INT);
948
949
/*
950
		if (!isset($airline_array) || count($airline_array) == 0) {
951
			$airline_array = Spotter->getAllAirlineInfo('NA');
952
		}
953
		if (!isset($aircraft_array) || count($aircraft_array) == 0) {
954
			$aircraft_array = Spotter->getAllAircraftInfo('NA');
955
            	}
956
            	if ($registration == '') $registration = 'NA';
957
		$airline_name = $airline_array[0]['name'];
958
		$airline_icao = $airline_array[0]['icao'];
959
		$airline_country = $airline_array[0]['country'];
960
		$airline_type = $airline_array[0]['type'];
961
		$aircraft_shadow = $aircraft_array[0]['aircraft_shadow'];
962
		$aircraft_type = $aircraft_array[0]['type'];
963
		$aircraft_manufacturer = $aircraft_array[0]['manufacturer'];
964
*/
965
		$airline_name = '';
966
		$airline_icao = '';
967
		$airline_country = '';
968
		$airline_type = '';
969
		$aircraft_shadow = '';
970
		$aircraft_type = '';
971
		$aircraft_manufacturer = '';
972
973
974
975
		$aircraft_name = '';
976
		if (isset($departure_airport_array[0])) {
0 ignored issues
show
Bug introduced by
The variable $departure_airport_array 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...
977
			$departure_airport_name = $departure_airport_array[0]['name'];
978
			$departure_airport_city = $departure_airport_array[0]['city'];
979
			$departure_airport_country = $departure_airport_array[0]['country'];
980
		} else {
981
			$departure_airport_name = '';
982
			$departure_airport_city = '';
983
			$departure_airport_country = '';
984
		}
985
		if (isset($arrival_airport_array[0])) {
0 ignored issues
show
Bug introduced by
The variable $arrival_airport_array 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...
986
			$arrival_airport_name = $arrival_airport_array[0]['name'];
987
			$arrival_airport_city = $arrival_airport_array[0]['city'];
988
			$arrival_airport_country = $arrival_airport_array[0]['country'];
989
		} else {
990
			$arrival_airport_name = '';
991
			$arrival_airport_city = '';
992
			$arrival_airport_country = '';
993
		}
994
            	
995
            	if ($squawk == '' || $Common->isInteger($squawk) == false ) $squawk = NULL;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
996
            	if ($verticalrate == '' || $Common->isInteger($verticalrate) == false ) $verticalrate = NULL;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
997
            	if ($groundspeed == '' || $Common->isInteger($groundspeed) == false ) $groundspeed = 0;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
998
            	if ($heading == '' || $Common->isInteger($heading) == false ) $heading = 0;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
999
            	
1000
		$query  = 'INSERT INTO spotter_live (flightaware_id, ident, registration, airline_name, airline_icao, airline_country, airline_type, aircraft_icao, aircraft_shadow, aircraft_name, aircraft_manufacturer, departure_airport_icao, departure_airport_name, departure_airport_city, departure_airport_country, arrival_airport_icao, arrival_airport_name, arrival_airport_city, arrival_airport_country, latitude, longitude, waypoints, altitude, heading, ground_speed, date, departure_airport_time, arrival_airport_time, squawk, route_stop, ModeS, pilot_id, pilot_name, verticalrate, ground, format_source, source_name, over_country) 
1001
		VALUES (:flightaware_id,:ident,:registration,:airline_name,:airline_icao,:airline_country,:airline_type,:aircraft_icao,:aircraft_shadow,:aircraft_type,:aircraft_manufacturer,:departure_airport_icao,:departure_airport_name, :departure_airport_city, :departure_airport_country, :arrival_airport_icao, :arrival_airport_name, :arrival_airport_city, :arrival_airport_country, :latitude,:longitude,:waypoints,:altitude,:heading,:groundspeed,:date,:departure_airport_time,:arrival_airport_time,:squawk,:route_stop,:ModeS, :pilot_id, :pilot_name, :verticalrate, :ground, :format_source, :source_name, :over_country)';
1002
1003
		$query_values = array(':flightaware_id' => $flightaware_id,':ident' => $ident, ':registration' => $registration,':airline_name' => $airline_name,':airline_icao' => $airline_icao,':airline_country' => $airline_country,':airline_type' => $airline_type,':aircraft_icao' => $aircraft_icao,':aircraft_shadow' => $aircraft_shadow,':aircraft_type' => $aircraft_type,':aircraft_manufacturer' => $aircraft_manufacturer,':departure_airport_icao' => $departure_airport_icao,':departure_airport_name' => $departure_airport_name,':departure_airport_city' => $departure_airport_city,':departure_airport_country' => $departure_airport_country,':arrival_airport_icao' => $arrival_airport_icao,':arrival_airport_name' => $arrival_airport_name,':arrival_airport_city' => $arrival_airport_city,':arrival_airport_country' => $arrival_airport_country,':latitude' => $latitude,':longitude' => $longitude, ':waypoints' => $waypoints,':altitude' => $altitude,':heading' => $heading,':groundspeed' => $groundspeed,':date' => $date, ':departure_airport_time' => $departure_airport_time,':arrival_airport_time' => $arrival_airport_time, ':squawk' => $squawk,':route_stop' => $route_stop,':ModeS' => $ModeS, ':pilot_id' => $pilot_id, ':pilot_name' => $pilot_name, ':verticalrate' => $verticalrate, ':format_source' => $format_source,':ground' => $ground, ':source_name' => $source_name, ':over_country' => $over_country);
1004
		//$query_values = array(':flightaware_id' => $flightaware_id,':ident' => $ident, ':registration' => $registration,':airline_name' => $airline_array[0]['name'],':airline_icao' => $airline_array[0]['icao'],':airline_country' => $airline_array[0]['country'],':airline_type' => $airline_array[0]['type'],':aircraft_icao' => $aircraft_icao,':aircraft_type' => $aircraft_array[0]['type'],':aircraft_manufacturer' => $aircraft_array[0]['manufacturer'],':departure_airport_icao' => $departure_airport_icao,':arrival_airport_icao' => $arrival_airport_icao,':latitude' => $latitude,':longitude' => $longitude, ':waypoints' => $waypoints,':altitude' => $altitude,':heading' => $heading,':groundspeed' => $groundspeed,':date' => $date);
1005
		try {
1006
			
1007
			$sth = $this->db->prepare($query);
1008
			$sth->execute($query_values);
1009
                } catch(PDOException $e) {
1010
                	return "error : ".$e->getMessage();
1011
                }
1012
		if (isset($globalArchive) && $globalArchive && $putinarchive && !$noarchive) {
1013
		    if ($globalDebug) echo '(Add to SBS archive : ';
1014
		    $result =  $SpotterArchive->addSpotterArchiveData($flightaware_id, $ident, $registration, $airline_name, $airline_icao, $airline_country, $airline_type, $aircraft_icao, $aircraft_shadow, $aircraft_name, $aircraft_manufacturer, $departure_airport_icao, $departure_airport_name, $departure_airport_city, $departure_airport_country, $departure_airport_time,$arrival_airport_icao, $arrival_airport_name, $arrival_airport_city, $arrival_airport_country, $arrival_airport_time, $route_stop, $date,$latitude, $longitude, $waypoints, $altitude, $heading, $groundspeed, $squawk, $ModeS, $pilot_id, $pilot_name,$verticalrate,$format_source,$source_name, $over_country);
1015
		    if ($globalDebug) echo $result.')';
1016
		}
1017
		return "success";
1018
1019
	}
1020
1021
	public function getOrderBy()
1022
	{
1023
		$orderby = array("aircraft_asc" => array("key" => "aircraft_asc", "value" => "Aircraft Type - ASC", "sql" => "ORDER BY spotter_live.aircraft_icao ASC"), "aircraft_desc" => array("key" => "aircraft_desc", "value" => "Aircraft Type - DESC", "sql" => "ORDER BY spotter_live.aircraft_icao DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY spotter_live.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY spotter_live.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY spotter_live.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY spotter_live.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY spotter_live.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY spotter_live.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure Airport - ASC", "sql" => "ORDER BY spotter_live.departure_airport_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY spotter_live.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY spotter_live.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY spotter_live.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY spotter_live.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY spotter_live.date DESC"));
1024
		return $orderby;
1025
	}
1026
1027
}
1028
1029
1030
?>
1 ignored issue
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
1031