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

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.145   ! www         4: # $Id: lonxml.pm,v 1.144 2002/01/02 13:18:26 matthew 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.73      harris41   44: # 5/10 Scott Harrison
1.78      www        45: # 5/26 Gerd Kortemeyer
1.80      harris41   46: # 5/27 H. K. Ng
1.89      www        47: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
1.93      ng         48: # 6/12,6/13 H. K. Ng
1.95      www        49: # 6/16 Gerd Kortemeyer
1.104     ng         50: # 7/27 H. K. Ng
1.127     www        51: # 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        52: # Guy Albertelli
                     53: # 9/26 Gerd Kortemeyer
1.143     www        54: # Dec Guy Albertelli
                     55: # YEAR=2002
                     56: # 1/1 Gerd Kortemeyer
1.145   ! www        57: # 1/2 Matthew Hall
        !            58: # 1/3 Gerd Kortemeyer
1.143     www        59: #
1.2       sakharuk   60: 
1.4       albertel   61: package Apache::lonxml; 
1.33      www        62: use vars 
1.76      albertel   63: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
1.1       sakharuk   64: use strict;
                     65: use HTML::TokeParser;
1.106     www        66: use HTML::TreeBuilder;
1.3       sakharuk   67: use Safe;
1.40      albertel   68: use Safe::Hole;
1.81      ng         69: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
1.91      ng         70: use Math::Random qw(:all);
1.13      albertel   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.71      www        92: use Apache::lontexconvert;
1.72      albertel   93: use Apache::style;
                     94: use Apache::run;
                     95: use Apache::londefdef;
                     96: use Apache::scripttag;
                     97: use Apache::edit;
1.79      www        98: use Apache::lonnet;
                     99: use Apache::File;
1.136     albertel  100: use Apache::loncommon;
1.79      www       101: 
1.72      albertel  102: #==================================================   Main subroutine: xmlparse  
                    103: #debugging control, to turn on debugging modify the correct handler
                    104: $Apache::lonxml::debug=0;
                    105: 
                    106: #path to the directory containing the file currently being processed
                    107: @pwd=();
                    108: 
                    109: #these two are used for capturing a subset of the output for later processing,
                    110: #don't touch them directly use &startredirection and &endredirection
                    111: @outputstack = ();
                    112: $redirection = 0;
                    113: 
                    114: #controls wheter the <import> tag actually does
                    115: $import = 1;
                    116: @extlinks=();
                    117: 
                    118: # meta mode is a bit weird only some output is to be turned off
                    119: #<output> tag turns metamode off (defined in londefdef.pm)
                    120: $metamode = 0;
                    121: 
                    122: # turns on and of run::evaluate actually derefencing var refs
                    123: $evaluate = 1;
1.7       albertel  124: 
1.74      albertel  125: # data structure for eidt mode, determines what tags can go into what other tags
                    126: %insertlist=();
1.68      www       127: 
1.99      albertel  128: # stores the list of active tag namespaces
1.76      albertel  129: @namespace=();
                    130: 
1.99      albertel  131: # has the dynamic menu been updated to know about this resource
                    132: $Apache::lonxml::registered=0;
                    133: 
1.68      www       134: sub xmlbegin {
                    135:   my $output='';
                    136:   if ($ENV{'browser.mathml'}) {
                    137:       $output='<?xml version="1.0"?>'
                    138:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
                    139:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                    140:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
                    141:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                    142: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
                    143:   } else {
                    144:       $output='<html>';
                    145:   }
                    146:   return $output;
                    147: }
                    148: 
                    149: sub xmlend {
1.103     www       150:     my $discussion='';
                    151:     if ($ENV{'request.course.id'}) {
1.109     www       152:        my $crs='/'.$ENV{'request.course.id'};
                    153:        if ($ENV{'request.course.sec'}) {
                    154:           $crs.='_'.$ENV{'request.course.sec'};
                    155:        }                 
                    156:        $crs=~s/\_/\//g;
                    157:        my $seeid=&Apache::lonnet::allowed('rin',$crs);
1.103     www       158:        my $symb=&Apache::lonnet::symbread();
                    159:        if ($symb) {
                    160:           my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                    161:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    162: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    163:           if ($contrib{'version'}) {
                    164:               $discussion.=
                    165:                   '<address><hr /><h2>Course Discussion of Resource</h2>';
                    166:               my $idx;
                    167:               for ($idx=1;$idx<=$contrib{'version'};$idx++) {
1.110     www       168: 		my $hidden=($contrib{'hidden'}=~/\.$idx\./);
                    169: 		unless (($hidden) && (!$seeid)) {
                    170:                  my $message=$contrib{$idx.':message'};
                    171:                  $message=~s/\n/\<br \/\>/g;
                    172:                  if ($message) {
                    173:                   if ($hidden) {
                    174: 		      $message='<font color="#888888">'.$message.'</font>';
                    175:                   }
1.109     www       176:                   my $sender='Anonymous';
                    177:                   if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
                    178:                       $sender=$contrib{$idx.':sendername'}.' at '.
                    179: 		      $contrib{$idx.':senderdomain'};
                    180:                       if ($contrib{$idx.':anonymous'}) {
                    181: 			  $sender.=' (anonymous)';
1.110     www       182:                       }
                    183:                       if ($seeid) {
                    184: 			  if ($hidden) {
                    185:                              $sender.=' <a href="/adm/feedback?unhide='.
                    186: 				 $symb.':::'.$idx.'">Make Visible</a>';
                    187:                           } else {
                    188:                              $sender.=' <a href="/adm/feedback?hide='.
                    189: 				 $symb.':::'.$idx.'">Hide</a>';
                    190: 			  }
1.109     www       191:                       }                   
                    192:                   }
                    193: 		  $discussion.='<p><b>'.$sender.'</b> ('.
1.103     www       194:                       localtime($contrib{$idx.':timestamp'}).
                    195:                       '):<blockquote>'.$message.
1.110     www       196:                       '</blockquote></p>';
                    197: 	        }
                    198:                } 
1.103     www       199:               }
                    200:               $discussion.='</address>';
                    201:           }
                    202:        }
                    203:     }
                    204:     return $discussion.'</html>';
