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

SpotterArchive::searchSpotterData()   F

Complexity

Conditions 54
Paths > 20000

Size

Total Lines 326
Code Lines 195

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 54
eloc 195
nc 429496.7295
nop 24
dl 0
loc 326
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

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

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

Commonly applied refactorings include:

Many Parameters

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

There are several approaches to avoid long parameter lists:

1
<?php
2
class SpotterArchive {
3
	public $global_query = "SELECT spotter_archive.* FROM spotter_archive";
4
	public $db;
5
6
	public function __construct($dbc = null) {
7
		$Connection = new Connection($dbc);
8
		$this->db = $Connection->db;
9
	}
10
11
	// Spotter_archive
12
	public function 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 = '', $ground_speed = '', $squawk = '', $ModeS = '', $pilot_id = '', $pilot_name = '',$verticalrate = '',$format_source = '', $source_name = '', $over_country = '') {
13
		require_once(dirname(__FILE__).'/class.Spotter.php');
14
		if ($over_country == '') {
15
			$Spotter = new Spotter($this->db);
16
			$data_country = $Spotter->getCountryFromLatitudeLongitude($latitude,$longitude);
17
			if (!empty($data_country)) $country = $data_country['iso2'];
18
			else $country = '';
19
		} else $country = $over_country;
20
		if ($airline_type === NULL) $airline_type ='';
21
	
22
		//if ($country == '') echo "\n".'************ UNKNOW COUNTRY ****************'."\n";
23
		//else echo "\n".'*/*/*/*/*/*/*/ Country : '.$country.' */*/*/*/*/*/*/*/*/'."\n";
24
25
		// Route is not added in spotter_archive
26
		$query  = "INSERT INTO spotter_archive (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, ground_speed, squawk, ModeS, pilot_id, pilot_name, verticalrate,format_source,over_country,source_name)
27
		        VALUES (: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, :ground_speed, :squawk, :ModeS, :pilot_id, :pilot_name, :verticalrate, :format_source, :over_country, :source_name)";
28
29
		$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_name' => $aircraft_name, ':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, ':departure_airport_time' => $departure_airport_time,':arrival_airport_icao' => $arrival_airport_icao, ':arrival_airport_name' => $arrival_airport_name, ':arrival_airport_city' => $arrival_airport_city, ':arrival_airport_country' => $arrival_airport_country, ':arrival_airport_time' => $arrival_airport_time, ':route_stop' => $route_stop, ':date' => $date,':latitude' => $latitude, ':longitude' => $longitude, ':waypoints' => $waypoints, ':altitude' => $altitude, ':heading' => $heading, ':ground_speed' => $ground_speed, ':squawk' => $squawk, ':ModeS' => $ModeS, ':pilot_id' => $pilot_id, ':pilot_name' => $pilot_name, ':verticalrate' => $verticalrate, ':format_source' => $format_source, ':over_country' => $country, ':source_name' => $source_name);
30
		try {
31
			$sth = $this->db->prepare($query);
32
			$sth->execute($query_values);
33
		} catch(PDOException $e) {
34
			return "error : ".$e->getMessage();
35
		}
36
		return "success";
37
	}
38
39
40
        /**
41
        * Gets all the spotter information based on a particular callsign
42
        *
43
        * @return Array the spotter information
44
        *
45
        */
46
        public function getLastArchiveSpotterDataByIdent($ident)
47
        {
48
		$Spotter = new Spotter($this->db);
49
                date_default_timezone_set('UTC');
50
51
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
52
                //$query  = "SELECT spotter_archive.* FROM spotter_archive INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_archive l WHERE l.ident = :ident GROUP BY l.flightaware_id) s on spotter_archive.flightaware_id = s.flightaware_id AND spotter_archive.date = s.maxdate LIMIT 1";
53
                $query  = "SELECT spotter_archive.* FROM spotter_archive WHERE ident = :ident ORDER BY date DESC LIMIT 1";
54
55
                $spotter_array = $Spotter->getDataFromDB($query,array(':ident' => $ident));
56
57
                return $spotter_array;
58
        }
59
60
61
        /**
62
        * Gets last the spotter information based on a particular id
63
        *
64
        * @return Array the spotter information
65
        *
66
        */
67
        public function getLastArchiveSpotterDataById($id)
68
        {
69
    		$Spotter = new Spotter($this->db);
70
                date_default_timezone_set('UTC');
71
                $id = filter_var($id, FILTER_SANITIZE_STRING);
72
                //$query  = SpotterArchive->$global_query." WHERE spotter_archive.flightaware_id = :id";
73
                //$query  = "SELECT spotter_archive.* FROM spotter_archive INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_archive l WHERE l.flightaware_id = :id GROUP BY l.flightaware_id) s on spotter_archive.flightaware_id = s.flightaware_id AND spotter_archive.date = s.maxdate LIMIT 1";
74
                $query  = "SELECT * FROM spotter_archive WHERE flightaware_id = :id ORDER BY date DESC LIMIT 1";
75
76
//              $spotter_array = Spotter->getDataFromDB($query,array(':id' => $id));
77
                  /*
78
                try {
79
                        $Connection = new Connection();
80
                        $sth = Connection->$db->prepare($query);
81
                        $sth->execute(array(':id' => $id));
82
                } catch(PDOException $e) {
83
                        return "error";
84
                }
85
                $spotter_array = $sth->fetchAll(PDO->FETCH_ASSOC);
86
                */
87
                $spotter_array = $Spotter->getDataFromDB($query,array(':id' => $id));
88
89
                return $spotter_array;
90
        }
