Annotation of rat/lonpage.pm, revision 1.14

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

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