Annotation of loncom/xml/lonxml.pm, revision 1.86

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.3       sakharuk    4: # last modified 06/26/00 by Alexander Sakharuk
1.33      www         5: # 11/6 Gerd Kortemeyer
1.45      www         6: # 6/1/1 Gerd Kortemeyer
1.56      albertel    7: # 2/21,3/13 Guy
1.68      www         8: # 3/29,5/4 Gerd Kortemeyer
1.73      harris41    9: # 5/10 Scott Harrison
1.78      www        10: # 5/26 Gerd Kortemeyer
1.80      harris41   11: # 5/27 H. K. Ng
1.86    ! www        12: # 6/2,6/3 Gerd Kortemeyer
1.2       sakharuk   13: 
1.4       albertel   14: package Apache::lonxml; 
1.33      www        15: use vars 
1.76      albertel   16: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
1.1       sakharuk   17: use strict;
                     18: use HTML::TokeParser;
1.3       sakharuk   19: use Safe;
1.40      albertel   20: use Safe::Hole;
1.81      ng         21: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
1.13      albertel   22: use Opcode;
1.72      albertel   23: 
                     24: sub register {
                     25:   my $space;
                     26:   my @taglist;
                     27:   my $temptag;
                     28:   ($space,@taglist) = @_;
                     29:   foreach $temptag (@taglist) {
                     30:     $Apache::lonxml::alltags{$temptag}=$space;
                     31:   }
                     32: }
                     33: 
1.46      www        34: use Apache::Constants qw(:common);
1.71      www        35: use Apache::lontexconvert;
1.72      albertel   36: use Apache::style;
                     37: use Apache::run;
                     38: use Apache::londefdef;
                     39: use Apache::scripttag;
                     40: use Apache::edit;
1.79      www        41: use Apache::lonnet;
                     42: use Apache::File;
                     43: 
1.72      albertel   44: #==================================================   Main subroutine: xmlparse  
                     45: #debugging control, to turn on debugging modify the correct handler
                     46: $Apache::lonxml::debug=0;
                     47: 
                     48: #path to the directory containing the file currently being processed
                     49: @pwd=();
                     50: 
                     51: #these two are used for capturing a subset of the output for later processing,
                     52: #don't touch them directly use &startredirection and &endredirection
                     53: @outputstack = ();
                     54: $redirection = 0;
                     55: 
                     56: #controls wheter the <import> tag actually does
                     57: $import = 1;
                     58: @extlinks=();
                     59: 
                     60: # meta mode is a bit weird only some output is to be turned off
                     61: #<output> tag turns metamode off (defined in londefdef.pm)
                     62: $metamode = 0;
                     63: 
                     64: # turns on and of run::evaluate actually derefencing var refs
                     65: $evaluate = 1;
1.7       albertel   66: 
1.74      albertel   67: # data structure for eidt mode, determines what tags can go into what other tags
                     68: %insertlist=();
1.68      www        69: 
1.76      albertel   70: #stores the list of active tag namespaces
                     71: @namespace=();
                     72: 
1.68      www        73: sub xmlbegin {
                     74:   my $output='';
                     75:   if ($ENV{'browser.mathml'}) {
                     76:       $output='<?xml version="1.0"?>'
                     77:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
                     78:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                     79:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
                     80:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                     81: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
                     82:   } else {
                     83:       $output='<html>';
                     84:   }
                     85:   return $output;
                     86: }
                     87: 
                     88: sub xmlend {
                     89:     return '</html>';
                     90: }
                     91: 
1.70      www        92: sub fontsettings() {
                     93:     my $headerstring='';
                     94:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
                     95:          $headerstring.=
                     96:              '<meta Content-Type="text/html; charset=x-mac-roman">';
                     97:     }
                     98:     return $headerstring;
                     99: }
                    100: 
