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