Annotation of rat/lonpage.pm, revision 1.10
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,
! 8: # 10/02 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.6 www 15: use HTML::TokeParser;
1.1 www 16: use GDBM_File;
17:
1.2 www 18: # -------------------------------------------------------------- Module Globals
19: my %hash;
20: my @rows;
1.6 www 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: }
1.2 www 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}}))) {
1.4 www 46: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
47: $sofar=
1.2 www 48: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
1.3 www 49: '&'.$frid.'&');
1.4 www 50: $sofar++;
51: if ($hash{'src_'.$frid}) {
1.3 www 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: }
1.4 www 60: }
1.2 www 61: }
62: } else {
1.4 www 63: $sofar++;
64: if ($hash{'src_'.$rid}) {
1.3 www 65: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
66: if (($brepriv eq '2') || ($brepriv eq 'F')) {
67: if (defined($rows[$sofar])) {
1.2 www 68: $rows[$sofar].='&'.$rid;
1.3 www 69: } else {
1.2 www 70: $rows[$sofar]=$rid;
1.3 www 71: }
72: }
1.4 www 73: }
1.2 www 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:
1.1 www 86: # ================================================================ Main Handler
87:
88: sub handler {
89: my $r=shift;
90:
1.3 www 91: # ------------------------------------------- Set document type for header only
1.1 www 92:
1.3 www 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: }
1.1 www 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") {
1.7 www 108: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
1.1 www 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:
1.3 www 115: @rows=();
1.2 www 116:
117: &tracetable(0,$firstres,'&'.$lastres.'&');
1.4 www 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: }
1.2 www 125:
1.9 www 126: # ------------------------------------------------------------ Add to symb list
127:
1.2 www 128: my $i;
1.9 www 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:
1.4 www 142: my $j;
1.6 www 143: my $lcm=1;
144: my $contents=0;
1.7 www 145: my $nforms=0;
1.6 www 146:
147: my %ssibody=();
148: my %ssibgcolor=();
149: my %ssitext=();
150: my %ssilink=();
151: my %ssivlink=();
152: my %ssialink=();
153: my %cellemb=();
1.3 www 154:
1.7 www 155: my $allscript='';
156: my $allmeta='';
157:
158: my $isxml=0;
159: my $xmlheader='';
160: my $xmlbody='';
161:
1.3 www 162: # --------------------------------------------- Get SSI output, post parameters
163:
1.2 www 164: for ($i=0;$i<=$#rows;$i++) {
1.4 www 165: if ($rows[$i]) {
1.6 www 166: $contents++;
1.3 www 167: my @colcont=split(/\&/,$rows[$i]);
1.6 www 168: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
1.3 www 169: map {
170: my $src=$hash{'src_'.$_};
1.5 www 171: $src=~/\.(\w+)$/;
1.3 www 172: $cellemb{$_}=Apache::lonnet::fileembstyle($1);
173: if ($cellemb{$_} eq 'ssi') {
174: # --------------------------------------------------------- This is an SSI cell
1.5 www 175: my $prefix=$_.'_';
176: my %posthash=('request.prefix' => $prefix);
1.8 www 177: if (($ENV{'form.'.$prefix.'submit'})
1.7 www 178: || ($ENV{'form.all_submit'})) {
179: map {
1.5 www 180: if ($_=~/^form.$prefix/) {
181: my $name=$_;
182: $name=~s/^form.$prefix//;
183: $posthash{$name}=$ENV{$_};
184: }
1.7 www 185: } keys %ENV;
186: }
1.5 www 187: my $output=Apache::lonnet::ssi($src,%posthash);
1.6 www 188: my $parser=HTML::TokeParser->new(\$output);
189: my $token;
190: my $bodydef=0;
1.7 www 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: }
1.6 www 199: while (($bodydef==0) &&
200: ($token=$parser->get_token)) {
201: if ($token->[1] eq 'body') {
1.7 www 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: }
1.6 www 211: }
212: if ($token->[1] eq 'meta') {
1.7 www 213: $allmeta.="\n".$token->[4].'</meta>';
1.6 www 214: }
215: if ($token->[1] eq 'script') {
1.7 www 216: $allscript.="\n\n"
217: .$parser->get_text('/script');
1.6 www 218: }
219: }
220: if ($output=~/\<body[^\>]*\>(.*)/si) {
221: $output=$1;
222: }
223: $output=~s/\<\/body\>.*//si;
1.7 www 224: if ($output=~/\<form/si) {
225: $nforms++;
226: $output=~s/\<form[^\>]*\>//gsi;
227: $output=~s/\<\/form[^\>]*\>//gsi;
228: }
1.5 www 229: $ssibody{$_}=$output;
1.3 www 230:
231: # ---------------------------------------------------------------- End SSI cell
232: }
233: } @colcont;
1.4 www 234: }
1.2 www 235: }
1.6 www 236: unless ($contents) {
1.3 www 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
1.7 www 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">');
1.5 www 272: for ($i=0;$i<=$#rows;$i++) {
273: if ($rows[$i]) {
1.4 www 274: $r->print("\n<tr>");
275: my @colcont=split(/\&/,$rows[$i]);
1.6 www 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') {
1.7 www 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>');
1.6 www 300: } elsif ($cellemb{$rid} eq 'img') {
1.7 www 301: $r->print('><img src="'.
302: $hash{'src_'.$rid}.'"></img>');
1.6 www 303: }
304: $r->print('</td>');
1.4 www 305: }
306: $r->print('</tr>');
1.5 www 307: }
1.4 www 308: }
309: $r->print("\n</table>");
1.7 www 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: }
1.4 www 316: $r->print('</body></html>');
1.3 www 317: # -------------------------------------------------------------------- End page
318: }
1.1 www 319: # ------------------------------------------------------------- End render page
320: } else {
1.3 www 321: $r->content_type('text/html');
322: $r->send_http_header;
323: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 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: }
1.10 ! www 336: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
1.1 www 337: return HTTP_NOT_ACCEPTABLE;
338: }
339:
340: 1;
341: __END__
342:
343:
344:
345:
346:
347:
348:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>