1.68      www       101: sub registerurl {
1.86    ! www       102:     if ($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) {
        !           103: 	return (<<ENDREGTHIS);
        !           104: 
1.68      www       105: <script language="JavaScript">
1.71      www       106: // BEGIN LON-CAPA Internal
1.86    ! www       107: 
1.69      www       108:     function LONCAPAreg() {
                    109: 	  menu=window.open("","LONCAPAmenu");
1.86    ! www       110:           menu.clearTimeout(menu.menucltim);
1.69      www       111: 	  menu.currentURL=window.location.pathname;
                    112:           menu.currentStale=0;
1.85      www       113:           menu.clearbut(3,1);
                    114:           menu.switchbutton
                    115:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
                    116:           menu.switchbutton
                    117:     (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
                    118:           menu.switchbutton
                    119:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
                    120:           menu.switchbutton
                    121:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
                    122:           menu.switchbutton
                    123:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
1.69      www       124:     }
1.86    ! www       125: 
1.69      www       126:     function LONCAPAstale() {
1.86    ! www       127: 	  menu=window.open("","LONCAPAmenu");
        !           128:           menu.currentStale=1;
        !           129:           menu.switchbutton
        !           130:             (3,1,'reload.gif','return','location','go(currentURL)');
        !           131:           menu.menucltim=menu.setTimeout(
        !           132:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);',
        !           133: 			  2000);
        !           134: 
        !           135:     }
        !           136: 
        !           137: // END LON-CAPA Internal
        !           138: </script>
        !           139: ENDREGTHIS
        !           140: 
        !           141:     } else {
        !           142:         return (<<ENDDONOTREGTHIS);
        !           143: 
        !           144: <script language="JavaScript">
        !           145: // BEGIN LON-CAPA Internal
        !           146: 
        !           147:     function LONCAPAreg() {
1.69      www       148: 	  menu=window.open("","LONCAPAmenu");
                    149:           menu.currentStale=1;
1.85      www       150:           menu.clearbut(2,1);
                    151:           menu.clearbut(2,3);
                    152:           menu.clearbut(8,1);
                    153:           menu.clearbut(8,2);
                    154:           menu.clearbut(8,3);
1.86    ! www       155:           if (menu.currentURL) {
        !           156:              menu.switchbutton
        !           157:               (3,1,'reload.gif','return','location','go(currentURL)');
        !           158:  	  } else {
        !           159: 	      menu.clearbut(3,1);
        !           160:           }
        !           161:     }
        !           162: 
        !           163:     function LONCAPAstale() {
1.68      www       164:     }
1.86    ! www       165: 
1.71      www       166: // END LON-CAPA Internal
1.68      www       167: </script>
1.86    ! www       168: ENDDONOTREGTHIS
        !           169: 
        !           170:     }
1.69      www       171: }
                    172: 
                    173: sub loadevents() {
                    174:     return 'LONCAPAreg();';
                    175: }
                    176: 
                    177: sub unloadevents() {
                    178:     return 'LONCAPAstale();';
1.68      www       179: }
                    180: 
1.48      albertel  181: sub printalltags {
                    182:   my $temp;
                    183:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.64      albertel  184:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
1.48      albertel  185:   }
                    186: }
