#!/usr/bin/perl
# Paul Sladen  2002-May-18
# Returns a file to a browser
# (rfc1806 seems to be the key, and describes the Content-disposition: header)

#$filename = "foobar.txt";
#$filename = "qfp.pdf";
$filename = "self-download.pl";

#$directory = "/home/sladen/public_html/hidden";
$directory = "/home/sladen/public_html/cgi-bin";

$full_filename = "$directory/$filename";

# if file exists;  read it in and call unmunge() to decrypt it
if (-f $full_filename)
{
    open(FP, "<$full_filename");
    $raw_data = join("", <FP>);
    close(FP);
    
    $content = &unmunge($raw_data);
    $content_length = length($content);

    # headers; change the "x-foobar" to "pdf" or some such
    #print "Content-type: application/x-foobar\r\n";
    #print "Content-type: application/pdf\r\n";
    print "Content-type: application/x-perl\r\n";
    print "Content-disposition: inline; filename=$filename\r\n";
    print "Content-length: $content_length\r\n";
    print "\r\n";
    print "$content";
}
else
{
    # this currently returns the full qualified filename, might want to change that.
    $error = "File doesn't exist! \`$full_filename\'";
    print "Content-type: text/html\r\n";
    print "\r\n";
    print "<html><body bgcolor=\"white\">$error</body></html>\r\n";
    die($error);
}

# hack this to do any decryption;  data comes in @_[0];
sub unmunge
{
    return @_[0];
}

# end-of-file


