Annotation of rat/lonratsrv.pm, revision 1.23
1.1 www 1: # The LearningOnline Network with CAPA
2: # Server for RAT Maps
3: #
1.23 ! www 4: # $Id: lonratsrv.pm,v 1.22 2002/08/31 00:42:30 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.8 harris41 34: # 4/30/2001 Scott Harrison
1.15 www 35: # 5/3,06/25,07/03,07/04,07/05 Gerd Kortemeyer
1.1 www 36:
37: package Apache::lonratsrv;
38:
39: use strict;
40: use Apache::Constants qw(:common);
1.2 www 41: use Apache::File;
42: use HTML::TokeParser;
43:
44:
1.4 www 45: # ------------------------------------------------------------- From RAT to XML
1.2 www 46:
47: sub qtescape {
48: my $str=shift;
1.4 www 49: $str=~s/\&\#58\;/\:/g;
50: $str=~s/\&\#39\;/\'/g;
51: $str=~s/\&\#44\;/\,/g;
1.15 www 52: $str=~s/\"/\&\#34\;/g;
1.2 www 53: return $str;
54: }
55:
1.4 www 56: # ------------------------------------------------------------- From XML to RAT
1.2 www 57:
1.4 www 58: sub qtunescape {
1.2 www 59: my $str=shift;
1.14 www 60: $str=~s/\:/\&colon\;/g;
1.4 www 61: $str=~s/\'/\&\#39\;/g;
62: $str=~s/\,/\&\#44\;/g;
63: $str=~s/\"/\&\#34\;/g;
1.2 www 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=();
1.21 www 74: my $instr='';
75: if ($fn=~/^\/*uploaded\//) {
76: $instr=&Apache::lonnet::getfile($fn);
77: } elsif (-e $fn) {
78: my @content=();
1.2 www 79: {
80: my $fh=Apache::File->new($fn);
81: @content=<$fh>;
82: }
1.21 www 83: $instr=join('',@content);
84: }
85: if ($instr) {
1.2 www 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') {
1.3 www 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.='<:>';
1.4 www 114: $outstr.=qtunescape($token->[2]->{'title'}).":";
115: $outstr.=qtunescape($token->[2]->{'src'}).":";
1.14 www 116: if ($token->[2]->{'external'} eq 'true') {
1.4 www 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';
1.2 www 127: } elsif ($token->[1] eq 'condition') {
1.3 www 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.='<:>';
1.4 www 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';
1.2 www 151: } elsif ($token->[1] eq 'link') {
1.3 www 152: # ----------------------------------------------------------------------- Links
1.2 www 153: $outstr.='<&>objlinks';
1.7 www 154:
1.3 www 155: if ($token->[2]->{'index'}) {
1.4 www 156: if ($links[$token->[2]->{'index'}]) {
157: $errtext.='Error: multiple use of link index '.
1.3 www 158: $token->[2]->{'index'}.'. ';
1.4 www 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: }
1.7 www 168:
1.2 www 169: $outstr.='<:>'.$token->[2]->{'from'}.
1.5 www 170: ':'.$token->[2]->{'to'};
1.2 www 171: if ($token->[2]->{'condition'}) {
1.5 www 172: $outstr.=':'.$token->[2]->{'condition'};
1.2 www 173: } else {
1.5 www 174: $outstr.=':0';
1.4 www 175: }
1.11 www 176: # ------------------------------------------------------------------- Parameter
177: } elsif ($token->[1] eq 'param') {
178: $outstr.='<&>objparms<:>'.$token->[2]->{'to'}.'<:>'.
1.13 www 179: $token->[2]->{'type'}.'___'.$token->[2]->{'name'}
1.11 www 180: .'___'.$token->[2]->{'value'};
1.2 www 181: } elsif ($graphmode) {
1.3 www 182: # --------------------------------------------- All other tags (graphical only)
183: $outstr.='<&>'.$token->[1];
1.4 www 184: if (defined($token->[2]->{'index'})) {
1.3 www 185: $outstr.='<:>'.$token->[2]->{'index'};
186: if ($token->[1] eq 'obj') {
187: $obj[$token->[2]->{'index'}]=2;
188: }
189: }
190: $outstr.='<:>'.$token->[2]->{'value'};
1.2 www 191: }
192: }
193: }
194:
195: } else {
1.3 www 196: $errtext.='Map not loaded: The file does not exist. ';
1.2 www 197: }
198: return($outstr,$errtext);
199: }
200:
201:
202: # ----------------------------------------------------------- Saves map to disk
203:
204: sub savemap {
1.20 albertel 205: my ($fn,$errtext)=@_;
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
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: }
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
226: if ($comp[$#comp] eq 'res') {
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: }
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";
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="'.$_
279: .'" value="'.$allvalues{$_}.'">'
1.12 www 280: ."</param>\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: }
289: $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
290: $parts[0].">\n";
291: }
1.23 ! www 292: }
1.2 www 293: $outstr.="</map>\n";
1.23 ! www 294: if ($fn=~/^\/*uploaded\/(\w+)\/(\w+)\//) {
1.21 www 295: $ENV{'form.output'}=$outstr;
1.23 ! www 296: my $home=&Apache::lonnet::homeserver($2,$1);
1.21 www 297: &Apache::lonnet::finishuserfileupload(
1.23 ! www 298: $2,$1,$home,
1.21 www 299: 'output',(split(/\//,$fn))[-1]);
300: } else {
1.2 www 301: my $fh;
302: if ($fh=Apache::File->new(">$fn")) {
303: print $fh $outstr;
1.3 www 304: $errtext.="Map saved as $fn. ";
1.2 www 305: } else {
1.17 matthew 306: $errtext.='Could not write file '.$fn.'. Map not saved. ';
1.2 www 307: }
308: }
309: } else {
310: # -------------------------------------------- Cannot write to that file, error
1.20 albertel 311: $errtext.='Map not saved: The specified path does not exist. ';
1.2 www 312: }
313: return $errtext;
314: }
1.1 www 315:
316: # ================================================================ Main Handler
317:
318: sub handler {
319: my $r=shift;
320: $r->content_type('text/html');
321: $r->send_http_header;
322:
323: return OK if $r->header_only;
324:
325: my $url=$r->uri;
1.2 www 326: $url=~/\/(\w+)\/ratserver$/;
327: my $mode=$1;
328:
329: $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
330:
331: my $fn=$r->filename;
1.19 albertel 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: }
1.2 www 339: my $errtext='';
340: my $outtext='';
341:
342: if ($mode ne 'loadonly') {
1.20 albertel 343: $errtext=&savemap($fn,$errtext);
1.2 www 344: }
345: ($outtext,$errtext)=&loadmap($fn,$errtext);
1.1 www 346:
347: $r->print(<<ENDDOCUMENT);
348: <html>
1.8 harris41 349: <body bgcolor="#FFFFFF">
1.2 www 350: <form name=storage method=post action="$url">
351: <input type=hidden name=output value="$outtext">
1.1 www 352: </form>
1.8 harris41 353: <script>
1.9 harris41 354: parent.flag=1;
1.8 harris41 355: </script>
1.2 www 356: ENDDOCUMENT
357: if ($errtext ne '') {
358: $r->print(<<ENDSCRIPT);
359: <script>
360: alert("$errtext");
361: </script>
362: ENDSCRIPT
363: }
364: $r->print("</body>\n</html>\n");
1.1 www 365:
366: return OK;
367: }
368:
369: 1;
370: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>