Annotation of rat/lonratsrv.pm, revision 1.28
1.1 www 1: # The LearningOnline Network with CAPA
2: # Server for RAT Maps
3: #
1.27 www 4: # $Id: lonratsrv.pm,v 1.26 2004/03/29 19:44:41 raeburn 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.28 ! 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)
! 33: # 7/1,7/3,7/4,7/7,7/8,7/10,7/26,10/2 Gerd Kortemeyer
! 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 {
1.28 ! www 69: my ($fn,$errtext,$infotext)=@_;
! 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: }
1.28 ! www 200: return($outstr,$errtext,$infotext);
1.2 www 201: }
202:
203:
204: # ----------------------------------------------------------- Saves map to disk
205:
206: sub savemap {
1.20 albertel 207: my ($fn,$errtext)=@_;
1.28 ! www 208: my $infotext='';
1.13 www 209: my %alltypes;
210: my %allvalues;
1.22 www 211: if (($fn=~/\.sequence(\.tmp)*$/) ||
212: ($fn=~/\.page(\.tmp)*$/)) {
1.4 www 213:
1.2 www 214: # ------------------------------------------------------------- Deal with input
215: my @tags=split(/<&>/,$ENV{'form.output'});
216: my $outstr='';
217: my $graphdef=0;
218: if ($tags[0] eq 'graphdef<:>yes') {
219: $outstr='<map mode="rat/graphical">'."\n";
220: $graphdef=1;
221: } else {
222: $outstr="<map>\n";
223: }
1.23 www 224: foreach (@tags) {
1.2 www 225: my @parts=split(/<:>/,$_);
226: if ($parts[0] eq 'objcont') {
227: my @comp=split(/:/,$parts[$#parts]);
228: # --------------------------------------------------------------- Logical input
229: if ($comp[$#comp] eq 'res') {
1.4 www 230: $comp[0]=qtescape($comp[0]);
231: $comp[1]=qtescape($comp[1]);
1.2 www 232: if ($comp[2] eq 'true') {
233: if ($comp[1]!~/^http\:\/\//) {
234: $comp[1]='http://'.$comp[1];
235: }
1.14 www 236: $comp[1].='" external="true';
1.2 www 237: } else {
238: if ($comp[1]=~/^http\:\/\//) {
239: $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
240: }
241: }
242: $outstr.='<resource id="'.$parts[1].'" src="'
1.4 www 243: .$comp[1].'"';
1.2 www 244:
245: if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
246: $outstr.=' type="'.$comp[3].'"';
247: }
248: if ($comp[0] ne '') {
1.4 www 249: $outstr.=' title="'.$comp[0].'"';
1.2 www 250: }
251: $outstr.="></resource>\n";
252: } elsif ($comp[$#comp] eq 'cond') {
253: $outstr.='<condition id="'.$parts[1].'"';
254: if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
255: $outstr.=' type="'.$comp[1].'"';
256: }
257: $outstr.=' value="'.qtescape($comp[0]).'"';
258: $outstr.="></condition>\n";
259: }
260: } elsif ($parts[0] eq 'objlinks') {
261: my @comp=split(/:/,$parts[$#parts]);
262: $outstr.='<link';
263: $outstr.=' from="'.$comp[0].'"';
264: $outstr.=' to="'.$comp[1].'"';
265: if (($comp[2] ne '') && ($comp[2]!=0)) {
266: $outstr.=' condition="'.$comp[2].'"';
267: }
268: $outstr.=' index="'.$parts[1].'"';
269: $outstr.="></link>\n";
1.11 www 270: } elsif ($parts[0] eq 'objparms') {
1.13 www 271: undef %alltypes;
272: undef %allvalues;
1.20 albertel 273: foreach (split(/:/,$parts[$#parts])) {
1.11 www 274: my ($type,$name,$value)=split(/\_\_\_/,$_);
1.13 www 275: $alltypes{$name}=$type;
276: $allvalues{$name}=$value;
1.20 albertel 277: }
278: foreach (keys %allvalues) {
279: if ($allvalues{$_} ne '') {
1.13 www 280: $outstr.='<param to="'.$parts[1].'" type="'
281: .$alltypes{$_}.'" name="'.$_
282: .'" value="'.$allvalues{$_}.'">'
1.12 www 283: ."</param>\n";
1.20 albertel 284: }
285: }
1.2 www 286: } elsif (($parts[0] ne '') && ($graphdef)) {
287: # ------------------------------------------------------------- Graphical input
288: $outstr.='<'.$parts[0];
289: if ($#parts==2) {
290: $outstr.=' index="'.$parts[1].'"';
291: }
292: $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
293: $parts[0].">\n";
294: }
1.23 www 295: }
1.2 www 296: $outstr.="</map>\n";
1.26 raeburn 297: if ($fn=~/^\/*uploaded\/(\w+)\/(\w+)\/(.*)$/) {
1.21 www 298: $ENV{'form.output'}=$outstr;
1.23 www 299: my $home=&Apache::lonnet::homeserver($2,$1);
1.25 albertel 300: my $result=&Apache::lonnet::finishuserfileupload($2,$1,$home,
1.26 raeburn 301: 'output',$3);
1.25 albertel 302: if ($result != m|^/uploaded/|) {
303: $errtext.='Map not saved: A network error occured when trying to save the map. ';
304: }
1.21 www 305: } else {
1.2 www 306: my $fh;
307: if ($fh=Apache::File->new(">$fn")) {
308: print $fh $outstr;
1.28 ! www 309: $infotext.="Map saved as $fn. ";
1.2 www 310: } else {
1.17 matthew 311: $errtext.='Could not write file '.$fn.'. Map not saved. ';
1.2 www 312: }
313: }
314: } else {
315: # -------------------------------------------- Cannot write to that file, error
1.20 albertel 316: $errtext.='Map not saved: The specified path does not exist. ';
1.2 www 317: }
1.28 ! www 318: return ($errtext,$infotext);
1.2 www 319: }
1.1 www 320:
321: # ================================================================ Main Handler
322:
323: sub handler {
324: my $r=shift;
325: $r->content_type('text/html');
326: $r->send_http_header;
327:
328: return OK if $r->header_only;
329:
330: my $url=$r->uri;
1.2 www 331: $url=~/\/(\w+)\/ratserver$/;
332: my $mode=$1;
333:
334: $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
335:
336: my $fn=$r->filename;
1.19 albertel 337: my $lonDocRoot=$r->dir_config('lonDocRoot');
338: if ( $fn =~ /$lonDocRoot/ ) {
339: #internal authentication, needs fixup.
340: $fn = $url;
341: $fn=~s|^/~(\w+)|/home/$1/public_html|;
342: $fn=~s|/[^/]*/ratserver$||;
343: }
1.2 www 344: my $errtext='';
1.28 ! www 345: my $infotext='';
1.2 www 346: my $outtext='';
347:
348: if ($mode ne 'loadonly') {
1.28 ! www 349: ($errtext,$infotext)=&savemap($fn,$errtext);
1.2 www 350: }
1.28 ! www 351: ($outtext,$errtext,$infotext)=&loadmap($fn,$errtext,$infotext);
1.1 www 352:
353: $r->print(<<ENDDOCUMENT);
354: <html>
1.8 harris41 355: <body bgcolor="#FFFFFF">
1.2 www 356: <form name=storage method=post action="$url">
357: <input type=hidden name=output value="$outtext">
1.1 www 358: </form>
1.8 harris41 359: <script>
1.9 harris41 360: parent.flag=1;
1.8 harris41 361: </script>
1.2 www 362: ENDDOCUMENT
1.28 ! www 363: if (($errtext ne '') || ($infotext ne '')) {
1.2 www 364: $r->print(<<ENDSCRIPT);
365: <script>
1.28 ! www 366: alert("$infotext $errtext");
1.2 www 367: </script>
368: ENDSCRIPT
369: }
370: $r->print("</body>\n</html>\n");
1.1 www 371:
372: return OK;
373: }
374:
375: 1;
376: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>