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