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

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

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