Completed
Push — master ( be2090...d8f2d2 )
by Yannick
33:22
created
require/class.AIS.php 1 patch
Braces   +202 added lines, -78 removed lines patch added patch discarded remove patch
@@ -123,7 +123,9 @@  discard block
 block discarded – undo
123 123
 			$temp += 1;
124 124
 			$flat = (float)($temp / (60.0 * 10000.0));
125 125
 			$flat *= -1.0;
126
-		} else $flat = (float)($temp / (60.0 * 10000.0));
126
+		} else {
127
+			$flat = (float)($temp / (60.0 * 10000.0));
128
+		}
127 129
 		return $flat; // float
128 130
 	}
129 131
 
@@ -135,7 +137,9 @@  discard block
 block discarded – undo
135 137
 			$temp += 1;
136 138
 			$flon = (float)($temp / (60.0 * 10000.0));
137 139
 			$flon *= -1.0;
138
-		} else $flon = (float)($temp / (60.0 * 10000.0));
140
+		} else {
141
+			$flon = (float)($temp / (60.0 * 10000.0));
142
+		}
139 143
 		return $flon;
140 144
 	}
141 145
 
@@ -158,10 +162,8 @@  discard block
 block discarded – undo
158 162
     */
159 163
 	private function asciidec_2_8bit($ascii) {
160 164
 		//only process in the following range: 48-87, 96-119
161
-		if ($ascii < 48) { }
162
-		else {
163
-			if($ascii>119) { }
164
-			else {
165
+		if ($ascii < 48) { } else {
166
+			if($ascii>119) { } else {
165 167
 				if ($ascii>87 && $ascii<96) ;
166 168
 				else {
167 169
 					$ascii=$ascii+40;
@@ -263,7 +265,9 @@  discard block
 block discarded – undo
263 265
 			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,57,28)));
264 266
 			$ro->lat = $this->make_latf(bindec(substr($_aisdata,85,27)));
265 267
 			$ro->heading = bindec(substr($_aisdata,124,9));
266
-			if ($ro->heading == 511) $ro->heading = '';
268
+			if ($ro->heading == 511) {
269
+				$ro->heading = '';
270
+			}
267 271
 			$ro->cls = 2; // class B
268 272
 		} else if ($ro->id == 19) {
269 273
 			$ro->cog = bindec(substr($_aisdata,112,12))/10;
@@ -273,7 +277,9 @@  discard block
 block discarded – undo
273 277
 			$ro->name = $this->binchar($_aisdata,143,120);
274 278
 			$ro->cls = 2; // class B
275 279
 			$ro->heading = bindec(substr($_aisdata,124,9));
276
-			if ($ro->heading == 511) $ro->heading = '';
280
+			if ($ro->heading == 511) {
281
+				$ro->heading = '';
282
+			}
277 283
 			$ro->typeid = bindec(substr($_aisdata,263,8));
278 284
 			$ro->type = $this->getShipType($ro->typeid);
279 285
 			//$ro->to_bow = bindec(substr($_aisdata,271,9));
@@ -304,9 +310,13 @@  discard block
 block discarded – undo
304 310
 			$ro->cls = 2; // class B
305 311
 		} else if ($ro->id == 27) {
306 312
 			$ro->cog = bindec(substr($_aisdata,85,9));
307
-			if ($ro->cog == 511) $ro->cog = 0.0;
313
+			if ($ro->cog == 511) {
314
+				$ro->cog = 0.0;
315
+			}
308 316
 			$ro->sog = bindec(substr($_aisdata,79,6));
309
-			if ($ro->sog == 63) $ro->sog = 0.0;
317
+			if ($ro->sog == 63) {
318
+				$ro->sog = 0.0;
319
+			}
310 320
 			$ro->lon = $this->make_lonf(bindec(substr($_aisdata,44,18))*10);
311 321
 			$ro->lat = $this->make_latf(bindec(substr($_aisdata,62,17))*10);
312 322
 			$ro->cls = 1; // class A
@@ -354,27 +364,46 @@  discard block
 block discarded – undo
354 364
 	}
355 365
 	
356 366
 	public function getShipType($code) {
357
-		if (isset($this->shiptype[$code])) return $this->shiptype[$code];
358
-		else return '';
367
+		if (isset($this->shiptype[$code])) {
368
+			return $this->shiptype[$code];
369
+		} else {
370
+			return '';
371
+		}
359 372
 	}
360 373
 
361 374
 	public function getShipTypeID($type) {
362 375
 		$typeid = array_search($type,$this->shiptype);
363
-		if ($typeid !== FALSE) return $typeid;
364
-		elseif ($type == 'Cargo' || $type == 'General Cargo' || $type == 'Bulk Carrier' || $type == 'Container Ship' || $type == 'Cargo A' || $type == 'Reefer' || $type == 'Vehicles Carrier') return 70;
365
-		elseif ($type == 'Passengers Ship' || $type == 'Passenger' || $type == 'Ro-Ro/Passenger Ship' || $type == 'Motor Passenger') return 60;
366
-		elseif ($type == 'Tanker' || $type == 'Crude Oil Tanker' || $type == 'Oil Products Tanker' || $type == 'Oil/Chemical Tanker' || $type == 'Bunkering Tanker') return 80;
367
-		elseif ($type == 'Lpg Tanker') return 84;
368
-		elseif ($type == 'Sailing Vessel') return 36;
369
-		elseif ($type == 'Yacht') return 37;
370
-		elseif ($type == 'Trailing Suction Hopper Dredger' || $type == 'Drilling Jack Up' || $type == 'Suction Dredger' || $type == 'Hopper Dredger') return 33;
371
-		elseif ($type == 'High speed craft') return 40;
372
-		elseif ($type == 'Icebreaker') return 52;
373
-		elseif ($type == 'Pollution Control Vessel') return 54;
374
-		elseif ($type == 'Other Type') return 90;
375
-		elseif ($type == 'Anchor Handling Vessel' || $type == 'Offshore Supply Ship' || $type == 'Research/Survey Vessel' || $type == 'Utility Vessel' || $type == 'Pipe Burying Vessel' || $type == 'Heavy Lift Vessel' || $type == 'Crane Ship') return 59;
376
-		elseif ($type == 'Fish Carrier' || $type == 'Trawler' || $type == 'Trawlers' || $type == 'Fishing Vessel' || $type == 'Trawlers|unknown Not Fishing' || $type == 'Purse Seines|trawlers' || $type == 'Purse Seines' || $type == 'Trollers') return 30;
377
-		else return '';
376
+		if ($typeid !== FALSE) {
377
+			return $typeid;
378
+		} elseif ($type == 'Cargo' || $type == 'General Cargo' || $type == 'Bulk Carrier' || $type == 'Container Ship' || $type == 'Cargo A' || $type == 'Reefer' || $type == 'Vehicles Carrier') {
379
+			return 70;
380
+		} elseif ($type == 'Passengers Ship' || $type == 'Passenger' || $type == 'Ro-Ro/Passenger Ship' || $type == 'Motor Passenger') {
381
+			return 60;
382
+		} elseif ($type == 'Tanker' || $type == 'Crude Oil Tanker' || $type == 'Oil Products Tanker' || $type == 'Oil/Chemical Tanker' || $type == 'Bunkering Tanker') {
383
+			return 80;
384
+		} elseif ($type == 'Lpg Tanker') {
385
+			return 84;
386
+		} elseif ($type == 'Sailing Vessel') {
387
+			return 36;
388
+		} elseif ($type == 'Yacht') {
389
+			return 37;
390
+		} elseif ($type == 'Trailing Suction Hopper Dredger' || $type == 'Drilling Jack Up' || $type == 'Suction Dredger' || $type == 'Hopper Dredger') {
391
+			return 33;
392
+		} elseif ($type == 'High speed craft') {
393
+			return 40;
394
+		} elseif ($type == 'Icebreaker') {
395
+			return 52;
396
+		} elseif ($type == 'Pollution Control Vessel') {
397
+			return 54;
398
+		} elseif ($type == 'Other Type') {
399
+			return 90;
400
+		} elseif ($type == 'Anchor Handling Vessel' || $type == 'Offshore Supply Ship' || $type == 'Research/Survey Vessel' || $type == 'Utility Vessel' || $type == 'Pipe Burying Vessel' || $type == 'Heavy Lift Vessel' || $type == 'Crane Ship') {
401
+			return 59;
402
+		} elseif ($type == 'Fish Carrier' || $type == 'Trawler' || $type == 'Trawlers' || $type == 'Fishing Vessel' || $type == 'Trawlers|unknown Not Fishing' || $type == 'Purse Seines|trawlers' || $type == 'Purse Seines' || $type == 'Trollers') {
403
+			return 30;
404
+		} else {
405
+			return '';
406
+		}
378 407
 	}
379 408
 
380 409
 	public function process_ais_itu($_itu, $_len, $_filler, $aux /*, $ais_ch*/) {
@@ -411,11 +440,20 @@  discard block
 block discarded – undo
411 440
 		// assume 1st ! is valid
412 441
 		// find * ensure that it is at correct position
413 442
 		$end = strrpos ( $rawdata , '*' );
414
-		if ($end === FALSE) return -1; // check for NULLS!!!
443
+		if ($end === FALSE) {
444
+			return -1;
445
+		}
446
+		// check for NULLS!!!
415 447
 		$cs = substr( $rawdata, $end + 1 );
416
-		if ( strlen($cs) != 2 ) return -1; // correct cs length
448
+		if ( strlen($cs) != 2 ) {
449
+			return -1;
450
+		}
451
+		// correct cs length
417 452
 		$dcs = (int)hexdec( $cs );
418
-		for ( $alias=1; $alias<$end; $alias++) $chksum ^= ord( $rawdata[$alias] ); // perform XOR for NMEA checksum
453
+		for ( $alias=1; $alias<$end; $alias++) {
454
+			$chksum ^= ord( $rawdata[$alias] );
455
+		}
456
+		// perform XOR for NMEA checksum
419 457
 		if ( $chksum == $dcs ) { // NMEA checksum pass
420 458
 			$pcs = explode(',', $rawdata);
421 459
 			// !AI??? identifier
@@ -426,8 +464,14 @@  discard block
 block discarded – undo
426 464
 			$num_seq = (int)$pcs[1]; // number of sequences
427 465
 			$seq = (int)$pcs[2]; // get sequence
428 466
 			// get msg sequence id
429
-			if ($pcs[3] == '') $msg_sid = -1; // non-multipart message, set to -1
430
-			else $msg_sid = (int)$pcs[3]; // multipart message
467
+			if ($pcs[3] == '') {
468
+				$msg_sid = -1;
469
+			}
470
+			// non-multipart message, set to -1
471
+			else {
472
+				$msg_sid = (int)$pcs[3];
473
+			}
474
+			// multipart message
431 475
 			$ais_ch = $pcs[4]; // get AIS channel
432 476
 			// message sequence checking
433 477
 			if ($num_seq < 1 || $num_seq > 9) {
@@ -490,10 +534,18 @@  discard block
 block discarded – undo
490 534
 				//DEBUG echo "[$start $end $tst]\n";
491 535
 				$result = $this->process_ais_raw( $tst, "" );
492 536
 				$last_pos = $end + 1;
493
-			} else break;
537
+			} else {
538
+				break;
539
+			}
540
+		}
541
+		if ($last_pos > 0) {
542
+			$cbuf = substr($cbuf, $last_pos);
494 543
 		}
495
-		if ($last_pos > 0) $cbuf = substr($cbuf, $last_pos); // move...
496
-		if (strlen($cbuf) > 1024) $cbuf = ""; // prevent overflow simple mode...
544
+		// move...
545
+		if (strlen($cbuf) > 1024) {
546
+			$cbuf = "";
547
+		}
548
+		// prevent overflow simple mode...
497 549
 		return $result;
498 550
 	}
499 551
 
@@ -513,7 +565,9 @@  discard block
 block discarded – undo
513 565
 		if ($lat<0.0) {
514 566
 			$lat = -$lat;
515 567
 			$neg=true;
516
-		} else $neg=false;
568
+		} else {
569
+			$neg=false;
570
+		}
517 571
 		$latd = 0x00000000;
