]> bicyclesonthemoon.info Git - ott/bsta/blob - frame.1.pl
launch index bugfix; reworked attachment
[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                 %cgi = merge_settings(\%cgi, \%cgi_post);
104         }
105         # multipart not supported
106         else{
107                 exit fail_content_type($method, $http{'content-type'});
108         }
109 }
110
111 $frame    = get_frame(\%cgi);
112 $password = get_password(\%cgi);
113
114 %settings  = read_data_file(DATA_SETTINGS_PATH());
115 %default   = read_data_file(DATA_DEFAULT_PATH());
116 %state     = read_data_file(DATA_STATE_PATH());
117
118 $ong_state  = int($state{'state'});
119 $last_frame = int($state{'last'});
120
121 if ($frame < 0) {
122         $frame = $state{'last'} + $frame +1;
123 }
124
125 $password_ok = ($password eq $settings{'password'});
126
127 $access = 0;
128 if (
129                 $password_ok || (
130                         ($ong_state >= STATE->{'waiting'}) &&
131                         ($frame <= $last_frame) &&
132                         ($frame >= 0)
133                 )
134         ) {
135         $access = 1;
136 }
137 elsif (
138         ($ong_state == STATE->{'inactive'}) &&
139         ($frame == 0)
140 ) {
141         my %story = read_data_file(DATA_STORY_PATH());
142         if (
143                 (int($story{'pass'}) == 1) &&
144                 (int($story{'state'}) == INTF_STATE->{'>|'})
145         ) {
146                 $access = 1;
147         }
148 }
149
150 $frame_indirect = !(
151         (!$access) || (
152                 ($frame <= $last_frame) &&
153                 ($ong_state > STATE->{'inactive'})
154         )
155 );
156
157 if ($access) {
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);
161 }
162 else {
163         %frame_data = read_data_file(DATA_NOACCESS_PATH());
164         %frame_data = merge_settings(\%default, \%frame_data);
165 }
166 if ($frame_data{'frame'} ne '') {
167         $frame_file = $frame_data{'frame'};
168 }
169 else {
170         $frame_file = sprintf(
171                 $settings{'frame'},
172                 $frame, $frame_data{'ext'}
173         );
174 }
175
176 unless ($frame_indirect) {
177         $frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
178         $r = open($fh, '<' , encode('locale_fs', $frame_path));
179         if ($r) {
180                 close($r);
181                 $frame_path = merge_url(
182                         {'path' => CGI_PATH()},
183                         {'path' => $frame_file}
184                 );
185                 exit redirect ($method, $frame_path, 303);
186         }
187 }
188 unless ($r) {
189         $frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
190         $r = open($fh, '<' , encode('locale_fs', $frame_path));
191         unless ($r) {
192                 exit fail_open_file($method, 'image file', $frame_file);
193         }
194 }
195 unless (binmode($fh)) {
196         close($fh);
197         exit fail_500("Can't switch file to binary mode.");
198 }
199
200 if (my @file_info = stat($frame_path)){
201         print 'Content-length: '.$file_info[7]."\n";
202 }
203 print 'Content-type: '.$frame_data{'content-type'}."\n";
204 unless (binmode STDOUT) {
205         close($fh);
206         exit fail_500("Can't switch output to binary mode.");
207 }
208 print "\n";
209
210 if($method ne 'HEAD'){
211         while (read ($fh, $buffer, 1024)) {
212                 print (STDOUT $buffer);
213         }
214 }
215 close($fh);