1.119     www       205: }
                    206: 
                    207: sub tokeninputfield {
1.120     www       208:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
                    209:     $defhost=~tr/a-z/A-Z/;
1.119     www       210:     return (<<ENDINPUTFIELD)
1.120     www       211: <script>
                    212:     function updatetoken() {
                    213: 	var comp=new Array;
                    214:         var barcode=unescape(document.tokeninput.barcode.value);
                    215:         comp=barcode.split('*');
                    216:         if (typeof(comp[0])!="undefined") {
                    217: 	    document.tokeninput.codeone.value=comp[0];
                    218: 	}
                    219:         if (typeof(comp[1])!="undefined") {
                    220: 	    document.tokeninput.codetwo.value=comp[1];
                    221: 	}
                    222:         if (typeof(comp[2])!="undefined") {
                    223:             comp[2]=comp[2].toUpperCase();
                    224: 	    document.tokeninput.codethree.value=comp[2];
                    225: 	}
                    226:         document.tokeninput.barcode.value='';
                    227:     }  
                    228: </script>
                    229: <form method="post" name="tokeninput">
1.119     www       230: <table border="2" bgcolor="#FFFFBB">
                    231: <tr><th>DocID Checkin</th></tr>
                    232: <tr><td>
                    233: <table>
                    234: <tr>
                    235: <td>Scan in Barcode</td>
1.120     www       236: <td><input type="text" size="22" name="barcode" 
                    237: onChange="updatetoken()"/></td>
1.119     www       238: </tr>
                    239: <tr><td><i>or</i> Type in DocID</td>
                    240: <td>
                    241: <input type="text" size="5" name="codeone" />
1.120     www       242: <b><font size="+2">*</font></b>
1.119     www       243: <input type="text" size="5" name="codetwo" />
1.120     www       244: <b><font size="+2">*</font></b>
                    245: <input type="text" size="10" name="codethree" value="$defhost" 
                    246: onChange="this.value=this.value.toUpperCase()" />
1.119     www       247: </td></tr>
                    248: </table>
                    249: </td></tr>
                    250: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
                    251: </table>
                    252: </form>
                    253: ENDINPUTFIELD
1.112     www       254: }
                    255: 
1.116     www       256: sub maketoken {
1.118     www       257:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112     www       258:     unless ($symb) {
                    259: 	$symb=&Apache::lonnet::symbread();
                    260:     }
                    261:     unless ($tuname) {
                    262: 	$tuname=$ENV{'user.name'};
                    263:         $tudom=$ENV{'user.domain'};
                    264:         $tcrsid=$ENV{'request.course.id'};
                    265:     }
1.116     www       266: 
1.118     www       267:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
                    268: }
                    269: 
                    270: sub printtokenheader {
1.133     albertel  271:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116     www       272:     unless ($token) { return ''; }
1.118     www       273: 
1.133     albertel  274:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    275:     unless ($tsymb) {
                    276: 	$tsymb=$symb;
1.118     www       277:     }
                    278:     unless ($tuname) {
1.133     albertel  279: 	$tuname=$name;
                    280:         $tudom=$domain;
                    281:         $tcrsid=$courseid;
1.118     www       282:     }
1.114     www       283: 
                    284:     my %reply=&Apache::lonnet::get('environment',
                    285:               ['firstname','middlename','lastname','generation'],
                    286:               $tudom,$tuname);
                    287:     my $plainname=$reply{'firstname'}.' '. 
                    288:                   $reply{'middlename'}.' '.
                    289:                   $reply{'lastname'}.' '.
                    290: 		  $reply{'generation'};
                    291: 
1.112     www       292:     if ($target eq 'web') {
1.145   ! www       293:         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115     www       294: 	return 
                    295:  '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
                    296:                'Checked out for '.$plainname.
1.114     www       297:                '<br />User: '.$tuname.' at '.$tudom.
1.145   ! www       298: 	       '<br />ID: '.$idhash{$tuname}.
1.117     www       299: 	       '<br />CourseID: '.$tcrsid.
1.145   ! www       300: 	       '<br />Course: '.$ENV{'course.'.$tcrsid.'.description'}.
1.114     www       301:                '<br />DocID: '.$token.
1.116     www       302:                '<br />Time: '.localtime().'<hr />';
1.112     www       303:     } else {
1.121     albertel  304:         return $token;
1.112     www       305:     }
1.68      www       306: }
                    307: 
1.70      www       308: sub fontsettings() {
                    309:     my $headerstring='';
                    310:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
                    311:          $headerstring.=
                    312:              '<meta Content-Type="text/html; charset=x-mac-roman">';
                    313:     }
                    314:     return $headerstring;
                    315: }
                    316: 
1.68      www       317: sub registerurl {
1.100     www       318:     my $forcereg=shift;
1.130     www       319:     if ($ENV{'request.publicaccess'}) {
                    320: 	return 
                    321:          '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
                    322:     }
1.128     albertel  323:     if ($Apache::lonxml::registered && !$forcereg) { return ''; }
1.105     albertel  324:     $Apache::lonxml::registered=1;
1.100     www       325:     if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
1.87      www       326:         my $hwkadd='';
                    327:         if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    328: 	    if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
                    329: 		$hwkadd.=(<<ENDSUBM);
                    330:                      menu.switchbutton
                    331:            (7,1,'subm.gif','view sub','missions',
1.88      www       332:                 'gocmd("/adm/grades","submission")');
1.87      www       333: ENDSUBM
                    334:             }
                    335: 	    if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
                    336: 		$hwkadd.=(<<ENDGRDS);
                    337:                      menu.switchbutton
                    338:            (7,2,'pgrd.gif','problem','grades',
1.88      www       339:                 'gocmd("/adm/grades","viewgrades")');
1.87      www       340: ENDGRDS
                    341:             }
                    342: 	    if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
                    343: 		$hwkadd.=(<<ENDPARM);
                    344:                      menu.switchbutton
                    345:            (7,3,'pparm.gif','problem','parms',
1.88      www       346:                 'gocmd("/adm/parmset","set")');
1.87      www       347: ENDPARM
                    348:             }
                    349: 	}
