home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / cgi-bin / nph-channel < prev    next >
Text File  |  2017-09-21  |  6KB  |  321 lines

  1. #!/usr/local/bin/perl
  2. #
  3. #  file: nph-channel
  4. #
  5. #  auth: Brad Burdick
  6. #  desc: dynamically generate HTML pages based on the currently selected
  7. #        audio *channel*
  8. #
  9.  
  10. eval 'exec /usr/local/bin/perl -s $0 ${1+"$@"}'
  11.   if 0;
  12.  
  13. # who am i?
  14. ($prog = $0) =~ s#.*/##;
  15.  
  16. # where we find local WWW libraries
  17. push(@INC, '/usr/local/etc/httpd/cgi-bin');
  18.  
  19. # used to parse form input and to display HTML header/trailer
  20. require 'cgi-lib.pl';
  21.  
  22. # non-zero for debug output
  23. $debug = 0;
  24.  
  25. # where the data lives
  26. $host = 'town.hall.org';
  27.  
  28. # supported audio formats
  29. %audio_fmts = (
  30. 'au',        'Sun audio (.au)',
  31. 'au.gsm',    'GSM encoded Sun audio (.gsm)',
  32. 'ram',        'RealAudio (.ra)',
  33. );
  34.  
  35. # default audio format
  36. $default_fmt = 'au';
  37.  
  38. # display HTTP header
  39. &print_HTTPheader('HTTP/1.0', 'NCSA/1.5b7', 'text/html');
  40.  
  41. if ($ENV{'CONTENT_LENGTH'} > 0 || $ENV{'QUERY_STRING'} ne "") {
  42.     # process input from audio selector form
  43.     &parse_request;
  44.  
  45.     # get HTML src and audio format, if available
  46.     ($src_html, $fmt) = split(',', $ENV{'PATH_INFO'});
  47.  
  48.     # use default format if no format info available
  49.     $fmt = $default_fmt unless $fmt;
  50.  
  51.     # check for valid format value
  52.     local($fmt_ok) = 0;
  53.     foreach $good_fmt (sort keys %audio_fmts) {
  54.         if ($fmt eq $good_fmt) {
  55.             $fmt_ok = 1;
  56.             last;
  57.         }
  58.     }
  59.  
  60.     # use default format if format value is no good
  61.     if (! $fmt_ok) {
  62.         $fmt = $default_fmt;
  63.     }
  64.     undef $fmt_ok;
  65.  
  66.     &process_form(*query, $src_html, $fmt);
  67. } elsif ($ENV{'PATH_TRANSLATED'}) {
  68.     # path to HTML src and current audio format
  69.     ($src_html, $fmt) = split(',', $ENV{'PATH_INFO'});
  70.  
  71.     # use default format if no format info available
  72.     $fmt = $default_fmt unless $fmt;
  73.  
  74.     # check for valid format value
  75.     local($fmt_ok) = 0;
  76.     foreach $good_fmt (sort keys %audio_fmts) {
  77.         if ($fmt eq $good_fmt) {
  78.             $fmt_ok = 1;
  79.             last;
  80.         }
  81.     }
  82.  
  83.     # use default format if format value is no good
  84.     if (! $fmt_ok) {
  85.         $fmt = $default_fmt;
  86.     }
  87.     undef $fmt_ok;
  88.  
  89.     # generate specified HTML page
  90.     &gen_page($src_html, $fmt);
  91. }
  92.  
  93. exit 0;
  94.  
  95.  
  96. #
  97. # process FORM query
  98. #
  99. sub process_form {
  100.     local(*query) = shift;
  101.     local($src_html) = shift;
  102.     local($fmt) = shift;
  103.  
  104.     # get audio selection based upon map coordinates from selector image
  105.     if (defined($query{'x'}) && $query{'x'} > 135) {
  106.         $fmt = 'ram';
  107.     } elsif (defined($query{'x'}) && $query{'x'} > 68) {
  108.         $fmt = 'au.gsm';
  109.     } elsif (defined($query{'x'})) {
  110.         $fmt = 'au';  # default format
  111.     } elsif (defined($query{'_Channel'})) {
  112.         $fmt = $query{'_Channel'};
  113.     }
  114.  
  115.     &gen_page($src_html, $fmt);
  116. }
  117.  
  118. #
  119. # display error message 
  120. #
  121. sub errmsg {
  122.     local($msg) = shift;
  123.  
  124.     print $msg, "\r\n";
  125. }
  126.  
  127. #
  128. # print HTTP headers (no server parsing on this output)
  129. #
  130. sub print_HTTPheader {
  131.     local($protocol, $server, $type) = @_;
  132.     local($savout) = $|;
  133.  
  134.     $| = 1;
  135.  
  136. print <<EndOfHTTP;
  137. $protocol 200 OK
  138. Server: $server
  139. Content-type: $type
  140.  
  141. EndOfHTTP
  142.  
  143.     $| = $savout;
  144. }
  145.  
  146. #
  147. # display HTML header
  148. #
  149. sub print_HTMLheader {
  150.     local($src_html, $fmt, $title) = @_;
  151.  
  152.     print <<EndOfHeader;
  153. <html>
  154. <head>
  155. <title>$title</title>
  156. </head>
  157.  
  158. <body>
  159.  
  160. EndOfHeader
  161.  
  162.     &print_AudioSelector($src_html, $fmt);
  163. }
  164.  
  165. # display HTML trailer
  166. #
  167. sub print_HTMLtrailer {
  168.  
  169.     print <<EndOfTrailer;
  170. <p>
  171. <center>
  172. <hr size="2" width="90%">
  173. <address><a href="/">Expo96</a> / <a href="/Tibet">Tibet</a></address>
  174. </center>
  175.  
  176. </body>
  177. </html>
  178.  
  179. EndOfTrailer
  180. }
  181.  
  182. #
  183. # display audio selector form
  184. #
  185. sub print_AudioSelector {
  186.     local($src_html, $fmt) = @_;
  187.  
  188.     # remove leading '/'s as they are added below
  189.     $src_html =~ s#^[/]+##g;
  190.  
  191.     if ($ENV{'HTTP_ACCEPT'} =~ m#image/#io) {
  192.         print <<EndOfForm;
  193.  
  194. <!-- Audio selection form -->
  195. <center>
  196.  
  197. <form method="GET" action="/cgi-bin/nph-channel/$src_html">
  198.  
  199. <input type="image"
  200.        src="/Images/$fmt.gif"
  201.        alt="[$fmt audio format selector]"
  202.        align="center"
  203.        width="200" height="27"
  204.        hspace="0" vspace="0"
  205.        border="0">
  206. <p>
  207. <font size="-2">
  208. Current audio selection: $audio_fmts{$fmt}
  209. </font>
  210.  
  211. </form>
  212.  
  213. </center>
  214. <!-- End audio selection form -->
  215.  
  216. EndOfForm
  217.  
  218.     } else {
  219.         local($aufmt, $gsmfmt, $rafmt);
  220.  
  221.         $aufmt  = ($fmt eq 'au')     ? "checked" : "";
  222.         $gsmfmt = ($fmt eq 'au.gsm') ? "checked" : "";
  223.         $rafmt  = ($fmt eq 'ram')    ? "checked" : "";
  224.  
  225.         print <<EndOfForm;
  226.  
  227. <!-- Audio selection form [non-image] -->
  228. <center>
  229.  
  230. <form method="GET" action="/cgi-bin/nph-channel/$src_html">
  231.  
  232. <hr size="2" width="80%">
  233. <input name="_Channel" type="radio" value="au" $aufmt> Sun audio (.au)
  234. <input name="_Channel" type="radio" value="au.gsm" $gsmfmt> GSM encoded Sun audio (.gsm)
  235. <input name="_Channel" type="radio" value="ram" $rafmt> RealAudio (.ra)
  236. <br clear=all>
  237. <input name="_Submit"  type="submit" value="Select Format">
  238. <p>
  239. Current audio selection: $audio_fmts{$fmt}
  240.  
  241. <hr size="2" width="80%">
  242.  
  243. </form>
  244.  
  245. </center>
  246. <!-- End audio selection form -->
  247.  
  248. EndOfForm
  249.  
  250.     }
  251. }
  252.  
  253. #
  254. # generate specified page
  255. #
  256. sub gen_page {
  257.     local($src_html, $fmt) = @_;
  258.     local($real_src, $real_fmt) = split(',', $ENV{'PATH_TRANSLATED'});
  259.     local($title);
  260.     local(@src);
  261.  
  262.     # display body of page
  263.     open(SRC, $real_src) || (&errmsg("$src_html: $!\n"), return);
  264.  
  265.     # display audio selector before normal HTML page
  266.     while (<SRC>) {
  267. #        s/\015$//;
  268.         # allow for multiline body tags
  269.         if (/<body>/io) {
  270.             print "$_";
  271.             &print_AudioSelector($src_html, $fmt);
  272.             last;
  273.         } elsif (/<body/io) {
  274.             print "$_";
  275.             while (<SRC>) {
  276. #                s/\015$//;
  277.                 print "$_";
  278.                 last if />$/;
  279.             }
  280.             &print_AudioSelector($src_html, $fmt);
  281.             last;
  282.         }
  283.         print "$_";
  284.     }
  285.  
  286.     # extra info for debugging...
  287.     if ($debug) {
  288.         print "<pre>\n";
  289.         for (sort keys(%ENV)) {
  290.             print "$_: $ENV{$_}\n";
  291.         }
  292.         print "</pre>\n";
  293.     }
  294.  
  295.     # slurp in the rest of the src
  296.     @src = <SRC>;
  297.     # substitute current audio format for placeholder ($FMT)
  298.     for (@src) {
  299.         local($size);
  300.  
  301.         if (/\$SIZE/) {
  302.             # TODO: determine approx. size of audio file
  303.             $size = "??? Mb";
  304.         }
  305.  
  306.         if (/<\/body>/) {
  307.             # display HTML trailer
  308.             &print_HTMLtrailer;
  309.             last;
  310.         }
  311.  
  312.         # substitions in HTML templates
  313.         s/\$FMTNAME/$audio_fmts{$fmt}/ego;
  314.         s/\$FMT/$fmt/ego;
  315.         s/\$SIZE/$size/ego;
  316.  
  317.         print "$_";
  318.     }
  319. }
  320.  
  321.