91
92
        /**
93
        * Gets all the spotter information based on a particular id
94
        *
95
        * @return Array the spotter information
96
        *
97
        */
98
        public function getAllArchiveSpotterDataById($id)
99
        {
100
                date_default_timezone_set('UTC');
101
                $id = filter_var($id, FILTER_SANITIZE_STRING);
102
                $query  = $this->global_query." WHERE spotter_archive.flightaware_id = :id";
103
104
//              $spotter_array = Spotter->getDataFromDB($query,array(':id' => $id));
105
106
                try {
107
                        $sth = $this->db->prepare($query);
108
                        $sth->execute(array(':id' => $id));
109
                } catch(PDOException $e) {
110
                        echo $e->getMessage();
111
                        die;
112
                }
113
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
114
115
                return $spotter_array;
116
        }
117
118
        /**
119
        * Gets coordinate & time spotter information based on a particular id
120
        *
121
        * @return Array the spotter information
122
        *
123
        */
124
        public function getCoordArchiveSpotterDataById($id)
125
        {
126
                date_default_timezone_set('UTC');
127
                $id = filter_var($id, FILTER_SANITIZE_STRING);
128
                $query  = "SELECT spotter_archive.latitude, spotter_archive.longitude, spotter_archive.date FROM spotter_archive WHERE spotter_archive.flightaware_id = :id";
129
130
//              $spotter_array = Spotter->getDataFromDB($query,array(':id' => $id));
131
132
                try {
133
                        $sth = $this->db->prepare($query);
134
                        $sth->execute(array(':id' => $id));
135
                } catch(PDOException $e) {
136
                        echo $e->getMessage();
137
                        die;
138
                }
139
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
140
141
                return $spotter_array;
142
        }
143
144
145
        /**
146
        * Gets altitude information based on a particular callsign
147
        *
148
        * @return Array the spotter information
149
        *
150
        */
151
        public function getAltitudeArchiveSpotterDataByIdent($ident)
152
        {
153
154
                date_default_timezone_set('UTC');
155
156
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
157
                $query  = "SELECT spotter_archive.altitude, spotter_archive.date FROM spotter_archive WHERE spotter_archive.ident = :ident";
158
159
                try {
160
                        $sth = $this->db->prepare($query);
161
                        $sth->execute(array(':ident' => $ident));
162
                } catch(PDOException $e) {
163
                        echo $e->getMessage();
164
                        die;
165
                }
166
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
167
168
                return $spotter_array;
169
        }
170
171
        /**
172
        * Gets altitude information based on a particular id
173
        *
174
        * @return Array the spotter information
175
        *
176
        */
177
        public function getAltitudeArchiveSpotterDataById($id)
178
        {
179
180
                date_default_timezone_set('UTC');
181
182
                $id = filter_var($id, FILTER_SANITIZE_STRING);
183
                $query  = "SELECT spotter_archive.altitude, spotter_archive.date FROM spotter_archive WHERE spotter_archive.flightaware_id = :id";
184
185
                try {
186
                        $sth = $this->db->prepare($query);
187
                        $sth->execute(array(':id' => $id));
188
                } catch(PDOException $e) {
189
                        echo $e->getMessage();
190
                        die;
191
                }
192
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
193
194
                return $spotter_array;
195
        }
196
197
        /**
198
        * Gets altitude & speed information based on a particular id
199
        *
200
        * @return Array the spotter information
201
        *
202
        */
203
        public function getAltitudeSpeedArchiveSpotterDataById($id)
204
        {
205
206
                date_default_timezone_set('UTC');
207
208
                $id = filter_var($id, FILTER_SANITIZE_STRING);
209
                $query  = "SELECT spotter_archive.altitude, spotter_archive.ground_speed, spotter_archive.date FROM spotter_archive WHERE spotter_archive.flightaware_id = :id";
210
211
                try {
212
                        $sth = $this->db->prepare($query);
213
                        $sth->execute(array(':id' => $id));
214
                } catch(PDOException $e) {
215
                        echo $e->getMessage();
216
                        die;
217
                }
218
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
219
220
                return $spotter_array;
221
        }
222
223
224
        /**
225
        * Gets altitude information based on a particular callsign
226
        *
227
        * @return Array the spotter information
228
        *
229
        */
230
        public function getLastAltitudeArchiveSpotterDataByIdent($ident)
231
        {
232
233
                date_default_timezone_set('UTC');
234
235
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
236
                $query  = "SELECT spotter_archive.altitude, spotter_archive.date FROM spotter_archive INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_archive l WHERE l.ident = :ident GROUP BY l.flightaware_id) s on spotter_archive.flightaware_id = s.flightaware_id AND spotter_archive.date = s.maxdate LIMIT 1";
237
//                $query  = "SELECT spotter_archive.altitude, spotter_archive.date FROM spotter_archive WHERE spotter_archive.ident = :ident";
238
239
                try {
240
                        $sth = $this->db->prepare($query);
241
                        $sth->execute(array(':ident' => $ident));
242
                } catch(PDOException $e) {
243
                        echo $e->getMessage();
244
                        die;
245
                }
246
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
247
248
                return $spotter_array;
249
        }
