From 06f4f61b0c2adc7f868ee10d7c45e966cd4fe64f Mon Sep 17 00:00:00 2001 From: b Date: Tue, 2 Jan 2024 18:00:34 +0000 Subject: [PATCH] bbcode interface reworked; fixed eval_bb --- bbcode.1.pl | 188 ++++++++++++++++++++++++-------------------------- bsta_lib.1.pm | 59 +++++++++------- 2 files changed, 125 insertions(+), 122 deletions(-) diff --git a/bbcode.1.pl b/bbcode.1.pl index c8112b9..3e3e053 100644 --- a/bbcode.1.pl +++ b/bbcode.1.pl @@ -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 @@ -21,14 +21,29 @@ # along with this program. If not, see . 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( ); - foreach my $ind (keys %cgipost) { - $cgi{$ind}=$cgipost{$ind}; - } + my %cgi_post = url_query_decode( ); + %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"; diff --git a/bsta_lib.1.pm b/bsta_lib.1.pm index 27df76b..76d2a6b 100644 --- a/bsta_lib.1.pm +++ b/bsta_lib.1.pm @@ -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]
'."\n"; print $fh '[url='.$_viewer_full_url.'][img]'.$_frame_full_url.'[/img][/url][/center]
'."\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 ' '."\n"; } elsif ($frame_data->{'content'} ne '') { print $fh '
'."\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 '
'."\n"; } -- 2.30.2