File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.311: download - view: text, annotated - select for diffs
Fri Mar 19 22:01:13 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fixes the order in which things get escaped, with the old order things like ° broke
  now it
   a) escapes individually the special characters except for & and #
   b) converts HTML entity references to correct TeX
   c) escapes any & or # that aren't preceded by \

    1: # The LearningOnline Network with CAPA
    2: # XML Parser Module 
    3: #
    4: # $Id: lonxml.pm,v 1.311 2004/03/19 22:01:13 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   30: # binary executable programs or libraries distributed by the 
   31: # Michigan State University (the "Licensee"), but any binaries so 
   32: # distributed are hereby licensed only for use in the context
   33: # of a program or computational system for which the Licensee is the 
   34: # primary author or distributor, and which performs substantial 
   35: # additional tasks beyond the translation of (La)TeX into HTML.
   36: # The C source of the Code may not be distributed by the Licensee
   37: # to any other parties under any circumstances.
   38: #
   39: # last modified 06/26/00 by Alexander Sakharuk
   40: # 11/6 Gerd Kortemeyer
   41: # 6/1/1 Gerd Kortemeyer
   42: # 2/21,3/13 Guy
   43: # 3/29,5/4 Gerd Kortemeyer
   44: # 5/26 Gerd Kortemeyer
   45: # 5/27 H. K. Ng
   46: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
   47: # 6/12,6/13 H. K. Ng
   48: # 6/16 Gerd Kortemeyer
   49: # 7/27 H. K. Ng
   50: # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20,8/23,8/24 Gerd Kortemeyer
   51: # Guy Albertelli
   52: # 9/26 Gerd Kortemeyer
   53: # Dec Guy Albertelli
   54: # YEAR=2002
   55: # 1/1 Gerd Kortemeyer
   56: # 1/2 Matthew Hall
   57: # 1/3 Gerd Kortemeyer
   58: #
   59: 
   60: package Apache::lonxml; 
   61: use vars 
   62: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $prevent_entity_encode $errorcount $warningcount);
   63: use strict;
   64: use HTML::LCParser();
   65: use HTML::TreeBuilder();
   66: use HTML::Entities();
   67: use Safe();
   68: use Safe::Hole();
   69: use Math::Cephes();
   70: use Math::Random();
   71: use Opcode();
   72: use POSIX qw(strftime);
   73: 
   74: 
   75: sub register {
   76:   my ($space,@taglist) = @_;
   77:   foreach my $temptag (@taglist) {
   78:     push(@{ $Apache::lonxml::alltags{$temptag} },$space);
   79:   }
   80: }
   81: 
   82: sub deregister {
   83:   my ($space,@taglist) = @_;
   84:   foreach my $temptag (@taglist) {
   85:     my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
   86:     if ($tempspace eq $space) {
   87:       pop(@{ $Apache::lonxml::alltags{$temptag} });
   88:     }
   89:   }
   90:   #&printalltags();
   91: }
   92: 
   93: use Apache::Constants qw(:common);
   94: use Apache::lontexconvert();
   95: use Apache::style();
   96: use Apache::run();
   97: use Apache::londefdef();
   98: use Apache::scripttag();
   99: use Apache::languagetags();
  100: use Apache::edit();
  101: use Apache::inputtags();
  102: use Apache::outputtags();
  103: use Apache::lonnet();
  104: use Apache::File();
  105: use Apache::loncommon();
  106: use Apache::lonfeedback();
  107: use Apache::lonmsg();
  108: use Apache::loncacc();
  109: use Apache::lonlocal;
  110: 
  111: #==================================================   Main subroutine: xmlparse  
  112: #debugging control, to turn on debugging modify the correct handler
  113: $Apache::lonxml::debug=0;
  114: 
  115: # keeps count of the number of warnings and errors generated in a parse
  116: $warningcount=0;
  117: $errorcount=0;
  118: 
  119: #path to the directory containing the file currently being processed
  120: @pwd=();
  121: 
  122: #these two are used for capturing a subset of the output for later processing,
  123: #don't touch them directly use &startredirection and &endredirection
  124: @outputstack = ();
  125: $redirection = 0;
  126: 
  127: #controls wheter the <import> tag actually does
  128: $import = 1;
  129: @extlinks=();
  130: 
  131: # meta mode is a bit weird only some output is to be turned off
  132: #<output> tag turns metamode off (defined in londefdef.pm)
  133: $metamode = 0;
  134: 
  135: # turns on and of run::evaluate actually derefencing var refs
  136: $evaluate = 1;
  137: 
  138: # data structure for eidt mode, determines what tags can go into what other tags
  139: %insertlist=();
  140: 
  141: # stores the list of active tag namespaces
  142: @namespace=();
  143: 
  144: # if 0 all high ASCII characters will be encoded into HTML Entities
  145: $prevent_entity_encode=0;
  146: 
  147: # has the dynamic menu been updated to know about this resource
  148: $Apache::lonxml::registered=0;
  149: 
  150: # a pointer the the Apache request object
  151: $Apache::lonxml::request='';
  152: 
  153: # a problem number counter, and check on ether it is used
  154: $Apache::lonxml::counter=1;
  155: $Apache::lonxml::counter_changed=0;
  156: 
  157: #internal check on whether to look at style defs
  158: $Apache::lonxml::usestyle=1;
  159: 
  160: #locations used to store the parameter string for style substitutions
  161: $Apache::lonxml::style_values='';
  162: $Apache::lonxml::style_end_values='';
  163: 
  164: #array of ssi calls that need to occur after we are done parsing
  165: @Apache::lonxml::ssi_info=();
  166: 
  167: #should we do the postag variable interpolation
  168: $Apache::lonxml::post_evaluate=1;
  169: 
  170: #a header message to emit in the case of any generated warning or errors
  171: $Apache::lonxml::warnings_error_header='';
  172: 
  173: sub xmlbegin {
  174:   my $output='';
  175:   if ($ENV{'browser.mathml'}) {
  176:       $output='<?xml version="1.0"?>'
  177:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
  178:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
  179:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
  180:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
  181: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
  182:   } else {
  183:       $output='<html>';
  184:   }
  185:   return $output;
  186: }
  187: 
  188: sub xmlend {
  189:     my $mode='xml';
  190:     my $status='OPEN';
  191:     if ($Apache::lonhomework::parsing_a_problem) {
  192: 	$mode='problem';
  193: 	$status=$Apache::inputtags::status[-1]; 
  194:     }
  195:     return &Apache::lonfeedback::list_discussion($mode,$status).'</html>';
  196: }
  197: 
  198: sub tokeninputfield {
  199:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
  200:     $defhost=~tr/a-z/A-Z/;
  201:     return (<<ENDINPUTFIELD)
  202: <script type="text/javascript">
  203:     function updatetoken() {
  204: 	var comp=new Array;
  205:         var barcode=unescape(document.tokeninput.barcode.value);
  206:         comp=barcode.split('*');
  207:         if (typeof(comp[0])!="undefined") {
  208: 	    document.tokeninput.codeone.value=comp[0];
  209: 	}
  210:         if (typeof(comp[1])!="undefined") {
  211: 	    document.tokeninput.codetwo.value=comp[1];
  212: 	}
  213:         if (typeof(comp[2])!="undefined") {
  214:             comp[2]=comp[2].toUpperCase();
  215: 	    document.tokeninput.codethree.value=comp[2];
  216: 	}
  217:         document.tokeninput.barcode.value='';
  218:     }  
  219: </script>
  220: <form method="post" name="tokeninput">
  221: <table border="2" bgcolor="#FFFFBB">
  222: <tr><th>DocID Checkin</th></tr>
  223: <tr><td>
  224: <table>
  225: <tr>
  226: <td>Scan in Barcode</td>
  227: <td><input type="text" size="22" name="barcode" 
  228: onChange="updatetoken()"/></td>
  229: </tr>
  230: <tr><td><i>or</i> Type in DocID</td>
  231: <td>
  232: <input type="text" size="5" name="codeone" />
  233: <b><font size="+2">*</font></b>
  234: <input type="text" size="5" name="codetwo" />
  235: <b><font size="+2">*</font></b>
  236: <input type="text" size="10" name="codethree" value="$defhost" 
  237: onChange="this.value=this.value.toUpperCase()" />
  238: </td></tr>
  239: </table>
  240: </td></tr>
  241: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
  242: </table>
  243: </form>
  244: ENDINPUTFIELD
  245: }
  246: 
  247: sub maketoken {
  248:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
  249:     unless ($symb) {
  250: 	$symb=&Apache::lonnet::symbread();
  251:     }
  252:     unless ($tuname) {
  253: 	$tuname=$ENV{'user.name'};
  254:         $tudom=$ENV{'user.domain'};
  255:         $tcrsid=$ENV{'request.course.id'};
  256:     }
  257: 
  258:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
  259: }
  260: 
  261: sub printtokenheader {
  262:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
  263:     unless ($token) { return ''; }
  264: 
  265:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  266:     unless ($tsymb) {
  267: 	$tsymb=$symb;
  268:     }
  269:     unless ($tuname) {
  270: 	$tuname=$name;
  271:         $tudom=$domain;
  272:         $tcrsid=$courseid;
  273:     }
  274: 
  275:     my %reply=&Apache::lonnet::get('environment',
  276:               ['firstname','middlename','lastname','generation'],
  277:               $tudom,$tuname);
  278:     my $plainname=$reply{'firstname'}.' '. 
  279:                   $reply{'middlename'}.' '.
  280:                   $reply{'lastname'}.' '.
  281: 		  $reply{'generation'};
  282: 
  283:     if ($target eq 'web') {
  284:         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
  285: 	return 
  286:  '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
  287:                &mt('Checked out for').' '.$plainname.
  288:                '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
  289: 	       '<br />'.&mt('ID').': '.$idhash{$tuname}.
  290: 	       '<br />'.&mt('CourseID').': '.$tcrsid.
  291: 	       '<br />'.&mt('Course').': '.$ENV{'course.'.$tcrsid.'.description'}.
  292:                '<br />'.&mt('DocID').': '.$token.
  293:                '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
  294:     } else {
  295:         return $token;
  296:     }
  297: }
  298: 
  299: sub fontsettings() {
  300:     my $headerstring='';
  301:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
  302: 	$headerstring.=
  303: 	    '<meta Content-Type="text/html; charset=x-mac-roman">';
  304:     } elsif (!$ENV{'browser.mathml'} && $ENV{'browser.unicode'}) {
  305: 	$headerstring.=
  306: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  307:     }
  308:     return $headerstring;
  309: }
  310: 
  311: sub printalltags {
  312:   my $temp;
  313:   foreach $temp (sort keys %Apache::lonxml::alltags) {
  314:     &Apache::lonxml::debug("$temp -- ".
  315: 		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
  316:   }
  317: }
  318: 
  319: sub xmlparse {
  320:  my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
  321: 
  322:  &setup_globals($request,$target);
  323:  &Apache::inputtags::initialize_inputtags();
  324:  &Apache::outputtags::initialize_outputtags();
  325:  &Apache::edit::initialize_edit();
  326:  &Apache::londefdef::initialize_londefdef();
  327: 
  328: #
  329: # do we have a course style file?
  330: #
  331: 
  332:  if ($ENV{'request.course.id'} && $ENV{'request.state'} ne 'construct') {
  333:      my $bodytext=
  334: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
  335:      if ($bodytext) {
  336:        my $location=&Apache::lonnet::filelocation('',$bodytext);
  337:        my $styletext=&Apache::lonnet::getfile($location);
  338:        if ($styletext ne '-1') {
  339:           %style_for_target = (%style_for_target,
  340:                           &Apache::style::styleparser($target,$styletext));
  341:        }
  342:     }
  343:  } elsif ($ENV{'construct.style'} && ($ENV{'request.state'} eq 'construct')) {
  344:      my $location=&Apache::lonnet::filelocation('',$ENV{'construct.style'});
  345:      my $styletext=&Apache::lonnet::getfile($location);
  346:        if ($styletext ne '-1') {
  347:           %style_for_target = (%style_for_target,
  348:                           &Apache::style::styleparser($target,$styletext));
  349:       }
  350:  }
  351: #&printalltags();
  352:  my @pars = ();
  353:  my $pwd=$ENV{'request.filename'};
  354:  $pwd =~ s:/[^/]*$::;
  355:  &newparser(\@pars,\$content_file_string,$pwd);
  356: 
  357:  my $safeeval = new Safe;
  358:  my $safehole = new Safe::Hole;
  359:  &init_safespace($target,$safeeval,$safehole,$safeinit);
  360: #-------------------- Redefinition of the target in the case of compound target
  361: 
  362:  ($target, my @tenta) = split('&&',$target);
  363: 
  364:  my @stack = ();
  365:  my @parstack = ();
  366:  &initdepth;
  367: 
  368:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
  369: 				   $safeeval,\%style_for_target);
  370: 
  371:  if ($ENV{'request.uri'}) {
  372:     &writeallows($ENV{'request.uri'});
  373:  }
  374:  &do_registered_ssi();
  375:  if ($Apache::lonxml::counter_changed) { &store_counter() }
  376:  return $finaloutput;
  377: }
  378: 
  379: sub htmlclean {
  380:     my ($raw,$full)=@_;
  381: 
  382:     my $tree = HTML::TreeBuilder->new;
  383:     $tree->ignore_unknown(0);
  384: 
  385:     $tree->parse($raw);
  386: 
  387:     my $output= $tree->as_HTML(undef,' ');
  388: 
  389:     $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
  390:     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
  391:     unless ($full) {
  392:        $output=~s/\<[\/]*(body|head|html)\>//gis;
  393:     }
  394: 
  395:     $tree = $tree->delete;
  396: 
  397:     return $output;
  398: }
  399: 
  400: sub latex_special_symbols {
  401:     my ($string,$where)=@_;
  402:     if ($where eq 'header') {
  403: 	$string =~ s/(\\|_|\^)/ /g;
  404: 	$string =~ s/(\$|%|\{|\})/\\$1/g;
  405: 	$string =~ s/_/ /g;
  406: 	$string=&Apache::lonprintout::character_chart($string);
  407: 	# any & or # leftover should be safe to just escape
  408:         $string=~s/([^\\])\&/$1\\\&/g;
  409:         $string=~s/([^\\])\#/$1\\\#/g;
  410:     } else {
  411: 	$string=~s/([^\\])\%/$1\\\%/g;
  412: 	$string=~s/([^\\])(\$|_)/$1\\$2/g;
  413: 	$string=~s/\$\$/\$\\\$/g;
  414: 	$string=~s/\#\#/\#\\\#/g;
  415:         $string=~s/([^\\])(\~|\^)/$1\\$2\\strut /g;
  416: 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
  417: 	$string=&Apache::lonprintout::character_chart($string);
  418: 	# any & or # leftover should be safe to just escape
  419:         $string=~s/([^\\])\&/$1\\\&/g;
  420:         $string=~s/([^\\])\#/$1\\\#/g;
  421: #single { or } How to escape?
  422:     }
  423:     return $string;
  424: }
  425: 
  426: sub inner_xmlparse {
  427:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
  428:   my $finaloutput = '';
  429:   my $result;
  430:   my $token;
  431:   my $dontpop=0;
  432:   while ( $#$pars > -1 ) {
  433:     while ($token = $$pars['-1']->get_token) {
  434:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
  435: 	if ($metamode<1) {
  436: 	    my $text=$token->[1];
  437: 	    if ($token->[0] eq 'C' && $target eq 'tex') {
  438: 		$text = '';
  439: #		$text = '%'.$text."\n";
  440: 	    }
  441: 	    $result.=$text;
  442: 	}
  443:       } elsif (($token->[0] eq 'D')) {
  444: 	if ($metamode<1 && $target eq 'web') {
  445: 	    my $text=$token->[1];
  446: 	    $result.=$text;
  447: 	}
  448:       } elsif ($token->[0] eq 'PI') {
  449: 	if ($metamode<1 && $target eq 'web') {
  450: 	  $result=$token->[2];
  451: 	}
  452:       } elsif ($token->[0] eq 'S') {
  453: 	# add tag to stack
  454: 	push (@$stack,$token->[1]);
  455: 	# add parameters list to another stack
  456: 	push (@$parstack,&parstring($token));
  457: 	&increasedepth($token);
  458: 	if ($Apache::lonxml::usestyle &&
  459: 	    exists($$style_for_target{$token->[1]})) {
  460: 	    $Apache::lonxml::usestyle=0;
  461: 	    my $string=$$style_for_target{$token->[1]}.
  462: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
  463: 	    &Apache::lonxml::newparser($pars,\$string);
  464: 	    $Apache::lonxml::style_values=$$parstack[-1];
  465: 	    $Apache::lonxml::style_end_values=$$parstack[-1];
  466: 	} else {
  467: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
  468: 			     $parstack, $pars, $safeeval, $style_for_target);
  469: 	}
  470:       } elsif ($token->[0] eq 'E') {
  471: 	if ($Apache::lonxml::usestyle &&
  472: 	    exists($$style_for_target{'/'."$token->[1]"})) {
  473: 	    $Apache::lonxml::usestyle=0;
  474: 	    my $string=$$style_for_target{'/'.$token->[1]}.
  475: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
  476: 	    &Apache::lonxml::newparser($pars,\$string);
  477: 	    $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
  478: 	    $Apache::lonxml::style_end_values='';
  479: 	    $dontpop=1;
  480: 	} else {
  481: 	    #clear out any tags that didn't end
  482: 	    while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
  483: 		my $lasttag=$$stack[-1];
  484: 		if ($token->[1] =~ /^$lasttag$/i) {
  485: 		    &Apache::lonxml::warning('Using tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' as end tag to &lt;'.$$stack[-1].'&gt;');
  486: 		    last;
  487: 		} else {
  488: 		    &Apache::lonxml::warning('Found tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' when looking for &lt;/'.$$stack[-1].'&gt; in file');
  489: 		    &end_tag($stack,$parstack,$token);
  490: 		}
  491: 	    }
  492: 	    $result = &callsub("end_$token->[1]", $target, $token, $stack,
  493: 			       $parstack, $pars,$safeeval, $style_for_target);
  494: 	}
  495:       } else {
  496: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
  497:       }
  498:       #evaluate variable refs in result
  499:       if ($Apache::lonxml::post_evaluate &&$result ne "") {
  500: 	  my $extras;
  501: 	  if (!$Apache::lonxml::usestyle) {
  502: 	      $extras=$Apache::lonxml::style_values;
  503: 	  }
  504: 	if ( $#$parstack > -1 ) {
  505: 	  $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
  506: 	} else {
  507: 	  $result= &Apache::run::evaluate($result,$safeeval,$extras);
  508: 	}
  509:       }
  510:       $Apache::lonxml::post_evaluate=1;
  511: 
  512:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
  513: 	  #Style file definitions should be correct
  514: 	  if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
  515: 	      $result=&latex_special_symbols($result);
  516: 	  }
  517:       }
  518: 
  519:       # Encode any high ASCII characters
  520: #      if (!$Apache::lonxml::prevent_entity_encode) {
  521: #	$result=&HTML::Entities::encode($result,"\200-\377");
  522: #      }
  523:       if ($Apache::lonxml::redirection) {
  524: 	$Apache::lonxml::outputstack['-1'] .= $result;
  525:       } else {
  526: 	$finaloutput.=$result;
  527:       }
  528:       $result = '';
  529: 
  530:       if ($token->[0] eq 'E' && !$dontpop) {
  531: 	&end_tag($stack,$parstack,$token);
  532:       }
  533:       $dontpop=0;
  534:     }	
  535:     if ($#$pars > -1) {
  536: 	pop @$pars;
  537: 	pop @Apache::lonxml::pwd;
  538:     }
  539:   }
  540: 
  541:   # if ($target eq 'meta') {
  542:   #   $finaloutput.=&endredirection;
  543:   # }
  544: 
  545: 
  546:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
  547:     $finaloutput=&afterburn($finaloutput);
  548:   }	    
  549:   return $finaloutput;
  550: }
  551: 
  552: sub callsub {
  553:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  554:   my $currentstring='';
  555:   my $nodefault;
  556:   {
  557:     my $sub1;
  558:     no strict 'refs';
  559:     my $tag=$token->[1];
  560: # get utterly rid of extended html tags
  561:     if ($tag=~/^x\-/i) { return ''; }
  562:     my $space=$Apache::lonxml::alltags{$tag}[-1];
  563:     if (!$space) {
  564:      	$tag=~tr/A-Z/a-z/;
  565: 	$sub=~tr/A-Z/a-z/;
  566: 	$space=$Apache::lonxml::alltags{$tag}[-1]
  567:     }
  568: 
  569:     my $deleted=0;
  570:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  571:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
  572:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
  573: 					     $parstack,$parser,$safeeval,
  574: 					     $style);
  575:     }
  576:     if (!$deleted) {
  577:       if ($space) {
  578: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
  579: 	$sub1="$space\:\:$sub";
  580: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
  581: 					     $parstack,$parser,$safeeval,
  582: 					     $style);
  583:       } else {
  584: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
  585: 	if ($metamode <1) {
  586: 	  if (defined($token->[4]) && ($metamode < 1)) {
  587: 	    $currentstring = $token->[4];
  588: 	  } else {
  589: 	    $currentstring = $token->[2];
  590: 	  }
  591: 	}
  592:       }
  593:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
  594:       if ($currentstring eq '' && $nodefault eq '') {
  595: 	if ($target eq 'edit') {
  596: 	  #&Apache::lonxml::debug("doing default edit for $token->[1]");
  597: 	  if ($token->[0] eq 'S') {
  598: 	    $currentstring = &Apache::edit::tag_start($target,$token);
  599: 	  } elsif ($token->[0] eq 'E') {
  600: 	    $currentstring = &Apache::edit::tag_end($target,$token);
  601: 	  }
  602: 	} elsif ($target eq 'modified') {
  603: 	  if ($token->[0] eq 'S') {
  604: 	    $currentstring = $token->[4];
  605: 	    $currentstring.=&Apache::edit::handle_insert();
  606: 	  } elsif ($token->[0] eq 'E') {
  607: 	    $currentstring = $token->[2];
  608:             $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
  609: 	  } else {
  610: 	    $currentstring = $token->[2];
  611: 	  }
  612: 	}
  613:       }
  614:     }
  615:     use strict 'refs';
  616:   }
  617:   return $currentstring;
  618: }
  619: 
  620: sub setup_globals {
  621:   my ($request,$target)=@_;
  622:   $Apache::lonxml::request=$request;
  623:   $Apache::lonxml::registered = 0;
  624:   $errorcount=0;
  625:   $warningcount=0;
  626:   $Apache::lonxml::default_homework_loaded=0;
  627:   $Apache::lonxml::usestyle=1;
  628:   &init_counter();
  629:   @Apache::lonxml::pwd=();
  630:   @Apache::lonxml::extlinks=();
  631:   @Apache::lonxml::ssi_info=();
  632:   $Apache::lonxml::post_evaluate=1;
  633:   $Apache::lonxml::warnings_error_header='';
  634:   if ($target eq 'meta') {
  635:     $Apache::lonxml::redirection = 0;
  636:     $Apache::lonxml::metamode = 1;
  637:     $Apache::lonxml::evaluate = 1;
  638:     $Apache::lonxml::import = 0;
  639:   } elsif ($target eq 'answer') {
  640:     $Apache::lonxml::redirection = 0;
  641:     $Apache::lonxml::metamode = 1;
  642:     $Apache::lonxml::evaluate = 1;
  643:     $Apache::lonxml::import = 1;
  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:   } elsif ($target eq 'analyze') {
  660:     $Apache::lonxml::redirection = 0;
  661:     $Apache::lonxml::metamode = 0;
  662:     $Apache::lonxml::evaluate = 1;
  663:     $Apache::lonxml::import = 1;
  664:   } else {
  665:     $Apache::lonxml::redirection = 0;
  666:     $Apache::lonxml::metamode = 0;
  667:     $Apache::lonxml::evaluate = 1;
  668:     $Apache::lonxml::import = 1;
  669:   }
  670: }
  671: 
  672: sub init_safespace {
  673:   my ($target,$safeeval,$safehole,$safeinit) = @_;
  674:   $safeeval->permit("entereval");
  675:   $safeeval->permit(":base_math");
  676:   $safeeval->permit("sort");
  677:   $safeeval->permit("time");
  678:   $safeeval->deny(":base_io");
  679:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
  680:   $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
  681:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  682:   
  683:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
  684:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
  685:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
  686:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
  687:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
  688:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
  689:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
  690:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
  691:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
  692:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
  693:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
  694:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
  695:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
  696:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
  697:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
  698:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
  699:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
  700:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
  701:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
  702:   
  703:   $safehole->wrap(\&Math::Cephes::bdtr  ,$safeeval,'&bdtr'  );
  704:   $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
  705:   $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
  706:   $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
  707:   $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
  708:   $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
  709:   $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
  710:   $safehole->wrap(\&Math::Cephes::fdtr  ,$safeeval,'&fdtr'  );
  711:   $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
  712:   $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
  713:   $safehole->wrap(\&Math::Cephes::gdtr  ,$safeeval,'&gdtr'  );
  714:   $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
  715:   $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
  716:   $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
  717:   $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
  718:   $safehole->wrap(\&Math::Cephes::ndtr  ,$safeeval,'&ndtr'  );
  719:   $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
  720:   $safehole->wrap(\&Math::Cephes::pdtr  ,$safeeval,'&pdtr'  );
  721:   $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
  722:   $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
  723:   $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
  724:   $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
  725: 
  726: #  $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
  727: #  $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
  728: #  $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
  729: #  $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
  730: #  $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
  731: #  $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
  732: 
  733:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
  734:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
  735:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
  736:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
  737:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
  738:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
  739:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
  740:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
  741:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
  742:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
  743:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
  744:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
  745:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
  746:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
  747:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
  748:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
  749:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
  750:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
  751:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
  752:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
  753:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
  754:   $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
  755:   $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
  756: 
  757: #need to inspect this class of ops
  758: # $safeeval->deny(":base_orig");
  759:   $safeinit .= ';$external::target="'.$target.'";';
  760:   my $rndseed;
  761:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  762:   $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
  763:   $safeinit .= ';$external::randomseed='.$rndseed.';';
  764:   &Apache::lonxml::debug("Setting rndseed to $rndseed");
  765:   &Apache::run::run($safeinit,$safeeval);
  766: 
  767:   my $subroutine=<<'EVALUATESUB';
  768: sub __LC_INTERNAL_EVALUATE__ {
  769:     my ($__LC__a,$__LC__b,$__LC__c)=@_;
  770:     my $__LC__prefix;
  771:     while(1){
  772: 	{ 
  773: 	    use strict;
  774: 	    no strict "vars";
  775: 	    if (eval(defined(eval($__LC__a.$__LC__b)))) {
  776: 		return $__LC__prefix.eval($__LC__a.$__LC__b.$__LC__c);
  777: 	    }
  778: 	}
  779: 	$__LC__prefix.=substr($__LC__a,0,1,"");
  780: 	if ($__LC__a!~/^(\$|&|\#)/) { last; }
  781:     }
  782:     return $__LC__prefix.$__LC__a.$__LC__b.$__LC__c;
  783: }
  784: EVALUATESUB
  785:     $safeeval->permit("require");
  786:     $safeeval->reval($subroutine);
  787:     $safeeval->deny("require");
  788: }
  789: 
  790: sub default_homework_load {
  791:     my ($safeeval)=@_;
  792:     &Apache::lonxml::debug('Loading default_homework');
  793:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
  794:     if ($default eq -1) {
  795: 	&Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
  796:     } else {
  797: 	&Apache::run::run($default,$safeeval);
  798: 	$Apache::lonxml::default_homework_loaded=1;
  799:     }
  800: }
  801: 
  802: sub startredirection {
  803:   $Apache::lonxml::redirection++;
  804:   push (@Apache::lonxml::outputstack, '');
  805: }
  806: 
  807: sub endredirection {
  808:   if (!$Apache::lonxml::redirection) {
  809:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
  810:     return '';
  811:   }
  812:   $Apache::lonxml::redirection--;
  813:   pop @Apache::lonxml::outputstack;
  814: }
  815: 
  816: sub end_tag {
  817:   my ($tagstack,$parstack,$token)=@_;
  818:   pop(@$tagstack);
  819:   pop(@$parstack);
  820:   &decreasedepth($token);
  821: }
  822: 
  823: sub initdepth {
  824:   @Apache::lonxml::depthcounter=();
  825:   $Apache::lonxml::depth=-1;
  826:   $Apache::lonxml::olddepth=-1;
  827: }
  828: 
  829: sub increasedepth {
  830:   my ($token) = @_;
  831:   $Apache::lonxml::depth++;
  832:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  833:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  834:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  835:   }
  836:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  837:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  838: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  839: }
  840: 
  841: sub decreasedepth {
  842:   my ($token) = @_;
  843:   $Apache::lonxml::depth--;
  844:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  845:     $#Apache::lonxml::depthcounter--;
  846:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  847:   }
  848:   if (  $Apache::lonxml::depth < -1) {
  849:     &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
  850:     $Apache::lonxml::depth='-1';
  851:   }
  852:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  853:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  854: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  855: }
  856: 
  857: sub get_all_text_unbalanced {
  858: #there is a copy of this in lonpublisher.pm
  859:  my($tag,$pars)= @_;
  860:  my $token;
  861:  my $result='';
  862:  $tag='<'.$tag.'>';
  863:  while ($token = $$pars[-1]->get_token) {
  864:    if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  865:      $result.=$token->[1];
  866:    } elsif ($token->[0] eq 'PI') {
  867:      $result.=$token->[2];
  868:    } elsif ($token->[0] eq 'S') {
  869:      $result.=$token->[4];
  870:    } elsif ($token->[0] eq 'E')  {
  871:      $result.=$token->[2];
  872:    }
  873:    if ($result =~ /(.*)\Q$tag\E(.*)/s) {
  874:      &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
  875:      &Apache::lonxml::debug('Result is :'.$1);
  876:      $result=$1;
  877:      my $redo=$tag.$2;
  878:      &Apache::lonxml::newparser($pars,\$redo);
  879:      last;
  880:    }
  881:  }
  882:  return $result
  883: }
  884: 
  885: sub increment_counter {
  886:     my ($increment) = @_;
  887:     if (defined($increment) && $increment gt 0) {
  888: 	$Apache::lonxml::counter+=$increment;
  889:     } else {
  890: 	$Apache::lonxml::counter++;
  891:     }
  892:     $Apache::lonxml::counter_changed=1;
  893: }
  894: 
  895: sub init_counter {
  896:     if (defined($ENV{'form.counter'})) {
  897: 	$Apache::lonxml::counter=$ENV{'form.counter'};
  898: 	$Apache::lonxml::counter_changed=0;
  899:     } else {
  900: 	$Apache::lonxml::counter=1;
  901: 	$Apache::lonxml::counter_changed=1;
  902:     }
  903: }
  904: 
  905: sub store_counter {
  906:     &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
  907:     return '';
  908: }
  909: 
  910: sub get_all_text {
  911:     my($tag,$pars,$style)= @_;
  912:     my $gotfullstack=1;
  913:     if (ref($pars) ne 'ARRAY') {
  914: 	$gotfullstack=0;
  915: 	$pars=[$pars];
  916:     }
  917:     if (ref($style) ne 'HASH') {
  918: 	$style={};
  919:     }
  920:     my $depth=0;
  921:     my $token;
  922:     my $result='';
  923:     if ( $tag =~ m:^/: ) { 
  924: 	my $tag=substr($tag,1); 
  925: 	#&Apache::lonxml::debug("have:$tag:");
  926: 	my $top_empty=0;
  927: 	while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
  928: 	    while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
  929: 		#&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
  930: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  931: 		    $result.=$token->[1];
  932: 		} elsif ($token->[0] eq 'PI') {
  933: 		    $result.=$token->[2];
  934: 		} elsif ($token->[0] eq 'S') {
  935: 		    if ($token->[1] =~ /^$tag$/i) { $depth++; }
  936: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/i) { $Apache::lonxml::usestyle=1; }
  937: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/i) { $Apache::lonxml::usestyle=0; }
  938: 		    $result.=$token->[4];
  939: 		} elsif ($token->[0] eq 'E')  {
  940: 		    if ( $token->[1] =~ /^$tag$/i) { $depth--; }
  941: 		    #skip sending back the last end tag
  942: 		    if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
  943: 			my $string=
  944: 			    '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
  945: 				$$style{'/'.$token->[1]}.
  946: 				    $token->[2].
  947: 					'<LONCAPA_INTERNAL_TURN_STYLE_ON />';
  948: 			&Apache::lonxml::newparser($pars,\$string);
  949: 			#&Apache::lonxml::debug("reParsing $string");
  950: 			next;
  951: 		    }
  952: 		    if ($depth > -1) {
  953: 			$result.=$token->[2];
  954: 		    } else {
  955: 			$$pars[-1]->unget_token($token);
  956: 		    }
  957: 		}
  958: 	    }
  959: 	    if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
  960: 	    if (($depth >=0) && ($#$pars > 0) ) {
  961: 		pop(@$pars);
  962: 		pop(@Apache::lonxml::pwd);
  963: 	    }
  964: 	}
  965: 	if ($top_empty && $depth >= 0) {
  966: 	    #never found the end tag ran out of text, throw error send back blank
  967: 	    &error('Never found end tag for &lt;'.$tag.
  968: 		   '&gt; current string <pre>'.
  969: 		   &HTML::Entities::encode($result).
  970: 		   '</pre>');
  971: 	    if ($gotfullstack) {
  972: 		my $newstring='</'.$tag.'>'.$result;
  973: 		&Apache::lonxml::newparser($pars,\$newstring);
  974: 	    }
  975: 	    $result='';
  976: 	}
  977:     } else {
  978: 	while ($#$pars > -1) {
  979: 	    while ($token = $$pars[-1]->get_token) {
  980: 		#&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  981: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||
  982: 		    ($token->[0] eq 'D')) {
  983: 		    $result.=$token->[1];
  984: 		} elsif ($token->[0] eq 'PI') {
  985: 		    $result.=$token->[2];
  986: 		} elsif ($token->[0] eq 'S') {
  987: 		    if ( $token->[1] =~ /^$tag$/i) {
  988: 			$$pars[-1]->unget_token($token); last;
  989: 		    } else {
  990: 			$result.=$token->[4];
  991: 		    }
  992: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/i) { $Apache::lonxml::usestyle=1; }
  993: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/i) { $Apache::lonxml::usestyle=0; }
  994: 		} elsif ($token->[0] eq 'E')  {
  995: 		    $result.=$token->[2];
  996: 		}
  997: 	    }
  998: 	    if (($#$pars > 0) ) {
  999: 		pop(@$pars);
 1000: 		pop(@Apache::lonxml::pwd);
 1001: 	    } else { last; }
 1002: 	}
 1003:     }
 1004:     #&Apache::lonxml::debug("Exit:$result:");
 1005:     return $result
 1006: }
 1007: 
 1008: sub newparser {
 1009:   my ($parser,$contentref,$dir) = @_;
 1010:   push (@$parser,HTML::LCParser->new($contentref));
 1011:   $$parser['-1']->xml_mode('1');
 1012:   if ( $dir eq '' ) {
 1013:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
 1014:   } else {
 1015:     push (@Apache::lonxml::pwd, $dir);
 1016:   } 
 1017: }
 1018: 
 1019: sub parstring {
 1020:   my ($token) = @_;
 1021:   my $temp='';
 1022:   foreach (@{$token->[3]}) {
 1023:     unless ($_=~/\W/) {
 1024:       my $val=$token->[2]->{$_};
 1025:       $val =~ s/([\%\@\\\"\'])/\\$1/g;
 1026:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
 1027:       $temp .= "my \$$_=\"$val\";";
 1028:     }
 1029:   }
 1030:   return $temp;
 1031: }
 1032: 
 1033: sub writeallows {
 1034:     unless ($#extlinks>=0) { return; }
 1035:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
 1036:     if ($ENV{'httpref.'.$thisurl}) {
 1037: 	$thisurl=$ENV{'httpref.'.$thisurl};
 1038:     }
 1039:     my $thisdir=$thisurl;
 1040:     $thisdir=~s/\/[^\/]+$//;
 1041:     my %httpref=();
 1042:     foreach (@extlinks) {
 1043:        $httpref{'httpref.'.
 1044:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
 1045:     }
 1046:     @extlinks=();
 1047:     &Apache::lonnet::appenv(%httpref);
 1048: }
 1049: 
 1050: sub register_ssi {
 1051:     my ($url,%form)=@_;
 1052:     push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
 1053:     return '';
 1054: }
 1055: 
 1056: sub do_registered_ssi {
 1057:     foreach my $info (@Apache::lonxml::ssi_info) {
 1058: 	my %form=%{ $info->{'form'}};
 1059: 	my $url=$info->{'url'};
 1060: 	&Apache::lonnet::ssi($url,%form);
 1061:     }
 1062: }
 1063: #
 1064: # Afterburner handles anchors, highlights and links
 1065: #
 1066: sub afterburn {
 1067:     my $result=shift;
 1068:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1069: 					    ['highlight','anchor','link']);
 1070:     if ($ENV{'form.highlight'}) {
 1071:        foreach (split(/\,/,$ENV{'form.highlight'})) {
 1072:            my $anchorname=$_;
 1073: 	   my $matchthis=$anchorname;
 1074:            $matchthis=~s/\_+/\\s\+/g;
 1075:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
 1076:        }
 1077:     }
 1078:     if ($ENV{'form.link'}) {
 1079:        foreach (split(/\,/,$ENV{'form.link'})) {
 1080:            my ($anchorname,$linkurl)=split(/\>/,$_);
 1081: 	   my $matchthis=$anchorname;
 1082:            $matchthis=~s/\_+/\\s\+/g;
 1083:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
 1084:        }
 1085:     }
 1086:     if ($ENV{'form.anchor'}) {
 1087:         my $anchorname=$ENV{'form.anchor'};
 1088: 	my $matchthis=$anchorname;
 1089:         $matchthis=~s/\_+/\\s\+/g;
 1090:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
 1091:         $result.=(<<"ENDSCRIPT");
 1092: <script type="text/javascript">
 1093:     document.location.hash='$anchorname';
 1094: </script>
 1095: ENDSCRIPT
 1096:     }
 1097:     return $result;
 1098: }
 1099: 
 1100: sub storefile {
 1101:     my ($file,$contents)=@_;
 1102:     &Apache::lonnet::correct_line_ends(\$contents);
 1103:     if (my $fh=Apache::File->new('>'.$file)) {
 1104: 	print $fh $contents;
 1105:         $fh->close();
 1106:         return 1;
 1107:     } else {
 1108: 	&warning("Unable to save file $file");
 1109: 	return 0;
 1110:     }
 1111: }
 1112: 
 1113: sub createnewhtml {
 1114:   my $filecontents=(<<SIMPLECONTENT);
 1115: <html>
 1116: <head>
 1117: <title>
 1118:                            Title of Document Goes Here
 1119: </title>
 1120: </head>
 1121: <body bgcolor="#FFFFFF">
 1122: 
 1123:                            Body of Document Goes Here
 1124: 
 1125: </body>
 1126: </html>
 1127: SIMPLECONTENT
 1128:   return $filecontents;
 1129: }
 1130: 
 1131: sub createnewsty {
 1132:   my $filecontents=(<<SIMPLECONTENT);
 1133: <definetag name="">
 1134:     <render>
 1135:        <web></web>
 1136:        <tex></tex>
 1137:     </render>
 1138: </definetag>
 1139: SIMPLECONTENT
 1140:   return $filecontents;
 1141: }
 1142: 
 1143: 
 1144: sub inserteditinfo {
 1145:       my ($result,$filecontents,$filetype)=@_;
 1146:       $filecontents = &HTML::Entities::encode($filecontents);
 1147: #      my $editheader='<a href="#editsection">Edit below</a><hr />';
 1148:       my $xml_help = '';
 1149:       if ($filetype eq 'html') {
 1150: 	  $xml_help=Apache::loncommon::helpLatexCheatsheet();
 1151:       }
 1152:       my $cleanbut = '';
 1153:       if ($filetype eq 'html') {
 1154: 	  $cleanbut='<input type="submit" name="attemptclean" value="'.
 1155: 	      &mt('Save and then attempt to clean HTML').'" />';
 1156:       }
 1157:       my $titledisplay=&display_title();
 1158:       my %lt=&Apache::lonlocal::texthash('st' => 'Save this',
 1159: 					 'vi' => 'View',
 1160: 					 'ed' => 'Edit');
 1161:       my $buttons=(<<BUTTONS);
 1162: $cleanbut
 1163: <input type="submit" name="savethisfile" accesskey="s"  value="$lt{'st'}" />
 1164: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
 1165: BUTTONS
 1166:       my $editfooter=(<<ENDFOOTER);
 1167: <hr />
 1168: <a name="editsection" />
 1169: <form method="post">
 1170: $xml_help
 1171: <input type="hidden" name="editmode" value="$lt{'ed'}" />
 1172: $buttons<br />
 1173: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
 1174: <br />$buttons
 1175: <br />
 1176: </form>
 1177: $titledisplay
 1178: ENDFOOTER
 1179: #      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
 1180:       $result=~s/(\<\/body\>)/$editfooter/is;
 1181:       return $result;
 1182: }
 1183: 
 1184: sub get_target {
 1185:   my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
 1186:   if ( $ENV{'request.state'} eq 'published') {
 1187:     if ( defined($ENV{'form.grade_target'})
 1188: 	 && ($viewgrades == 'F' )) {
 1189:       return ($ENV{'form.grade_target'});
 1190:     } elsif (defined($ENV{'form.grade_target'})) {
 1191:       if (($ENV{'form.grade_target'} eq 'web') ||
 1192: 	  ($ENV{'form.grade_target'} eq 'tex') ) {
 1193: 	return $ENV{'form.grade_target'}
 1194:       } else {
 1195: 	return 'web';
 1196:       }
 1197:     } else {
 1198:       return 'web';
 1199:     }
 1200:   } elsif ($ENV{'request.state'} eq 'construct') {
 1201:     if ( defined($ENV{'form.grade_target'})) {
 1202:       return ($ENV{'form.grade_target'});
 1203:     } else {
 1204:       return 'web';
 1205:     }
 1206:   } else {
 1207:     return 'web';
 1208:   }
 1209: }
 1210: 
 1211: sub handler {
 1212:     my $request=shift;
 1213:     
 1214:     my $target=&get_target();
 1215:     
 1216:     $Apache::lonxml::debug=$ENV{'user.debug'};
 1217:     
 1218:     if ($ENV{'browser.mathml'}) {
 1219: 	&Apache::loncommon::content_type($request,'text/xml');
 1220:     } else {
 1221: 	&Apache::loncommon::content_type($request,'text/html');
 1222:     }
 1223:     &Apache::loncommon::no_cache($request);
 1224:     $request->send_http_header;
 1225:     
 1226:     return OK if $request->header_only;
 1227: 
 1228: 
 1229:     my $file=&Apache::lonnet::filelocation("",$request->uri);
 1230:     my $filetype;
 1231:     if ($file =~ /\.sty$/) {
 1232: 	$filetype='sty';
 1233:     } else {
 1234: 	$filetype='html';
 1235:     }
 1236: #
 1237: # Edit action? Save file.
 1238: #
 1239:     unless ($ENV{'request.state'} eq 'published') {
 1240: 	if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
 1241: 	    if (&storefile($file,$ENV{'form.filecont'})) {
 1242: 		&Apache::lonxml::info("<font COLOR=\"#0000FF\">".
 1243: 				      &mt('Updated').": ".
 1244: 				      &Apache::lonlocal::locallocaltime(time).
 1245: 				      " </font>");
 1246: 	    } 
 1247: 	}
 1248:     }
 1249:     my %mystyle;
 1250:     my $result = '';
 1251:     my $filecontents=&Apache::lonnet::getfile($file);
 1252:     if ($filecontents eq -1) {
 1253: 	my $bodytag=&Apache::loncommon::bodytag('File Error');
 1254: 	my $fnf=&mt('File not found');
 1255: 	$result=(<<ENDNOTFOUND);
 1256: <html>
 1257: <head>
 1258: <title>$fnf</title>
 1259: </head>
 1260: $bodytag
 1261: <b>$fnf: $file</b>
 1262: </body>
 1263: </html>
 1264: ENDNOTFOUND
 1265:     $filecontents='';
 1266: 	if ($ENV{'request.state'} ne 'published') {
 1267: 	    if ($filetype eq 'sty') {
 1268: 		$filecontents=&createnewsty();
 1269: 	    } else {
 1270: 		$filecontents=&createnewhtml();
 1271: 	    }
 1272: 	    $ENV{'form.editmode'}='Edit'; #force edit mode
 1273: 	}
 1274:     } else {
 1275: 	unless ($ENV{'request.state'} eq 'published') {
 1276: 	    if ($ENV{'form.attemptclean'}) {
 1277: 		$filecontents=&htmlclean($filecontents,1);
 1278: 	    }
 1279: #
 1280: # we are in construction space, see if edit mode forced
 1281:             &Apache::loncommon::get_unprocessed_cgi
 1282:                           ($ENV{'QUERY_STRING'},['editmode']);
 1283: 	}
 1284: 	if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
 1285: 	    $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
 1286: 						'',%mystyle);
 1287: 	}
 1288:     }
 1289:     
 1290: #
 1291: # Edit action? Insert editing commands
 1292: #
 1293:     unless ($ENV{'request.state'} eq 'published') {
 1294: 	if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
 1295: 	    my $displayfile=$request->uri;
 1296: 	    $displayfile=~s/^\/[^\/]*//;
 1297: 	    $result='<html><body bgcolor="#FFFFFF">'.
 1298: 		&Apache::lonxml::message_location().'<h3>'.
 1299: 		$displayfile.
 1300: 		'</h3></body></html>';
 1301: 	    $result=&inserteditinfo($result,$filecontents,$filetype);
 1302: 	}
 1303:     }
 1304:     if ($filetype eq 'html') { writeallows($request->uri); }
 1305: 	
 1306:     
 1307:     &Apache::lonxml::add_messages(\$result);
 1308:     $request->print($result);
 1309:     
 1310:     return OK;
 1311: }
 1312: 
 1313: sub display_title {
 1314:     my $result;
 1315:     if ($ENV{'request.state'} eq 'construct') {
 1316: 	my $title=&Apache::lonnet::gettitle();
 1317: 	if (!defined($title) || $title eq '') {
 1318: 	    $title = $ENV{'request.filename'};
 1319: 	    $title = substr($title, rindex($title, '/') + 1);
 1320: 	}
 1321: 	$result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
 1322:     }
 1323:     return $result;
 1324: }
 1325: 
 1326: sub debug {
 1327:     if ($Apache::lonxml::debug eq "1") {
 1328: 	$|=1;
 1329: 	my $request=$Apache::lonxml::request;
 1330: 	if (!$request) { $request=Apache->request; }
 1331: 	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0])."</pre></font>\n");
 1332:     }
 1333: }
 1334: 
 1335: sub error {
 1336:   $errorcount++;
 1337:   my $request=$Apache::lonxml::request;
 1338:   if (!$request) { $request=Apache->request; }
 1339:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
 1340:     # If printing in construction space, put the error inside <pre></pre>
 1341:       push(@Apache::lonxml::error_messages,
 1342: 	   $Apache::lonxml::warnings_error_header.
 1343: 	   "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
 1344:       $Apache::lonxml::warnings_error_header='';
 1345:   } else {
 1346:       push(@Apache::lonxml::error_messages,
 1347: 	   "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />");
 1348:     #notify author
 1349:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
 1350:     #notify course
 1351:     if ( $ENV{'request.course.id'} ) {
 1352:       my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
 1353:       my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
 1354:       foreach (keys %users) {
 1355: 	my ($user,$domain) = split(/:/, $_);
 1356: 	&Apache::lonmsg::user_normal_msg($user,$domain,
 1357:         "Error [$declutter]",join('<br />',@_));
 1358:       }
 1359:     }
 1360:   }
 1361: }
 1362: 
 1363: sub warning {
 1364:     $warningcount++;
 1365:   
 1366:     if ($ENV{'form.grade_target'} ne 'tex') {
 1367: 	if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) {
 1368: 	    my $request=$Apache::lonxml::request;
 1369: 	    if (!$request) { $request=Apache->request; }
 1370: 	    push(@Apache::lonxml::warning_messages,
 1371: 		 $Apache::lonxml::warnings_error_header.
 1372: 		 "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
 1373: 	    $Apache::lonxml::warnings_error_header='';
 1374: 	}
 1375:     }
 1376: }
 1377: 
 1378: sub info {
 1379:     if ($ENV{'form.grade_target'} ne 'tex' 
 1380: 	&& $ENV{'request.state'} eq 'construct') {
 1381: 	push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
 1382:     }
 1383: }
 1384: 
 1385: sub message_location {
 1386:     return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
 1387: }
 1388: 
 1389: sub add_messages {
 1390:     my ($msg)=@_;
 1391:     my $result=join(' ',
 1392: 		    @Apache::lonxml::info_messages,
 1393: 		    @Apache::lonxml::error_messages,
 1394: 		    @Apache::lonxml::warning_messages);
 1395:     undef(@Apache::lonxml::info_messages);
 1396:     undef(@Apache::lonxml::error_messages);
 1397:     undef(@Apache::lonxml::warning_messages);
 1398:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
 1399:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
 1400: }
 1401: 
 1402: sub get_param {
 1403:     my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
 1404:     if ( ! $context ) { $context = -1; }
 1405:     my $args ='';
 1406:     if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
 1407:     if ( ! $Apache::lonxml::usestyle ) {
 1408: 	$args=$Apache::lonxml::style_values.$args;
 1409:     }
 1410:     if ( ! $args ) { return undef; }
 1411:     if ( $case_insensitive ) {
 1412: 	if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
 1413: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
 1414:                                      $safeeval); #'
 1415: 	} else {
 1416: 	    return undef;
 1417: 	}
 1418:     } else {
 1419: 	if ( $args =~ /my \$\Q$param\E=\"/ ) {
 1420: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
 1421:                                      $safeeval); #'
 1422: 	} else {
 1423: 	    return undef;
 1424: 	}
 1425:     }
 1426: }
 1427: 
 1428: sub get_param_var {
 1429:   my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
 1430:   if ( ! $context ) { $context = -1; }
 1431:   my $args ='';
 1432:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
 1433:   if ( ! $Apache::lonxml::usestyle ) {
 1434:       $args=$Apache::lonxml::style_values.$args;
 1435:   }
 1436:   &Apache::lonxml::debug("Args are $args param is $param");
 1437:   if ($case_insensitive) {
 1438:       if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
 1439: 	  return undef;
 1440:       }
 1441:   } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
 1442:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
 1443:   &Apache::lonxml::debug("first run is $value");
 1444:   if ($value =~ /^[\$\@\%]\w+$/) {
 1445:       &Apache::lonxml::debug("doing second");
 1446:       my @result=&Apache::run::run("return $value",$safeeval,1);
 1447:       if (!defined($result[0])) {
 1448: 	  return $value
 1449:       } else {
 1450: 	  if (wantarray) { return @result; } else { return $result[0]; }
 1451:       }
 1452:   } else {
 1453:     return $value;
 1454:   }
 1455: }
 1456: 
 1457: sub register_insert {
 1458:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
 1459:   my $i;
 1460:   my $tagnum=0;
 1461:   my @order;
 1462:   for ($i=0;$i < $#data; $i++) {
 1463:     my $line = $data[$i];
 1464:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
 1465:     if ( $line =~ /TABLE/ ) { last; }
 1466:     my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
 1467:     if ($tag) {
 1468:       $insertlist{"$tagnum.tag"} = $tag;
 1469:       $insertlist{"$tagnum.description"} = $descrip;
 1470:       $insertlist{"$tagnum.color"} = $color;
 1471:       $insertlist{"$tagnum.function"} = $function;
 1472:       if (!defined($show)) { $show='yes'; }
 1473:       $insertlist{"$tagnum.show"}= $show;
 1474:       $insertlist{"$tagnum.helpfile"} = $helpfile;
 1475:       $insertlist{"$tagnum.helpdesc"} = $helpdesc;
 1476:       $insertlist{"$tag.num"}=$tagnum;
 1477:       $tagnum++;
 1478:     }
 1479:   }
 1480:   $i++; #skipping TABLE line
 1481:   $tagnum = 0;
 1482:   for (;$i < $#data;$i++) {
 1483:     my $line = $data[$i];
 1484:     my ($mnemonic,@which) = split(/ +/,$line);
 1485:     my $tag = $insertlist{"$tagnum.tag"};
 1486:     for (my $j=0;$j <=$#which;$j++) {
 1487:       if ( $which[$j] eq 'Y' ) {
 1488: 	if ($insertlist{"$j.show"} ne 'no') {
 1489: 	  push(@{ $insertlist{"$tag.which"} },$j);
 1490: 	}
 1491:       }
 1492:     }
 1493:     $tagnum++;
 1494:   }
 1495: }
 1496: 
 1497: sub description {
 1498:   my ($token)=@_;
 1499:   my $tagnum;
 1500:   my $tag=$token->[1];
 1501:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
 1502:     my $testtag=$namespace.'::'.$tag;
 1503:     $tagnum=$insertlist{"$testtag.num"};
 1504:     if (defined($tagnum)) { last; }
 1505:   }
 1506:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
 1507:   return $insertlist{$tagnum.'.description'};
 1508: }
 1509: 
 1510: # Returns a list containing the help file, and the description
 1511: sub helpinfo {
 1512:   my ($token)=@_;
 1513:   my $tagnum;
 1514:   my $tag=$token->[1];
 1515:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
 1516:     my $testtag=$namespace.'::'.$tag;
 1517:     $tagnum=$insertlist{"$testtag.num"};
 1518:     if (defined($tagnum)) { last; }
 1519:   }
 1520:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
 1521:   return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'});
 1522: }
 1523: 
 1524: # ----------------------------------------------------------------- whichuser
 1525: # returns a list of $symb, $courseid, $domain, $name that is correct for
 1526: # calls to lonnet functions for this setup.
 1527: # - looks for form.grade_ parameters
 1528: sub whichuser {
 1529:   my ($passedsymb)=@_;
 1530:   my ($symb,$courseid,$domain,$name,$publicuser);
 1531:   if (defined($ENV{'form.grade_symb'})) {
 1532:     my $tmp_courseid=$ENV{'form.grade_courseid'};
 1533:     my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
 1534:     if ($allowed) {
 1535:       $symb=$ENV{'form.grade_symb'};
 1536:       $courseid=$ENV{'form.grade_courseid'};
 1537:       $domain=$ENV{'form.grade_domain'};
 1538:       $name=$ENV{'form.grade_username'};
 1539:     }
 1540:   } else {
 1541:       if (!$passedsymb) {
 1542:           $symb=&Apache::lonnet::symbread();
 1543:       } else {
 1544:           $symb=$passedsymb;
 1545:       }
 1546:       $courseid=$ENV{'request.course.id'};
 1547:       $domain=$ENV{'user.domain'};
 1548:       $name=$ENV{'user.name'};
 1549:       if ($name eq 'public' && $domain eq 'public') {
 1550: 	  if (!defined($ENV{'form.username'})) {
 1551: 	      $ENV{'form.username'}.=time.rand(10000000);
 1552: 	  }
 1553: 	  $name.=$ENV{'form.username'};
 1554:       }
 1555:   }
 1556:   return ($symb,$courseid,$domain,$name,$publicuser);
 1557: }
 1558: 
 1559: 1;
 1560: __END__
 1561: 
 1562: 

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