1.31      sakharuk  187: 
1.3       sakharuk  188: sub xmlparse {
                    189: 
1.18      albertel  190:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.41      albertel  191:  if ($target eq 'meta') {
1.59      albertel  192:    $Apache::lonxml::redirection = 0;
                    193:    $Apache::lonxml::metamode = 1;
1.72      albertel  194:    $Apache::lonxml::evaluate = 1;
1.55      albertel  195:    $Apache::lonxml::import = 0;
1.47      albertel  196:  } elsif ($target eq 'grade') {
1.55      albertel  197:    &startredirection;
1.59      albertel  198:    $Apache::lonxml::metamode = 0;
1.72      albertel  199:    $Apache::lonxml::evaluate = 1;
1.55      albertel  200:    $Apache::lonxml::import = 1;
1.72      albertel  201:  } elsif ($target eq 'modified') {
                    202:    $Apache::lonxml::redirection = 0;
                    203:    $Apache::lonxml::metamode = 0;
                    204:    $Apache::lonxml::evaluate = 0;
                    205:    $Apache::lonxml::import = 0;
1.44      albertel  206:  } else {
1.72      albertel  207:    $Apache::lonxml::redirection = 0;
1.59      albertel  208:    $Apache::lonxml::metamode = 0;
1.72      albertel  209:    $Apache::lonxml::evaluate = 1;
1.55      albertel  210:    $Apache::lonxml::import = 1;
1.32      sakharuk  211:  }
1.48      albertel  212:  #&printalltags();
1.16      albertel  213:  my @pars = ();
1.23      albertel  214:  @Apache::lonxml::pwd=();
                    215:  my $pwd=$ENV{'request.filename'};
                    216:  $pwd =~ s:/[^/]*$::;
                    217:  &newparser(\@pars,\$content_file_string,$pwd);
1.3       sakharuk  218:  my $currentstring = '';
                    219:  my $finaloutput = ''; 
                    220:  my $newarg = '';
1.16      albertel  221:  my $result;
1.24      sakharuk  222: 
1.3       sakharuk  223:  my $safeeval = new Safe;
1.40      albertel  224:  my $safehole = new Safe::Hole;
1.82      ng        225:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  226: #-------------------- Redefinition of the target in the case of compound target
                    227: 
                    228:  ($target, my @tenta) = split('&&',$target);
                    229: 
                    230:  my @stack = (); 
                    231:  my @parstack = ();
1.17      albertel  232:  &initdepth;
1.3       sakharuk  233:  my $token;
1.16      albertel  234:  while ( $#pars > -1 ) {
                    235:    while ($token = $pars[$#pars]->get_token) {
1.57      albertel  236:      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.61      albertel  237:        if ($metamode<1) { $result=$token->[1]; }
1.57      albertel  238:      } elsif ($token->[0] eq 'PI') {
1.61      albertel  239:        if ($metamode<1) { $result=$token->[2]; }
1.16      albertel  240:      } elsif ($token->[0] eq 'S') {
                    241:        # add tag to stack 	    
                    242:        push (@stack,$token->[1]);
                    243:        # add parameters list to another stack
                    244:        push (@parstack,&parstring($token));
1.19      albertel  245:        &increasedepth($token);       
1.16      albertel  246:        if (exists $style_for_target{$token->[1]}) {
1.61      albertel  247: 	 if ($Apache::lonxml::redirection) {
1.55      albertel  248: 	   $Apache::lonxml::outputstack['-1'] .=  
                    249: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
                    250: 		      \%style_for_target,@parstack);
1.41      albertel  251: 	 } else {
1.55      albertel  252: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
                    253: 				    $safeeval,\%style_for_target,@parstack);
1.41      albertel  254: 	 }
1.16      albertel  255:        } else {
1.84      albertel  256: 	 $result = &callsub("start_$token->[1]", $target, $token, \@stack,
                    257: 			    \@parstack, \@pars, $safeeval, \%style_for_target);
1.16      albertel  258:        }              
                    259:      } elsif ($token->[0] eq 'E')  {
                    260:        #clear out any tags that didn't end
1.55      albertel  261:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
                    262: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
1.43      albertel  263: 	 pop @stack;pop @parstack;&decreasedepth($token);
                    264:        }
1.16      albertel  265:        
                    266:        if (exists $style_for_target{'/'."$token->[1]"}) {
1.61      albertel  267: 	 if ($Apache::lonxml::redirection) {
1.55      albertel  268: 	   $Apache::lonxml::outputstack['-1'] .=  
                    269: 	     &recurse($style_for_target{'/'."$token->[1]"},
                    270: 		      $target,$safeeval,\%style_for_target,@parstack);
                    271: 	 } else {
                    272: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
                    273: 				    $target,$safeeval,\%style_for_target,
                    274: 				    @parstack);
                    275: 	 }
1.59      albertel  276: 
1.16      albertel  277:        } else {
1.84      albertel  278: 	 $result = &callsub("end_$token->[1]", $target, $token, \@stack, 
                    279: 			    \@parstack, \@pars,$safeeval, \%style_for_target);
1.13      albertel  280:        }
1.57      albertel  281:      } else {
                    282:        &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
1.16      albertel  283:      }
1.55      albertel  284:      #evaluate variable refs in result
1.25      sakharuk  285:      if ($result ne "") {
1.24      sakharuk  286:        if ( $#parstack > -1 ) {
1.55      albertel  287: 	 if ($Apache::lonxml::redirection) {
                    288: 	   $Apache::lonxml::outputstack['-1'] .= 
                    289: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
                    290: 	 } else {
                    291: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
                    292: 						  $parstack[$#parstack]);
                    293: 	 }
1.16      albertel  294:        } else {
                    295: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13      albertel  296:        }
1.16      albertel  297:        $result = '';
1.55      albertel  298:      } 
                    299:      if ($token->[0] eq 'E') { 
                    300:        pop @stack;pop @parstack;&decreasedepth($token);
1.2       sakharuk  301:      }
1.5       albertel  302:    }
1.16      albertel  303:    pop @pars;
1.23      albertel  304:    pop @Apache::lonxml::pwd;
1.3       sakharuk  305:  }
1.24      sakharuk  306: 
1.59      albertel  307: # if ($target eq 'meta') {
                    308: #   $finaloutput.=&endredirection;
                    309: # }
