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

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

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