250
251
252
253
       /**
254
        * Gets all the archive spotter information
255
        *
256
        * @return Array the spotter information
257
        *
258
        */
259
        public function getSpotterArchiveData($ident,$flightaware_id,$date)
260
        {
261
    		$Spotter = new Spotter($this->db);
262
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
263
                $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.flightaware_id = :flightaware_id AND l.date LIKE :date GROUP BY l.flightaware_id) s on spotter_live.flightaware_id = s.flightaware_id AND spotter_live.date = s.maxdate";
264
265
                $spotter_array = $Spotter->getDataFromDB($query,array(':ident' => $ident,':flightaware_id' => $flightaware_id,':date' => $date.'%'));
266
267
                return $spotter_array;
268
        }
269
        
270
        public function deleteSpotterArchiveTrackData()
271
        {
272
		global $globalArchiveKeepTrackMonths;
273
                date_default_timezone_set('UTC');
274
		$query = 'DELETE FROM spotter_archive WHERE spotter_archive.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveKeepTrackMonths.' MONTH)';
275
                try {
276
                        $sth = $this->db->prepare($query);
277
                        $sth->execute();
278
                } catch(PDOException $e) {
279
                        echo $e->getMessage();
280
                        die;
281
                }
282
	}
283
284
	/**
285
        * Gets Minimal Live Spotter data
286
        *
287
        * @return Array the spotter information
288
        *
289
        */
290
        public function getMinLiveSpotterData($begindate,$enddate,$filter = array())
0 ignored issues
show
Unused Code introduced by
The parameter $enddate 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...
291
        {
292
                global $globalDBdriver, $globalLiveInterval;
293
                date_default_timezone_set('UTC');
294
295
                $filter_query = '';
296
                if (isset($filter['source']) && !empty($filter['source'])) {
297
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
298
                }
299
                // Use spotter_output also ?
300
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
301
                        $filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_archive_output WHERE spotter_archive_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.flightaware_id = spotter_archive.flightaware_id ";
302
                }
303
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
304
                        $filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_archive_output WHERE spotter_archive_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_archive.flightaware_id ";
305
                }
306
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
307
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
308
                }
309
310
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
311
                if ($globalDBdriver == 'mysql') {
312
                        /*
313
                        $query  = 'SELECT a.aircraft_shadow, spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk 
314
                    		    FROM spotter_archive 
315
                    		    INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_archive l WHERE (l.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') GROUP BY l.flightaware_id) s on spotter_archive.flightaware_id = s.flightaware_id AND spotter_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_archive.aircraft_icao = a.icao';
316
			*/
317
			$query  = 'SELECT a.aircraft_shadow, spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk 
318
				    FROM spotter_archive 
319
				    INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate 
320
						FROM spotter_archive l 
321
						WHERE (l.date BETWEEN DATE_SUB('."'".$begindate."'".',INTERVAL '.$globalLiveInterval.' SECOND) AND '."'".$begindate."'".') 
322
						GROUP BY l.flightaware_id) s on spotter_archive.flightaware_id = s.flightaware_id 
323
				    AND spotter_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_archive.aircraft_icao = a.icao';
324
                } else if ($globalDBdriver == 'pgsql') {
325
                        $query  = 'SELECT spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk, a.aircraft_shadow FROM spotter_archive INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_archive l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_archive.flightaware_id = s.flightaware_id AND spotter_archive.date = s.maxdate '.$filter_query.'INNER JOIN (SELECT * FROM aircraft) a on spotter_archive.aircraft_icao = a.icao';
326
                }
327
                //echo $query;
328
                try {
329
                        $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...
330
                        $sth->execute();
331
                } catch(PDOException $e) {
332
                        echo $e->getMessage();
333
                        die;
334
                }
335
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
336
337
                return $spotter_array;
338
        }
339
340
	/**
341
        * Gets Minimal Live Spotter data
342
        *
343
        * @return Array the spotter information
344
        *
345
        */
346
        public function getMinLiveSpotterDataPlayback($begindate,$enddate,$filter = array())
347
        {
348
                global $globalDBdriver, $globalLiveInterval;
349
                date_default_timezone_set('UTC');
350
351
                $filter_query = '';
352
                if (isset($filter['source']) && !empty($filter['source'])) {
353
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
354
                }
355
                // Should use spotter_output also ?
356
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
357
                        $filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_archive_output WHERE spotter_archive_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.flightaware_id = spotter_archive.flightaware_id ";
358
                }
359
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
360
                        $filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_archive_output WHERE spotter_archive_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_archive.flightaware_id ";
361
                }
362
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
363
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
364
                }
