]> bicyclesonthemoon.info Git - ott/bsta/blob - viewer.1.pl
continued bugfix of 2words & index & launch
[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  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         'read_data_file', 'write_data_file',
32         'url_query_decode',
33         'join_path'
34 );
35 use bsta_lib (
36         'STATE', 'TEXT_MODE', 'INTF_STATE',
37         'fail_method', 'fail_content_type',
38         'get_remote_addr', 'get_frame', 'get_password',
39         'merge_settings',
40         'print_viewer_page', 'write_index'
41 );
42 use File::Copy;
43
44 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
45
46 ###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta/
47 ###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
48 ###PERL_DATA_LIST_PATH:     DATA_LIST_PATH     = /botm/data/bsta/list
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 %next_frame_data;
68 my %default;
69 my %settings;
70 my %state;
71 my %new_state;
72 my %goto_list;
73
74 my $method;
75 my $frame;
76 my $frame_data_path;
77 my $next_frame_data_path;
78 my $password;
79 my $password_ok;
80 my $IP;
81 my $access;
82 my $timer;
83 my $timer_unlocked;
84 my $state_file;
85 my $show_command;
86 my $ongtime;
87 my $text_mode;
88
89 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
90 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
91
92 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
93         $method = $1;
94 }
95 else{
96         exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
97 }
98
99 %http = read_header_env(\%ENV);
100 %cgi = url_query_decode($ENV{'QUERY_STRING'});
101 if ($method eq 'POST') {
102         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
103                 my %cgi_post=url_query_decode( <STDIN> );
104                 foreach my $ind (keys %cgi_post) {
105                         $cgi{$ind} = $cgi_post{$ind};
106                 }
107         }
108         # multipart not supported
109         else{
110                 exit fail_content_type($http{'content-type'}, $method);
111         }
112 }
113 $IP = get_remote_addr();
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
120 if ($frame >= 0) {
121         $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
122         %frame_data= read_data_file($frame_data_path);
123 }
124
125 $password_ok = ($password eq $settings{'password'});
126
127 # state & activation logic
128 if (open ($state_file, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
129         if (flock($state_file, 2)) {
130                 
131                 %state = read_data_file($state_file);
132                 
133                 if ($frame < 0) {
134                         $frame = int($state{'last'}) + $frame +1;
135                         $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
136                         %frame_data = read_data_file($frame_data_path);
137                 }
138                 
139                 if (
140                         (int($state{'state'}) == STATE->{'waiting'}) &&
141                         ($frame == int($state{'last'})) &&
142                         ($method ne 'HEAD') &&
143                         (!$password_ok)
144                 ) {
145                         # register IP for progress
146                         my %new_state = %state;
147                         unless (
148                                 ($state{'ip1'} eq $IP) ||
149                                 ($state{'ip2'} eq $IP) ||
150                                 ($state{'ip3'} eq $IP)
151                         )
152                         {
153                                 if ($state{'ip1'} eq '') {
154                                         $new_state{'ip1'} = $IP;
155                                 }
156                                 elsif ($state{'ip2'} eq '') {
157                                         $new_state{'ip2'} = $IP;
158                                 }
159                                 elsif ($state{'ip3'} eq '') {
160                                         $new_state{'ip3'} = $IP;
161                                         $new_state{'state'} = STATE->{'ready'};
162                                 }
163                                 else {
164                                         $new_state{'state'} = STATE->{'ready'};
165                                 }
166                                 write_data_file($state_file, '', '', \%new_state);
167                         }
168                 }
169                 elsif (
170                         (int($state{'state'}) == STATE->{'inactive'}) &&
171                         ($frame == 1)
172                 ) {
173                         # ready to activate?
174                         # NOTE: at this point frame 0 is already ONGed.
175                         my %story;
176                         my $frame_file;
177                         my $in_path;
178                         my $out_path;
179                         my $ext;
180                         my $ong_time = int($settings{'firstongtime'});
181                         
182                         %story     = read_data_file(DATA_STORY_PATH());
183                         %goto_list = read_data_file(DATA_LIST_PATH);
184                         
185                         if (
186                                 (int($story{'state'}) == INTF_STATE->{'>|'} ) &&
187                                 (int($story{'pass'}) == 1)
188                         ) {
189                                 # conditions met; ACTIVATE!
190                                 
191                                 # update ONG time of frame 1
192                                 $frame_data{'ongtime'} = $time;
193                                 $frame_data{'timer'} = $ong_time;
194                                 write_data_file($frame_data_path, '', '', \%frame_data);
195                                 
196                                 # set initial state
197                                 $state{'state'} = STATE->{'waiting'};
198                                 $state{'last'}  = 1; 
199                                 $state{'ip1'}   = '0.0.0.0';
200                                 $state{'ip2'}   = '0.0.0.0';
201                                 $state{'ip3'}   = '';
202                                 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
203                                 $state{'ongtime'} = $ong_time;
204                                 
205                                 # prepare to ONG frame 1
206                                 
207                                 # determine frame file & paths
208                                 $ext = (defined($frame_data{'ext'})) ?
209                                         $frame_data{'ext'} :
210                                         $default{'ext'};
211                                 $frame_file = sprintf($settings{'frame'}, $frame, $ext);
212                                 $in_path  = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
213                                 $out_path = join_path(PATH_SEPARATOR(), WWW_PATH(),  $frame_file);
214                                 
215                                 # update the GOTO list with frame 1
216                                 $goto_list{'title-1'}   = $frame_data{'title'};
217                                 $goto_list{'ongtime-1'} = $frame_data{'ongtime'};
218                                 
219                                 if (copy ($in_path, $out_path)) {
220                                         write_index(\%state, \%settings);
221                                         write_data_file($state_file,     '','', \%state);
222                                         write_data_file(DATA_LIST_PATH(),'','', \%goto_list);
223                                 }
224                                 else {
225                                         # FAILED ONG! Story as if it was inactive!
226                                         $state{'state'} = STATE->{'inactive'};
227                                 }
228                         }
229                 }
230         }
231         else {
232                 # FAILED GET STATE! Story as if it was inactive!
233                 $state{'state'} = STATE->{'inactive'};
234         }
235         close ($state_file);
236 }
237 else {
238         $state{'state'} = STATE->{'inactive'};
239 }
240
241 $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
242 %next_frame_data = read_data_file($next_frame_data_path);
243
244 # apply defaults
245 %frame_data      = merge_settings(\%default,      \%frame_data);
246 %next_frame_data = merge_settings(\%default, \%next_frame_data);
247
248 $timer   = int($state{'nextong'}) - $time;
249 $ongtime = int($state{'ongtime'});
250 if($ongtime == 0) {
251         $ongtime = int($settings{'ongtime'})
252 }
253
254 $show_command = ($timer < ($ongtime*3600/3));
255 if ($state{'state'} >= STATE->{'ready'}) {
256         $timer_unlocked = 3;
257 }
258 elsif ($state{'ip3'} ne '') {
259         $timer_unlocked = 3;
260 }
261 elsif ($state{'ip2'} ne '') {
262         $timer_unlocked = 2;
263 }
264 elsif ($state{'ip1'} ne '') {
265         $timer_unlocked = 1;
266 }
267 else {
268         $timer_unlocked = 0;
269 }
270
271 if (
272                 $password_ok || (
273                         (int($state{'state'}) >= STATE->{'waiting'}) &&
274                         ($frame <= int($state{'last'})) &&
275                         ($frame >= 0)
276                 )
277         ) {
278         $access = 1;
279 }
280 else {
281         $access = 0;
282         # replace frame data with fail state replacement
283         %frame_data = read_data_file(DATA_NOACCESS_PATH());
284         %frame_data = merge_settings(\%default, \%frame_data);
285 }
286
287 $text_mode = int($cgi{'b'});
288 if($text_mode > 2) {
289         $text_mode = TEXT_MODE->{'normal'};
290 }
291
292 print "Content-type: text/html\n";
293 if(!$access) {
294         print "Status: 403 Forbidden\n";
295 }
296 print "\n";
297 if($method eq 'HEAD') {
298         exit;
299 }
300
301 print_viewer_page (
302         \*STDOUT,
303         {
304                 'frame'         => $frame,
305                 'access'        => $access,
306                 'password_ok'   => $password_ok,
307                 'timer_unlocked'=> $timer_unlocked,
308                 'timer'         => $timer,
309                 'static'        => 0,
310                 'show_command'  => $show_command,
311                 'text_mode'     => $text_mode,
312         },
313         \%state,
314         \%settings,
315         \%frame_data,
316         \%next_frame_data
317 );