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

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

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