365
366
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
367
                if ($globalDBdriver == 'mysql') {
368
                        /*
369
                        $query  = 'SELECT a.aircraft_shadow, spotter_archive.ident, spotter_archive.flightaware_id, spotter_archive.aircraft_icao, spotter_archive.departure_airport_icao as departure_airport, spotter_archive.arrival_airport_icao as arrival_airport, spotter_archive.latitude, spotter_archive.longitude, spotter_archive.altitude, spotter_archive.heading, spotter_archive.ground_speed, spotter_archive.squawk 
370
                    		    FROM spotter_archive 
371
                    		    INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_archive l WHERE (l.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') GROUP BY l.flightaware_id) s on spotter_archive.flightaware_id = s.flightaware_id AND spotter_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_archive.aircraft_icao = a.icao';
372
			*/
373
			$query  = 'SELECT a.aircraft_shadow, spotter_archive_output.ident, spotter_archive_output.flightaware_id, spotter_archive_output.aircraft_icao, spotter_archive_output.departure_airport_icao as departure_airport, spotter_archive_output.arrival_airport_icao as arrival_airport, spotter_archive_output.latitude, spotter_archive_output.longitude, spotter_archive_output.altitude, spotter_archive_output.heading, spotter_archive_output.ground_speed, spotter_archive_output.squawk 
374
				    FROM spotter_archive_output 
375
				    LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON spotter_archive_output.aircraft_icao = a.icao 
376
				    WHERE (spotter_archive_output.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') 
377
                        	    '.$filter_query.' GROUP BY spotter_archive_output.flightaware_id, spotter_archive_output.ident, spotter_archive_output.aircraft_icao, spotter_archive_output.departure_airport_icao, spotter_archive_output.arrival_airport_icao, spotter_archive_output.latitude, spotter_archive_output.longitude, spotter_archive_output.altitude, spotter_archive_output.heading, spotter_archive_output.ground_speed, spotter_archive_output.squawk, a.aircraft_shadow';
378
379
                } else {
380
                        //$query  = 'SELECT spotter_archive_output.ident, spotter_archive_output.flightaware_id, spotter_archive_output.aircraft_icao, spotter_archive_output.departure_airport_icao as departure_airport, spotter_archive_output.arrival_airport_icao as arrival_airport, spotter_archive_output.latitude, spotter_archive_output.longitude, spotter_archive_output.altitude, spotter_archive_output.heading, spotter_archive_output.ground_speed, spotter_archive_output.squawk, a.aircraft_shadow FROM spotter_archive_output INNER JOIN (SELECT l.flightaware_id, max(l.date) as maxdate FROM spotter_archive_output l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.flightaware_id) s on spotter_archive_output.flightaware_id = s.flightaware_id AND spotter_archive_output.date = s.maxdate '.$filter_query.'INNER JOIN (SELECT * FROM aircraft) a on spotter_archive_output.aircraft_icao = a.icao';
381
                        $query  = 'SELECT spotter_archive_output.ident, spotter_archive_output.flightaware_id, spotter_archive_output.aircraft_icao, spotter_archive_output.departure_airport_icao as departure_airport, spotter_archive_output.arrival_airport_icao as arrival_airport, spotter_archive_output.latitude, spotter_archive_output.longitude, spotter_archive_output.altitude, spotter_archive_output.heading, spotter_archive_output.ground_speed, spotter_archive_output.squawk, a.aircraft_shadow
0 ignored issues
show
Unused Code introduced by
$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...
382
                        	    FROM spotter_archive_output 
383
                        	    INNER JOIN (SELECT * FROM aircraft) a on spotter_archive_output.aircraft_icao = a.icao
384
                        	    WHERE spotter_archive_output.date >= '."'".$begindate."'".' AND spotter_archive_output.date <= '."'".$enddate."'".'
385
                        	    '.$filter_query.' GROUP BY spotter_archive_output.flightaware_id, spotter_archive_output.ident, spotter_archive_output.aircraft_icao, spotter_archive_output.departure_airport_icao, spotter_archive_output.arrival_airport_icao, spotter_archive_output.latitude, spotter_archive_output.longitude, spotter_archive_output.altitude, spotter_archive_output.heading, spotter_archive_output.ground_speed, spotter_archive_output.squawk, a.aircraft_shadow';
386
                        $query  = 'SELECT DISTINCT spotter_output.flightaware_id, spotter_output.ident, spotter_output.aircraft_icao, spotter_output.departure_airport_icao as departure_airport, spotter_output.arrival_airport_icao as arrival_airport, spotter_output.latitude, spotter_output.longitude, spotter_output.altitude, spotter_output.heading, spotter_output.ground_speed, spotter_output.squawk, a.aircraft_shadow
387
                        	    FROM spotter_output 
388
                        	    INNER JOIN (SELECT * FROM aircraft) a on spotter_output.aircraft_icao = a.icao
389
                        	    WHERE spotter_output.date >= '."'".$begindate."'".' AND spotter_output.date <= '."'".$enddate."'".'
390
                        	    '.$filter_query.' LIMIT 200 OFFSET 0';
391
//                        	    .' GROUP BY spotter_output.flightaware_id, spotter_output.ident, spotter_output.aircraft_icao, spotter_output.departure_airport_icao, spotter_output.arrival_airport_icao, spotter_output.latitude, spotter_output.longitude, spotter_output.altitude, spotter_output.heading, spotter_output.ground_speed, spotter_output.squawk, a.aircraft_shadow';
392
                        	    
393
                }
394
                //echo $query;
395
                try {
396
                        $sth = $this->db->prepare($query);
397
                        $sth->execute();
398
                } catch(PDOException $e) {
399
                        echo $e->getMessage();
400
                        die;
401
                }
402
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
403
404
                return $spotter_array;
405
        }
406
407
	 /**
408
        * Gets count Live Spotter data
409
        *
410
        * @return Array the spotter information
411
        *
412
        */
