Completed
Push — master ( 0a922a...0f8d98 )
by Yannick
29:36
created

AIS::mk_ais_lon()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
Copyright 2014 Aaron Gong Hsien-Joen <[email protected]>
4
5
Licensed under the Apache License, Version 2.0 (the "License");
6
you may not use this file except in compliance with the License.
7
You may obtain a copy of the License at
8
9
    http://www.apache.org/licenses/LICENSE-2.0
10
11
Unless required by applicable law or agreed to in writing, software
12
distributed under the License is distributed on an "AS IS" BASIS,
13
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
See the License for the specific language governing permissions and
15
limitations under the License.
16
*/
17
/*
18
Modified in 2017 by Ycarus <[email protected]>
19
Original version come from https://github.com/ais-one/phpais
20
*/
21
class AIS {
22
/* AIS Decoding
23
- Receive and get ITU payload
24
- Organises the binary bits of the Payload into 6-bit strings,
25
- Converts the 6-bit strings into their representative "valid characters" – see IEC 61162-1, table 7,
26
- Assembles the valid characters into an encapsulation string, and
27
- Transfers the encapsulation string using the VDM sentence formatter.
28
*/
29
30
	private function make_latf($temp) { // unsigned long 
31
		$flat = 0.0; // float
0 ignored issues
show
Unused Code introduced by
$flat 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...
32
		$temp = $temp & 0x07FFFFFF;
33
		if ($temp & 0x04000000) {
34
			$temp = $temp ^ 0x07FFFFFF;
35
			$temp += 1;
36
			$flat = (float)($temp / (60.0 * 10000.0));
37
			$flat *= -1.0;
38
		} else $flat = (float)($temp / (60.0 * 10000.0));
39
		return $flat; // float
40
	}
41
42
	private function make_lonf($temp) { // unsigned long
43
		$flon = 0.0; // float
0 ignored issues
show
Unused Code introduced by
$flon 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...
44
		$temp = $temp & 0x0FFFFFFF;
45
		if ($temp & 0x08000000) {
46
			$temp = $temp ^ 0x0FFFFFFF;
47
			$temp += 1;
48
			$flon = (float)($temp / (60.0 * 10000.0));
49
			$flon *= -1.0;
50
		} else $flon = (float)($temp / (60.0 * 10000.0));
51
		return $flon;
52
	}
53
54
	private function ascii_2_dec($chr) {
55
		$dec=ord($chr);//get decimal ascii code
56
		$hex=dechex($dec);//convert decimal to hex
0 ignored issues
show
Unused Code introduced by
$hex 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...
57
		return ($dec);
58
	}
59
	
60
    /*
61
    $ais_map64 = array(
62
       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', // 48
63
       ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C',
64
       'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
65
       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', // 87
66
       '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', // 96
67
       'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
68
       't', 'u', 'v', 'w' // 119
69
    ); // char 64
70
    */
71
	private function asciidec_2_8bit($ascii) {
72
		//only process in the following range: 48-87, 96-119
73
		if ($ascii < 48) { }
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...
74
		else {
75
			if($ascii>119) { }
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...
76
			else {
77
				if ($ascii>87 && $ascii<96) ;
78
				else {
79
					$ascii=$ascii+40;
80
					if ($ascii>128){
81
						$ascii=$ascii+32;
82
					} else {
83
						$ascii=$ascii+40;
84
					}
85
				}
86
			}
87
		}
88
		return ($ascii);
89
	}
90
91
	private function dec_2_6bit($dec) {
92
		$bin=decbin($dec);
93
		return(substr($bin, -6)); 
94
	}
95
96
	private function binchar($_str, $_start, $_size) {
97
		//  ' ' --- '?', // 0x20 - 0x3F
98
		//  '@' --- '_', // 0x40 - 0x5F
99
		$ais_chars = array(
100
		    '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
101
		    'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
102
		    'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']',
103
		    '^', '_', ' ', '!', '\"', '#', '$', '%', '&', '\'',
104
		    '(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
105
		    '2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
106
		    '<', '=', '>', '?'
107
		);
108
		// "
109
		$rv = '';
110
		if ($_size % 6 == 0) {
111
			$len = $_size / 6;
112
			for ($i=0; $i<$len; $i++) {
113
				$offset = $i * 6;
114
				$rv .= $ais_chars[ bindec(substr($_str,$_start + $offset,6)) ];
115
			}
116
		}
117
		return $rv;
118
	}
119
120
	// function for decoding the AIS Message ITU Payload
121
	private function decode_ais($_aisdata) {
122
		$ro = new stdClass(); // return object
123
		$ro->cls = 0; // AIS class undefined, also indicate unparsed msg
124
		$ro->name = '';
125
		$ro->status = '';
126
		$ro->callsign = '';
127
		$ro->imo = '';
128
		$ro->typeid = '';
129
		$ro->type = '';
130
		$ro->sog = -1.0;
131
		$ro->cog = 0.0;
132
		$ro->lon = 0.0;
133
		$ro->lat = 0.0;
134
		$ro->heading = '';
135
		$ro->destination = '';
136
		$ro->eta_month = '';
137
		$ro->eta_day = '';
138
		$ro->eta_hour = '';
139
		$ro->eta_minute = '';
140
		$ro->ts = time();
141
		$ro->id = bindec(substr($_aisdata,0,6));
142
		$ro->mmsi = bindec(substr($_aisdata,8,30));
143
		if ($ro->id >= 1 && $ro->id <= 3) {
144
			$ro->cog = bindec(substr($_aisdata,116,12))/10;
145
			$ro->sog = bindec(substr($_aisdata,50,10))/10;
146
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,61,28)));
147
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,89,27)));
148
			$ro->cls = 1; // class A
