File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.29: download - view: text, annotated - select for diffs
Thu Feb 17 09:09:57 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- more $r->content_type eradication

    1: # The LearningOnline Network with CAPA
    2: # Server for RAT Maps
    3: #
    4: # $Id: lonratsrv.pm,v 1.29 2005/02/17 09:09:57 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: 
   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:                     $outstr.='res';
  122:                 } elsif ($token->[1] eq 'condition') {
  123: # ------------------------------------------------------------------- Condition
  124:                     $outstr.='<&>objcont';
  125:                     if ($token->[2]->{'id'}) {
  126: 			$outstr.='<:>'.$token->[2]->{'id'};
  127:                         if ($obj[$token->[2]->{'id'}]==1) {
  128:                            $errtext.='Error: multiple use of ID '.
  129:                                      $token->[2]->{'id'}.'. ';
  130:                         }
  131:                         $obj[$token->[2]->{'id'}]=1; 
  132:                     } else {
  133:                         my $i=1;
  134:                         while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
  135:                         $outstr.='<:>'.$i;
  136:                         $obj[$i]=1;
  137:                     }
  138:                     $outstr.='<:>';
  139:                     $outstr.=qtunescape($token->[2]->{'value'}).':';
  140:                     if ($token->[2]->{'type'}) {
  141: 			$outstr.=$token->[2]->{'type'}.':';
  142:                     } else {
  143:                         $outstr.='normal:';
  144:                     }
  145:                     $outstr.='cond';
  146:                 } elsif ($token->[1] eq 'link') {
  147: # ----------------------------------------------------------------------- Links
  148:                     $outstr.='<&>objlinks';
  149: 
  150:                         if ($token->[2]->{'index'}) {
  151: 			   if ($links[$token->[2]->{'index'}]) {
  152:                                $errtext.='Error: multiple use of link index '.
  153: 			       $token->[2]->{'index'}.'. ';
  154:                            }
  155: 			   $outstr.='<:>'.$token->[2]->{'index'};
  156:                            $links[$token->[2]->{'index'}]=1;
  157:                         } else {
  158:                            my $i=1;
  159:                            while (($i<=$#links) && ($links[$i]==1)) { $i++; }
  160:                            $outstr.='<:>'.$i;
  161:                            $links[$i]=1;
  162: 		       }
  163: 		    
  164:                     $outstr.='<:>'.$token->[2]->{'from'}.
  165:                              ':'.$token->[2]->{'to'};
  166:                     if ($token->[2]->{'condition'}) {
  167: 			$outstr.=':'.$token->[2]->{'condition'};
  168:                     } else {
  169:  			$outstr.=':0';
  170:                     }
  171: # ------------------------------------------------------------------- Parameter
  172:                 } elsif ($token->[1] eq 'param') {
  173:                     $outstr.='<&>objparms<:>'.$token->[2]->{'to'}.'<:>'.
  174:                             $token->[2]->{'type'}.'___'.$token->[2]->{'name'}
  175:                                                  .'___'.$token->[2]->{'value'};
  176:                 } elsif ($graphmode) {
  177: # --------------------------------------------- All other tags (graphical only)
  178:                     $outstr.='<&>'.$token->[1];
  179:                     if (defined($token->[2]->{'index'})) {
  180: 			$outstr.='<:>'.$token->[2]->{'index'};
  181:                         if ($token->[1] eq 'obj') {
  182: 			    $obj[$token->[2]->{'index'}]=2;
  183:                         }
  184:                     }
  185:                     $outstr.='<:>'.$token->[2]->{'value'};
  186:                 }
  187:             }
  188:         }
  189: 
  190:     } else {
  191:         $errtext.='Map not loaded: The file does not exist. ';
  192:     }
  193:     return($outstr,$errtext,$infotext);
  194: }
  195: 
  196: 
  197: # ----------------------------------------------------------- Saves map to disk
  198: 
  199: sub savemap {
  200:     my ($fn,$errtext)=@_;
  201:     my $infotext='';
  202:     my %alltypes;
  203:     my %allvalues;
  204:     if (($fn=~/\.sequence(\.tmp)*$/) ||
  205:         ($fn=~/\.page(\.tmp)*$/)) {
  206: 
  207: # ------------------------------------------------------------- Deal with input
  208:         my @tags=split(/<&>/,$ENV{'form.output'});
  209:         my $outstr='';
  210:         my $graphdef=0;
  211:         if ($tags[0] eq 'graphdef<:>yes') {
  212: 	    $outstr='<map mode="rat/graphical">'."\n";
  213:             $graphdef=1;
  214:         } else {
  215:             $outstr="<map>\n";
  216:         }
  217:         foreach (@tags) {
  218: 	   my @parts=split(/<:>/,$_);
  219:            if ($parts[0] eq 'objcont') {
  220:                my @comp=split(/:/,$parts[$#parts]);
  221: # --------------------------------------------------------------- Logical input
  222: 	       if ($comp[$#comp] eq 'res') {
  223:                    $comp[0]=qtescape($comp[0]);
  224:                    $comp[1]=qtescape($comp[1]);
  225:                    if ($comp[2] eq 'true') {
  226: 		       if ($comp[1]!~/^http\:\/\//) {
  227: 			   $comp[1]='http://'.$comp[1];
  228:                        }
  229:                        $comp[1].='" external="true';
  230:                    } else {
  231: 		       if ($comp[1]=~/^http\:\/\//) {
  232: 			   $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
  233:                        }
  234:                    }
  235: 		   $outstr.='<resource id="'.$parts[1].'" src="'
  236:                           .$comp[1].'"';
  237: 
  238:                    if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
  239: 		       $outstr.=' type="'.$comp[3].'"';
  240:                    }
  241:                    if ($comp[0] ne '') {
  242: 		       $outstr.=' title="'.$comp[0].'"';
  243:                    }
  244:                    $outstr.="></resource>\n";
  245:                } elsif ($comp[$#comp] eq 'cond') {
  246:                    $outstr.='<condition id="'.$parts[1].'"';
  247:                    if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
  248: 		       $outstr.=' type="'.$comp[1].'"';
  249:                    }
  250:                    $outstr.=' value="'.qtescape($comp[0]).'"';
  251:                    $outstr.="></condition>\n";
  252:                }
  253:            } elsif ($parts[0] eq 'objlinks') {
  254:                my @comp=split(/:/,$parts[$#parts]);
  255:                $outstr.='<link';
  256:                $outstr.=' from="'.$comp[0].'"';
  257:                $outstr.=' to="'.$comp[1].'"';
  258:                if (($comp[2] ne '') && ($comp[2]!=0)) {
  259:                   $outstr.=' condition="'.$comp[2].'"';
  260:                }
  261:                $outstr.=' index="'.$parts[1].'"';
  262:                $outstr.="></link>\n";
  263:            } elsif ($parts[0] eq 'objparms') {
  264:                undef %alltypes;
  265:                undef %allvalues;
  266:                foreach (split(/:/,$parts[$#parts])) {
  267:                    my ($type,$name,$value)=split(/\_\_\_/,$_);
  268:                    $alltypes{$name}=$type;
  269:                    $allvalues{$name}=$value;
  270:                }
  271:                foreach (keys %allvalues) {
  272:                   if ($allvalues{$_} ne '') {
  273:                    $outstr.='<param to="'.$parts[1].'" type="'
  274:                           .$alltypes{$_}.'" name="'.$_
  275:                           .'" value="'.$allvalues{$_}.'">'
  276:                           ."</param>\n";
  277: 	          }
  278:                }
  279:            } elsif (($parts[0] ne '') && ($graphdef)) {
  280: # ------------------------------------------------------------- Graphical input
  281:                $outstr.='<'.$parts[0];
  282:                if ($#parts==2) {
  283: 		   $outstr.=' index="'.$parts[1].'"';
  284:                }
  285:                $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
  286:                         $parts[0].">\n";
  287:            }
  288:         }
  289:         $outstr.="</map>\n";
  290: 	if ($fn=~/^\/*uploaded\/(\w+)\/(\w+)\/(.*)$/) {
  291: 	    $ENV{'form.output'}=$outstr;
  292:             my $home=&Apache::lonnet::homeserver($2,$1);
  293:             my $result=&Apache::lonnet::finishuserfileupload($2,$1,$home,
  294: 						'output',$3);
  295: 	    if ($result != m|^/uploaded/|) {
  296: 		$errtext.='Map not saved: A network error occured when trying to save the map. ';
  297: 	    }
  298:         } else {
  299:           my $fh;
  300:           if ($fh=Apache::File->new(">$fn")) {
  301:              print $fh $outstr;
  302:              $infotext.="Map saved as $fn. ";
  303: 	  } else {
  304:              $errtext.='Could not write file '.$fn.'.  Map not saved. ';
  305: 	  }
  306:         }
  307:     } else {
  308: # -------------------------------------------- Cannot write to that file, error
  309:         $errtext.='Map not saved: The specified path does not exist. ';
  310:     }
  311:     return ($errtext,$infotext);
  312: }
  313: 
  314: # ================================================================ Main Handler
  315: 
  316: sub handler {
  317:   my $r=shift;
  318:   &Apache::loncommon::content_type($r,'text/html');
  319:   $r->send_http_header;
  320: 
  321:   return OK if $r->header_only;
  322: 
  323:   my $url=$r->uri;
  324:   $url=~/\/(\w+)\/ratserver$/;
  325:   my $mode=$1;
  326: 
  327:   $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
  328:   
  329:   my $fn=$r->filename;
  330:   my $lonDocRoot=$r->dir_config('lonDocRoot');
  331:   if ( $fn =~ /$lonDocRoot/ ) {
  332:       #internal authentication, needs fixup.
  333:       $fn = $url;
  334:       $fn=~s|^/~(\w+)|/home/$1/public_html|;
  335:       $fn=~s|/[^/]*/ratserver$||;
  336:   }
  337:   my $errtext='';
  338:   my $infotext='';
  339:   my $outtext='';
  340: 
  341:   if ($mode ne 'loadonly') {
  342:      ($errtext,$infotext)=&savemap($fn,$errtext);
  343:   }
  344:   ($outtext,$errtext,$infotext)=&loadmap($fn,$errtext,$infotext);
  345: 
  346:   $r->print(<<ENDDOCUMENT);
  347: <html>
  348: <body bgcolor="#FFFFFF">
  349: <form name=storage method=post action="$url">
  350: <input type=hidden name=output value="$outtext">
  351: </form>
  352: <script>
  353:     parent.flag=1;
  354: </script>
  355: ENDDOCUMENT
  356:     if (($errtext ne '') || ($infotext ne '')) {
  357: 	$r->print(<<ENDSCRIPT);
  358: <script>
  359:     alert("$infotext $errtext");
  360: </script>
  361: ENDSCRIPT
  362:     }
  363:     $r->print("</body>\n</html>\n");
  364: 
  365:   return OK;
  366: }
  367: 
  368: 1;
  369: __END__

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