Completed
Push — master ( c1d943...56b04e )
by Yannick
07:43
created

aprs::parse()   F

Complexity

Conditions 66
Paths > 20000

Size

Total Lines 273
Code Lines 155

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 66
eloc 155
nc 429496.7295
nop 1
dl 0
loc 273
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
class aprs {
3
    protected $symbols = array('!' => 'Police',
4
	'#' => 'DIGI',
5
	'$' => 'Phone',
6
	'%' => 'DX Cluster',
7
	'&' => 'HF Gateway',
8
	"'" => 'Aircraft (small)',
9
	'(' => 'Cloudy',
10
	'*' => 'Snowmobile',
11
	'+' => 'Red Cross',
12
	',' => 'Reverse L Shape',
13
	'-' => 'House QTH (VHF)',
14
	'.' => 'X',
15
	'/' => 'Dot',
16
	'0' => '0',
17
	'1' => '1',
18
	'2' => '2',
19
	'3' => '3',
20
	'4' => '4',
21
	'5' => '5',
22
	'6' => '6',
23
	'7' => '7',
24
	'8' => '8',
25
	'9' => '9',
26
	':' => 'Fire',
27
	';' => 'Campground',
28
	'<' => 'Motorcycle',
29
	'=' => 'Railroad Engine',
30
	'>' => 'Car',
31
	'?' => 'Server for Files',
32
	'@' => 'HC Future Predict',
33
	'A' => 'Aid Station',
34
	'B' => 'BBS',
35
	'C' => 'Canoe',
36
	'E' => 'Eyeball',
37
	'G' => 'Grid Square',
38
	'H' => 'Hotel',
39
	'I' => 'TCP-IP',
40
	'K' => 'School',
41
	'M' => 'MacAPRS',
42
	'N' => 'NTS Station',
43
	'O' => 'Balloon',
44
	'P' => 'Police',
45
	'Q' => 'T.B.D.',
46
	'R' => 'Recreational Vehicle',
47
	'S' => 'Shuttle',
48
	'T' => 'SSTV',
49
	'U' => 'Bus',
50
	'V' => 'ATV',
51
	'W' => 'National Weather Service Site',
52
	'X' => 'Helicopter',
53
	'Y' => 'Yacht (Sail)',
54
	'Z' => 'WinAPRS',
55
	'[' => 'Jogger',
56
	']' => 'PBBS',
57
	'^' => 'Large Aircraft',
58
	'_' => 'Weather Station',
59
	'`' => 'Dish Antenna',
60
	'a' => 'Ambulance',
61
	'b' => 'Bike',
62
	'c' => 'T.B.D.',
63
	'd' => 'Dial Garage (Fire Department)',
64
	'e' => 'Horse (Equestrian)',
65
	'f' => 'Firetruck',
66
	'g' => 'Glider',
67
	'h' => 'Hospital',
68
	'i' => 'IOTA (Islands On The Air)',
69
	'j' => 'Jeep',
70
	'k' => 'Truck',
71
	'l' => 'Laptop',
72
	'm' => 'Mic-Repeater',
73
	'n' => 'Node',
74
	'o' => 'EOC',
75
	'p' => 'Rover (Puppy)',
76
	'q' => 'Grid SQ Shown Above 128 Miles',
77
	'r' => 'Antenna',
78
	's' => 'Ship (Power Boat)',
79
	't' => 'Truck Stop',
80
	'u' => 'Truck (18 Wheeler)',
81
	'v' => 'Van',
82
	'w' => 'Water Station',
83
	'x' => 'xAPRS (UNIX)',
84
	'y' => 'Yagi At QTH');
85
	
86
87
    private function urshift($n, $s) {
88
	return ($n >= 0) ? ($n >> $s) :
89
    	    (($n & 0x7fffffff) >> $s) | 
90
        	(0x40000000 >> ($s - 1));
91
    }
92
93
    public function parse($input) {
94
	global $globalDebug;
95
	$debug = false;
96
	$result = array();
97
	$input_len = strlen($input);
98
	//$split_input = str_split($input);
99
100
	/* Find the end of header checking for NULL bytes while doing it. */
101
	$splitpos = strpos($input,':');
102
	
103
	/* Check that end was found and body has at least one byte. */
104
	if ($splitpos == 0 || $splitpos + 1 == $input_len || $splitpos === FALSE) {
105
	    if ($globalDebug) echo '!!! APRS invalid : '.$input."\n";
106
	    return false;
107
	}
108
	
109
	/* Save header and body. */
110
	$body = substr($input,$splitpos+1,$input_len);
111
	$body_len = strlen($body);
112
	$header = substr($input,0,$splitpos);
113
	//$header_len = strlen($header);
114
	if ($debug) echo 'header : '.$header."\n";
115
	
116
	/* Parse source, target and path. */
117
	//FLRDF0A52>APRS,qAS,LSTB
118
	if (preg_match('/^([A-Z0-9\\-]{1,9})>(.*)$/',$header,$matches)) {
119
	    $ident = $matches[1];
120
	    $all_elements = $matches[2];
121
	    if ($debug) echo 'ident : '.$ident."\n";
122
	    $result['ident'] = $ident;
123
	} else return false;
124
	$elements = explode(',',$all_elements);
125
	$source = end($elements);
126
	$result['source'] = $source;
127
	foreach ($elements as $element) {
128
	    if (preg_match('/^([a-zA-Z0-9-]{1,9})([*]?)$/',$element)) {
129
	        //echo "ok";
130
	        if ($element == 'TCPIP*') return false;
131
	    } elseif (!preg_match('/^([0-9A-F]{32})$/',$element)) {
132
		return false;
133
	    }
134
	    /*
135
	    } elseif (preg_match('/^([0-9A-F]{32})$/',$element)) {
136
		//echo "ok";
137
	    } else {
138
	        return false;
139
	    }
140
	    */
141
	}
142
	// Check for Timestamp
143
	$find = false;
0 ignored issues
show
Unused Code introduced by
$find 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...
144
	$body_parse = substr($body,1);
145
	//echo 'Body : '.$body."\n";
146
	if (preg_match('/^;(.){9}\*/',$body,$matches)) {
147
	    $body_parse = substr($body_parse,10);
148
	    $find = true;
0 ignored issues
show
Unused Code introduced by
$find 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...
149
	    //echo $body_parse."\n";
150
	}
151
	if (preg_match('/^`(.*)\//',$body,$matches)) {
152
	    $body_parse = substr($body_parse,strlen($matches[1])-1);
153
	    $find = true;
0 ignored issues
show
Unused Code introduced by
$find 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...
154
	    //echo $body_parse."\n";
155
	}
156
	if (preg_match("/^'(.*)\//",$body,$matches)) {
157
	    $body_parse = substr($body_parse,strlen($matches[1])-1);
158
	    $find = true;
0 ignored issues
show
Unused Code introduced by
$find 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...
159
	    //echo $body_parse."\n";
160
	}
161
	if (preg_match('/^([0-9]{2})([0-9]{2})([0-9]{2})([zh\\/])/',$body_parse,$matches)) {
162
	    $find = true;
0 ignored issues
show
Unused Code introduced by
$find 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...
163
	    //print_r($matches);
164
	    $timestamp = $matches[0];
165
	    if ($matches[4] == 'h') {
166
		$timestamp = strtotime($matches[1].':'.$matches[2].':'.$matches[3]);
167
		//echo 'timestamp : '.$timestamp.' - now : '.time()."\n";
168
		/*
169
		if (time() + 3900 < $timestamp) $timestamp -= 86400;
170
		elseif (time() - 82500 > $timestamp) $timestamp += 86400;
171
		*/
172
	    } elseif ($matches[4] == 'z' || $matches[4] == '/') {
173
		// This work or not ?
174
		$timestamp = strtotime(date('Ym').$matches[1].' '.$matches[2].':'.$matches[3]);
175
	    }
176
	    $body_parse = substr($body_parse,7);
177
	    $result['timestamp'] = $timestamp;
178
	    //echo date('Ymd H:i:s',$timestamp);
179
	}
180
	//if (strlen($body_parse) > 19) {
181
	    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)) {
182
	    $find = true;
0 ignored issues
show
Unused Code introduced by
$find 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...
183
		// 4658.70N/00707.78Ez
184
		//print_r(str_split($body_parse));
185
		
186
		//$latlon = $matches[0];
187
		$sind = strtoupper($matches[3]);
188
		$wind = strtoupper($matches[7]);
189
		$lat_deg = $matches[1];
190
		$lat_min = $matches[2];
191
		$lon_deg = $matches[5];
192
		$lon_min = $matches[6];
193
	    
194
		//$symbol_table = $matches[4];
195
		$lat = intval($lat_deg);
196
		$lon = intval($lon_deg);
197
		if ($lat > 89 || $lon > 179) return false;
198
	    
199
	    /*
200
	    $tmp_5b = str_replace('.','',$lat_min);
201
	    if (preg_match('/^([0-9]{0,4})( {0,4})$/',$tmp_5b,$matches)) {
202
	        print_r($matches);
203
	    }
204
	    */
205
		$latitude = $lat + floatval($lat_min)/60;
206
		$longitude = $lon + floatval($lon_min)/60;
207
		if ($sind == 'S') $latitude = 0-$latitude;
208
		if ($wind == 'W') $longitude = 0-$longitude;
209
		$result['latitude'] = $latitude;
210
		$result['longitude'] = $longitude;
211
		$body_parse = substr($body_parse,18);
212
		$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...
213
	    }
