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

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

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