Completed
Push — master ( 118bb3...d652ce )
by Yannick
29:38
created

APRSSpotter::addLiveSpotterData()   D

Complexity

Conditions 13
Paths 163

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 24
nc 163
nop 28
dl 0
loc 31
rs 4.8178
c 0
b 0
f 0

How to fix   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
require_once(dirname(__FILE__).'/settings.php');
3
require_once(dirname(__FILE__).'/class.Common.php');
4
class aprs {
5
    private $socket;
6
    private $connected = false;
7
8
    protected $symbols = array('!' => 'Police',
9
	'#' => 'DIGI',
10
	'$' => 'Phone',
11
	'%' => 'DX Cluster',
12
	'&' => 'HF Gateway',
13
	"'" => 'Aircraft (small)',
14
	'(' => 'Cloudy',
15
	'*' => 'Snowmobile',
16
	'+' => 'Red Cross',
17
	',' => 'Reverse L Shape',
18
	'-' => 'House QTH (VHF)',
19
	'.' => 'X',
20
	'/' => 'Dot',
21
	'0' => '0',
22
	'1' => '1',
23
	'2' => '2',
24
	'3' => '3',
25
	'4' => '4',
26
	'5' => '5',
27
	'6' => '6',
28
	'7' => '7',
29
	'8' => '8',
30
	'9' => '9',
31
	':' => 'Fire',
32
	';' => 'Campground',
33
	'<' => 'Motorcycle',
34
	'=' => 'Railroad Engine',
35
	'>' => 'Car',
36
	'?' => 'Server for Files',
37
	'@' => 'HC Future Predict',
38
	'A' => 'Aid Station',
39
	'B' => 'BBS',
40
	'C' => 'Canoe',
41
	'E' => 'Eyeball',
42
	'G' => 'Grid Square',
43
	'H' => 'Hotel',
44
	'I' => 'TCP-IP',
45
	'K' => 'School',
46
	'M' => 'MacAPRS',
47
	'N' => 'NTS Station',
48
	'O' => 'Balloon',
49
	'P' => 'Police',
50
	'Q' => 'T.B.D.',
51
	'R' => 'Recreational Vehicle',
52
	'S' => 'Shuttle',
53
	'T' => 'SSTV',
54
	'U' => 'Bus',
55
	'V' => 'ATV',
56
	'W' => 'National Weather Service Site',
57
	'X' => 'Helicopter',
58
	'Y' => 'Yacht (Sail)',
59
	'Z' => 'WinAPRS',
60
	'[' => 'Jogger',
61
	']' => 'PBBS',
62
	'^' => 'Large Aircraft',
63
	'_' => 'Weather Station',
64
	'`' => 'Dish Antenna',
65
	'a' => 'Ambulance',
66
	'b' => 'Bike',
67
	'c' => 'T.B.D.',
68
	'd' => 'Dial Garage (Fire Department)',
69
	'e' => 'Horse (Equestrian)',
70
	'f' => 'Firetruck',
71
	'g' => 'Glider',
72
	'h' => 'Hospital',
73
	'i' => 'IOTA (Islands On The Air)',
74
	'j' => 'Jeep',
75
	'k' => 'Truck',
76
	'l' => 'Laptop',
77
	'm' => 'Mic-Repeater',
78
	'n' => 'Node',
79
	'o' => 'EOC',
80
	'p' => 'Rover (Puppy)',
81
	'q' => 'Grid SQ Shown Above 128 Miles',
82
	'r' => 'Antenna',
83
	's' => 'Ship (Power Boat)',
84
	't' => 'Truck Stop',
85
	'u' => 'Truck (18 Wheeler)',
86
	'v' => 'Van',
87
	'w' => 'Water Station',
88
	'x' => 'xAPRS (UNIX)',
89
	'y' => 'Yagi At QTH');
90
	
91
92
    private function urshift($n, $s) {
93
	return ($n >= 0) ? ($n >> $s) :
94
	    (($n & 0x7fffffff) >> $s) | 
95
		(0x40000000 >> ($s - 1));
96
    }
97
98
    public function parse($input) {
99
	global $globalDebug;
100
	$debug = false;
101
	$result = array();
102
	$input_len = strlen($input);
103
	//$split_input = str_split($input);
104
105
	/* Find the end of header checking for NULL bytes while doing it. */
106
	$splitpos = strpos($input,':');
107
	
108
	/* Check that end was found and body has at least one byte. */
109
	if ($splitpos == 0 || $splitpos + 1 == $input_len || $splitpos === FALSE) {
110
	    if ($globalDebug) echo '!!! APRS invalid : '.$input."\n";
111
	    return false;
112
	}
113
	
114
	if ($debug) echo 'input : '.$input."\n";
115
	/* Save header and body. */
116
	$body = substr($input,$splitpos+1,$input_len);
117
	$body_len = strlen($body);
0 ignored issues
show
Unused Code introduced by
$body_len 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...
118
	$header = substr($input,0,$splitpos);
119
	//$header_len = strlen($header);
120
	if ($debug) echo 'header : '.$header."\n";
121
	
122
	/* Parse source, target and path. */
123
	//FLRDF0A52>APRS,qAS,LSTB
124
	if (preg_match('/^([A-Z0-9\\-]{1,9})>(.*)$/',$header,$matches)) {
125
	    $ident = $matches[1];
126
	    $all_elements = $matches[2];
127
	    if ($ident == 'AIRCRAFT') {
128
		$result['format_source'] = 'famaprs';
129
		$result['source_type'] = 'modes';
130
	    } elseif ($ident == 'MARINE') {
131
		$result['format_source'] = 'famaprs';
132
		$result['source_type'] = 'ais';
133
	    } else {
134
		if ($debug) echo 'ident : '.$ident."\n";
135
		$result['ident'] = $ident;
136
	    }
137
	} else return false;
138
	$elements = explode(',',$all_elements);
139
	$source = end($elements);
140
	$result['source'] = $source;
141
	foreach ($elements as $element) {
142
	    if (preg_match('/^([a-zA-Z0-9-]{1,9})([*]?)$/',$element)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
143
	        //echo "ok";
144
	        //if ($element == 'TCPIP*') return false;
145
	    } elseif (!preg_match('/^([0-9A-F]{32})$/',$element)) {
146
		if ($debug) echo 'element : '.$element."\n";
147
		return false;
148
	    }
149
	    /*
150
	    } elseif (preg_match('/^([0-9A-F]{32})$/',$element)) {
151
		//echo "ok";
152
	    } else {
153
	        return false;
154
	    }
155
	    */
156
	}
157
	
158
	$type = substr($body,0,1);
159
	if ($debug) echo 'type : '.$type."\n";
160
	if ($type == ';') {
161
		if (isset($result['source_type']) && $result['source_type'] == 'modes') {
162
			$result['address'] = trim(substr($body,1,9));
163
		} elseif (isset($result['source_type']) && $result['source_type'] == 'ais') {
164
			$result['mmsi'] = trim(substr($body,1,9));
165
		} else $result['ident'] = trim(substr($body,1,9));
166
	} elseif ($type == ',') {
167
		// Invalid data or test data
168
		return false;
169
	}
170
	
171
	// Check for Timestamp
172
	$find = false;
173
	$body_parse = substr($body,1);
174
	//echo 'Body : '.$body."\n";
175
	if (preg_match('/^;(.){9}\*/',$body,$matches)) {
176
	    $body_parse = substr($body_parse,10);
177
	    $find = true;
178
	    //echo $body_parse."\n";
179
	}
180
	if (preg_match('/^`(.*)\//',$body,$matches)) {
181
	    $body_parse = substr($body_parse,strlen($matches[1])-1);
182
	    $find = true;
183
	    //echo $body_parse."\n";
184
	}
185
	if (preg_match("/^'(.*)\//",$body,$matches)) {
186
	    $body_parse = substr($body_parse,strlen($matches[1])-1);
187
	    $find = true;
188
	    //echo $body_parse."\n";
189
	}
190
	if (preg_match('/^([0-9]{2})([0-9]{2})([0-9]{2})([zh\\/])/',$body_parse,$matches)) {
191
	    $find = true;
192
	    //print_r($matches);
193
	    $timestamp = $matches[0];
194
	    if ($matches[4] == 'h') {
195
		$timestamp = strtotime(date('Ymd').' '.$matches[1].':'.$matches[2].':'.$matches[3]);
196
		//echo 'timestamp : '.$timestamp.' - now : '.time()."\n";
197
		/*
198
		if (time() + 3900 < $timestamp) $timestamp -= 86400;
199
		elseif (time() - 82500 > $timestamp) $timestamp += 86400;
200
		*/
201
	    } elseif ($matches[4] == 'z' || $matches[4] == '/') {
202
		// This work or not ?
203
		$timestamp = strtotime(date('Ym').$matches[1].' '.$matches[2].':'.$matches[3]);
204
	    }
205
	    $body_parse = substr($body_parse,7);
206
	    $result['timestamp'] = $timestamp;
207
	    //echo date('Ymd H:i:s',$timestamp);
208
	}
209
	if (preg_match('/^([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/',$body_parse,$matches)) {
210
	    $find = true;
211
	    $timestamp = strtotime(date('Y').$matches[1].$matches[2].' '.$matches[3].':'.$matches[4]);
212
	    $body_parse = substr($body_parse,8);
213
	    $result['timestamp'] = $timestamp;
214
	    //echo date('Ymd H:i:s',$timestamp);
215
	}
216
	//if (strlen($body_parse) > 19) {
217
	    if (preg_match('/^([0-9]{2})([0-7 ][0-9 ]\\.[0-9 ]{2})([NnSs])(.)([0-9]{3})([0-7 ][0-9 ]\\.[0-9 ]{2})([EeWw])(.)/',$body_parse,$matches)) {
218
	    $find = true;
219
		// 4658.70N/00707.78Ez
220
		//print_r(str_split($body_parse));
221
		
222
		//$latlon = $matches[0];
223
		$sind = strtoupper($matches[3]);
224
		$wind = strtoupper($matches[7]);
225
		$lat_deg = $matches[1];
226
		$lat_min = $matches[2];
227
		$lon_deg = $matches[5];
228
		$lon_min = $matches[6];
229
	    
230
		//$symbol_table = $matches[4];
231
		$lat = intval($lat_deg);
232
		$lon = intval($lon_deg);
233
		if ($lat > 89 || $lon > 179) return false;
234
	    
235
	    /*
236
	    $tmp_5b = str_replace('.','',$lat_min);
237
	    if (preg_match('/^([0-9]{0,4})( {0,4})$/',$tmp_5b,$matches)) {
238
	        print_r($matches);
239
	    }
240
	    */
241
		$latitude = $lat + floatval($lat_min)/60;
242
		$longitude = $lon + floatval($lon_min)/60;
243
		if ($sind == 'S') $latitude = 0-$latitude;
244
		if ($wind == 'W') $longitude = 0-$longitude;
245
		$result['latitude'] = $latitude;
246
		$result['longitude'] = $longitude;
247
		$body_parse = substr($body_parse,18);
248
		$body_parse_len = strlen($body_parse);
0 ignored issues
show
Unused Code introduced by
$body_parse_len 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...
249
	    }
250
	    $body_parse_len = strlen($body_parse);
251
	    if ($body_parse_len > 0) {
252
		/*
253
		if (!isset($result['timestamp']) && !isset($result['latitude'])) {
254
			$body_split = str_split($body);
255
			$symbol_code = $body_split[0];
256
			$body_parse = substr($body,1);
257
			$body_parse_len = strlen($body_parse);
258
		} else { 
259
		*/
260
		/*
261
		if ($find === false) {
262
			$body_split = str_split($body);
263
			$symbol_code = $body_split[0];
264
			$body_parse = substr($body,1);
265
			$body_parse_len = strlen($body_parse);
266
		} else { 
267
		*/
268
		if ($find) {
269
			$body_split = str_split($body_parse);
270
			$symbol_code = $body_split[0];
271
		//}
272
		//echo $body_parse;
273
			//if ($type != ';' && $type != '>') {
274
			if ($type != '') {
275
			$body_parse = substr($body_parse,1);
276
			$body_parse_len = strlen($body_parse);
277
			$result['symbol_code'] = $symbol_code;
278
			if (isset($this->symbols[$symbol_code])) $result['symbol'] = $this->symbols[$symbol_code];
279
			if ($symbol_code != '_') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
280
			}
281
		    //$body_parse = substr($body_parse,1);
282
		    //$body_parse = trim($body_parse);
283
		    //$body_parse_len = strlen($body_parse);
284
		    if ($body_parse_len >= 7) {
285
			
286
		        if (preg_match('/^([0-9\\. ]{3})\\/([0-9\\. ]{3})/',$body_parse)) {
287
		    	    $course = substr($body_parse,0,3);
288
		    	    $tmp_s = intval($course);
289
		    	    if ($tmp_s >= 1 && $tmp_s <= 360) $result['heading'] = intval($course);
290
		    	    $speed = substr($body_parse,4,3);
291
		    	    if ($speed != '...') {
292
		    		//$result['speed'] = round($speed*1.852);
293
		    		$result['speed'] = intval($speed);
294
		    	    }
295
		    	    $body_parse = substr($body_parse,7);
296
		        }
297
		        // Check PHGR, PHG, RNG
298
		    } 
299
		    /*
300
		    else if ($body_parse_len > 0) {
301
			$rest = $body_parse;
302
		    }
303
		    */
304
		    if (strlen($body_parse) > 0) {
305
		        if (preg_match('/\\/A=(-[0-9]{5}|[0-9]{6})/',$body_parse,$matches)) {
306
		            $altitude = intval($matches[1]);
307
		            //$result['altitude'] = round($altitude*0.3048);
308
		            $result['altitude'] = $altitude;
309
		            //$body_parse = trim(substr($body_parse,strlen($matches[0])));
310
		            $body_parse = trim(preg_replace('/\\/A=(-[0-9]{5}|[0-9]{6})/','',$body_parse));
311
		        }
312
		    }
313
		    
314
		    // Telemetry
315
		    /*
316
		    if (preg_match('/^([0-9]+),(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,([01]{0,8})/',$body_parse,$matches)) {
317
		        // Nothing yet...
318
		    }
319
		    */
320
		    // DAO
321
		    
322
		    if (preg_match('/^!([0-9A-Z]{3})/',$body_parse,$matches)) {
323
			    
324
			    $dao = $matches[1];
325
			    if (preg_match('/^([A-Z])([0-9]{2})/',$dao)) {
326
				$dao_split = str_split($dao);
327
			        $lat_off = (($dao_split[1])-48.0)*0.001/60.0;
328
			        $lon_off = (($dao_split[2])-48.0)*0.001/60.0;
329
			    
330
				if ($result['latitude'] < 0) $result['latitude'] -= $lat_off;
331
				else $result['latitude'] += $lat_off;
332
				if ($result['longitude'] < 0) $result['longitude'] -= $lon_off;
333
				else $result['longitude'] += $lon_off;
334
			    }
335
			    
336
		            $body_parse = substr($body_parse,6);
337
		    }
338
		    
339
		    if (preg_match('/CS=([0-9A-Z_]*)/',$body_parse,$matches)) {
340
			$result['ident'] = str_replace('_',' ',$matches[1]);
341
		    }
342
		    if (preg_match('/SQ=([0-9]{4})/',$body_parse,$matches)) {
343
			$result['squawk'] = $matches[1];
344
		    }
345
		    if (preg_match('/AI=([0-9A-Z]{4})/',$body_parse,$matches)) {
346
			$result['aircraft_icao'] = $matches[1];
347
		    }
348
		    if (preg_match('/VR=([0-9]*)/',$body_parse,$matches)) {
349
			$result['verticalrate'] = $matches[1];
350
		    }
351
		    if (preg_match('/TI=([0-9]*)/',$body_parse,$matches)) {
352
			$result['typeid'] = $matches[1];
353
		    }
354
		    if (preg_match('/SI=([0-9]*)/',$body_parse,$matches)) {
355
			$result['statusid'] = $matches[1];
356
		    }
357
		    if (preg_match('/IMO=([0-9]{7})/',$body_parse,$matches)) {
358
			$result['imo'] = $matches[1];
359
		    }
360
		    if (preg_match('/AD=([0-9]*)/',$body_parse,$matches)) {
361
			$result['arrival_date'] = $matches[1];
362
		    }
363
		    if (preg_match('/AC=([0-9A-Z_]*)/',$body_parse,$matches)) {
364
			$result['arrival_code'] = str_replace('_',' ',$matches[1]);
365
		    }
366
		    // OGN comment
367
		   // echo "Before OGN : ".$body_parse."\n";
368
		    //if (preg_match('/^id([0-9A-F]{8}) ([+-])([0-9]{3,4})fpm ([+-])([0-9.]{3,4})rot (.*)$/',$body_parse,$matches)) {
369
		    if (preg_match('/^id([0-9A-F]{8})/',$body_parse,$matches)) {
370
			$id = $matches[1];
371
			//$mode = substr($id,0,2);
372
			$address = substr($id,2);
373
			//print_r($matches);
374
			$addressType = (intval(substr($id,0,2),16))&3;
375
			if ($addressType == 0) $result['addresstype'] = "RANDOM";
376
			elseif ($addressType == 1) $result['addresstype'] = "ICAO";
377
			elseif ($addressType == 2) $result['addresstype'] = "FLARM";
378
			elseif ($addressType == 3) $result['addresstype'] = "OGN";
379
			$aircraftType = $this->urshift(((intval(substr($id,0,2),16)) & 0b1111100),2);
380
			$result['aircrafttype_code'] = $aircraftType;
381
			if ($aircraftType == 0) $result['aircrafttype'] = "UNKNOWN";
382
			elseif ($aircraftType == 1) $result['aircrafttype'] = "GLIDER";
383
			elseif ($aircraftType == 2) $result['aircrafttype'] = "TOW_PLANE";
384
			elseif ($aircraftType == 3) $result['aircrafttype'] = "HELICOPTER_ROTORCRAFT";
385
			elseif ($aircraftType == 4) $result['aircrafttype'] = "PARACHUTE";
386
			elseif ($aircraftType == 5) $result['aircrafttype'] = "DROP_PLANE";
387
			elseif ($aircraftType == 6) $result['aircrafttype'] = "HANG_GLIDER";
388
			elseif ($aircraftType == 7) $result['aircrafttype'] = "PARA_GLIDER";
389
			elseif ($aircraftType == 8) $result['aircrafttype'] = "POWERED_AIRCRAFT";
390
			elseif ($aircraftType == 9) $result['aircrafttype'] = "JET_AIRCRAFT";
391
			elseif ($aircraftType == 10) $result['aircrafttype'] = "UFO";
392
			elseif ($aircraftType == 11) $result['aircrafttype'] = "BALLOON";
393
			elseif ($aircraftType == 12) $result['aircrafttype'] = "AIRSHIP";
394
			elseif ($aircraftType == 13) $result['aircrafttype'] = "UAV";
395
			elseif ($aircraftType == 15) $result['aircrafttype'] = "STATIC_OBJECT";
396
			$stealth = (intval(substr($id,0,2), 16) & 0b10000000) != 0;
397
			$result['stealth'] = $stealth;
398
			$result['address'] = $address;
399
		    }
400
		    
401
		    //Comment
402
		    $result['comment'] = trim($body_parse);
403
		} else {
404
		    // parse weather
405
		    //$body_parse = substr($body_parse,1);
406
		    //$body_parse_len = strlen($body_parse);
407
408
		    if (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) {
409
			    $result['wind_dir'] = intval($matches[1]);
410
			    $result['wind_speed'] = round(intval($matches[2])*1.60934,1);
411
			    $result['wind_gust'] = round(intval($matches[3])*1.60934,1);
412
			    $result['temp'] = round(5/9*((intval($matches[4]))-32),1);
413
		    	    $body_parse = substr($body_parse,strlen($matches[0])+1);
414
		    } elseif (preg_match('/^_{0,1}c([0-9 \\.\\-]{3})s([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) {
415
			$result['wind_dir'] = intval($matches[1]);
416
			$result['wind_speed'] = round($matches[2]*1.60934,1);
417
			$result['wind_gust'] = round($matches[3]*1.60934,1);
418
			$result['temp'] = round(5/9*(($matches[4])-32),1);
419
		        $body_parse = substr($body_parse,strlen($matches[0])+1);
420
		    } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) {
421
			$result['wind_dir'] = intval($matches[1]);
422
			$result['wind_speed'] = round($matches[2]*1.60934,1);
423
			$result['wind_gust'] = round($matches[3]*1.60934,1);
424
		        $body_parse = substr($body_parse,strlen($matches[0])+1);
425
		    } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)/',$body_parse,$matches)) {
426
			$result['wind_dir'] = intval($matches[1]);
427
			$result['wind_speed'] = round($matches[2]*1.60934,1);
428
			$result['wind_gust'] = round($matches[3]*1.60934,1);
429
		        $body_parse = substr($body_parse,strlen($matches[0])+1);
430
		    }
431
		    if (!isset($result['temp']) && strlen($body_parse) > 0 && preg_match('/^g([0-9]+)t(-?[0-9 \\.]{1,3})/',$body_parse,$matches)) {
432
			$result['temp'] = round(5/9*(($matches[1])-32),1);
433
		    }
434
		}
435
		} else $result['comment'] = trim($body_parse);
