###RUN_PERL: #!/usr/bin/perl

# /bsta/a
# attach.pl is generated from attach.1.pl.
#
# The attachment interface
#
# Copyright (C) 2016, 2023  Balthasar Szczepañski
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
#use warnings;
###PERL_LIB: use lib /botm/lib/bsta

use bsta_lib qw(failpage gethttpheader getcgi readdatafile);

###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta
###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state

my %http;
my %cgi;
my %settings;
my %state;
my %filedata;

my $time = time();
srand ($time-$$);

my $method;
my $ID;
my $frame;
my $password;
my $passwordOK;
my $IP;
my $buffer;
my @fileinfo;
my $file;
my $filepath;
my $direct;

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;

if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
	$method=$1;
}
else{
	exit failpage("Status: 405 Method Not Allowed\nAllow: GET, POST, HEAD\n","405 Method Not Allowed","The interface does not support the $ENV{'REQUEST_METHOD'} method.",$method);
}

%http = gethttpheader (\%ENV);
%cgi = getcgi($ENV{'QUERY_STRING'});

if ($method eq 'POST') {
	if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
		my %cgipost=getcgi( <STDIN> );
		foreach my $ind (keys %cgipost) {
			$cgi{$ind}=$cgipost{$ind};
		}
	}
	# multipart not supported
	else{
		exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
	}
}

# print "content-type: text/plain\n\n";

if ($cgi{'i'} =~ /^(.+)$/) {
	$ID=int($1);
}
elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
	$ID=int($1);
}
else {
	$ID = 0;
}

if ($cgi{'p'} =~ /^(.+)$/) {
	$password=$1;
}
else {
	$password='';
}

%settings=readdatafile(DATA_SETTINGS_PATH);
%state=readdatafile(DATA_STATE_PATH);
%filedata=readdatafile(DATA_PATH.'a'.$ID);
if ($filedata{'frame'} ne '') {
	$frame=int($filedata{'frame'});
}
else {
	$frame = -1;
}

if($password eq $settings{'password'}){
	$passwordOK = 1;
}
else{
	$passwordOK = 0;
}

if ($filedata{'filename'} ne '' && ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >=0))) {
}
else {
	exit failpage("Status: 404 Not Found\n","404 Not Found"," Attachment ".$ID." not found.");
}

if ($filedata{'content'} ne '') {
	$direct=1;
}
else {
	$direct = 0;
	$filepath=DATA_PATH.$filedata{'filename'};
	open($file,'<',$filepath) or exit failpage("Status: 404 Not Found\n","404 Not Found"," Attachment ".$ID." not found.");
	unless(binmode($file)) {
		close($file);
		return failpage("Status: 500 Internal Server Error\n","500 Internal Server Error"," Can't switch to binary mode.");
	}
	if (my @fileinfo = stat($filepath)){
		print 'Content-length: '.$fileinfo[7]."\n";
	}
}
print 'Content-type: '.$filedata{'content-type'}."\n";
print 'Content-disposition: attachment; filename="'.$filedata{'filename'}.'"'."\n";
print "\n";
if($method ne 'HEAD'){
	if($direct) {
		print $filedata{'content'};
	}
	else {
		while (read ($file,$buffer,1024)) {
			print (STDOUT $buffer);
		}
	}
}
if (!$direct) {
	close($file);
}

