File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.15: download - view: text, annotated - select for diffs
Tue Oct 31 19:31:42 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Clear out junk from session environment, and puts in http referers

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>