436
437
	    }
438
	//}
439
	if (isset($result['latitude'])) $result['latitude'] = round($result['latitude'],4);
440
	if (isset($result['longitude'])) $result['longitude'] = round($result['longitude'],4);
441
	if ($debug) print_r($result);
442
	return $result;
443
    }
444
    
445
    public function connect() {
446
	global $globalAPRSversion, $globalServerAPRSssid, $globalServerAPRSpass,$globalName, $globalServerAPRShost, $globalServerAPRSport;
447
	$aprs_connect = 0;
0 ignored issues
show
Unused Code introduced by
$aprs_connect 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...
448
	$aprs_keep = 120;
0 ignored issues
show
Unused Code introduced by
$aprs_keep 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...
449
	$aprs_last_tx = time();
0 ignored issues
show
Unused Code introduced by
$aprs_last_tx 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...
450
	if (isset($globalAPRSversion)) $aprs_version = $globalAPRSversion;
451
	else $aprs_version = 'FlightAirMap '.str_replace(' ','_',$globalName);
452
	if (isset($globalServerAPRSssid)) $aprs_ssid = $globalServerAPRSssid;
453
	else $aprs_ssid = substr('FAM'.strtoupper(str_replace(' ','_',$globalName)),0,8);
454
	if (isset($globalServerAPRSpass)) $aprs_pass = $globalServerAPRSpass;
455
	else $aprs_pass = '-1';
456
	
457
	$aprs_filter  = '';
0 ignored issues
show
Unused Code introduced by
$aprs_filter 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...
458
	$aprs_login = "user {$aprs_ssid} pass {$aprs_pass} vers {$aprs_version}\n";
459
	$Common = new Common();
460
	$s = $Common->create_socket($globalServerAPRShost,$globalServerAPRSport,$errno,$errstr);
461
	if ($s !== false) {
462
		echo 'Connected to APRS server! '."\n";
463
		$authstart = time();
464
		$this->socket = $s;
465
		$send = socket_send( $this->socket  , $aprs_login , strlen($aprs_login) , 0 );
0 ignored issues
show
Unused Code introduced by
$send 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...
466
		while ($msgin = socket_read($this->socket, 1000,PHP_NORMAL_READ)) {
467
			if (strpos($msgin, "$aprs_ssid verified") !== FALSE) {
468
			    echo 'APRS user verified !'."\n";
469
			    $this->connected = true;
470
			    return true;
471
			    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
472
			}
473
			if (time()-$authstart > 5) {
474
			    echo 'APRS timeout'."\n";
475
			    break;
476
			}
477
		}
478
		socket_set_option($this->socket,SOL_SOCKET,SO_KEEPALIVE);
479
	}
480
    }
