Annotation of rat/lonratsrv.pm, revision 1.31
1.1 www 1: # The LearningOnline Network with CAPA
2: # Server for RAT Maps
3: #
1.31 ! www 4: # $Id: lonratsrv.pm,v 1.30 2005/04/07 06:56:27 albertel Exp $
1.16 www 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: #
1.1 www 28:
29: package Apache::lonratsrv;
30:
31: use strict;
32: use Apache::Constants qw(:common);
1.2 www 33: use Apache::File;
34: use HTML::TokeParser;
1.30 albertel 35: use Apache::lonnet;
1.2 www 36:
1.4 www 37: # ------------------------------------------------------------- From RAT to XML
1.2 www 38:
39: sub qtescape {
40: my $str=shift;
1.4 www 41: $str=~s/\&\#58\;/\:/g;
42: $str=~s/\&\#39\;/\'/g;
43: $str=~s/\&\#44\;/\,/g;
1.15 www 44: $str=~s/\"/\&\#34\;/g;
1.2 www 45: return $str;
46: }
47:
1.4 www 48: # ------------------------------------------------------------- From XML to RAT
1.2 www 49:
1.4 www 50: sub qtunescape {
1.2 www 51: my $str=shift;
1.14 www 52: $str=~s/\:/\&colon\;/g;
1.4 www 53: $str=~s/\'/\&\#39\;/g;
54: $str=~s/\,/\&\#44\;/g;
55: $str=~s/\"/\&\#34\;/g;
1.2 www 56: return $str;
57: }
58:
59: # --------------------------------------------------------- Loads map from disk
60:
61: sub loadmap {
1.28 www 62: my ($fn,$errtext,$infotext)=@_;
63: if ($errtext) { return('',$errtext); }
1.2 www 64: my $outstr='';
65: my @obj=();
66: my @links=();
1.21 www 67: my $instr='';
68: if ($fn=~/^\/*uploaded\//) {
69: $instr=&Apache::lonnet::getfile($fn);
70: } elsif (-e $fn) {
71: my @content=();
1.2 www 72: {
73: my $fh=Apache::File->new($fn);
74: @content=<$fh>;
75: }
1.21 www 76: $instr=join('',@content);
77: }
1.25 albertel 78: if ($instr eq -2) {
79: $errtext.='Map not loaded: An error occured while trying to load the map.';
80: } elsif ($instr) {
1.2 www 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') {
1.3 www 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.='<:>';
1.4 www 109: $outstr.=qtunescape($token->[2]->{'title'}).":";
110: $outstr.=qtunescape($token->[2]->{'src'}).":";
1.14 www 111: if ($token->[2]->{'external'} eq 'true') {
1.4 www 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: }
1.31 ! www 121: if ($token->[2]->{'type'} ne 'zombie') {
! 122: $outstr.='res';
! 123: } else {
! 124: $outstr.='zombie';
! 125: }
1.2 www 126: } elsif ($token->[1] eq 'condition') {
1.3 www 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.='<:>';
1.4 www 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';
1.2 www 150: } elsif ($token->[1] eq 'link') {
1.3 www 151: # ----------------------------------------------------------------------- Links
1.2 www 152: $outstr.='<&>objlinks';
1.7 www 153:
1.3 www 154: if ($token->[2]->{'index'}) {
1.4 www 155: if ($links[$token->[2]->{'index'}]) {
156: $errtext.='Error: multiple use of link index '.
1.3 www 157: $token->[2]->{'index'}.'. ';
1.4 www 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: }
1.7 www 167:
1.2 www 168: $outstr.='<:>'.$token->[2]->{'from'}.
1.5 www 169: ':'.$token->[2]->{'to'};
1.2 www 170: if ($token->[2]->{'condition'}) {
1.5 www 171: $outstr.=':'.$token->[2]->{'condition'};
1.2 www 172: } else {
1.5 www 173: $outstr.=':0';
1.4 www 174: }
1.11 www 175: # ------------------------------------------------------------------- Parameter
176: } elsif ($token->[1] eq 'param') {
177: $outstr.='<&>objparms<:>'.$token->[2]->{'to'}.'<:>'.
1.13 www 178: $token->[2]->{'type'}.'___'.$token->[2]->{'name'}
1.11 www 179: .'___'.$token->[2]->{'value'};
1.2 www 180: } elsif ($graphmode) {
1.3 www 181: # --------------------------------------------- All other tags (graphical only)
182: $outstr.='<&>'.$token->[1];
1.4 www 183: if (defined($token->[2]->{'index'})) {
1.3 www 184: $outstr.='<:>'.$token->[2]->{'index'};
185: if ($token->[1] eq 'obj') {
186: $obj[$token->[2]->{'index'}]=2;
187: }
188: }
189: $outstr.='<:>'.$token->[2]->{'value'};
1.2 www 190: }
191: }
192: }
193:
194: } else {
1.3 www 195: $errtext.='Map not loaded: The file does not exist. ';
1.2 www 196: }
1.28 www 197: return($outstr,$errtext,$infotext);
1.2 www 198: }
199:
200:
201: # ----------------------------------------------------------- Saves map to disk
202:
203: sub savemap {
1.20 albertel 204: my ($fn,$errtext)=@_;
1.28 www 205: my $infotext='';
1.13 www 206: my %alltypes;
207: my %allvalues;
1.22 www 208: if (($fn=~/\.sequence(\.tmp)*$/) ||
209: ($fn=~/\.page(\.tmp)*$/)) {
1.4 www 210:
1.2 www 211: # ------------------------------------------------------------- Deal with input
1.30 albertel 212: my @tags=split(/<&>/,$env{'form.output'});
1.2 www 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: }
1.23 www 221: foreach (@tags) {
1.2 www 222: my @parts=split(/<:>/,$_);
223: if ($parts[0] eq 'objcont') {
224: my @comp=split(/:/,$parts[$#parts]);
225: # --------------------------------------------------------------- Logical input
1.31 ! www 226: if (($comp[$#comp] eq 'res') || ($comp[$#comp] eq 'zombie')) {
1.4 www 227: $comp[0]=qtescape($comp[0]);
228: $comp[1]=qtescape($comp[1]);
1.2 www 229: if ($comp[2] eq 'true') {
230: if ($comp[1]!~/^http\:\/\//) {
231: $comp[1]='http://'.$comp[1];
232: }
1.14 www 233: $comp[1].='" external="true';
1.2 www 234: } else {
235: if ($comp[1]=~/^http\:\/\//) {
236: $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
237: }
238: }
239: $outstr.='<resource id="'.$parts[1].'" src="'
1.4 www 240: .$comp[1].'"';
1.2 www 241:
242: if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
243: $outstr.=' type="'.$comp[3].'"';
244: }
245: if ($comp[0] ne '') {
1.4 www 246: $outstr.=' title="'.$comp[0].'"';
1.2 www 247: }
1.31 ! www 248: $outstr.=" />\n";
1.2 www 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]).'"';
1.31 ! www 255: $outstr.=" />\n";
1.2 www 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].'"';
1.31 ! www 266: $outstr.=" />\n";
1.11 www 267: } elsif ($parts[0] eq 'objparms') {
1.13 www 268: undef %alltypes;
269: undef %allvalues;
1.20 albertel 270: foreach (split(/:/,$parts[$#parts])) {
1.11 www 271: my ($type,$name,$value)=split(/\_\_\_/,$_);
1.13 www 272: $alltypes{$name}=$type;
273: $allvalues{$name}=$value;
1.20 albertel 274: }
275: foreach (keys %allvalues) {
276: if ($allvalues{$_} ne '') {
1.13 www 277: $outstr.='<param to="'.$parts[1].'" type="'
278: .$alltypes{$_}.'" name="'.$_
1.31 ! www 279: .'" value="'.$allvalues{$_}.'" />'
! 280: ."\n";
1.20 albertel 281: }
282: }
1.2 www 283: } elsif (($parts[0] ne '') && ($graphdef)) {
284: # ------------------------------------------------------------- Graphical input
285: $outstr.='<'.$parts[0];
286: if ($#parts==2) {
287: $outstr.=' index="'.$parts[1].'"';
288: }
1.31 ! www 289: $outstr.=' value="'.qtescape($parts[$#parts]).'" />'."\n";
1.2 www 290: }
1.23 www 291: }
1.2 www 292: $outstr.="</map>\n";
1.26 raeburn 293: if ($fn=~/^\/*uploaded\/(\w+)\/(\w+)\/(.*)$/) {
1.30 albertel 294: $env{'form.output'}=$outstr;
1.23 www 295: my $home=&Apache::lonnet::homeserver($2,$1);
1.25 albertel 296: my $result=&Apache::lonnet::finishuserfileupload($2,$1,$home,
1.26 raeburn 297: 'output',$3);
1.25 albertel 298: if ($result != m|^/uploaded/|) {
299: $errtext.='Map not saved: A network error occured when trying to save the map. ';
300: }
1.21 www 301: } else {
1.2 www 302: my $fh;
303: if ($fh=Apache::File->new(">$fn")) {
304: print $fh $outstr;
1.28 www 305: $infotext.="Map saved as $fn. ";
1.2 www 306: } else {
1.17 matthew 307: $errtext.='Could not write file '.$fn.'. Map not saved. ';
1.2 www 308: }
309: }
310: } else {
311: # -------------------------------------------- Cannot write to that file, error
1.20 albertel 312: $errtext.='Map not saved: The specified path does not exist. ';
1.2 www 313: }
1.28 www 314: return ($errtext,$infotext);
1.2 www 315: }
1.1 www 316:
317: # ================================================================ Main Handler
318:
319: sub handler {
320: my $r=shift;
1.29 albertel 321: &Apache::loncommon::content_type($r,'text/html');
1.1 www 322: $r->send_http_header;
323:
324: return OK if $r->header_only;
325:
326: my $url=$r->uri;
1.2 www 327: $url=~/\/(\w+)\/ratserver$/;
328: my $mode=$1;
329:
330: $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
331:
332: my $fn=$r->filename;
1.19 albertel 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: }
1.2 www 340: my $errtext='';
1.28 www 341: my $infotext='';
1.2 www 342: my $outtext='';
343:
344: if ($mode ne 'loadonly') {
1.28 www 345: ($errtext,$infotext)=&savemap($fn,$errtext);
1.2 www 346: }
1.28 www 347: ($outtext,$errtext,$infotext)=&loadmap($fn,$errtext,$infotext);
1.1 www 348:
349: $r->print(<<ENDDOCUMENT);
350: <html>
1.8 harris41 351: <body bgcolor="#FFFFFF">
1.2 www 352: <form name=storage method=post action="$url">
353: <input type=hidden name=output value="$outtext">
1.1 www 354: </form>
1.8 harris41 355: <script>
1.9 harris41 356: parent.flag=1;
1.8 harris41 357: </script>
1.2 www 358: ENDDOCUMENT
1.28 www 359: if (($errtext ne '') || ($infotext ne '')) {
1.2 www 360: $r->print(<<ENDSCRIPT);
361: <script>
1.28 www 362: alert("$infotext $errtext");
1.2 www 363: </script>
364: ENDSCRIPT
365: }
366: $r->print("</body>\n</html>\n");
1.1 www 367:
368: return OK;
369: }
370:
371: 1;
372: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>