1.86      www       350: 	return (<<ENDREGTHIS);
1.87      www       351:      
1.68      www       352: <script language="JavaScript">
1.71      www       353: // BEGIN LON-CAPA Internal
1.86      www       354: 
1.69      www       355:     function LONCAPAreg() {
                    356: 	  menu=window.open("","LONCAPAmenu");
1.86      www       357:           menu.clearTimeout(menu.menucltim);
1.69      www       358: 	  menu.currentURL=window.location.pathname;
                    359:           menu.currentStale=0;
1.85      www       360:           menu.clearbut(3,1);
                    361:           menu.switchbutton
1.108     www       362:        (6,3,'catalog.gif','catalog','info','catalog_info()');
                    363:           menu.switchbutton
1.85      www       364:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
                    365:           menu.switchbutton
                    366:     (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
                    367:           menu.switchbutton
                    368:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
                    369:           menu.switchbutton
                    370:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
                    371:           menu.switchbutton
                    372:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
1.94      www       373:           menu.switchbutton
                    374:                             (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
                    375:           menu.switchbutton
                    376:                          (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
                    377:           menu.switchbutton
1.95      www       378:                                (9,3,'anot.gif','anno-','tations','annotate()');
1.87      www       379:           $hwkadd
1.69      www       380:     }
1.86      www       381: 
1.69      www       382:     function LONCAPAstale() {
1.86      www       383: 	  menu=window.open("","LONCAPAmenu");
                    384:           menu.currentStale=1;
1.127     www       385:           menu.switchbutton
                    386:              (3,1,'reload.gif','return','location','go(currentURL)');
                    387:           menu.clearbut(7,1);
                    388:           menu.clearbut(7,2);
                    389:           menu.clearbut(7,3);
1.86      www       390:           menu.menucltim=menu.setTimeout(
1.94      www       391:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
1.108     www       392:  'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
1.86      www       393: 			  2000);
                    394: 
1.87      www       395:       }
1.86      www       396: 
                    397: // END LON-CAPA Internal
                    398: </script>
                    399: ENDREGTHIS
                    400: 
                    401:     } else {
                    402:         return (<<ENDDONOTREGTHIS);
                    403: 
                    404: <script language="JavaScript">
                    405: // BEGIN LON-CAPA Internal
                    406: 
                    407:     function LONCAPAreg() {
1.69      www       408: 	  menu=window.open("","LONCAPAmenu");
                    409:           menu.currentStale=1;
1.85      www       410:           menu.clearbut(2,1);
                    411:           menu.clearbut(2,3);
                    412:           menu.clearbut(8,1);
                    413:           menu.clearbut(8,2);
                    414:           menu.clearbut(8,3);
1.86      www       415:           if (menu.currentURL) {
                    416:              menu.switchbutton
                    417:               (3,1,'reload.gif','return','location','go(currentURL)');
                    418:  	  } else {
                    419: 	      menu.clearbut(3,1);
                    420:           }
                    421:     }
                    422: 
                    423:     function LONCAPAstale() {
1.68      www       424:     }
1.86      www       425: 
1.71      www       426: // END LON-CAPA Internal
1.68      www       427: </script>
1.86      www       428: ENDDONOTREGTHIS
                    429: 
                    430:     }
1.69      www       431: }
                    432: 
                    433: sub loadevents() {
                    434:     return 'LONCAPAreg();';
                    435: }
                    436: 
                    437: sub unloadevents() {
                    438:     return 'LONCAPAstale();';
1.68      www       439: }
                    440: 
1.48      albertel  441: sub printalltags {
                    442:   my $temp;
                    443:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141     albertel  444:     &Apache::lonxml::debug("$temp -- ".
                    445: 		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48      albertel  446:   }
                    447: }
1.31      sakharuk  448: 
1.3       sakharuk  449: sub xmlparse {
1.18      albertel  450:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96      albertel  451: 
                    452:  &setup_globals($target);
1.48      albertel  453:  #&printalltags();
1.16      albertel  454:  my @pars = ();
1.23      albertel  455:  my $pwd=$ENV{'request.filename'};
                    456:  $pwd =~ s:/[^/]*$::;
                    457:  &newparser(\@pars,\$content_file_string,$pwd);
1.24      sakharuk  458: 
1.3       sakharuk  459:  my $safeeval = new Safe;
1.40      albertel  460:  my $safehole = new Safe::Hole;
1.82      ng        461:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  462: #-------------------- Redefinition of the target in the case of compound target
                    463: 
                    464:  ($target, my @tenta) = split('&&',$target);
                    465: 
                    466:  my @stack = (); 
                    467:  my @parstack = ();
1.17      albertel  468:  &initdepth;
1.67      www       469: 
1.101     albertel  470:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
                    471: 				   $safeeval,\%style_for_target);
1.125     www       472:  if ($ENV{'request.uri'}) {
                    473:     &writeallows($ENV{'request.uri'});
                    474:  }
1.3       sakharuk  475:  return $finaloutput;
1.106     www       476: }
                    477: 
                    478: sub htmlclean {
1.107     www       479:     my ($raw,$full)=@_;
1.106     www       480: 
                    481:     my $tree = HTML::TreeBuilder->new;
                    482:     $tree->ignore_unknown(0);
1.140     albertel  483: 
1.106     www       484:     $tree->parse($raw);
                    485: 
1.107     www       486:     my $output= $tree->as_HTML(undef,' ');
1.140     albertel  487: 
1.111     www       488:     $output=~s/\<(br|hr|img|meta|allow)([^\>\/]*)\>/\<$1$2 \/\>/gis;
                    489:     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
1.107     www       490:     unless ($full) {
                    491:        $output=~s/\<[\/]*(body|head|html)\>//gis;
                    492:     }
1.106     www       493: 
                    494:     $tree = $tree->delete;
                    495: 
                    496:     return $output;
1.15      albertel  497: }
                    498: 
