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