1.67      www       310: 
                    311:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    312:       $finaloutput=&afterburn($finaloutput);
                    313:   }
                    314: 
1.3       sakharuk  315:  return $finaloutput;
1.15      albertel  316: }
                    317: 
1.67      www       318: 
1.15      albertel  319: sub recurse {
                    320:   
                    321:   my @innerstack = (); 
                    322:   my @innerparstack = ();
                    323:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  324:   my @pat = ();
1.23      albertel  325:   &newparser(\@pat,\$newarg);
1.15      albertel  326:   my $tokenpat;
                    327:   my $partstring = '';
                    328:   my $output='';
1.16      albertel  329:   my $decls='';
                    330:   while ( $#pat > -1 ) {
                    331:     while  ($tokenpat = $pat[$#pat]->get_token) {
1.57      albertel  332:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61      albertel  333: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57      albertel  334:       } elsif ($tokenpat->[0] eq 'PI') {
1.61      albertel  335: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16      albertel  336:       } elsif ($tokenpat->[0] eq 'S') {
                    337: 	push (@innerstack,$tokenpat->[1]);
                    338: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  339: 	&increasedepth($tokenpat);
1.84      albertel  340: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
                    341: 			       \@innerstack, \@innerparstack, \@pat,
                    342: 			       $safeeval, $style_for_target);
1.16      albertel  343:       } elsif ($tokenpat->[0] eq 'E') {
                    344: 	#clear out any tags that didn't end
                    345: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
1.43      albertel  346: 	       && ($#innerstack > -1)) {
1.49      albertel  347: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
1.43      albertel  348: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
                    349: 	}
1.84      albertel  350: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
                    351: 			       \@innerstack, \@innerparstack, \@pat,
                    352: 			       $safeeval, $style_for_target);
1.57      albertel  353:       } else {
                    354: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16      albertel  355:       }
                    356:       #pass both the variable to the style tag, and the tag we 
                    357:       #are processing inside the <definedtag>
                    358:       if ( $partstring ne "" ) {
                    359: 	if ( $#parstack > -1 ) { 
                    360: 	  if ( $#innerparstack > -1 ) { 
                    361: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    362: 	  } else {
                    363: 	    $decls= $parstack[$#parstack];
                    364: 	  }
                    365: 	} else {
                    366: 	  if ( $#innerparstack > -1 ) { 
                    367: 	    $decls=$innerparstack[$#innerparstack];
                    368: 	  } else {
                    369: 	    $decls='';
                    370: 	  }
                    371: 	}
                    372: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    373: 	$partstring = '';
                    374:       }
1.17      albertel  375:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  376: 				 &decreasedepth($tokenpat);}
1.15      albertel  377:     }
1.16      albertel  378:     pop @pat;
1.23      albertel  379:     pop @Apache::lonxml::pwd;
1.15      albertel  380:   }
                    381:   return $output;