149
		} else if ($ro->id == 4) {
150
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,79,28)));
151
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,107,27)));
152
			$ro->cls = 1; // class A
153
		} else if ($ro->id == 5) {
154
			$ro->imo = bindec(substr($_aisdata,40,30));
155
			$ro->callsign = $this->binchar($_aisdata,70,42);
156
			$ro->name = $this->binchar($_aisdata,112,120);
157
			$ro->typeid = bindec(substr($_aisdata,232,8));
158
			$ro->type = $this->getShipType($ro->typeid);
159
			//$ro->to_bow = bindec(substr($_aisdata,240,9));
160
			//$ro->to_stern = bindec(substr($_aisdata,249,9));
161
			//$ro->to_port = bindec(substr($_aisdata,258,6));
162
			//$ro->to_starboard = bindec(substr($_aisdata,264,6));
163
			$ro->eta_month = bindec(substr($_aisdata,274,4));
164
			$ro->eta_day = bindec(substr($_aisdata,278,5));
165
			$ro->eta_hour = bindec(substr($_aisdata,283,5));
166
			$ro->eta_minute = bindec(substr($_aisdata,288,6));
167
			//$ro->draught = bindec(substr($_aisdata,294,8));
168
			$ro->destination = $this->binchar($_aisdata,302,120);
169
			$ro->cls = 1; // class A
170
		} else if ($ro->id == 9) {
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...
171
			// Search and Rescue aircraft position report
172
		} else if ($ro->id == 18) {
173
			$ro->cog = bindec(substr($_aisdata,112,12))/10;
174
			$ro->sog = bindec(substr($_aisdata,46,10))/10;
175
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,57,28)));
176
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,85,27)));
177
			$ro->heading = bindec(substr($_aisdata,124,9));
178
			if ($ro->heading == 511) $ro->heading = '';
179
			$ro->cls = 2; // class B
180
		} else if ($ro->id == 19) {
181
			$ro->cog = bindec(substr($_aisdata,112,12))/10;
182
			$ro->sog = bindec(substr($_aisdata,46,10))/10;
183
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,61,28)));
184
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,89,27)));
185
			$ro->name = $this->binchar($_aisdata,143,120);
186
			$ro->cls = 2; // class B
187
			$ro->heading = bindec(substr($_aisdata,124,9));
188
			if ($ro->heading == 511) $ro->heading = '';
189
			$ro->typeid = bindec(substr($_aisdata,263,8));
190
			$ro->type = $this->getShipType($ro->typeid);
191
			//$ro->to_bow = bindec(substr($_aisdata,271,9));
192
			//$ro->to_stern = bindec(substr($_aisdata,280,9));
193
			//$ro->to_port = bindec(substr($_aisdata,289,6));
194
			//$ro->to_starboard = bindec(substr($_aisdata,295,6));
195
		} else if ($ro->id == 21) {
196
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,164,28)));
197
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,192,27)));
198
			$ro->name = $this->binchar($_aisdata,43,120);
199
			//$ro->to_bow = bindec(substr($_aisdata,219,9));
200
			//$ro->to_stern = bindec(substr($_aisdata,228,9));
201
			//$ro->to_port = bindec(substr($_aisdata,237,6));
202
			//$ro->to_starboard = bindec(substr($_aisdata,243,6));
203
			$ro->cls = 2; // class B
204
		} else if ($ro->id == 24) {
205
			$pn = bindec(substr($_aisdata,38,2));
206
			if ($pn == 0) {
207
				$ro->name = $this->binchar($_aisdata,40,120);
208
			}
209
			$ro->typeid = bindec(substr($_aisdata,40,8));
210
			$ro->type = $this->getShipType($ro->typeid);
211
			$ro->callsign = $this->binchar($_aisdata,90,42);
212
			//$ro->to_bow = bindec(substr($_aisdata,132,9));
213
			//$ro->to_stern = bindec(substr($_aisdata,141,9));
214
			//$ro->to_port = bindec(substr($_aisdata,150,6));
215
			//$ro->to_starboard = bindec(substr($_aisdata,156,6));
216
			$ro->cls = 2; // class B
217
		} else if ($ro->id == 27) {
218
			$ro->cog = bindec(substr($_aisdata,85,9));
219
			if ($ro->cog == 511) $ro->cog = 0.0;
220
			$ro->sog = bindec(substr($_aisdata,79,6));
221
			if ($ro->sog == 63) $ro->sog = 0.0;
222
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,44,18))*10);
223
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,62,17))*10);
224
			$ro->cls = 1; // class A
225
		
226
		}
227
		$ro->statusid = bindec(substr($_aisdata,38,4));
228
		$ro->status = $this->getStatus($ro->statusid);
229
		//var_dump($ro); // dump results here for demo purpose
230
		return $ro;
