]> bicyclesonthemoon.info Git - ott/bsta/blob - viewer.1.pl
c7d74d9d4edb1401940671026c30cab5fe9a085f
[ott/bsta] / viewer.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # /bsta/v
4 # viewer is generated from viewer.1.pl.
5 #
6 # The viewer interface
7 #
8 # Copyright (C) 2016, 2017, 2019, 2020, 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         'open_encoded', '_x_encoded',
35         'http_header_status',
36         'merge_url'
37 );
38 use bsta_lib (
39         'STATE', 'TEXT_MODE', 'INTF_STATE',
40         'fail_method', 'fail_content_type', 'redirect',
41         'get_remote_addr', 'get_frame', 'get_password',
42         'merge_settings',
43         'print_viewer_page',
44         'write_index', 'write_static_goto', 'write_static_viewer_page',
45         'ong',
46         'read_frame_data', 'read_default', 'read_noaccess',
47         'read_words_list', 'read_settings', 'read_story', 'read_goto',
48         'read_state', 'write_state',
49         'get_page_file'
50 );
51
52 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
53
54 ###PERL_CGI_PATH:           CGI_PATH           = /bsta/
55 ###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v
56
57 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
58
59 ###PERL_WWW_PATH:           WWW_PATH           = /botm/www/
60
61 binmode STDIN,  ':encoding(UTF-8)';
62 binmode STDOUT, ':encoding(UTF-8)';
63 binmode STDERR, ':encoding(UTF-8)';
64 # decode_argv();
65
66 my $time = time();
67 srand ($time-$$);
68
69 my %http;
70 my %cgi;
71 my %frame_data;
72 my %prev_frame_data;
73 my %next_frame_data;
74 my %default;
75 my %settings;
76 my %state;
77 my %new_state;
78 my %goto_list;
79 my %words_data;
80
81 my $method;
82 my $frame;
83 my $prev_frame_data_path;
84 my $next_frame_data_path;
85 my $password;
86 my $password_ok;
87 my $IP;
88 my $access;
89 my $timer;
90 my $timer_unlocked;
91 my $fh;
92 my $show_command;
93 my $ongtime;
94 my $goto;
95 my $text_mode;
96 my $words_page;
97 my $words_data_path;
98 my $no_cgi;
99 my $force_redirect;
100
101 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
102 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
103
104 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
105         $method = $1;
106 }
107 else{
108         exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
109 }
110
111 %http = read_header_env(\%ENV);
112 %cgi = url_query_decode($ENV{'QUERY_STRING'});
113
114 if ($method eq 'POST') {
115         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
116                 my %cgi_post = url_query_decode( <STDIN> );
117                 %cgi = merge_settings(\%cgi, \%cgi_post);
118         }
119         # multipart not supported
120         else{
121                 exit fail_content_type($method, $http{'content-type'});
122         }
123 }
124 $no_cgi = (scalar (keys %cgi) == 0);
125
126 $IP = get_remote_addr();
127 $frame = get_frame(\%cgi);
128 $password = get_password(\%cgi);
129
130 %settings  = read_settings();
131 %default   = read_default();
132
133 if ($frame >= 0) {
134         %frame_data= read_frame_data($frame);
135 }
136
137 $password_ok = ($password eq $settings{'password'});
138
139 # state & activation logic
140 if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
141         if (flock($fh, 2)) {
142                 
143                 %state = read_state($fh);
144                 
145                 if ($frame < 0) {
146                         $frame = int($state{'last'}) + $frame +1;
147                         if ($frame >= 0) {
148                                 $force_redirect = 1;
149                                 %frame_data = read_frame_data($frame);
150                         }
151                 }
152                 
153                 if (
154                         (int($state{'state'}) == STATE->{'waiting'}) &&
155                         ($frame == int($state{'last'})) &&
156                         ($method ne 'HEAD') &&
157                         (!$password_ok)
158                 ) {
159                         # register IP for progress
160                         my %new_state = %state;
161                         unless (
162                                 ($state{'ip1'} eq $IP) ||
163                                 ($state{'ip2'} eq $IP) ||
164                                 ($state{'ip3'} eq $IP)
165                         )
166                         {
167                                 if ($state{'ip1'} eq '') {
168                                         $new_state{'ip1'} = $IP;
169                                 }
170                                 elsif ($state{'ip2'} eq '') {
171                                         $new_state{'ip2'} = $IP;
172                                 }
173                                 elsif ($state{'ip3'} eq '') {
174                                         $new_state{'ip3'} = $IP;
175                                         $new_state{'state'} = STATE->{'ready'};
176                                 }
177                                 else {
178                                         $new_state{'state'} = STATE->{'ready'};
179                                 }
180                                 if ($new_state{'state'} == STATE->{'ready'}) {
181                                         write_static_goto(\%new_state, \%settings, '');
182                                         write_static_viewer_page(
183                                                 $frame-1,
184                                                 \%new_state,
185                                                 \%settings,
186                                                 \%default,
187                                                 '', # frame data
188                                                 '', # prev frame data
189                                                 \%frame_data, # next frame data,
190                                                 '' # words data
191                                         );
192                                 }
193                                 write_state($fh, \%new_state);
194                         }
195                 }
196                 elsif (
197                         (int($state{'state'}) == STATE->{'inactive'}) &&
198                         ($frame == 1) &&
199                         (!$password_ok)
200                 ) {
201                         # ready to activate?
202                         # NOTE: at this point frame 0 is already ONGed.
203                         my %story;
204                         my $ong_time = int($settings{'firstongtime'});
205                         my $r;
206                         
207                         %story     = read_story();
208                         %goto_list = read_goto();
209                         
210                         if (
211                                 (int($story{'state'}) == INTF_STATE->{'>|'} ) &&
212                                 (int($story{'pass'}) == 1)
213                         ) {
214                                 # conditions met; ACTIVATE!
215                                 
216                                 # set initial state
217                                 $state{'state'} = STATE->{'waiting'};
218                                 $state{'last'}  = 1; 
219                                 $state{'ip1'}   = '0.0.0.0';
220                                 $state{'ip2'}   = '0.0.0.0';
221                                 $state{'ip3'}   = '';
222                                 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
223                                 $state{'ongtime'} = $ong_time;
224                                 
225                                 # prepare to ONG frame 1
226                                 
227                                 $r = ong(
228                                         1,         # frame ID
229                                         $time,     # ONG time,
230                                         $ong_time, # timer
231                                         0,         # update
232                                         0,         # print
233                                         \%settings,
234                                         \%default,
235                                         \%frame_data,
236                                         \%goto_list
237                                 );
238                                 if ($r) {
239                                         $r = write_index(\%state, \%settings);
240                                 }
241                                 if ($r) {
242                                         $r = write_static_goto(\%state, \%settings, \%goto_list);
243                                 }
244                                 if ($r) {
245                                         $r = write_state($fh, \%state);
246                                 }
247                                 unless ($r) {
248                                         # FAILED ONG! Story as if it was inactive!
249                                         $state{'state'} = STATE->{'inactive'};
250                                 }
251                         }
252                 }
253         }
254         else {
255                 # FAILED GET STATE! Story as if it was inactive!
256                 $state{'state'} = STATE->{'inactive'};
257         }
258         close ($fh);
259 }
260 else {
261         $state{'state'} = STATE->{'inactive'};
262 }
263
264 $access = (
265         $password_ok || (
266                 (int($state{'state'}) >= STATE->{'waiting'}) &&
267                 ($frame <= int($state{'last'})) &&
268                 ($frame >= 0)
269         )
270 );
271
272 if ($access) {
273         if ($no_cgi) {
274                 # no CGI - static page is OK
275                 if ($frame == 0) {
276                         exit redirect($method, CGI_PATH(), HTTP_STATUS->{'see_other'});
277                 }
278                 elsif ($frame < int($state{'last'})) {
279                         my $page_file = get_page_file($frame, \%frame_data, \%settings);
280                         if (_x_encoded('-f',
281                                 join_path(PATH_SEPARATOR(), WWW_PATH() , $page_file)
282                         )) {
283                                 my $static_url = merge_url(
284                                         {'path' => CGI_PATH()},
285                                         {'path' => $page_file}
286                                 );
287                                 exit redirect($method, $static_url, HTTP_STATUS->{'see_other'});
288                         }
289                 }
290         }
291         if ($force_redirect) {
292                 my $redirect_url = merge_url(
293                         {'path' => CGI_VIEWER_PATH()},
294                         {'path' => $frame}
295                 );
296                 unless ($no_cgi) {
297                         delete $cgi{'f'}; # to avoid infinite loop
298                         $redirect_url = merge_url(
299                                 {'path' => $redirect_url},
300                                 {'query' => \%cgi}
301                         );
302                 }
303                 exit redirect($method, $redirect_url, HTTP_STATUS->{'see_other'});
304         }
305         
306         if ($frame > 0) {
307                 %prev_frame_data = read_frame_data($frame-1, \%default);
308         }
309         else {
310                 %prev_frame_data = %default;
311         }
312         %next_frame_data = read_frame_data($frame+1, \%default);
313         %frame_data      = merge_settings(\%default, \%frame_data);
314 }
315 else {
316         # replace frame data with fail state replacement
317         %frame_data = read_noaccess(\%default);
318 }
319
320 $timer   = int($state{'nextong'}) - $time;
321 $ongtime = int($state{'ongtime'});
322 if($ongtime == 0) {
323         $ongtime = int($settings{'ongtime'})
324 }
325
326 $show_command = ($timer < ($ongtime*3600/3));
327 if ($state{'state'} >= STATE->{'ready'}) {
328         $timer_unlocked = 3;
329 }
330 elsif ($state{'ip3'} ne '') {
331         $timer_unlocked = 3;
332 }
333 elsif ($state{'ip2'} ne '') {
334         $timer_unlocked = 2;
335 }
336 elsif ($state{'ip1'} ne '') {
337         $timer_unlocked = 1;
338 }
339 else {
340         $timer_unlocked = 0;
341 }
342
343 $text_mode = int($cgi{'b'});
344 if($text_mode > TEXT_MODE->{'words'}) {
345         $text_mode = TEXT_MODE->{'normal'};
346 }
347 $words_page = int($cgi{'i'});
348 $goto = int($cgi{'g'});
349
350 %words_data = read_words_list(
351         $frame,
352         ($text_mode != TEXT_MODE->{'words'})
353 );
354
355 if (!$access) {
356         print http_header_status(HTTP_STATUS->{'forbidden'});
357 }
358 print "Content-type: text/html; charset=UTF-8\n\n";
359 if($method eq 'HEAD') {
360         exit;
361 }
362
363 print_viewer_page (
364         \*STDOUT,
365         {
366                 'launch'        => 0,
367                 'frame'         => $frame,
368                 'access'        => $access,
369                 'password_ok'   => $password_ok,
370                 'timer_unlocked'=> $timer_unlocked,
371                 'timer'         => $timer,
372                 'static'        => 0,
373                 'show_command'  => $show_command,
374                 'text_mode'     => $text_mode,
375                 'words_page'    => $words_page,
376                 'goto'          => $goto
377         },
378         \%state,
379         \%settings,
380         \%frame_data,
381         $access ? \%prev_frame_data : \%frame_data,
382         $access ? \%next_frame_data : \%frame_data,
383         \%words_data,
384 );