Annotation of rat/lonpage.pm, revision 1.8
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:
125: my $i;
1.4 www 126: my $j;
1.6 www 127: my $lcm=1;
128: my $contents=0;
1.7 www 129: my $nforms=0;
1.6 www 130:
131: my %ssibody=();
132: my %ssibgcolor=();
133: my %ssitext=();
134: my %ssilink=();
135: my %ssivlink=();
136: my %ssialink=();
137: my %cellemb=();
1.3 www 138:
1.7 www 139: my $allscript='';
140: my $allmeta='';
141:
142: my $isxml=0;
143: my $xmlheader='';
144: my $xmlbody='';
145:
1.3 www 146: # --------------------------------------------- Get SSI output, post parameters
147:
1.2 www 148: for ($i=0;$i<=$#rows;$i++) {
1.4 www 149: if ($rows[$i]) {
1.6 www 150: $contents++;
1.3 www 151: my @colcont=split(/\&/,$rows[$i]);
1.6 www 152: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
1.3 www 153: map {
154: my $src=$hash{'src_'.$_};
1.5 www 155: $src=~/\.(\w+)$/;
1.3 www 156: $cellemb{$_}=Apache::lonnet::fileembstyle($1);
157: if ($cellemb{$_} eq 'ssi') {
158: # --------------------------------------------------------- This is an SSI cell
1.5 www 159: my $prefix=$_.'_';
160: my %posthash=('request.prefix' => $prefix);
1.8 ! www 161: if (($ENV{'form.'.$prefix.'submit'})
1.7 www 162: || ($ENV{'form.all_submit'})) {
163: map {
1.5 www 164: if ($_=~/^form.$prefix/) {
165: my $name=$_;
166: $name=~s/^form.$prefix//;
167: $posthash{$name}=$ENV{$_};
168: }
1.7 www 169: } keys %ENV;
170: }
1.5 www 171: my $output=Apache::lonnet::ssi($src,%posthash);
1.6 www 172: my $parser=HTML::TokeParser->new(\$output);
173: my $token;
174: my $bodydef=0;
1.7 www 175: my $thisxml=0;
176: if ($output=~/\?xml/) {
177: $isxml=1;
178: $thisxml=1;
179: $output=~
180: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
181: $xmlheader=$1;
182: }
1.6 www 183: while (($bodydef==0) &&
184: ($token=$parser->get_token)) {
185: if ($token->[1] eq 'body') {
1.7 www 186: $bodydef=1;
187: $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
188: $ssitext{$_}=$token->[2]->{'text'};
189: $ssilink{$_}=$token->[2]->{'link'};
190: $ssivlink{$_}=$token->[2]->{'vlink'};
191: $ssialink{$_}=$token->[2]->{'alink'};
192: if ($thisxml) {
193: $xmlbody=$token->[4];
194: }
1.6 www 195: }
196: if ($token->[1] eq 'meta') {
1.7 www 197: $allmeta.="\n".$token->[4].'</meta>';
1.6 www 198: }
199: if ($token->[1] eq 'script') {
1.7 www 200: $allscript.="\n\n"
201: .$parser->get_text('/script');
1.6 www 202: }
203: }
204: if ($output=~/\<body[^\>]*\>(.*)/si) {
205: $output=$1;
206: }
207: $output=~s/\<\/body\>.*//si;
1.7 www 208: if ($output=~/\<form/si) {
209: $nforms++;
210: $output=~s/\<form[^\>]*\>//gsi;
211: $output=~s/\<\/form[^\>]*\>//gsi;
212: }
1.5 www 213: $ssibody{$_}=$output;
1.3 www 214:
215: # ---------------------------------------------------------------- End SSI cell
216: }
217: } @colcont;
1.4 www 218: }
1.2 www 219: }
1.6 www 220: unless ($contents) {
1.3 www 221: $r->content_type('text/html');
222: $r->send_http_header;
223: $r->print('<html><body>Empty page.</body></html>');
224: } else {
225: # ------------------------------------------------------------------ Build page
1.7 www 226:
227: # ---------------------------------------------------------------- Send headers
228: if ($isxml) {
229: $r->content_type('text/xml');
230: $r->send_http_header;
231: $r->print($xmlheader);
232: } else {
233: $r->content_type('text/html');
234: $r->send_http_header;
235: $r->print('<html>');
236: }
237: # ------------------------------------------------------------------------ Head
238: $r->print("\n<head>\n".$allmeta);
239: if ($allscript) {
240: $r->print("\n<script>\n".$allscript."\n</script>\n");
241: }
242: $r->print("\n</head>\n");
243: # ------------------------------------------------------------------ Start body
244: if ($isxml) {
245: $r->print($xmlbody);
246: } else {
247: $r->print('<body bgcolor="#FFFFFF">');
248: }
249:
250: # ------------------------------------------------------------------ Start form
251: if ($nforms) {
252: $r->print('<form method="post" action="'.
253: $requrl.'">');
254: }
255: # ----------------------------------------------------------------- Start table
256: $r->print('<table cols="'.$lcm.'" border="0">');
1.5 www 257: for ($i=0;$i<=$#rows;$i++) {
258: if ($rows[$i]) {
1.4 www 259: $r->print("\n<tr>");
260: my @colcont=split(/\&/,$rows[$i]);
1.6 www 261: my $avespan=$lcm/($#colcont+1);
262: for ($j=0;$j<=$#colcont;$j++) {
263: my $rid=$colcont[$j];
264: $r->print('<td colspan="'.$avespan.'"');
265: if ($cellemb{$rid} eq 'ssi') {
1.7 www 266: if ($ssibgcolor{$rid}) {
267: $r->print(' bgcolor="'.
268: $ssibgcolor{$rid}.'"');
269: }
270: $r->print('><font');
271: if ($ssitext{$rid}) {
272: $r->print(' text="'.$ssitext{$rid}.'"');
273: }
274: if ($ssilink{$rid}) {
275: $r->print(' link="'.$ssilink{$rid}.'"');
276: }
277: if ($ssitext{$rid}) {
278: $r->print(' vlink="'.$ssivlink{$rid}.'"');
279: }
280: if ($ssialink{$rid}) {
281: $r->print(' alink="'.$ssialink{$rid}.'"');
282: }
283:
284: $r->print('>'.$ssibody{$rid}.'</font>');
1.6 www 285: } elsif ($cellemb{$rid} eq 'img') {
1.7 www 286: $r->print('><img src="'.
287: $hash{'src_'.$rid}.'"></img>');
1.6 www 288: }
289: $r->print('</td>');
1.4 www 290: }
291: $r->print('</tr>');
1.5 www 292: }
1.4 www 293: }
294: $r->print("\n</table>");
1.7 www 295: # ---------------------------------------------------------------- Submit, etc.
296: if ($nforms) {
297: $r->print(
298: '<input name="all_submit" value="Submit All" type="'.
299: (($nforms>1)?'submit':'hidden').'"></input></form>');
300: }
1.4 www 301: $r->print('</body></html>');
1.3 www 302: # -------------------------------------------------------------------- End page
303: }
1.1 www 304: # ------------------------------------------------------------- End render page
305: } else {
1.3 www 306: $r->content_type('text/html');
307: $r->send_http_header;
308: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 309: }
310: # ------------------------------------------------------------------ Untie hash
311: unless (untie(%hash)) {
312: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
313: "Could not untie coursemap $fn (browse).</font>");
314: }
315: # -------------------------------------------------------------------- All done
316: return OK;
317: # ----------------------------------------------- Errors, hash could no be tied
318: }
319: }
320: }
321: $ENV{'user.error.msg'}="$requrl:bre:1:1:Course not initialized";
322: return HTTP_NOT_ACCEPTABLE;
323: }
324:
325: 1;
326: __END__
327:
328:
329:
330:
331:
332:
333:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>