]> bicyclesonthemoon.info Git - ott/bsta/commitdiff
bbcode interface reworked; fixed eval_bb
authorb <rowerynaksiezycu@gmail.com>
Tue, 2 Jan 2024 18:00:34 +0000 (18:00 +0000)
committerb <rowerynaksiezycu@gmail.com>
Tue, 2 Jan 2024 18:00:34 +0000 (18:00 +0000)
bbcode.1.pl
bsta_lib.1.pm

index c8112b9f6699de48819af46923377f5fe070d01a..3e3e05328db95ea948c9bffec8913bad89663a34 100644 (file)
@@ -5,7 +5,7 @@
 #
 # The bbcode interface
 #
-# Copyright (C) 2017, 2023  Balthasar Szczepański
+# Copyright (C) 2017, 2023, 2024  Balthasar Szczepański
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 use strict;
-#use warnings;
+use utf8;
+# use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
 ###PERL_LIB: use lib /botm/lib/bsta
-use bsta_lib qw(failpage gethttpheader getcgi readdatafile bb2bb linehtml);
-use File::Copy;
+use botm_common (
+       'read_header_env',
+       'read_data_file', 'write_data_file',
+       'url_query_decode',
+       'join_path',
+       'merge_url'
+);
+use bsta_lib (
+       'STATE',
+       'fail_method', 'fail_content_type',
+       'get_frame', 'get_password',
+       'merge_settings',
+       'eval_bb', 'bb_to_bbcode'
+);
+
+###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
 
 ###PERL_CGI_PATH:           CGI_PATH           = /bsta/
-###PERL_CGI_ATTACH_PATH:    CGI_ATTACH_PATH    = /bsta/a
-###PERL_CGI_FRAME_PATH:     CGI_FRAME_PATH     = /bsta/f
 ###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v
 
 ###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta/
@@ -37,103 +52,100 @@ use File::Copy;
 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
 
+###PERL_SCHEME:             SCHEME             = http
 ###PERL_WEBSITE:            WEBSITE            = 1190.bicyclesonthemoon.info
 
+binmode STDIN,  ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+# decode_argv();
+
+my $time = time();
+srand ($time-$$);
+
 my %http;
 my %cgi;
-my %framedata;
+my %frame_data;
 my %default;
 my %settings;
 my %state;
 
-my $time = time();
-srand ($time-$$);
-
 my $method;
 my $frame;
 my $password;
-my $passwordOK;
+my $password_ok;
 my $access;
-my $showcommand;
 my $ongtime;
-my $seconds;
+my $ong_state;
+my $last_frame;
+my $frame_data_path;
+my $frame_file;
 
 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
 
 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
-       $method=$1;
+       $method = $1;
 }
 else{
-       exit failpage("Status: 405 Method Not Allowed\nAllow: GET, POST, HEAD\n","405 Method Not Allowed","The interface does not support the $ENV{'REQUEST_METHOD'} method.",$method);
+       exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
 }
 
-%http = gethttpheader (\%ENV);
-%cgi = getcgi($ENV{'QUERY_STRING'});
+%http = read_header_env(\%ENV);
+%cgi = url_query_decode($ENV{'QUERY_STRING'});
 
 if ($method eq 'POST') {
        if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
-               my %cgipost=getcgi( <STDIN> );
-               foreach my $ind (keys %cgipost) {
-                       $cgi{$ind}=$cgipost{$ind};
-               }
+               my %cgi_post = url_query_decode( <STDIN> );
+               %cgi = merge_settings(\%cgi, \%cgi_post);
        }
        # multipart not supported
        else{
-               exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
+               exit fail_content_type($method, $http{'content-type'});
        }
 }
 
-if ($cgi{'f'} =~ /^(.+)$/) {
-       $frame=int($1);
-}
-elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
-       $frame=int($1);
-}
-else {
-       $frame = 0;
-}
+$frame  = get_frame(\%cgi);
+$password = get_password(\%cgi);
 
-if ($cgi{'p'} =~ /^(.+)$/) {
-       $password=$1;
-}
-else {
-       $password='';
-}
+%settings   = read_data_file(DATA_SETTINGS_PATH());
+%default    = read_data_file(DATA_DEFAULT_PATH());
+%state      = read_data_file(DATA_STATE_PATH());
 
-%settings=readdatafile(DATA_SETTINGS_PATH);
-%default=readdatafile(DATA_DEFAULT_PATH);
-%framedata=readdatafile(DATA_PATH.$frame);
-%state=readdatafile(DATA_STATE_PATH);
-if($password eq $settings{'password'}){
-       $passwordOK = 1;
-}
-else{
-       $passwordOK = 0;
-}
+$ong_state  = int($state{'state'});
+$last_frame = int($state{'last'});
 
-if($frame<0) {
-       $frame = int($state{'last'}) + $frame +1;
-       %framedata=readdatafile(DATA_PATH.$frame);
-}
+$password_ok = ($password eq $settings{'password'});
 
