File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.34: download - view: text, annotated - select for diffs
Mon Oct 31 18:52:10 2005 UTC (18 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- : in filename (BUG#4432)

    1: # The LearningOnline Network with CAPA
    2: # Server for RAT Maps
    3: #
    4: # $Id: lonratsrv.pm,v 1.34 2005/10/31 18:52:10 albertel Exp $
    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: 
   29: package Apache::lonratsrv;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::File;
   34: use HTML::TokeParser;
   35: use Apache::lonnet;
   36: 
   37: # ------------------------------------------------------------- From RAT to XML
   38: 
   39: sub qtescape {
   40:     my $str=shift;
   41:     $str=~s/\:/\:/g;
   42:     $str=~s/\&\#58\;/\:/g;
   43:     $str=~s/\&\#39\;/\'/g;
   44:     $str=~s/\&\#44\;/\,/g;
   45:     $str=~s/\"/\&\#34\;/g;
   46:     return $str;
   47: }
   48: 
   49: # ------------------------------------------------------------- From XML to RAT
   50: 
   51: sub qtunescape {
   52:     my $str=shift;
   53:     $str=~s/\:/\&colon\;/g;
   54:     $str=~s/\'/\&\#39\;/g;
   55:     $str=~s/\,/\&\#44\;/g;
   56:     $str=~s/\"/\&\#34\;/g;
   57:     return $str;
   58: }
   59: 
   60: # --------------------------------------------------------- Loads map from disk
   61: 
   62: sub loadmap {
   63:     my ($fn,$errtext,$infotext)=@_;
   64:     if ($errtext) { return('',$errtext); }
   65:     my $outstr='';
   66:     my @obj=();
   67:     my @links=();
   68:     my $instr='';
   69:     if ($fn=~/^\/*uploaded\//) {
   70:         $instr=&Apache::lonnet::getfile($fn);
   71:     } elsif (-e $fn) {
   72:         my @content=();
   73:         {
   74: 	    my $fh=Apache::File->new($fn);
   75:             @content=<$fh>;
   76:         }
   77:         $instr=join('',@content);
   78:     }
   79:     if ($instr eq -2) {
   80:         $errtext.='Map not loaded: An error occured while trying to load the map.';
   81:     } elsif ($instr) {
   82:         my $parser = HTML::TokeParser->new(\$instr);
   83:         my $token;
   84:         my $graphmode=0;
   85: 
   86:         $fn=~/\.(\w+)$/;
   87:         $outstr="mode<:>$1";
   88: 
   89:         while ($token = $parser->get_token) {
   90: 	    if ($token->[0] eq 'S') {
   91:                 if ($token->[1] eq 'map') {
   92: 		    $graphmode=($token->[2]->{'mode'} eq 'rat/graphical');
   93:                 } elsif ($token->[1] eq 'resource') {
   94: # -------------------------------------------------------------------- Resource
   95:                     $outstr.='<&>objcont';
   96:                     if (defined($token->[2]->{'id'})) {
   97: 			$outstr.='<:>'.$token->[2]->{'id'};
   98:                         if ($obj[$token->[2]->{'id'}]==1) {
   99:                            $errtext.='Error: multiple use of ID '.
  100:                                      $token->[2]->{'id'}.'. ';
  101:                         }
  102:                         $obj[$token->[2]->{'id'}]=1; 
  103:                     } else {
  104:                         my $i=1;
  105:                         while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
  106:                         $outstr.='<:>'.$i;
  107:                         $obj[$i]=1;
  108:                     }
  109:                     $outstr.='<:>';
  110:                     $outstr.=qtunescape($token->[2]->{'title'}).":";
  111:                     $outstr.=qtunescape($token->[2]->{'src'}).":";
  112:                     if ($token->[2]->{'external'} eq 'true') {
  113:                         $outstr.='true:';
  114:                     } else {
  115:                         $outstr.='false:';
  116:                     }
  117:                     if (defined($token->[2]->{'type'})) {
  118: 			$outstr.=$token->[2]->{'type'}.':';
  119:                     }  else {
  120:                         $outstr.='normal:';
  121:                     }
  122: 		    if ($token->[2]->{'type'} ne 'zombie') {
  123: 			$outstr.='res';
  124: 		    } else {
  125:                         $outstr.='zombie';
  126: 		    }
  127:                 } elsif ($token->[1] eq 'condition') {
  128: # ------------------------------------------------------------------- Condition
  129:                     $outstr.='<&>objcont';
  130:                     if (defined($token->[2]->{'id'})) {
  131: 			$outstr.='<:>'.$token->[2]->{'id'};
  132:                         if ($obj[$token->[2]->{'id'}]==1) {
  133:                            $errtext.='Error: multiple use of ID '.
  134:                                      $token->[2]->{'id'}.'. ';
  135:                         }
  136:                         $obj[$token->[2]->{'id'}]=1; 
  137:                     } else {
  138:                         my $i=1;
  139:                         while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
  140:                         $outstr.='<:>'.$i;
  141:                         $obj[$i]=1;
  142:                     }
  143:                     $outstr.='<:>';
  144:                     $outstr.=qtunescape($token->[2]->{'value'}).':';
  145:                     if (defined($token->[2]->{'type'})) {
  146: 			$outstr.=$token->[2]->{'type'}.':';
  147:                     } else {
  148:                         $outstr.='normal:';
  149:                     }
  150:                     $outstr.='cond';
  151:                 } elsif ($token->[1] eq 'link') {
  152: # ----------------------------------------------------------------------- Links
  153:                     $outstr.='<&>objlinks';
  154: 
  155:                         if (defined($token->[2]->{'index'})) {
  156: 			   if ($links[$token->[2]->{'index'}]) {
  157:                                $errtext.='Error: multiple use of link index '.
  158: 			       $token->[2]->{'index'}.'. ';
  159:                            }
  160: 			   $outstr.='<:>'.$token->[2]->{'index'};
  161:                            $links[$token->[2]->{'index'}]=1;
  162:                         } else {
  163:                            my $i=1;
  164:                            while (($i<=$#links) && ($links[$i]==1)) { $i++; }
  165:                            $outstr.='<:>'.$i;
  166:                            $links[$i]=1;
  167: 		       }
  168: 		    
  169:                     $outstr.='<:>'.$token->[2]->{'from'}.
  170:                              ':'.$token->[2]->{'to'};
  171:                     if (defined($token->[2]->{'condition'})) {
  172: 			$outstr.=':'.$token->[2]->{'condition'};
  173:                     } else {
  174:  			$outstr.=':0';
  175:                     }
  176: # ------------------------------------------------------------------- Parameter
  177:                 } elsif ($token->[1] eq 'param') {
  178:                     $outstr.='<&>objparms<:>'.$token->[2]->{'to'}.'<:>'.
  179:                             $token->[2]->{'type'}.'___'.$token->[2]->{'name'}
  180:                                                  .'___'.$token->[2]->{'value'};
  181:                 } elsif ($graphmode) {
  182: # --------------------------------------------- All other tags (graphical only)
  183:                     $outstr.='<&>'.$token->[1];
  184:                     if (defined($token->[2]->{'index'})) {
  185: 			$outstr.='<:>'.$token->[2]->{'index'};
  186:                         if ($token->[1] eq 'obj') {
  187: 			    $obj[$token->[2]->{'index'}]=2;
  188:                         }
  189:                     }
  190:                     $outstr.='<:>'.$token->[2]->{'value'};
  191:                 }
  192:             }
  193:         }
  194: 
  195:     } else {
  196:         $errtext.='Map not loaded: The file does not exist. ';
  197:     }
  198:     return($outstr,$errtext,$infotext);
  199: }
  200: 
  201: 
  202: # ----------------------------------------------------------- Saves map to disk
  203: 
  204: sub savemap {
  205:     my ($fn,$errtext)=@_;
  206:     my $infotext='';
  207:     my %alltypes;
  208:     my %allvalues;
  209:     if (($fn=~/\.sequence(\.tmp)*$/) ||
  210:         ($fn=~/\.page(\.tmp)*$/)) {
  211: 
  212: # ------------------------------------------------------------- Deal with input
  213:         my @tags=split(/<&>/,$env{'form.output'});
  214:         my $outstr='';
  215:         my $graphdef=0;
  216:         if ($tags[0] eq 'graphdef<:>yes') {
  217: 	    $outstr='<map mode="rat/graphical">'."\n";
  218:             $graphdef=1;
  219:         } else {
  220:             $outstr="<map>\n";
  221:         }
  222:         foreach (@tags) {
  223: 	   my @parts=split(/<:>/,$_);
  224:            if ($parts[0] eq 'objcont') {
  225:                my @comp=split(/:/,$parts[$#parts]);
  226: # --------------------------------------------------------------- Logical input
  227: 	       if (($comp[$#comp] eq 'res') || ($comp[$#comp] eq 'zombie')) {
  228:                    $comp[0]=qtescape($comp[0]);
  229:                    $comp[1]=qtescape($comp[1]);
  230:                    if ($comp[2] eq 'true') {
  231: 		       if ($comp[1]!~/^http\:\/\//) {
  232: 			   $comp[1]='http://'.$comp[1];
  233:                        }
  234:                        $comp[1].='" external="true';
  235:                    } else {
  236: 		       if ($comp[1]=~/^http\:\/\//) {
  237: 			   $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
  238:                        }
  239:                    }
  240: 		   $outstr.='<resource id="'.$parts[1].'" src="'
  241:                           .$comp[1].'"';
  242: 
  243:                    if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
  244: 		       $outstr.=' type="'.$comp[3].'"';
  245:                    }
  246:                    if ($comp[0] ne '') {
  247: 		       $outstr.=' title="'.$comp[0].'"';
  248:                    }
  249:                    $outstr.=" />\n";
  250:                } elsif ($comp[$#comp] eq 'cond') {
  251:                    $outstr.='<condition id="'.$parts[1].'"';
  252:                    if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
  253: 		       $outstr.=' type="'.$comp[1].'"';
  254:                    }
  255:                    $outstr.=' value="'.qtescape($comp[0]).'"';
  256:                    $outstr.=" />\n";
  257:                }
  258:            } elsif ($parts[0] eq 'objlinks') {
  259:                my @comp=split(/:/,$parts[$#parts]);
  260:                $outstr.='<link';
  261:                $outstr.=' from="'.$comp[0].'"';
  262:                $outstr.=' to="'.$comp[1].'"';
  263:                if (($comp[2] ne '') && ($comp[2]!=0)) {
  264:                   $outstr.=' condition="'.$comp[2].'"';
  265:                }
  266:                $outstr.=' index="'.$parts[1].'"';
  267:                $outstr.=" />\n";
  268:            } elsif ($parts[0] eq 'objparms') {
  269:                undef %alltypes;
  270:                undef %allvalues;
  271:                foreach (split(/:/,$parts[$#parts])) {
  272:                    my ($type,$name,$value)=split(/\_\_\_/,$_);
  273:                    $alltypes{$name}=$type;
  274:                    $allvalues{$name}=$value;
  275:                }
  276:                foreach (keys %allvalues) {
  277:                   if ($allvalues{$_} ne '') {
  278:                    $outstr.='<param to="'.$parts[1].'" type="'
  279:                           .$alltypes{$_}.'" name="'.$_
  280:                           .'" value="'.$allvalues{$_}.'" />'
  281:                           ."\n";
  282: 	          }
  283:                }
  284:            } elsif (($parts[0] ne '') && ($graphdef)) {
  285: # ------------------------------------------------------------- Graphical input
  286:                $outstr.='<'.$parts[0];
  287:                if ($#parts==2) {
  288: 		   $outstr.=' index="'.$parts[1].'"';
  289:                }
  290:                $outstr.=' value="'.qtescape($parts[$#parts]).'" />'."\n";
  291:            }
  292:         }
  293:         $outstr.="</map>\n";
  294: 	if ($fn=~/^\/*uploaded\/(\w+)\/(\w+)\/(.*)$/) {
  295: 	    $env{'form.output'}=$outstr;
  296:             my $result=&Apache::lonnet::finishuserfileupload($2,$1,
  297: 							     'output',$3);
  298: 	    if ($result != m|^/uploaded/|) {
  299: 		$errtext.='Map not saved: A network error occured when trying to save the map. ';
  300: 	    }
  301:         } else {
  302:           my $fh;
  303:           if ($fh=Apache::File->new(">$fn")) {
  304:              print $fh $outstr;
  305:              $infotext.="Map saved as $fn. ";
  306: 	  } else {
  307:              $errtext.='Could not write file '.$fn.'.  Map not saved. ';
  308: 	  }
  309:         }
  310:     } else {
  311: # -------------------------------------------- Cannot write to that file, error
  312:         $errtext.='Map not saved: The specified path does not exist. ';
  313:     }
  314:     return ($errtext,$infotext);
  315: }
  316: 
  317: # ================================================================ Main Handler
  318: 
  319: sub handler {
  320:   my $r=shift;
  321:   &Apache::loncommon::content_type($r,'text/html');
  322:   $r->send_http_header;
  323: 
  324:   return OK if $r->header_only;
  325: 
  326:   my $url=$r->uri;
  327:   $url=~/\/(\w+)\/ratserver$/;
  328:   my $mode=$1;
  329: 
  330:   $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
  331:   
  332:   my $fn=$r->filename;
  333:   my $lonDocRoot=$r->dir_config('lonDocRoot');
  334:   if ( $fn =~ /$lonDocRoot/ ) {
  335:       #internal authentication, needs fixup.
  336:       $fn = $url;
  337:       $fn=~s|^/~(\w+)|/home/$1/public_html|;
  338:       $fn=~s|/[^/]*/ratserver$||;
  339:   }
  340:   my $errtext='';
  341:   my $infotext='';
  342:   my $outtext='';
  343: 
  344:   if ($mode ne 'loadonly') {
  345:      ($errtext,$infotext)=&savemap($fn,$errtext);
  346:   }
  347:   ($outtext,$errtext,$infotext)=&loadmap($fn,$errtext,$infotext);
  348: 
  349:   $r->print(<<ENDDOCUMENT);
  350: <html>
  351: <body bgcolor="#FFFFFF">
  352: <form name=storage method=post action="$url">
  353: <input type=hidden name=output value="$outtext">
  354: </form>
  355: <script>
  356:     parent.flag=1;
  357: </script>
  358: ENDDOCUMENT
  359:     if (($errtext ne '') || ($infotext ne '')) {
  360: 	$r->print(<<ENDSCRIPT);
  361: <script>
  362:     alert("$infotext $errtext");
  363: </script>
  364: ENDSCRIPT
  365:     }
  366:     $r->print("</body>\n</html>\n");
  367: 
  368:   return OK;
  369: }
  370: 
  371: 1;
  372: __END__

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