1.101     albertel  499: sub inner_xmlparse {
                    500:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
                    501:   my $finaloutput = '';
                    502:   my $result;
                    503:   my $token;
                    504:   while ( $#$pars > -1 ) {
                    505:     while ($token = $$pars['-1']->get_token) {
                    506:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
                    507: 	if ($metamode<1) {
                    508: 	  $result=$token->[1];
                    509: 	}
                    510:       } elsif ($token->[0] eq 'PI') {
                    511: 	if ($metamode<1) {
                    512: 	  $result=$token->[2];
                    513: 	}
                    514:       } elsif ($token->[0] eq 'S') {
1.140     albertel  515: 	# add tag to stack
1.101     albertel  516: 	push (@$stack,$token->[1]);
                    517: 	# add parameters list to another stack
                    518: 	push (@$parstack,&parstring($token));
1.140     albertel  519: 	&increasedepth($token);
1.101     albertel  520: 	if (exists $$style_for_target{$token->[1]}) {
                    521: 	  if ($Apache::lonxml::redirection) {
1.140     albertel  522: 	    $Apache::lonxml::outputstack['-1'] .=
1.101     albertel  523: 	      &recurse($$style_for_target{$token->[1]},$target,$safeeval,
                    524: 		       $style_for_target,@$parstack);
                    525: 	  } else {
                    526: 	    $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
                    527: 				     $safeeval,$style_for_target,@$parstack);
                    528: 	  }
                    529: 	} else {
                    530: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
                    531: 			     $parstack, $pars, $safeeval, $style_for_target);
1.140     albertel  532: 	}
1.101     albertel  533:       } elsif ($token->[0] eq 'E') {
                    534: 	#clear out any tags that didn't end
                    535: 	while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
1.140     albertel  536: 	  &Apache::lonxml::warning('Missing tag &lt;/'.$$stack['-1'].'&gt; in file');
1.101     albertel  537: 	  &end_tag($stack,$parstack,$token);
                    538: 	}
1.140     albertel  539: 
                    540: 	if (exists($$style_for_target{'/'."$token->[1]"})) {
1.101     albertel  541: 	  if ($Apache::lonxml::redirection) {
                    542: 	    $Apache::lonxml::outputstack['-1'] .=  
                    543: 	      &recurse($$style_for_target{'/'."$token->[1]"},
                    544: 		       $target,$safeeval,$style_for_target,@$parstack);
                    545: 	  } else {
                    546: 	    $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
                    547: 				     $target,$safeeval,$style_for_target,
                    548: 				     @$parstack);
                    549: 	  }
                    550: 	} else {
                    551: 	  $result = &callsub("end_$token->[1]", $target, $token, $stack,
                    552: 			     $parstack, $pars,$safeeval, $style_for_target);
                    553: 	}
                    554:       } else {
                    555: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
                    556:       }
                    557:       #evaluate variable refs in result
                    558:       if ($result ne "") {
                    559: 	if ( $#$parstack > -1 ) {
                    560: 	  if ($Apache::lonxml::redirection) {
                    561: 	    $Apache::lonxml::outputstack['-1'] .= 
                    562: 	      &Apache::run::evaluate($result,$safeeval,$$parstack['-1']);
                    563: 	  } else {
                    564: 	    $finaloutput .= &Apache::run::evaluate($result,$safeeval,
                    565: 						   $$parstack['-1']);
                    566: 	  }
                    567: 	} else {
                    568: 	  $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
                    569: 	}
                    570: 	$result = '';
                    571:       } 
                    572:       if ($token->[0] eq 'E') { 
                    573: 	&end_tag($stack,$parstack,$token);
                    574:       }
                    575:     }
                    576:     pop @$pars;
                    577:     pop @Apache::lonxml::pwd;
                    578:   }
                    579: 
                    580:   # if ($target eq 'meta') {
                    581:   #   $finaloutput.=&endredirection;
                    582:   # }
                    583: 
                    584:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    585:     $finaloutput=&afterburn($finaloutput);
                    586:   }
                    587:   return $finaloutput;
                    588: }