214
	    if ($body_len > 0) {
215
		/*
216
		if (!isset($result['timestamp']) && !isset($result['latitude'])) {
217
			$body_split = str_split($body);
218
			$symbol_code = $body_split[0];
219
			$body_parse = substr($body,1);
220
			$body_parse_len = strlen($body_parse);
221
		} else { 
222
		*/
223
		/*
224
		if ($find === false) {
225
			$body_split = str_split($body);
226
			$symbol_code = $body_split[0];
227
			$body_parse = substr($body,1);
228
			$body_parse_len = strlen($body_parse);
229
		} else { 
230
		*/
231
			$body_split = str_split($body_parse);
232
			$symbol_code = $body_split[0];
233
			$body_parse = substr($body_parse,1);
234
			$body_parse_len = strlen($body_parse);
235
		//}
236
		//echo $body_parse;
237
		$result['symbol_code'] = $symbol_code;
238
		if (isset($this->symbols[$symbol_code])) $result['symbol'] = $this->symbols[$symbol_code];
239
		if ($symbol_code != '_') {
240
		    //$body_parse = substr($body_parse,1);
241
		    //$body_parse = trim($body_parse);
242
		    //$body_parse_len = strlen($body_parse);
243
		    if ($body_parse_len >= 7) {
244
			
245
		        if (preg_match('/^([0-9\\. ]{3})\\/([0-9\\. ]{3})/',$body_parse)) {
246
		    	    $course = substr($body_parse,0,3);
247
		    	    $tmp_s = intval($course);
248
		    	    if ($tmp_s >= 1 && $tmp_s <= 360) $result['heading'] = intval($course);
249
		    	    $speed = substr($body_parse,4,3);
250
		    	    $result['speed'] = round($speed*1.852);
251
		    	    $body_parse = substr($body_parse,7);
252
		        }
253
		        // Check PHGR, PHG, RNG
254
		    } 
255
		    /*
256
		    else if ($body_parse_len > 0) {
257
			$rest = $body_parse;
258
		    }
259
		    */
260
		    if (strlen($body_parse) > 0) {
261
		        if (preg_match('/\\/A=(-[0-9]{5}|[0-9]{6})/',$body_parse,$matches)) {
262
		            $altitude = intval($matches[1]);
263
		            //$result['altitude'] = round($altitude*0.3048);
264
		            $result['altitude'] = $altitude;
265
		            $body_parse = trim(substr($body_parse,strlen($matches[0])));
266
		        }
267
		    }
268
		    
269
		    // Telemetry
270
		    /*
271
		    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)) {
272
		        // Nothing yet...
273
		    }
274
		    */
275
		    // DAO
276
		    if (preg_match('/^!([0-9A-Z]{3})/',$body_parse,$matches)) {
277
			    $dao = $matches[1];
278
			    if (preg_match('/^([A-Z])([0-9]{2})/',$dao)) {
279
				$dao_split = str_split($dao);
280
			        $lat_off = (($dao_split[1])-48.0)*0.001/60.0;
281
			        $lon_off = (($dao_split[2])-48.0)*0.001/60.0;
282
			    
283
				if ($result['latitude'] < 0) $result['latitude'] -= $lat_off;
284
				else $result['latitude'] += $lat_off;
285
				if ($result['longitude'] < 0) $result['longitude'] -= $lon_off;
286
				else $result['longitude'] += $lon_off;
287
			    }
288
		            $body_parse = substr($body_parse,6);
289
		    }
290
		    
291
		    // OGN comment
292
		   // echo "Before OGN : ".$body_parse."\n";
293
		    if (preg_match('/^id([0-9A-F]{8}) ([+-])([0-9]{3,4})fpm ([+-])([0-9.]{3,4})rot (.*)$/',$body_parse,$matches)) {
294
			$id = $matches[1];
295
			//$mode = substr($id,0,2);
296
			$address = substr($id,2);
297
			//print_r($matches);
298
			$addressType = (intval(substr($id,0,2),16))&3;
299
			if ($addressType == 0) $result['addresstype'] = "RANDOM";
300
			elseif ($addressType == 1) $result['addresstype'] = "ICAO";
301
			elseif ($addressType == 2) $result['addresstype'] = "FLARM";
302
			elseif ($addressType == 3) $result['addresstype'] = "OGN";
303
			$aircraftType = $this->urshift(((intval(substr($id,0,2),16)) & 0b1111100),2);
304
			$result['aircrafttype_code'] = $aircraftType;
305
			if ($aircraftType == 0) $result['aircrafttype'] = "UNKNOWN";
306
			elseif ($aircraftType == 1) $result['aircrafttype'] = "GLIDER";
307
			elseif ($aircraftType == 2) $result['aircrafttype'] = "TOW_PLANE";
308
			elseif ($aircraftType == 3) $result['aircrafttype'] = "HELICOPTER_ROTORCRAFT";
309
			elseif ($aircraftType == 4) $result['aircrafttype'] = "PARACHUTE";
310
			elseif ($aircraftType == 5) $result['aircrafttype'] = "DROP_PLANE";
311
			elseif ($aircraftType == 6) $result['aircrafttype'] = "HANG_GLIDER";
312
			elseif ($aircraftType == 7) $result['aircrafttype'] = "PARA_GLIDER";
313
			elseif ($aircraftType == 8) $result['aircrafttype'] = "POWERED_AIRCRAFT";
314
			elseif ($aircraftType == 9) $result['aircrafttype'] = "JET_AIRCRAFT";
315
			elseif ($aircraftType == 10) $result['aircrafttype'] = "UFO";
316
			elseif ($aircraftType == 11) $result['aircrafttype'] = "BALLOON";
317
			elseif ($aircraftType == 12) $result['aircrafttype'] = "AIRSHIP";
318
			elseif ($aircraftType == 13) $result['aircrafttype'] = "UAV";
319
			elseif ($aircraftType == 15) $result['aircrafttype'] = "STATIC_OBJECT";
320
			$stealth = (intval(substr($id,0,2), 16) & 0b10000000) != 0;
321
			$result['stealth'] = $stealth;
322
			$result['address'] = $address;
323
		    }
324
		    
325
		    //Comment
326
		    $result['comment'] = trim($body_parse);
327
		} else {
328
		    // parse weather
329
		    //$body_parse = substr($body_parse,1);
330
		    //$body_parse_len = strlen($body_parse);
331
332
		    if (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) {
333
			    $result['wind_dir'] = intval($matches[1]);
334
			    $result['wind_speed'] = round(intval($matches[2])*1.60934,1);
335
			    $result['wind_gust'] = round(intval($matches[3])*1.60934,1);
336
			    $result['temp'] = round(5/9*(($matches[4])-32),1);
337
		    	    $body_parse = substr($body_parse,strlen($matches[0])+1);
338
		    } elseif (preg_match('/^_{0,1}c([0-9 \\.\\-]{3})s([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) {
339
			$result['wind_dir'] = intval($matches[1]);
340
			$result['wind_speed'] = round($matches[2]*1.60934,1);
341
			$result['wind_gust'] = round($matches[3]*1.60934,1);
342
			$result['temp'] = round(5/9*(($matches[4])-32),1);
343
		        $body_parse = substr($body_parse,strlen($matches[0])+1);
344
		    } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) {
345
			$result['wind_dir'] = intval($matches[1]);
346
			$result['wind_speed'] = round($matches[2]*1.60934,1);
347
			$result['wind_gust'] = round($matches[3]*1.60934,1);
348
		        $body_parse = substr($body_parse,strlen($matches[0])+1);
349
		    } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)/',$body_parse,$matches)) {
350
			$result['wind_dir'] = intval($matches[1]);
351
			$result['wind_speed'] = round($matches[2]*1.60934,1);
352
			$result['wind_gust'] = round($matches[3]*1.60934,1);
353
		        $body_parse = substr($body_parse,strlen($matches[0])+1);
354
		    }
355
		    if (!isset($result['temp']) && strlen($body_parse) > 0 && preg_match('/^g([0-9]+)t(-?[0-9 \\.]{1,3})/',$body_parse,$matches)) {
356
			$result['temp'] = round(5/9*(($matches[1])-32),1);
357
		    }
358
		}
359
	    }
360
	//}
361
	if (isset($result['latitude'])) $result['latitude'] = round($result['latitude'],4);
362
	if (isset($result['longitude'])) $result['longitude'] = round($result['longitude'],4);
363
	//print_r($result);
364
	return $result;
365
    }
366
}
367
/*
368
$aprs = new aprs();
369
print_r($aprs->parse('ICA400EE9>APRS,qAS,UKHUN:/083216h5138.51N\00121.61W^279/050/A=003949 !W25! id21400EE9 -8988fpm -10.2rot 10.8dB 0e -6.9kHz gps5x7'));
370
  */
371
?>