231
	}
232
233
	public function getStatus($statusid) {
234
		if ($statusid == 0) {
235
			return 'under way using engine';
236
		} elseif ($statusid == 1) {
237
			return 'at anchor';
238
		} elseif ($statusid == 2) {
239
			return 'not under command';
240
		} elseif ($statusid == 3) {
241
			return 'restricted maneuverability';
242
		} elseif ($statusid == 4) {
243
			return 'constrained by her draught';
244
		} elseif ($statusid == 5) {
245
			return 'moored';
246
		} elseif ($statusid == 6) {
247
			return 'aground';
248
		} elseif ($statusid == 7) {
249
			return 'engaged in fishing';
250
		} elseif ($statusid == 8) {
251
			return 'under way sailing';
252
		} elseif ($statusid == 9) {
253
			return 'reserved for future amendment of navigational status for ships carrying DG, HS, or MP, or IMO hazard or pollutant category C, high speed craft (HSC)';
254
		} elseif ($statusid == 10) {
255
			return 'reserved for future amendment of navigational status for ships carrying dangerous goods (DG), harmful substances (HS) or marine pollutants (MP), or IMO hazard or pollutant category A, wing in ground (WIG)';
256
		} elseif ($statusid == 11) {
257
			return 'power-driven vessel towing astern (regional use)';
258
		} elseif ($statusid == 12) {
259
			return 'power-driven vessel pushing ahead or towing alongside (regional use)';
260
		} elseif ($statusid == 13) {
261
			return 'reserved for future use';
262
		} elseif ($statusid == 14) {
263
			return 'AIS-SART (active), MOB-AIS, EPIRB-AIS';
264
		} elseif ($statusid == 15) {
265
			return 'undefined = default (also used by AIS-SART, MOB-AIS and EPIRB-AIS under test)';
266
		}
267
	}
268
	
269
	public function getShipType($code) {
270
		if ($code == 0) return 'Not available (default)';
271
		elseif ($code >= 1 && $code <= 19) return 'Reserved for future use';
272
		elseif ($code == 20) return 'Wing in ground (WIG), all ships of this type';
273
		elseif ($code == 21) return 'Wing in ground (WIG), Hazardous category A';
274
		elseif ($code == 22) return 'Wing in ground (WIG), Hazardous category B';
275
		elseif ($code == 23) return 'Wing in ground (WIG), Hazardous category C';
276
		elseif ($code == 24) return 'Wing in ground (WIG), Hazardous category D';
277
		elseif ($code == 25) return 'Wing in ground (WIG), Reserved for future use';
278
		elseif ($code == 26) return 'Wing in ground (WIG), Reserved for future use';
279
		elseif ($code == 27) return 'Wing in ground (WIG), Reserved for future use';
280
		elseif ($code == 28) return 'Wing in ground (WIG), Reserved for future use';
281
		elseif ($code == 29) return 'Wing in ground (WIG), Reserved for future use';
282
		elseif ($code == 30) return 'Fishing';
283
		elseif ($code == 31) return 'Towing';
284
		elseif ($code == 32) return 'Towing: length exceeds 200m or breadth exceeds 25m';
285
		elseif ($code == 33) return 'Dredging or underwater ops';
286
		elseif ($code == 34) return 'Diving ops';
287
		elseif ($code == 35) return 'Military ops';
288
		elseif ($code == 36) return 'Sailing';
289
		elseif ($code == 37) return 'Pleasure Craft';
290
		elseif ($code == 38) return 'Reserved';
291
		elseif ($code == 39) return 'Reserved';
292
		elseif ($code == 40) return 'High speed craft (HSC), all ships of this type';
293
		elseif ($code == 41) return 'High speed craft (HSC), Hazardous category A';
294
		elseif ($code == 42) return 'High speed craft (HSC), Hazardous category B';
295
		elseif ($code == 43) return 'High speed craft (HSC), Hazardous category C';
296
		elseif ($code == 44) return 'High speed craft (HSC), Hazardous category D';
297
		elseif ($code == 45) return 'High speed craft (HSC), Reserved for future use';
298
		elseif ($code == 46) return 'High speed craft (HSC), Reserved for future use';
299
		elseif ($code == 47) return 'High speed craft (HSC), Reserved for future use';
300
		elseif ($code == 48) return 'High speed craft (HSC), Reserved for future use';
301
		elseif ($code == 49) return 'High speed craft (HSC), No additional information';
302
		elseif ($code == 50) return 'Pilot Vessel';
303
		elseif ($code == 51) return 'Search and Rescue vessel';
304
		elseif ($code == 52) return 'Tug';
305
		elseif ($code == 53) return 'Port Tender';
306
		elseif ($code == 54) return 'Anti-pollution equipment';
307
		elseif ($code == 55) return 'Law Enforcement';
308
		elseif ($code == 56) return 'Spare - Local Vessel';
309
		elseif ($code == 57) return 'Spare - Local Vessel';
310
		elseif ($code == 58) return 'Medical Transport';
311
		elseif ($code == 59) return 'Noncombatant ship according to RR Resolution No. 18';
312
		elseif ($code == 60) return 'Passenger, all ships of this type';
313
		elseif ($code == 61) return 'Passenger, Hazardous category A';
314
		elseif ($code == 62) return 'Passenger, Hazardous category B';
315
		elseif ($code == 63) return 'Passenger, Hazardous category C';
316
		elseif ($code == 64) return 'Passenger, Hazardous category D';
317
		elseif ($code == 65) return 'Passenger, Reserved for future use';
318
		elseif ($code == 66) return 'Passenger, Reserved for future use';
319
		elseif ($code == 67) return 'Passenger, Reserved for future use';
320
		elseif ($code == 68) return 'Passenger, Reserved for future use';
321
		elseif ($code == 69) return 'Passenger, No additional information';
322
		elseif ($code == 70) return 'Cargo, all ships of this type';
323
		elseif ($code == 71) return 'Cargo, Hazardous category A';
324
		elseif ($code == 72) return 'Cargo, Hazardous category B';
325
		elseif ($code == 73) return 'Cargo, Hazardous category C';
326
		elseif ($code == 74) return 'Cargo, Hazardous category D';
327
		elseif ($code == 75) return 'Cargo, Reserved for future use';
328
		elseif ($code == 76) return 'Cargo, Reserved for future use';
329
		elseif ($code == 77) return 'Cargo, Reserved for future use';
330
		elseif ($code == 78) return 'Cargo, Reserved for future use';
331
		elseif ($code == 79) return 'Cargo, No additional information';
332
		elseif ($code == 80) return 'Tanker, all ships of this type';
333
		elseif ($code == 81) return 'Tanker, Hazardous category A';
334
		elseif ($code == 82) return 'Tanker, Hazardous category B';
335
		elseif ($code == 83) return 'Tanker, Hazardous category C';
336
		elseif ($code == 84) return 'Tanker, Hazardous category D';
337
		elseif ($code == 85) return 'Tanker, Reserved for future use';
338
		elseif ($code == 86) return 'Tanker, Reserved for future use';
339
		elseif ($code == 87) return 'Tanker, Reserved for future use';
340
		elseif ($code == 88) return 'Tanker, Reserved for future use';
341
		elseif ($code == 89) return 'Tanker, No additional information';
342
		elseif ($code == 90) return 'Other Type, all ships of this type';
343
		elseif ($code == 91) return 'Other Type, Hazardous category A';
344
		elseif ($code == 92) return 'Other Type, Hazardous category B';
345
		elseif ($code == 93) return 'Other Type, Hazardous category C';
346
		elseif ($code == 94) return 'Other Type, Hazardous category D';
347
		elseif ($code == 95) return 'Other Type, Reserved for future use';
348
		elseif ($code == 96) return 'Other Type, Reserved for future use';
349
		elseif ($code == 97) return 'Other Type, Reserved for future use';
350
		elseif ($code == 98) return 'Other Type, Reserved for future use';
351
		elseif ($code == 99) return 'Other Type, no additional information';
352
	}
