]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - opomba.1.pl
use optimised HTML entity encoding
[ott/bsta] / opomba.1.pl
index 3b2adb567abb7ecc3ee68ff521e1faa568273f43..639097a9774708b42f646d866ccc3a58fe016bd0 100644 (file)
@@ -27,13 +27,15 @@ use Encode ('encode', 'decode');
 
 ###PERL_LIB: use lib /botm/lib/bsta
 use botm_common (
+       'HTTP_STATUS',
        'read_header_env', 'url_query_decode',
-       'read_data_file', 'write_data_file',
+       'write_data_file',
        'html_entity_encode_dec',
        'open_encoded',
        'join_path',
        'merge_url',
-       'make_id'
+       'make_id',
+       'http_header_status'
 );
 use bsta_lib (
        'TEXT_MODE', 'STATE',
@@ -45,7 +47,11 @@ use bsta_lib (
        'print_html_body_start', 'print_html_body_end',
        'bb_to_html', 'eval_bb',
        'merge_settings',
-       'write_index'
+       'write_index', 'write_static_viewer_page',
+       'read_settings', 'read_state',
+       'read_words', 'write_words',
+       'read_words_list', 'write_words_list'
+       
 );
 
 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
@@ -53,8 +59,6 @@ use bsta_lib (
 ###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v
 ###PERL_CGI_WORDS_PATH:     CGI_WORDS_PATH     = /bsta/w
 
-###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
-###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
 ###PERL_DATA_WORDS_PATH:    DATA_WORDS_PATH    = /botm/data/bsta/words/
 
 ###PERL_LOG_SPAM_PATH:      LOG_SPAM_PATH      = /botm/log/bsta/words_spam.log
@@ -94,8 +98,6 @@ my $remove = 0;
 my $post = 0;
 my $quote;
 my $words_data_path;
-my $post_data_path;
-my $last_post_data_path;
 my $index;
 my $page;
 my $ong_state;
@@ -111,7 +113,7 @@ if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
        $method = $1;
 }
 else {
-       exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
+       exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
 }
 
 %http = read_header_env(\%ENV);
@@ -142,8 +144,8 @@ if ($cgi{'i'} =~ /^.+$/) {
 }
 $password = get_password(\%cgi);
 
-%settings = read_data_file(DATA_SETTINGS_PATH());
-%state    = read_data_file(DATA_STATE_PATH());
+%settings = read_settings();
+%state    = read_state();
 $ong_state = int($state{'state'});
 $last_frame = int($state{'last'});
 
@@ -172,15 +174,14 @@ else {
 }
 
 if ($ID ne '') {
-       $post_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $ID);
-       %post_data = read_data_file($post_data_path);
+       %post_data = read_words($ID);
        if ($post_data{'frame'} ne '') {
                $frame = int($post_data{'frame'});
        }
 }
 
 unless ($frame ne '') {
-       exit output(0, '400 Bad Request', 'Frame ID not specified.');
+       exit output(0, HTTP_STATUS->{'bad_request'}, 'Frame ID not specified.');
 }
 $access = (
        $password_ok || (
@@ -189,7 +190,7 @@ $access = (
        )
 );
 unless ($access) {
-       exit output(0, '403 Forbidden', 'Not allowed to post this here now');
+       exit output(0, HTTP_STATUS->{'forbidden'}, 'Not allowed to post this here now');
 }
 
 $words_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame);
@@ -200,15 +201,12 @@ unless (open_encoded($fh, "+<:encoding(UTF-8)", $words_data_path)) {
        }
 }
 unless (flock($fh, 2)) {
-       exit output(0, '500 Internal Server Error', 'Failed locking data file.', 1);
+       exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed locking data file.', 1);
 }
 
