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

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

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