Annotation of rat/lonratsrv.pm, revision 1.24
1.1 www 1: # The LearningOnline Network with CAPA
2: # Server for RAT Maps
3: #
1.24 ! harris41 4: # $Id: lonratsrv.pm,v 1.23 2002/09/05 19:37:00 www 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: # (Edit Handler for RAT Maps
29: # (TeX Content Handler
30: #
31: # 05/29/00,05/30 Gerd Kortemeyer)
32: # 7/1 Gerd Kortemeyer)
1.7 www 33: # 7/1,7/3,7/4,7/7,7/8,7/10,7/26,10/2 Gerd Kortemeyer
1.15 www 34: # 5/3,06/25,07/03,07/04,07/05 Gerd Kortemeyer
1.1 www 35:
36: package Apache::lonratsrv;
37:
38: use strict;
39: use Apache::Constants qw(:common);
1.2 www 40: use Apache::File;
41: use HTML::TokeParser;
42:
43:
1.4 www 44: # ------------------------------------------------------------- From RAT to XML
1.2 www 45:
46: sub qtescape {
47: my $str=shift;
1.4 www 48: $str=~s/\&\#58\;/\:/g;
49: $str=~s/\&\#39\;/\'/g;
50: $str=~s/\&\#44\;/\,/g;
1.15 www 51: $str=~s/\"/\&\#34\;/g;
1.2 www 52: return $str;
53: }
54:
1.4 www 55: # ------------------------------------------------------------- From XML to RAT
1.2 www 56:
1.4 www 57: sub qtunescape {
1.2 www 58: my $str=shift;
1.14 www 59: $str=~s/\:/\&colon\;/g;
1.4 www 60: $str=~s/\'/\&\#39\;/g;
61: $str=~s/\,/\&\#44\;/g;
62: $str=~s/\"/\&\#34\;/g;
1.2 www 63: return $str;
64: }
65:
66: # --------------------------------------------------------- Loads map from disk
67:
68: sub loadmap {
69: my ($fn,$errtext)=@_;
70: my $outstr='';
71: my @obj=();
72: my @links=();
1.21 www 73: my $instr='';
74: if ($fn=~/^\/*uploaded\//) {
75: $instr=&Apache::lonnet::getfile($fn);
76: } elsif (-e $fn) {
77: my @content=();
1.2 www 78: {
79: my $fh=Apache::File->new($fn);
80: @content=<$fh>;
81: }
1.21 www 82: $instr=join('',@content);
83: }
84: if ($instr) {
1.2 www 85: my $parser = HTML::TokeParser->new(\$instr);
86: my $token;
87: my $graphmode=0;
88:
89: $fn=~/\.(\w+)$/;
90: $outstr="mode<:>$1";
91:
92: while ($token = $parser->get_token) {
93: if ($token->[0] eq 'S') {
94: if ($token->[1] eq 'map') {
95: $graphmode=($token->[2]->{'mode'} eq 'rat/graphical');
96: } elsif ($token->[1] eq 'resource') {
1.3 www 97: # -------------------------------------------------------------------- Resource
98: $outstr.='<&>objcont';
99: if ($token->[2]->{'id'}) {
100: $outstr.='<:>'.$token->[2]->{'id'};
101: if ($obj[$token->[2]->{'id'}]==1) {
102: $errtext.='Error: multiple use of ID '.
103: $token->[2]->{'id'}.'. ';
104: }
105: $obj[$token->[2]->{'id'}]=1;
106: } else {
107: my $i=1;
108: while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
109: $outstr.='<:>'.$i;
110: $obj[$i]=1;
111: }
112: $outstr.='<:>';
1.4 www 113: $outstr.=qtunescape($token->[2]->{'title'}).":";
114: $outstr.=qtunescape($token->[2]->{'src'}).":";
1.14 www 115: if ($token->[2]->{'external'} eq 'true') {
1.4 www 116: $outstr.='true:';
117: } else {
118: $outstr.='false:';
119: }
120: if ($token->[2]->{'type'}) {
121: $outstr.=$token->[2]->{'type'}.':';
122: } else {
123: $outstr.='normal:';
124: }
125: $outstr.='res';
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: }
197: return($outstr,$errtext);
198: }
199:
200:
201: # ----------------------------------------------------------- Saves map to disk
202:
203: sub savemap {
1.20 albertel 204: my ($fn,$errtext)=@_;
1.13 www 205: my %alltypes;
206: my %allvalues;
1.22 www 207: if (($fn=~/\.sequence(\.tmp)*$/) ||
208: ($fn=~/\.page(\.tmp)*$/)) {
1.4 www 209:
1.2 www 210: # ------------------------------------------------------------- Deal with input
211: my @tags=split(/<&>/,$ENV{'form.output'});
212: my $outstr='';
213: my $graphdef=0;
214: if ($tags[0] eq 'graphdef<:>yes') {
215: $outstr='<map mode="rat/graphical">'."\n";
216: $graphdef=1;
217: } else {
218: $outstr="<map>\n";
219: }
1.23 www 220: foreach (@tags) {
1.2 www 221: my @parts=split(/<:>/,$_);
222: if ($parts[0] eq 'objcont') {
223: my @comp=split(/:/,$parts[$#parts]);
224: # --------------------------------------------------------------- Logical input
225: if ($comp[$#comp] eq 'res') {
1.4 www 226: $comp[0]=qtescape($comp[0]);
227: $comp[1]=qtescape($comp[1]);
1.2 www 228: if ($comp[2] eq 'true') {
229: if ($comp[1]!~/^http\:\/\//) {
230: $comp[1]='http://'.$comp[1];
231: }
1.14 www 232: $comp[1].='" external="true';
1.2 www 233: } else {
234: if ($comp[1]=~/^http\:\/\//) {
235: $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
236: }
237: }
238: $outstr.='<resource id="'.$parts[1].'" src="'
1.4 www 239: .$comp[1].'"';
1.2 www 240:
241: if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
242: $outstr.=' type="'.$comp[3].'"';
243: }
244: if ($comp[0] ne '') {
1.4 www 245: $outstr.=' title="'.$comp[0].'"';
1.2 www 246: }
247: $outstr.="></resource>\n";
248: } elsif ($comp[$#comp] eq 'cond') {
249: $outstr.='<condition id="'.$parts[1].'"';
250: if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
251: $outstr.=' type="'.$comp[1].'"';
252: }
253: $outstr.=' value="'.qtescape($comp[0]).'"';
254: $outstr.="></condition>\n";
255: }
256: } elsif ($parts[0] eq 'objlinks') {
257: my @comp=split(/:/,$parts[$#parts]);
258: $outstr.='<link';
259: $outstr.=' from="'.$comp[0].'"';
260: $outstr.=' to="'.$comp[1].'"';
261: if (($comp[2] ne '') && ($comp[2]!=0)) {
262: $outstr.=' condition="'.$comp[2].'"';
263: }
264: $outstr.=' index="'.$parts[1].'"';
265: $outstr.="></link>\n";
1.11 www 266: } elsif ($parts[0] eq 'objparms') {
1.13 www 267: undef %alltypes;
268: undef %allvalues;
1.20 albertel 269: foreach (split(/:/,$parts[$#parts])) {
1.11 www 270: my ($type,$name,$value)=split(/\_\_\_/,$_);
1.13 www 271: $alltypes{$name}=$type;
272: $allvalues{$name}=$value;
1.20 albertel 273: }
274: foreach (keys %allvalues) {
275: if ($allvalues{$_} ne '') {
1.13 www 276: $outstr.='<param to="'.$parts[1].'" type="'
277: .$alltypes{$_}.'" name="'.$_
278: .'" value="'.$allvalues{$_}.'">'
1.12 www 279: ."</param>\n";
1.20 albertel 280: }
281: }
1.2 www 282: } elsif (($parts[0] ne '') && ($graphdef)) {
283: # ------------------------------------------------------------- Graphical input
284: $outstr.='<'.$parts[0];
285: if ($#parts==2) {
286: $outstr.=' index="'.$parts[1].'"';
287: }
288: $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
289: $parts[0].">\n";
290: }
1.23 www 291: }
1.2 www 292: $outstr.="</map>\n";
1.23 www 293: if ($fn=~/^\/*uploaded\/(\w+)\/(\w+)\//) {
1.21 www 294: $ENV{'form.output'}=$outstr;
1.23 www 295: my $home=&Apache::lonnet::homeserver($2,$1);
1.21 www 296: &Apache::lonnet::finishuserfileupload(
1.23 www 297: $2,$1,$home,
1.21 www 298: 'output',(split(/\//,$fn))[-1]);
299: } else {
1.2 www 300: my $fh;
301: if ($fh=Apache::File->new(">$fn")) {
302: print $fh $outstr;
1.3 www 303: $errtext.="Map saved as $fn. ";
1.2 www 304: } else {
1.17 matthew 305: $errtext.='Could not write file '.$fn.'. Map not saved. ';
1.2 www 306: }
307: }
308: } else {
309: # -------------------------------------------- Cannot write to that file, error
1.20 albertel 310: $errtext.='Map not saved: The specified path does not exist. ';
1.2 www 311: }
312: return $errtext;
313: }
1.1 www 314:
315: # ================================================================ Main Handler
316:
317: sub handler {
318: my $r=shift;
319: $r->content_type('text/html');
320: $r->send_http_header;
321:
322: return OK if $r->header_only;
323:
324: my $url=$r->uri;
1.2 www 325: $url=~/\/(\w+)\/ratserver$/;
326: my $mode=$1;
327:
328: $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
329:
330: my $fn=$r->filename;
1.19 albertel 331: my $lonDocRoot=$r->dir_config('lonDocRoot');
332: if ( $fn =~ /$lonDocRoot/ ) {
333: #internal authentication, needs fixup.
334: $fn = $url;
335: $fn=~s|^/~(\w+)|/home/$1/public_html|;
336: $fn=~s|/[^/]*/ratserver$||;
337: }
1.2 www 338: my $errtext='';
339: my $outtext='';
340:
341: if ($mode ne 'loadonly') {
1.20 albertel 342: $errtext=&savemap($fn,$errtext);
1.2 www 343: }
344: ($outtext,$errtext)=&loadmap($fn,$errtext);
1.1 www 345:
346: $r->print(<<ENDDOCUMENT);
347: <html>
1.8 harris41 348: <body bgcolor="#FFFFFF">
1.2 www 349: <form name=storage method=post action="$url">
350: <input type=hidden name=output value="$outtext">
1.1 www 351: </form>
1.8 harris41 352: <script>
1.9 harris41 353: parent.flag=1;
1.8 harris41 354: </script>
1.2 www 355: ENDDOCUMENT
356: if ($errtext ne '') {
357: $r->print(<<ENDSCRIPT);
358: <script>
359: alert("$errtext");
360: </script>
361: ENDSCRIPT
362: }
363: $r->print("</body>\n</html>\n");
1.1 www 364:
365: return OK;
366: }
367:
368: 1;
369: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>