413
        public function getLiveSpotterCount($begindate,$enddate,$filter = array())
414
        {
415
                global $globalDBdriver, $globalLiveInterval;
416
                date_default_timezone_set('UTC');
417
418
                $filter_query = '';
419
                if (isset($filter['source']) && !empty($filter['source'])) {
420
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
421
                }
422
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
423
                        $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_archive.flightaware_id ";
424
                }
425
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
426
                        $filter_query .= " INNER JOIN (SELECT flightaware_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.flightaware_id = spotter_archive.flightaware_id ";
427
                }
428
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
429
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
430
                }
431
432
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
433
                if ($globalDBdriver == 'mysql') {
434
			$query = 'SELECT COUNT(DISTINCT flightaware_id) as nb 
435
			FROM spotter_archive l 
436
			WHERE (l.date BETWEEN DATE_SUB('."'".$begindate."'".',INTERVAL '.$globalLiveInterval.' SECOND) AND '."'".$begindate."'".')'.$filter_query;
437
                } else {
438
			$query = 'SELECT COUNT(DISTINCT flightaware_id) as nb FROM spotter_archive l WHERE (l.date BETWEEN '."'".$begindate."' - INTERVAL '".$globalLiveInterval." SECONDS' AND "."'".$enddate."'".')'.$filter_query;
439
                }
440
                //echo $query;
441
                try {
442
                        $sth = $this->db->prepare($query);
443
                        $sth->execute();
444
                } catch(PDOException $e) {
445
                        echo $e->getMessage();
446
                        die;
447
                }
448
		$result = $sth->fetch(PDO::FETCH_ASSOC);
449
                return $result['nb'];
450
451
        }
452
453
454
455
	// Spotter_Archive_output
456
	
457
    /**
458
    * Gets all the spotter information
459
    *
460
    * @return Array the spotter information
461
    *
462
    */
463
    public function searchSpotterData($q = '', $registration = '', $aircraft_icao = '', $aircraft_manufacturer = '', $highlights = '', $airline_icao = '', $airline_country = '', $airline_type = '', $airport = '', $airport_country = '', $callsign = '', $departure_airport_route = '', $arrival_airport_route = '', $owner = '',$pilot_id = '',$pilot_name = '',$altitude = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '')
464
    {
465
	global $globalTimezone, $globalDbdriver;
466
	require_once(dirname(__FILE__).'/class.Translation.php');
467
	$Translation = new Translation();
468
469
	date_default_timezone_set('UTC');
470
	
471
	$query_values = array();
472
	$additional_query = '';
473
	if ($q != "")
474
	{
475
	    if (!is_string($q))
476
	    {
477
		return false;
478
	    } else {
479
	        
480
		$q_array = explode(" ", $q);
481
		
482
		foreach ($q_array as $q_item){
483
		    $additional_query .= " AND (";
484
		    $additional_query .= "(spotter_archive_output.spotter_id like '%".$q_item."%') OR ";
485
		    $additional_query .= "(spotter_archive_output.aircraft_icao like '%".$q_item."%') OR ";
486
		    $additional_query .= "(spotter_archive_output.aircraft_name like '%".$q_item."%') OR ";
487
		    $additional_query .= "(spotter_archive_output.aircraft_manufacturer like '%".$q_item."%') OR ";
488
		    $additional_query .= "(spotter_archive_output.airline_icao like '%".$q_item."%') OR ";
489
		    $additional_query .= "(spotter_archive_output.airline_name like '%".$q_item."%') OR ";
490
		    $additional_query .= "(spotter_archive_output.airline_country like '%".$q_item."%') OR ";
491
		    $additional_query .= "(spotter_archive_output.departure_airport_icao like '%".$q_item."%') OR ";
492
		    $additional_query .= "(spotter_archive_output.departure_airport_name like '%".$q_item."%') OR ";
493
		    $additional_query .= "(spotter_archive_output.departure_airport_city like '%".$q_item."%') OR ";
494
		    $additional_query .= "(spotter_archive_output.departure_airport_country like '%".$q_item."%') OR ";
495
		    $additional_query .= "(spotter_archive_output.arrival_airport_icao like '%".$q_item."%') OR ";
496
		    $additional_query .= "(spotter_archive_output.arrival_airport_name like '%".$q_item."%') OR ";
497
		    $additional_query .= "(spotter_archive_output.arrival_airport_city like '%".$q_item."%') OR ";
498
		    $additional_query .= "(spotter_archive_output.arrival_airport_country like '%".$q_item."%') OR ";
499
		    $additional_query .= "(spotter_archive_output.registration like '%".$q_item."%') OR ";
500
		    $additional_query .= "(spotter_archive_output.owner_name like '%".$q_item."%') OR ";
501
		    $additional_query .= "(spotter_archive_output.pilot_id like '%".$q_item."%') OR ";
502
		    $additional_query .= "(spotter_archive_output.pilot_name like '%".$q_item."%') OR ";
503
		    $additional_query .= "(spotter_archive_output.ident like '%".$q_item."%') OR ";
504
		    $translate = $Translation->ident2icao($q_item);
505
		    if ($translate != $q_item) $additional_query .= "(spotter_archive_output.ident like '%".$translate."%') OR ";
506
		    $additional_query .= "(spotter_archive_output.highlight like '%".$q_item."%')";
507
		    $additional_query .= ")";
508
		}
509
	    }
510
	}
511
	
512
	if ($registration != "")
513
	{
514
	    $registration = filter_var($registration,FILTER_SANITIZE_STRING);
515
	    if (!is_string($registration))
516
	    {
517
		return false;
518
	    } else {
519
		$additional_query .= " AND (spotter_archive_output.registration = '".$registration."')";
520
	    }
521
	}
522
	
523
	if ($aircraft_icao != "")
524
	{
525
	    $aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING);
526
	    if (!is_string($aircraft_icao))
527
	    {
528
		return false;
529
	    } else {
530
		$additional_query .= " AND (spotter_archive_output.aircraft_icao = '".$aircraft_icao."')";
531
	    }
532
	}