-foreach my $ind (keys %default) {
-       unless(defined($framedata{$ind})){
-               $framedata{$ind}=$default{$ind};
-       }
+if ($frame < 0) {
+       $frame = $last_frame + $frame +1;
 }
+$frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
+%frame_data = read_data_file($frame_data_path);
+%frame_data = merge_settings(\%default, \%frame_data);
 
-if ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >= 0)) {
-       $access=1;
+if (
+       $password_ok || (
+               ($ong_state >= STATE->{'waiting'}) &&
+               ($frame <= $last_frame) &&
+               ($frame >= 0)
+       )
+) {
+       $access = 1;
 }
 else {
        $access=0;
-       %framedata = readdatafile(DATA_NOACCESS_PATH);
-       foreach my $ind (keys %default) {
-               unless(defined($framedata{$ind})){
-                       $framedata{$ind}=$default{$ind};
-               }
-       }
+       %frame_data = read_data_file(DATA_NOACCESS_PATH());
+       %frame_data = merge_settings(\%default, \%frame_data);
+}
+if ($frame_data{'frame'} ne '') {
+       $frame_file = $frame_data{'frame'};
+}
+else {
+       $frame_file = sprintf(
+               $settings{'frame'},
+               $frame, $frame_data{'ext'}
+       );
 }
 
 print "Content-type: text/plain\n";
@@ -145,38 +157,18 @@ if($method eq 'HEAD') {
        exit;
 }
 
-print '[quote][center][size=200]'.$framedata{'title'}.'[/size]'."\n";
-print '[url=http://'.WEBSITE.CGI_VIEWER_PATH.'/'.$frame.'][img]http://'.WEBSITE.CGI_PATH.($access?sprintf($settings{'frame'},$frame,$framedata{'ext'}):$framedata{'frame'}).'[/img][/url][/center]'."\n";
-print bb2bbf($framedata{'content'}).'[/quote]'."\n";
-
-sub bb2bbf {
-       (my $bb) = @_;
-       my $tag;
-       my $tagvalue;
-       my $pretext;
-       my $posttext;
-       
-       while($bb =~ m/(###([^#;]*);)/g) {
-               $tag = $1;
-               $tagvalue = $2;
-               $pretext = substr($bb,0,pos ($bb)-length($tag));
-               $posttext = substr ($bb,pos ($bb));
-               
-               if ($tagvalue =~ /^att&([0-9]+)$/) {
-                       $tagvalue = 'http://'.WEBSITE.CGI_ATTACH_PATH.'/'.int($1);
-               }
-               elsif ($tagvalue =~ /^vw&([0-9]+)$/) {
-                       $tagvalue = 'http://'.WEBSITE.CGI_VIEWER_PATH.'/'.int($1);
-               }
-               elsif ($tagvalue =~ /^fr&([0-9]+)$/) {
-                       $tagvalue = 'http://'.WEBSITE.CGI_FRAME_PATH.'/'.int($1);
-               }
-               else {
-                       $tagvalue = '';
-               }
-               
-               $bb = $pretext.$tagvalue.$posttext;
-       }
-       
-       return bb2bb ($bb);
-}
+my $viewer_url = merge_url(
+       {'scheme' => SCHEME(), 'host' => WEBSITE()},
+       {'path' => CGI_VIEWER_PATH()},
+       {'path' => $frame}
+);
+my $frame_url = merge_url(
+       {'scheme' => SCHEME(), 'host' => WEBSITE()},
+       {'path' => CGI_PATH()},
+       {'path' => $frame_file}
+);
+my $content = bb_to_bbcode(eval_bb($frame_data{'content'}, 1));
+
+print '[quote][center][size=200]'.$frame_data{'title'}.'[/size]'."\n";
+print '[url='.$viewer_url.'][img]'.$frame_url.'[/img][/url][/center]'."\n";
+print $content.'[/quote]'."\n";
index 27df76b5631f656560d27ca099d809443afa80b1..76d2a6bd18d5309e3e63fe973dc0122c674e98cc 100644 (file)
@@ -48,7 +48,9 @@ our @EXPORT_OK   = (
        'print_viewer_page',
        'write_index',
        'ong',
+       'eval_bb', 'bb_to_bbcode', 'bb_to_html'
        
+       ,
        'readdatafile', 'writedatafile', 'printdatafile', # TO REMOVE
        'entityencode', # TO REMOVE
        'printdatafileht', # TO REMOVE ???
@@ -74,6 +76,7 @@ use File::Copy;
 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
 
 ###PERL_CGI_PATH:           CGI_PATH           = /bsta/
+###PERL_CGI_ATTACH_PATH:    CGI_ATTACH_PATH    = /bsta/a
 ###PERL_CGI_2WORDS_PATH:    CGI_2WORDS_PATH    = /bsta/2words
 ###PERL_CGI_BBCODE_PATH:    CGI_BBCODE_PATH    = /bsta/b
 ###PERL_CGI_COIN_PATH:      CGI_COIN_PATH      = /bsta/coin
@@ -378,7 +381,7 @@ sub get_id {
                return int($1);
        }
        else {
-               return $default;
+               return int($default);
        }
 }
 
@@ -827,11 +830,15 @@ sub bb2bb {
 }
 
 sub eval_bb {
-       (my $bb) = @_;
+       (my $bb, my $full_url) = @_;
        my $value;
        my $before;
        my $after;
        
+       my $base_url = $full_url ?
+               {'scheme' => SCHEME(), 'host' => WEBSITE()} :
+               {'path' => ''};
+       
        while ($bb =~ m/###([^#;]*);/g) {
                $value = $1;
                $before = $`;
@@ -839,23 +846,23 @@ sub eval_bb {
                
                if ($value =~ /^att&([0-9]+)$/) {
                        $value = merge_url(
-                               SCHEME().WEBSITE(),
-                               CGI_ATTACH_PATH(),
-                               int($1)
+                               $base_url,
+                               {'path' => CGI_ATTACH_PATH()},
+                               {'path' => int($1)}
                        )
                }
                elsif ($value =~ /^vw&([0-9]+)$/) {
                        $value = merge_url(
-                               SCHEME().WEBSITE(),
-                               CGI_VIEWER_PATH(),
-                               int($1)
+                               $base_url,
+                               {'path' => CGI_VIEWER_PATH()},
+                               {'path' => int($1)}
                        )
                }
                elsif ($value =~ /^fr&([0-9]+)$/) {
                        $value = merge_url(
-                               SCHEME().WEBSITE(),
-                               CGI_FRAME_PATH(),
-                               int($1)
+                               $base_url,
+                               {'path' => CGI_FRAME_PATH()},
+                               {'path' => int($1)}
                        )
                }
                else {
@@ -1057,11 +1064,9 @@ sub print_viewer_page {
        
        my $password_query;
        
-       my $base_url   = CGI_PATH;
-       my $goto_url   = CGI_GOTO_PATH;
-       my $info_url   = CGI_INFO_PATH;
-       my $bbcode_url = CGI_BBCODE_PATH;
-       my $timer_url  = CGI_TIMER_PATH;
+       my $base_url   = CGI_PATH();
+       my $goto_url   = CGI_GOTO_PATH();
+       my $timer_url  = CGI_TIMER_PATH();
        my $viewer_full_url = merge_url(
                {'scheme' => SCHEME(), 'host' => WEBSITE()},
                {'path' => CGI_VIEWER_PATH()},
@@ -1087,22 +1092,28 @@ sub print_viewer_page {
                {'path' => CGI_VIEWER_PATH()},
                {'path' => ($static ? -1 : $last_frame)}
        );
-       if ($text_mode != TEXT_MODE->{'bb'}) {
-               $bbcode_url = merge_url(
+       my $bbcode_url = ($text_mode == TEXT_MODE->{'bb'}) ?
+               merge_url(
+                       {'path' => CGI_BBCODE_PATH()},
+                       {'path' => $frame}
+               ) :
+               merge_url (
                        $viewer_url,
                        {'query'=>{
                                'b' => TEXT_MODE->{'bb'}
                        }}
                );
-       }
-       if ($text_mode != TEXT_MODE->{'info'}) {
-               $info_url = merge_url(
+       my $info_url = ($text_mode == TEXT_MODE->{'info'}) ?
+               merge_url(
+                       {'path' => CGI_INFO_PATH()},
+                       {'path' => $frame}
+               ) :
+               merge_url (
                        $viewer_url,
                        {'query'=>{
                                'b' => TEXT_MODE->{'info'}
                        }}
                );
-       }
        my $frame_file;
        my $frame_url;
        my $frame_next_url;
@@ -1263,14 +1274,14 @@ sub print_viewer_page {
                
                print $fh '[quote][center][size=200]'.$_title.'[/size]<br>'."\n";
                print $fh '[url='.$_viewer_full_url.'][img]'.$_frame_full_url.'[/img][/url][/center]<br>'."\n";
-               print $fh html_encode_line(bb_to_bbcode(eval_bb($frame_data->{'content'})));
+               print $fh html_encode_line(bb_to_bbcode(eval_bb($frame_data->{'content'}, 1)));
                print $fh '[/quote]'."\n";
                
                print $fh '    </div>'."\n";
        }
        elsif ($frame_data->{'content'} ne '') {
                print $fh '    <div id="undertext">'."\n";
-               print $fh bb_to_html(eval_bb($frame_data->{'content'}))."\n";
+               print $fh bb_to_html(eval_bb($frame_data->{'content'}), 0)."\n";
                print $fh '    </div>'."\n";
        }