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 Gerd Kortemeyer
9:
10: package Apache::lonpage;
11:
12: use strict;
13: use Apache::Constants qw(:common :http);
14: use Apache::lonnet();
15: use HTML::TokeParser;
16: use GDBM_File;
17:
18: # -------------------------------------------------------------- Module Globals
19: my %hash;
20: my @rows;
21:
22: # ------------------------------------------------------------------ Euclid gcd
23:
24: sub euclid {
25: my ($e,$f)=@_;
26: my $a; my $b; my $r;
27: if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
28: while ($r!=0) {
29: $a=$b; $b=$r;
30: $r=$a%$b;
31: }
32: return $b;
33: }
34:
35: # ------------------------------------------------------------ Build page table
36:
37: sub tracetable {
38: my ($sofar,$rid,$beenhere)=@_;
39: my $further=$sofar;
40: unless ($beenhere=~/\&$rid\&/) {
41: $beenhere.=$rid.'&';
42:
43: if (defined($hash{'is_map_'.$rid})) {
44: if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
45: (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
46: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
47: $sofar=
48: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
49: '&'.$frid.'&');
50: $sofar++;
51: if ($hash{'src_'.$frid}) {
52: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
53: if (($brepriv eq '2') || ($brepriv eq 'F')) {
54: if (defined($rows[$sofar])) {
55: $rows[$sofar].='&'.$frid;
56: } else {
57: $rows[$sofar]=$frid;
58: }
59: }
60: }
61: }
62: } else {
63: $sofar++;
64: if ($hash{'src_'.$rid}) {
65: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
66: if (($brepriv eq '2') || ($brepriv eq 'F')) {
67: if (defined($rows[$sofar])) {
68: $rows[$sofar].='&'.$rid;
69: } else {
70: $rows[$sofar]=$rid;
71: }
72: }
73: }
74: }
75:
76: if (defined($hash{'to_'.$rid})) {
77: map {
78: my $now=&tracetable($sofar,$hash{'goesto_'.$_},$beenhere);
79: if ($now>$further) { $further=$now; }
80: } split(/\,/,$hash{'to_'.$rid});
81: }
82: }
83: return $further;
84: }
85:
86: # ================================================================ Main Handler
87:
88: sub handler {
89: my $r=shift;
90:
91: # ------------------------------------------- Set document type for header only
92:
93: if ($r->header_only) {
94: if ($ENV{'browser.mathml'}) {
95: $r->content_type('text/xml');
96: } else {
97: $r->content_type('text/html');
98: }
99: $r->send_http_header;
100: return OK;
101: }
102:
103: my $requrl=$r->uri;
104: # ----------------------------------------------------------------- Tie db file
105: if ($ENV{'request.course.fn'}) {
106: my $fn=$ENV{'request.course.fn'};
107: if (-e "$fn.db") {
108: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
109: # ------------------------------------------------------------------- Hash tied
110: my $firstres=$hash{'map_start_'.$requrl};
111: my $lastres=$hash{'map_finish_'.$requrl};
112: if (($firstres) && ($lastres)) {
113: # ----------------------------------------------------------------- Render page
114:
115: @rows=();
116:
117: &tracetable(0,$firstres,'&'.$lastres.'&');
118: if ($hash{'src_'.$lastres}) {
119: my $brepriv=
120: &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
121: if (($brepriv eq '2') || ($brepriv eq 'F')) {
122: $rows[$#rows+1]=''.$lastres;
123: }
124: }
125:
126: # ------------------------------------------------------------ Add to symb list
127:
128: my $i;
129: my %symbhash=();
130: for ($i=0;$i<=$#rows;$i++) {
131: if ($rows[$i]) {
132: my @colcont=split(/\&/,$rows[$i]);
133: map {
134: $symbhash{$hash{'src_'.$_}}='';
135: } @colcont;
136: }
137: }
138: &Apache::lonnet::symblist($requrl,%symbhash);
139:
140: # ------------------------------------------------------------------ Page parms
141:
142: my $j;
143: my $lcm=1;
144: my $contents=0;
145: my $nforms=0;
146:
147: my %ssibody=();
148: my %ssibgcolor=();
149: my %ssitext=();
150: my %ssilink=();
151: my %ssivlink=();
152: my %ssialink=();
153: my %cellemb=();
154:
155: my $allscript='';
156: my $allmeta='';
157:
158: my $isxml=0;
159: my $xmlheader='';
160: my $xmlbody='';
161:
162: # --------------------------------------------- Get SSI output, post parameters
163:
164: for ($i=0;$i<=$#rows;$i++) {
165: if ($rows[$i]) {
166: $contents++;
167: my @colcont=split(/\&/,$rows[$i]);
168: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
169: map {
170: my $src=$hash{'src_'.$_};
171: $src=~/\.(\w+)$/;
172: $cellemb{$_}=Apache::lonnet::fileembstyle($1);
173: if ($cellemb{$_} eq 'ssi') {
174: # --------------------------------------------------------- This is an SSI cell
175: my $prefix=$_.'_';
176: my %posthash=('request.prefix' => $prefix);
177: if (($ENV{'form.'.$prefix.'submit'})
178: || ($ENV{'form.all_submit'})) {
179: map {
180: if ($_=~/^form.$prefix/) {
181: my $name=$_;
182: $name=~s/^form.$prefix//;
183: $posthash{$name}=$ENV{$_};
184: }
185: } keys %ENV;
186: }
187: my $output=Apache::lonnet::ssi($src,%posthash);
188: my $parser=HTML::TokeParser->new(\$output);
189: my $token;
190: my $bodydef=0;
191: my $thisxml=0;
192: if ($output=~/\?xml/) {
193: $isxml=1;
194: $thisxml=1;
195: $output=~
196: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
197: $xmlheader=$1;
198: }
199: while (($bodydef==0) &&
200: ($token=$parser->get_token)) {
201: if ($token->[1] eq 'body') {
202: $bodydef=1;
203: $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
204: $ssitext{$_}=$token->[2]->{'text'};
205: $ssilink{$_}=$token->[2]->{'link'};
206: $ssivlink{$_}=$token->[2]->{'vlink'};
207: $ssialink{$_}=$token->[2]->{'alink'};
208: if ($thisxml) {
209: $xmlbody=$token->[4];
210: }
211: }
212: if ($token->[1] eq 'meta') {
213: $allmeta.="\n".$token->[4].'</meta>';
214: }
215: if ($token->[1] eq 'script') {
216: $allscript.="\n\n"
217: .$parser->get_text('/script');
218: }
219: }
220: if ($output=~/\<body[^\>]*\>(.*)/si) {
221: $output=$1;
222: }
223: $output=~s/\<\/body\>.*//si;
224: if ($output=~/\<form/si) {
225: $nforms++;
226: $output=~s/\<form[^\>]*\>//gsi;
227: $output=~s/\<\/form[^\>]*\>//gsi;
228: }
229: $ssibody{$_}=$output;
230:
231: # ---------------------------------------------------------------- End SSI cell
232: }
233: } @colcont;
234: }
235: }
236: unless ($contents) {
237: $r->content_type('text/html');
238: $r->send_http_header;
239: $r->print('<html><body>Empty page.</body></html>');
240: } else {
241: # ------------------------------------------------------------------ Build page
242:
243: # ---------------------------------------------------------------- Send headers
244: if ($isxml) {
245: $r->content_type('text/xml');
246: $r->send_http_header;
247: $r->print($xmlheader);
248: } else {
249: $r->content_type('text/html');
250: $r->send_http_header;
251: $r->print('<html>');
252: }
253: # ------------------------------------------------------------------------ Head
254: $r->print("\n<head>\n".$allmeta);
255: if ($allscript) {
256: $r->print("\n<script>\n".$allscript."\n</script>\n");
257: }
258: $r->print("\n</head>\n");
259: # ------------------------------------------------------------------ Start body
260: if ($isxml) {
261: $r->print($xmlbody);
262: } else {
263: $r->print('<body bgcolor="#FFFFFF">');
264: }
265: # ------------------------------------------------------------------ Start form
266: if ($nforms) {
267: $r->print('<form method="post" action="'.
268: $requrl.'">');
269: }
270: # ----------------------------------------------------------------- Start table
271: $r->print('<table cols="'.$lcm.'" border="0">');
272: for ($i=0;$i<=$#rows;$i++) {
273: if ($rows[$i]) {
274: $r->print("\n<tr>");
275: my @colcont=split(/\&/,$rows[$i]);
276: my $avespan=$lcm/($#colcont+1);
277: for ($j=0;$j<=$#colcont;$j++) {
278: my $rid=$colcont[$j];
279: $r->print('<td colspan="'.$avespan.'"');
280: if ($cellemb{$rid} eq 'ssi') {
281: if ($ssibgcolor{$rid}) {
282: $r->print(' bgcolor="'.
283: $ssibgcolor{$rid}.'"');
284: }
285: $r->print('><font');
286: if ($ssitext{$rid}) {
287: $r->print(' text="'.$ssitext{$rid}.'"');
288: }
289: if ($ssilink{$rid}) {
290: $r->print(' link="'.$ssilink{$rid}.'"');
291: }
292: if ($ssitext{$rid}) {
293: $r->print(' vlink="'.$ssivlink{$rid}.'"');
294: }
295: if ($ssialink{$rid}) {
296: $r->print(' alink="'.$ssialink{$rid}.'"');
297: }
298:
299: $r->print('>'.$ssibody{$rid}.'</font>');
300: } elsif ($cellemb{$rid} eq 'img') {
301: $r->print('><img src="'.
302: $hash{'src_'.$rid}.'"></img>');
303: }
304: $r->print('</td>');
305: }
306: $r->print('</tr>');
307: }
308: }
309: $r->print("\n</table>");
310: # ---------------------------------------------------------------- Submit, etc.
311: if ($nforms) {
312: $r->print(
313: '<input name="all_submit" value="Submit All" type="'.
314: (($nforms>1)?'submit':'hidden').'"></input></form>');
315: }
316: $r->print('</body></html>');
317: # -------------------------------------------------------------------- End page
318: }
319: # ------------------------------------------------------------- End render page
320: } else {
321: $r->content_type('text/html');
322: $r->send_http_header;
323: $r->print('<html><body>Page undefined.</body></html>');
324: }
325: # ------------------------------------------------------------------ Untie hash
326: unless (untie(%hash)) {
327: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
328: "Could not untie coursemap $fn (browse).</font>");
329: }
330: # -------------------------------------------------------------------- All done
331: return OK;
332: # ----------------------------------------------- Errors, hash could no be tied
333: }
334: }
335: }
336: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
337: return HTTP_NOT_ACCEPTABLE;
338: }
339:
340: 1;
341: __END__
342:
343:
344:
345:
346:
347:
348:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>