533
	
534
	if ($aircraft_manufacturer != "")
535
	{
536
	    $aircraft_manufacturer = filter_var($aircraft_manufacturer,FILTER_SANITIZE_STRING);
537
	    if (!is_string($aircraft_manufacturer))
538
	    {
539
		return false;
540
	    } else {
541
		$additional_query .= " AND (spotter_archive_output.aircraft_manufacturer = '".$aircraft_manufacturer."')";
542
	    }
543
	}
544
	
545
	if ($highlights == "true")
546
	{
547
	    if (!is_string($highlights))
548
	    {
549
		return false;
550
	    } else {
551
		$additional_query .= " AND (spotter_archive_output.highlight <> '')";
552
	    }
553
	}
554
	
555
	if ($airline_icao != "")
556
	{
557
	    $registration = filter_var($airline_icao,FILTER_SANITIZE_STRING);
0 ignored issues
show
Unused Code introduced by
$registration 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...
558
	    if (!is_string($airline_icao))
559
	    {
560
		return false;
561
	    } else {
562
		$additional_query .= " AND (spotter_archive_output.airline_icao = '".$airline_icao."')";
563
	    }
564
	}
565
	
566
	if ($airline_country != "")
567
	{
568
	    $airline_country = filter_var($airline_country,FILTER_SANITIZE_STRING);
569
	    if (!is_string($airline_country))
570
	    {
571
		return false;
572
	    } else {
573
		$additional_query .= " AND (spotter_archive_output.airline_country = '".$airline_country."')";
574
	    }
575
	}
576
	
577
	if ($airline_type != "")
578
	{
579
	    $airline_type = filter_var($airline_type,FILTER_SANITIZE_STRING);
580
	    if (!is_string($airline_type))
581
	    {
582
		return false;
583
	    } else {
584
		if ($airline_type == "passenger")
585
		{
586
		    $additional_query .= " AND (spotter_archive_output.airline_type = 'passenger')";
587
		}
588
		if ($airline_type == "cargo")
589
		{
590
		    $additional_query .= " AND (spotter_archive_output.airline_type = 'cargo')";
591
		}
592
		if ($airline_type == "military")
593
		{
594
		    $additional_query .= " AND (spotter_archive_output.airline_type = 'military')";
595
		}
596
	    }
597
	}
598
	
599
	if ($airport != "")
600
	{
601
	    $airport = filter_var($airport,FILTER_SANITIZE_STRING);
602
	    if (!is_string($airport))
603
	    {
604
		return false;
605
	    } else {
606
		$additional_query .= " AND ((spotter_archive_output.departure_airport_icao = '".$airport."') OR (spotter_archive_output.arrival_airport_icao = '".$airport."'))";
607
	    }
608
	}
609
	
610
	if ($airport_country != "")
611
	{
612
	    $airport_country = filter_var($airport_country,FILTER_SANITIZE_STRING);
613
	    if (!is_string($airport_country))
614
	    {
615
		return false;
616
	    } else {
617
		$additional_query .= " AND ((spotter_archive_output.departure_airport_country = '".$airport_country."') OR (spotter_archive_output.arrival_airport_country = '".$airport_country."'))";
618
	    }
619
	}
620
    
621
	if ($callsign != "")
622
	{
623
	    $callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
624
	    if (!is_string($callsign))
625
	    {
626
		return false;
627
	    } else {
628
		$translate = $Translation->ident2icao($callsign);
629
		if ($translate != $callsign) {
630
			$additional_query .= " AND (spotter_archive_output.ident = :callsign OR spotter_archive_output.ident = :translate)";
631
			$query_values = array_merge($query_values,array(':callsign' => $callsign,':translate' => $translate));
0 ignored issues
show
Unused Code introduced by
$query_values is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
632
		} else {
633
			$additional_query .= " AND (spotter_archive_output.ident = '".$callsign."')";
634
		}
635
	    }
636
	}
637
638
	if ($owner != "")
639
	{
640
	    $owner = filter_var($owner,FILTER_SANITIZE_STRING);
641
	    if (!is_string($owner))
642
	    {
643
		return false;
644
	    } else {
645
		$additional_query .= " AND (spotter_archive_output.owner_name = '".$owner."')";
646
	    }
647
	}
648
649
	if ($pilot_name != "")
650
	{
651
	    $pilot_name = filter_var($pilot_name,FILTER_SANITIZE_STRING);
652
	    if (!is_string($pilot_name))
653
	    {
654
		return false;
655
	    } else {
656
		$additional_query .= " AND (spotter_archive_output.pilot_name = '".$pilot_name."')";
657
	    }
658
	}
