]> bicyclesonthemoon.info Git - ott/bsta/blob - opomba.1.pl
static GOTO
[ott/bsta] / opomba.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # /bsta/w
4 # opomba is generated from opomba.1.pl.
5 #
6 # The comment posting interface
7 #
8 # Copyright (C) 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', 'url_query_decode',
32         'read_data_file', 'write_data_file',
33         'html_entity_encode_dec',
34         'open_encoded',
35         'join_path',
36         'merge_url',
37         'make_id',
38         'http_header_status'
39 );
40 use bsta_lib (
41         'TEXT_MODE', 'STATE',
42         'get_password',
43         'fail_method', 'fail_content_type',
44         'redirect',
45         'print_html_start', 'print_html_end',
46         'print_html_head_start', 'print_html_head_end',
47         'print_html_body_start', 'print_html_body_end',
48         'bb_to_html', 'eval_bb',
49         'merge_settings',
50         'write_index', 'write_static_viewer_page'
51 );
52
53 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
54
55 ###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v
56 ###PERL_CGI_WORDS_PATH:     CGI_WORDS_PATH     = /bsta/w
57
58 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
59 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
60 ###PERL_DATA_WORDS_PATH:    DATA_WORDS_PATH    = /botm/data/bsta/words/
61
62 ###PERL_LOG_SPAM_PATH:      LOG_SPAM_PATH      = /botm/log/bsta/words_spam.log
63 ###PERL_LOG_WORDS_PATH:     LOG_WORDS_PATH     = /botm/log/bsta/words.log
64
65 ###PERL_WEBSITE_NAME:       WEBSITE_NAME       = Bicycles on the Moon
66
67 ###PERL_COMMENT_PAGE_LENGTH:COMMENT_PAGE_LENGTH= 16
68
69 binmode STDIN,  ':encoding(UTF-8)';
70 binmode STDOUT, ':encoding(UTF-8)';
71 binmode STDERR, ':encoding(UTF-8)';
72 # decode_argv();
73
74 my $time = time();
75 srand ($time-$$);
76
77 my %http;
78 my %cgi;
79 my %state;
80 my %settings;
81 my %words_data;
82 my %post_data;
83 my %last_post_data;
84
85 my @post_list;
86
87 my $method;
88 my $frame;
89 my $ID;
90 my $last_ID;
91 my $password;
92 my $password_ok;
93 my $access;
94 my $edit = 0;
95 my $remove = 0;
96 my $post = 0;
97 my $quote;
98 my $words_data_path;
99 my $post_data_path;
100 my $last_post_data_path;
101 my $index;
102 my $page;
103 my $ong_state;
104 my $last_frame;
105 my $fh;
106 my $r;
107
108
109 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
110 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
111
112 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
113         $method = $1;
114 }
115 else {
116         exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
117 }
118
119 %http = read_header_env(\%ENV);
120 %cgi = url_query_decode($ENV{'QUERY_STRING'});
121
122 if ($method eq 'POST') {
123         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
124                 my %cgi_post = url_query_decode( <STDIN> );
125                 %cgi = merge_settings(\%cgi, \%cgi_post);
126         }
127         # multipart not supported
128         else {
129                 exit fail_content_type($method, $http{'content-type'});
130         }
131 }
132
133 if ($ENV{'PATH_INFO'} =~ /^\/([0-9]+)$/) {
134         $frame = int($1);
135 }
136 elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
137         $ID = $1;
138 }
139 if ($cgi{'f'} =~ /^.+$/) {
140         $frame = int($&);
141 }
142 if ($cgi{'i'} =~ /^.+$/) {
143         $ID = $&;
144 }
145 $password = get_password(\%cgi);
146
147 %settings = read_data_file(DATA_SETTINGS_PATH());
148 %state    = read_data_file(DATA_STATE_PATH());
149 $ong_state = int($state{'state'});
150 $last_frame = int($state{'last'});
151
152 $password_ok = ($password eq $settings{'password'});
153
154 if ($cgi{'post'} ne '') {
155         $post = 1;
156 }
157 elsif ($cgi{'edit'} ne '') {
158         $edit = 1;
159         if ($ID eq '') {
160                 $ID = $cgi{'edit'};
161         }
162 }
163 elsif ($cgi{'remove'} ne '') {
164         $remove = 1;
165         if ($ID eq '') {
166                 $ID = $cgi{'remove'};
167         }
168 }
169 else {
170         $edit = 1;
171         if ($cgi{'quote'} ne '') {
172                 $quote = $cgi{'quote'};
173         }
174 }
175
176 if ($ID ne '') {
177         $post_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $ID);
178         %post_data = read_data_file($post_data_path);
179         if ($post_data{'frame'} ne '') {
180                 $frame = int($post_data{'frame'});
181         }
182 }
183
184 unless ($frame ne '') {
185         exit output(0, HTTP_STATUS->{'bad_request'}, 'Frame ID not specified.');
186 }
187 $access = (
188         $password_ok || (
189                 ($ong_state >= STATE->{'waiting'}) &&
190                 ($frame <= $last_frame)
191         )
192 );
193 unless ($access) {
194         exit output(0, HTTP_STATUS->{'forbidden'}, 'Not allowed to post this here now');
195 }
196
197 $words_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame);
198
199 unless (open_encoded($fh, "+<:encoding(UTF-8)", $words_data_path)) {
200         unless (open_encoded($fh, "+>:encoding(UTF-8)", $words_data_path)) {
201                 exit output(0, '500 Internal Server Error', 'Failed opening data file.', 1);
202         }
203 }
204 unless (flock($fh, 2)) {
205         exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed locking data file.', 1);
206 }
207
208 %words_data = read_data_file(
209         $fh, # file
210         '',  # encoding
211         0,   # no header
212         0,   # header only
213         1    # as list
214 );
215
216 @post_list = @{$words_data{'content'}};
217
218 for (my $i=0; $i< scalar(@post_list); $i +=1) {
219         if ($post_list[$i] eq $ID) {
220                 $index = $i;
221                 $page = int($index / COMMENT_PAGE_LENGTH());
222                 last;
223         }
224 }
225
226 if ($remove || ($ID ne '')) {
227         unless ($index ne '') {
228                 close($fh);
229                 exit output(0, HTTP_STATUS->{'not_found'}, $remove ? 'Nothing to remove.' : 'No such message.');
230         }
231         unless ($cgi{'key'} eq $post_data{'key'}) {
232                 close($fh);
233                 exit output(0, HTTP_STATUS->{'bad_request'}, 'Invalid request.');
234         }
235 }
236 if ($remove) {
237         unless (($method eq 'POST') && ($cgi{'i'} ne '')) { # followed a link, not confirmed yet
238                 close($fh);
239                 exit output(0, '', '', 1);
240         }
241 }
242 else {
243         unless (($method eq 'POST') && $post) { # followed a link, not confirmed yet
244                 close($fh);
245                 exit output(0, '', '', 1);
246         }
247 }
248 if (!$remove) {
249         unless ($cgi{'words'} ne '') {
250                 exit output(0, HTTP_STATUS->{'bad_request'}, 'Where are your words?', 1);
251         }
252 }
253 unless ($cgi{'username'} ne '') {
254         close($fh);
255         exit output(0, HTTP_STATUS->{'bad_request'}, 'Missing user name.', 1);
256 }
257 if ($remove || ($ID ne '')) {
258         unless ($cgi{'username'} eq $post_data{'name'}) {
259                 close($fh);
260                 exit output(0, HTTP_STATUS->{'forbidden'}, 'Wrong user name.', 1);
261         }
262 }
263 if ($remove || ($ID ne '')) {
264                 unless ($cgi{'password'} ne '') {
265                 close($fh);
266                 exit output(0, HTTP_STATUS->{'bad_request'}, 'Missing password.', 1);
267         }
268         unless (
269                 ($cgi{'password'} eq $post_data{'password'}) || (
270                         ($cgi{'password'} eq $settings{'password'}) &&
271                         $password_ok
272                 )
273         ) {
274                 close($fh);
275                 exit output(0, HTTP_STATUS->{'forbidden'}, 'Wrong password.', 1);
276         }
277         if ($password_ok) {
278                 $cgi{'password'} = $post_data{'password'};
279         }
280 }
281 unless ($cgi{'password2'} eq '') {
282         close($fh);
283         # no error code to confuse spambot :)
284         output(0, '', 'Please don\'t write anything in the place which should remain empty.', 1);
285         if (open_encoded($fh, ">>:encoding(UTF-8)", LOG_SPAM_PATH())) {
286                 $cgi{'content'} = $cgi{'words'};
287                 $cgi{'empty'} = $cgi{'password2'};
288                 delete($cgi{'words'});
289                 delete ($cgi{'password'});
290                 delete ($cgi{'password2'});
291                 print $fh "$time SPAM $ID\n";
292                 write_data_file(
293                         $fh, \%cgi, '',
294                         0, 0, 0,
295                         '>>', 1
296                 );
297                 print $fh "\n\n";
298                 close ($fh);
299         }
300         exit;
301 }
302
303 # all conditions fulfilled
304
305 if ($remove) {
306         splice @post_list, $index, 1;
307         $words_data{'posts'} = scalar(@post_list);
308         $words_data{'content'} = \@post_list;
309         
310         $r = write_data_file(
311                 $fh, # file 
312                 \%words_data,
313                 '',  # encoding
314                 0,   # no header
315                 0,   # header only
316                 1    # as list
317         );
318         unless ($r) {
319                 close($fh);
320                 exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing data file.');
321         }
322         
323         close ($fh);
324         
325         if (open_encoded($fh, ">>:encoding(UTF-8)", LOG_WORDS_PATH())) {
326                 delete ($post_data{'password'});
327                 print $fh "$time REMOVE $ID\n";
328                 write_data_file(
329                         $fh, \%post_data, '',
330                         0, 0, 0,
331                         '>>', 1
332                 );
333                 print $fh "\n\n";
334                 close ($fh);
335         }
336         
337         $ID = 'insw';
338         exit output(1);
339 }
340
341 if ($ID eq '') {
342         $ID = make_id($frame, 1);
343 }
344 if ($index eq '') {
345         $index = scalar(@post_list);
346         $page = int($index / COMMENT_PAGE_LENGTH());
347         if ($index > 0) {
348                 $last_ID = $post_list[-1];
349                 $last_post_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $last_ID);
350                 %last_post_data = read_data_file($last_post_data_path);
351                 if (
352                         ($cgi{'username'} eq $last_post_data{'name'   }) &&
353                         ($cgi{'words'   } eq $last_post_data{'content'})
354                 ) { # duplicate post
355                         $index -= 1;
356                         $page = int($index / COMMENT_PAGE_LENGTH());
357                         $ID = $last_ID;
358                         close ($fh);
359                         exit output(1);
360                 }
361         }
362         push @post_list, $ID;
363 }
364 $words_data{'posts'} = scalar(@post_list);
365 $words_data{'content'} = \@post_list;
366
367 $post_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $ID);
368
369 $post_data{'frame'} = $frame;
370 $post_data{'name'} = $cgi{'username'};
371 $post_data{'password'} = $cgi{'password'};
372 if ($post_data{'posttime'} eq '') {
373         $post_data{'posttime'} = $time;
374 }
375 else {
376         $post_data{'edittime'} = $time;
377 }
378 if ($post_data{'key'} eq '') {
379         my $new_key = '';
380         for (my $i=1; $i<16; $i+=1) {
381                 $new_key .= sprintf('%02X', int(rand(0x100)));
382         }
383         $post_data{'key'} = $new_key;
384 }
385 $post_data{'content'} = $cgi{'words'};
386
387 $r = write_data_file($post_data_path, \%post_data);
388 unless ($r) {
389         close($fh);
390         exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing post file.', 1, 0);
391 }
392
393 $r = write_data_file(
394         $fh, # file 
395         \%words_data,
396         '',  # encoding
397         0,   # no header
398         0,   # header only
399         1    # as list
400 );
401 unless ($r) {
402         close($fh);
403         exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing data file.', 1, 0);
404 }
405
406 close($fh);
407
408 if (($frame == 0) && ($ong_state > STATE->{'inactive'})) {
409         write_index(\%state, \%settings);
410 }
411 elsif ($frame >= 1) {
412         write_static_viewer_page(
413                 $frame,
414                 \%state,
415                 \%settings,
416                 '', # default
417                 '', # frame data
418                 '', # prev frame data
419                 '', # next frame data
420                 \%words_data
421         );
422 }
423
424 if (open_encoded($fh, ">>:encoding(UTF-8)", LOG_WORDS_PATH())) {
425         delete ($post_data{'password'});
426         print $fh "$time POST $ID\n";
427         write_data_file(
428                 $fh, \%post_data, '',
429                 0, 0, 0,
430                 '>>', 1
431         );
432         print $fh "\n\n";
433         close ($fh);
434 }
435
436 exit output(1);
437
438
439 sub output {
440         (my $done, my $status, my $message, my $show_content) = @_;
441         
442         my $return_url = merge_url(
443                 {'path' => CGI_VIEWER_PATH()},
444                 {
445                         'path' => $frame,
446                         'query' => {
447                                 'b' => TEXT_MODE->{'words'},
448                                 'i' => $page,
449                                 'p' => ($password_ok ? $settings{'password'} : '')
450                         },
451                         'fragment' => $ID
452                 }
453         );
454         if ($done) {
455                 return redirect($method, $return_url, HTTP_STATUS->{'see_other'});
456         }
457         
458         if ($status ne '') {
459                 print http_header_status($status);
460         }
461         print "Content-type: text/html; charset=UTF-8\n\n";
462         if ($method eq 'HEAD') {
463                 return;
464         }
465         
466         my $title;
467         my $name;
468         my $content;
469         
470         if ($remove) {
471                 $title = 'Remove message "'.$ID.'"';
472         }
473         elsif ($ID ne '') {
474                 $title = 'Edit message "'.$ID.'"';
475         }
476         else {
477                 $title = 'Words';
478         }
479         if ($frame ne '') {
480                 $title = $frame.'. '.$title;
481         }
482         
483         if ($cgi{'username'} ne '') {
484                 $name = $cgi{'username'}
485         }
486         elsif ($post_data{'name'} ne '') {
487                 $name = $post_data{'name'}
488         }
489         else {
490                 $name = '';
491         }
492         
493         if ($cgi{'words'} ne '') {
494                 $content = $cgi{'words'};
495         }
496         elsif ($quote ne '') {
497                 my $quote_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $quote);
498                 my %quote_data = read_data_file($quote_data_path);
499                 $content = '[quote="'.$quote_data{'name'}.'"]'.$quote_data{'content'}.'[/quote]';
500         }
501         elsif (($cgi{'edit'} ne '') || $remove) {
502                 $content = $post_data{'content'};
503         }
504         else {
505                 $content = '';
506         }
507         
508         my $_key = html_entity_encode_dec($post_data{'key'}, 1);
509         my $_ID = html_entity_encode_dec($ID, 1);
510         my $_title = html_entity_encode_dec($title, 1);
511         my $_message = html_entity_encode_dec($message, 1);
512         my $_password = html_entity_encode_dec($settings{'password'}, 1);
513         my $_story = html_entity_encode_dec($settings{'story'}, 1);
514         my $_name = html_entity_encode_dec($name, 1);
515         my $_content = html_entity_encode_dec($content, 1);
516         my $_empty = html_entity_encode_dec($cgi{'password2'}, 1);
517         my $_website_name = html_entity_encode_dec(WEBSITE_NAME(), 1);
518         my $_post_url = html_entity_encode_dec(CGI_WORDS_PATH(), 1);
519         my $_return_url = html_entity_encode_dec($return_url, 1);
520                 
521         print_html_start(\*STDOUT);
522         print_html_head_start(\*STDOUT);
523         
524         print '  <title>'.$_title.' &bull; '.$_story.' &bull; '.$_website_name.'</title>';
525         
526         print_html_head_end(\*STDOUT);
527         print_html_body_start(\*STDOUT);
528         
529         print '   <div id="inst" class="ins">'."\n";
530         
531         print '    <div id="title">'."\n";
532         print '     <h1 id="titletext">'.$_title.'</h1>'."\n";
533         print '    </div>'."\n";
534         
535         print '   </div>'."\n";
536         
537         if ($message ne '') {
538                 print '   <div id="insb" class="ins">'."\n";
539                 
540                 print '    <div id="command">'."\n";
541                 print '     <span class="br">'.$_message.'</span>'."\n";
542                 print '    </div>'."\n";
543                 
544                 print '   </div>'."\n";
545         }
546         
547         print '   <div id="insw" class="ins">'."\n";
548         
549         if ($show_content) {
550                 print '    <div class="undertext" id="words">'."\n";
551                 print '     <form method="post" action="'.$_post_url.'">'."\n";
552                 unless ($remove) {
553                         print '      <b>Your words:</b>'."\n";
554                         print '      <textarea class="inta" name="words" rows="4">'.$_content.'</textarea>'."\n";
555                 }
556                 print '      <table cellpadding="0" cellspacing="0" border="0"><tr>'."\n";
557                 print '       <td><b>Your name: </b></td>'."\n";
558                 print '       <td><input class="intx" type="text" name="username" value="'.$_name.'"></td>'."\n";
559                 print '       <td></td>'."\n";
560                 print '      </tr><tr>'."\n";
561                 print '       <td><b>'.(($ID ne '') ? 'Password' : 'Optional password').': </b></td>'."\n";
562                 print '       <td><input class="intx" type="password" name="password" value=""></td>'."\n";
563                 print '       <td>'.(($ID ne '') ? '' : '(if you want to edit later)').'</td>'."\n";
564                 print '      </tr><tr>'."\n";
565                 print '       <td><b>Leave this empty: </b></td>'."\n";
566                 print '       <td><input class="intx" type="text" name="password2" value="'.$_empty.'"></td>'."\n";
567                 if ($remove) {
568                         print '       <td><input class="inbt" type="submit" name="remove" value="Remove"></td>'."\n";
569                 }
570                 else {
571                         print '       <td>'."\n";
572                         print '        <input class="inbt" type="submit" name="post" value="'.(($ID ne '') ? 'Update' : 'Send').'">'."\n";
573                         print '        <input class="inbt" type="submit" name="preview" value="Preview">'."\n";
574                         print '       </td>'."\n";
575                 }
576                 print '      </tr></table>'."\n";
577                 print '      <input type="hidden" name="f" value="'.$frame.'">'."\n";
578                 if ($ID ne '') {
579                         print '      <input type="hidden" name="i" value="'.$_ID.'">'."\n";
580                 }
581                 print '      <input type="hidden" name="key" value="'.$_key.'">'."\n";
582                 if ($password_ok) {
583                         print '      <input type="hidden" name="p" value="'.$_password.'">'."\n";
584                 }
585                 print '     </form>'."\n";
586                 if ($content ne ''){
587                         print '     <br>'."\n";
588                         print '     <div id="preview"class="opomba">'."\n";
589                         print '      <div class="opomba_info">'."\n";
590                         print '       Preview:'."\n";
591                         print '      </div>'."\n";
592                         print '      <div class="opomba_text">'."\n";
593                   print bb_to_html(
594                                 eval_bb(
595                                         $content,
596                                         0,
597                                         $password_ok ? $settings{'password'} : ''
598                                 )
599                         )."\n";
600                         print '      </div>'."\n";
601                         print '     </div>'."\n";
602                 }
603                 print '    </div>'."\n";
604         }
605         print '    <div id="underlinks">'."\n";
606         print '     <a href="'.$_return_url.'">Return</a>'."\n";
607         print '    </div>'."\n";
608         
609         print '   </div>'."\n";
610         
611         print_html_body_end(\*STDOUT, $ong_state == STATE->{'inactive'});
612         print_html_end(\*STDOUT);
613 }