481
482
    public function disconnect() {
483
	socket_close($this->socket);
484
    }
485
    
486
    public function send($data) {
487
	if ($this->connected === false) $this->connect();
488
	$send = socket_send( $this->socket  , $data , strlen($data),0);
489
	if ($send === FALSE) {
490
		socket_close($this->socket);
491
		$this->connect();
492
	}
493
    }
494
}
495
496
class APRSSpotter extends APRS {
497
	public function addLiveSpotterData($id,$ident,$aircraft_icao,$departure_airport,$arrival_airport,$latitude,$longitude,$waypoints,$altitude,$altitude_real,$heading,$speed,$datetime,$departure_airport_time,$arrival_airport_time,$squawk,$route_stop,$hex,$putinarchive,$registration,$pilot_id,$pilot_name, $verticalrate, $noarchive, $ground,$format_source,$source_name,$over_country) {
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
Unused Code introduced by
The parameter $departure_airport 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...
Unused Code introduced by
The parameter $arrival_airport 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...
Unused Code introduced by
The parameter $waypoints 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...
Unused Code introduced by
The parameter $altitude 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...
Unused Code introduced by
The parameter $departure_airport_time 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...
Unused Code introduced by
The parameter $arrival_airport_time 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...
Unused Code introduced by
The parameter $route_stop 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...
Unused Code introduced by
The parameter $putinarchive 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...
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...
Unused Code introduced by
The parameter $pilot_id 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...
Unused Code introduced by
The parameter $pilot_name 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...
Unused Code introduced by
The parameter $noarchive 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...
Unused Code introduced by
The parameter $ground 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...
Unused Code introduced by
The parameter $format_source 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...
Unused Code introduced by
The parameter $source_name 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...
Unused Code introduced by
The parameter $over_country 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...
498
		$Common = new Common();
499
		if ($latitude != '' && $longitude != '') {
500
			$latitude = $Common->convertDM($latitude,'latitude');
501
			$longitude = $Common->convertDM($longitude,'longitude');
502
			$coordinate = sprintf("%02d",$latitude['deg']).str_pad(number_format($latitude['min'],2,'.',''),5,'0',STR_PAD_LEFT).$latitude['NSEW'].'/'.sprintf("%03d",$longitude['deg']).str_pad(number_format($longitude['min'],2,'.',''),5,'0',STR_PAD_LEFT).$longitude['NSEW'];
503
			$w1 = abs(ceil(($latitude['min'] - number_format($latitude['min'],2,'.',''))*1000));
504
			$w2 = abs(ceil(($longitude['min'] - number_format($longitude['min'],2,'.',''))*1000));
505
			$w = $w1.$w2;
506
			//$w = '00';
507
			$custom = '';
508
			if ($ident != '') {
509
				if ($custom != '') $custom .= '/';
510
				$custom .= 'CS='.$ident;
511
			}
512
			if ($squawk != '') {
513
				if ($custom != '') $custom .= '/';
514
				$custom .= 'SQ='.$squawk;
515
			}
516
			if ($verticalrate != '') {
517
				if ($custom != '') $custom .= '/';
518
				$custom .= 'VR='.$verticalrate;
519
			}
520
			if ($aircraft_icao != '' && $aircraft_icao != 'NA') {
521
				if ($custom != '') $custom .= '/';
522
				$custom .= 'AI='.$aircraft_icao;
523
			}
524
			if ($custom != '') $custom = ' '.$custom;
525
			$this->send('AIRCRAFT>APRS,TCPIP*:;'.$hex.'   *'.date('His',strtotime($datetime)).'h'.$coordinate.'^'.str_pad($heading,3,'0',STR_PAD_LEFT).'/'.str_pad($speed,3,'0',STR_PAD_LEFT).'/A='.str_pad($altitude_real,6,'0',STR_PAD_LEFT).' !W'.$w.'!'.$custom."\n");
526
		}
527
	}
528
}
529
class APRSMarine extends APRS {
530
	public function addLiveMarineData($id, $ident, $latitude, $longitude, $heading, $speed,$datetime, $putinarchive,$mmsi,$type,$typeid,$imo,$callsign,$arrival_code,$arrival_date,$status,$statusid,$noarchive,$format_source,$source_name,$over_country) {
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
Unused Code introduced by
The parameter $putinarchive 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...
Unused Code introduced by
The parameter $type 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...
Unused Code introduced by
The parameter $callsign 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...
Unused Code introduced by
The parameter $status 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...
Unused Code introduced by
The parameter $noarchive 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...
Unused Code introduced by
The parameter $format_source 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...
Unused Code introduced by
The parameter $source_name 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...
Unused Code introduced by
The parameter $over_country 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...
531
		$Common = new Common();
532
		if ($latitude != '' && $longitude != '') {
533
			$latitude = $Common->convertDM($latitude,'latitude');
534
			$longitude = $Common->convertDM($longitude,'longitude');
535
			$coordinate = sprintf("%02d",$latitude['deg']).str_pad(number_format($latitude['min'],2,'.',''),5,'0',STR_PAD_LEFT).$latitude['NSEW'].'/'.sprintf("%03d",$longitude['deg']).str_pad(number_format($longitude['min'],2,'.',''),5,'0',STR_PAD_LEFT).$longitude['NSEW'];
536
			$w1 = abs(ceil(($latitude['min'] - number_format($latitude['min'],2,'.',''))*1000));
537
			$w2 = abs(ceil(($longitude['min'] - number_format($longitude['min'],2,'.',''))*1000));
538
			$w = $w1.$w2;
539
			//$w = '00';
540
			$custom = '';
541
			if ($ident != '') {
542
				if ($custom != '') $custom .= '/';
543
				$custom .= 'CS='.str_replace(' ','_',$ident);
544
			}
545
			if ($typeid != '') {
546
				if ($custom != '') $custom .= '/';
547
				$custom .= 'TI='.$typeid;
548
			}
549
			if ($statusid != '') {
550
				if ($custom != '') $custom .= '/';
551
				$custom .= 'SI='.$statusid;
552
			}
553
			if ($imo != '') {
554
				if ($custom != '') $custom .= '/';
555
				$custom .= 'IMO='.$imo;
556
			}
557
			if ($arrival_date != '') {
558
				if ($custom != '') $custom .= '/';
559
				$custom .= 'AD='.strtotime($arrival_date);
560
			}
561
			if ($arrival_code != '') {
562
				if ($custom != '') $custom .= '/';
563
				$custom .= 'AC='.str_replace(' ','_',$arrival_code);
564
			}
565
			if ($custom != '') $custom = ' '.$custom;
566
			$altitude = 0;
567
			$this->send('MARINE>APRS,TCPIP*:;'.$mmsi.'*'.date('His',strtotime($datetime)).'h'.$coordinate.'s'.str_pad($heading,3,'0',STR_PAD_LEFT).'/'.str_pad($speed,3,'0',STR_PAD_LEFT).'/A='.str_pad($altitude,6,'0',STR_PAD_LEFT).' !W'.$w.'!'.$custom."\n");
568
		}
569
	}
570
}
571
?>