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

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

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