<?php
  include "./config.php";
  $prog=$_GET['prog'];
  $platform=$_GET['platform'];
  $ip=$_SERVER['REMOTE_ADDR'];
  $cmd="echo 'date: ".date("D M j G:i:s T Y")."; prog: $prog; platform: $platform; ip:$ip;' >>download_log.txt";
  //echo $cmd;
  //exit(0);
  exec($cmd);

  $file="";
  switch($prog){
    case "fastanova": $file="FastANOVA-".$prog_version[$prog]; break;
    case "coe": $file="COE-".$prog_version[$prog]; break;
    case "team": $file="TEAM-".$prog_version[$prog]; break;
    default: header('HTTP/1.1 404 Not Found'); exit(1);
  }
  switch($platform){
    case "linux": $file=$file."_linux.tar.gz"; break;
    case "win32": $file=$file."_win32.zip"; break;
    default: header('HTTP/1.1 404 Not Found'); exit(1);
  }
 
  $file="./download/".$file;   
  //echo "$file";
if (file_exists($file)) {    
    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    
    #Fix the ob_clean notice "ob_clean() failed to delete buffer. No buffer to delete."
    if (count(ob_list_handlers()) > 0) {
      ob_clean();
    }
    #ob_clean();
    flush();
    readfile($file);    
    exit;
}
else {
   header('HTTP/1.1 404 Not Found'); exit(1);
}
?>
