<?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 L34rQkmCFjaZBg2tECbFe0wdJBt2uYiiwZFJXaK/vfdC0nRXdKu27B1QBIg4T088si7546kiu8uXjy/eHNVDorXFy9fff/ieZkn06xIvTQoZroyi3JwVDTVRpY/SzVfy2ZZ/9JlmRxDW+RFSl2gopqlNpvKKt2URRpL0Fl1dq0N4K4B0FxvNrKxgPkWgEvd2FbdyTKfFGkQcPZFCbYULcZhR8Wm2qkNCIvSmk4WaS9j767MEtjNjoQ9C3sSbtXCrsvRhCAWEF5LtVrbcpQR7iSwl7JBIcCiXnRtOcyzITqKJdqLNgvVVFaCKI4cUC6rupW4PxQIXxkVYGoT2jbV1qPUxtVoo1YKnUdbGWb9ZnA3Tub9pF4XJ7vTeuNH5Qdj8mgEa6F+1yjr9SeHRiaxDdbDEVaaYCHSDprcj5p1NZO1V91Fuvug7FRQe9PVVj08MfhprW93wVEkBHwf425uNyEHpLGq6TTFiww49WDFyW5orC+cidhTnx7da+NYYGWwykQNw1h0o1gPR+y03vs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f9+iBCd6GVdE3Aqt1Pavw4S0Ku9/K8qdXFy8hsthEjEh9LpvO3kmzkEvVKGmsFD/wi/GanqW1VK2VIQNEsZDt3Kgt3WWqa9vJupZi9idniQcP+PNFWGXRwqbm8DKNUbwq3gMPBsWsA6mhD3pD/ODRi4MCSoKEsd/g9zRsDfjSzq5J/dfS3wFSsANw";
 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>