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