1.67      www       589: 
1.15      albertel  590: sub recurse {
                    591:   my @innerstack = (); 
                    592:   my @innerparstack = ();
                    593:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  594:   my @pat = ();
1.23      albertel  595:   &newparser(\@pat,\$newarg);
1.15      albertel  596:   my $tokenpat;
                    597:   my $partstring = '';
                    598:   my $output='';
1.16      albertel  599:   my $decls='';
1.140     albertel  600:   &Apache::lonxml::debug("Recursing");
1.16      albertel  601:   while ( $#pat > -1 ) {
                    602:     while  ($tokenpat = $pat[$#pat]->get_token) {
1.57      albertel  603:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61      albertel  604: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57      albertel  605:       } elsif ($tokenpat->[0] eq 'PI') {
1.61      albertel  606: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16      albertel  607:       } elsif ($tokenpat->[0] eq 'S') {
                    608: 	push (@innerstack,$tokenpat->[1]);
                    609: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  610: 	&increasedepth($tokenpat);
1.84      albertel  611: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
                    612: 			       \@innerstack, \@innerparstack, \@pat,
                    613: 			       $safeeval, $style_for_target);
1.16      albertel  614:       } elsif ($tokenpat->[0] eq 'E') {
                    615: 	#clear out any tags that didn't end
                    616: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
1.43      albertel  617: 	       && ($#innerstack > -1)) {
1.140     albertel  618: 	  &Apache::lonxml::warning('Missing tag &lt;/'.$innerstack['-1'].'&gt; in style');
1.97      albertel  619: 	  &end_tag(\@innerstack,\@innerparstack,$tokenpat);
1.43      albertel  620: 	}
1.84      albertel  621: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
                    622: 			       \@innerstack, \@innerparstack, \@pat,
                    623: 			       $safeeval, $style_for_target);
1.57      albertel  624:       } else {
                    625: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16      albertel  626:       }
                    627:       #pass both the variable to the style tag, and the tag we 
                    628:       #are processing inside the <definedtag>
                    629:       if ( $partstring ne "" ) {
                    630: 	if ( $#parstack > -1 ) { 
                    631: 	  if ( $#innerparstack > -1 ) { 
                    632: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    633: 	  } else {
                    634: 	    $decls= $parstack[$#parstack];
                    635: 	  }
                    636: 	} else {
                    637: 	  if ( $#innerparstack > -1 ) { 
                    638: 	    $decls=$innerparstack[$#innerparstack];
                    639: 	  } else {
                    640: 	    $decls='';
                    641: 	  }
                    642: 	}
                    643: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    644: 	$partstring = '';
                    645:       }
1.17      albertel  646:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  647: 				 &decreasedepth($tokenpat);}
1.15      albertel  648:     }
1.16      albertel  649:     pop @pat;
1.23      albertel  650:     pop @Apache::lonxml::pwd;
1.15      albertel  651:   }
1.140     albertel  652:   &Apache::lonxml::debug("Exiting Recursing");
1.15      albertel  653:   return $output;
1.7       albertel  654: }
                    655: 
                    656: sub callsub {
1.84      albertel  657:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  658:   my $currentstring='';
1.72      albertel  659:   my $nodefault;
1.7       albertel  660:   {
1.59      albertel  661:     my $sub1;
1.7       albertel  662:     no strict 'refs';
1.68      www       663:     my $tag=$token->[1];
1.141     albertel  664:     my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68      www       665:     if (!$space) {
1.141     albertel  666:      	$tag=~tr/A-Z/a-z/;
1.68      www       667: 	$sub=~tr/A-Z/a-z/;
1.141     albertel  668: 	$space=$Apache::lonxml::alltags{$tag}[-1]
1.68      www       669:     }
1.97      albertel  670: 
                    671:     my $deleted=0;
                    672:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
                    673:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
                    674:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
                    675: 					     $parstack,$parser,$safeeval,
                    676: 					     $style);
                    677:     }
                    678:     if (!$deleted) {
                    679:       if ($space) {
                    680: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
                    681: 	$sub1="$space\:\:$sub";
                    682: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    683: 					     $parstack,$parser,$safeeval,
                    684: 					     $style);
                    685:       } else {
                    686: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
                    687: 	if ($metamode <1) {
                    688: 	  if (defined($token->[4]) && ($metamode < 1)) {
                    689: 	    $currentstring = $token->[4];
                    690: 	  } else {
                    691: 	    $currentstring = $token->[2];
                    692: 	  }
1.62      sakharuk  693: 	}
1.7       albertel  694:       }
1.97      albertel  695:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
                    696:       if ($currentstring eq '' && $nodefault eq '') {
                    697: 	if ($target eq 'edit') {
                    698: 	  &Apache::lonxml::debug("doing default edit for $token->[1]");
                    699: 	  if ($token->[0] eq 'S') {
                    700: 	    $currentstring = &Apache::edit::tag_start($target,$token);
                    701: 	  } elsif ($token->[0] eq 'E') {
                    702: 	    $currentstring = &Apache::edit::tag_end($target,$token);
                    703: 	  }
                    704: 	} elsif ($target eq 'modified') {
                    705: 	  if ($token->[0] eq 'S') {
                    706: 	    $currentstring = $token->[4];
                    707: 	    $currentstring.=&Apache::edit::handle_insert();
                    708: 	  } else {
                    709: 	    $currentstring = $token->[2];
                    710: 	  }
1.72      albertel  711: 	}
                    712:       }
1.7       albertel  713:     }
                    714:     use strict 'refs';
                    715:   }
                    716:   return $currentstring;
1.82      ng        717: }
                    718: 
1.96      albertel  719: sub setup_globals {
                    720:   my ($target)=@_;
1.99      albertel  721:   $Apache::lonxml::registered = 0;
1.101     albertel  722:   @Apache::lonxml::pwd=();
1.124     albertel  723:   @Apache::lonxml::extlinks=();
1.96      albertel  724:   if ($target eq 'meta') {
                    725:     $Apache::lonxml::redirection = 0;
                    726:     $Apache::lonxml::metamode = 1;
                    727:     $Apache::lonxml::evaluate = 1;
                    728:     $Apache::lonxml::import = 0;
1.129     albertel  729:   } elsif ($target eq 'answer') {
                    730:     $Apache::lonxml::redirection = 0;
                    731:     $Apache::lonxml::metamode = 1;
                    732:     $Apache::lonxml::evaluate = 1;
                    733:     $Apache::lonxml::import = 1;
1.96      albertel  734:   } elsif ($target eq 'grade') {
                    735:     &startredirection;
                    736:     $Apache::lonxml::metamode = 0;
                    737:     $Apache::lonxml::evaluate = 1;
                    738:     $Apache::lonxml::import = 1;
                    739:   } elsif ($target eq 'modified') {
                    740:     $Apache::lonxml::redirection = 0;
                    741:     $Apache::lonxml::metamode = 0;
                    742:     $Apache::lonxml::evaluate = 0;
                    743:     $Apache::lonxml::import = 0;
                    744:   } elsif ($target eq 'edit') {
                    745:     $Apache::lonxml::redirection = 0;
                    746:     $Apache::lonxml::metamode = 0;
                    747:     $Apache::lonxml::evaluate = 0;
                    748:     $Apache::lonxml::import = 0;
                    749:   } else {
                    750:     $Apache::lonxml::redirection = 0;
                    751:     $Apache::lonxml::metamode = 0;
                    752:     $Apache::lonxml::evaluate = 1;
                    753:     $Apache::lonxml::import = 1;
                    754:   }
                    755: }
                    756: 
1.82      ng        757: sub init_safespace {
                    758:   my ($target,$safeeval,$safehole,$safeinit) = @_;
                    759:   $safeeval->permit("entereval");
                    760:   $safeeval->permit(":base_math");
                    761:   $safeeval->permit("sort");
                    762:   $safeeval->deny(":base_io");
1.102     albertel  763:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.82      ng        764:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
                    765:   
                    766:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    767:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    768:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    769:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    770:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    771:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    772:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    773:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    774:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    775:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    776:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    777:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    778:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    779:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    780:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    781:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    782:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    783:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    784:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.91      ng        785:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
                    786:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
                    787:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
                    788:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
                    789:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
                    790:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
                    791:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
                    792:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
                    793:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
                    794:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
                    795:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93      ng        796:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91      ng        797:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
                    798:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
                    799:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
                    800:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
                    801:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
                    802:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
                    803:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
                    804:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
                    805:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
                    806: 
1.82      ng        807: #need to inspect this class of ops
                    808: # $safeeval->deny(":base_orig");
1.91      ng        809:   $safeinit .= ';$external::target="'.$target.'";';
1.121     albertel  810:   my $rndseed;
1.123     albertel  811:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    812:   $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.121     albertel  813:   $safeinit .= ';$external::randomseed='.$rndseed.';';
1.82      ng        814:   &Apache::run::run($safeinit,$safeeval);
1.17      albertel  815: }
                    816: 
