]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - bsta_lib.1.pm
Generate static viewer pages
[ott/bsta] / bsta_lib.1.pm
index fe1f349eebb43f99a971cfb0f1e43b87ee9ceb7e..a6d4b39c13def32b593e8c8c09f4ff801fa6c081 100644 (file)
@@ -46,13 +46,14 @@ our @EXPORT_OK   = (
        'print_html_head_start', 'print_html_head_end',
        'print_html_body_start', 'print_html_body_end',
        'print_viewer_page',
-       'write_index',
+       'write_index', 'write_static_viewer_page', 'write_static_goto',
        'ong',
        'eval_bb', 'bb_to_bbcode', 'bb_to_html'
 );
 
 ###PERL_LIB: use lib /botm/lib/bsta
 use botm_common (
+       'HTTP_STATUS',
        'url_query_decode', 'url_query_encode',
        'url_decode', 'url_encode',
        'html_entity_encode_dec',
@@ -60,7 +61,9 @@ use botm_common (
        'read_header_env',
        'read_data_file', 'write_data_file',
        'join_path',
-       'copy_encoded', 'open_encoded'
+       'copy_encoded', 'open_encoded',
+       'http_header_line', 'http_status',
+       'http_header_status', 'http_header_allow', 'http_header_location'
 );
 
 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
@@ -85,6 +88,7 @@ use botm_common (
 ###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
 ###PERL_DATA_LIST_PATH:     DATA_LIST_PATH     = /botm/data/bsta/list
 ###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
+###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
 ###PERL_DATA_WORDS_PATH:    DATA_WORDS_PATH    = /botm/data/bsta/words/
 
 ###PERL_WWW_PATH:           WWW_PATH           = /botm/www/
@@ -218,7 +222,13 @@ use constant tags_html => {
 # arguments: 1 - header fields, 2 - page title, 3 - error message, 4 method
 sub failpage {
        (my $header, my $title, my $message, my $method, my $hyperlink) = @_;
-       if($header ne ''){
+       
+       if (ref($header)) {
+               foreach my $header_name (keys %$header) {
+                       print http_header_line($header_name, $header->{$header_name});
+               }
+       }
+       elsif($header ne '') {
                print $header;
        }
        if($method eq 'HEAD') {
@@ -260,13 +270,14 @@ sub failpage {
 sub fail_method {
        (my $method, my $allowed) = @_;
        
-       my $header = "Status: 405 Method Not Allowed\n";
-       if ($allowed ne '') {
-               $header .= "Allow: $allowed\n";
-       }
+       my $status = http_status(HTTP_STATUS->{'method_not_allowed'});
+       my $header =
+               http_header_line('status', $status) .
+               http_header_allow($allowed);
+       
        return failpage(
                $header,
-               "405 Method Not Allowed",
+               $status,
                "The interface does not support the $method method.",
                $method
        );
@@ -276,9 +287,12 @@ sub fail_content_type
 {
        (my $method, my $content_type) = @_;
        
+       my $status = http_status(HTTP_STATUS->{'unsupported_media_type'});
+       my $header = http_header_line('status', $status);
+       
        return failpage(
-               "Status: 415 Unsupported Media Type\n",
-               "415 Unsupported Media Type",
+               $header,
+               $status,
                "Unsupported Content-type: $content_type.",
                $method
        );
@@ -288,9 +302,12 @@ sub fail_open_file
 {
        (my $method, my $type, my $path) = @_;
        
+       my $status = http_status(HTTP_STATUS->{'not_found'});
+       my $header = http_header_line('status', $status);
+       
        return failpage(
-               "Status: 404 Not Found\n",
-               "404 Not Found",
+               $header,
+               $status,
                        "Can't open ".
                        ($type ne '' ? $type : 'file').
                        ($path ne '' ? ': "'.$path.'"' : '').
@@ -303,9 +320,12 @@ sub fail_attachment
 {
        (my $method, my $ID) = @_;
        
+       my $status = http_status(HTTP_STATUS->{'not_found'});
+       my $header = http_header_line('status', $status);
+
        return failpage(
-               "Status: 404 Not Found\n",
-               "404 Not Found",
+               $header,
+               $status,
                "Attachment $ID not found.",
                $method
        );
@@ -314,9 +334,13 @@ sub fail_attachment
 sub fail_500
 {
        (my $method, my $text) = @_;
+       
+       my $status = http_status(HTTP_STATUS->{'internal_server_error'});
+       my $header = http_header_line('status', $status);
+       
        return failpage(
-               "Status: 500 Internal Server Error\n",
-               "500 Internal Server Error",
+               $header,
+               $status,
                $text,
                $method
        );
@@ -325,31 +349,22 @@ sub fail_500
 sub redirect
 {
        (my $method, my $uri, my $code) = @_;
+       my $header;
        my $status;
        if ($code eq '') {
-               $code = 302
-       }
-       $code = int($code);
-       if ($code == 301) {
-               $status = '301 Moved Permanently';
-       }
-       elsif ($code == 302) {
-               $status = '302 Found';
-       }
-       elsif ($code == 303) {
-               $status = '303 See Other';
-       }
-       elsif ($code == 307) {
-               $status = '307 Temporary Redirect';
-       }
-       elsif ($code == 308) {
-               $status = '308 Permanent Redirect';
-       }
-       else{
-               $status = "$code Redirect";
+               $code = HTTP_STATUS->{'found'};
        }
+       # 301 Moved Permanently
+       # 302 Found
+       # 303 See Other
+       # 307 Temporary Redirect
+       # 308 Permanent Redirect
+       $status = http_status($code);
+       $header = http_header_line('status', $status);
+       $header .= http_header_location($uri);
+       
        return failpage(
-               "Status: $status\nLocation: $uri\n",
+               $header,
                $status,
                '',
                $method,
@@ -857,7 +872,6 @@ sub print_html_data {
        print $fh html_encode_line("\n".$data->{'content'});
 }
 
-# TODO: previous page preload?
 sub print_viewer_page {
        (
                my $file,
@@ -1803,8 +1817,133 @@ sub write_index {
        return $r
 }
 
+sub write_static_viewer_page {
+       (
+               my $frame,
+               my $state_ref,
+               my $settings_ref,
+               my $default_ref,
+               my $frame_data_ref,
+               my $prev_frame_data_ref,
+               my $next_frame_data_ref,
+               my $words_data_ref
+       ) = @_;
+       
+       my %state;
+       my %settings;
+       my %default;
+       my %frame_data;
+       my %prev_frame_data;
+       my %next_frame_data;
+       my %words_data;
+       
+       my $file;
+       
+       $frame = int($frame);
+       my $prev_frame = $frame -1;
+       my $next_frame = $frame +1;
+       
+       %state = (ref ($state_ref)) ?
+               %$state_ref :
+               read_data_file(DATA_STATE_PATH());
+       my $ong_state = int($state{'state'});
+       my $last_frame = int($state{'last'});
+       
+       unless ($ong_state > STATE->{'inactive'}) {
+               return 0;
+       }
+       unless (
+               ($frame >= 0) && (
+                       ($frame < $last_frame) || (
+                               ($frame <= $last_frame) &&
+                               ($ong_state >= STATE->{'end'})
+                       )
+               )
+       ) {
+               return 0;
+       }
+       
+       %settings = (ref ($settings_ref)) ?
+               %$settings_ref :
+               read_data_file(DATA_SETTINGS_PATH());
+       %default = (ref ($default_ref)) ?
+               %$default_ref :
+               read_data_file(DATA_DEFAULT_PATH());
+       
+       %frame_data = (ref ($frame_data_ref)) ?
+               %$frame_data_ref :
+               read_data_file(join_path(PATH_SEPARATOR(), DATA_PATH(), $frame));
+       
+       %prev_frame_data = (ref ($prev_frame_data_ref)) ?
+               %$prev_frame_data_ref : (
+                       ($prev_frame >= 0) ?
+                       read_data_file(join_path(PATH_SEPARATOR(), DATA_PATH(), $prev_frame)) :
+                       %default
+               );
+               
+       %next_frame_data = (ref ($next_frame_data_ref)) ?
+               %$next_frame_data_ref : (
+                       (($next_frame < $last_frame) || (
+                               ($next_frame <= $last_frame) &&
+                               ($next_frame >= STATE->{'end'})
+                       )) ?
+                       read_data_file(join_path(PATH_SEPARATOR(), DATA_PATH(), $next_frame)) :
+                       %default
+               );
+       
+       %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);
+       
+       %words_data = (ref ($words_data_ref)) ?
+               %$words_data_ref :
+               read_data_file(
+                       join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame), # file
+                       '', # encoding
+                       0,  # no header
+                       1,  # header only
+                       1,  # as list; not relevant
+               );
+       
+       if ($frame_data{'page'} ne '') {
+               $file = $frame_data{'page'}
+       }
+       else {
+               $file = sprintf(
+                       $settings{'frame'},
+                       $frame, 'htm'
+               );
+       }
+       $file = join_path(PATH_SEPARATOR(), WWW_PATH(), $file);
+       
+       return print_viewer_page(
+               $file,
+               {
+                       'launch'        => 0,
+                       'frame'         => $frame,
+                       'access'        => 1,
+                       'password_ok'   => 0,
+                       'timer_unlocked'=> 3, # not relevant
+                       'timer'         => 0, # not relevant
+                       'static'        => 1,
+                       'show_command'  => 1,
+                       'text_mode'     => TEXT_MODE->{'normal'},
+                       'words_page'    => 0, # not relevant
+               },
+               \%state,
+               \%settings,
+               \%frame_data,
+               \%prev_frame_data,
+               \%next_frame_data,
+               \%words_data
+       );
+}
+
+sub write_static_goto {
+       
+}
 
-# ONG the frame + attachment & stiff. NOT update state file.
+# ONG the frame + attachment & stuff. NOT update state file.
 sub ong {
        (
                my $ID, my $ongtime, my $timer, my $update, my $print,