File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.100: download - view: text, annotated - select for diffs
Thu Jul 5 15:53:09 2001 UTC (22 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Force registration mode

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

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