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