]> bicyclesonthemoon.info Git - ott/bsta/blob - frame.1.pl
viewer redirects to static page; info includes static page;
[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, 2024  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         'HTTP_STATUS',
31         'read_header_env',
32         'url_query_decode',
33         'read_data_file',
34         'join_path',
35         'merge_url',
36         'open_encoded', 'stat_encoded',
37         'http_header_line', 'http_header_content_length', 'http_header_content_disposition'
38 );
39 use bsta_lib (
40         'STATE', 'INTF_STATE',
41         'fail_method', 'fail_content_type', 'fail_open_file', 'fail_500', 'redirect',
42         'get_frame', 'get_password',
43         'merge_settings'
44 );
45
46 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
47
48 ###PERL_CGI_PATH:           CGI_PATH           = /bsta/
49
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
56
57 ###PERL_WWW_PATH:           WWW_PATH           = /botm/www/1190/bsta/
58
59 binmode STDIN,  ':encoding(UTF-8)';
60 binmode STDOUT, ':encoding(UTF-8)';
61 binmode STDERR, ':encoding(UTF-8)';
62 # decode_argv();
63
64 my $time = time();
65 srand ($time-$$);
66
67 my %http;
68 my %cgi;
69 my %frame_data;
70 my %default;
71 my %settings;
72 my %state;
73
74 my $method;
75 my $frame;
76 my $password;
77 my $password_ok;
78 my $IP;
79 my $access;
80 my $try_onged;
81 my $frame_path;
82 my $frame_data_path;
83 my $frame_file;
84 my $fh;
85 my $buffer;
86 my $ong_state;
87 my $last_frame;
88 my $r = 0;
89
90 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
91 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
92
93 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
94         $method = $1;
95 }
96 else{
97         exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
98 }
99
100 %http = read_header_env(\%ENV);
101 %cgi = url_query_decode($ENV{'QUERY_STRING'});
102
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);
107         }
108         # multipart not supported
109         else{
110                 exit fail_content_type($method, $http{'content-type'});
111         }
112 }
113
114 $frame    = get_frame(\%cgi);
115 $password = get_password(\%cgi);
116
117 %settings  = read_data_file(DATA_SETTINGS_PATH());
118 %default   = read_data_file(DATA_DEFAULT_PATH());
119 %state     = read_data_file(DATA_STATE_PATH());
120
121 $ong_state  = int($state{'state'});
122 $last_frame = int($state{'last'});
123
124 if ($frame < 0) {
125         $frame = $state{'last'} + $frame +1;
126 }
127
128 $password_ok = ($password eq $settings{'password'});
129
130 $access = 0;
131 if (
132                 $password_ok || (
133                         ($ong_state >= STATE->{'waiting'}) &&
134                         ($frame <= $last_frame) &&
135                         ($frame >= 0)
136                 )
137         ) {
138         $access = 1;
139 }
140 elsif (
141         ($ong_state == STATE->{'inactive'}) &&
142         ($frame == 0)
143 ) {
144         my %story = read_data_file(DATA_STORY_PATH());
145         if (
146                 (int($story{'pass'}) == 1) &&
147                 (int($story{'state'}) == INTF_STATE->{'>|'})
148         ) {
149                 $access = 1;
150         }
151 }
152
153 $try_onged = (
154         (!$access) || (
155                 ($frame <= $last_frame) &&
156                 ($ong_state > STATE->{'inactive'})
157         )
158 );
159
160 if ($access) {
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);
164 }
165 else {
166         %frame_data = read_data_file(DATA_NOACCESS_PATH());
167         %frame_data = merge_settings(\%default, \%frame_data);
168 }
169 if ($frame_data{'frame'} ne '') {
170         $frame_file = $frame_data{'frame'};
171 }
172 else {
173         $frame_file = sprintf(
174                 $settings{'frame'},
175                 $frame, $frame_data{'ext'}
176         );
177 }
178
179 if ($try_onged) {
180         $frame_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
181         $r = open_encoded($fh, '<' , $frame_path);
182         if ($r) {
183                 close($r);
184                 $frame_path = merge_url(
185                         {'path' => CGI_PATH()},
186                         {'path' => $frame_file}
187                 );
188                 exit redirect ($method, $frame_path, HTTP_STATUS->{'see_other'});
189         }
190 }
191 unless ($r) {
192         $frame_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
193         $r = open_encoded($fh, '<' , $frame_path);
194         unless ($r) {
195                 exit fail_open_file($method, 'image file', $frame_file);
196         }
197 }
198 unless (binmode($fh)) {
199         close($fh);
200         exit fail_500("Can't switch file to binary mode.");
201 }
202
203 if (my @file_info = stat_encoded($frame_path)){
204         print http_header_content_length($file_info[7]);
205 }
206 if ($frame_data{'content-type'} ne '') {
207         print http_header_line('content-type', $frame_data{'content-type'});
208 }
209 if ($frame_file ne '') {
210         print http_header_content_disposition('inline', $frame_file);
211 }
212 unless (binmode STDOUT) {
213         close($fh);
214         exit fail_500("Can't switch output to binary mode.");
215 }
216 print "\n";
217
218 if($method ne 'HEAD'){
219         while (read ($fh, $buffer, 1024)) {
220                 print (STDOUT $buffer);
221         }
222 }
223 close($fh);