1.7       albertel  382: }
                    383: 
                    384: sub callsub {
1.84      albertel  385:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  386:   my $currentstring='';
1.72      albertel  387:   my $nodefault;
1.7       albertel  388:   {
1.59      albertel  389:     my $sub1;
1.7       albertel  390:     no strict 'refs';
1.59      albertel  391:     if ($target eq 'edit' && $token->[0] eq 'S') {
1.84      albertel  392:       $currentstring = &Apache::edit::tag_start($target,$token,$tagstack,
                    393: 						$parstack,$parser,
1.59      albertel  394: 						$safeeval,$style);
                    395:     }
1.68      www       396:     my $tag=$token->[1];
                    397:     my $space=$Apache::lonxml::alltags{$tag};
                    398:     if (!$space) {
                    399: 	$tag=~tr/A-Z/a-z/;
                    400: 	$sub=~tr/A-Z/a-z/;
                    401: 	$space=$Apache::lonxml::alltags{$tag}
                    402:     }
                    403:     if ($space) {
1.72      albertel  404:       #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
1.24      sakharuk  405:       $sub1="$space\:\:$sub";
1.17      albertel  406:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.84      albertel  407:       ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    408: 					   $parstack,$parser,$safeeval,
                    409: 					   $style);
1.7       albertel  410:     } else {
1.72      albertel  411:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
1.62      sakharuk  412:       if ($metamode <1) {
                    413: 	if (defined($token->[4]) && ($metamode < 1)) {
1.72      albertel  414: 	  $currentstring = $token->[4];
1.62      sakharuk  415: 	} else {
1.72      albertel  416: 	  $currentstring = $token->[2];
1.62      sakharuk  417: 	}
1.7       albertel  418:       }
1.59      albertel  419:     }
1.83      albertel  420: #    &Apache::lonxml::debug("nodefalt:$nodefault:");
1.72      albertel  421:     if ($currentstring eq '' && $nodefault eq '') {
                    422:       if ($target eq 'edit') {
1.74      albertel  423: 	&Apache::lonxml::debug("doing default edit for $token->[1]");
1.72      albertel  424: 	if ($token->[0] eq 'S') {
1.74      albertel  425: 	  $currentstring = &Apache::edit::tag_start($target,$token);
1.72      albertel  426: 	} elsif ($token->[0] eq 'E') {
1.74      albertel  427: 	  $currentstring = &Apache::edit::tag_end($target,$token);
1.72      albertel  428: 	}
                    429:       } elsif ($target eq 'modified') {
                    430: 	if ($token->[0] eq 'S') {
                    431: 	  $currentstring = $token->[4];
1.76      albertel  432: 	  $currentstring.=&Apache::edit::handle_insert();
1.72      albertel  433: 	} else {
                    434: 	  $currentstring = $token->[2];
                    435: 	}
                    436:       }
1.7       albertel  437:     }
                    438:     use strict 'refs';
                    439:   }
                    440:   return $currentstring;
1.82      ng        441: }
                    442: 
                    443: sub init_safespace {
                    444:   my ($target,$safeeval,$safehole,$safeinit) = @_;
                    445:   $safeeval->permit("entereval");
                    446:   $safeeval->permit(":base_math");
                    447:   $safeeval->permit("sort");
                    448:   $safeeval->deny(":base_io");
                    449:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
                    450:   
                    451:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    452:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    453:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    454:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    455:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    456:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    457:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    458:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    459:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    460:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    461:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    462:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    463:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    464:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    465:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    466:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    467:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    468:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    469:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
                    470:   
                    471: #need to inspect this class of ops
                    472: # $safeeval->deny(":base_orig");
                    473:   $safeinit .= ';$external::target='.$target.';';
                    474:   $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
                    475:   &Apache::run::run($safeinit,$safeeval);
1.17      albertel  476: }
                    477: 
1.55      albertel  478: sub startredirection {
                    479:   $Apache::lonxml::redirection++;
                    480:   push (@Apache::lonxml::outputstack, '');
                    481: }
                    482: 
                    483: sub endredirection {
                    484:   if (!$Apache::lonxml::redirection) {
1.72      albertel  485:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55      albertel  486:     return '';
                    487:   }
                    488:   $Apache::lonxml::redirection--;
                    489:   pop @Apache::lonxml::outputstack;
                    490: }
                    491: 
1.17      albertel  492: sub initdepth {
                    493:   @Apache::lonxml::depthcounter=();
                    494:   $Apache::lonxml::depth=-1;
                    495:   $Apache::lonxml::olddepth=-1;
                    496: }
                    497: 
                    498: sub increasedepth {
1.19      albertel  499:   my ($token) = @_;
1.17      albertel  500:   $Apache::lonxml::depth++;
                    501:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    502:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    503:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    504:   }