659
	
660
	if ($pilot_id != "")
661
	{
662
	    $pilot_id = filter_var($pilot_id,FILTER_SANITIZE_NUMBER_INT);
663
	    if (!is_string($pilot_id))
664
	    {
665
		return false;
666
	    } else {
667
		$additional_query .= " AND (spotter_archive_output.pilot_id = '".$pilot_id."')";
668
	    }
669
	}
670
	
671
	if ($departure_airport_route != "")
672
	{
673
	    $departure_airport_route = filter_var($departure_airport_route,FILTER_SANITIZE_STRING);
674
	    if (!is_string($departure_airport_route))
675
	    {
676
		return false;
677
	    } else {
678
		$additional_query .= " AND (spotter_archive_output.departure_airport_icao = '".$departure_airport_route."')";
679
	    }
680
	}
681
	
682
	if ($arrival_airport_route != "")
683
	{
684
	    $arrival_airport_route = filter_var($arrival_airport_route,FILTER_SANITIZE_STRING);
685
	    if (!is_string($arrival_airport_route))
686
	    {
687
		return false;
688
	    } else {
689
		$additional_query .= " AND (spotter_archive_output.arrival_airport_icao = '".$arrival_airport_route."')";
690
	    }
691
	}
692
	
693
	if ($altitude != "")
694
	{
695
	    $altitude_array = explode(",", $altitude);
696
	    
697
	    $altitude_array[0] = filter_var($altitude_array[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
698
	    $altitude_array[1] = filter_var($altitude_array[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
699
	    
700
701
	    if ($altitude_array[1] != "")
702
	    {                
703
		$altitude_array[0] = substr($altitude_array[0], 0, -2);
704
		$altitude_array[1] = substr($altitude_array[1], 0, -2);
705
		$additional_query .= " AND altitude BETWEEN '".$altitude_array[0]."' AND '".$altitude_array[1]."' ";
706
	    } else {
707
		$altitude_array[0] = substr($altitude_array[0], 0, -2);
708
		$additional_query .= " AND altitude <= '".$altitude_array[0]."' ";
709
	    }
710
	}
711
	
712
	if ($date_posted != "")
713
	{
714
	    $date_array = explode(",", $date_posted);
715
	    
716
	    $date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING);
717
	    $date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING);
718
	    
719
	    if ($globalTimezone != '') {
720
		date_default_timezone_set($globalTimezone);
721
		$datetime = new DateTime();
722
		$offset = $datetime->format('P');
723
	    } else $offset = '+00:00';
724
725
726
	    if ($date_array[1] != "")
727
	    {                
728
		$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
729
		$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1]));
730
		if ($globalDBdriver == 'mysql') {
0 ignored issues
show
Bug introduced by
The variable $globalDBdriver does not exist. Did you mean $globalDbdriver?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
731
			$additional_query .= " AND TIMESTAMP(CONVERT_TZ(spotter_archive_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' AND TIMESTAMP(CONVERT_TZ(spotter_archive_output.date,'+00:00', '".$offset."')) <= '".$date_array[1]."' ";
732
		} else {
733
			$additional_query .= " AND spotter_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." >= CAST('".$date_array[0]."' AS TIMESTAMP) AND spotter_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." <= CAST('".$date_array[1]."' AS TIMESTAMP) ";
734
		}
735
	    } else {
736
		$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
737
                if ($globalDBdriver == 'mysql') {
0 ignored issues
show
Bug introduced by
The variable $globalDBdriver does not exist. Did you mean $globalDbdriver?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
738
			$additional_query .= " AND TIMESTAMP(CONVERT_TZ(spotter_archive_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' ";
739
		} else {
740
			$additional_query .= " AND spotter_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." >= CAST('".$date_array[0]."' AS TIMESTAMP) ";
741
		}
742
	    }
743
	}
744
	
745
	if ($limit != "")
746
	{
747
	    $limit_array = explode(",", $limit);
748
	    
749
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
750
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
751
	    
752
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
753
	    {
754
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
755
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
756
	    }
757
	}
758
	
759
760
	if ($origLat != "" && $origLon != "" && $dist != "") {
761
		$dist = number_format($dist*0.621371,2,'.','');
762
		$query="SELECT spotter_output.*, 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - ABS(CAST(spotter_archive.latitude as double precision)))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(ABS(CAST(spotter_archive.latitude as double precision))*pi()/180)*POWER(SIN(($origLon-CAST(spotter_archive.longitude as double precision))*pi()/180/2),2))) as distance 
763
                          FROM spotter_output_archive, spotter_archive WHERE spotter_output_archive.flightaware_id = spotter_archive.flightaware_id AND spotter_output.ident <> '' ".$additional_query."AND CAST(spotter_archive.longitude as double precision) between ($origLon-$dist/ABS(cos(radians($origLat))*69)) and ($origLon+$dist/ABS(cos(radians($origLat))*69)) and CAST(spotter_archive.latitude as double precision) between ($origLat-($dist/69)) and ($origLat+($dist/69)) 
764
                          AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - ABS(CAST(spotter_archive.latitude as double precision)))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(ABS(CAST(spotter_archive.latitude as double precision))*pi()/180)*POWER(SIN(($origLon-CAST(spotter_archive.longitude as double precision))*pi()/180/2),2)))) < $dist ORDER BY distance";
