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