<?PHP
header("Content-type: text/xml");
# haalt informatie op uit textfiles en geeft output als XML 
# Electronic-Service / Robert Verhagen maart 2015
# Versie 001a

function positie($naamschip)
	{
	#variabelen
		$lokatiepad = "";

	#lees eerste regel van file
		$bestandsnaam =("$lokatiepad"."$naamschip");
		$file = fopen($bestandsnaam,"r");
		$posinfo = fgets($file);
		fclose($file);
 		
   #Zet XML om naar variabelen en stuur deze als output
		$CSVvar = explode(",", $posinfo);
		$resultaat = "'$CSVvar[2]', $CSVvar[3], $CSVvar[4]";
		$age=(int)((time()-$CSVvar[6]) /60);
		echo "<ship>";
		echo "<name>$CSVvar[2]</name>";
		echo "<lat>$CSVvar[3]</lat>";
		echo "<lng>$CSVvar[4]</lng>";
		echo "<last_pos_update>$CSVvar[0] $CSVvar[1]</last_pos_update>";
		echo "<update_age>$age</update_age>";
		echo "<speed>$CSVvar[5]</speed>";
		echo "</ship>";
   }
   
   #hoofdprogramma
   
   # stuur XML code
   echo "<?xml version='1.0' encoding='UTF-8'?>";
   echo "<shipinfo>";
		#aanroep locaties vanuit PHP
		#Zoek alle *.txt bestanden welke jonger zijn dan 20 dagen ( 172800 sec )
        foreach (glob("*.txt") as $filename) {
		     if (filemtime($filename) > time() - 1728000) {
                          positie($filename);
                          }
	}
 
		# # Amavida heeft geen GPS positie, zal vaste positie doorgeven
		echo "<ship>";
		echo "<name>AMAVIDA</name>";
		echo "<lat>41.140525</lat>";
		echo "<lng>-8.618119</lng>";
		echo "<last_pos_update>01-01-2015</last_pos_update>";
		echo "<update_age>0</update_age>";
		echo "<speed>0.00</speed>";
		echo "</ship>";
        
		#Electronoc-Service KANTOOR
         echo "<ship>";
         echo "<name>ES-BV</name>";
         echo "<lat>51.778972</lat>";
         echo "<lng>4.925503</lng>";
         echo "<last_pos_update>01-01-2017</last_pos_update>";
         echo "<update_age>0</update_age>";
         echo "<speed>99.00</speed>";
         echo "</ship>";
		

        #amakristina fake, straks gebruiken we:
	#positie("AMAKRISTINA");
#	echo "<ship>";
 #       echo "<name>AMAKRISTINA</name>";
  #      echo "<lat>51.976072</lat>";
   #     echo "<lng>5.972363</lng>";
    #    echo "<last_pos_update>01-01-2015</last_pos_update>";
     #   echo "<update_age>0</update_age>";
      #  echo "<speed>0.00</speed>";
       # echo "</ship>";


  echo "</shipinfo>";


?>
