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