File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.32: download - view: text, annotated - select for diffs
Mon Jun 13 20:23:53 2005 UTC (18 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1, version_1_99_0, HEAD
- eliminating $home from that args to finishuserfileupload it shoudn't have had it in the first place

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

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