Annotation of loncom/cgi/plot.gif, revision 1.15

1.5       matthew     1: #!/usr/bin/perl
1.1       matthew     2: #
1.15    ! musolffc    3: # $Id: plot.gif,v 1.14 2013/08/14 00:38:55 raeburn Exp $
1.1       matthew     4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/cgi-bin/plot.gif
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
1.6       matthew    27: use strict;
1.15    ! musolffc   28: use bytes;
1.6       matthew    29: 
                     30: $|=1;
1.4       matthew    31: 
1.5       matthew    32: my $tmpdir = '/home/httpd/perl/tmp/';
1.8       matthew    33: my %data;
                     34: foreach (split/&/,$ENV{'QUERY_STRING'}) {
                     35:     my ($name,$value)=split/=/;
                     36:     $data{$name}=$value;
1.7       matthew    37: }
1.8       matthew    38: my $filename = $data{'file'};
1.9       matthew    39: # unescape filename
                     40: $filename =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.8       matthew    41: die if ($filename =~ /\// || $filename !~ /_plot.data$/);
1.7       matthew    42: $filename = $tmpdir . $filename;
1.8       matthew    43: die "$data{'file'} does not exist\n" if (! -e $filename);
                     44: 
                     45: my $output = $data{'output'};
1.12      albertel   46: if ($output eq '') {
                     47:     $output = (split('\.',$0))[-1];
                     48: }
                     49: 
1.11      matthew    50: if ($output eq 'gif' || $output eq 'png') {
1.13      albertel   51:     open(my $plot, "gnuplot $filename |");
1.15    ! musolffc   52:     my $image_content= '';
        !            53:     while (my $line = <$plot>) {
        !            54:         $image_content .= $line;
        !            55:     }
        !            56:     my $length = bytes::length($image_content);
1.6       matthew    57:     print <<"END";
1.11      matthew    58: Content-type: image/$output
1.15    ! musolffc   59: Content-length: $length
1.1       matthew    60: 
1.15    ! musolffc   61: $image_content
1.5       matthew    62: END
1.13      albertel   63: 
1.8       matthew    64: } elsif ($output eq 'eps') {
1.10      matthew    65:     print <<"END";
                     66: Content-type: text/html
                     67: 
                     68: <html><head><title>eps plot creation</title></head>
                     69: <body>
                     70: <h2>Creating eps plot</h2>
                     71: 
                     72: END
                     73:     if (! system ("gnuplot $filename")) {
1.14      raeburn    74:         print "<h2>An error occurred.</h2>\n".
1.10      matthew    75:             "I am not going to tell you about it as I have not bothered ".
                     76:             "to figure out how to get you the error text.\n";
                     77:     } else {
                     78:         print "<h2>EPS file generated successfully.</h2>\n";
                     79:     }
                     80:     print "</body></html>\n";
1.8       matthew    81: } else {
                     82:     die "output $output is not a recognized value or has no value\n";
1.4       matthew    83: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>