765
	} else {
766
		if ($sort != "")
767
		{
768
			$search_orderby_array = $this->getOrderBy();
0 ignored issues
show
Bug introduced by
The method getOrderBy() does not seem to exist on object<SpotterArchive>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
769
			$orderby_query = $search_orderby_array[$sort]['sql'];
770
		} else {
771
			$orderby_query = " ORDER BY spotter_archive_output.date DESC";
772
		}
773
	
774
		if ($includegeodata == "true")
775
		{
776
			$additional_query .= " AND (spotter_archive_output.waypoints <> '')";
777
		}
778
779
		$query  = "SELECT spotter_archive_output.* FROM spotter_archive_output 
780
		    WHERE spotter_archive_output.ident <> '' 
781
		    ".$additional_query."
782
		    ".$orderby_query;
783
	}
784
	$Spotter = new Spotter($this->db);
785
	$spotter_array = $Spotter->getDataFromDB($query, array(),$limit_query);
0 ignored issues
show
Bug introduced by
The variable $limit_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...
786
787
	return $spotter_array;
788
    }
789
790
    public function deleteSpotterArchiveData()
791
    {
792
		global $globalArchiveKeepMonths, $globalDBdriver;
793
                date_default_timezone_set('UTC');
794
                if ($globalDBdriver == 'mysql') {
795
			$query = 'DELETE FROM spotter_archive_output WHERE spotter_archive_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveKeepMonths.' MONTH)';
796
		} else {
797
			$query = "DELETE FROM spotter_archive_output WHERE spotter_archive_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveKeepMonths." MONTH'";
798
		}
799
                try {
800
                        $sth = $this->db->prepare($query);
801
                        $sth->execute();
802
                } catch(PDOException $e) {
803
                        return "error";
804
                }
805
	}
806
807
    /**
808
    * Gets all the spotter information based on the callsign
809
    *
810
    * @return Array the spotter information
811
    *
812
    */
813
    public function getSpotterDataByIdent($ident = '', $limit = '', $sort = '')
814
    {
815
	$global_query = "SELECT spotter_archive_output.* FROM spotter_archive_output";
816
	
817
	date_default_timezone_set('UTC');
818
	
819
	$query_values = array();
820
	
821
	if ($ident != "")
822
	{
823
	    if (!is_string($ident))
824
	    {
825
		return false;
826
	    } else {
827
		$additional_query = " AND (spotter_archive_output.ident = :ident)";
828
		$query_values = array(':ident' => $ident);
829
	    }
830
	}
831
	
832
	if ($limit != "")
833
	{
834
	    $limit_array = explode(",", $limit);
835
	    
836
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
837
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
838
	    
839
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
840
	    {
841
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
842
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
843
	    }
844
	}
845
846
	if ($sort != "")
847
	{
848
	    $search_orderby_array = $this->getOrderBy();
0 ignored issues
show
Bug introduced by
The method getOrderBy() does not seem to exist on object<SpotterArchive>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
849
	    $orderby_query = $search_orderby_array[$sort]['sql'];
850
	} else {
851
	    $orderby_query = " ORDER BY spotter_archive_output.date DESC";
852
	}
853
854
	$query = $global_query." WHERE spotter_archive_output.ident <> '' ".$additional_query." ".$orderby_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...
855
856
	$Spotter = new Spotter($this->db);
857
	$spotter_array = $Spotter->getDataFromDB($query, $query_values, $limit_query);
0 ignored issues
show
Bug introduced by
The variable $limit_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...
858
859
	return $spotter_array;
860
    }
861
862
    /**
863
    * Gets all number of flight over countries
864
    *
865
    * @return Array the airline country list
866
    *
867
    */
868
    public function countAllFlightOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '')
869
    {
870
	global $globalDBdriver;
871
	/*
872
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb 
873
		    FROM countries c, spotter_archive s
874
		    WHERE Within(GeomFromText(CONCAT('POINT(',s.longitude,' ',s.latitude,')')), ogc_geom) ";
875
	*/
876
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb
877
		    FROM countries c, spotter_archive s
878
		    WHERE c.iso2 = s.over_country ";
879
                if ($olderthanmonths > 0) {
880
            		if ($globalDBdriver == 'mysql') {
881
				$query .= 'AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
882
			} else {
883
				$query .= "AND date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
884
			}
885
		}
886
                if ($sincedate != '') $query .= "AND date > '".$sincedate."' ";
887
	$query .= "GROUP BY c.name, c.iso3, c.iso2 ORDER BY nb DESC";
888
	if ($limit) $query .= " LIMIT 0,10";
889
      
890
	
891
	$sth = $this->db->prepare($query);
892
	$sth->execute();
893
 
894
	$flight_array = array();
895
	$temp_array = array();
896
        
897
	while($row = $sth->fetch(PDO::FETCH_ASSOC))
898
	{
899
	    $temp_array['flight_count'] = $row['nb'];
900
	    $temp_array['flight_country'] = $row['name'];
901
	    $temp_array['flight_country_iso3'] = $row['iso3'];
902
	    $temp_array['flight_country_iso2'] = $row['iso2'];
903
	    $flight_array[] = $temp_array;
904
	}
905
	return $flight_array;
906
    }
907
908
}
909
?>
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...