]> bicyclesonthemoon.info Git - ott/bsta/blob - frame.1.pl
b175644aa98747e62aec6a109f36e2fdf570c9b7
[ott/bsta] / frame.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # /bsta/f
4 # viewer.pl is generated from viewer.1.pl.
5 #
6 # The frame interface
7 #
8 # Copyright (C) 2016, 2023  Balthasar SzczepaƄski
9 #
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.
14 #
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.
19 #
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/>.
22
23 use strict;
24 use utf8;
25 # use Encode::Locale ('decode_argv');
26 use Encode ('encode', 'decode');
27
28 ###PERL_LIB: use lib /botm/lib/bsta
29 use botm_common (
30         'read_header_env',
31         'url_query_decode',
32         'read_data_file',
33         'join_path',
34         'merge_url'
35 );
36 use bsta_lib (
37         'STATE', 'INTF_STATE',
38         'fail_method', 'fail_content_type', 'fail_open_file', 'fail_500', 'redirect',
39         'get_frame', 'get_password',
40         'merge_settings'
41 );
42
43 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
44
45 ###PERL_CGI_PATH:           CGI_PATH           = /bsta/
46
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
53
54 ###PERL_WWW_PATH:           WWW_PATH           = /botm/www/1190/bsta/
55
56 binmode STDIN,  ':encoding(UTF-8)';
57 binmode STDOUT, ':encoding(UTF-8)';
58 binmode STDERR, ':encoding(UTF-8)';
59 # decode_argv();
60
61 my $time = time();
62 srand ($time-$$);
63
64 my %http;
65 my %cgi;
66 my %frame_data;
67 my %default;
68 my %settings;
69 my %state;
70
71 my $method;
72 my $frame;
73 my $password;
74 my $password_ok;
75 my $IP;
76 my $access;
77 my $frame_indirect;
78 my $frame_path;
79 my $frame_data_path;
80 my $frame_file;
81 my $fh;
82 my $buffer;
83 my $ong_state;
84 my $last_frame;
85 my $r = 0;
86
87 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
88 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
89
90 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
91         $method = $1;
92 }
93 else{
94         exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
95 }
96
97 %http = read_header_env(\%ENV);
98 %cgi = url_query_decode($ENV{'QUERY_STRING'});
99
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                 foreach my $ind (keys %cgi_post) {
104                         $cgi{$ind} = $cgi_post{$ind};
105                 }
106         }
107         # multipart not supported
108         else{
109                 exit fail_content_type($method, $http{'content-type'});
110         }
111 }
112
113 $frame    = get_frame(\%cgi);
114 $password = get_password(\%cgi);
115
116 %settings  = read_data_file(DATA_SETTINGS_PATH());
117 %default   = read_data_file(DATA_DEFAULT_PATH());
118 %state     = read_data_file(DATA_STATE_PATH());
119
120 $ong_state  = int($state{'state'});
121 $last_frame = int($state{'last'});
122
123 if ($frame < 0) {
124         $frame = $state{'last'} + $frame +1;
125 }
126
127 $password_ok = ($password eq $settings{'password'});
128
129 $access = 0;
130 if (
131                 $password_ok || (
132                         ($ong_state >= STATE->{'waiting'}) &&
133                         ($frame <= $last_frame) &&
134                         ($frame >= 0)
135                 )
136         ) {
137         $access = 1;
138 }
139 elsif (
140         ($ong_state == STATE->{'inactive'}) &&
141         ($frame == 0)
142 ) {
143         my %story = read_data_file(DATA_STORY_PATH());
144         if (
145                 (int($story{'pass'}) == 1) &&
146                 (int($story{'state'}) == INTF_STATE->{'>|'})
147         ) {
148                 $access = 1;
149         }
150 }
151
152 $frame_indirect = !(
153         (!$access) || (
154                 ($frame <= $last_frame) &&
155                 ($ong_state > STATE->{'inactive'})
156         )
157 );
158
159 if ($access) {
160         $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
161         %frame_data = read_data_file($frame_data_path);
162         %frame_data = merge_settings(\%default, \%frame_data);
163 }
164 else {
165         %frame_data = read_data_file(DATA_NOACCESS_PATH());
166         %frame_data = merge_settings(\%default, \%frame_data);
167 }
168 if ($frame_data{'frame'} ne '') {
169         $frame_file = $frame_data{'frame'};
170 }
171 else {
172         $frame_file = sprintf(
173                 $settings{'frame'},
174                 $frame, $frame_data{'ext'}
175         );
176 }
177
178 unless ($frame_indirect) {
179         $frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
180         $r = open($fh, '<' , encode('locale_fs', $frame_path));
181         if ($r) {
182                 close($r);
183                 $frame_path = merge_url(
184                         {'path' => CGI_PATH()},
185                         {'path' => $frame_file}
186                 );
187                 exit redirect ($method, $frame_path, 303);
188         }
189 }
190 unless ($r) {
191         $frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
192         $r = open($fh, '<' , encode('locale_fs', $frame_path));
193         unless ($r) {
194                 exit fail_open_file($method, 'image file', $frame_file);
195         }
196 }
197 unless (binmode($fh)) {
198         close($fh);
199         exit fail_500("Can't switch file to binary mode.");
200 }
201
202 if (my @file_info = stat($frame_path)){
203         print 'Content-length: '.$file_info[7]."\n";
204 }
205 print 'Content-type: '.$frame_data{'content-type'}."\n";
206 unless (binmode STDOUT) {
207         close($fh);
208         exit fail_500("Can't switch output to binary mode.");
209 }
210 print "\n";
211
212 if($method ne 'HEAD'){
213         while (read ($fh, $buffer, 1024)) {
214                 print (STDOUT $buffer);
215         }
216 }
217 close($fh);