518 572
 		$latd = intval ($lat * 600000.0);
519 573
 		if ($neg==true) {
@@ -529,7 +583,9 @@  discard block
 block discarded – undo
529 583
 		if ($lon<0.0) {
530 584
 			$lon = -$lon;
531 585
 			$neg=true;
532
-		} else $neg=false;
586
+		} else {
587
+			$neg=false;
588
+		}
533 589
 		$lond = 0x00000000;
534 590
 		$lond = intval ($lon * 600000.0);
535 591
 		if ($neg==true) {
@@ -542,9 +598,14 @@  discard block
 block discarded – undo
542 598
 
543 599
 	private function char2bin($name, $max_len) {
544 600
 		$len = strlen($name);
545
-		if ($len > $max_len) $name = substr($name,0,$max_len);
546
-		if ($len < $max_len) $pad = str_repeat('0', ($max_len - $len) * 6);
547
-		else $pad = '';
601
+		if ($len > $max_len) {
602
+			$name = substr($name,0,$max_len);
603
+		}
604
+		if ($len < $max_len) {
605
+			$pad = str_repeat('0', ($max_len - $len) * 6);
606
+		} else {
607
+			$pad = '';
608
+		}
548 609
 		$rv = '';
549 610
 		$ais_chars = array(
550 611
 		    '@'=>0, 'A'=>1, 'B'=>2, 'C'=>3, 'D'=>4, 'E'=>5, 'F'=>6, 'G'=>7, 'H'=>8, 'I'=>9,
@@ -557,9 +618,12 @@  discard block
 block discarded – undo
557 618
 		);
558 619
 		// "
559 620
 		$_a = str_split($name);
560
-		if ($_a) foreach ($_a as $_1) {
621
+		if ($_a) {
622
+			foreach ($_a as $_1) {
561 623
 			if (isset($ais_chars[$_1])) $dec = $ais_chars[$_1];
562
-			else $dec = 0;
624
+		} else {
625
+				$dec = 0;
626
+			}
563 627
 			$bin = str_pad(decbin( $dec ), 6, '0', STR_PAD_LEFT);
564 628
 			$rv .= $bin;
565 629
 			//echo "$_1 $dec ($bin)<br/>";
@@ -571,7 +635,9 @@  discard block
 block discarded – undo
571 635
 		$len_bit = strlen($_enc);
572 636
 		$rem6 = $len_bit % 6;
573 637
 		$pad6_len = 0;
574
-		if ($rem6) $pad6_len = 6 - $rem6;
638
+		if ($rem6) {
639
+			$pad6_len = 6 - $rem6;
640
+		}
575 641
 		//echo  $pad6_len.'<br>';
576 642
 		$_enc .= str_repeat("0", $pad6_len); // pad the text...
577 643
 		$len_enc = strlen($_enc) / 6;
@@ -580,8 +646,11 @@  discard block
 block discarded – undo
580 646
 		for ($i=0; $i<$len_enc; $i++) {
581 647
 			$offset = $i * 6;
582 648
 			$dec = bindec(substr($_enc,$offset,6));
583
-			if ($dec < 40) $dec += 48;
584
-			else $dec += 56;
649
+			if ($dec < 40) {
650
+				$dec += 48;
651
+			} else {
652
+				$dec += 56;
653
+			}
585 654
 			//echo chr($dec)." $dec<br/>";
586 655
 			$itu .= chr($dec);
587 656
 		}
@@ -594,26 +663,42 @@  discard block
 block discarded – undo
594 663
 		}
595 664
 		$hex_arr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
596 665
 		$lsb = $chksum & 0x0F;
597
-		if ($lsb >=0 && $lsb <= 15 ) $lsbc = $hex_arr[$lsb];
598
-		else $lsbc = '0';
666
+		if ($lsb >=0 && $lsb <= 15 ) {
667
+			$lsbc = $hex_arr[$lsb];
668
+		} else {
669
+			$lsbc = '0';
670
+		}
599 671
 		$msb = (($chksum & 0xF0) >> 4) & 0x0F;
600
-		if ($msb >=0 && $msb <= 15 ) $msbc = $hex_arr[$msb];
601
-		else $msbc = '0';
672
+		if ($msb >=0 && $msb <= 15 ) {
673
+			$msbc = $hex_arr[$msb];
674
+		} else {
675
+			$msbc = '0';
676
+		}
602 677
 		$itu = '!'.$itu."*{$msbc}{$lsbc}\r\n";
603 678
 		return $itu;
604 679
 	}
605 680
 
606 681
 	public function parse($buffer) {
607 682
 		$data = $this->process_ais_buf($buffer);
608
-		if (!is_object($data)) return array();
683
+		if (!is_object($data)) {
684
+			return array();
685
+		}
609 686
 		$result = array();
610
-		if ($data->lon != 0) $result['longitude'] = $data->lon;
611
-		if ($data->lat != 0) $result['latitude'] = $data->lat;
687
+		if ($data->lon != 0) {
688
+			$result['longitude'] = $data->lon;
689
+		}
690
+		if ($data->lat != 0) {
691
+			$result['latitude'] = $data->lat;
692
+		}
612 693
 		$result['ident'] = trim($data->name);
613 694
 		$result['timestamp'] = $data->ts;
614 695
 		$result['mmsi'] = $data->mmsi;
615
-		if ($data->sog != -1.0) $result['speed'] = $data->sog;
616
-		if ($data->cog != 0) $result['heading'] = $data->cog;
696
+		if ($data->sog != -1.0) {
697
+			$result['speed'] = $data->sog;
698
+		}
699
+		if ($data->cog != 0) {
700
+			$result['heading'] = $data->cog;
701
+		}
617 702
 		/*
618 703
 		    $ro->cls = 0; // AIS class undefined, also indicate unparsed msg
619 704
 		    $ro->id = bindec(substr($_aisdata,0,6));
@@ -623,15 +708,25 @@  discard block
 block discarded – undo
623 708
 
624 709
 	public function mmsitype($mmsi) {
625 710
 		if (strlen($mmsi) == 9) {
626
-			if (substr($mmsi,0,3) == '974') return 'EPIRB (Emergency Position Indicating Radio Beacon) AIS';
627
-			elseif (substr($mmsi,0,3) == '972') return 'MOB (Man Overboard) device';
628
-			elseif (substr($mmsi,0,3) == '970') return 'AIS SART (Search and Rescue Transmitter)';
629
-			elseif (substr($mmsi,0,3) == '111') return 'SAR (Search and Rescue) aircraft';
630
-			elseif (substr($mmsi,0,2) == '98') return 'Auxiliary craft associated with a parent ship';
631
-			elseif (substr($mmsi,0,2) == '99') return 'Aids to Navigation';
632
-			elseif (substr($mmsi,0,2) == '00') return 'Coastal stations';
633
-			elseif (substr($mmsi,0,1) == '0') return 'Group of ships';
634
-			else return 'Ship';
711
+			if (substr($mmsi,0,3) == '974') {
712
+				return 'EPIRB (Emergency Position Indicating Radio Beacon) AIS';
713
+			} elseif (substr($mmsi,0,3) == '972') {
714
+				return 'MOB (Man Overboard) device';
715
+			} elseif (substr($mmsi,0,3) == '970') {
716
+				return 'AIS SART (Search and Rescue Transmitter)';
717
+			} elseif (substr($mmsi,0,3) == '111') {
718
+				return 'SAR (Search and Rescue) aircraft';
719
+			} elseif (substr($mmsi,0,2) == '98') {
720
+				return 'Auxiliary craft associated with a parent ship';
721
+			} elseif (substr($mmsi,0,2) == '99') {
722
+				return 'Aids to Navigation';
723
+			} elseif (substr($mmsi,0,2) == '00') {
724
+				return 'Coastal stations';
725
+			} elseif (substr($mmsi,0,1) == '0') {
726
+				return 'Group of ships';
727
+			} else {
728
+				return 'Ship';
729
+			}
635 730
 		}
636 731
 
637 732
 	
@@ -647,32 +742,61 @@  discard block
 block discarded – undo
647 742
 			//if ($globalDebug) echo '==== Line format not supported : '.$buffer."\n";
648 743
 			return array();
649 744
 		}
650
-		if ($data->lon != 0) $result['longitude'] = $data->lon;
651
-		if ($data->lat != 0) $result['latitude'] = $data->lat;
745
+		if ($data->lon != 0) {
746
+			$result['longitude'] = $data->lon;
747
+		}
748
+		if ($data->lat != 0) {
749
+			$result['latitude'] = $data->lat;
750
+		}
652 751
 		$result['ident'] = trim(str_replace('@','',$data->name));
653 752
 		$result['timestamp'] = $data->ts;
654 753
 		$result['mmsi'] = $data->mmsi;
655
-		if (strlen($result['mmsi']) == 8 && substr($result['mmsi'],0,3) == '669') $result['mmsi'] = '3'.$result['mmsi'];
754
+		if (strlen($result['mmsi']) == 8 && substr($result['mmsi'],0,3) == '669') {
755
+			$result['mmsi'] = '3'.$result['mmsi'];
756
+		}
656 757
 		$result['mmsi_type'] = $this->mmsitype($result['mmsi']);
657
-		if ($data->sog != -1.0) $result['speed'] = $data->sog;
658
-		if ($data->heading !== '') $result['heading'] = $data->heading;
659
-		elseif ($data->cog != 0) $result['heading'] = $data->cog;
660
-		if ($data->status != '') $result['status'] = $data->status;
661
-		if ($data->statusid !== '') $result['statusid'] = $data->statusid;
662
-		if ($data->type !== '') $result['type'] = $data->type;
663
-		if ($data->typeid !== '') $result['typeid'] = $data->typeid;
664
-		if ($data->imo !== '') $result['imo'] = $data->imo;
665
-		if ($data->callsign !== '') $result['callsign'] = trim(str_replace('@','',$data->callsign));
758
+		if ($data->sog != -1.0) {
759
+			$result['speed'] = $data->sog;
760
+		}
761
+		if ($data->heading !== '') {
762
+			$result['heading'] = $data->heading;
763
+		} elseif ($data->cog != 0) {
764
+			$result['heading'] = $data->cog;
765
+		}
766
+		if ($data->status != '') {
767
+			$result['status'] = $data->status;
768
+		}
769
+		if ($data->statusid !== '') {
770
+			$result['statusid'] = $data->statusid;
771
+		}
772
+		if ($data->type !== '') {
773
+			$result['type'] = $data->type;
774
+		}
775
+		if ($data->typeid !== '') {
776
+			$result['typeid'] = $data->typeid;
777
+		}
778
+		if ($data->imo !== '') {
779
+			$result['imo'] = $data->imo;
780
+		}
781
+		if ($data->callsign !== '') {
782
+			$result['callsign'] = trim(str_replace('@','',$data->callsign));
783
+		}
666 784
 		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 !== '') {
667 785
 			$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');
668
-			if ($eta_ts != '') $result['eta_ts'] = $eta_ts;
786
+			if ($eta_ts != '') {
787
+				$result['eta_ts'] = $eta_ts;
788
+			}
669 789
 		} elseif (is_numeric($data->eta_hour) && is_numeric($data->eta_minute)) {
670 790
 			$eta_ts = strtotime(date('Y-m-d').' '.sprintf("%02d",$data->eta_hour).':'.sprintf("%02d",$data->eta_minute).':00');
671
-			if ($eta_ts != '') $result['eta_ts'] = $eta_ts;
791
+			if ($eta_ts != '') {
792
+				$result['eta_ts'] = $eta_ts;
793
+			}
672 794
 		}
673 795
 		if ($data->destination != '') {
674 796
 			$dest = trim(str_replace('@','',$data->destination));
675
-			if ($dest != '') $result['destination'] = $dest;
797
+			if ($dest != '') {
798
+				$result['destination'] = $dest;
799
+			}
676 800
 		}
677 801
 		$result['all'] = (array) $data;
678 802
 		/*
Please login to merge, or discard this patch.