353
354
	public function process_ais_itu($_itu, $_len, $_filler, $aux /*, $ais_ch*/) {
0 ignored issues
show
Unused Code introduced by
The parameter $_len 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 $_filler 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 $aux 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...
355
		global $port; // tcpip port...
356
		
357
		static $debug_counter = 0;
358
		$aisdata168='';//six bit array of ascii characters
359
		$ais_nmea_array = str_split($_itu); // convert to an array
360
		foreach ($ais_nmea_array as $value) {
361
			$dec = $this->ascii_2_dec($value);
362
			$bit8 = $this->asciidec_2_8bit($dec);
363
			$bit6 = $this->dec_2_6bit($bit8);
364
			//echo $value ."-" .$bit6 ."";
365
			$aisdata168 .=$bit6;
366
		}
367
		//echo $aisdata168 . "<br/>";
368
		//return $this->decode_ais($aisdata168, $aux);
369
		return $this->decode_ais($aisdata168);
370
	}
371
372
	// char* - AIS \r terminated string
373
	// TCP based streams which send messages in full can use this instead of calling process_ais_buf
374
	public function process_ais_raw($rawdata, $aux = '') { // return int
375
		static $num_seq; // 1 to 9
376
		static $seq; // 1 to 9
377
		static $pseq; // previous seq
378
		static $msg_sid = -1; // 0 to 9, indicate -1 at start state of device, do not process messages
379
		static $cmsg_sid; // current msg_sid
380
		static $itu; // buffer for ITU message
381
382
		$filler = 0; // fill bits (int)
383
		$chksum = 0;
384
		// raw data without the \n
385
		// calculate checksum after ! till *
386
		// assume 1st ! is valid
387
		// find * ensure that it is at correct position
388
		$end = strrpos ( $rawdata , '*' );
389
		if ($end === FALSE) return -1; // check for NULLS!!!
390
		$cs = substr( $rawdata, $end + 1 );
391
		if ( strlen($cs) != 2 ) return -1; // correct cs length
392
		$dcs = (int)hexdec( $cs );
393
		for ( $alias=1; $alias<$end; $alias++) $chksum ^= ord( $rawdata[$alias] ); // perform XOR for NMEA checksum
394
		if ( $chksum == $dcs ) { // NMEA checksum pass
395
			$pcs = explode(',', $rawdata);
396
			// !AI??? identifier
397
			if (!isset($pcs[1])) {
398
				echo "ERROR,INVALID_DATA ".time()." $rawdata\n";
399
				return -1;
400
			}
401
			$num_seq = (int)$pcs[1]; // number of sequences
402
			$seq = (int)$pcs[2]; // get sequence
403
			// get msg sequence id
404
			if ($pcs[3] == '') $msg_sid = -1; // non-multipart message, set to -1
405
			else $msg_sid = (int)$pcs[3]; // multipart message
406
			$ais_ch = $pcs[4]; // get AIS channel
0 ignored issues
show
Unused Code introduced by
$ais_ch 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...
407
			// message sequence checking
408
			if ($num_seq < 1 || $num_seq > 9) {
409
				echo "ERROR,INVALID_NUMBER_OF_SEQUENCES ".time()." $rawdata\n";
410
				return -1;
411
			} else if ($seq < 1 || $seq > 9) { // invalid sequences number
412
				echo "ERROR,INVALID_SEQUENCES_NUMBER ".time()." $rawdata\n";
413
				return -1;
414
			} else if ($seq > $num_seq) {
415
				echo "ERROR,INVALID_SEQUENCE_NUMBER_OR_INVALID_NUMBER_OF_SEQUENCES ".time()." $rawdata\n";
416
				return -1;
417
			} else { // sequencing ok, handle single/multi-part messaging
418
				if ($seq == 1) { // always init to 0 at first sequence
419
					$filler = 0; // ?
420
					$itu = ""; // init message length
421
					$pseq = 0; // note previous sequence number
422
					$cmsg_sid = $msg_sid; // note msg_sid
423
				}
424
				if ($num_seq > 1) { // for multipart messages
425
					if ($cmsg_sid != $msg_sid // different msg_sid
426
					    || $msg_sid == -1 // invalid initial msg_sid
427
					    || ($seq - $pseq) != 1 // not insequence
428
					) {  // invalid for multipart message
429
						$msg_sid = -1;
430
						$cmsg_sid = -1;
431
						echo "ERROR,INVALID_MULTIPART_MESSAGE ".time()." $rawdata\n";
432
						return -1;
433
					} else {
434
						$pseq++;
435
					}
436
				}
437
				$itu = $itu.$pcs[5]; // get itu message
438
				$filler += (int)$pcs[6][0]; // get filler
439
				if ($num_seq == 1 // valid single message
440
				    || $num_seq == $pseq // valid multi-part message
441
				) {
442
					if ($num_seq != 1) { // test
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...
443
						//echo $rawdata;
444
					}
445
					return $this->process_ais_itu($itu, strlen($itu), $filler, $aux /*, $ais_ch*/);
446
				}
447
			} // end process raw AIS string (checksum passed)
448
		}
449
		return -1;
450
	}
