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

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

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