[system] / trunk / pg / lib / PGalias.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/PGalias.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6249 - (download) (as text) (annotate)
Fri May 14 11:39:02 2010 UTC (9 years, 9 months ago) by gage
File size: 32277 byte(s)
major update which adds objective methods to the basic code of PG.
HEAD should be considered more beta than usual for a few days until minor glitches
are shaken out.
new modules needed:

PGcore PGalias PGresource PGloadfiles PGanswergroup PGresponsegroup  Tie::IxHash

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/PG.pl,v 1.40 2009/06/25 23:28:44 gage Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 package PGresource;
   18 use strict;
   19 use Exporter;
   20 use PGcore;
   21 
   22 sub new {
   23   my $class = shift;
   24   my $aux_file_name = shift;  #pointer to auxiliary fle
   25   my $self = {
   26     type        =>  'png', # gif eps pdf html pg (macro: pl) (applets: java js fla geogebra )
   27     path    =>  { content => undef,
   28               is_complete=>0,
   29               is_accessible => 0,
   30             },
   31     url     =>  { content => undef,
   32               is_complete=>0,
   33               is_accessible => 0,
   34             },
   35     return_uri  =>  '',
   36     recorded_uri => '',
   37     convert      => { needed  => 0,
   38               from_type => undef,
   39               from_path => undef,
   40               to_type => undef,
   41               to_path => undef,
   42             },
   43     copy_link   =>  { type => undef,  # copy or link or ??
   44               link_to_path => undef,
   45               copy_to_path => undef,
   46             },
   47     cache_info  =>  {},
   48     unique_id   =>  undef,
   49   };
   50   bless $self, $class;
   51   # $self->initialize;
   52   # $self->check_parameters;
   53   return $self;
   54 }
   55 
   56 package PGalias;
   57 use strict;
   58 use Exporter;
   59 use PGcore;
   60 use WeBWorK::PG::IO;
   61 
   62 our @ISA = ( qw ( PGcore  ) );  # look up features in PGcore -- in this case we want the environment.
   63 
   64 # new
   65 #   Create one alias object per question (and per PGcore object)
   66 #   Check that information is intact
   67 #   Construct unique id stubs
   68 #   Keep list of external links
   69 sub new {
   70   my $class = shift;
   71   my $envir = shift;  #pointer to environment hash
   72   warn "PGlias must be called with an environment" unless ref($envir) eq 'HASH';
   73   my $self = {
   74     envir   =>  $envir,
   75     searchList  =>  [{url=>'foo',dir=>'.'}],   # for subclasses -> list of url/directories to search
   76     resourceList => {},
   77 
   78   };
   79   bless $self, $class;
   80   $self->initialize;
   81   $self->check_parameters;
   82   return $self;
   83 }
   84 
   85 # methods
   86 #     make_alias   -- outputs url and does what needs to be done
   87 #     normalize paths (remove extra precursors to the path)
   88 #     search directories for item
   89 #     make_links   -- in those cases where links need to be made
   90 #     create_files  -- e.g. when printing hardcopy
   91 #     dispatcher -- decides what needs to be done based on displayMode and file type
   92 #     alias_for_html
   93 #     alias_for_image_in_html   image includes gif, png, jpg, swf, svg, flv?? ogg??
   94 #     alias_for_image_in_tex
   95 
   96 
   97 sub initialize {
   98   my $self = shift;
   99   my $envir = $self->{envir};
  100   # warn "envir-- ", join(" ", %$envir);
  101   $self->{fileName}            = $envir->{probFileName};
  102   $self->{htmlDirectory}       = $envir->{htmlDirectory};
  103   $self->{htmlURL}             = $envir->{htmlURL};
  104   $self->{tempDirectory}       = $envir->{tempDirectory};
  105   $self->{templateDirectory}   = $envir->{templateDirectory};
  106   $self->{tempURL}             = $envir->{tempURL};
  107   $self->{studentLogin}        = $envir->{studentLogin};
  108   $self->{psvnNumber}          = $envir->{psvnNumber};
  109   $self->{setNumber}           = $envir->{setNumber};
  110   $self->{probNum}             = $envir->{probNum};
  111   $self->{displayMode}         = $envir->{displayMode};
  112   $self->{externalGif2EpsPath} = $envir->{externalGif2EpsPath};
  113   $self->{externalPng2EpsPath} = $envir->{externalPng2EpsPath};
  114   #
  115   #  Find auxiliary files even when the main file is in tempates/tmpEdit
  116   #
  117   $self->{fileName} =~ s!(^|/)tmpEdit/!$1!;
  118 
  119   $self->{ext}      = "";
  120 
  121   # create uniqeID stub    "gif/uniqIDstub-filePath"
  122   $self->{uniqIDstub} = join("-",
  123                  $self->{studentLogin},
  124                  $self->{psvnNumber},
  125                  'set'.$self->{setNumber},
  126                  'prob'.$self->{probNum}
  127   );
  128 
  129 
  130 }
  131 
  132 sub check_parameters {
  133   my $self = shift;
  134 
  135   # problem specific data
  136   warn "The path to the current problem file template is not defined."     unless $self->{fileName};
  137   warn "The current studentLogin is not defined "                          unless $self->{studentLogin};
  138   warn "The current problem set number is not defined"                     if $self->{setNumber} eq ""; # allow for sets equal to 0
  139   warn "The current problem number is not defined"                         if $self->{probNum} eq "";
  140   warn "The current problem set version number (psvn) is not defined"      unless defined($self->{psvnNumber});
  141   warn "The displayMode is not defined"                                    unless $self->{displayMode};
  142 
  143   # required macros
  144 # warn "The macro &surePathToTmpFile can't be found"                    unless defined(&{$self->surePathToTmpFile()} );
  145 # warn "The macro &convertPath can't be found"                          unless defined(&{$self->convertPath()});
  146 # warn "The macro &directoryFromPath can't be found"                    unless defined(&{$self->directoryFromPath()});
  147 #    warn $self->surePathToTmpFile("foo");
  148   # warn "The webwork server does not have permission to execute the gif2eps script at  ${externalGif2EpsPath}." unless ( -x "${externalGif2EpsPath}" );
  149   # warn "The webwork server does not have permission to execute the png2eps script at ${externalPng2EpsPath}." unless ( -x "${externalPng2EpsPath}" );
  150 
  151   # required directory addresses (and URL address)
  152   warn "htmlDirectory is not defined." unless $self->{htmlDirectory};
  153   warn "htmlURL is not defined." unless $self->{htmlURL};
  154   warn "tempURL is not defined." unless $self->{tempURL};
  155 }
  156 
  157 sub make_alias {
  158     my $self = shift;
  159     # input is a path to the original auxiliary file
  160     my $aux_file_path = shift @_;
  161     my $resource_alias = new PGresource($aux_file_path);  # just call it alias? FIXME -- not in use yet.
  162 
  163     # warn "make alias for $aux_file_path";
  164   warn "Empty string used as input into the function alias" unless $aux_file_path;
  165 
  166   my $displayMode         = $self->{displayMode};
  167     my $fileName            = $self->{fileName};    # name of .pg file
  168   my $envir               = $self->{envir};
  169   my $htmlDirectory       = $envir->{htmlDirectory};
  170   my $htmlURL             = $envir->{htmlURL};
  171   my $tempDirectory       = $envir->{tempDirectory};
  172   my $tempURL             = $envir->{tempURL};
  173   my $studentLogin        = $envir->{studentLogin};
  174   my $psvnNumber          = $envir->{psvnNumber};
  175   my $setNumber           = $envir->{setNumber};
  176   my $probNum             = $envir->{probNum};
  177     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  178     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  179 
  180     my $templateDirectory   = $self->{templateDirectory};
  181 
  182   # $adr_output is a url in HTML and Latex2HTML modes
  183   # and a complete path in TEX mode.
  184   my $adr_output;
  185   my $ext;
  186 
  187   # determine file type
  188   # determine display mode
  189   # dispatch
  190 
  191   # determine extension, if there is one
  192   # if extension exists, strip and use the value for $ext
  193   # files without extensions are considered to be picture files:
  194 
  195 
  196   if ($aux_file_path =~ s/\.([^\.]*)$// ) {
  197     $ext = $1;
  198   } else {
  199     warn "This file name $aux_file_path did not have an extension.<BR> " .
  200          "Every file name used as an argument to alias must have an extension.<BR> " .
  201          "The permissable extensions are .gif, .png, and .html .<BR>";
  202     $ext  = "gif";
  203   }
  204 
  205 
  206   # in order to facilitate maintenance of this macro the routines for handling
  207   # different file types are defined separately.  This involves some redundancy
  208   # in the code but it makes it easier to define special handling for a new file
  209   # type, (but harder to change the behavior for all of the file types at once
  210   # (sigh)  ).
  211 
  212   if ($ext eq 'html') {
  213      $adr_output = $self->alias_for_html($aux_file_path)
  214   } elsif ($ext eq 'gif') {
  215     if ( $displayMode eq 'HTML' ||
  216          $displayMode eq 'HTML_tth'||
  217          $displayMode eq 'HTML_dpng'||
  218          $displayMode eq 'HTML_asciimath'||
  219          $displayMode eq 'HTML_LaTeXMathML'||
  220          $displayMode eq 'HTML_jsMath'||
  221          $displayMode eq 'HTML_img') {
  222       ################################################################################
  223       # .gif FILES in HTML; HTML_tth; HTML_dpng; HTML_img; and Latex2HTML modes
  224       ################################################################################
  225       $adr_output=$self->alias_for_gif_in_html_mode($aux_file_path);
  226 
  227     } elsif ($displayMode eq 'TeX') {
  228       ################################################################################
  229       # .gif FILES in TeX mode
  230       ################################################################################
  231             $adr_output=$self->alias_for_gif_in_tex_mode($aux_file_path);
  232 
  233     } else {
  234       die "Error in alias: dangerousMacros.pl: unrecognizable displayMode = $displayMode";
  235     }
  236   } elsif ($ext eq 'png') {
  237     if ( $displayMode eq 'HTML' ||
  238          $displayMode eq 'HTML_tth'||
  239          $displayMode eq 'HTML_dpng'||
  240          $displayMode eq 'HTML_asciimath'||
  241          $displayMode eq 'HTML_LaTeXMathML'||
  242          $displayMode eq 'HTML_jsMath'||
  243          $displayMode eq 'HTML_img' )  {
  244         $adr_output = $self->alias_for_png_in_html_mode($aux_file_path);
  245     } elsif ($displayMode eq 'TeX') {
  246       $adr_output = $self->alias_for_png_in_tex_mode($aux_file_path);
  247 
  248     } else {
  249       warn  "Error in alias: dangerousMacros.pl","unrecognizable displayMode = $displayMode","";
  250     }
  251   } else { # $ext is not recognized
  252     ################################################################################
  253     # FILES  with unrecognized file extensions in any display modes
  254     ################################################################################
  255 
  256     warn "Error in the macro alias. Alias does not understand how to process files with extension $ext.  (Path ot problem file is  $fileName) ";
  257   }
  258 
  259   warn "The macro alias was unable to form a URL for some auxiliary file used in this problem." unless $adr_output;
  260   return $adr_output;
  261 }
  262 
  263 
  264 
  265 sub alias_for_html {
  266   my $self = shift;
  267   my $aux_file_path = shift;
  268     warn "aux_file for html $aux_file_path";
  269   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  270   my $htmlDirectory       = $envir->{htmlDirectory};
  271   my $htmlURL             = $envir->{htmlURL};
  272   my $tempDirectory       = $envir->{tempDirectory};
  273   my $tempURL             = $envir->{tempURL};
  274   my $studentLogin        = $envir->{studentLogin};
  275   my $psvnNumber          = $envir->{psvnNumber};
  276   my $setNumber           = $envir->{setNumber};
  277   my $probNum             = $envir->{probNum};
  278   my $displayMode         = $envir->{displayMode};
  279     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  280     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  281 
  282     my $templateDirectory   = $self->{templateDirectory};
  283 
  284 
  285   # $adr_output is a url in HTML and Latex2HTML modes
  286   # and a complete path in TEX mode.
  287   my $adr_output;
  288   my $ext                 =   "html";
  289     ################################################################################
  290     # .html FILES in HTML, HTML_tth, HTML_dpng, HTML_img, etc. and Latex2HTML mode
  291     ################################################################################
  292 
  293     # No changes are made for auxiliary files in the
  294     # ${Global::htmlDirectory} subtree.
  295     if ( $aux_file_path =~ m|^$tempDirectory| ) {
  296       $adr_output = $aux_file_path,
  297       $adr_output =~ s|$tempDirectory|$tempURL/|,
  298       $adr_output .= ".$ext",
  299     } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  300       $adr_output = $aux_file_path,
  301       $adr_output =~ s|$htmlDirectory|$htmlURL|,
  302       $adr_output .= ".$ext",
  303     } else {
  304       # HTML files not in the htmlDirectory are assumed under live under the
  305       # templateDirectory in the same directory as the problem.
  306       # Create an alias file (link) in the directory html/tmp/html which
  307       # points to the original file and return the URL of this alias.
  308       # Create all of the subdirectories of html/tmp/html which are needed
  309       # using sure file to path.
  310 
  311       # $fileName is obtained from environment for PGeval
  312       # it gives the  full path to the current problem
  313       my $filePath = directoryFromPath($fileName);
  314       my $htmlFileSource = convertPath("$templateDirectory${filePath}$aux_file_path.html");
  315       my $link = "html/".$self->{uniqIDstub}."-$aux_file_path.$ext";
  316       my $linkPath = $self->surePathToTmpFile($link);
  317       $adr_output = "${tempURL}$link";
  318       if (-e $htmlFileSource) {
  319         if (-e $linkPath) {
  320           unlink($linkPath) || warn "Unable to unlink alias file at |$linkPath|";
  321           # destroy the old link.
  322         }
  323         symlink( $htmlFileSource, $linkPath)
  324               || warn "The macro alias cannot create a link from |$linkPath|  to |$htmlFileSource| <BR>" ;
  325       } else {
  326         warn("The macro alias cannot find an HTML file at: |$htmlFileSource|");
  327       }
  328     }
  329   $adr_output;
  330 }
  331 
  332 
  333 sub alias_for_gif_in_html_mode {
  334   my $self = shift;
  335   my $aux_file_path = shift;
  336 #    warn "entering alias_for_gif_in_html_mode $aux_file_path";
  337 
  338   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  339   my $htmlDirectory       = $envir->{htmlDirectory};
  340   my $htmlURL             = $envir->{htmlURL};
  341   my $tempDirectory       = $envir->{tempDirectory};
  342   my $tempURL             = $envir->{tempURL};
  343   my $studentLogin        = $envir->{studentLogin};
  344   my $psvnNumber          = $envir->{psvnNumber};
  345   my $setNumber           = $envir->{setNumber};
  346   my $probNum             = $envir->{probNum};
  347   my $displayMode         = $envir->{displayMode};
  348     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  349     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  350 
  351     my $templateDirectory   = $self->{templateDirectory};
  352 
  353 
  354   # $adr_output is a url in HTML and Latex2HTML modes
  355   # and a complete path in TEX mode.
  356   my $adr_output;
  357   my $ext                 = "gif";
  358 
  359       ################################################################################
  360       # .gif FILES in HTML, HTML_tth, HTML_dpng, HTML_img, and Latex2HTML modes
  361       ################################################################################
  362 
  363       #warn "tempDirectory is $tempDirectory";
  364       #warn "file Path for auxiliary file is $aux_file_path";
  365 
  366       # No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  367       if ( $aux_file_path =~ m|^$tempDirectory| ) {
  368         $adr_output = $aux_file_path;
  369         $adr_output =~ s|$tempDirectory|$tempURL|;
  370         $adr_output .= ".$ext";
  371         #warn "adress out is $adr_output",
  372       } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  373         $adr_output = $aux_file_path;
  374         $adr_output =~ s|$htmlDirectory|$htmlURL|;
  375         $adr_output .= ".$ext";
  376       } else {
  377         # files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  378         # subtree in the same directory as the problem.
  379 
  380         # For a gif file the alias macro creates an alias under the html/images directory
  381         # which points to the gif file in the problem directory.
  382         # All of the subdirectories of html/tmp/gif which are needed are also created.
  383         my $filePath = directoryFromPath($fileName);
  384 
  385         # $fileName is obtained from environment for PGeval
  386         # it gives the full path to the current problem
  387         my $gifSourceFile = convertPath("$templateDirectory${filePath}$aux_file_path.gif");
  388         #my $link = "gif/$studentLogin-$psvnNumber-set$setNumber-prob$probNum-$aux_file_path.$ext";
  389 
  390         #  Make file names work in Library Browser when the images in several
  391         #  files have the same names.
  392         my $libFix = "";
  393         if ($setNumber eq "Undefined_Set") {
  394           $libFix = $fileName;
  395           $libFix =~ s!.*/!!, $libFix =~ s!\.pg(\..*)?$!!;
  396           $libFix =~ s![^a-zA-Z0-9._-]!!g;
  397           $libFix .= '-';
  398         }
  399 
  400         my $link = "gif/$setNumber-prob$probNum-$libFix$aux_file_path.$ext";
  401 
  402         my $linkPath = $self->surePathToTmpFile($link);
  403         $adr_output = "${tempURL}$link";
  404         #warn "linkPath is $linkPath";
  405         #warn "adr_output is $adr_output";
  406         if (-e $gifSourceFile) {
  407           if (-e $linkPath) {
  408             unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  409           }
  410           symlink($gifSourceFile, $linkPath)
  411             || warn "The macro alias cannot create a link from |$linkPath|  to |$gifSourceFile| <BR>" ;
  412         } else {
  413           warn("The macro alias cannot find a GIF file at: |$gifSourceFile|");
  414         }
  415       }
  416     $adr_output;
  417 }
  418 
  419 sub alias_for_gif_in_tex_mode {
  420   my $self = shift;
  421   my $aux_file_path = shift;
  422 
  423   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  424   my $htmlDirectory       = $envir->{htmlDirectory};
  425   my $htmlURL             = $envir->{htmlURL};
  426   my $tempDirectory       = $envir->{tempDirectory};
  427   my $tempURL             = $envir->{tempURL};
  428   my $studentLogin        = $envir->{studentLogin};
  429   my $psvnNumber          = $envir->{psvnNumber};
  430   my $setNumber           = $envir->{setNumber};
  431   my $probNum             = $envir->{probNum};
  432   my $displayMode         = $envir->{displayMode};
  433     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  434     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  435 
  436     my $templateDirectory   = $self->{templateDirectory};
  437 
  438 
  439   # $adr_output is a url in HTML and Latex2HTML modes
  440   # and a complete path in TEX mode.
  441   my $adr_output;
  442   my $ext                 = "gif";
  443       ################################################################################
  444       # .gif FILES in TeX mode
  445       ################################################################################
  446 
  447             $setNumber =~ s/\./_/g;  ## extra dots confuse latex's graphics package
  448       if ($envir->{texDisposition} eq "pdf") {
  449         # We're going to create PDF files with our TeX (using pdflatex); so we
  450         # need images in PNG format.
  451 
  452         my $gifFilePath;
  453 
  454         if ($aux_file_path =~ m/^$htmlDirectory/ or $aux_file_path =~ m/^$tempDirectory/) {
  455           # we've got a full pathname to a file
  456           $gifFilePath = "$aux_file_path.gif";
  457         } else {
  458           # we assume the file is in the same directory as the problem source file
  459           $gifFilePath = $templateDirectory . directoryFromPath($fileName) . "$aux_file_path.gif";
  460         }
  461 
  462         my $gifFileName = fileFromPath($gifFilePath);
  463 
  464         $gifFileName =~ /^(.*)\.gif$/;
  465 #       my $pngFilePath = $self->surePathToTmpFile("${tempDirectory}png/$probNum-$1.png");
  466         my $pngFilePath = $self->surePathToTmpFile("${tempDirectory}png/$setNumber-$probNum-$1.png");
  467         my $returnCode = system "cat $gifFilePath | ${$envir->{externalGif2PngPath}} > $pngFilePath";
  468 
  469         if ($returnCode or not -e $pngFilePath) {
  470           die "failed to convert $gifFilePath to $pngFilePath using gif->png with ${$envir->{externalGif2PngPath}}: $!\n";
  471         }
  472 
  473         $adr_output = $pngFilePath;
  474       } else {
  475         # Since we're not creating PDF files; we're probably just using a plain
  476         # vanilla latex. Hence; we need EPS images.
  477 
  478         ################################################################################
  479         # This is statement used below is system dependent.
  480         # Notice that the range of colors is restricted when converting to postscript to keep the files small
  481         # "cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmtops -noturn > $adr_output"
  482         # "cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn > $adr_output"
  483         ################################################################################
  484         if ($aux_file_path =~  m|^$htmlDirectory|  or $aux_file_path =~  m|^$tempDirectory|)  {
  485           # To serve an eps file copy an eps version of the gif file to the subdirectory of eps/
  486           my $linkPath = directoryFromPath($fileName);
  487 
  488           my $gifSourceFile = "$aux_file_path.gif";
  489           my $gifFileName = fileFromPath($gifSourceFile);
  490           $adr_output = surePathToTmpFile("$tempDirectory/eps/$studentLogin-$psvnNumber-$gifFileName.eps") ;
  491 
  492           if (-e $gifSourceFile) {
  493             #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output")
  494             system("cat $gifSourceFile | ${externalGif2EpsPath} > $adr_output" )
  495               && die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $probNum " .
  496                      "using the system dependent script\n |${externalGif2EpsPath}| \n";
  497           } else {
  498             die "|$gifSourceFile| cannot be found.  Problem number: |$probNum|";
  499           }
  500         } else {
  501           # To serve an eps file copy an eps version of the gif file to  a subdirectory of eps/
  502           my $filePath = directoryFromPath($fileName);
  503           my $gifSourceFile = "${templateDirectory}${filePath}$aux_file_path.gif";
  504           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  505           $adr_output = surePathToTmpFile("eps/$studentLogin-$psvnNumber-set$setNumber-prob$probNum-$aux_file_path.eps");
  506 
  507           if (-e $gifSourceFile) {
  508             #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  509             #warn "Unable to create eps file: |$adr_output|\n from file\n |$gifSourceFile|\n in problem $probNum";
  510             #warn "Help ${:externalGif2EpsPath}" unless -x "${main::externalGif2EpsPath}";
  511             system("cat $gifSourceFile | ${externalGif2EpsPath} > $adr_output" )
  512               && die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $probNum " .
  513                      "using the system dependent commands \n |${externalGif2EpsPath}| \n ";
  514           }  else {
  515             die "|$gifSourceFile| cannot be found.  Problem number: |$probNum|";
  516           }
  517         }
  518       }
  519   $adr_output;
  520 
  521 }
  522 sub alias_for_png_in_html_mode {
  523   my $self = shift;
  524   my $aux_file_path = shift;
  525 
  526   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  527   my $htmlDirectory       = $envir->{htmlDirectory};
  528   my $htmlURL             = $envir->{htmlURL};
  529   my $tempDirectory       = $envir->{tempDirectory};
  530   my $tempURL             = $envir->{tempURL};
  531   my $studentLogin        = $envir->{studentLogin};
  532   my $psvnNumber          = $envir->{psvnNumber};
  533   my $setNumber           = $envir->{setNumber};
  534   my $probNum             = $envir->{probNum};
  535   my $displayMode         = $envir->{displayMode};
  536     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  537     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  538 
  539     my $templateDirectory   = $self->{templateDirectory};
  540 
  541 
  542   # $adr_output is a url in HTML and Latex2HTML modes
  543   # and a complete path in TEX mode.
  544   my $adr_output;
  545   my $ext                 = "png";
  546       ################################################################################
  547       # .png FILES in HTML; HTML_tth; HTML_dpng; HTML_img; etc. and Latex2HTML modes
  548       ################################################################################
  549 
  550       #warn "tempDirectory is $tempDirectory";
  551       #warn "file Path for auxiliary file is $aux_file_path";
  552 
  553       # No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  554       if ( $aux_file_path =~ m|^$tempDirectory| ) {
  555       $adr_output = $aux_file_path;
  556         $adr_output =~ s|$tempDirectory|$tempURL|;
  557         $adr_output .= ".$ext";
  558         #warn "adress out is $adr_output";
  559       } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  560         $adr_output = $aux_file_path;
  561         $adr_output =~ s|$htmlDirectory|$htmlURL|;
  562         $adr_output .= ".$ext";
  563       } else {
  564         # files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  565         # subtree in the same directory as the problem.
  566 
  567         # For a png file the alias macro creates an alias under the html/images directory
  568         # which points to the png file in the problem directory.
  569         # All of the subdirectories of html/tmp/gif which are needed are also created.
  570         my $filePath = directoryFromPath($fileName);
  571 
  572         # $fileName is obtained from environment for PGeval
  573         # it gives the full path to the current problem
  574         my $pngSourceFile = convertPath("$templateDirectory${filePath}$aux_file_path.png");
  575         my $link = "gif/".$self->{uniqIDstub}."-$aux_file_path.$ext";
  576         my $linkPath = surePathToTmpFile($link);
  577         $adr_output = "${tempURL}$link";
  578         #warn "linkPath is $linkPath";
  579         #warn "adr_output is $adr_output";
  580         if (-e $pngSourceFile) {
  581           if (-e $linkPath) {
  582             unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  583           }
  584           symlink($pngSourceFile, $linkPath)
  585           || warn "The macro alias cannot create a link from |$linkPath|  to |$pngSourceFile| <BR>" ;
  586         } else {
  587           warn("The macro alias cannot find a PNG file at: |$pngSourceFile|");
  588         }
  589       }
  590   $adr_output;
  591 
  592 }
  593 
  594 sub alias_for_png_in_tex_mode {
  595 
  596     my $self = shift;
  597   my $aux_file_path = shift;
  598 
  599   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  600   my $htmlDirectory       = $envir->{htmlDirectory};
  601   my $htmlURL             = $envir->{htmlURL};
  602   my $tempDirectory       = $envir->{tempDirectory};
  603   my $tempURL             = $envir->{tempURL};
  604   my $studentLogin        = $envir->{studentLogin};
  605   my $psvnNumber          = $envir->{psvnNumber};
  606   my $setNumber           = $envir->{setNumber};
  607   my $probNum             = $envir->{probNum};
  608   my $displayMode         = $envir->{displayMode};
  609     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  610     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  611 
  612     my $templateDirectory   = $self->{templateDirectory};
  613 
  614 
  615   # $adr_output is a url in HTML and Latex2HTML modes
  616   # and a complete path in TEX mode.
  617   my $adr_output;
  618   my $ext                 = "png";
  619             ################################################################################
  620       # .png FILES in TeX mode
  621       ################################################################################
  622 
  623             $setNumber =~ s/\./_/g;  ## extra dots confuse latex's graphics package
  624       if ($envir->{texDisposition} eq "pdf") {
  625         # We're going to create PDF files with our TeX (using pdflatex); so we
  626         # need images in PNG format. what luck! they're already in PDF format!
  627 
  628         my $pngFilePath;
  629 
  630         if ($aux_file_path =~ m/^$htmlDirectory/ or $aux_file_path =~ m/^$tempDirectory/) {
  631           # we've got a full pathname to a file
  632           $pngFilePath = "$aux_file_path.png";
  633         } else {
  634           # we assume the file is in the same directory as the problem source file
  635           $pngFilePath = $templateDirectory . directoryFromPath($fileName) . "$aux_file_path.png";
  636         }
  637 
  638         $adr_output = $pngFilePath;
  639       } else {
  640         # Since we're not creating PDF files; we're probably just using a plain
  641         # vanilla latex. Hence; we need EPS images.
  642 
  643         ################################################################################
  644         # This is statement used below is system dependent.
  645         # Notice that the range of colors is restricted when converting to postscript to keep the files small
  646         # "cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmtops -noturn > $adr_output"
  647         # "cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn > $adr_output"
  648         ################################################################################
  649 
  650         if ($aux_file_path =~  m|^$htmlDirectory|  or $aux_file_path =~  m|^$tempDirectory|)  {
  651           # To serve an eps file copy an eps version of the png file to the subdirectory of eps/
  652           my $linkPath = directoryFromPath($fileName);
  653 
  654           my $pngSourceFile = "$aux_file_path.png";
  655           my $pngFileName = fileFromPath($pngSourceFile);
  656           $adr_output = surePathToTmpFile("$tempDirectory/eps/$studentLogin-$psvnNumber-$pngFileName.eps") ;
  657 
  658           if (-e $pngSourceFile) {
  659             #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output")
  660             system("cat $pngSourceFile | ${externalPng2EpsPath} > $adr_output" )
  661               && die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $probNum " .
  662                      "using the system dependent commands\n |${externalPng2EpsPath}| \n";
  663           } else {
  664             die "|$pngSourceFile| cannot be found.  Problem number: |$probNum|";
  665           }
  666         } else {
  667           # To serve an eps file copy an eps version of the png file to  a subdirectory of eps/
  668           my $filePath = directoryFromPath($fileName);
  669           my $pngSourceFile = "${templateDirectory}${filePath}$aux_file_path.png";
  670           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  671           $adr_output = surePathToTmpFile("eps/$studentLogin-$psvnNumber-set$setNumber-prob$probNum-$aux_file_path.eps") ;
  672           if (-e $pngSourceFile) {
  673             #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  674             #warn "Unable to create eps file: |$adr_output|\n from file\n |$pngSourceFile|\n in problem $probNum";
  675             #warn "Help ${externalPng2EpsPath}" unless -x "${externalPng2EpsPath}";
  676             system("cat $pngSourceFile | ${externalPng2EpsPath} > $adr_output" )
  677               && die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $probNum " .
  678                      "using the system dependent commands\n |${externalPng2EpsPath}| \n ";
  679           } else {
  680             die "|$pngSourceFile| cannot be found.  Problem number: |$probNum|";
  681           }
  682         }
  683       }
  684   $adr_output;
  685 
  686 }
  687 
  688 ################################################
  689 
  690 # More resource search macros
  691 
  692 ################################################
  693 
  694 #
  695 #  Look for a macro file in the directories specified in the macros path
  696 #
  697 
  698 # ^variable my $macrosPath
  699 our ($macrosPath,
  700     # ^variable my $pwd
  701     $pwd,
  702     # ^variable my $appletPath
  703     $appletPath,
  704     # ^variable my $server_root_url
  705     $server_root_url,
  706   # ^variable my $templateDirectory
  707   $templateDirectory,
  708   # ^variable my $scriptDirectory
  709   $scriptDirectory,
  710   # ^variable my $externalTTHPath
  711   $externalTTHPath,
  712   );
  713 
  714 # ^function findMacroFile
  715 # ^uses $macrosPath
  716 # ^uses $pwd
  717 sub findMacroFile {
  718   my $self   = shift;
  719   my $fileName = shift;
  720   my $filePath;
  721   foreach my $dir (@{$macrosPath}) {
  722     $filePath = "$dir/$fileName";
  723     $filePath =~ s!^\.\.?/!$pwd/!;
  724     return $filePath if (-r $filePath);
  725   }
  726   return;  # no file found
  727 }
  728 
  729 # ^function check_url
  730 # ^uses %envir
  731 sub check_url {
  732   my $self = shift;
  733   my $url  = shift;
  734   return undef if $url =~ /;/;   # make sure we can't get a second command in the url
  735   #FIXME -- check for other exploits of the system call
  736   #FIXME -- ALARM feature so that the response cannot be held up for too long.
  737   #FIXME doesn't seem to work with relative addresses.
  738   #FIXME  Can we get the machine name of the server?
  739 
  740    my $check_url_command = $self->{envir}->{externalCheckUrl};
  741    my $response = system("$check_url_command $url");
  742   return ($response) ? 0 : 1; # 0 indicates success, 256 is failure possibly more checks can be made
  743 }
  744 
  745 # ^variable our %appletCodebaseLocations
  746 our %appletCodebaseLocations = ();
  747 # ^function findAppletCodebase
  748 # ^uses %appletCodebaseLocations
  749 # ^uses $appletPath
  750 # ^uses $server_root_url
  751 # ^uses check_url
  752 sub findAppletCodebase {
  753   my $self     = shift;
  754   my $fileName = shift;  # probably the name of a jar file
  755   return $appletCodebaseLocations{$fileName}    #check cache first
  756     if defined($appletCodebaseLocations{$fileName})
  757       and $appletCodebaseLocations{$fileName} =~/\S/;
  758 
  759   foreach my $appletLocation (@{$appletPath}) {
  760     if ($appletLocation =~ m|^/|) {
  761       $appletLocation = "$server_root_url$appletLocation";
  762     }
  763     return $appletLocation;  # --hack workaround -- just pick the first location and use that -- no checks
  764 #hack to workaround conflict between lwp-request and apache2
  765 # comment out the check_url block
  766 #     my $url = "$appletLocation/$fileName";
  767 #     if ($self->check_url($url)) {
  768 #         $appletCodebaseLocations{$fileName} = $appletLocation; #update cache
  769 #       return $appletLocation   # return codebase part of url
  770 #     }
  771   }
  772   return "Error: $fileName not found at ". join(",  ", @{$appletPath} );  # no file found
  773 }
  774 
  775 
  776 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9