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