1 ###RUN_PERL: #!/usr/bin/perl
4 # viewer.pl is generated from viewer.1.pl.
8 # Copyright (C) 2016, 2023 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
37 'STATE', 'INTF_STATE',
38 'fail_method', 'fail_content_type', 'fail_open_file', 'fail_500', 'redirect',
39 'get_frame', 'get_password',
43 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
45 ###PERL_CGI_PATH: CGI_PATH = /bsta/
47 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta
48 ###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default
49 ###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
50 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
51 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
52 ###PERL_DATA_STORY_PATH: DATA_STORY_PATH = /botm/data/bsta/story
54 ###PERL_WWW_PATH: WWW_PATH = /botm/www/1190/bsta/
56 binmode STDIN, ':encoding(UTF-8)';
57 binmode STDOUT, ':encoding(UTF-8)';
58 binmode STDERR, ':encoding(UTF-8)';
87 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
88 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
90 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
94 exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
97 %http = read_header_env(\%ENV);
98 %cgi = url_query_decode($ENV{'QUERY_STRING'});
100 if ($method eq 'POST') {
101 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
102 my %cgi_post = url_query_decode( <STDIN> );
103 %cgi = merge_settings(\%cgi, \%cgi_post);
105 # multipart not supported
107 exit fail_content_type($method, $http{'content-type'});
111 $frame = get_frame(\%cgi);
112 $password = get_password(\%cgi);
114 %settings = read_data_file(DATA_SETTINGS_PATH());
115 %default = read_data_file(DATA_DEFAULT_PATH());
116 %state = read_data_file(DATA_STATE_PATH());
118 $ong_state = int($state{'state'});
119 $last_frame = int($state{'last'});
122 $frame = $state{'last'} + $frame +1;
125 $password_ok = ($password eq $settings{'password'});
130 ($ong_state >= STATE->{'waiting'}) &&
131 ($frame <= $last_frame) &&
138 ($ong_state == STATE->{'inactive'}) &&
141 my %story = read_data_file(DATA_STORY_PATH());
143 (int($story{'pass'}) == 1) &&
144 (int($story{'state'}) == INTF_STATE->{'>|'})
152 ($frame <= $last_frame) &&
153 ($ong_state > STATE->{'inactive'})
158 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
159 %frame_data = read_data_file($frame_data_path);
160 %frame_data = merge_settings(\%default, \%frame_data);
163 %frame_data = read_data_file(DATA_NOACCESS_PATH());
164 %frame_data = merge_settings(\%default, \%frame_data);
166 if ($frame_data{'frame'} ne '') {
167 $frame_file = $frame_data{'frame'};
170 $frame_file = sprintf(
172 $frame, $frame_data{'ext'}
176 unless ($frame_indirect) {
177 $frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
178 $r = open($fh, '<' , encode('locale_fs', $frame_path));
181 $frame_path = merge_url(
182 {'path' => CGI_PATH()},
183 {'path' => $frame_file}
185 exit redirect ($method, $frame_path, 303);
189 $frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
190 $r = open($fh, '<' , encode('locale_fs', $frame_path));
192 exit fail_open_file($method, 'image file', $frame_file);
195 unless (binmode($fh)) {
197 exit fail_500("Can't switch file to binary mode.");
200 if (my @file_info = stat($frame_path)){
201 print 'Content-length: '.$file_info[7]."\n";
203 print 'Content-type: '.$frame_data{'content-type'}."\n";
204 unless (binmode STDOUT) {
206 exit fail_500("Can't switch output to binary mode.");
210 if($method ne 'HEAD'){
211 while (read ($fh, $buffer, 1024)) {
212 print (STDOUT $buffer);