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

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

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