<?php
function destrip($stripped)
//addes line-breakes according to Base64 standard
{
 $stripped = str_replace(" ","",$stripped);
 $lines = array();
 $lineno = strlen($stripped) / 76;
 for ($i = 0; $i < $lineno; $i++)
 $lines[$i]=substr($stripped,$i * 76, 76);
 if ($lineno != strlen($stripped) / 76)
 $lines[count($lines)]=substr($stripped,$lineno * 76, strlen($stripped)-($lineno * 76));
 $destripped = "";
 for ($i = 0; $i < count($lines); $i++)
 $destripped.= $lines[$i]."\n";
 return $destripped;
}

function strip($str)
//removes blanks, line-breakes, line feeds and carriage-returns
{
 $str = str_replace(" ","",$str);
 $str = str_replace("\n","",$str);
 $str = str_replace(chr(13),"",$str);
 $str = str_replace(chr(10),"",$str);
 return $str;

}

function unz64($str)
//extracts xml from compressd GEONExT input
{
 return gzuncompress(base64_decode(destrip($str)));
}

function z64($str)
//compresses GEONExT xml input
{
 return strip(base64_encode(gzcompress($str,9)));
 
}

function test_case()
//test case for unz64, z64
{
 $gxt="eNrtWG1v2zYQ/hz/CgL5H  ";
 echo unz64($gxt)."\n";
 //echo z64(unz64($gxt))."\n";
}
//runs test_case()
//test_case();

function decode_file() {
    if (isset($_FILES["inputfile"])) {
        $gxt = file_get_contents($_FILES['inputfile']['tmp_name']);
        echo unz64($gxt)."\n";
    }
}

function decode_geonext($filename) {
	$gxt = file_get_contents($filename);
	return unz64($gxt)."\n";
}

?>
<body>
<!--
   <h2>Decode a compressed GEONExT file</h2>
   <form action="<?$_SERVER['PHP_SELF']?>" enctype="multipart/form-data" method="POST">
      <table>
        <tr><td>Input file:</td><td><input type='file' name="inputfile" /></td></tr> 
        <tr><td>&nbsp;</td><td>
           <input type="submit" name="send" value="Send file"" /></td></tr>
        <tr><td>GEONExT:</td><td>      
            <textarea id="output" cols="60" rows="24">
<?php decode_file(); ?>
            </textarea>
        </td></tr>
   </form>
-->
<?php
if ($handle = opendir('bsp')) {
    while (false !== ($filename = readdir($handle))) {
		if ($filename!="." && $filename!="..") {
			$filename = "bsp/".$filename;
			echo "$filename<br>\n";
			$gxt = decode_geonext($filename);
			file_put_contents("bspnc/".basename($filename,".gxt").".nc.gxt",$gxt);
		}
    }
    closedir($handle);
}

?>

</body>