File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.7: download - view: text, annotated - select for diffs
Fri Sep 22 10:28:41 2000 UTC (23 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Recognizes XML and forms (latter not tested)

    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 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_READER,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:                   my $nforms=0;
  130:                   
  131:                   my %ssibody=();
  132:                   my %ssibgcolor=();
  133:                   my %ssitext=();
  134:                   my %ssilink=();
  135:                   my %ssivlink=();
  136:                   my %ssialink=();
  137:                   my %cellemb=();
  138: 
  139:                   my $allscript='';
  140:                   my $allmeta='';
  141: 
  142:                   my $isxml=0;
  143:                   my $xmlheader='';
  144:                   my $xmlbody='';
  145: 
  146: # --------------------------------------------- Get SSI output, post parameters
  147: 
  148:                   for ($i=0;$i<=$#rows;$i++) {
  149: 		     if ($rows[$i]) {
  150: 		      $contents++;
  151:                       my @colcont=split(/\&/,$rows[$i]);
  152:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  153:                       map {
  154:                           my $src=$hash{'src_'.$_};
  155:                           $src=~/\.(\w+)$/;
  156:                           $cellemb{$_}=Apache::lonnet::fileembstyle($1);
  157:                           if ($cellemb{$_} eq 'ssi') {
  158: # --------------------------------------------------------- This is an SSI cell
  159: 			      my $prefix=$_.'_';
  160:                               my %posthash=('request.prefix' => $prefix);
  161:                               if (($ENV{'$form.'.$prefix.'submit'}) 
  162:                                || ($ENV{'form.all_submit'})) {
  163:                                map {
  164: 				  if ($_=~/^form.$prefix/) {
  165: 				      my $name=$_;
  166:                                       $name=~s/^form.$prefix//;
  167:                                       $posthash{$name}=$ENV{$_};
  168:                                   }
  169:                                } keys %ENV;
  170: 			      }
  171:                               my $output=Apache::lonnet::ssi($src,%posthash);
  172:                               my $parser=HTML::TokeParser->new(\$output);
  173:                               my $token;
  174:                               my $bodydef=0;
  175:                               my $thisxml=0;
  176:                               if ($output=~/\?xml/) {
  177:                                  $isxml=1;
  178:                                  $thisxml=1;
  179:                                  $output=~
  180:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  181:                                  $xmlheader=$1;
  182: 			      }
  183:                               while (($bodydef==0) &&
  184:                                      ($token=$parser->get_token)) {
  185: 				  if ($token->[1] eq 'body') {
  186: 				      $bodydef=1;
  187:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  188:                                       $ssitext{$_}=$token->[2]->{'text'};
  189:                                       $ssilink{$_}=$token->[2]->{'link'};
  190:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  191:                                       $ssialink{$_}=$token->[2]->{'alink'};
  192:                                       if ($thisxml) {
  193: 					  $xmlbody=$token->[4];
  194:                                       }
  195:                                   }
  196:                                   if ($token->[1] eq 'meta') {
  197: 				      $allmeta.="\n".$token->[4].'</meta>';
  198:                                   }
  199:                                   if ($token->[1] eq 'script') {
  200: 				      $allscript.="\n\n"
  201:                                                 .$parser->get_text('/script');
  202:                                   }
  203:                               }
  204:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  205:                                  $output=$1; 
  206:                               }
  207:                               $output=~s/\<\/body\>.*//si;
  208:                               if ($output=~/\<form/si) {
  209: 				  $nforms++;
  210:                                   $output=~s/\<form[^\>]*\>//gsi;
  211:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  212:                               }
  213: 			      $ssibody{$_}=$output;
  214: 
  215: # ---------------------------------------------------------------- End SSI cell
  216:                           }
  217:                       } @colcont;
  218:                      } 
  219:                   }
  220:                   unless ($contents) {
  221:                       $r->content_type('text/html');
  222:                       $r->send_http_header;
  223:                       $r->print('<html><body>Empty page.</body></html>');
  224:                   } else {
  225: # ------------------------------------------------------------------ Build page
  226: 
  227: # ---------------------------------------------------------------- Send headers
  228:                       if ($isxml) {
  229: 			  $r->content_type('text/xml');
  230:                           $r->send_http_header;
  231:                           $r->print($xmlheader);
  232: 		      } else {
  233:                           $r->content_type('text/html');
  234:                           $r->send_http_header;
  235:                           $r->print('<html>');
  236: 		      }
  237: # ------------------------------------------------------------------------ Head
  238:                       $r->print("\n<head>\n".$allmeta);
  239:                       if ($allscript) {
  240: 			  $r->print("\n<script>\n".$allscript."\n</script>\n");
  241:                       }
  242:                       $r->print("\n</head>\n");
  243: # ------------------------------------------------------------------ Start body
  244:                       if ($isxml) {
  245:                           $r->print($xmlbody);
  246:                       } else {
  247: 			  $r->print('<body bgcolor="#FFFFFF">');
  248:                       }
  249: 
  250: # ------------------------------------------------------------------ Start form
  251:                       if ($nforms) {
  252: 			  $r->print('<form method="post" action="'.
  253: 				    $requrl.'">');
  254:                       }
  255: # ----------------------------------------------------------------- Start table
  256:                       $r->print('<table cols="'.$lcm.'" border="0">');
  257:                       for ($i=0;$i<=$#rows;$i++) {
  258: 			if ($rows[$i]) {
  259:                           $r->print("\n<tr>");
  260:                           my @colcont=split(/\&/,$rows[$i]);
  261:                           my $avespan=$lcm/($#colcont+1);
  262:                           for ($j=0;$j<=$#colcont;$j++) {
  263:                               my $rid=$colcont[$j];
  264:                               $r->print('<td colspan="'.$avespan.'"');
  265:                               if ($cellemb{$rid} eq 'ssi') {
  266: 				  if ($ssibgcolor{$rid}) {
  267:                                      $r->print(' bgcolor="'.
  268:                                                $ssibgcolor{$rid}.'"');
  269:                                   }
  270:                                   $r->print('><font');
  271:                                   if ($ssitext{$rid}) {
  272: 				     $r->print(' text="'.$ssitext{$rid}.'"');
  273:                                   }
  274:                                   if ($ssilink{$rid}) {
  275: 				     $r->print(' link="'.$ssilink{$rid}.'"');
  276:                                   }
  277:                                   if ($ssitext{$rid}) {
  278: 				     $r->print(' vlink="'.$ssivlink{$rid}.'"');
  279:                                   }
  280:                                   if ($ssialink{$rid}) {
  281: 				     $r->print(' alink="'.$ssialink{$rid}.'"');
  282:                                   }
  283:                             
  284:                                   $r->print('>'.$ssibody{$rid}.'</font>');
  285:                               } elsif ($cellemb{$rid} eq 'img') {
  286:                                   $r->print('><img src="'.
  287:                                     $hash{'src_'.$rid}.'"></img>');
  288: 			      }
  289:                               $r->print('</td>');
  290:                           }
  291:                           $r->print('</tr>');
  292: 		        }
  293:                       }
  294:                       $r->print("\n</table>");
  295: # ---------------------------------------------------------------- Submit, etc.
  296:                       if ($nforms) {
  297:                           $r->print(
  298: 	                  '<input name="all_submit" value="Submit All" type="'.
  299: 			  (($nforms>1)?'submit':'hidden').'"></input></form>');
  300:                       }
  301:                       $r->print('</body></html>');
  302: # -------------------------------------------------------------------- End page
  303:                   }                  
  304: # ------------------------------------------------------------- End render page
  305:               } else {
  306:                   $r->content_type('text/html');
  307:                   $r->send_http_header;
  308: 		  $r->print('<html><body>Page undefined.</body></html>');
  309:               }
  310: # ------------------------------------------------------------------ Untie hash
  311:               unless (untie(%hash)) {
  312:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  313:                        "Could not untie coursemap $fn (browse).</font>"); 
  314:               }
  315: # -------------------------------------------------------------------- All done
  316: 	      return OK;
  317: # ----------------------------------------------- Errors, hash could no be tied
  318:           }
  319:       } 
  320:   }
  321:   $ENV{'user.error.msg'}="$requrl:bre:1:1:Course not initialized";
  322:   return HTTP_NOT_ACCEPTABLE; 
  323: }
  324: 
  325: 1;
  326: __END__
  327: 
  328: 
  329: 
  330: 
  331: 
  332: 
  333: 

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