1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
4: # (TeX Content Handler
5: #
6: # 05/29/00,05/30 Gerd Kortemeyer)
7: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
8: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer
9:
10: package Apache::lonpage;
11:
12: use strict;
13: use Apache::Constants qw(:common :http);
14: use Apache::lonnet();
15: use Apache::lonxml();
16: use HTML::TokeParser;
17: use GDBM_File;
18:
19: # -------------------------------------------------------------- Module Globals
20: my %hash;
21: my @rows;
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: }
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}}))) {
47: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
48: $sofar=
49: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
50: '&'.$frid.'&');
51: $sofar++;
52: if ($hash{'src_'.$frid}) {
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: }
61: }
62: }
63: } else {
64: $sofar++;
65: if ($hash{'src_'.$rid}) {
66: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
67: if (($brepriv eq '2') || ($brepriv eq 'F')) {
68: if (defined($rows[$sofar])) {
69: $rows[$sofar].='&'.$rid;
70: } else {
71: $rows[$sofar]=$rid;
72: }
73: }
74: }
75: }
76:
77: if (defined($hash{'to_'.$rid})) {
78: my $mincond=1;
79: my $next='';
80: map {
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: }
91: } split(/\,/,$hash{'to_'.$rid});
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:
100: }
101: }
102: return $further;
103: }
104:
105: # ================================================================ Main Handler
106:
107: sub handler {
108: my $r=shift;
109:
110: # ------------------------------------------- Set document type for header only
111:
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: }
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") {
127: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
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:
134: @rows=();
135:
136: &tracetable(0,$firstres,'&'.$lastres.'&');
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: }
144:
145: # ------------------------------------------------------------ Add to symb list
146:
147: my $i;
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:
161: my $j;
162: my $lcm=1;
163: my $contents=0;
164: my $nforms=0;
165:
166: my %ssibody=();
167: my %ssibgcolor=();
168: my %ssitext=();
169: my %ssilink=();
170: my %ssivlink=();
171: my %ssialink=();
172:
173: my %metalink=();
174:
175: my %cellemb=();
176:
177: my $allscript='';
178: my $allmeta='';
179:
180: my $isxml=0;
181: my $xmlheader='';
182: my $xmlbody='';
183:
184: # --------------------------------------------- Get SSI output, post parameters
185:
186: for ($i=0;$i<=$#rows;$i++) {
187: if ($rows[$i]) {
188: $contents++;
189: my @colcont=split(/\&/,$rows[$i]);
190: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
191: map {
192: my $src=$hash{'src_'.$_};
193: $src=~/\.(\w+)$/;
194: $metalink{$_}=$src.'.meta';
195: $cellemb{$_}=Apache::lonnet::fileembstyle($1);
196: if ($cellemb{$_} eq 'ssi') {
197: # --------------------------------------------------------- This is an SSI cell
198: my $prefix=$_.'_';
199: my %posthash=('request.prefix' => $prefix);
200: if (($ENV{'form.'.$prefix.'submit'})
201: || ($ENV{'form.all_submit'})) {
202: map {
203: if ($_=~/^form.$prefix/) {
204: my $name=$_;
205: $name=~s/^form.$prefix//;
206: $posthash{$name}=$ENV{$_};
207: }
208: } keys %ENV;
209: }
210: my $output=Apache::lonnet::ssi($src,%posthash);
211: my $parser=HTML::TokeParser->new(\$output);
212: my $token;
213: my $thisdir=$src;
214: my $bodydef=0;
215: my $thisxml=0;
216: my @rlinks=();
217: if ($output=~/\?xml/) {
218: $isxml=1;
219: $thisxml=1;
220: $output=~
221: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
222: $xmlheader=$1;
223: }
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') {
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: }
249: } elsif ($token->[1] eq 'meta') {
250: $allmeta.="\n".$token->[4].'</meta>';
251: } elsif (($token->[1] eq 'script') &&
252: ($bodydef==0)) {
253: $allscript.="\n\n"
254: .$parser->get_text('/script');
255: }
256: }
257: }
258: if ($output=~/\<body[^\>]*\>(.*)/si) {
259: $output=$1;
260: }
261: $output=~s/\<\/body\>.*//si;
262: if ($output=~/\<form/si) {
263: $nforms++;
264: $output=~s/\<form[^\>]*\>//gsi;
265: $output=~s/\<\/form[^\>]*\>//gsi;
266: $output=~
267: s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
268: }
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;
279: $ssibody{$_}=$output;
280: # ---------------------------------------------------------------- End SSI cell
281: }
282: } @colcont;
283: }
284: }
285: unless ($contents) {
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
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);
304: $allscript=~
305: s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
306: if ($allscript) {
307: $r->print("\n<script language='JavaScript'>\n".
308: $allscript."\n</script>\n");
309: }
310: $r->print(&Apache::lonxml::registerurl);
311: $r->print("\n</head>\n");
312: # ------------------------------------------------------------------ Start body
313: if ($isxml) {
314: $r->print($xmlbody);
315: } else {
316: $r->print(
317: '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
318: '" onUnload="'.&Apache::lonxml::unloadevents.'">');
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">');
327: for ($i=0;$i<=$#rows;$i++) {
328: if ($rows[$i]) {
329: $r->print("\n<tr>");
330: my @colcont=split(/\&/,$rows[$i]);
331: my $avespan=$lcm/($#colcont+1);
332: for ($j=0;$j<=$#colcont;$j++) {
333: my $rid=$colcont[$j];
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>';
338: $r->print('<td colspan="'.$avespan.'"');
339: if ($cellemb{$rid} eq 'ssi') {
340: if ($ssibgcolor{$rid}) {
341: $r->print(' bgcolor="'.
342: $ssibgcolor{$rid}.'"');
343: }
344: $r->print('>'.$metainfo.'<font');
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>');
359: } elsif ($cellemb{$rid} eq 'img') {
360: $r->print('>'.$metainfo.'<img src="'.
361: $hash{'src_'.$rid}.'"></img>');
362: } elsif ($cellemb{$rid} eq 'emb') {
363: $r->print('>'.$metainfo.'<embed src="'.
364: $hash{'src_'.$rid}.'"></embed>');
365: }
366: $r->print('</td>');
367: }
368: $r->print('</tr>');
369: }
370: }
371: $r->print("\n</table>");
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: }
378: $r->print('</body></html>');
379: # -------------------------------------------------------------------- End page
380: }
381: # ------------------------------------------------------------- End render page
382: } else {
383: $r->content_type('text/html');
384: $r->send_http_header;
385: $r->print('<html><body>Page undefined.</body></html>');
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: }
398: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
399: return HTTP_NOT_ACCEPTABLE;
400: }
401:
402: 1;
403: __END__
404:
405:
406:
407:
408:
409:
410:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>