1 ###RUN_PERL: #!/usr/bin/perl
4 # attach.pl is generated from attach.1.pl.
6 # The attachment interface
8 # Copyright (C) 2016, 2023, 2024 Balthasar SzczepaĆski
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.
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.
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/>.
25 # use Encode::Locale ('decode_argv');
26 use Encode ('encode', 'decode');
28 ###PERL_LIB: use lib /botm/lib/bsta
36 'open_encoded', 'stat_encoded',
37 'http_header_line', 'http_header_content_length', 'http_header_content_disposition'
42 'get_id', 'get_password',
43 'fail_method', 'fail_content_type', 'fail_attachment', 'fail_500',
47 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
49 ###PERL_CGI_PATH: CGI_PATH = /bsta/
51 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta
52 ###PERL_DATA_ATTACH_PATH: DATA_ATTACH_PATH = /botm/data/bsta/a
53 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
54 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
56 ###PERL_WWW_PATH: WWW_PATH = /botm/www/1190/bsta/
58 binmode STDIN, ':encoding(UTF-8)';
59 binmode STDOUT, ':encoding(UTF-8)';
60 binmode STDERR, ':encoding(UTF-8)';
88 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
89 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
91 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
95 exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
98 %http = read_header_env(\%ENV);
99 %cgi = url_query_decode($ENV{'QUERY_STRING'});
101 if ($method eq 'POST') {
102 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
103 my %cgi_post = url_query_decode( <STDIN> );
104 %cgi = merge_settings(\%cgi, \%cgi_post);
106 # multipart not supported
108 exit fail_content_type($method, $http{'content-type'});
112 $ID = get_id( \%cgi);
113 $password = get_password(\%cgi);
115 %settings = read_data_file(DATA_SETTINGS_PATH());
116 %state = read_data_file(DATA_STATE_PATH());
117 %file_data = read_data_file(DATA_ATTACH_PATH().$ID);
118 $frame = ($file_data{'frame'} ne '') ? int($file_data{'frame'}) : -1;
119 $last_frame = int($state{'last'});
120 $ong_state = int($state{'state'});
121 $file_name = $file_data{'filename'};
123 $password_ok = ($password eq $settings{'password'});
126 ($file_name ne '') && (
128 ($ong_state >= STATE->{'waiting'}) &&
129 ($frame <= $last_frame) &&
134 exit fail_attachment($method, $ID);
137 if ($file_data{'content'} ne '') {
143 ($ong_state >= STATE->{'waiting'}) &&
144 ($frame <= $last_frame) &&
148 $file_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $file_name);
149 $r = open_encoded($fh, '<' , $file_path);
152 $file_path = merge_url(
153 {'path' => CGI_PATH()},
154 {'path' => $file_name}
156 exit redirect ($method, $file_path, HTTP_STATUS->{'see_other'});
160 $file_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $file_name);
161 $r = open_encoded($fh,'<', $file_path);
163 exit fail_attachment($method, $ID);
166 unless (binmode($fh)) {
168 exit fail_500("Can't switch file to binary mode.");
170 if (my @file_info = stat_encoded($file_path)) {
171 print http_header_content_length($file_info[7]);
174 if ($file_data{'content-type'} ne '') {
175 print http_header_line('content-type', $file_data{'content-type'});
177 if ($file_name ne '') {
178 print http_header_content_disposition('inline', $file_name);
181 unless (binmode STDOUT) {
183 exit fail_500("Can't switch output to binary mode.");
188 if($method ne 'HEAD'){
190 print $file_data{'content'};
193 while (read ($fh, $buffer, 1024)) {
194 print (STDOUT $buffer);