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 Gerd Kortemeyer
8:
9: package Apache::lonpage;
10:
11: use strict;
12: use Apache::Constants qw(:common :http);
13: use Apache::lonnet();
14: use HTML::TokeParser;
15: use GDBM_File;
16:
17: # -------------------------------------------------------------- Module Globals
18: my %hash;
19: my @rows;
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: }
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}}))) {
45: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
46: $sofar=
47: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
48: '&'.$frid.'&');
49: $sofar++;
50: if ($hash{'src_'.$frid}) {
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: }
59: }
60: }
61: } else {
62: $sofar++;
63: if ($hash{'src_'.$rid}) {
64: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
65: if (($brepriv eq '2') || ($brepriv eq 'F')) {
66: if (defined($rows[$sofar])) {
67: $rows[$sofar].='&'.$rid;
68: } else {
69: $rows[$sofar]=$rid;
70: }
71: }
72: }
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:
85: # ================================================================ Main Handler
86:
87: sub handler {
88: my $r=shift;
89:
90: # ------------------------------------------- Set document type for header only
91:
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: }
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") {
107: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT,0640)) {
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:
114: @rows=();
115:
116: &tracetable(0,$firstres,'&'.$lastres.'&');
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: }
124:
125: my $i;
126: my $j;
127: my $lcm=1;
128: my $contents=0;
129:
130: my %ssibody=();
131: my %ssibgcolor=();
132: my %ssitext=();
133: my %ssilink=();
134: my %ssivlink=();
135: my %ssialink=();
136: my %cellemb=();
137:
138: # --------------------------------------------- Get SSI output, post parameters
139:
140: for ($i=0;$i<=$#rows;$i++) {
141: if ($rows[$i]) {
142: $contents++;
143: my @colcont=split(/\&/,$rows[$i]);
144: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
145: map {
146: my $src=$hash{'src_'.$_};
147: $src=~/\.(\w+)$/;
148: $cellemb{$_}=Apache::lonnet::fileembstyle($1);
149: if ($cellemb{$_} eq 'ssi') {
150: # --------------------------------------------------------- This is an SSI cell
151: my $prefix=$_.'_';
152: my %posthash=('request.prefix' => $prefix);
153: map {
154: if ($_=~/^form.$prefix/) {
155: my $name=$_;
156: $name=~s/^form.$prefix//;
157: $posthash{$name}=$ENV{$_};
158: }
159: } keys %ENV;
160: my $output=Apache::lonnet::ssi($src,%posthash);
161: my $parser=HTML::TokeParser->new(\$output);
162: my $token;
163: my $bodydef=0;
164: while (($bodydef==0) &&
165: ($token=$parser->get_token)) {
166: if ($token->[1] eq 'body') {
167: $bodydef=1
168: }
169: if ($token->[1] eq 'meta') {
170: }
171: if ($token->[1] eq 'script') {
172: }
173: if ($token->[1] eq 'basefont') {
174: }
175: }
176: if ($output=~/\<body[^\>]*\>(.*)/si) {
177: $output=$1;
178: }
179: $output=~s/\<\/body\>.*//si;
180: $ssibody{$_}=$output;
181:
182: # ---------------------------------------------------------------- End SSI cell
183: }
184: } @colcont;
185: }
186: }
187: unless ($contents) {
188: $r->content_type('text/html');
189: $r->send_http_header;
190: $r->print('<html><body>Empty page.</body></html>');
191: } else {
192: # ------------------------------------------------------------------ Build page
193: $r->content_type('text/html');
194: $r->send_http_header;
195: $r->print('<html><body>');
196:
197: $r->print('<table cols="'.$lcm.'" border="1">');
198: for ($i=0;$i<=$#rows;$i++) {
199: if ($rows[$i]) {
200: $r->print("\n<tr>");
201: my @colcont=split(/\&/,$rows[$i]);
202: my $avespan=$lcm/($#colcont+1);
203: for ($j=0;$j<=$#colcont;$j++) {
204: my $rid=$colcont[$j];
205: $r->print('<td colspan="'.$avespan.'"');
206: if ($cellemb{$rid} eq 'ssi') {
207: $r->print('>'.$ssibody{$rid});
208: } elsif ($cellemb{$rid} eq 'img') {
209: $r->print('><img src="'.
210: $hash{'src_'.$rid}.'">');
211: }
212: $r->print('</td>');
213: }
214: $r->print('</tr>');
215: }
216: }
217: $r->print("\n</table>");
218:
219: $r->print('</body></html>');
220: # -------------------------------------------------------------------- End page
221: }
222: # ------------------------------------------------------------- End render page
223: } else {
224: $r->content_type('text/html');
225: $r->send_http_header;
226: $r->print('<html><body>Page undefined.</body></html>');
227: }
228: # ------------------------------------------------------------------ Untie hash
229: unless (untie(%hash)) {
230: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
231: "Could not untie coursemap $fn (browse).</font>");
232: }
233: # -------------------------------------------------------------------- All done
234: return OK;
235: # ----------------------------------------------- Errors, hash could no be tied
236: }
237: }
238: }
239: $ENV{'user.error.msg'}="$requrl:bre:1:1:Course not initialized";
240: return HTTP_NOT_ACCEPTABLE;
241: }
242:
243: 1;
244: __END__
245:
246:
247:
248:
249:
250:
251:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>