File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.22: download - view: text, annotated - select for diffs
Sat Aug 31 00:42:30 2002 UTC (21 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Fixes "View" Bug
Provides temporary file/save mechanism (both edt and srv needed)

    1: # The LearningOnline Network with CAPA
    2: # Server for RAT Maps
    3: #
    4: # $Id: lonratsrv.pm,v 1.22 2002/08/31 00:42:30 www 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: # (Edit Handler for RAT Maps
   29: # (TeX Content Handler
   30: #
   31: # 05/29/00,05/30 Gerd Kortemeyer)
   32: # 7/1 Gerd Kortemeyer)
   33: # 7/1,7/3,7/4,7/7,7/8,7/10,7/26,10/2 Gerd Kortemeyer
   34: # 4/30/2001 Scott Harrison
   35: # 5/3,06/25,07/03,07/04,07/05 Gerd Kortemeyer
   36: 
   37: package Apache::lonratsrv;
   38: 
   39: use strict;
   40: use Apache::Constants qw(:common);
   41: use Apache::File;
   42: use HTML::TokeParser;
   43: 
   44: 
   45: # ------------------------------------------------------------- From RAT to XML
   46: 
   47: sub qtescape {
   48:     my $str=shift;
   49:     $str=~s/\&\#58\;/\:/g;
   50:     $str=~s/\&\#39\;/\'/g;
   51:     $str=~s/\&\#44\;/\,/g;
   52:     $str=~s/\"/\&\#34\;/g;
   53:     return $str;
   54: }
   55: 
   56: # ------------------------------------------------------------- From XML to RAT
   57: 
   58: sub qtunescape {
   59:     my $str=shift;
   60:     $str=~s/\:/\&colon\;/g;
   61:     $str=~s/\'/\&\#39\;/g;
   62:     $str=~s/\,/\&\#44\;/g;
   63:     $str=~s/\"/\&\#34\;/g;
   64:     return $str;
   65: }
   66: 
   67: # --------------------------------------------------------- Loads map from disk
   68: 
   69: sub loadmap {
   70:     my ($fn,$errtext)=@_;
   71:     my $outstr='';
   72:     my @obj=();
   73:     my @links=();
   74:     my $instr='';
   75:     if ($fn=~/^\/*uploaded\//) {
   76:         $instr=&Apache::lonnet::getfile($fn);
   77:     } elsif (-e $fn) {
   78:         my @content=();
   79:         {
   80: 	    my $fh=Apache::File->new($fn);
   81:             @content=<$fh>;
   82:         }
   83:         $instr=join('',@content);
   84:     }
   85:     if ($instr) {
   86:         my $parser = HTML::TokeParser->new(\$instr);
   87:         my $token;
   88:         my $graphmode=0;
   89: 
   90:         $fn=~/\.(\w+)$/;
   91:         $outstr="mode<:>$1";
   92: 
   93:         while ($token = $parser->get_token) {
   94: 	    if ($token->[0] eq 'S') {
   95:                 if ($token->[1] eq 'map') {
   96: 		    $graphmode=($token->[2]->{'mode'} eq 'rat/graphical');
   97:                 } elsif ($token->[1] eq 'resource') {
   98: # -------------------------------------------------------------------- Resource
   99:                     $outstr.='<&>objcont';
  100:                     if ($token->[2]->{'id'}) {
  101: 			$outstr.='<:>'.$token->[2]->{'id'};
  102:                         if ($obj[$token->[2]->{'id'}]==1) {
  103:                            $errtext.='Error: multiple use of ID '.
  104:                                      $token->[2]->{'id'}.'. ';
  105:                         }
  106:                         $obj[$token->[2]->{'id'}]=1; 
  107:                     } else {
  108:                         my $i=1;
  109:                         while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
  110:                         $outstr.='<:>'.$i;
  111:                         $obj[$i]=1;
  112:                     }
  113:                     $outstr.='<:>';
  114:                     $outstr.=qtunescape($token->[2]->{'title'}).":";
  115:                     $outstr.=qtunescape($token->[2]->{'src'}).":";
  116:                     if ($token->[2]->{'external'} eq 'true') {
  117:                         $outstr.='true:';
  118:                     } else {
  119:                         $outstr.='false:';
  120:                     }
  121:                     if ($token->[2]->{'type'}) {
  122: 			$outstr.=$token->[2]->{'type'}.':';
  123:                     }  else {
  124:                         $outstr.='normal:';
  125:                     }
  126:                     $outstr.='res';
  127:                 } elsif ($token->[1] eq 'condition') {
  128: # ------------------------------------------------------------------- Condition
  129:                     $outstr.='<&>objcont';
  130:                     if ($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 ($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 ($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 ($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);
  199: }
  200: 
  201: 
  202: # ----------------------------------------------------------- Saves map to disk
  203: 
  204: sub savemap {
  205:     my ($fn,$errtext)=@_;
  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:         map {
  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') {
  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.="></resource>\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.="></condition>\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.="></link>\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:                           ."</param>\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]).'"></'.
  290:                         $parts[0].">\n";
  291:            }
  292:         } @tags;
  293:         $outstr.="</map>\n";
  294: 	if ($fn=~/^\/*uploaded\//) {
  295: 	    $ENV{'form.output'}=$outstr;
  296:             &Apache::lonnet::finishuserfileupload(
  297:               $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  298:               $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  299:               $ENV{'course.'.$ENV{'request.course.id'}.'.home'},
  300:               'output',(split(/\//,$fn))[-1]);
  301:         } else {
  302:           my $fh;
  303:           if ($fh=Apache::File->new(">$fn")) {
  304:              print $fh $outstr;
  305:              $errtext.="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;
  315: }
  316: 
  317: # ================================================================ Main Handler
  318: 
  319: sub handler {
  320:   my $r=shift;
  321:   $r->content_type('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 $outtext='';
  342: 
  343:   if ($mode ne 'loadonly') {
  344:      $errtext=&savemap($fn,$errtext);
  345:   }
  346:   ($outtext,$errtext)=&loadmap($fn,$errtext);
  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 '') {
  359: 	$r->print(<<ENDSCRIPT);
  360: <script>
  361:     alert("$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>
500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.