[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 6502 - (download) (as text) (annotate)
Thu Nov 11 15:32:39 2010 UTC (9 years, 3 months ago) by gage
File size: 33199 byte(s)
cosmetic change


    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/lib/PGalias.pm,v 1.6 2010/05/15 18:41:23 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->{psvn}          = $envir->{psvn};
  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   $self->{appletPath} = $self->{envir}->{pgDirectories}->{appletPath};
  116   #
  117   #  Find auxiliary files even when the main file is in tempates/tmpEdit
  118   #
  119   $self->{fileName} =~ s!(^|/)tmpEdit/!$1!;
  120 
  121   $self->{ext}      = "";
  122 
  123   # create uniqeID stub    "gif/uniqIDstub-filePath"
  124         #  Make file names work in Library Browser when the images in several
  125         #  files have the same names.
  126         my $libFix = "";
  127         if ($self->{setNumber} eq "Undefined_Set") {
  128           $libFix = $self->{fileName};
  129           $libFix =~ s!.*/!!, $libFix =~ s!\.pg(\..*)?$!!;
  130           $libFix =~ s![^a-zA-Z0-9._-]!!g;
  131           $libFix .= '-';
  132         }
  133   my $uniqIDstub = join("-",
  134                  $self->{studentLogin},
  135                  $self->{psvn},
  136                  'set'.$self->{setNumber},
  137                  'prob'.$self->{probNum},
  138                  $libFix,
  139   );
  140   $uniqIDstub =~ tr/@.,/___/;  # replace @ . and , by _ since they don't work well in file names
  141   $self->{uniqIDstub} = $uniqIDstub;
  142 
  143 }
  144 
  145 sub check_parameters {
  146   my $self = shift;
  147 
  148   # problem specific data
  149   warn "The path to the current problem file template is not defined."     unless $self->{fileName};
  150   warn "The current studentLogin is not defined "                          unless $self->{studentLogin};
  151   warn "The current problem set number is not defined"                     if $self->{setNumber} eq ""; # allow for sets equal to 0
  152   warn "The current problem number is not defined"                         if $self->{probNum} eq "";
  153   warn "The current problem set version number (psvn) is not defined"      unless defined($self->{psvn});
  154   warn "The displayMode is not defined"                                    unless $self->{displayMode};
  155 
  156   # required macros
  157 # warn "The macro &surePathToTmpFile can't be found"                    unless defined(&{$self->surePathToTmpFile()} );
  158 # warn "The macro &convertPath can't be found"                          unless defined(&{$self->convertPath()});
  159 # warn "The macro &directoryFromPath can't be found"                    unless defined(&{$self->directoryFromPath()});
  160 #    warn $self->surePathToTmpFile("foo");
  161   # warn "The webwork server does not have permission to execute the gif2eps script at  ${externalGif2EpsPath}." unless ( -x "${externalGif2EpsPath}" );
  162   # warn "The webwork server does not have permission to execute the png2eps script at ${externalPng2EpsPath}." unless ( -x "${externalPng2EpsPath}" );
  163 
  164   # required directory addresses (and URL address)
  165   warn "htmlDirectory is not defined." unless $self->{htmlDirectory};
  166   warn "htmlURL is not defined." unless $self->{htmlURL};
  167   warn "tempURL is not defined." unless $self->{tempURL};
  168 }
  169 
  170 sub make_alias {
  171     my $self = shift;
  172     # input is a path to the original auxiliary file
  173     my $aux_file_path = shift @_;
  174     my $resource_alias = new PGresource($aux_file_path);  # just call it alias? FIXME -- not in use yet.
  175 
  176   warn "Empty string used as input into the function alias" unless $aux_file_path;
  177 
  178   my $displayMode         = $self->{displayMode};
  179     my $fileName            = $self->{fileName};    # name of .pg file
  180   my $envir               = $self->{envir};
  181   my $htmlDirectory       = $envir->{htmlDirectory};
  182   my $htmlURL             = $envir->{htmlURL};
  183   my $tempDirectory       = $envir->{tempDirectory};
  184   my $tempURL             = $envir->{tempURL};
  185   my $studentLogin        = $envir->{studentLogin};
  186   my $psvn          = $envir->{psvn};
  187   my $setNumber           = $envir->{setNumber};
  188   my $probNum             = $envir->{probNum};
  189     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  190     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  191 
  192     my $templateDirectory   = $self->{templateDirectory};
  193 
  194   # $adr_output is a url in HTML and Latex2HTML modes
  195   # and a complete path in TEX mode.
  196   my $adr_output;
  197   my $ext;
  198 
  199   # determine file type
  200   # determine display mode
  201   # dispatch
  202 
  203   # determine extension, if there is one
  204   # if extension exists, strip and use the value for $ext
  205   # files without extensions are considered to be picture files:
  206 
  207 
  208   if ($aux_file_path =~ s/\.([^\.]*)$// ) {
  209     $ext = $1;
  210   } else {
  211     warn "This file name $aux_file_path did not have an extension.<BR> " .
  212          "Every file name used as an argument to alias must have an extension.<BR> " .
  213          "The permissable extensions are .gif, .png, and .html .<BR>";
  214     $ext  = "gif";
  215   }
  216 
  217 
  218   # in order to facilitate maintenance of this macro the routines for handling
  219   # different file types are defined separately.  This involves some redundancy
  220   # in the code but it makes it easier to define special handling for a new file
  221   # type, (but harder to change the behavior for all of the file types at once
  222   # (sigh)  ).
  223 
  224   if ($ext eq 'html') {
  225      $adr_output = $self->alias_for_html($aux_file_path)
  226   } elsif ($ext eq 'gif') {
  227     if ( $displayMode eq 'HTML_MathJax'||
  228          $displayMode eq 'HTML_dpng'||
  229          $displayMode eq 'HTML' ||
  230          $displayMode eq 'HTML_tth'||
  231          $displayMode eq 'HTML_asciimath'||
  232          $displayMode eq 'HTML_LaTeXMathML'||
  233          $displayMode eq 'HTML_jsMath'||
  234          $displayMode eq 'HTML_img') {
  235       ################################################################################
  236       # .gif FILES in HTML; HTML_tth; HTML_dpng; HTML_img; and Latex2HTML modes
  237       ################################################################################
  238       $adr_output=$self->alias_for_gif_in_html_mode($aux_file_path);
  239 
  240     } elsif ($displayMode eq 'TeX') {
  241       ################################################################################
  242       # .gif FILES in TeX mode
  243       ################################################################################
  244             $adr_output=$self->alias_for_gif_in_tex_mode($aux_file_path);
  245 
  246     } else {
  247       die "Error in alias: dangerousMacros.pl: unrecognizable displayMode = $displayMode";
  248     }
  249   } elsif ($ext eq 'png') {
  250     if ( $displayMode eq 'HTML_MathJax'||
  251          $displayMode eq 'HTML_dpng'||
  252          $displayMode eq 'HTML' ||
  253          $displayMode eq 'HTML_tth'||
  254          $displayMode eq 'HTML_asciimath'||
  255          $displayMode eq 'HTML_LaTeXMathML'||
  256          $displayMode eq 'HTML_jsMath'||
  257          $displayMode eq 'HTML_img' )  {
  258         $adr_output = $self->alias_for_png_in_html_mode($aux_file_path);
  259     } elsif ($displayMode eq 'TeX') {
  260       $adr_output = $self->alias_for_png_in_tex_mode($aux_file_path);
  261 
  262     } else {
  263       warn  "Error in alias: dangerousMacros.pl","unrecognizable displayMode = $displayMode","";
  264     }
  265   } else { # $ext is not recognized
  266     ################################################################################
  267     # FILES  with unrecognized file extensions in any display modes
  268     ################################################################################
  269 
  270     warn "Error in the macro alias. Alias does not understand how to process files with extension $ext.  (Path ot problem file is  $fileName) ";
  271   }
  272 
  273   warn "The macro alias was unable to form a URL for some auxiliary file used in this problem." unless $adr_output;
  274   return $adr_output;
  275 }
  276 
  277 
  278 
  279 sub alias_for_html {
  280   my $self = shift;
  281   my $aux_file_path = shift;
  282     # warn "aux_file for html $aux_file_path";
  283   my $envir               = $self->{envir};
  284   my $fileName            = $self->{fileName};
  285   my $htmlDirectory       = $envir->{htmlDirectory};
  286   my $htmlURL             = $envir->{htmlURL};
  287   my $tempDirectory       = $envir->{tempDirectory};
  288   my $tempURL             = $envir->{tempURL};
  289   my $studentLogin        = $envir->{studentLogin};
  290   my $psvn          = $envir->{psvn};
  291   my $setNumber           = $envir->{setNumber};
  292   my $probNum             = $envir->{probNum};
  293   my $displayMode         = $envir->{displayMode};
  294     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  295     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  296 
  297     my $templateDirectory   = $self->{templateDirectory};
  298 
  299 
  300   # $adr_output is a url in HTML and Latex2HTML modes
  301   # and a complete path in TEX mode.
  302   my $adr_output;
  303   my $ext                 =   "html";
  304     ################################################################################
  305     # .html FILES in HTML, HTML_tth, HTML_dpng, HTML_img, etc. and Latex2HTML mode
  306     ################################################################################
  307 
  308     # No changes are made for auxiliary files in the
  309     # ${Global::htmlDirectory} subtree.
  310     if ( $aux_file_path =~ m|^$tempDirectory| ) {
  311       $adr_output = $aux_file_path,
  312       $adr_output =~ s|$tempDirectory|$tempURL/|,
  313       $adr_output .= ".$ext",
  314     } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  315       $adr_output = $aux_file_path,
  316       $adr_output =~ s|$htmlDirectory|$htmlURL|,
  317       $adr_output .= ".$ext",
  318     } else {
  319       # HTML files not in the htmlDirectory are assumed under live under the
  320       # templateDirectory in the same directory as the problem.
  321       # Create an alias file (link) in the directory html/tmp/html which
  322       # points to the original file and return the URL of this alias.
  323       # Create all of the subdirectories of html/tmp/html which are needed
  324       # using sure file to path.
  325 
  326       # $fileName is obtained from environment for PGeval
  327       # it gives the  full path to the current problem
  328       my $filePath = $self->directoryFromPath($fileName);
  329       my $htmlFileSource = $self->convertPath("$templateDirectory${filePath}$aux_file_path.html");
  330       my $link = "html/".$self->{uniqIDstub}."-$aux_file_path.$ext";
  331       my $linkPath = $self->surePathToTmpFile($link);
  332       $adr_output = "${tempURL}$link";
  333       if (-e $htmlFileSource) {
  334         if (-e $linkPath) {
  335           unlink($linkPath) || warn "Unable to unlink alias file at |$linkPath|";
  336           # destroy the old link.
  337         }
  338         symlink( $htmlFileSource, $linkPath)
  339               || warn "The macro alias cannot create a link from |$linkPath|  to |$htmlFileSource| <BR>" ;
  340       } else {
  341         warn("The macro alias cannot find an HTML file at: |$htmlFileSource|");
  342       }
  343     }
  344   $adr_output;
  345 }
  346 
  347 
  348 sub alias_for_gif_in_html_mode {
  349   my $self = shift;
  350   my $aux_file_path = shift;
  351 #    warn "entering alias_for_gif_in_html_mode $aux_file_path";
  352 
  353   my $envir               = $self->{envir};
  354   my $fileName            = $self->{fileName};
  355   my $htmlDirectory       = $envir->{htmlDirectory};
  356   my $htmlURL             = $envir->{htmlURL};
  357   my $tempDirectory       = $envir->{tempDirectory};
  358   my $tempURL             = $envir->{tempURL};
  359   my $studentLogin        = $envir->{studentLogin};
  360   my $psvn          = $envir->{psvn};
  361   my $setNumber           = $envir->{setNumber};
  362   my $probNum             = $envir->{probNum};
  363   my $displayMode         = $envir->{displayMode};
  364     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  365     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  366 
  367     my $templateDirectory   = $self->{templateDirectory};
  368 
  369 
  370   # $adr_output is a url in HTML and Latex2HTML modes
  371   # and a complete path in TEX mode.
  372   my $adr_output;
  373   my $ext                 = "gif";
  374 
  375       ################################################################################
  376       # .gif FILES in HTML, HTML_tth, HTML_dpng, HTML_img, and Latex2HTML modes
  377       ################################################################################
  378 
  379       #warn "tempDirectory is $tempDirectory";
  380       #warn "file Path for auxiliary file is $aux_file_path";
  381 
  382       # No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  383       if ( $aux_file_path =~ m|^$tempDirectory| ) {
  384         $adr_output = $aux_file_path;
  385         $adr_output =~ s|$tempDirectory|$tempURL|;
  386         $adr_output .= ".$ext";
  387         #warn "adress out is $adr_output",
  388       } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  389         $adr_output = $aux_file_path;
  390         $adr_output =~ s|$htmlDirectory|$htmlURL|;
  391         $adr_output .= ".$ext";
  392       } else {
  393         # files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  394         # subtree in the same directory as the problem.
  395 
  396         # For a gif file the alias macro creates an alias under the html/images directory
  397         # which points to the gif file in the problem directory.
  398         # All of the subdirectories of html/tmp/gif which are needed are also created.
  399       #warn "fileName is $fileName   $self";
  400         my $filePath = ( $self->directoryFromPath($fileName) );
  401           #warn "filePath is $filePath";
  402         # $fileName is obtained from environment for PGeval
  403         # it gives the full path to the current problem
  404         my $gifSourceFile = $self->convertPath("$templateDirectory${filePath}$aux_file_path.gif");
  405         #my $link = "gif/$studentLogin-$psvn-set$setNumber-prob$probNum-$aux_file_path.$ext";
  406        #warn "fileName is $fileName filePath is $filePath gifSourceFile is $gifSourceFile";
  407 
  408         #  Make file names work in Library Browser when the images in several
  409         #  files have the same names.
  410         my $libFix = "";
  411         if ($setNumber eq "Undefined_Set") {
  412           $libFix = $fileName;
  413           $libFix =~ s!.*/!!, $libFix =~ s!\.pg(\..*)?$!!;
  414           $libFix =~ s![^a-zA-Z0-9._-]!!g;
  415           $libFix .= '-';
  416         }
  417 
  418         # my $link = "gif/$setNumber-prob$probNum-$libFix$aux_file_path.$ext";
  419           my $uniqIDstub = $self->{uniqIDstub};
  420         my $link = "gif/${uniqIDstub}-$aux_file_path.$ext";
  421         my $linkPath = $self->surePathToTmpFile($link);
  422         $adr_output = "${tempURL}$link";
  423         #warn "linkPath is $linkPath";
  424         #warn "adr_output is $adr_output";
  425         if (-e $gifSourceFile) {
  426           if (-e $linkPath) {
  427             unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  428           }
  429           symlink($gifSourceFile, $linkPath)
  430             || warn "The macro alias cannot create a link from |$linkPath|  to |$gifSourceFile| <BR>" ;
  431         } else {
  432           warn("The macro alias cannot find a GIF file at: |$gifSourceFile|");
  433         }
  434       }
  435     $adr_output;
  436 }
  437 
  438 sub alias_for_gif_in_tex_mode {
  439   my $self = shift;
  440   my $aux_file_path = shift;
  441 
  442   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  443   my $htmlDirectory       = $envir->{htmlDirectory};
  444   my $htmlURL             = $envir->{htmlURL};
  445   my $tempDirectory       = $envir->{tempDirectory};
  446   my $tempURL             = $envir->{tempURL};
  447   my $studentLogin        = $envir->{studentLogin};
  448   my $psvn          = $envir->{psvn};
  449   my $setNumber           = $envir->{setNumber};
  450   my $probNum             = $envir->{probNum};
  451   my $displayMode         = $envir->{displayMode};
  452     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  453     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  454 
  455     my $templateDirectory   = $self->{templateDirectory};
  456 
  457 
  458   # $adr_output is a url in HTML and Latex2HTML modes
  459   # and a complete path in TEX mode.
  460   my $adr_output;
  461   my $ext                 = "gif";
  462       ################################################################################
  463       # .gif FILES in TeX mode
  464       ################################################################################
  465 
  466             $setNumber =~ s/\./_/g;  ## extra dots confuse latex's graphics package
  467       if ($envir->{texDisposition} eq "pdf") {
  468         # We're going to create PDF files with our TeX (using pdflatex); so we
  469         # need images in PNG format.
  470 
  471         my $gifFilePath;
  472 
  473         if ($aux_file_path =~ m/^$htmlDirectory/ or $aux_file_path =~ m/^$tempDirectory/) {
  474           # we've got a full pathname to a file
  475           $gifFilePath = "$aux_file_path.gif";
  476         } else {
  477           # we assume the file is in the same directory as the problem source file
  478           my $dir = $self->directoryFromPath($fileName);
  479           $gifFilePath = "$templateDirectory${dir}$aux_file_path.gif";
  480         }
  481 
  482         my $gifFileName = $self->fileFromPath($gifFilePath);
  483         $gifFileName =~ /^(.*)\.gif$/;
  484           my $pngFilePath = $self->surePathToTmpFile("${tempDirectory}png/$setNumber-$probNum-$1.png");
  485         my $command = $envir->{externalGif2PngPath};
  486         my $returnCode = system "cat $gifFilePath | $command > $pngFilePath";
  487       #warn "FILE path $pngFilePath  exists =", -e $pngFilePath;
  488         if ($returnCode or not -e $pngFilePath) {
  489           warn "returnCode $returnCode: failed to convert $gifFilePath to $pngFilePath using gif->png with $command: $!";
  490         }
  491 
  492         $adr_output = $pngFilePath;
  493       } else {
  494         # Since we're not creating PDF files; we're probably just using a plain
  495         # vanilla latex. Hence; we need EPS images.
  496 
  497         ################################################################################
  498         # This is statement used below is system dependent.
  499         # Notice that the range of colors is restricted when converting to postscript to keep the files small
  500         # "cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmtops -noturn > $adr_output"
  501         # "cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn > $adr_output"
  502         ################################################################################
  503         if ($aux_file_path =~  m|^$htmlDirectory|  or $aux_file_path =~  m|^$tempDirectory|)  {
  504           # To serve an eps file copy an eps version of the gif file to the subdirectory of eps/
  505           my $linkPath = $self->directoryFromPath($fileName);
  506 
  507           my $gifSourceFile = "$aux_file_path.gif";
  508           my $gifFileName = $self->fileFromPath($gifSourceFile);
  509           $adr_output = $self->surePathToTmpFile("$tempDirectory/eps/$studentLogin-$psvn-$gifFileName.eps") ;
  510 
  511           if (-e $gifSourceFile) {
  512             #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output")
  513             system("cat $gifSourceFile | ${externalGif2EpsPath} > $adr_output" )
  514               && die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $probNum " .
  515                      "using the system dependent script\n |${externalGif2EpsPath}| \n";
  516           } else {
  517             die "|$gifSourceFile| cannot be found.  Problem number: |$probNum|";
  518           }
  519         } else {
  520           # To serve an eps file copy an eps version of the gif file to  a subdirectory of eps/
  521           my $filePath = $self->directoryFromPath($fileName);
  522           my $gifSourceFile = "${templateDirectory}${filePath}$aux_file_path.gif";
  523           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  524           $adr_output = $self->surePathToTmpFile("eps/$studentLogin-$psvn-set$setNumber-prob$probNum-$aux_file_path.eps");
  525 
  526           if (-e $gifSourceFile) {
  527             #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  528             #warn "Unable to create eps file: |$adr_output|\n from file\n |$gifSourceFile|\n in problem $probNum";
  529             #warn "Help ${:externalGif2EpsPath}" unless -x "${main::externalGif2EpsPath}";
  530             system("cat $gifSourceFile | ${externalGif2EpsPath} > $adr_output" )
  531               && die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $probNum " .
  532                      "using the system dependent commands \n |${externalGif2EpsPath}| \n ";
  533           }  else {
  534             die "|$gifSourceFile| cannot be found.  Problem number: |$probNum|";
  535           }
  536         }
  537       }
  538   $adr_output;
  539 
  540 }
  541 sub alias_for_png_in_html_mode {
  542   my $self = shift;
  543   my $aux_file_path = shift;
  544 
  545   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  546   my $htmlDirectory       = $envir->{htmlDirectory};
  547   my $htmlURL             = $envir->{htmlURL};
  548   my $tempDirectory       = $envir->{tempDirectory};
  549   my $tempURL             = $envir->{tempURL};
  550   my $studentLogin        = $envir->{studentLogin};
  551   my $psvn          = $envir->{psvn};
  552   my $setNumber           = $envir->{setNumber};
  553   my $probNum             = $envir->{probNum};
  554   my $displayMode         = $envir->{displayMode};
  555     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  556     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  557 
  558     my $templateDirectory   = $self->{templateDirectory};
  559 
  560 
  561   # $adr_output is a url in HTML and Latex2HTML modes
  562   # and a complete path in TEX mode.
  563   my $adr_output;
  564   my $ext                 = "png";
  565       ################################################################################
  566       # .png FILES in HTML; HTML_tth; HTML_dpng; HTML_img; etc. and Latex2HTML modes
  567       ################################################################################
  568 
  569       #warn "tempDirectory is $tempDirectory";
  570       #warn "file Path for auxiliary file is $aux_file_path";
  571 
  572       # No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  573       if ( $aux_file_path =~ m|^$tempDirectory| ) {
  574       $adr_output = $aux_file_path;
  575         $adr_output =~ s|$tempDirectory|$tempURL|;
  576         $adr_output .= ".$ext";
  577         #warn "adress out is $adr_output";
  578       } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  579         $adr_output = $aux_file_path;
  580         $adr_output =~ s|$htmlDirectory|$htmlURL|;
  581         $adr_output .= ".$ext";
  582       } else {
  583         # files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  584         # subtree in the same directory as the problem.
  585 
  586         # For a png file the alias macro creates an alias under the html/images directory
  587         # which points to the png file in the problem directory.
  588         # All of the subdirectories of html/tmp/gif which are needed are also created.
  589         my $filePath = $self->directoryFromPath($fileName);
  590 
  591         # $fileName is obtained from environment for PGeval
  592         # it gives the full path to the current problem
  593         my $pngSourceFile = $self->convertPath("$templateDirectory${filePath}$aux_file_path.png");
  594         my $uniqIDstub = $self->{uniqIDstub};
  595         my $link = "gif/${uniqIDstub}-$aux_file_path.$ext";
  596         my $linkPath = $self->surePathToTmpFile($link);
  597         $adr_output = "${tempURL}$link";
  598         #warn "linkPath is $linkPath";
  599         #warn "adr_output is $adr_output";
  600         if (-e $pngSourceFile) {
  601           if (-e $linkPath) {
  602             unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  603           }
  604           symlink($pngSourceFile, $linkPath)
  605           || warn "The macro alias cannot create a link from |$linkPath|  to |$pngSourceFile| <BR>" ;
  606         } else {
  607           warn("The macro alias cannot find a PNG file at: |$pngSourceFile|");
  608         }
  609       }
  610   $adr_output;
  611 
  612 }
  613 
  614 sub alias_for_png_in_tex_mode {
  615 
  616     my $self = shift;
  617   my $aux_file_path = shift;
  618 
  619   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  620   my $htmlDirectory       = $envir->{htmlDirectory};
  621   my $htmlURL             = $envir->{htmlURL};
  622   my $tempDirectory       = $envir->{tempDirectory};
  623   my $tempURL             = $envir->{tempURL};
  624   my $studentLogin        = $envir->{studentLogin};
  625   my $psvn          = $envir->{psvn};
  626   my $setNumber           = $envir->{setNumber};
  627   my $probNum             = $envir->{probNum};
  628   my $displayMode         = $envir->{displayMode};
  629     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  630     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  631 
  632     my $templateDirectory   = $self->{templateDirectory};
  633 
  634 
  635   # $adr_output is a url in HTML and Latex2HTML modes
  636   # and a complete path in TEX mode.
  637   my $adr_output;
  638   my $ext                 = "png";
  639             ################################################################################
  640       # .png FILES in TeX mode
  641       ################################################################################
  642 
  643             $setNumber =~ s/\./_/g;  ## extra dots confuse latex's graphics package
  644       if ($envir->{texDisposition} eq "pdf") {
  645         # We're going to create PDF files with our TeX (using pdflatex); so we
  646         # need images in PNG format. what luck! they're already in PDF format!
  647 
  648         my $pngFilePath;
  649 
  650         if ($aux_file_path =~ m/^$htmlDirectory/ or $aux_file_path =~ m/^$tempDirectory/) {
  651           # we've got a full pathname to a file
  652           $pngFilePath = "$aux_file_path.png";
  653         } else {
  654           # we assume the file is in the same directory as the problem source file
  655           my $dir = $self->directoryFromPath($fileName);
  656           $pngFilePath = "$templateDirectory${dir}$aux_file_path.png";
  657         }
  658 
  659         $adr_output = $pngFilePath;
  660       } else {
  661         # Since we're not creating PDF files; we're probably just using a plain
  662         # vanilla latex. Hence; we need EPS images.
  663 
  664         ################################################################################
  665         # This is statement used below is system dependent.
  666         # Notice that the range of colors is restricted when converting to postscript to keep the files small
  667         # "cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmtops -noturn > $adr_output"
  668         # "cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn > $adr_output"
  669         ################################################################################
  670 
  671         if ($aux_file_path =~  m|^$htmlDirectory|  or $aux_file_path =~  m|^$tempDirectory|)  {
  672           # To serve an eps file copy an eps version of the png file to the subdirectory of eps/
  673           my $linkPath = $self->directoryFromPath($fileName);
  674 
  675           my $pngSourceFile = "$aux_file_path.png";
  676           my $pngFileName = fileFromPath($pngSourceFile);
  677           $adr_output = $self->surePathToTmpFile("$tempDirectory/eps/$studentLogin-$psvn-$pngFileName.eps") ;
  678 
  679           if (-e $pngSourceFile) {
  680             #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output")
  681             system("cat $pngSourceFile | ${externalPng2EpsPath} > $adr_output" )
  682               && die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $probNum " .
  683                      "using the system dependent commands\n |${externalPng2EpsPath}| \n";
  684           } else {
  685             die "|$pngSourceFile| cannot be found.  Problem number: |$probNum|";
  686           }
  687         } else {
  688           # To serve an eps file copy an eps version of the png file to  a subdirectory of eps/
  689           my $filePath = $self->directoryFromPath($fileName);
  690           my $pngSourceFile = "${templateDirectory}${filePath}$aux_file_path.png";
  691           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  692           $adr_output = $self->surePathToTmpFile("eps/$studentLogin-$psvn-set$setNumber-prob$probNum-$aux_file_path.eps") ;
  693           if (-e $pngSourceFile) {
  694             #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  695             #warn "Unable to create eps file: |$adr_output|\n from file\n |$pngSourceFile|\n in problem $probNum";
  696             #warn "Help ${externalPng2EpsPath}" unless -x "${externalPng2EpsPath}";
  697             system("cat $pngSourceFile | ${externalPng2EpsPath} > $adr_output" )
  698               && die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $probNum " .
  699                      "using the system dependent commands\n |${externalPng2EpsPath}| \n ";
  700           } else {
  701             die "|$pngSourceFile| cannot be found.  Problem number: |$probNum|";
  702           }
  703         }
  704       }
  705   $adr_output;
  706 
  707 }
  708 
  709 ################################################
  710 
  711 # More resource search macros
  712 
  713 ################################################
  714 
  715 #
  716 #  Look for a macro file in the directories specified in the macros path
  717 #
  718 
  719 # ^variable my $macrosPath
  720 our ($macrosPath,
  721     # ^variable my $pwd
  722     $pwd,
  723     # ^variable my $appletPath
  724     $appletPath,
  725     # ^variable my $server_root_url
  726     $server_root_url,
  727   # ^variable my $templateDirectory
  728   $templateDirectory,
  729   # ^variable my $scriptDirectory
  730   $scriptDirectory,
  731   # ^variable my $externalTTHPath
  732   $externalTTHPath,
  733   );
  734 
  735 # ^function findMacroFile
  736 # ^uses $macrosPath
  737 # ^uses $pwd
  738 sub findMacroFile {
  739   my $self   = shift;
  740   my $fileName = shift;
  741   my $filePath;
  742   foreach my $dir (@{$macrosPath}) {
  743     $filePath = "$dir/$fileName";
  744     $filePath =~ s!^\.\.?/!$pwd/!;
  745     return $filePath if (-r $filePath);
  746   }
  747   return;  # no file found
  748 }
  749 
  750 # ^function check_url
  751 # ^uses %envir
  752 sub check_url {
  753   my $self = shift;
  754   my $url  = shift;
  755   my $OK_CONSTANT = "200 OK";
  756   return undef if $url =~ /;/;   # make sure we can't get a second command in the url
  757   #FIXME -- check for other exploits of the system call
  758   #FIXME -- ALARM feature so that the response cannot be held up for too long.
  759   #FIXME doesn't seem to work with relative addresses.
  760   #FIXME  Can we get the machine name of the server?
  761 
  762    my $check_url_command = $self->{envir}->{externalCheckUrl};
  763    my $response = `$check_url_command $url`;
  764   return ($response =~ /^$OK_CONSTANT/) ? 1 : 0;
  765 }
  766 
  767 # ^variable our %appletCodebaseLocations
  768 
  769 # ^function findAppletCodebase
  770 # ^uses %appletCodebaseLocations
  771 # ^uses $appletPath
  772 # ^uses $server_root_url
  773 # ^uses check_url
  774 
  775 our %appletCodebaseLocations = ();   # cache for found applets (lasts until the child exits
  776 sub findAppletCodebase {
  777   my $self     = shift;
  778   my $fileName = shift;  # probably the name of a jar file
  779   $server_root_url=$self->envir("server_root_url");
  780   #check cache first
  781   if (defined($appletCodebaseLocations{$fileName})
  782         and $appletCodebaseLocations{$fileName} =~/\S/  )
  783   {
  784       return $appletCodebaseLocations{$fileName}; # return if found in cache
  785   }
  786   my $appletPath = $self->{appletPath};
  787   foreach my $appletLocation (@{$appletPath}) {
  788     if ($appletLocation =~ m|^/|) {
  789       $appletLocation = "$server_root_url$appletLocation";
  790     }
  791     my $url = "$appletLocation/$fileName";
  792 
  793     if ($self->check_url($url)) {
  794         $appletCodebaseLocations{$fileName} = $appletLocation; #update cache
  795       return $appletLocation   # return codebase part of url
  796     }
  797   }
  798   warn "findAppletCodebase Error: $fileName not found after searching ". join(",  ", @{$appletPath} );
  799   return "";
  800 }
  801 
  802 
  803 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9