<?php
include("/var/www/html/assets/templates/nrel/apps/ftp/io.pinc");
// Pull in "get" variables
if (!empty($_GET['DownloadDir'])) {
  $DownloadDir=RemoveXSS($_GET['DownloadDir']);
}
if (!empty($_GET['DownloadFile'])) {
  $DownloadFile=RemoveXSS($_GET['DownloadFile']);
}
if (!empty($_GET['Directory'])) {
  $Directory=RemoveXSS($_GET['Directory']);
};

// Determine where we're looking
#$FileBase="../outgoing";
$FileBase=".";

#$browser = get_browser(null, true);

if (!empty($DownloadDir)) // If we've been requetsted to ship a directory
  {
    $ArchiveType="zip";
    #if ($browser->browser == 'msie')
    #if (preg_match("/msie/i",$_SERVER["HTTP_USER_AGENT"]))
      #{
        #header("Content-Type: application/x-unknown-octet-stream");
      #}
    #else
      #{
        #header("Content-Type: application/x-download");
      #}
    $dname=basename($DownloadDir);
    if ($ArchiveType=="zip")
      {

      #  header("Content-Transfer-Encoding: binary\n");
      #  header("Content-Type: application/zip");
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=$dname.zip");
	header("Content-Transfer-Encoding: binary");
	#if(strpos($_SERVER[’HTTP_USER_AGENT’], ‘MSIE’)){
	#// IE cannot download from sessions without a cache
	header("Cache-Control: public");
        $fp = popen ("cd $FileBase/$DownloadDir/.. ; zip -r - $dname","r");
        fpassthru($fp);
        pclose($fp);
        exit;
      }
    if ($ArchiveType==tgz)
      {
        header("Content-Disposition: attachment; filename=$dname.tgz");
        $fp = popen ("cd $FileBase/$DownloadDir/.. ; tar czf - $dname","r");
        fpassthru($fp);
        pclose($fp);
        exit;
      }
  }


if (!empty($DownloadFile)) // If we've been requetsted to ship a file
  {
    #if ($browser->browser == 'msie')
    if (preg_match("/msie/i",$_SERVER["HTTP_USER_AGENT"]))
      {
        header("Content-Type: application/x-unknown-octet-stream");
      }
    else
      {
        header("Content-Type: application/x-download");
      }
    $dname=basename($DownloadFile);
    header("Content-Disposition: attachment; filename=$dname");
    readfile($DownloadFile);
    exit;
  }
?>

<html>
<head><title>Download Files from the NREL</title></head>
<body bgcolor="white" FONT="tahoma, verdana, arial, helvetica, sans-serif" TOPMARGIN=3 BOTTOMMARGIN=3>
<a href="http://www.nrel.colostate.edu"><img src=/nrel/images/nrelogo-cal.gif align="right" alt="NREL Home Page" border="0"></a><br>
<H2>
Files available for download:
</H2>
<br>
<?php

//build a file list

if (empty($Directory))
  {
    $Directory="";
    $SearchDir=$FileBase;
  }
else
  {
    # Don't allow file assension - no one can come up
    if ((preg_match("/^\.\./",$Directory))||(preg_match("/\/\.\.\//",$Directory))||(preg_match("/\.\.$/",$Directory)))
      {
	$Directory="";
        $SearchDir=$FileBase;
      }
    else
      {
        $Directory="$Directory/";
        $SearchDir="$FileBase/$Directory";
      }
  }

print "<i>Listing for files in /$Directory</i><br><br>\n";

$files=`ls $SearchDir | grep -v .webinfo`;

$filesarray=preg_split("/\s+/", $files);
# spaces aren't allowed in web files anyway, so go with the above
#$filesarray=preg_split("/\n/", $files);

?>



<!-- Start of table -->
<table Border cellpadding=3>
<th>View</th>
<th>Download</th>
<th>Description</th>
<th>Date</th>
<th>Size</th>

<?php
$PHPSELF=$_SERVER['PHP_SELF'];
foreach ($filesarray as $file)
  {
    if (!empty($file) && $file!="index.php" && substr($file,-5,5)!="_lock")
      {
        if (is_file("$SearchDir/$file"))
          {
            $extension="bytes";
            $size=filesize("$SearchDir/$file");
	    if ($size >= 1024)
	      {
	        $size=$size/1024;
	        $extension="KB";
	      }
	    if ($size >= 1024)
	      {
	        $size=$size/1024;
	        $extension="MB";
	      }
	    if ($size >= 1024)
	      {
	        $size=$size/1024;
	        $extension="GB";
	      }
            if (is_file("$SearchDir/$file.webinfo"))
	      {
	        $description=`cat "$SearchDir/$file.webinfo"`;
              }
	    else
	      {
	        $description="No Description";
	      }
            $date=date("M d, Y",filemtime("$SearchDir/$file"));
	    print "<tr>\n";
            print "<td><a href=\"$SearchDir/$file\">$file</a></td>\n";
            print "<td><a href=\"$PHPSELF?DownloadFile=$SearchDir/$file\">$file</a></td>\n";
	    print "<td>$description</td>\n";
	    print "<td>$date</td>\n";
            printf("<td>%d %s</td>\n",$size,$extension);
	    print "</tr>\n";
          }
        else if (is_dir("$SearchDir/$file"))
          {
            if (is_file("$SearchDir/$file.webinfo"))
	      {
	        $description=`cat $SearchDir/$file.webinfo`;
              }
	    else
	      {
	        $description="Sub Directory";
	      }
            $date=date("M d, Y",filemtime("$SearchDir/$file"));
	    print "<tr bgcolor=yellow>\n";
            print "<td><a href=\"$PHPSELF?Directory=$Directory$file\">$file</a></td>\n";
            print "<td><a href=\"$PHPSELF?DownloadDir=$Directory$file\">ZIP Archive</a></td>\n";
	    print "<td>$description</td>\n";
	    print "<td>$date</td>\n";
            print "<td>Not Reported</td>\n";
	    print "</tr>\n";
          }
      }
  }

?>
</table>

<br>
To see a file, choose its name from the "View" column.  To download that 
file, click on its name from the "Download" column. <br><br>

Directories are listed in yellow.  Viewing a directory will let you see
the contents of that directory.  Downloading a directory will let you download
a .zip archive of all the files/subdirectories in that directory.  These zip
files can be large, so please verify that you really want the entire directory
before downloading. <br><br>

<br>

<?php
print "<a href=\"{$_SERVER['PHP_SELF']}\">To top of download tree</a>\n";
?>
</body>
</html> 
