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
36 'open_encoded', 'stat_encoded',
37 'http_header_line', 'http_header_content_length', 'http_header_content_disposition'
40 'STATE', 'INTF_STATE',
41 'fail_method', 'fail_content_type', 'fail_open_file', 'fail_500', 'redirect',
42 'get_frame', 'get_password',
46 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
48 ###PERL_CGI_PATH: CGI_PATH = /bsta/
50 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta
51 ###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default
52 ###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
53 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
54 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
55 ###PERL_DATA_STORY_PATH: DATA_STORY_PATH = /botm/data/bsta/story
57 ###PERL_WWW_PATH: WWW_PATH = /botm/www/1190/bsta/
59 binmode STDIN, ':encoding(UTF-8)';
60 binmode STDOUT, ':encoding(UTF-8)';
61 binmode STDERR, ':encoding(UTF-8)';
90 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
91 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
93 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
97 exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
100 %http = read_header_env(\%ENV);
101 %cgi = url_query_decode($ENV{'QUERY_STRING'});
103 if ($method eq 'POST') {
104 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
105 my %cgi_post = url_query_decode( <STDIN> );
106 %cgi = merge_settings(\%cgi, \%cgi_post);
108 # multipart not supported
110 exit fail_content_type($method, $http{'content-type'});
114 $frame = get_frame(\%cgi);
115 $password = get_password(\%cgi);
117 %settings = read_data_file(DATA_SETTINGS_PATH());
118 %default = read_data_file(DATA_DEFAULT_PATH());
119 %state = read_data_file(DATA_STATE_PATH());
121 $ong_state = int($state{'state'});
122 $last_frame = int($state{'last'});
125 $frame = $state{'last'} + $frame +1;
128 $password_ok = ($password eq $settings{'password'});
133 ($ong_state >= STATE->{'waiting'}) &&
134 ($frame <= $last_frame) &&
141 ($ong_state == STATE->{'inactive'}) &&
144 my %story = read_data_file(DATA_STORY_PATH());
146 (int($story{'pass'}) == 1) &&
147 (int($story{'state'}) == INTF_STATE->{'>|'})
155 ($frame <= $last_frame) &&
156 ($ong_state > STATE->{'inactive'})
161 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
162 %frame_data = read_data_file($frame_data_path);
163 %frame_data = merge_settings(\%default, \%frame_data);
166 %frame_data = read_data_file(DATA_NOACCESS_PATH());
167 %frame_data = merge_settings(\%default, \%frame_data);
169 if ($frame_data{'frame'} ne '') {
170 $frame_file = $frame_data{'frame'};
173 $frame_file = sprintf(
175 $frame, $frame_data{'ext'}
180 $frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
181 $r = open_encoded($fh, '<' , $frame_path);
184 $frame_path = merge_url(
185 {'path' => CGI_PATH()},
186 {'path' => $frame_file}
188 exit redirect ($method, $frame_path, HTTP_STATUS->{'see_other'});
192 $frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
193 $r = open_encoded($fh, '<' , $frame_path);
195 exit fail_open_file($method, 'image file', $frame_file);
198 unless (binmode($fh)) {
200 exit fail_500("Can't switch file to binary mode.");
203 if (my @file_info = stat_encoded($frame_path)){
204 print http_header_content_length($file_info[7]);
206 if ($frame_data{'content-type'} ne '') {
207 print http_header_line('content-type', $frame_data{'content-type'});
209 if ($frame_file ne '') {
210 print http_header_content_disposition('inline', $frame_file);
212 unless (binmode STDOUT) {
214 exit fail_500("Can't switch output to binary mode.");
218 if($method ne 'HEAD'){
219 while (read ($fh, $buffer, 1024)) {
220 print (STDOUT $buffer);