451
452
	// incoming data from serial or IP comms
453
	public function process_ais_buf($ibuf) {
454
		static $cbuf = "";
455
		$cbuf = $cbuf.$ibuf;
456
		$last_pos = 0;
457
		$result = new stdClass();
458
		while ( ($start = strpos($cbuf,"VDM",$last_pos)) !== FALSE) {
459
		//while ( ($start = strpos($cbuf,"!AI",$last_pos)) !== FALSE) {
460
			//DEBUG echo $cbuf;
461
			if ( ($end = strpos($cbuf,"\r\n", $start)) !== FALSE) { //TBD need to trim?
462
				$tst = substr($cbuf, $start - 3, ($end - $start + 3));
463
				//DEBUG echo "[$start $end $tst]\n";
464
				$result = $this->process_ais_raw( $tst, "" );
465
				$last_pos = $end + 1;
466
			} else break;
467
		}
468
		if ($last_pos > 0) $cbuf = substr($cbuf, $last_pos); // move...
469
		if (strlen($cbuf) > 1024) $cbuf = ""; // prevent overflow simple mode...
470
		return $result;
471
	}
472
473
	// incoming data from serial or IP comms
474
	public function process_ais_line($cbuf) {
475
		$result = new stdClass();
0 ignored issues
show
Unused Code introduced by
$result 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...
476
		$start = strpos($cbuf,"VDM");
477
		$tst = substr($cbuf, $start - 3);
478
		$result = $this->process_ais_raw( $tst, "" );
479
		return $result;
480
	}
481
482
	/* AIS Encoding
483
	*/
484
	private function mk_ais_lat( $lat ) {
485
		//$lat = 1.2569;
486
		if ($lat<0.0) {
487
			$lat = -$lat;
488
			$neg=true;
489
		} else $neg=false;
490
		$latd = 0x00000000;
0 ignored issues
show
Unused Code introduced by
$latd 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...
491
		$latd = intval ($lat * 600000.0);
492
		if ($neg==true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

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

Loading history...
493
			$latd = ~$latd;
494
			$latd+=1;
495
			$latd &= 0x07FFFFFF;
496
		}
497
		return $latd;
498
	}
499
500
	private function mk_ais_lon( $lon ) {
501
		//$lon = 103.851;
502
		if ($lon<0.0) {
503
			$lon = -$lon;
504
			$neg=true;
505
		} else $neg=false;
506
		$lond = 0x00000000;
0 ignored issues
show
Unused Code introduced by
$lond 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...
507
		$lond = intval ($lon * 600000.0);
508
		if ($neg==true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

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

Loading history...
509
			$lond = ~$lond;
510
			$lond+=1;
511
			$lond &= 0x0FFFFFFF;
512
		}
513
		return $lond;
514
	}
