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

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

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