1.55      albertel  817: sub startredirection {
                    818:   $Apache::lonxml::redirection++;
                    819:   push (@Apache::lonxml::outputstack, '');
                    820: }
                    821: 
                    822: sub endredirection {
                    823:   if (!$Apache::lonxml::redirection) {
1.72      albertel  824:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55      albertel  825:     return '';
                    826:   }
                    827:   $Apache::lonxml::redirection--;
                    828:   pop @Apache::lonxml::outputstack;
1.97      albertel  829: }
                    830: 
                    831: sub end_tag {
                    832:   my ($tagstack,$parstack,$token)=@_;
                    833:   pop(@$tagstack);
                    834:   pop(@$parstack);
                    835:   &decreasedepth($token);
1.55      albertel  836: }
                    837: 
1.17      albertel  838: sub initdepth {
                    839:   @Apache::lonxml::depthcounter=();
                    840:   $Apache::lonxml::depth=-1;
                    841:   $Apache::lonxml::olddepth=-1;
                    842: }
                    843: 
                    844: sub increasedepth {
1.19      albertel  845:   my ($token) = @_;
1.17      albertel  846:   $Apache::lonxml::depth++;
                    847:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    848:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    849:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    850:   }
1.42      albertel  851:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  852:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54      albertel  853: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  854: }
                    855: 
                    856: sub decreasedepth {
1.19      albertel  857:   my ($token) = @_;
1.17      albertel  858:   $Apache::lonxml::depth--;
1.36      albertel  859:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    860:     $#Apache::lonxml::depthcounter--;
                    861:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    862:   }
1.43      albertel  863:   if (  $Apache::lonxml::depth < -1) {
1.140     albertel  864:     &Apache::lonxml::warning("Missing tags, unable to properly run file.");
1.43      albertel  865:     $Apache::lonxml::depth='-1';
                    866:   }
1.42      albertel  867:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  868:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\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: 
                    872: sub get_all_text {
                    873: 
                    874:  my($tag,$pars)= @_;
                    875:  my $depth=0;
                    876:  my $token;
                    877:  my $result='';
1.57      albertel  878:  if ( $tag =~ m:^/: ) { 
                    879:    my $tag=substr($tag,1); 
                    880: #   &Apache::lonxml::debug("have:$tag:");
                    881:    while (($depth >=0) && ($token = $pars->get_token)) {
                    882: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
                    883:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    884:        $result.=$token->[1];
                    885:      } elsif ($token->[0] eq 'PI') {
                    886:        $result.=$token->[2];
                    887:      } elsif ($token->[0] eq 'S') {
                    888:        if ($token->[1] eq $tag) { $depth++; }
                    889:        $result.=$token->[4];
                    890:      } elsif ($token->[0] eq 'E')  {
                    891:        if ( $token->[1] eq $tag) { $depth--; }
                    892:        #skip sending back the last end tag
                    893:        if ($depth > -1) { $result.=$token->[2]; } else {
                    894: 	 $pars->unget_token($token);
                    895:        }
                    896:      }
                    897:    }
                    898:  } else {
                    899:    while ($token = $pars->get_token) {
                    900: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                    901:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    902:        $result.=$token->[1];
                    903:      } elsif ($token->[0] eq 'PI') {
                    904:        $result.=$token->[2];
                    905:      } elsif ($token->[0] eq 'S') {
                    906:        if ( $token->[1] eq $tag) { 
                    907: 	 $pars->unget_token($token); last;
                    908:        } else {
                    909: 	 $result.=$token->[4];
                    910:        }
                    911:      } elsif ($token->[0] eq 'E')  {
                    912:        $result.=$token->[2];
1.36      albertel  913:      }
1.19      albertel  914:    }
                    915:  }
1.49      albertel  916: # &Apache::lonxml::debug("Exit:$result:");
1.19      albertel  917:  return $result
                    918: }
                    919: 
1.23      albertel  920: sub newparser {
                    921:   my ($parser,$contentref,$dir) = @_;
                    922:   push (@$parser,HTML::TokeParser->new($contentref));
1.56      albertel  923:   $$parser['-1']->xml_mode('1');
1.23      albertel  924:   if ( $dir eq '' ) {
                    925:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                    926:   } else {
                    927:     push (@Apache::lonxml::pwd, $dir);
                    928:   } 
                    929: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                    930: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                    931: }
1.1       sakharuk  932: 
1.8       albertel  933: sub parstring {
                    934:   my ($token) = @_;
                    935:   my $temp='';
1.142     albertel  936:   foreach (@{$token->[3]}) {
1.35      www       937:     unless ($_=~/\W/) {
1.42      albertel  938:       my $val=$token->[2]->{$_};
1.53      albertel  939:       $val =~ s/([\%\@\\])/\\$1/g;
1.51      albertel  940:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42      albertel  941:       $temp .= "my \$$_=\"$val\";"
1.20      albertel  942:     }
1.142     albertel  943:   }
1.8       albertel  944:   return $temp;
                    945: }