515
516
	private function char2bin($name, $max_len) {
517
		$len = strlen($name);
518
		if ($len > $max_len) $name = substr($name,0,$max_len);
519
		if ($len < $max_len) $pad = str_repeat('0', ($max_len - $len) * 6);
520
		else $pad = '';
521
		$rv = '';
522
		$ais_chars = array(
523
		    '@'=>0, 'A'=>1, 'B'=>2, 'C'=>3, 'D'=>4, 'E'=>5, 'F'=>6, 'G'=>7, 'H'=>8, 'I'=>9,
524
		    'J'=>10, 'K'=>11, 'L'=>12, 'M'=>13, 'N'=>14, 'O'=>15, 'P'=>16, 'Q'=>17, 'R'=>18, 'S'=>19,
525
		    'T'=>20, 'U'=>21, 'V'=>22, 'W'=>23, 'X'=>24, 'Y'=>25, 'Z'=>26, '['=>27, '\\'=>28, ']'=>29,
526
		    '^'=>30, '_'=>31, ' '=>32, '!'=>33, '\"'=>34, '#'=>35, '$'=>36, '%'=>37, '&'=>38, '\''=>39,
527
		    '('=>40, ')'=>41, '*'=>42, '+'=>43, ','=>44, '-'=>45, '.'=>46, '/'=>47, '0'=>48, '1'=>49,
528
		    '2'=>50, '3'=>51, '4'=>52, '5'=>53, '6'=>54, '7'=>55, '8'=>56, '9'=>57, ':'=>58, ';'=>59,
529
		    '<'=>60, '='=>61, '>'=>62, '?'=>63
530
		);
531
		// "
532
		$_a = str_split($name);
533
		if ($_a) foreach ($_a as $_1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $_a of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
534
			if (isset($ais_chars[$_1])) $dec = $ais_chars[$_1];
535
			else $dec = 0;
536
			$bin = str_pad(decbin( $dec ), 6, '0', STR_PAD_LEFT);
537
			$rv .= $bin;
538
			//echo "$_1 $dec ($bin)<br/>";
539
		}
540
		return $rv.$pad;
541
	}
542
543
	private function mk_ais($_enc, $_part=1,$_total=1,$_seq='',$_ch='A') {
544
		$len_bit = strlen($_enc);
545
		$rem6 = $len_bit % 6;
546
		$pad6_len = 0;
547
		if ($rem6) $pad6_len = 6 - $rem6;
548
		//echo  $pad6_len.'<br>';
549
		$_enc .= str_repeat("0", $pad6_len); // pad the text...
550
		$len_enc = strlen($_enc) / 6;
551
		//echo $_enc.' '.$len_enc.'<br/>';
552
		$itu = '';
553
		for ($i=0; $i<$len_enc; $i++) {
554
			$offset = $i * 6;
555
			$dec = bindec(substr($_enc,$offset,6));
556
			if ($dec < 40) $dec += 48;
557
			else $dec += 56;
558
			//echo chr($dec)." $dec<br/>";
559
			$itu .= chr($dec);
560
		}
561
		// add checksum
562
		$chksum = 0;
563
		$itu = "AIVDM,$_part,$_total,$_seq,$_ch,".$itu.",0";
564
		$len_itu = strlen($itu);
565
		for ($i=0; $i<$len_itu; $i++) {
566
			$chksum ^= ord( $itu[$i] );
567
		}
568
		$hex_arr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
569
		$lsb = $chksum & 0x0F;
570
		if ($lsb >=0 && $lsb <= 15 ) $lsbc = $hex_arr[$lsb];
571
		else $lsbc = '0';
572
		$msb = (($chksum & 0xF0) >> 4) & 0x0F;
573
		if ($msb >=0 && $msb <= 15 ) $msbc = $hex_arr[$msb];
574
		else $msbc = '0';
575
		$itu = '!'.$itu."*{$msbc}{$lsbc}\r\n";
576
		return $itu;
577
	}
578
579
	public function parse($buffer) {
580
		$data = $this->process_ais_buf($buffer);
581
		if (!is_object($data)) return array();
582
		if ($data->lon != 0) $result['longitude'] = $data->lon;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
583
		if ($data->lat != 0) $result['latitude'] = $data->lat;
0 ignored issues
show
Bug introduced by
The variable $result 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...
584
		$result['ident'] = trim($data->name);
585
		$result['timestamp'] = $data->ts;
586
		$result['mmsi'] = $data->mmsi;
587
		if ($data->sog != -1.0) $result['speed'] = $data->sog;
588
		if ($data->cog != 0) $result['heading'] = $data->cog;
589
		/*
590
		    $ro->cls = 0; // AIS class undefined, also indicate unparsed msg
591
		    $ro->id = bindec(substr($_aisdata,0,6));
592
		*/
593
		return $result;
594
	}
