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

# /bsta/f
# viewer.pl is generated from viewer.1.pl.
#
# The frame 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_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
###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 %framedata;
my %default;
my %settings;
my %state;

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

my $method;
my $frame;
my $password;
my $passwordOK;
my $IP;
my $access;
my $framepath;
my $framefile;
my $buffer;
my @fileinfo;

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'}.");
	}
}

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

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

%settings=readdatafile(DATA_SETTINGS_PATH);
%default=readdatafile(DATA_DEFAULT_PATH);
%state=readdatafile(DATA_STATE_PATH);
if($frame<0) {
	$frame = $state{'last'} + $frame +1;
}
%framedata=readdatafile(DATA_PATH.$frame);
foreach my $ind (keys %default) {
	unless(defined($framedata{$ind})){
		$framedata{$ind}=$default{$ind};
	}
}
if($password eq $settings{'password'}){
	$passwordOK = 1;
}
else{
	$passwordOK = 0;
}

if ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >= 0)) {
	$access=1;
}
else {
	$access=0;
	%framedata = readdatafile(DATA_NOACCESS_PATH);
	foreach my $ind (keys %default) {
		unless(defined($framedata{$ind})){
			$framedata{$ind}=$default{$ind};
		}
	}
}

if($access){
	$framepath=DATA_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'});
}
else {
	$framepath=DATA_PATH.$framedata{'frame'};
}

open($framefile,'<',$framepath) or exit failpage("Status: 404 Not Found\n","404 Not Found"," Can't open image file.");
unless(binmode($framefile)) {
	close($framefile);
	exit failpage("Status: 500 Internal Server Error\n","500 Internal Server Error"," Can't switch to binary mode.");
}
if (my @fileinfo = stat($framepath)){
	print 'Content-length: '.$fileinfo[7]."\n";
}
print 'Content-type: '.$framedata{'content-type'}."\n";
print "\n";
if($method ne 'HEAD'){
	while (read ($framefile,$buffer,1024)) {
		print (STDOUT $buffer);
	}
}
close($framefile);