1.42      albertel  505:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  506:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54      albertel  507: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  508: }
                    509: 
                    510: sub decreasedepth {
1.19      albertel  511:   my ($token) = @_;
1.17      albertel  512:   $Apache::lonxml::depth--;
1.36      albertel  513:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    514:     $#Apache::lonxml::depthcounter--;
                    515:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    516:   }
1.43      albertel  517:   if (  $Apache::lonxml::depth < -1) {
1.49      albertel  518:     &Apache::lonxml::warning("Unbalanced tags in resource");   
1.43      albertel  519:     $Apache::lonxml::depth='-1';
                    520:   }
1.42      albertel  521:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  522:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54      albertel  523: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  524: }
1.19      albertel  525: 
                    526: sub get_all_text {
                    527: 
                    528:  my($tag,$pars)= @_;
                    529:  my $depth=0;
                    530:  my $token;
                    531:  my $result='';
1.57      albertel  532:  if ( $tag =~ m:^/: ) { 
                    533:    my $tag=substr($tag,1); 
                    534: #   &Apache::lonxml::debug("have:$tag:");
                    535:    while (($depth >=0) && ($token = $pars->get_token)) {
                    536: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
                    537:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    538:        $result.=$token->[1];
                    539:      } elsif ($token->[0] eq 'PI') {
                    540:        $result.=$token->[2];
                    541:      } elsif ($token->[0] eq 'S') {
                    542:        if ($token->[1] eq $tag) { $depth++; }
                    543:        $result.=$token->[4];
                    544:      } elsif ($token->[0] eq 'E')  {
                    545:        if ( $token->[1] eq $tag) { $depth--; }
                    546:        #skip sending back the last end tag
                    547:        if ($depth > -1) { $result.=$token->[2]; } else {
                    548: 	 $pars->unget_token($token);
                    549:        }
                    550:      }
                    551:    }
                    552:  } else {
                    553:    while ($token = $pars->get_token) {
                    554: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                    555:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    556:        $result.=$token->[1];
                    557:      } elsif ($token->[0] eq 'PI') {
                    558:        $result.=$token->[2];
                    559:      } elsif ($token->[0] eq 'S') {
                    560:        if ( $token->[1] eq $tag) { 
                    561: 	 $pars->unget_token($token); last;
                    562:        } else {
                    563: 	 $result.=$token->[4];
                    564:        }
                    565:      } elsif ($token->[0] eq 'E')  {
                    566:        $result.=$token->[2];
1.36      albertel  567:      }
1.19      albertel  568:    }
                    569:  }
1.49      albertel  570: # &Apache::lonxml::debug("Exit:$result:");
1.19      albertel  571:  return $result
                    572: }
                    573: 
1.23      albertel  574: sub newparser {
                    575:   my ($parser,$contentref,$dir) = @_;
                    576:   push (@$parser,HTML::TokeParser->new($contentref));
1.56      albertel  577:   $$parser['-1']->xml_mode('1');
1.23      albertel  578:   if ( $dir eq '' ) {
                    579:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                    580:   } else {
                    581:     push (@Apache::lonxml::pwd, $dir);
                    582:   } 
                    583: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                    584: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                    585: }
1.1       sakharuk  586: 
1.8       albertel  587: sub parstring {
                    588:   my ($token) = @_;
                    589:   my $temp='';
1.20      albertel  590:   map {
1.35      www       591:     unless ($_=~/\W/) {
1.42      albertel  592:       my $val=$token->[2]->{$_};
1.53      albertel  593:       $val =~ s/([\%\@\\])/\\$1/g;
1.51      albertel  594:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42      albertel  595:       $temp .= "my \$$_=\"$val\";"
1.20      albertel  596:     }
                    597:   } @{$token->[3]};
1.8       albertel  598:   return $temp;
                    599: }
