Annotation of rat/lonpage.pm, revision 1.35
1.1 www 1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
1.35 ! www 4: # $Id: lonpage.pm,v 1.34 2002/03/26 16:14:51 www Exp $
1.29 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.1 www 28: # (TeX Content Handler
29: #
1.30 harris41 30: # YEAR=2000
1.1 www 31: # 05/29/00,05/30 Gerd Kortemeyer)
1.10 www 32: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
1.24 www 33: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16,
1.30 harris41 34: # YEAR=2001
1.27 www 35: # 08/13/01,08/30,10/1 Gerd Kortemeyer
1.30 harris41 36: # 12/16 Scott Harrison
1.33 www 37: # YEAR=2002
38: # 03/19 Gerd Kortemeyer
1.30 harris41 39: #
40: ###
1.1 www 41:
42: package Apache::lonpage;
43:
44: use strict;
45: use Apache::Constants qw(:common :http);
46: use Apache::lonnet();
1.30 harris41 47: use Apache::loncommon();
1.21 www 48: use Apache::lonxml();
1.6 www 49: use HTML::TokeParser;
1.1 www 50: use GDBM_File;
51:
1.2 www 52: # -------------------------------------------------------------- Module Globals
53: my %hash;
54: my @rows;
1.6 www 55:
56: # ------------------------------------------------------------------ Euclid gcd
57:
58: sub euclid {
59: my ($e,$f)=@_;
60: my $a; my $b; my $r;
61: if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
62: while ($r!=0) {
63: $a=$b; $b=$r;
64: $r=$a%$b;
65: }
66: return $b;
67: }
1.2 www 68:
69: # ------------------------------------------------------------ Build page table
70:
71: sub tracetable {
72: my ($sofar,$rid,$beenhere)=@_;
73: my $further=$sofar;
74: unless ($beenhere=~/\&$rid\&/) {
75: $beenhere.=$rid.'&';
76:
77: if (defined($hash{'is_map_'.$rid})) {
78: if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
79: (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
1.4 www 80: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
81: $sofar=
1.2 www 82: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
1.3 www 83: '&'.$frid.'&');
1.4 www 84: $sofar++;
85: if ($hash{'src_'.$frid}) {
1.3 www 86: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
87: if (($brepriv eq '2') || ($brepriv eq 'F')) {
88: if (defined($rows[$sofar])) {
89: $rows[$sofar].='&'.$frid;
90: } else {
91: $rows[$sofar]=$frid;
92: }
93: }
1.4 www 94: }
1.2 www 95: }
96: } else {
1.4 www 97: $sofar++;
98: if ($hash{'src_'.$rid}) {
1.3 www 99: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
100: if (($brepriv eq '2') || ($brepriv eq 'F')) {
101: if (defined($rows[$sofar])) {
1.2 www 102: $rows[$sofar].='&'.$rid;
1.3 www 103: } else {
1.2 www 104: $rows[$sofar]=$rid;
1.3 www 105: }
106: }
1.4 www 107: }
1.2 www 108: }
109:
110: if (defined($hash{'to_'.$rid})) {
1.11 www 111: my $mincond=1;
112: my $next='';
1.30 harris41 113: foreach (split(/\,/,$hash{'to_'.$rid})) {
1.11 www 114: my $thiscond=
115: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
116: if ($thiscond>=$mincond) {
117: if ($next) {
118: $next.=','.$_.':'.$thiscond;
119: } else {
120: $next=$_.':'.$thiscond;
121: }
122: if ($thiscond>$mincond) { $mincond=$thiscond; }
123: }
1.30 harris41 124: }
125: foreach (split(/\,/,$next)) {
1.11 www 126: my ($linkid,$condval)=split(/\:/,$_);
127: if ($condval>=$mincond) {
128: my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
129: if ($now>$further) { $further=$now; }
130: }
1.30 harris41 131: }
1.11 www 132:
1.2 www 133: }
134: }
135: return $further;
136: }
137:
1.1 www 138: # ================================================================ Main Handler
139:
140: sub handler {
141: my $r=shift;
142:
1.3 www 143: # ------------------------------------------- Set document type for header only
1.1 www 144:
1.3 www 145: if ($r->header_only) {
146: if ($ENV{'browser.mathml'}) {
147: $r->content_type('text/xml');
148: } else {
149: $r->content_type('text/html');
150: }
151: $r->send_http_header;
152: return OK;
153: }
1.1 www 154:
155: my $requrl=$r->uri;
156: # ----------------------------------------------------------------- Tie db file
157: if ($ENV{'request.course.fn'}) {
158: my $fn=$ENV{'request.course.fn'};
159: if (-e "$fn.db") {
1.7 www 160: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
1.1 www 161: # ------------------------------------------------------------------- Hash tied
162: my $firstres=$hash{'map_start_'.$requrl};
163: my $lastres=$hash{'map_finish_'.$requrl};
164: if (($firstres) && ($lastres)) {
165: # ----------------------------------------------------------------- Render page
166:
1.3 www 167: @rows=();
1.2 www 168:
169: &tracetable(0,$firstres,'&'.$lastres.'&');
1.4 www 170: if ($hash{'src_'.$lastres}) {
171: my $brepriv=
172: &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
173: if (($brepriv eq '2') || ($brepriv eq 'F')) {
174: $rows[$#rows+1]=''.$lastres;
175: }
176: }
1.2 www 177:
1.9 www 178: # ------------------------------------------------------------ Add to symb list
179:
1.2 www 180: my $i;
1.9 www 181: my %symbhash=();
182: for ($i=0;$i<=$#rows;$i++) {
183: if ($rows[$i]) {
184: my @colcont=split(/\&/,$rows[$i]);
1.30 harris41 185: foreach (@colcont) {
1.9 www 186: $symbhash{$hash{'src_'.$_}}='';
1.30 harris41 187: }
1.9 www 188: }
189: }
190: &Apache::lonnet::symblist($requrl,%symbhash);
191:
192: # ------------------------------------------------------------------ Page parms
193:
1.4 www 194: my $j;
1.6 www 195: my $lcm=1;
196: my $contents=0;
1.7 www 197: my $nforms=0;
1.6 www 198:
199: my %ssibody=();
200: my %ssibgcolor=();
201: my %ssitext=();
202: my %ssilink=();
203: my %ssivlink=();
204: my %ssialink=();
1.14 www 205:
206: my %metalink=();
207:
1.6 www 208: my %cellemb=();
1.3 www 209:
1.7 www 210: my $allscript='';
211: my $allmeta='';
212:
213: my $isxml=0;
214: my $xmlheader='';
215: my $xmlbody='';
216:
1.3 www 217: # --------------------------------------------- Get SSI output, post parameters
218:
1.2 www 219: for ($i=0;$i<=$#rows;$i++) {
1.4 www 220: if ($rows[$i]) {
1.6 www 221: $contents++;
1.3 www 222: my @colcont=split(/\&/,$rows[$i]);
1.6 www 223: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
1.30 harris41 224: foreach (@colcont) {
1.3 www 225: my $src=$hash{'src_'.$_};
1.5 www 226: $src=~/\.(\w+)$/;
1.14 www 227: $metalink{$_}=$src.'.meta';
1.30 harris41 228: $cellemb{$_}=Apache::loncommon::fileembstyle($1);
1.3 www 229: if ($cellemb{$_} eq 'ssi') {
230: # --------------------------------------------------------- This is an SSI cell
1.5 www 231: my $prefix=$_.'_';
232: my %posthash=('request.prefix' => $prefix);
1.8 www 233: if (($ENV{'form.'.$prefix.'submit'})
1.7 www 234: || ($ENV{'form.all_submit'})) {
1.30 harris41 235: foreach (keys %ENV) {
1.5 www 236: if ($_=~/^form.$prefix/) {
237: my $name=$_;
238: $name=~s/^form.$prefix//;
239: $posthash{$name}=$ENV{$_};
240: }
1.30 harris41 241: }
1.7 www 242: }
1.5 www 243: my $output=Apache::lonnet::ssi($src,%posthash);
1.6 www 244: my $parser=HTML::TokeParser->new(\$output);
245: my $token;
1.12 www 246: my $thisdir=$src;
1.6 www 247: my $bodydef=0;
1.7 www 248: my $thisxml=0;
1.12 www 249: my @rlinks=();
1.7 www 250: if ($output=~/\?xml/) {
251: $isxml=1;
252: $thisxml=1;
253: $output=~
254: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
255: $xmlheader=$1;
256: }
1.12 www 257: while ($token=$parser->get_token) {
258: if ($token->[0] eq 'S') {
259: if ($token->[1] eq 'a') {
260: if ($token->[2]->{'href'}) {
261: $rlinks[$#rlinks+1]=
262: $token->[2]->{'href'};
263: }
264: } elsif ($token->[1] eq 'img') {
265: $rlinks[$#rlinks+1]=
266: $token->[2]->{'src'};
267: } elsif ($token->[1] eq 'embed') {
268: $rlinks[$#rlinks+1]=
269: $token->[2]->{'src'};
270: } elsif ($token->[1] eq 'base') {
271: $thisdir=$token->[2]->{'href'};
272: } elsif ($token->[1] eq 'body') {
1.7 www 273: $bodydef=1;
274: $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
275: $ssitext{$_}=$token->[2]->{'text'};
276: $ssilink{$_}=$token->[2]->{'link'};
277: $ssivlink{$_}=$token->[2]->{'vlink'};
278: $ssialink{$_}=$token->[2]->{'alink'};
279: if ($thisxml) {
280: $xmlbody=$token->[4];
281: }
1.12 www 282: } elsif ($token->[1] eq 'meta') {
1.28 albertel 283: if ($token->[4] !~ m:/>$:) {
1.7 www 284: $allmeta.="\n".$token->[4].'</meta>';
1.28 albertel 285: } else {
286: $allmeta.="\n".$token->[4];
287: }
1.12 www 288: } elsif (($token->[1] eq 'script') &&
289: ($bodydef==0)) {
1.7 www 290: $allscript.="\n\n"
291: .$parser->get_text('/script');
1.6 www 292: }
1.12 www 293: }
294: }
1.6 www 295: if ($output=~/\<body[^\>]*\>(.*)/si) {
296: $output=$1;
297: }
298: $output=~s/\<\/body\>.*//si;
1.7 www 299: if ($output=~/\<form/si) {
300: $nforms++;
301: $output=~s/\<form[^\>]*\>//gsi;
302: $output=~s/\<\/form[^\>]*\>//gsi;
1.17 www 303: $output=~
1.18 www 304: s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
1.7 www 305: }
1.12 www 306: $thisdir=~s/\/[^\/]*$//;
1.30 harris41 307: foreach (@rlinks) {
1.12 www 308: unless (($_=~/^http:\/\//i) ||
1.31 albertel 309: ($_=~/^\//) ||
310: ($_=~/^javascript:/i) ||
311: ($_=~/^mailto:/i) ||
312: ($_=~/^\#/)) {
1.12 www 313: my $newlocation=
314: &Apache::lonnet::hreflocation($thisdir,$_);
315: $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
316: }
1.30 harris41 317: }
1.24 www 318: # -------------------------------------------------- Deal with Applet codebases
319: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
1.5 www 320: $ssibody{$_}=$output;
1.3 www 321: # ---------------------------------------------------------------- End SSI cell
322: }
1.30 harris41 323: }
1.4 www 324: }
1.2 www 325: }
1.6 www 326: unless ($contents) {
1.3 www 327: $r->content_type('text/html');
328: $r->send_http_header;
329: $r->print('<html><body>Empty page.</body></html>');
330: } else {
331: # ------------------------------------------------------------------ Build page
1.7 www 332:
333: # ---------------------------------------------------------------- Send headers
334: if ($isxml) {
335: $r->content_type('text/xml');
336: $r->send_http_header;
337: $r->print($xmlheader);
338: } else {
339: $r->content_type('text/html');
340: $r->send_http_header;
341: $r->print('<html>');
342: }
343: # ------------------------------------------------------------------------ Head
344: $r->print("\n<head>\n".$allmeta);
1.21 www 345: $allscript=~
346: s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
1.7 www 347: if ($allscript) {
1.21 www 348: $r->print("\n<script language='JavaScript'>\n".
349: $allscript."\n</script>\n");
1.7 www 350: }
1.32 matthew 351: $r->print(&Apache::lonxml::registerurl(1,undef));
1.7 www 352: $r->print("\n</head>\n");
353: # ------------------------------------------------------------------ Start body
354: if ($isxml) {
355: $r->print($xmlbody);
356: } else {
1.21 www 357: $r->print(
358: '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
359: '" onUnload="'.&Apache::lonxml::unloadevents.'">');
1.7 www 360: }
361: # ------------------------------------------------------------------ Start form
362: if ($nforms) {
363: $r->print('<form method="post" action="'.
364: $requrl.'">');
365: }
366: # ----------------------------------------------------------------- Start table
367: $r->print('<table cols="'.$lcm.'" border="0">');
1.5 www 368: for ($i=0;$i<=$#rows;$i++) {
369: if ($rows[$i]) {
1.4 www 370: $r->print("\n<tr>");
371: my @colcont=split(/\&/,$rows[$i]);
1.6 www 372: my $avespan=$lcm/($#colcont+1);
373: for ($j=0;$j<=$#colcont;$j++) {
374: my $rid=$colcont[$j];
1.23 www 375: my $metainfo='<a href="'.
376: $metalink{$rid}.'" target="LONcatInfo">'.
377: '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
1.34 www 378: '</img></a><a href="/adm/evaluate?postdata='.
1.35 ! www 379: &Apache::lonnet::escape(&Apache::lonnet::declutter($hash{'src_'.$rid}))
! 380: .'" target="LONcatInfo">'.
1.33 www 381: '<img src="/adm/lonMisc/eval_button.gif" border=0>'.
382: '</img></a>';
1.27 www 383: if (
384: ($hash{'src_'.$rid}=~/\.(problem|exam|quiz|assess|survey|form)$/) &&
385: (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}))) {
386: my ($mapid,$resid)=split(/\./,$rid);
387: my $symb=
388: &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
389: '___'.$resid.'___'.
390: &Apache::lonnet::declutter($hash{'src_'.$rid});
391: $metainfo.=
392: '<a href="/adm/grades?symb='.$symb.
393: '&command=submission" target="LONcatInfo">'.
394: '<img src="/adm/lonMisc/subm_button.gif" border=0>'.
395: '</img></a>'.
396: '<a href="/adm/grades?symb='.$symb.
397: '&command=viewgrades" target="LONcatInfo">'.
398: '<img src="/adm/lonMisc/pgrd_button.gif" border=0>'.
399: '</img></a>'.
400: '<a href="/adm/parmset?symb='.$symb.'" target="LONcatInfo">'.
401: '<img src="/adm/lonMisc/pprm_button.gif" border=0>'.
402: '</img></a>';
403: }
404: $metainfo.='<br></br>';
1.6 www 405: $r->print('<td colspan="'.$avespan.'"');
406: if ($cellemb{$rid} eq 'ssi') {
1.7 www 407: if ($ssibgcolor{$rid}) {
408: $r->print(' bgcolor="'.
409: $ssibgcolor{$rid}.'"');
410: }
1.14 www 411: $r->print('>'.$metainfo.'<font');
1.7 www 412: if ($ssitext{$rid}) {
413: $r->print(' text="'.$ssitext{$rid}.'"');
414: }
415: if ($ssilink{$rid}) {
416: $r->print(' link="'.$ssilink{$rid}.'"');
417: }
418: if ($ssitext{$rid}) {
419: $r->print(' vlink="'.$ssivlink{$rid}.'"');
420: }
421: if ($ssialink{$rid}) {
422: $r->print(' alink="'.$ssialink{$rid}.'"');
423: }
424:
425: $r->print('>'.$ssibody{$rid}.'</font>');
1.6 www 426: } elsif ($cellemb{$rid} eq 'img') {
1.14 www 427: $r->print('>'.$metainfo.'<img src="'.
1.7 www 428: $hash{'src_'.$rid}.'"></img>');
1.13 www 429: } elsif ($cellemb{$rid} eq 'emb') {
1.14 www 430: $r->print('>'.$metainfo.'<embed src="'.
1.13 www 431: $hash{'src_'.$rid}.'"></embed>');
432: }
1.6 www 433: $r->print('</td>');
1.4 www 434: }
435: $r->print('</tr>');
1.5 www 436: }
1.4 www 437: }
438: $r->print("\n</table>");
1.7 www 439: # ---------------------------------------------------------------- Submit, etc.
440: if ($nforms) {
441: $r->print(
442: '<input name="all_submit" value="Submit All" type="'.
443: (($nforms>1)?'submit':'hidden').'"></input></form>');
444: }
1.25 www 445: $r->print('</body>'.&Apache::lonxml::xmlend());
1.3 www 446: # -------------------------------------------------------------------- End page
447: }
1.1 www 448: # ------------------------------------------------------------- End render page
449: } else {
1.3 www 450: $r->content_type('text/html');
451: $r->send_http_header;
452: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 453: }
454: # ------------------------------------------------------------------ Untie hash
455: unless (untie(%hash)) {
456: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
457: "Could not untie coursemap $fn (browse).</font>");
458: }
459: # -------------------------------------------------------------------- All done
460: return OK;
461: # ----------------------------------------------- Errors, hash could no be tied
462: }
463: }
464: }
1.10 www 465: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
1.1 www 466: return HTTP_NOT_ACCEPTABLE;
467: }
468:
469: 1;
470: __END__
471:
1.30 harris41 472: =head1 NAME
473:
474: Apache::lonpage - Page Handler
475:
476: =head1 SYNOPSIS
477:
478: Invoked by /etc/httpd/conf/srm.conf:
479:
480: <LocationMatch "^/res/.*\.page$>
481: SetHandler perl-script
482: PerlHandler Apache::lonpage
483: </LocationMatch>
484:
485: =head1 INTRODUCTION
486:
487: This module renders a .page resource.
488:
489: This is part of the LearningOnline Network with CAPA project
490: described at http://www.lon-capa.org.
491:
492: =head1 HANDLER SUBROUTINE
493:
494: This routine is called by Apache and mod_perl.
495:
496: =over 4
497:
498: =item *
499:
500: set document type for header only
501:
502: =item *
503:
504: tie db file
505:
506: =item *
507:
508: render page
509:
510: =item *
511:
512: add to symb list
513:
514: =item *
515:
516: page parms
517:
518: =item *
519:
520: Get SSI output, post parameters
521:
522: =item *
523:
524: SSI cell rendering
525:
526: =item *
527:
528: Deal with Applet codebases
529:
530: =item *
531:
532: Build page
533:
534: =item *
535:
536: send headers
537:
538: =item *
539:
540: start body
541:
542: =item *
543:
544: start form
545:
546: =item *
547:
548: start table
549:
550: =item *
551:
552: submit element, etc, render page, untie hash
553:
554: =back
555:
556: =head1 OTHER SUBROUTINES
557:
558: =over 4
559:
560: =item *
561:
562: euclid() : Euclid's method for determining the greatest common denominator.
563:
564: =item *
565:
566: tracetable() : Build page table.
1.1 www 567:
1.30 harris41 568: =back
1.1 www 569:
1.30 harris41 570: =cut
1.1 www 571:
572:
573:
574:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>