]> bicyclesonthemoon.info Git - ott/bsta/blob - attach.1.pl
Adapted for new config tool.
[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  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 warnings;
25 ###PERL_LIB: use lib /botm/lib/bsta
26
27 use bsta_lib qw(failpage gethttpheader getcgi readdatafile);
28
29 ###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta
30 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
31 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
32
33 my %http;
34 my %cgi;
35 my %settings;
36 my %state;
37 my %filedata;
38
39 my $time = time();
40 srand ($time-$$);
41
42 my $method;
43 my $ID;
44 my $frame;
45 my $password;
46 my $passwordOK;
47 my $IP;
48 my $buffer;
49 my @fileinfo;
50 my $file;
51 my $filepath;
52 my $direct;
53
54 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
55 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
56
57 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
58         $method=$1;
59 }
60 else{
61         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);
62 }
63
64 %http = gethttpheader (\%ENV);
65 %cgi = getcgi($ENV{'QUERY_STRING'});
66
67 if ($method eq 'POST') {
68         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
69                 my %cgipost=getcgi( <STDIN> );
70                 foreach my $ind (keys %cgipost) {
71                         $cgi{$ind}=$cgipost{$ind};
72                 }
73         }
74         # multipart not supported
75         else{
76                 exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
77         }
78 }
79
80 # print "content-type: text/plain\n\n";
81
82 if ($cgi{'i'} =~ /^(.+)$/) {
83         $ID=int($1);
84 }
85 elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
86         $ID=int($1);
87 }
88 else {
89         $ID = 0;
90 }
91
92 if ($cgi{'p'} =~ /^(.+)$/) {
93         $password=$1;
94 }
95 else {
96         $password='';
97 }
98
99 %settings=readdatafile(DATA_SETTINGS_PATH);
100 %state=readdatafile(DATA_STATE_PATH);
101 %filedata=readdatafile(DATA_PATH.'a'.$ID);
102 if ($filedata{'frame'} ne '') {
103         $frame=int($filedata{'frame'});
104 }
105 else {
106         $frame = -1;
107 }
108
109 if($password eq $settings{'password'}){
110         $passwordOK = 1;
111 }
112 else{
113         $passwordOK = 0;
114 }
115
116 if ($filedata{'filename'} ne '' && ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >=0))) {
117 }
118 else {
119         exit failpage("Status: 404 Not Found\n","404 Not Found"," Attachment ".$ID." not found.");
120 }
121
122 if ($filedata{'content'} ne '') {
123         $direct=1;
124 }
125 else {
126         $direct = 0;
127         $filepath=DATA_PATH.$filedata{'filename'};
128         open($file,'<',$filepath) or exit failpage("Status: 404 Not Found\n","404 Not Found"," Attachment ".$ID." not found.");
129         unless(binmode($file)) {
130                 close($file);
131                 return failpage("Status: 500 Internal Server Error\n","500 Internal Server Error"," Can't switch to binary mode.");
132         }
133         if (my @fileinfo = stat($filepath)){
134                 print 'Content-length: '.$fileinfo[7]."\n";
135         }
136 }
137 print 'Content-type: '.$filedata{'content-type'}."\n";
138 print 'Content-disposition: attachment; filename="'.$filedata{'filename'}.'"'."\n";
139 print "\n";
140 if($method ne 'HEAD'){
141         if($direct) {
142                 print $filedata{'content'};
143         }
144         else {
145                 while (read ($file,$buffer,1024)) {
146                         print (STDOUT $buffer);
147                 }
148         }
149 }
150 if (!$direct) {
151         close($file);
152 }
153