1.22      albertel  946: 
1.34      www       947: sub writeallows {
1.126     www       948:     unless ($#extlinks>=0) { return; }
1.34      www       949:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1.111     www       950:     if ($ENV{'httpref.'.$thisurl}) {
                    951: 	$thisurl=$ENV{'httpref.'.$thisurl};
                    952:     }
1.34      www       953:     my $thisdir=$thisurl;
                    954:     $thisdir=~s/\/[^\/]+$//;
                    955:     my %httpref=();
1.142     albertel  956:     foreach (@extlinks) {
1.34      www       957:        $httpref{'httpref.'.
1.125     www       958:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142     albertel  959:     }
1.126     www       960:     @extlinks=();
1.34      www       961:     &Apache::lonnet::appenv(%httpref);
                    962: }
                    963: 
1.66      www       964: #
                    965: # Afterburner handles anchors, highlights and links
                    966: #
                    967: sub afterburn {
                    968:     my $result=shift;
1.142     albertel  969:     foreach (split(/&/,$ENV{'QUERY_STRING'})) {
1.66      www       970:        my ($name, $value) = split(/=/,$_);
                    971:        $value =~ tr/+/ /;
                    972:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    973:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
                    974:            unless ($ENV{'form.'.$name}) {
                    975:               $ENV{'form.'.$name}=$value;
                    976: 	   }
                    977:        }
1.142     albertel  978:     }
1.66      www       979:     if ($ENV{'form.highlight'}) {
1.142     albertel  980:        foreach (split(/\,/,$ENV{'form.highlight'})) {
1.66      www       981:            my $anchorname=$_;
                    982: 	   my $matchthis=$anchorname;
                    983:            $matchthis=~s/\_+/\\s\+/g;
                    984:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142     albertel  985:        }
1.66      www       986:     }
                    987:     if ($ENV{'form.link'}) {
1.142     albertel  988:        foreach (split(/\,/,$ENV{'form.link'})) {
1.66      www       989:            my ($anchorname,$linkurl)=split(/\>/,$_);
                    990: 	   my $matchthis=$anchorname;
                    991:            $matchthis=~s/\_+/\\s\+/g;
                    992:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142     albertel  993:        }
1.66      www       994:     }
                    995:     if ($ENV{'form.anchor'}) {
                    996:         my $anchorname=$ENV{'form.anchor'};
                    997: 	my $matchthis=$anchorname;
                    998:         $matchthis=~s/\_+/\\s\+/g;
                    999:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
                   1000:         $result.=(<<"ENDSCRIPT");
                   1001: <script>
                   1002:     document.location.hash='$anchorname';
                   1003: </script>
                   1004: ENDSCRIPT
                   1005:     }
                   1006:     return $result;
                   1007: }
                   1008: 
1.79      www      1009: sub storefile {
                   1010:     my ($file,$contents)=@_;
                   1011:     if (my $fh=Apache::File->new('>'.$file)) {
                   1012: 	print $fh $contents;
                   1013:         $fh->close();
                   1014:     }
                   1015: }
                   1016: 
1.78      www      1017: sub inserteditinfo {
                   1018:       my ($result,$filecontents)=@_;
                   1019:       unless ($filecontents) {
                   1020: 	  $filecontents=(<<SIMPLECONTENT);
                   1021: <html>
                   1022: <head>
                   1023: <title>
                   1024:                            Title of Document Goes Here
                   1025: </title>
                   1026: </head>
                   1027: <body bgcolor="#FFFFFF">
                   1028: 
                   1029:                            Body of Document Goes Here
                   1030: 
                   1031: </body>
                   1032: </html>
                   1033: SIMPLECONTENT
                   1034:       }
                   1035:       my $editheader='<a href="#editsection">Edit below</a><hr />';
                   1036:       my $editfooter=(<<ENDFOOTER);
                   1037: <hr />
                   1038: <a name="editsection" />
                   1039: <form method="post">
                   1040: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
                   1041: <br />
1.107     www      1042: <input type="submit" name="attemptclean" 
                   1043:        value="Save and then attempt to clean HTML" />
                   1044: <input type="submit" name="savethisfile" value="Save this" />
1.78      www      1045: </form>
                   1046: ENDFOOTER
                   1047:       $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
                   1048:       $result=~s/(\<\/body\>)/$editfooter/is;
                   1049:       return $result;
                   1050: }
                   1051: 
1.24      sakharuk 1052: sub handler {
                   1053:   my $request=shift;
1.68      www      1054: 
1.64      albertel 1055:   my $target='web';
1.68      www      1056: 
1.65      albertel 1057:   $Apache::lonxml::debug=0;
1.68      www      1058: 
1.25      sakharuk 1059:   if ($ENV{'browser.mathml'}) {
1.27      albertel 1060:     $request->content_type('text/xml');
                   1061:   } else {
                   1062:     $request->content_type('text/html');
1.25      sakharuk 1063:   }
1.141     albertel 1064:   &Apache::loncommon::no_cache($request);
1.27      albertel 1065:   $request->send_http_header;
1.141     albertel 1066: 
1.45      www      1067:   return OK if $request->header_only;
1.27      albertel 1068: 
1.79      www      1069: 
                   1070:   my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78      www      1071: #
                   1072: # Edit action? Save file.
                   1073: #
                   1074:   unless ($ENV{'request.state'} eq 'published') {
1.107     www      1075:       if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1.79      www      1076: 	  &storefile($file,$ENV{'form.filecont'});
1.78      www      1077:       }
                   1078:   }
1.24      sakharuk 1079:   my %mystyle;
1.50      albertel 1080:   my $result = ''; 
                   1081:   my $filecontents=&Apache::lonnet::getfile($file);
                   1082:   if ($filecontents == -1) {
1.78      www      1083:     $result=(<<ENDNOTFOUND);
                   1084: <html>
                   1085: <head>
                   1086: <title>File not found</title>
                   1087: </head>
                   1088: <body bgcolor="#FFFFFF">
                   1089: <b>File not found: $file</b>
                   1090: </body>
                   1091: </html>
                   1092: ENDNOTFOUND
1.50      albertel 1093:     $filecontents='';
                   1094:   } else {
1.107     www      1095:       unless ($ENV{'request.state'} eq 'published') {
                   1096:          if ($ENV{'form.attemptclean'}) {
                   1097: 	    $filecontents=&htmlclean($filecontents,1);
                   1098:          }
                   1099:       }
1.50      albertel 1100:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1.78      www      1101:   }
                   1102: 
                   1103: #
                   1104: # Edit action? Insert editing commands
                   1105: #
                   1106:   unless ($ENV{'request.state'} eq 'published') {
                   1107:       $result=&inserteditinfo($result,$filecontents);
1.66      www      1108:   }
1.126     www      1109:   
                   1110:   writeallows($request->uri);
1.50      albertel 1111: 
1.67      www      1112:   $request->print($result);
1.64      albertel 1113: 
1.45      www      1114:   return OK;
1.24      sakharuk 1115: }
                   1116:  
1.22      albertel 1117: sub debug {
                   1118:   if ($Apache::lonxml::debug eq 1) {
1.96      albertel 1119:     print("DEBUG:".$_[0]."<br />\n");
1.22      albertel 1120:   }
                   1121: }