-%words_data = read_data_file(
+%words_data = read_words_list(
        $fh, # file
-       '',  # encoding
-       0,   # no header
        0,   # header only
-       1    # as list
 );
 
 @post_list = @{$words_data{'content'}};
@@ -224,44 +222,44 @@ for (my $i=0; $i< scalar(@post_list); $i +=1) {
 if ($remove || ($ID ne '')) {
        unless ($index ne '') {
                close($fh);
-               exit output(0, '404 Not Found', $remove ? 'Nothing to remove.' : 'No such message.');
+               exit output(0, HTTP_STATUS->{'not_found'}, $remove ? 'Nothing to remove.' : 'No such message.');
        }
        unless ($cgi{'key'} eq $post_data{'key'}) {
                close($fh);
-               exit output(0, '400 Bad Request', 'Invalid request.');
+               exit output(0, HTTP_STATUS->{'bad_request'}, 'Invalid request.');
        }
 }
 if ($remove) {
-       if ($cgi{'i'} eq '') { # followed a link, not confirmed yet
+       unless (($method eq 'POST') && ($cgi{'i'} ne '')) { # followed a link, not confirmed yet
                close($fh);
                exit output(0, '', '', 1);
        }
 }
 else {
-       unless ($post) { # followed a link, not confirmed yet
+       unless (($method eq 'POST') && $post) { # followed a link, not confirmed yet
                close($fh);
                exit output(0, '', '', 1);
        }
 }
 if (!$remove) {
        unless ($cgi{'words'} ne '') {
-               exit output(0, '400 Bad Request', 'Where are your words?', 1);
+               exit output(0, HTTP_STATUS->{'bad_request'}, 'Where are your words?', 1);
        }
 }
 unless ($cgi{'username'} ne '') {
        close($fh);
-       exit output(0, '400 Bad Request', 'Missing user name.', 1);
+       exit output(0, HTTP_STATUS->{'bad_request'}, 'Missing user name.', 1);
 }
 if ($remove || ($ID ne '')) {
        unless ($cgi{'username'} eq $post_data{'name'}) {
                close($fh);
-               exit output(0, '403 Forbidden', 'Wrong user name.', 1);
+               exit output(0, HTTP_STATUS->{'forbidden'}, 'Wrong user name.', 1);
        }
 }
 if ($remove || ($ID ne '')) {
                unless ($cgi{'password'} ne '') {
                close($fh);
-               exit output(0, '400 Bad Request', 'Missing password.', 1);
+               exit output(0, HTTP_STATUS->{'bad_request'}, 'Missing password.', 1);
        }
        unless (
                ($cgi{'password'} eq $post_data{'password'}) || (
@@ -270,7 +268,7 @@ if ($remove || ($ID ne '')) {
                )
        ) {
                close($fh);
-               exit output(0, '403 Forbidden', 'Wrong password.', 1);
+               exit output(0, HTTP_STATUS->{'forbidden'}, 'Wrong password.', 1);
        }
        if ($password_ok) {
                $cgi{'password'} = $post_data{'password'};
@@ -305,17 +303,10 @@ if ($remove) {
        $words_data{'posts'} = scalar(@post_list);
        $words_data{'content'} = \@post_list;
        
-       $r = write_data_file(
-               $fh, # file 
-               \%words_data,
-               '',  # encoding
-               0,   # no header
-               0,   # header only
-               1    # as list
-       );
+       $r = write_words_list($fh, \%words_data);
        unless ($r) {
                close($fh);
-               exit output(0, '500 Internal Server Error', 'Failed writing data file.');
+               exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing data file.');
        }
        
        close ($fh);
@@ -344,8 +335,7 @@ if ($index eq '') {
        $page = int($index / COMMENT_PAGE_LENGTH());
        if ($index > 0) {
                $last_ID = $post_list[-1];
-               $last_post_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $last_ID);
-               %last_post_data = read_data_file($last_post_data_path);
+               %last_post_data = read_words($last_ID);
                if (
                        ($cgi{'username'} eq $last_post_data{'name'   }) &&
                        ($cgi{'words'   } eq $last_post_data{'content'})
@@ -362,8 +352,6 @@ if ($index eq '') {
 $words_data{'posts'} = scalar(@post_list);
 $words_data{'content'} = \@post_list;
 
-$post_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $ID);
-
 $post_data{'frame'} = $frame;
 $post_data{'name'} = $cgi{'username'};
 $post_data{'password'} = $cgi{'password'};
@@ -382,23 +370,16 @@ if ($post_data{'key'} eq '') {
 }
 $post_data{'content'} = $cgi{'words'};
 
-$r = write_data_file($post_data_path, \%post_data);
+$r = write_words($ID, \%post_data);
 unless ($r) {
        close($fh);
-       exit output(0, '500 Internal Server Error', 'Failed writing post file.', 1, 0);
+       exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing post file.', 1, 0);
 }
 
-$r = write_data_file(
-       $fh, # file 
-       \%words_data,
-       '',  # encoding
-       0,   # no header
-       0,   # header only
-       1    # as list
-);
+$r = write_words_list($fh, \%words_data);
 unless ($r) {
        close($fh);
-       exit output(0, '500 Internal Server Error', 'Failed writing data file.', 1, 0);
+       exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing data file.', 1, 0);
 }
 
 close($fh);
@@ -406,6 +387,18 @@ close($fh);
 if (($frame == 0) && ($ong_state > STATE->{'inactive'})) {
        write_index(\%state, \%settings);
 }
+elsif ($frame >= 1) {
+       write_static_viewer_page(
+               $frame,
+               \%state,
+               \%settings,
+               '', # default
+               '', # frame data
+               '', # prev frame data
+               '', # next frame data
+               \%words_data
+       );
+}
 
 if (open_encoded($fh, ">>:encoding(UTF-8)", LOG_WORDS_PATH())) {
        delete ($post_data{'password'});
@@ -438,14 +431,13 @@ sub output {
                }
        );
        if ($done) {
-               return redirect ($method, $return_url, 303);
+               return redirect($method, $return_url, HTTP_STATUS->{'see_other'});
        }
        
-       print "Content-type: text/html; charset=UTF-8\n";
        if ($status ne '') {
-               print 'Status: '.$status."\n";
+               print http_header_status($status);
        }
-       print "\n";
+       print "Content-type: text/html; charset=UTF-8\n\n";
        if ($method eq 'HEAD') {
                return;
        }
@@ -481,8 +473,7 @@ sub output {
                $content = $cgi{'words'};
        }
        elsif ($quote ne '') {
-               my $quote_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $quote);
-               my %quote_data = read_data_file($quote_data_path);
+               my %quote_data = read_words($quote);
                $content = '[quote="'.$quote_data{'name'}.'"]'.$quote_data{'content'}.'[/quote]';
        }
        elsif (($cgi{'edit'} ne '') || $remove) {
@@ -492,11 +483,11 @@ sub output {
                $content = '';
        }
        
+       my $_password = $password_ok ? html_entity_encode_dec($settings{'password'}, 1) : '';
        my $_key = html_entity_encode_dec($post_data{'key'}, 1);
        my $_ID = html_entity_encode_dec($ID, 1);
        my $_title = html_entity_encode_dec($title, 1);
        my $_message = html_entity_encode_dec($message, 1);
-       my $_password = html_entity_encode_dec($settings{'password'}, 1);
        my $_story = html_entity_encode_dec($settings{'story'}, 1);
        my $_name = html_entity_encode_dec($name, 1);
        my $_content = html_entity_encode_dec($content, 1);