1.22      albertel  600: 
1.34      www       601: sub writeallows {
                    602:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
                    603:     my $thisdir=$thisurl;
                    604:     $thisdir=~s/\/[^\/]+$//;
                    605:     my %httpref=();
                    606:     map {
                    607:        $httpref{'httpref.'.
                    608:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
                    609:     &Apache::lonnet::appenv(%httpref);
                    610: }
                    611: 
1.66      www       612: #
                    613: # Afterburner handles anchors, highlights and links
                    614: #
                    615: sub afterburn {
                    616:     my $result=shift;
                    617:     map {
                    618:        my ($name, $value) = split(/=/,$_);
                    619:        $value =~ tr/+/ /;
                    620:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    621:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
                    622:            unless ($ENV{'form.'.$name}) {
                    623:               $ENV{'form.'.$name}=$value;
                    624: 	   }
                    625:        }
                    626:     } (split(/&/,$ENV{'QUERY_STRING'}));
                    627:     if ($ENV{'form.highlight'}) {
                    628:         map {
                    629:            my $anchorname=$_;
                    630: 	   my $matchthis=$anchorname;
                    631:            $matchthis=~s/\_+/\\s\+/g;
                    632:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
                    633:        } split(/\,/,$ENV{'form.highlight'});
                    634:     }
                    635:     if ($ENV{'form.link'}) {
                    636:         map {
                    637:            my ($anchorname,$linkurl)=split(/\>/,$_);
                    638: 	   my $matchthis=$anchorname;
                    639:            $matchthis=~s/\_+/\\s\+/g;
                    640:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
                    641:        } split(/\,/,$ENV{'form.link'});
                    642:     }
                    643:     if ($ENV{'form.anchor'}) {
                    644:         my $anchorname=$ENV{'form.anchor'};
                    645: 	my $matchthis=$anchorname;
                    646:         $matchthis=~s/\_+/\\s\+/g;
                    647:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
                    648:         $result.=(<<"ENDSCRIPT");
                    649: <script>
                    650:     document.location.hash='$anchorname';
                    651: </script>
                    652: ENDSCRIPT
                    653:     }
                    654:     return $result;
                    655: }
                    656: 
1.79      www       657: sub storefile {
                    658:     my ($file,$contents)=@_;
                    659:     if (my $fh=Apache::File->new('>'.$file)) {
                    660: 	print $fh $contents;
                    661:         $fh->close();
                    662:     }
                    663: }
                    664: 
1.78      www       665: sub inserteditinfo {
                    666:       my ($result,$filecontents)=@_;
                    667:       unless ($filecontents) {
                    668: 	  $filecontents=(<<SIMPLECONTENT);
                    669: <html>
                    670: <head>
                    671: <title>
                    672:                            Title of Document Goes Here
                    673: </title>
                    674: </head>
                    675: <body bgcolor="#FFFFFF">
                    676: 
                    677:                            Body of Document Goes Here
                    678: 
                    679: </body>
                    680: </html>
                    681: SIMPLECONTENT
                    682:       }
                    683:       my $editheader='<a href="#editsection">Edit below</a><hr />';
                    684:       my $editfooter=(<<ENDFOOTER);
                    685: <hr />
                    686: <a name="editsection" />
                    687: <form method="post">
                    688: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
                    689: <br />
                    690: <input type="submit" name="savethisfile" value="Save this file" />
                    691: </form>
                    692: ENDFOOTER
                    693:       $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
                    694:       $result=~s/(\<\/body\>)/$editfooter/is;
                    695:       return $result;
                    696: }
                    697: 
1.24      sakharuk  698: sub handler {
                    699:   my $request=shift;
1.68      www       700: 
1.64      albertel  701:   my $target='web';
1.68      www       702: 
1.65      albertel  703:   $Apache::lonxml::debug=0;
1.68      www       704: 
1.25      sakharuk  705:   if ($ENV{'browser.mathml'}) {
1.27      albertel  706:     $request->content_type('text/xml');
                    707:   } else {
                    708:     $request->content_type('text/html');
1.25      sakharuk  709:   }
1.64      albertel  710:   
1.27      albertel  711:   $request->send_http_header;
1.64      albertel  712:   
1.45      www       713:   return OK if $request->header_only;
1.27      albertel  714: 
1.79      www       715: 
                    716:   my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78      www       717: #
                    718: # Edit action? Save file.
                    719: #
                    720:   unless ($ENV{'request.state'} eq 'published') {
                    721:       if ($ENV{'form.savethisfile'}) {
1.79      www       722: 	  &storefile($file,$ENV{'form.filecont'});
1.78      www       723:       }
                    724:   }
1.24      sakharuk  725:   my %mystyle;
1.50      albertel  726:   my $result = ''; 
                    727:   my $filecontents=&Apache::lonnet::getfile($file);
                    728:   if ($filecontents == -1) {
1.78      www       729:     $result=(<<ENDNOTFOUND);
                    730: <html>
                    731: <head>
                    732: <title>File not found</title>
                    733: </head>
                    734: <body bgcolor="#FFFFFF">
                    735: <b>File not found: $file</b>
                    736: </body>
                    737: </html>
                    738: ENDNOTFOUND
1.50      albertel  739:     $filecontents='';
                    740:   } else {
                    741:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1.78      www       742:   }
                    743: 
                    744: #
                    745: # Edit action? Insert editing commands
                    746: #
                    747:   unless ($ENV{'request.state'} eq 'published') {
                    748:       $result=&inserteditinfo($result,$filecontents);
1.66      www       749:   }
1.50      albertel  750: 
1.67      www       751:   $request->print($result);
1.64      albertel  752: 
1.34      www       753:   writeallows($request->uri);
1.45      www       754:   return OK;
1.24      sakharuk  755: }
                    756:  
1.22      albertel  757: sub debug {
                    758:   if ($Apache::lonxml::debug eq 1) {
1.54      albertel  759:     print "DEBUG:".$_[0]."<br />\n";
1.22      albertel  760:   }
                    761: }
1.49      albertel  762: 
1.22      albertel  763: sub error {
1.74      albertel  764:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.55      albertel  765:     print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52      albertel  766:   } else {
                    767:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
                    768:     #notify author
                    769:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
                    770:     #notify course
                    771:     if ( $ENV{'request.course.id'} ) {
                    772:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
                    773:       foreach my $user (split /\,/, $users) {
                    774: 	($user,my $domain) = split /:/, $user;
1.54      albertel  775: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel  776:       }
                    777:     }
1.74      albertel  778: 
1.52      albertel  779:     #FIXME probably shouldn't have me get everything forever.
1.54      albertel  780:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
1.74      albertel  781:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel  782:   }
1.22      albertel  783: }
1.49      albertel  784: 
1.22      albertel  785: sub warning {
1.73      harris41  786:   if ($ENV{'request.state'} eq 'construct') {
1.55      albertel  787:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.73      harris41  788:   }
1.83      albertel  789: }
                    790: 
                    791: sub get_param {
                    792:   my ($param,$parstack,$safeeval,$context) = @_;
                    793:   if ( ! $context ) { $context = -1; }
                    794:   my $args ='';
                    795:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
                    796:   return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.22      albertel  797: }
                    798: 