1.49      albertel 1122: 
1.22      albertel 1123: sub error {
1.74      albertel 1124:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.55      albertel 1125:     print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52      albertel 1126:   } else {
                   1127:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
                   1128:     #notify author
                   1129:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
                   1130:     #notify course
                   1131:     if ( $ENV{'request.course.id'} ) {
                   1132:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1.143     www      1133:       my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1.52      albertel 1134:       foreach my $user (split /\,/, $users) {
                   1135: 	($user,my $domain) = split /:/, $user;
1.143     www      1136: 	&Apache::lonmsg::user_normal_msg($user,$domain,
                   1137:         "Error [$declutter]",$_[0]);
1.52      albertel 1138:       }
                   1139:     }
1.74      albertel 1140: 
1.52      albertel 1141:     #FIXME probably shouldn't have me get everything forever.
1.54      albertel 1142:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
1.74      albertel 1143:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel 1144:   }
1.22      albertel 1145: }
1.49      albertel 1146: 
1.22      albertel 1147: sub warning {
1.73      harris41 1148:   if ($ENV{'request.state'} eq 'construct') {
1.55      albertel 1149:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.73      harris41 1150:   }
1.83      albertel 1151: }
                   1152: 
                   1153: sub get_param {
                   1154:   my ($param,$parstack,$safeeval,$context) = @_;
                   1155:   if ( ! $context ) { $context = -1; }
                   1156:   my $args ='';
                   1157:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.131     albertel 1158:   if ( $args =~ /my \$$param=\"/ ) {
                   1159:     return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
                   1160:   } else {
                   1161:     return undef;
                   1162:   }
1.22      albertel 1163: }
                   1164: 
1.132     albertel 1165: sub get_param_var {
                   1166:   my ($param,$parstack,$safeeval,$context) = @_;
                   1167:   if ( ! $context ) { $context = -1; }
                   1168:   my $args ='';
                   1169:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
                   1170:   if ( $args !~ /my \$$param=\"/ ) { return undef; }
                   1171:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
                   1172:   if ($value =~ /^[\$\@\%]/) {
                   1173:     return &Apache::run::run("return $value",$safeeval,1);
                   1174:   } else {
                   1175:     return $value;
                   1176:   }
                   1177: }
                   1178: 
1.74      albertel 1179: sub register_insert {
1.75      albertel 1180:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74      albertel 1181:   my $i;
1.76      albertel 1182:   my $tagnum=0;
1.74      albertel 1183:   my @order;
                   1184:   for ($i=0;$i < $#data; $i++) {
                   1185:     my $line = $data[$i];
                   1186:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
                   1187:     if ( $line =~ /TABLE/ ) { last; }
1.92      albertel 1188:     my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1.135     albertel 1189:     if ($tag) {
                   1190:       $insertlist{"$tagnum.tag"} = $tag;
                   1191:       $insertlist{"$tagnum.description"} = $descrip;
                   1192:       $insertlist{"$tagnum.color"} = $color;
                   1193:       $insertlist{"$tagnum.function"} = $function;
                   1194:       if (!defined($show)) { $show='yes'; }
                   1195:       $insertlist{"$tagnum.show"}= $show;
                   1196:       $insertlist{"$tag.num"}=$tagnum;
                   1197:       $tagnum++;
                   1198:     }
1.74      albertel 1199:   }
1.76      albertel 1200:   $i++; #skipping TABLE line
                   1201:   $tagnum = 0;
1.74      albertel 1202:   for (;$i < $#data;$i++) {
                   1203:     my $line = $data[$i];
1.76      albertel 1204:     my ($mnemonic,@which) = split(/ +/,$line);
                   1205:     my $tag = $insertlist{"$tagnum.tag"};
1.144     matthew  1206:     for (my $j=0;$j <=$#which;$j++) {
1.74      albertel 1207:       if ( $which[$j] eq 'Y' ) {
1.76      albertel 1208: 	if ($insertlist{"$j.show"} ne 'no') {
                   1209: 	  push(@{ $insertlist{"$tag.which"} },$j);
                   1210: 	}
1.74      albertel 1211:       }
                   1212:     }
1.76      albertel 1213:     $tagnum++;
1.74      albertel 1214:   }
                   1215: }
1.98      albertel 1216: 
                   1217: sub description {
                   1218:   my ($token)=@_;
1.138     albertel 1219:   my $tagnum;
                   1220:   my $tag=$token->[1];
                   1221:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
                   1222:     my $testtag=$namespace.'::'.$tag;
                   1223:     $tagnum=$insertlist{"$testtag.num"};
                   1224:     if (defined($tagnum)) { last; }
                   1225:   }
                   1226:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
                   1227:   return $insertlist{$tagnum.'.description'};
1.98      albertel 1228: }
1.123     albertel 1229: 
                   1230: # ----------------------------------------------------------------- whichuser
                   1231: # returns a list of $symb, $courseid, $domain, $name that is correct for
                   1232: # calls to lonnet functions for this setup.
                   1233: # - looks for form.grade_ parameters
                   1234: sub whichuser {
1.134     albertel 1235:   my ($symb,$courseid,$domain,$name);
1.123     albertel 1236:   if (defined($ENV{'form.grade_symb'})) {
                   1237:     my $tmp_courseid=$ENV{'form.grade_courseid'};
                   1238:     my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
                   1239:     if ($allowed) {
                   1240:       $symb=$ENV{'form.grade_symb'};
                   1241:       $courseid=$ENV{'form.grade_courseid'};
                   1242:       $domain=$ENV{'form.grade_domain'};
                   1243:       $name=$ENV{'form.grade_username'};
                   1244:     }
1.134     albertel 1245:   } else {
                   1246:     $symb=&Apache::lonnet::symbread();
                   1247:     $courseid=$ENV{'request.course.id'};
                   1248:     $domain=$ENV{'user.domain'};
                   1249:     $name=$ENV{'user.name'};
1.123     albertel 1250:   }
                   1251:   return ($symb,$courseid,$domain,$name);
                   1252: }
                   1253: 
1.1       sakharuk 1254: 1;
                   1255: __END__
1.68      www      1256: 
                   1257: 

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