]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - viewer.1.pl
hyperlinks to static previous/next page
[ott/bsta] / viewer.1.pl
index 1a4637f9c6c1f0b106613f23ce2a5a074990ca3c..33c90edcb079ba8a18b60a3830160fbc3515ffbe 100644 (file)
@@ -27,11 +27,13 @@ use Encode ('encode', 'decode');
 
 ###PERL_LIB: use lib /botm/lib/bsta
 use botm_common (
+       'HTTP_STATUS',
        'read_header_env',
        'read_data_file', 'write_data_file',
        'url_query_decode',
        'join_path',
-       'open_encoded'
+       'open_encoded',
+       'http_header_status',
 );
 use bsta_lib (
        'STATE', 'TEXT_MODE', 'INTF_STATE',
@@ -63,6 +65,7 @@ srand ($time-$$);
 my %http;
 my %cgi;
 my %frame_data;
+my %prev_frame_data;
 my %next_frame_data;
 my %default;
 my %settings;
@@ -74,6 +77,7 @@ my %words_data;
 my $method;
 my $frame;
 my $frame_data_path;
+my $prev_frame_data_path;
 my $next_frame_data_path;
 my $password;
 my $password_ok;
@@ -95,7 +99,7 @@ if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
        $method = $1;
 }
 else{
-       exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
+       exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
 }
 
 %http = read_header_env(\%ENV);
@@ -165,7 +169,7 @@ if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
                                else {
                                        $new_state{'state'} = STATE->{'ready'};
                                }
-                               write_data_file($fh, '', '', \%new_state);
+                               write_data_file($fh, \%new_state);
                        }
                }
                elsif (
@@ -212,7 +216,7 @@ if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
                                        $r = write_index(\%state, \%settings);
                                }
                                if ($r) {
-                                       $r = write_data_file($fh,     '','', \%state);
+                                       $r = write_data_file($fh, \%state);
                                }
                                unless ($r) {
                                        # FAILED ONG! Story as if it was inactive!
@@ -231,13 +235,6 @@ else {
        $state{'state'} = STATE->{'inactive'};
 }
 
-$next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
-%next_frame_data = read_data_file($next_frame_data_path);
-
-# apply defaults
-%frame_data      = merge_settings(\%default,      \%frame_data);
-%next_frame_data = merge_settings(\%default, \%next_frame_data);
-
 $timer   = int($state{'nextong'}) - $time;
 $ongtime = int($state{'ongtime'});
 if($ongtime == 0) {
@@ -269,6 +266,16 @@ if (
                )
        ) {
        $access = 1;
+       if ($frame > 0) {
+               $prev_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame-1);
+               %prev_frame_data = read_data_file($prev_frame_data_path);
+       }
+       $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
+       %next_frame_data = read_data_file($next_frame_data_path);
+       
+       %frame_data      = merge_settings(\%default,      \%frame_data);
+       %prev_frame_data = merge_settings(\%default, \%prev_frame_data);
+       %next_frame_data = merge_settings(\%default, \%next_frame_data);
 }
 else {
        $access = 0;
@@ -292,11 +299,10 @@ $words_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame);
        1,  # as list
 );
 
-print "Content-type: text/html\n";
-if(!$access) {
-       print "Status: 403 Forbidden\n";
+if (!$access) {
+       print http_header_status(HTTP_STATUS->{'forbidden'});
 }
-print "\n";
+print "Content-type: text/html; charset=UTF-8\n\n";
 if($method eq 'HEAD') {
        exit;
 }
@@ -318,6 +324,7 @@ print_viewer_page (
        \%state,
        \%settings,
        \%frame_data,
-       \%next_frame_data,
+       $access ? \%prev_frame_data : \%frame_data,
+       $access ? \%next_frame_data : \%frame_data,
        \%words_data,
 );