595
596
	public function mmsitype($mmsi) {
597
		if (strlen($mmsi) == 9) {
598
			if (substr($mmsi,0,3) == '974') return 'EPIRB (Emergency Position Indicating Radio Beacon) AIS';
599
			elseif (substr($mmsi,0,3) == '972') return 'MOB (Man Overboard) device';
600
			elseif (substr($mmsi,0,3) == '970') return 'AIS SART (Search and Rescue Transmitter)';
601
			elseif (substr($mmsi,0,3) == '111') return 'SAR (Search and Rescue) aircraft';
602
			elseif (substr($mmsi,0,2) == '98') return 'Auxiliary craft associated with a parent ship';
603
			elseif (substr($mmsi,0,2) == '99') return 'Aids to Navigation';
604
			elseif (substr($mmsi,0,2) == '00') return 'Coastal stations';
605
			elseif (substr($mmsi,0,1) == '0') return 'Group of ships';
606
			else return 'Ship';
607
		}
608
609
	
610
	}
611
612
	public function parse_line($buffer) {
613
		global $globalDebug;
614
		$result = array();
615
		$data = new stdClass();
0 ignored issues
show
Unused Code introduced by
$data 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...
616
		$start = strpos($buffer,"VDM");
617
		$tst = substr($buffer, $start - 3);
618
		$data = $this->process_ais_raw( $tst, "" );
619
		if (!is_object($data)) {
620
			//if ($globalDebug) echo '==== Line format not supported : '.$buffer."\n";
621
			return array();
622
		}
623
		if ($data->lon != 0) $result['longitude'] = $data->lon;
624
		if ($data->lat != 0) $result['latitude'] = $data->lat;
625
		$result['ident'] = trim(str_replace('@','',$data->name));
626
		$result['timestamp'] = $data->ts;
627
		$result['mmsi'] = $data->mmsi;
628
		if (strlen($result['mmsi']) == 8 && substr($result['mmsi'],0,3) == '669') $result['mmsi'] = '3'.$result['mmsi'];
629
		$result['mmsi_type'] = $this->mmsitype($result['mmsi']);
630
		if ($data->sog != -1.0) $result['speed'] = $data->sog;
631
		if ($data->heading !== '') $result['heading'] = $data->heading;
632
		elseif ($data->cog != 0) $result['heading'] = $data->cog;
633
		if ($data->status != '') $result['status'] = $data->status;
634
		if ($data->statusid !== '') $result['statusid'] = $data->statusid;
635
		if ($data->type !== '') $result['type'] = $data->type;
636
		if ($data->typeid !== '') $result['typeid'] = $data->typeid;
637
		if ($data->imo !== '') $result['imo'] = $data->imo;
638
		if ($data->callsign !== '') $result['callsign'] = trim(str_replace('@','',$data->callsign));
639
		if (is_numeric($data->eta_month) && $data->eta_month != 0 && is_numeric($data->eta_day) && $data->eta_day != 0 && $data->eta_hour !== '' && $data->eta_minute !== '') {
640
			$eta_ts = strtotime(date('Y').'-'.sprintf("%02d",$data->eta_month).'-'.sprintf("%02d",$data->eta_day).' '.sprintf("%02d",$data->eta_hour).':'.sprintf("%02d",$data->eta_minute).':00');
641
			if ($eta_ts != '') $result['eta_ts'] = $eta_ts;
642
		} elseif (is_numeric($data->eta_hour) && is_numeric($data->eta_minute)) {
643
			$eta_ts = strtotime(date('Y-m-d').' '.sprintf("%02d",$data->eta_hour).':'.sprintf("%02d",$data->eta_minute).':00');
644
			if ($eta_ts != '') $result['eta_ts'] = $eta_ts;
645
		}
646
		if ($data->destination != '') {
647
			$dest = trim(str_replace('@','',$data->destination));
648
			if ($dest != '') $result['destination'] = $dest;
649
		}
650
		$result['all'] = (array) $data;
651
		/*
652
		    $ro->cls = 0; // AIS class undefined, also indicate unparsed msg
653
		    $ro->id = bindec(substr($_aisdata,0,6));
654
		*/
655
		return $result;
656
	}
657
	
658
	public function famaprs_to_ais($data) {
659
		
660
		$result['id'] = str_pad(decbin($id),6,'0',STR_PAD_LEFT);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

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

Loading history...
661
		$result['noidea'] = str_pad(decbin(0), 2, '0', STR_PAD_LEFT);
662
		$result['mmsi'] = str_pad(decbin($data['mmsi']),30,'0',STR_PAD_LEFT);
663
		//$result['noidea2'] = str_pad(decbin(0), 2, '0', STR_PAD_LEFT);
664
		$result['statusid'] = str_pad(decbin($data['status_id']),4,'0',STR_PAD_LEFT);
665
		//$result['statusid'] = str_pad(decbin($data['status_id']),2,'0',STR_PAD_LEFT);
666
		
667
		//$result['callsign'] = $this->char2bin($data['callsign'],20);
668
		if ($id >= 1 && $id <= 3) {
669
			$result['noidea2'] = str_pad(decbin(0), 8, '0', STR_PAD_LEFT);
670
			$result['sog'] = str_pad(decbin($data['speed']*10),10,'0',STR_PAD_LEFT);
671
			$result['longitude'] = str_pad(decbin($this->mk_ais_lon($data['longitude'])),28,'0',STR_PAD_LEFT);
672
			$result['latitude'] = str_pad(decbin($this->mk_ais_lat($data['latitude'])),27,'0',STR_PAD_LEFT);
673
			$result['cog'] = str_pad(decbin($data['heading']*10),12,'0',STR_PAD_LEFT);
674
		} else if ($id == 4) {
675
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,79,28)));
0 ignored issues
show
Bug introduced by
The variable $ro does not exist. Did you forget to declare it?

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

