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