]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - attach.1.pl
Generate static viewer pages
[ott/bsta] / attach.1.pl
index da38af8ff770610d61df696c6c44f5df4208f22e..151dddcf5dcd8104a3873153e286a71531105de8 100644 (file)
-###PERL;
-#
+###RUN_PERL: #!/usr/bin/perl
+
 # /bsta/a
 # attach.pl is generated from attach.1.pl.
-# 19.10.2016
 #
 # The attachment interface
 #
-#    Copyright (C) 2016  Balthasar Szczepañski
+# Copyright (C) 2016, 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
-#    published by the Free Software Foundation, either version 3 of the
-#    License, or (at your option) any later version.
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
 #
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU Affero General Public License for more details.
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 use strict;
-#use warnings;
-###LIB;
-
-use bsta_lib qw(failpage gethttpheader getcgi readdatafile);
+use utf8;
+# use Encode::Locale ('decode_argv');
+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',
+       'join_path',
+       'merge_url',
+       'open_encoded', 'stat_encoded',
+       'http_header_line', 'http_header_content_length', 'http_header_content_disposition'
+);
+use bsta_lib (
+       'STATE',
+       'merge_settings',
+       'get_id', 'get_password',
+       'fail_method', 'fail_content_type', 'fail_attachment', 'fail_500',
+       'redirect'
+);
+
+###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
+
+###PERL_CGI_PATH:           CGI_PATH           = /bsta/
+
+###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta
+###PERL_DATA_ATTACH_PATH:   DATA_ATTACH_PATH   = /botm/data/bsta/a
+###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
+###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
+
+###PERL_WWW_PATH:           WWW_PATH           = /botm/www/1190/bsta/
+
+binmode STDIN,  ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+# decode_argv();
 
-###SETTINGS_PATH;
-###DATA_PATH;
-###STATE_PATH;
+my $time = time();
+srand ($time-$$);
 
 my %http;
 my %cgi;
 my %settings;
 my %state;
-my %filedata;
-
-my $time = time();
-srand ($time-$$);
+my %file_data;
 
 my $method;
 my $ID;
 my $frame;
+my $last_frame;
+my $ong_state;
 my $password;
-my $passwordOK;
+my $password_ok;
 my $IP;
+my $try_onged;
 my $buffer;
-my @fileinfo;
-my $file;
-my $filepath;
+my $fh;
+my $file_path;
+my $file_name;
 my $direct;
+my $r = 0;
 
 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
-###PATH;
+###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'});
        }
 }
 
-# print "content-type: text/plain\n\n";
-
-if ($cgi{'i'} =~ /^(.+)$/) {
-       $ID=int($1);
-}
-elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
-       $ID=int($1);
-}
-else {
-       $ID = 0;
-}
-
-if ($cgi{'p'} =~ /^(.+)$/) {
-       $password=$1;
-}
-else {
-       $password='';
-}
+$ID       = get_id(      \%cgi);
+$password = get_password(\%cgi);
 
-%settings=readdatafile(SETTINGS_PATH);
-%state=readdatafile(STATE_PATH);
-%filedata=readdatafile(DATA_PATH.'a'.$ID);
-if ($filedata{'frame'} ne '') {
-       $frame=int($filedata{'frame'});
-}
-else {
-       $frame = -1;
-}
+%settings  = read_data_file(DATA_SETTINGS_PATH());
+%state     = read_data_file(DATA_STATE_PATH());
+%file_data = read_data_file(DATA_ATTACH_PATH().$ID);
+$frame = ($file_data{'frame'} ne '') ? int($file_data{'frame'}) : -1;
+$last_frame = int($state{'last'});
+$ong_state  = int($state{'state'});
+$file_name  = $file_data{'filename'};
 
-if($password eq $settings{'password'}){
-       $passwordOK = 1;
-}
-else{
-       $passwordOK = 0;
-}
+$password_ok = ($password eq $settings{'password'});
 
-if ($filedata{'filename'} ne '' && ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >=0))) {
-}
-else {
-       exit failpage("Status: 404 Not Found\n","404 Not Found"," Attachment ".$ID." not found.");
+unless (
+       ($file_name ne '') && (
+               $password_ok || (
+                       ($ong_state >= STATE->{'waiting'}) &&
+                       ($frame <= $last_frame) &&
+                       ($frame >=0)
+               )
+       )
+) {
+       exit fail_attachment($method, $ID);
 }
 
-if ($filedata{'content'} ne '') {
-       $direct=1;
+if ($file_data{'content'} ne '') {
+       $direct = 1;
 }
 else {
        $direct = 0;
-       $filepath=DATA_PATH.$filedata{'filename'};
-       open($file,'<',$filepath) or exit failpage("Status: 404 Not Found\n","404 Not Found"," Attachment ".$ID." not found.");
-       unless(binmode($file)) {
-               close($file);
-               return failpage("Status: 500 Internal Server Error\n","500 Internal Server Error"," Can't switch to binary mode.");
+       $try_onged = (
+               ($ong_state >= STATE->{'waiting'}) &&
+               ($frame <= $last_frame) &&
+               ($frame >=0)
+       );
+       if ($try_onged) {
+               $file_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $file_name);
+               $r = open_encoded($fh, '<' , $file_path);
+               if ($r) {
+                       close($r);
+                       $file_path = merge_url(
+                               {'path' => CGI_PATH()},
+                               {'path' => $file_name}
+                       );
+                       exit redirect ($method, $file_path, HTTP_STATUS->{'see_other'});
+               }
        }
-       if (my @fileinfo = stat($filepath)){
-               print 'Content-length: '.$fileinfo[7]."\n";
+       unless ($r) {
+               $file_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $file_name);
+               $r = open_encoded($fh,'<', $file_path);
+               unless ($r) {
+                       exit fail_attachment($method, $ID);
+               }
+       }
+       unless (binmode($fh)) {
+               close($fh);
+               exit fail_500("Can't switch file to binary mode.");
+       }
+       if (my @file_info = stat_encoded($file_path)) {
+               print http_header_content_length($file_info[7]);
+       }
+}
+if ($file_data{'content-type'} ne '') {
+       print http_header_line('content-type', $file_data{'content-type'});
+}
+if ($file_name ne '') {
+       print http_header_content_disposition('inline', $file_name);
+}
+unless ($direct) {
+       unless (binmode STDOUT) {
+               close($fh);
+               exit fail_500("Can't switch output to binary mode.");
        }
 }
-print 'Content-type: '.$filedata{'content-type'}."\n";
-print 'Content-disposition: attachment; filename="'.$filedata{'filename'}.'"'."\n";
 print "\n";
+
 if($method ne 'HEAD'){
        if($direct) {
-               print $filedata{'content'};
+               print $file_data{'content'};
        }
        else {
-               while (read ($file,$buffer,1024)) {
+               while (read ($fh, $buffer, 1024)) {
                        print (STDOUT $buffer);
                }
        }
 }
-if (!$direct) {
-       close($file);
+unless ($direct) {
+       close($fh);
 }
-