1.74      albertel  799: sub register_insert {
1.75      albertel  800:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74      albertel  801:   my $i;
1.76      albertel  802:   my $tagnum=0;
1.74      albertel  803:   my @order;
                    804:   for ($i=0;$i < $#data; $i++) {
                    805:     my $line = $data[$i];
                    806:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
                    807:     if ( $line =~ /TABLE/ ) { last; }
                    808:     my ($tag,$descrip,$function,$show) = split(/,/, $line);
1.76      albertel  809:     $insertlist{"$tagnum.tag"} = $tag;
                    810:     $insertlist{"$tagnum.description"} = $descrip;
                    811:     $insertlist{"$tagnum.function"} = $function;
                    812:     $insertlist{"$tagnum.show"}= $show;
                    813:     $tagnum++;
1.74      albertel  814:   }
1.76      albertel  815:   $i++; #skipping TABLE line
                    816:   $tagnum = 0;
1.74      albertel  817:   for (;$i < $#data;$i++) {
                    818:     my $line = $data[$i];
1.76      albertel  819:     my ($mnemonic,@which) = split(/ +/,$line);
                    820:     my $tag = $insertlist{"$tagnum.tag"};
1.74      albertel  821:     for (my $j=0;$j <$#which;$j++) {
                    822:       if ( $which[$j] eq 'Y' ) {
1.76      albertel  823: 	if ($insertlist{"$j.show"} ne 'no') {
                    824: 	  push(@{ $insertlist{"$tag.which"} },$j);
                    825: 	}
1.74      albertel  826:       }
                    827:     }
1.76      albertel  828:     $tagnum++;
1.74      albertel  829:   }
                    830: }
1.1       sakharuk  831: 1;
                    832: __END__
1.68      www       833: 
                    834: 

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