Annotation of rat/lonpage.pm, revision 1.21
1.1 www 1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
4: # (TeX Content Handler
5: #
6: # 05/29/00,05/30 Gerd Kortemeyer)
1.10 www 7: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
1.20 www 8: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer
1.1 www 9:
10: package Apache::lonpage;
11:
12: use strict;
13: use Apache::Constants qw(:common :http);
14: use Apache::lonnet();
1.21 ! www 15: use Apache::lonxml();
1.6 www 16: use HTML::TokeParser;
1.1 www 17: use GDBM_File;
18:
1.2 www 19: # -------------------------------------------------------------- Module Globals
20: my %hash;
21: my @rows;
1.6 www 22:
23: # ------------------------------------------------------------------ Euclid gcd
24:
25: sub euclid {
26: my ($e,$f)=@_;
27: my $a; my $b; my $r;
28: if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
29: while ($r!=0) {
30: $a=$b; $b=$r;
31: $r=$a%$b;
32: }
33: return $b;
34: }
1.2 www 35:
36: # ------------------------------------------------------------ Build page table
37:
38: sub tracetable {
39: my ($sofar,$rid,$beenhere)=@_;
40: my $further=$sofar;
41: unless ($beenhere=~/\&$rid\&/) {
42: $beenhere.=$rid.'&';
43:
44: if (defined($hash{'is_map_'.$rid})) {
45: if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
46: (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
1.4 www 47: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
48: $sofar=
1.2 www 49: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
1.3 www 50: '&'.$frid.'&');
1.4 www 51: $sofar++;
52: if ($hash{'src_'.$frid}) {
1.3 www 53: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
54: if (($brepriv eq '2') || ($brepriv eq 'F')) {
55: if (defined($rows[$sofar])) {
56: $rows[$sofar].='&'.$frid;
57: } else {
58: $rows[$sofar]=$frid;
59: }
60: }
1.4 www 61: }
1.2 www 62: }
63: } else {
1.4 www 64: $sofar++;
65: if ($hash{'src_'.$rid}) {
1.3 www 66: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
67: if (($brepriv eq '2') || ($brepriv eq 'F')) {
68: if (defined($rows[$sofar])) {
1.2 www 69: $rows[$sofar].='&'.$rid;
1.3 www 70: } else {
1.2 www 71: $rows[$sofar]=$rid;
1.3 www 72: }
73: }
1.4 www 74: }
1.2 www 75: }
76:
77: if (defined($hash{'to_'.$rid})) {
1.11 www 78: my $mincond=1;
79: my $next='';
1.2 www 80: map {
1.11 www 81: my $thiscond=
82: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
83: if ($thiscond>=$mincond) {
84: if ($next) {
85: $next.=','.$_.':'.$thiscond;
86: } else {
87: $next=$_.':'.$thiscond;
88: }
89: if ($thiscond>$mincond) { $mincond=$thiscond; }
90: }
1.2 www 91: } split(/\,/,$hash{'to_'.$rid});
1.11 www 92: map {
93: my ($linkid,$condval)=split(/\:/,$_);
94: if ($condval>=$mincond) {
95: my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
96: if ($now>$further) { $further=$now; }
97: }
98: } split(/\,/,$next);
99:
1.2 www 100: }
101: }
102: return $further;
103: }
104:
1.1 www 105: # ================================================================ Main Handler
106:
107: sub handler {
108: my $r=shift;
109:
1.3 www 110: # ------------------------------------------- Set document type for header only
1.1 www 111:
1.3 www 112: if ($r->header_only) {
113: if ($ENV{'browser.mathml'}) {
114: $r->content_type('text/xml');
115: } else {
116: $r->content_type('text/html');
117: }
118: $r->send_http_header;
119: return OK;
120: }
1.1 www 121:
122: my $requrl=$r->uri;
123: # ----------------------------------------------------------------- Tie db file
124: if ($ENV{'request.course.fn'}) {
125: my $fn=$ENV{'request.course.fn'};
126: if (-e "$fn.db") {
1.7 www 127: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
1.1 www 128: # ------------------------------------------------------------------- Hash tied
129: my $firstres=$hash{'map_start_'.$requrl};
130: my $lastres=$hash{'map_finish_'.$requrl};
131: if (($firstres) && ($lastres)) {
132: # ----------------------------------------------------------------- Render page
133:
1.3 www 134: @rows=();
1.2 www 135:
136: &tracetable(0,$firstres,'&'.$lastres.'&');
1.4 www 137: if ($hash{'src_'.$lastres}) {
138: my $brepriv=
139: &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
140: if (($brepriv eq '2') || ($brepriv eq 'F')) {
141: $rows[$#rows+1]=''.$lastres;
142: }
143: }
1.2 www 144:
1.9 www 145: # ------------------------------------------------------------ Add to symb list
146:
1.2 www 147: my $i;
1.9 www 148: my %symbhash=();
149: for ($i=0;$i<=$#rows;$i++) {
150: if ($rows[$i]) {
151: my @colcont=split(/\&/,$rows[$i]);
152: map {
153: $symbhash{$hash{'src_'.$_}}='';
154: } @colcont;
155: }
156: }
157: &Apache::lonnet::symblist($requrl,%symbhash);
158:
159: # ------------------------------------------------------------------ Page parms
160:
1.4 www 161: my $j;
1.6 www 162: my $lcm=1;
163: my $contents=0;
1.7 www 164: my $nforms=0;
1.6 www 165:
166: my %ssibody=();
167: my %ssibgcolor=();
168: my %ssitext=();
169: my %ssilink=();
170: my %ssivlink=();
171: my %ssialink=();
1.14 www 172:
173: my %metalink=();
174:
1.6 www 175: my %cellemb=();
1.3 www 176:
1.7 www 177: my $allscript='';
178: my $allmeta='';
179:
180: my $isxml=0;
181: my $xmlheader='';
182: my $xmlbody='';
183:
1.3 www 184: # --------------------------------------------- Get SSI output, post parameters
185:
1.2 www 186: for ($i=0;$i<=$#rows;$i++) {
1.4 www 187: if ($rows[$i]) {
1.6 www 188: $contents++;
1.3 www 189: my @colcont=split(/\&/,$rows[$i]);
1.6 www 190: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
1.3 www 191: map {
192: my $src=$hash{'src_'.$_};
1.5 www 193: $src=~/\.(\w+)$/;
1.14 www 194: $metalink{$_}=$src.'.meta';
1.3 www 195: $cellemb{$_}=Apache::lonnet::fileembstyle($1);
196: if ($cellemb{$_} eq 'ssi') {
197: # --------------------------------------------------------- This is an SSI cell
1.5 www 198: my $prefix=$_.'_';
199: my %posthash=('request.prefix' => $prefix);
1.8 www 200: if (($ENV{'form.'.$prefix.'submit'})
1.7 www 201: || ($ENV{'form.all_submit'})) {
202: map {
1.5 www 203: if ($_=~/^form.$prefix/) {
204: my $name=$_;
205: $name=~s/^form.$prefix//;
206: $posthash{$name}=$ENV{$_};
207: }
1.7 www 208: } keys %ENV;
209: }
1.5 www 210: my $output=Apache::lonnet::ssi($src,%posthash);
1.6 www 211: my $parser=HTML::TokeParser->new(\$output);
212: my $token;
1.12 www 213: my $thisdir=$src;
1.6 www 214: my $bodydef=0;
1.7 www 215: my $thisxml=0;
1.12 www 216: my @rlinks=();
1.7 www 217: if ($output=~/\?xml/) {
218: $isxml=1;
219: $thisxml=1;
220: $output=~
221: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
222: $xmlheader=$1;
223: }
1.12 www 224: while ($token=$parser->get_token) {
225: if ($token->[0] eq 'S') {
226: if ($token->[1] eq 'a') {
227: if ($token->[2]->{'href'}) {
228: $rlinks[$#rlinks+1]=
229: $token->[2]->{'href'};
230: }
231: } elsif ($token->[1] eq 'img') {
232: $rlinks[$#rlinks+1]=
233: $token->[2]->{'src'};
234: } elsif ($token->[1] eq 'embed') {
235: $rlinks[$#rlinks+1]=
236: $token->[2]->{'src'};
237: } elsif ($token->[1] eq 'base') {
238: $thisdir=$token->[2]->{'href'};
239: } elsif ($token->[1] eq 'body') {
1.7 www 240: $bodydef=1;
241: $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
242: $ssitext{$_}=$token->[2]->{'text'};
243: $ssilink{$_}=$token->[2]->{'link'};
244: $ssivlink{$_}=$token->[2]->{'vlink'};
245: $ssialink{$_}=$token->[2]->{'alink'};
246: if ($thisxml) {
247: $xmlbody=$token->[4];
248: }
1.12 www 249: } elsif ($token->[1] eq 'meta') {
1.7 www 250: $allmeta.="\n".$token->[4].'</meta>';
1.12 www 251: } elsif (($token->[1] eq 'script') &&
252: ($bodydef==0)) {
1.7 www 253: $allscript.="\n\n"
254: .$parser->get_text('/script');
1.6 www 255: }
1.12 www 256: }
257: }
1.6 www 258: if ($output=~/\<body[^\>]*\>(.*)/si) {
259: $output=$1;
260: }
261: $output=~s/\<\/body\>.*//si;
1.7 www 262: if ($output=~/\<form/si) {
263: $nforms++;
264: $output=~s/\<form[^\>]*\>//gsi;
265: $output=~s/\<\/form[^\>]*\>//gsi;
1.17 www 266: $output=~
1.18 www 267: s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
1.7 www 268: }
1.12 www 269: $thisdir=~s/\/[^\/]*$//;
270: map {
271: unless (($_=~/^http:\/\//i) ||
272: ($_=~/^\//)) {
273: my $newlocation=
274: &Apache::lonnet::hreflocation($thisdir,$_);
275: $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
276: }
277: } @rlinks;
278: $output=~s/\<\s*applet/\<applet codebase=\"$thisdir\" /gi;
1.5 www 279: $ssibody{$_}=$output;
1.3 www 280: # ---------------------------------------------------------------- End SSI cell
281: }
282: } @colcont;
1.4 www 283: }
1.2 www 284: }
1.6 www 285: unless ($contents) {
1.3 www 286: $r->content_type('text/html');
287: $r->send_http_header;
288: $r->print('<html><body>Empty page.</body></html>');
289: } else {
290: # ------------------------------------------------------------------ Build page
1.7 www 291:
292: # ---------------------------------------------------------------- Send headers
293: if ($isxml) {
294: $r->content_type('text/xml');
295: $r->send_http_header;
296: $r->print($xmlheader);
297: } else {
298: $r->content_type('text/html');
299: $r->send_http_header;
300: $r->print('<html>');
301: }
302: # ------------------------------------------------------------------------ Head
303: $r->print("\n<head>\n".$allmeta);
1.21 ! www 304: $allscript=~
! 305: s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
1.7 www 306: if ($allscript) {
1.21 ! www 307: $r->print("\n<script language='JavaScript'>\n".
! 308: $allscript."\n</script>\n");
1.7 www 309: }
1.21 ! www 310: $r->print(&Apache::lonxml::registerurl);
1.7 www 311: $r->print("\n</head>\n");
312: # ------------------------------------------------------------------ Start body
313: if ($isxml) {
314: $r->print($xmlbody);
315: } else {
1.21 ! www 316: $r->print(
! 317: '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
! 318: '" onUnload="'.&Apache::lonxml::unloadevents.'">');
1.7 www 319: }
320: # ------------------------------------------------------------------ Start form
321: if ($nforms) {
322: $r->print('<form method="post" action="'.
323: $requrl.'">');
324: }
325: # ----------------------------------------------------------------- Start table
326: $r->print('<table cols="'.$lcm.'" border="0">');
1.5 www 327: for ($i=0;$i<=$#rows;$i++) {
328: if ($rows[$i]) {
1.4 www 329: $r->print("\n<tr>");
330: my @colcont=split(/\&/,$rows[$i]);
1.6 www 331: my $avespan=$lcm/($#colcont+1);
332: for ($j=0;$j<=$#colcont;$j++) {
333: my $rid=$colcont[$j];
1.14 www 334: my $metainfo='<a href="'.
335: $metalink{$rid}.'" target="LONcatInfo">'.
336: '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
337: '</img></a><br></br>';
1.6 www 338: $r->print('<td colspan="'.$avespan.'"');
339: if ($cellemb{$rid} eq 'ssi') {
1.7 www 340: if ($ssibgcolor{$rid}) {
341: $r->print(' bgcolor="'.
342: $ssibgcolor{$rid}.'"');
343: }
1.14 www 344: $r->print('>'.$metainfo.'<font');
1.7 www 345: if ($ssitext{$rid}) {
346: $r->print(' text="'.$ssitext{$rid}.'"');
347: }
348: if ($ssilink{$rid}) {
349: $r->print(' link="'.$ssilink{$rid}.'"');
350: }
351: if ($ssitext{$rid}) {
352: $r->print(' vlink="'.$ssivlink{$rid}.'"');
353: }
354: if ($ssialink{$rid}) {
355: $r->print(' alink="'.$ssialink{$rid}.'"');
356: }
357:
358: $r->print('>'.$ssibody{$rid}.'</font>');
1.6 www 359: } elsif ($cellemb{$rid} eq 'img') {
1.14 www 360: $r->print('>'.$metainfo.'<img src="'.
1.7 www 361: $hash{'src_'.$rid}.'"></img>');
1.13 www 362: } elsif ($cellemb{$rid} eq 'emb') {
1.14 www 363: $r->print('>'.$metainfo.'<embed src="'.
1.13 www 364: $hash{'src_'.$rid}.'"></embed>');
365: }
1.6 www 366: $r->print('</td>');
1.4 www 367: }
368: $r->print('</tr>');
1.5 www 369: }
1.4 www 370: }
371: $r->print("\n</table>");
1.7 www 372: # ---------------------------------------------------------------- Submit, etc.
373: if ($nforms) {
374: $r->print(
375: '<input name="all_submit" value="Submit All" type="'.
376: (($nforms>1)?'submit':'hidden').'"></input></form>');
377: }
1.4 www 378: $r->print('</body></html>');
1.3 www 379: # -------------------------------------------------------------------- End page
380: }
1.1 www 381: # ------------------------------------------------------------- End render page
382: } else {
1.3 www 383: $r->content_type('text/html');
384: $r->send_http_header;
385: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 386: }
387: # ------------------------------------------------------------------ Untie hash
388: unless (untie(%hash)) {
389: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
390: "Could not untie coursemap $fn (browse).</font>");
391: }
392: # -------------------------------------------------------------------- All done
393: return OK;
394: # ----------------------------------------------- Errors, hash could no be tied
395: }
396: }
397: }
1.10 www 398: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
1.1 www 399: return HTTP_NOT_ACCEPTABLE;
400: }
401:
402: 1;
403: __END__
404:
405:
406:
407:
408:
409:
410:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>