Annotation of loncom/cgi/thumbnail.gif, revision 1.2

1.1       www         1: #!/usr/bin/perl
                      2: 
                      3: # The LearningOnline Network with CAPA
                      4: # thumbnail.gif - a CGI script that ships out the thumbnail
                      5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA 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 General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: # Scott Harrison
                     29: # YEAR=2001
                     30: # 8/15,9/28,12/11
                     31: #
                     32: ###
                     33: 
                     34: # Example usage: /cgi-bin/thumbnail.gif?url=...
                     35: 
1.2     ! www        36: 
1.1       www        37: use strict;
                     38: 
1.2     ! www        39: sub filelocation {
        !            40:   my ($dir,$file) = @_;
        !            41:   my $location;
        !            42:   $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
        !            43:   if ($file=~m:^/~:) { # is a contruction space reference
        !            44:     $location = $file;
        !            45:     $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
        !            46:   } else {
        !            47:     $file=~s/\/home\/httpd\/html//;
        !            48:     $file=~s:^/*res::;
        !            49:     if ( !( $file =~ m:^/:) ) {
        !            50:       $location = $dir. '/'.$file;
        !            51:     } else {
        !            52:       $location = '/home/httpd/html/res'.$file;
        !            53:     }
        !            54:   }
        !            55:   $location=~s://+:/:g; # remove duplicate /
        !            56:   while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
        !            57:   return $location;
        !            58: }
        !            59: 
1.1       www        60: $|=1;   # Autoflush after each print/write
                     61: foreach (split(/&/,$ENV{'QUERY_STRING'})) {
                     62:     my ($name, $value) = split(/=/,$_);
                     63:     $value =~ tr/+/ /;
                     64:     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                     65:     if ($name eq 'url') {
                     66: 	$ENV{'form.'.$name}=$value;
                     67:     }
                     68: }
                     69: 
1.2     ! www        70: 
        !            71: my $ofn=&filelocation('',$ENV{'form.url'});
        !            72: my $fn=$ofn.'.thumbnail';
        !            73: 
        !            74: my $exists=1;
        !            75: my $generate=0;
        !            76: 
        !            77: if (-e $ofn) {
        !            78:    my ($dev,$ino,$mode,$nlink,
        !            79:        $uid,$gid,$rdev,$size,
        !            80:        $atime,$omtime,$ctime,
        !            81:        $blksize,$blocks)=stat($ofn);
        !            82:    if (-e $fn) {
        !            83:        my ($dev,$ino,$mode,$nlink,
        !            84:            $uid,$gid,$rdev,$size,
        !            85:            $atime,$mtime,$ctime,
        !            86:            $blksize,$blocks)=stat($fn);
        !            87:        if ($omtime>$mtime) { $generate=1; }
        !            88:    } else {
        !            89:        $generate=1;
        !            90:    }
        !            91: } else {
        !            92:     $exists=0;
        !            93: }
        !            94: 
        !            95: # FIXME: generate thumbnail instead
        !            96: if ($generate) { $exists=0; }
        !            97: 
        !            98: 
        !            99: unless ($exists) {
        !           100:    $fn='/home/httpd/html/res/adm/pages/nothumb.gif';
        !           101: }
        !           102: 
1.1       www       103: # Tell the server we are sending a gif graphic
                    104: print <<END;
                    105: Content-type: image/gif
                    106: 
                    107: END
                    108: 
                    109: open (IN,$fn);
                    110: binmode(STDOUT);
                    111: print join('',<IN>); # output image
                    112: $|=1; # be sure to flush before closing
                    113: close IN;
                    114: 
                    115: 
                    116: 
                    117: 
                    118: 
                    119: 

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