Loading history...
Bug introduced by
The variable $_aisdata does not exist. Did you forget to declare it?

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

Loading history...
676
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,107,27)));
677
		} else if ($id == 5) {
678
			$result['imo'] = str_pad(decbin($data['imo']),30,'0',STR_PAD_LEFT);
679
			$result['callsign'] = $this->char2bin($data['callsign'],42);
680
			$result['name'] = $this->char2bin($data['ident'],120);
681
			$ro->typeid = bindec(substr($_aisdata,232,8));
682
			$ro->type = $this->getShipType($ro->typeid);
683
			//$ro->to_bow = bindec(substr($_aisdata,240,9));
684
			//$ro->to_stern = bindec(substr($_aisdata,249,9));
685
			//$ro->to_port = bindec(substr($_aisdata,258,6));
686
			//$ro->to_starboard = bindec(substr($_aisdata,264,6));
687
			$ro->eta_month = bindec(substr($_aisdata,274,4));
688
			$ro->eta_day = bindec(substr($_aisdata,278,5));
689
			$ro->eta_hour = bindec(substr($_aisdata,283,5));
690
			$ro->eta_minute = bindec(substr($_aisdata,288,6));
691
			//$ro->draught = bindec(substr($_aisdata,294,8));
692
			$ro->destination = $this->binchar($_aisdata,302,120);
693
		} else if ($id == 9) {
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...
694
			// Search and Rescue aircraft position report
695
		} else if ($id == 18) {
696
			$ro->sog = bindec(substr($_aisdata,46,10))/10;
697
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,57,28)));
698
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,85,27)));
699
			$ro->cog = bindec(substr($_aisdata,112,12))/10;
700
			$ro->heading = bindec(substr($_aisdata,124,9));
701
			if ($ro->heading == 511) $ro->heading = '';
702
		} else if ($id == 19) {
703
			$ro->cog = bindec(substr($_aisdata,112,12))/10;
704
			$ro->sog = bindec(substr($_aisdata,46,10))/10;
705
			$ro->lon = str_pad($this->mk_ais_lon($data['longitude']),28,'0',STR_PAD_LEFT);
706
			$ro->lat = $this->mk_ais_lat(bindec(substr($_aisdata,89,27)));
707
			$ro->name = $this->binchar($_aisdata,143,120);
708
			$ro->cls = 2; // class B
709
			$ro->heading = bindec(substr($_aisdata,124,9));
710
			if ($ro->heading == 511) $ro->heading = '';
711
			$ro->typeid = bindec(substr($_aisdata,263,8));
712
			$ro->type = $this->getShipType($ro->typeid);
713
			//$ro->to_bow = bindec(substr($_aisdata,271,9));
714
			//$ro->to_stern = bindec(substr($_aisdata,280,9));
715
			//$ro->to_port = bindec(substr($_aisdata,289,6));
716
			//$ro->to_starboard = bindec(substr($_aisdata,295,6));
717
		} else if ($id == 21) {
718
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,164,28)));
719
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,192,27)));
720
			$ro->name = $this->binchar($_aisdata,43,120);
721
			//$ro->to_bow = bindec(substr($_aisdata,219,9));
722
			//$ro->to_stern = bindec(substr($_aisdata,228,9));
723
			//$ro->to_port = bindec(substr($_aisdata,237,6));
724
			//$ro->to_starboard = bindec(substr($_aisdata,243,6));
725
		} else if ($id == 24) {
726
			$pn = bindec(substr($_aisdata,38,2));
727
			if ($pn == 0) {
728
				$ro->name = $this->binchar($_aisdata,40,120);
729
			}
730
			$ro->typeid = bindec(substr($_aisdata,40,8));
731
			$ro->type = $this->getShipType($ro->typeid);
732
			$ro->callsign = $this->binchar($_aisdata,90,42);
733
			//$ro->to_bow = bindec(substr($_aisdata,132,9));
734
			//$ro->to_stern = bindec(substr($_aisdata,141,9));
735
			//$ro->to_port = bindec(substr($_aisdata,150,6));
736
			//$ro->to_starboard = bindec(substr($_aisdata,156,6));
737
		} else if ($id == 27) {
738
			$ro->cog = bindec(substr($_aisdata,85,9));
739
			if ($ro->cog == 511) $ro->cog = 0.0;
740
			$ro->sog = bindec(substr($_aisdata,79,6));
741
			if ($ro->sog == 63) $ro->sog = 0.0;
742
			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,44,18))*10);
743
			$ro->lat = $this->make_latf(bindec(substr($_aisdata,62,17))*10);
744
			$ro->cls = 1; // class A
745
		
746
		}
747
		$ro->statusid = bindec(substr($_aisdata,38,4));
748
		$ro->status = $this->getStatus($ro->statusid);
749
750
		return $this->mk_ais(implode('',$result));
751
	}
752
}
753