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

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

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