1 ###RUN_PERL: #!/usr/bin/perl
4 # viewer.pl is generated from viewer.1.pl.
8 # Copyright (C) 2016, 2023, 2024 Balthasar SzczepaĆski
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Affero General Public License as
12 # published by the Free Software Foundation, either version 3 of the
13 # License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Affero General Public License for more details.
20 # You should have received a copy of the GNU Affero General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 # use Encode::Locale ('decode_argv');
26 use Encode ('encode', 'decode');
28 ###PERL_LIB: use lib /botm/lib/bsta
31 'fail_method', 'fail_content_type', 'fail_open_file', 'fail_500', 'redirect',
36 'open_encoded', 'stat_encoded',
37 'http_header_line', 'http_header_content_length', 'http_header_content_disposition',
41 'STATE', 'INTF_STATE',
42 'get_frame', 'get_password',
44 'read_frame_data', 'read_default', 'read_noaccess',
45 'read_settings', 'read_state', 'read_story'
48 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
50 ###PERL_CGI_PATH: CGI_PATH = /bsta/
51 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta
52 ###PERL_WWW_PATH: WWW_PATH = /botm/www/1190/bsta/
54 binmode STDIN, ':encoding(UTF-8)';
55 binmode STDOUT, ':encoding(UTF-8)';
56 binmode STDERR, ':encoding(UTF-8)';
84 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
85 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
87 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
91 exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
94 %http = read_header_env(\%ENV);
95 %cgi = url_query_decode($ENV{'QUERY_STRING'});
97 if ($method eq 'POST') {
98 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
99 my %cgi_post = url_query_decode( <STDIN> );
100 %cgi = merge_settings(\%cgi, \%cgi_post);
102 # multipart not supported
104 exit fail_content_type($method, $http{'content-type'});
108 $frame = get_frame(\%cgi);
109 $password = get_password(\%cgi);
111 %settings = read_settings();
112 %default = read_default();
113 %state = read_state();
115 $ong_state = int($state{'state'});
116 $last_frame = int($state{'last'});
119 $frame = $state{'last'} + $frame +1;
122 $password_ok = ($password eq $settings{'password'});
127 ($ong_state >= STATE->{'waiting'}) &&
128 ($frame <= $last_frame) &&
135 ($ong_state == STATE->{'inactive'}) &&
138 my %story = read_story();
140 (int($story{'pass'}) == 1) &&
141 (int($story{'state'}) == INTF_STATE->{'>|'})
149 ($frame <= $last_frame) &&
150 ($ong_state > STATE->{'inactive'})
155 %frame_data = read_frame_data($frame, \%default);
158 %frame_data = read_noaccess(\%default);
160 $frame_file = get_frame_file($frame, \%frame_data, \%settings);
163 $frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
164 $r = open_encoded($fh, '<' , $frame_path);
167 $frame_path = merge_url(
168 {'path' => CGI_PATH()},
169 {'path' => $frame_file}
171 exit redirect ($method, $frame_path, HTTP_STATUS->{'see_other'});
175 $frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
176 $r = open_encoded($fh, '<' , $frame_path);
178 exit fail_open_file($method, 'image file', $frame_file);
181 unless (binmode($fh)) {
183 exit fail_500("Can't switch file to binary mode.");
186 if (my @file_info = stat_encoded($frame_path)){
187 print http_header_content_length($file_info[7]);
189 if ($frame_data{'content-type'} ne '') {
190 print http_header_line('content-type', $frame_data{'content-type'});
192 if ($frame_file ne '') {
193 print http_header_content_disposition('inline', $frame_file);
195 unless (binmode STDOUT) {
197 exit fail_500("Can't switch output to binary mode.");
201 if($method ne 'HEAD'){
202 while (read ($fh, $buffer, 1024)) {
203 print (STDOUT $buffer);