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

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

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