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

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

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