]> bicyclesonthemoon.info Git - ott/bsta/blob - attach.1.pl
remove TBST target
[ott/bsta] / attach.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # /bsta/a
4 # attach.pl is generated from attach.1.pl.
5 #
6 # The attachment interface
7 #
8 # Copyright (C) 2016, 2023, 2024  Balthasar SzczepaƄski
9 #
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.
14 #
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.
19 #
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/>.
22
23 use strict;
24 use utf8;
25 # use Encode::Locale ('decode_argv');
26 use Encode ('encode', 'decode');
27
28 ###PERL_LIB: use lib /botm/lib/bsta
29 use botm_common (
30         'read_header_env',
31         'url_query_decode',
32         'read_data_file',
33         'join_path',
34         'merge_url'
35 );
36 use bsta_lib (
37         'STATE',
38         'merge_settings',
39         'get_id', 'get_password',
40         'fail_attachment', 'fail_500', 'redirect'
41 );
42
43 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
44
45 ###PERL_CGI_PATH:           CGI_PATH           = /bsta/
46
47 ###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta
48 ###PERL_DATA_ATTACH_PATH:   DATA_ATTACH_PATH   = /botm/data/bsta/a
49 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
50 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
51
52 ###PERL_WWW_PATH:           WWW_PATH           = /botm/www/1190/bsta/
53
54 binmode STDIN,  ':encoding(UTF-8)';
55 binmode STDOUT, ':encoding(UTF-8)';
56 binmode STDERR, ':encoding(UTF-8)';
57 # decode_argv();
58
59 my $time = time();
60 srand ($time-$$);
61
62 my %http;
63 my %cgi;
64 my %settings;
65 my %state;
66 my %file_data;
67
68 my $method;
69 my $ID;
70 my $frame;
71 my $last_frame;
72 my $ong_state;
73 my $password;
74 my $password_ok;
75 my $IP;
76 my $try_onged;
77 my $buffer;
78 my $fh;
79 my $file_path;
80 my $file_name;
81 my $direct;
82 my $r = 0;
83
84 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
85 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
86
87 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
88         $method = $1;
89 }
90 else{
91         exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
92 }
93
94 %http = read_header_env(\%ENV);
95 %cgi = url_query_decode($ENV{'QUERY_STRING'});
96
97 if ($method eq 'POST') {
98         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
99                 my %cgi_post = url_query_decode( <STDIN> );
100                 %cgi = merge_settings(\%cgi, \%cgi_post);
101         }
102         # multipart not supported
103         else{
104                 exit fail_content_type($method, $http{'content-type'});
105         }
106 }
107
108 # print "content-type: text/plain\n\n";
109
110 $ID       = get_id(      \%cgi);
111 $password = get_password(\%cgi);
112
113 %settings  = read_data_file(DATA_SETTINGS_PATH());
114 %state     = read_data_file(DATA_STATE_PATH());
115 %file_data = read_data_file(DATA_ATTACH_PATH().$ID);
116 $frame = ($file_data{'frame'} ne '') ? int($file_data{'frame'}) : -1;
117 $last_frame = int($state{'last'});
118 $ong_state  = int($state{'state'});
119 $file_name  = $file_data{'filename'};
120
121 $password_ok = ($password eq $settings{'password'});
122
123 unless (
124         ($file_name ne '') && (
125                 $password_ok || (
126                         ($ong_state >= STATE->{'waiting'}) &&
127                         ($frame <= $last_frame) &&
128                         ($frame >=0)
129                 )
130         )
131 ) {
132         exit fail_attachment($method, $ID);
133 }
134
135 if ($file_data{'content'} ne '') {
136         $direct = 1;
137 }
138 else {
139         $direct = 0;
140         $try_onged = (
141                 ($ong_state >= STATE->{'waiting'}) &&
142                 ($frame <= $last_frame) &&
143                 ($frame >=0)
144         );
145         if ($try_onged) {
146                 $file_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $file_name);
147                 $r = open($fh, '<' , encode('locale_fs', $file_path));
148                 if ($r) {
149                         close($r);
150                         $file_path = merge_url(
151                                 {'path' => CGI_PATH()},
152                                 {'path' => $file_name}
153                         );
154                         exit redirect ($method, $file_path, 303);
155                 }
156         }
157         unless ($r) {
158                 $file_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $file_name);
159                 $r = open($fh,'<', encode('locale_fs', $file_path));
160                 unless ($r) {
161                         exit fail_attachment($method, $ID);
162                 }
163         }
164         unless (binmode($fh)) {
165                 close($fh);
166                 exit fail_500("Can't switch file to binary mode.");
167         }
168         if (my @file_info = stat($file_path)) {
169                 print 'Content-length: '.$file_info[7]."\n";
170         }
171 }
172 print 'Content-type: '.$file_data{'content-type'}."\n";
173 print 'Content-disposition: attachment; filename="'.$file_name.'"'."\n";
174 unless ($direct) {
175         unless (binmode STDOUT) {
176                 close($fh);
177                 exit fail_500("Can't switch output to binary mode.");
178         }
179 }
180 print "\n";
181
182 if($method ne 'HEAD'){
183         if($direct) {
184                 print $file_data{'content'};
185         }
186         else {
187                 while (read ($fh, $buffer, 1024)) {
188                         print (STDOUT $buffer);
189                 }
190         }
191 }
192 unless ($direct) {
193         close($fh);
194 }