#
# The frame interface
#
-# Copyright (C) 2016, 2023 Balthasar Szczepañski
+# Copyright (C) 2016, 2023, 2024 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
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
-#use warnings;
+use utf8;
+# use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
###PERL_LIB: use lib /botm/lib/bsta
+use botm_common (
+ 'HTTP_STATUS',
+ 'read_header_env',
+ 'url_query_decode',
+ 'join_path',
+ 'merge_url',
+ 'open_encoded', 'stat_encoded',
+ 'http_header_line', 'http_header_content_length', 'http_header_content_disposition'
+);
+use bsta_lib (
+ 'STATE', 'INTF_STATE',
+ 'fail_method', 'fail_content_type', 'fail_open_file', 'fail_500', 'redirect',
+ 'get_frame', 'get_password',
+ 'merge_settings',
+ 'get_frame_file',
+ 'read_frame_data', 'read_default', 'read_noaccess',
+ 'read_settings', 'read_state', 'read_story'
+);
+
+###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
+
+###PERL_CGI_PATH: CGI_PATH = /bsta/
+###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta
+###PERL_WWW_PATH: WWW_PATH = /botm/www/1190/bsta/
-use bsta_lib qw(failpage gethttpheader getcgi readdatafile);
+binmode STDIN, ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+# decode_argv();
-###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
-###PERL_DATA_STORY_PATH: DATA_STORY_PATH = /botm/data/bsta/story
+my $time = time();
+srand ($time-$$);
my %http;
my %cgi;
-my %framedata;
+my %frame_data;
my %default;
my %settings;
my %state;
-my $time = time();
-srand ($time-$$);
-
my $method;
my $frame;
my $password;
-my $passwordOK;
+my $password_ok;
my $IP;
my $access;
-my $framepath;
-my $framefile;
+my $try_onged;
+my $frame_path;
+my $frame_file;
+my $fh;
my $buffer;
-my @fileinfo;
+my $ong_state;
+my $last_frame;
+my $r = 0;
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;
+ $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);
+ exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
}
-%http = gethttpheader (\%ENV);
-%cgi = getcgi($ENV{'QUERY_STRING'});
+%http = read_header_env(\%ENV);
+%cgi = url_query_decode($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};
- }
+ my %cgi_post = url_query_decode( <STDIN> );
+ %cgi = merge_settings(\%cgi, \%cgi_post);
}
# multipart not supported
else{
- exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
+ exit fail_content_type($method, $http{'content-type'});
}
}
-if ($cgi{'f'} =~ /^(.+)$/) {
- $frame=int($1);
-}
-elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
- $frame=int($1);
-}
-else {
- $frame = 0;
-}
+$frame = get_frame(\%cgi);
+$password = get_password(\%cgi);
-if ($cgi{'p'} =~ /^(.+)$/) {
- $password=$1;
-}
-else {
- $password='';
-}
+%settings = read_settings();
+%default = read_default();
+%state = read_state();
+
+$ong_state = int($state{'state'});
+$last_frame = int($state{'last'});
-%settings=readdatafile(DATA_SETTINGS_PATH);
-%default=readdatafile(DATA_DEFAULT_PATH);
-%state=readdatafile(DATA_STATE_PATH);
-if($frame<0) {
+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};
+
+$password_ok = ($password eq $settings{'password'});
+
+$access = 0;
+if (
+ $password_ok || (
+ ($ong_state >= STATE->{'waiting'}) &&
+ ($frame <= $last_frame) &&
+ ($frame >= 0)
+ )
+ ) {
+ $access = 1;
+}
+elsif (
+ ($ong_state == STATE->{'inactive'}) &&
+ ($frame == 0)
+) {
+ my %story = read_story();
+ if (
+ (int($story{'pass'}) == 1) &&
+ (int($story{'state'}) == INTF_STATE->{'>|'})
+ ) {
+ $access = 1;
}
}
-if($password eq $settings{'password'}){
- $passwordOK = 1;
-}
-else{
- $passwordOK = 0;
-}
-if ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >= 0)) {
- $access=1;
+$try_onged = (
+ (!$access) || (
+ ($frame <= $last_frame) &&
+ ($ong_state > STATE->{'inactive'})
+ )
+);
+
+if ($access) {
+ %frame_data = read_frame_data($frame, \%default);
}
else {
- $access=0;
- if(int($state{'state'}) == 0) {
- my %story=readdatafile(DATA_STORY_PATH);
- if (
- (int($story{'pass'}) == 1) &&
- (int($story{'state'}) == 17)
- ) {
- $access = 1;
- }
+ %frame_data = read_noaccess(\%default);
+}
+$frame_file = get_frame_file($frame, \%frame_data, \%settings);
+
+if ($try_onged) {
+ $frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
+ $r = open_encoded($fh, '<' , $frame_path);
+ if ($r) {
+ close($r);
+ $frame_path = merge_url(
+ {'path' => CGI_PATH()},
+ {'path' => $frame_file}
+ );
+ exit redirect ($method, $frame_path, HTTP_STATUS->{'see_other'});
}
}
-
-if($access){
- $framepath=DATA_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'});
-}
-else {
- %framedata = readdatafile(DATA_NOACCESS_PATH);
- foreach my $ind (keys %default) {
- unless(defined($framedata{$ind})){
- $framedata{$ind}=$default{$ind};
- }
+unless ($r) {
+ $frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
+ $r = open_encoded($fh, '<' , $frame_path);
+ unless ($r) {
+ exit fail_open_file($method, 'image file', $frame_file);
}
- $framepath=DATA_PATH.$framedata{'frame'};
+}
+unless (binmode($fh)) {
+ close($fh);
+ exit fail_500("Can't switch file to binary mode.");
}
-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 @file_info = stat_encoded($frame_path)){
+ print http_header_content_length($file_info[7]);
}
-if (my @fileinfo = stat($framepath)){
- print 'Content-length: '.$fileinfo[7]."\n";
+if ($frame_data{'content-type'} ne '') {
+ print http_header_line('content-type', $frame_data{'content-type'});
+}
+if ($frame_file ne '') {
+ print http_header_content_disposition('inline', $frame_file);
+}
+unless (binmode STDOUT) {
+ close($fh);
+ exit fail_500("Can't switch output to binary mode.");
}
-print 'Content-type: '.$framedata{'content-type'}."\n";
print "\n";
+
if($method ne 'HEAD'){
- while (read ($framefile,$buffer,1024)) {
+ while (read ($fh, $buffer, 1024)) {
print (STDOUT $buffer);
}
}
-close($framefile);
+close($fh);