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

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.163   ! albertel    4: # $Id: lonxml.pm,v 1.162 2002/03/25 20:06:08 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;
1.161     albertel   65: use HTML::TokeParser();
                     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.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.160     www       322:     if (($ENV{'request.publicaccess'}) || 
                    323:        ($ENV{'REQUEST_URI'} eq '/res/adm/pages/menu.html')) {
1.130     www       324: 	return 
                    325:          '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
                    326:     }
1.128     albertel  327:     if ($Apache::lonxml::registered && !$forcereg) { return ''; }
1.105     albertel  328:     $Apache::lonxml::registered=1;
1.159     www       329:     my $nothing='';
                    330:     if ($ENV{'browser.type'} eq 'explorer') { $nothing='javascript:void(0);'; }
1.100     www       331:     if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
1.87      www       332:         my $hwkadd='';
                    333:         if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    334: 	    if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
                    335: 		$hwkadd.=(<<ENDSUBM);
                    336:                      menu.switchbutton
                    337:            (7,1,'subm.gif','view sub','missions',
1.88      www       338:                 'gocmd("/adm/grades","submission")');
1.87      www       339: ENDSUBM
                    340:             }
                    341: 	    if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
                    342: 		$hwkadd.=(<<ENDGRDS);
                    343:                      menu.switchbutton
                    344:            (7,2,'pgrd.gif','problem','grades',
1.88      www       345:                 'gocmd("/adm/grades","viewgrades")');
1.87      www       346: ENDGRDS
                    347:             }
                    348: 	    if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
                    349: 		$hwkadd.=(<<ENDPARM);
                    350:                      menu.switchbutton
                    351:            (7,3,'pparm.gif','problem','parms',
1.88      www       352:                 'gocmd("/adm/parmset","set")');
1.87      www       353: ENDPARM
                    354:             }
                    355: 	}
1.155     matthew   356: 	$result = (<<ENDREGTHIS);
1.87      www       357:      
1.68      www       358: <script language="JavaScript">
1.71      www       359: // BEGIN LON-CAPA Internal
1.86      www       360: 
1.69      www       361:     function LONCAPAreg() {
1.159     www       362: 	  menu=window.open("$nothing","LONCAPAmenu","",false);
1.86      www       363:           menu.clearTimeout(menu.menucltim);
1.69      www       364: 	  menu.currentURL=window.location.pathname;
                    365:           menu.currentStale=0;
1.85      www       366:           menu.clearbut(3,1);
                    367:           menu.switchbutton
1.108     www       368:        (6,3,'catalog.gif','catalog','info','catalog_info()');
                    369:           menu.switchbutton
1.85      www       370:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
                    371:           menu.switchbutton
                    372:     (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
                    373:           menu.switchbutton
                    374:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
                    375:           menu.switchbutton
                    376:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
                    377:           menu.switchbutton
                    378:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
1.94      www       379:           menu.switchbutton
                    380:                             (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
                    381:           menu.switchbutton
                    382:                          (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
                    383:           menu.switchbutton
1.95      www       384:                                (9,3,'anot.gif','anno-','tations','annotate()');
1.87      www       385:           $hwkadd
1.69      www       386:     }
1.86      www       387: 
1.69      www       388:     function LONCAPAstale() {
1.159     www       389: 	  menu=window.open("$nothing","LONCAPAmenu","",false);
1.86      www       390:           menu.currentStale=1;
1.127     www       391:           menu.switchbutton
                    392:              (3,1,'reload.gif','return','location','go(currentURL)');
                    393:           menu.clearbut(7,1);
                    394:           menu.clearbut(7,2);
                    395:           menu.clearbut(7,3);
1.86      www       396:           menu.menucltim=menu.setTimeout(
1.94      www       397:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
1.108     www       398:  'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
1.86      www       399: 			  2000);
                    400: 
1.87      www       401:       }
1.86      www       402: 
                    403: // END LON-CAPA Internal
                    404: </script>
                    405: ENDREGTHIS
                    406: 
                    407:     } else {
1.155     matthew   408:         $result = (<<ENDDONOTREGTHIS);
1.86      www       409: 
                    410: <script language="JavaScript">
                    411: // BEGIN LON-CAPA Internal
                    412: 
                    413:     function LONCAPAreg() {
1.159     www       414: 	  menu=window.open("$nothing","LONCAPAmenu","",false);
1.69      www       415:           menu.currentStale=1;
1.85      www       416:           menu.clearbut(2,1);
                    417:           menu.clearbut(2,3);
                    418:           menu.clearbut(8,1);
                    419:           menu.clearbut(8,2);
                    420:           menu.clearbut(8,3);
1.86      www       421:           if (menu.currentURL) {
                    422:              menu.switchbutton
                    423:               (3,1,'reload.gif','return','location','go(currentURL)');
                    424:  	  } else {
                    425: 	      menu.clearbut(3,1);
                    426:           }
                    427:     }
                    428: 
                    429:     function LONCAPAstale() {
1.68      www       430:     }
1.86      www       431: 
1.71      www       432: // END LON-CAPA Internal
1.68      www       433: </script>
1.86      www       434: ENDDONOTREGTHIS
                    435:     }
1.155     matthew   436:     if ($target eq 'edit') {
1.156     matthew   437: 	# Javascript routines for construction space:
                    438: 	# openbrowser and opensearcher will start the file browser
                    439: 	# (lonindexer) and searcher (lonsearchcat) respectively.
                    440: 	# Inputs are the name of the html form being used
                    441: 	# and the name of the element the selected URL should
                    442: 	# be placed in.
1.155     matthew   443:         $result .=<<"ENDBROWSERSCRIPT";
                    444: <script>
1.156     matthew   445:     var editbrowser;
1.162     matthew   446:     function openbrowser(formname,elementname,only,omit) {
1.155     matthew   447:         var url = '/res/?';
1.156     matthew   448:         if (editbrowser == null) {
1.155     matthew   449:             url += 'launch=1&';
                    450:         }
                    451:         url += 'catalogmode=interactive&';
                    452:         url += 'mode=edit&';
                    453:         url += 'form=' + formname + '&';
1.162     matthew   454:         if (only != null) {
                    455:             url += 'only=' + only + '&';
                    456:         } 
                    457:         if (omit != null) {
                    458:             url += 'omit=' + omit + '&';
                    459:         }
1.155     matthew   460:         url += 'element=' + elementname + '';
                    461:         var title = 'Browser';
                    462:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    463:         options += ',width=700,height=600';
1.156     matthew   464:         editbrowser = open(url,title,options,'1');
                    465:         editbrowser.focus();
                    466:     }
                    467:     var editsearcher;
                    468:     function opensearcher(formname,elementname) {
                    469:         var url = '/adm/searchcat?';
                    470:         if (editsearcher == null) {
                    471:             url += 'launch=1&';
                    472:         }
                    473:         url += 'catalogmode=interactive&';
                    474:         url += 'mode=edit&';
                    475:         url += 'form=' + formname + '&';
                    476:         url += 'element=' + elementname + '';
                    477:         var title = 'Search';
                    478:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    479:         options += ',width=700,height=600';
                    480:         editsearcher = open(url,title,options,'1');
                    481:         editsearcher.focus();
1.155     matthew   482:     }
                    483: </script>
                    484: ENDBROWSERSCRIPT
                    485:     }
                    486:     return $result;
1.69      www       487: }
                    488: 
                    489: sub loadevents() {
                    490:     return 'LONCAPAreg();';
                    491: }
                    492: 
                    493: sub unloadevents() {
                    494:     return 'LONCAPAstale();';
1.68      www       495: }
                    496: 
1.48      albertel  497: sub printalltags {
                    498:   my $temp;
                    499:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141     albertel  500:     &Apache::lonxml::debug("$temp -- ".
                    501: 		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48      albertel  502:   }
                    503: }
1.31      sakharuk  504: 
1.3       sakharuk  505: sub xmlparse {
1.18      albertel  506:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96      albertel  507: 
                    508:  &setup_globals($target);
1.48      albertel  509:  #&printalltags();
1.16      albertel  510:  my @pars = ();
1.23      albertel  511:  my $pwd=$ENV{'request.filename'};
                    512:  $pwd =~ s:/[^/]*$::;
                    513:  &newparser(\@pars,\$content_file_string,$pwd);
1.24      sakharuk  514: 
1.3       sakharuk  515:  my $safeeval = new Safe;
1.40      albertel  516:  my $safehole = new Safe::Hole;
1.82      ng        517:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  518: #-------------------- Redefinition of the target in the case of compound target
                    519: 
                    520:  ($target, my @tenta) = split('&&',$target);
                    521: 
1.150     albertel  522:  my @stack = ();
1.3       sakharuk  523:  my @parstack = ();
1.17      albertel  524:  &initdepth;
1.67      www       525: 
1.101     albertel  526:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
                    527: 				   $safeeval,\%style_for_target);
1.125     www       528:  if ($ENV{'request.uri'}) {
                    529:     &writeallows($ENV{'request.uri'});
                    530:  }
1.3       sakharuk  531:  return $finaloutput;
1.106     www       532: }
                    533: 
                    534: sub htmlclean {
1.107     www       535:     my ($raw,$full)=@_;
1.106     www       536: 
                    537:     my $tree = HTML::TreeBuilder->new;
                    538:     $tree->ignore_unknown(0);
1.140     albertel  539: 
1.106     www       540:     $tree->parse($raw);
                    541: 
1.107     www       542:     my $output= $tree->as_HTML(undef,' ');
1.140     albertel  543: 
1.161     albertel  544:     $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
1.111     www       545:     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
1.107     www       546:     unless ($full) {
                    547:        $output=~s/\<[\/]*(body|head|html)\>//gis;
                    548:     }
1.106     www       549: 
                    550:     $tree = $tree->delete;
                    551: 
                    552:     return $output;
1.15      albertel  553: }
                    554: 
1.101     albertel  555: sub inner_xmlparse {
                    556:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
                    557:   my $finaloutput = '';
                    558:   my $result;
                    559:   my $token;
                    560:   while ( $#$pars > -1 ) {
                    561:     while ($token = $$pars['-1']->get_token) {
                    562:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
                    563: 	if ($metamode<1) {
                    564: 	  $result=$token->[1];
                    565: 	}
                    566:       } elsif ($token->[0] eq 'PI') {
                    567: 	if ($metamode<1) {
                    568: 	  $result=$token->[2];
                    569: 	}
                    570:       } elsif ($token->[0] eq 'S') {
1.140     albertel  571: 	# add tag to stack
1.101     albertel  572: 	push (@$stack,$token->[1]);
                    573: 	# add parameters list to another stack
                    574: 	push (@$parstack,&parstring($token));
1.140     albertel  575: 	&increasedepth($token);
1.101     albertel  576: 	if (exists $$style_for_target{$token->[1]}) {
                    577: 	  if ($Apache::lonxml::redirection) {
1.140     albertel  578: 	    $Apache::lonxml::outputstack['-1'] .=
1.101     albertel  579: 	      &recurse($$style_for_target{$token->[1]},$target,$safeeval,
                    580: 		       $style_for_target,@$parstack);
                    581: 	  } else {
                    582: 	    $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
                    583: 				     $safeeval,$style_for_target,@$parstack);
                    584: 	  }
                    585: 	} else {
                    586: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
                    587: 			     $parstack, $pars, $safeeval, $style_for_target);
1.140     albertel  588: 	}
1.101     albertel  589:       } elsif ($token->[0] eq 'E') {
                    590: 	#clear out any tags that didn't end
                    591: 	while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
1.150     albertel  592: 	  my $lasttag=$$stack[-1];
                    593: 	  if ($token->[1] =~ /^$lasttag$/i) {
                    594: 	    &Apache::lonxml::warning('Using tag &lt;/'.$token->[1].'&gt; as end tag to &lt;'.$$stack[-1].'&gt;');
                    595: 	    last;
                    596: 	  } else {
1.152     albertel  597: 	    &Apache::lonxml::warning('Found tag &lt;/'.$token->[1].'&gt; when looking for &lt;/'.$$stack[-1].'&gt; in file');
1.150     albertel  598: 	    &end_tag($stack,$parstack,$token);
                    599: 	  }
1.101     albertel  600: 	}
1.140     albertel  601: 
                    602: 	if (exists($$style_for_target{'/'."$token->[1]"})) {
1.101     albertel  603: 	  if ($Apache::lonxml::redirection) {
                    604: 	    $Apache::lonxml::outputstack['-1'] .=  
                    605: 	      &recurse($$style_for_target{'/'."$token->[1]"},
                    606: 		       $target,$safeeval,$style_for_target,@$parstack);
                    607: 	  } else {
                    608: 	    $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
                    609: 				     $target,$safeeval,$style_for_target,
                    610: 				     @$parstack);
                    611: 	  }
                    612: 	} else {
                    613: 	  $result = &callsub("end_$token->[1]", $target, $token, $stack,
                    614: 			     $parstack, $pars,$safeeval, $style_for_target);
                    615: 	}
                    616:       } else {
                    617: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
                    618:       }
                    619:       #evaluate variable refs in result
                    620:       if ($result ne "") {
                    621: 	if ( $#$parstack > -1 ) {
                    622: 	  if ($Apache::lonxml::redirection) {
                    623: 	    $Apache::lonxml::outputstack['-1'] .= 
                    624: 	      &Apache::run::evaluate($result,$safeeval,$$parstack['-1']);
                    625: 	  } else {
                    626: 	    $finaloutput .= &Apache::run::evaluate($result,$safeeval,
                    627: 						   $$parstack['-1']);
                    628: 	  }
                    629: 	} else {
                    630: 	  $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
                    631: 	}
                    632: 	$result = '';
1.163   ! albertel  633:       }
1.101     albertel  634:       if ($token->[0] eq 'E') { 
                    635: 	&end_tag($stack,$parstack,$token);
                    636:       }
                    637:     }
                    638:     pop @$pars;
                    639:     pop @Apache::lonxml::pwd;
                    640:   }
                    641: 
                    642:   # if ($target eq 'meta') {
                    643:   #   $finaloutput.=&endredirection;
                    644:   # }
                    645: 
                    646:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    647:     $finaloutput=&afterburn($finaloutput);
                    648:   }
                    649:   return $finaloutput;
                    650: }
1.67      www       651: 
1.15      albertel  652: sub recurse {
                    653:   my @innerstack = (); 
                    654:   my @innerparstack = ();
                    655:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  656:   my @pat = ();
1.23      albertel  657:   &newparser(\@pat,\$newarg);
1.15      albertel  658:   my $tokenpat;
                    659:   my $partstring = '';
                    660:   my $output='';
1.16      albertel  661:   my $decls='';
1.140     albertel  662:   &Apache::lonxml::debug("Recursing");
1.16      albertel  663:   while ( $#pat > -1 ) {
                    664:     while  ($tokenpat = $pat[$#pat]->get_token) {
1.57      albertel  665:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61      albertel  666: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57      albertel  667:       } elsif ($tokenpat->[0] eq 'PI') {
1.61      albertel  668: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16      albertel  669:       } elsif ($tokenpat->[0] eq 'S') {
                    670: 	push (@innerstack,$tokenpat->[1]);
                    671: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  672: 	&increasedepth($tokenpat);
1.84      albertel  673: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
                    674: 			       \@innerstack, \@innerparstack, \@pat,
                    675: 			       $safeeval, $style_for_target);
1.16      albertel  676:       } elsif ($tokenpat->[0] eq 'E') {
                    677: 	#clear out any tags that didn't end
1.150     albertel  678: 	while ($tokenpat->[1] ne $innerstack[$#innerstack]
1.43      albertel  679: 	       && ($#innerstack > -1)) {
1.150     albertel  680: 	  my $lasttag=$innerstack[-1];
                    681: 	  if ($tokenpat->[1] =~ /^$lasttag$/i) {
                    682: 	    &Apache::lonxml::warning('Using tag &lt;/'.$tokenpat->[1].'&gt; as end tag to &lt;'.$innerstack[-1].'&gt;');
                    683: 	    last;
                    684: 	  } else {
1.152     albertel  685: 	    &Apache::lonxml::warning('Found tag &lt;/'.$tokenpat->[1].'&gt; when looking for &lt;/'.$innerstack[-1].'&gt; in file');
1.150     albertel  686: 	    &end_tag(\@innerstack,\@innerparstack,$tokenpat);
                    687: 	  }
1.43      albertel  688: 	}
1.84      albertel  689: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
                    690: 			       \@innerstack, \@innerparstack, \@pat,
                    691: 			       $safeeval, $style_for_target);
1.57      albertel  692:       } else {
                    693: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16      albertel  694:       }
                    695:       #pass both the variable to the style tag, and the tag we 
                    696:       #are processing inside the <definedtag>
                    697:       if ( $partstring ne "" ) {
                    698: 	if ( $#parstack > -1 ) { 
                    699: 	  if ( $#innerparstack > -1 ) { 
                    700: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    701: 	  } else {
                    702: 	    $decls= $parstack[$#parstack];
                    703: 	  }
                    704: 	} else {
                    705: 	  if ( $#innerparstack > -1 ) { 
                    706: 	    $decls=$innerparstack[$#innerparstack];
                    707: 	  } else {
                    708: 	    $decls='';
                    709: 	  }
                    710: 	}
                    711: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    712: 	$partstring = '';
                    713:       }
1.17      albertel  714:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  715: 				 &decreasedepth($tokenpat);}
1.15      albertel  716:     }
1.16      albertel  717:     pop @pat;
1.23      albertel  718:     pop @Apache::lonxml::pwd;
1.15      albertel  719:   }
1.140     albertel  720:   &Apache::lonxml::debug("Exiting Recursing");
1.15      albertel  721:   return $output;
1.7       albertel  722: }
                    723: 
                    724: sub callsub {
1.84      albertel  725:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  726:   my $currentstring='';
1.72      albertel  727:   my $nodefault;
1.7       albertel  728:   {
1.59      albertel  729:     my $sub1;
1.7       albertel  730:     no strict 'refs';
1.68      www       731:     my $tag=$token->[1];
1.141     albertel  732:     my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68      www       733:     if (!$space) {
1.141     albertel  734:      	$tag=~tr/A-Z/a-z/;
1.68      www       735: 	$sub=~tr/A-Z/a-z/;
1.141     albertel  736: 	$space=$Apache::lonxml::alltags{$tag}[-1]
1.68      www       737:     }
1.97      albertel  738: 
                    739:     my $deleted=0;
                    740:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
                    741:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
                    742:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
                    743: 					     $parstack,$parser,$safeeval,
                    744: 					     $style);
                    745:     }
                    746:     if (!$deleted) {
                    747:       if ($space) {
1.152     albertel  748: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97      albertel  749: 	$sub1="$space\:\:$sub";
                    750: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    751: 					     $parstack,$parser,$safeeval,
                    752: 					     $style);
                    753:       } else {
1.152     albertel  754: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97      albertel  755: 	if ($metamode <1) {
                    756: 	  if (defined($token->[4]) && ($metamode < 1)) {
                    757: 	    $currentstring = $token->[4];
                    758: 	  } else {
                    759: 	    $currentstring = $token->[2];
                    760: 	  }
1.62      sakharuk  761: 	}
1.7       albertel  762:       }
1.97      albertel  763:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
                    764:       if ($currentstring eq '' && $nodefault eq '') {
                    765: 	if ($target eq 'edit') {
                    766: 	  &Apache::lonxml::debug("doing default edit for $token->[1]");
                    767: 	  if ($token->[0] eq 'S') {
                    768: 	    $currentstring = &Apache::edit::tag_start($target,$token);
                    769: 	  } elsif ($token->[0] eq 'E') {
                    770: 	    $currentstring = &Apache::edit::tag_end($target,$token);
                    771: 	  }
                    772: 	} elsif ($target eq 'modified') {
                    773: 	  if ($token->[0] eq 'S') {
                    774: 	    $currentstring = $token->[4];
                    775: 	    $currentstring.=&Apache::edit::handle_insert();
                    776: 	  } else {
                    777: 	    $currentstring = $token->[2];
                    778: 	  }
1.72      albertel  779: 	}
                    780:       }
1.7       albertel  781:     }
                    782:     use strict 'refs';
                    783:   }
                    784:   return $currentstring;
1.82      ng        785: }
                    786: 
1.96      albertel  787: sub setup_globals {
                    788:   my ($target)=@_;
1.99      albertel  789:   $Apache::lonxml::registered = 0;
1.101     albertel  790:   @Apache::lonxml::pwd=();
1.124     albertel  791:   @Apache::lonxml::extlinks=();
1.96      albertel  792:   if ($target eq 'meta') {
                    793:     $Apache::lonxml::redirection = 0;
                    794:     $Apache::lonxml::metamode = 1;
                    795:     $Apache::lonxml::evaluate = 1;
                    796:     $Apache::lonxml::import = 0;
1.129     albertel  797:   } elsif ($target eq 'answer') {
                    798:     $Apache::lonxml::redirection = 0;
                    799:     $Apache::lonxml::metamode = 1;
                    800:     $Apache::lonxml::evaluate = 1;
                    801:     $Apache::lonxml::import = 1;
1.96      albertel  802:   } elsif ($target eq 'grade') {
                    803:     &startredirection;
                    804:     $Apache::lonxml::metamode = 0;
                    805:     $Apache::lonxml::evaluate = 1;
                    806:     $Apache::lonxml::import = 1;
                    807:   } elsif ($target eq 'modified') {
                    808:     $Apache::lonxml::redirection = 0;
                    809:     $Apache::lonxml::metamode = 0;
                    810:     $Apache::lonxml::evaluate = 0;
                    811:     $Apache::lonxml::import = 0;
                    812:   } elsif ($target eq 'edit') {
                    813:     $Apache::lonxml::redirection = 0;
                    814:     $Apache::lonxml::metamode = 0;
                    815:     $Apache::lonxml::evaluate = 0;
                    816:     $Apache::lonxml::import = 0;
1.163   ! albertel  817:   } elsif ($target eq 'analyze') {
        !           818:     $Apache::lonxml::redirection = 0;
        !           819:     $Apache::lonxml::metamode = 0;
        !           820:     $Apache::lonxml::evaluate = 1;
        !           821:     $Apache::lonxml::import = 1;
1.96      albertel  822:   } else {
                    823:     $Apache::lonxml::redirection = 0;
                    824:     $Apache::lonxml::metamode = 0;
                    825:     $Apache::lonxml::evaluate = 1;
                    826:     $Apache::lonxml::import = 1;
                    827:   }
                    828: }
                    829: 
1.82      ng        830: sub init_safespace {
                    831:   my ($target,$safeeval,$safehole,$safeinit) = @_;
                    832:   $safeeval->permit("entereval");
                    833:   $safeeval->permit(":base_math");
                    834:   $safeeval->permit("sort");
                    835:   $safeeval->deny(":base_io");
1.102     albertel  836:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.82      ng        837:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
                    838:   
                    839:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    840:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    841:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    842:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    843:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    844:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    845:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    846:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    847:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    848:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    849:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    850:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    851:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    852:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    853:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    854:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    855:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    856:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    857:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.91      ng        858:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
                    859:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
                    860:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
                    861:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
                    862:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
                    863:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
                    864:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
                    865:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
                    866:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
                    867:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
                    868:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93      ng        869:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91      ng        870:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
                    871:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
                    872:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
                    873:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
                    874:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
                    875:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
                    876:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
                    877:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
                    878:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
                    879: 
1.82      ng        880: #need to inspect this class of ops
                    881: # $safeeval->deny(":base_orig");
1.91      ng        882:   $safeinit .= ';$external::target="'.$target.'";';
1.121     albertel  883:   my $rndseed;
1.123     albertel  884:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    885:   $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.121     albertel  886:   $safeinit .= ';$external::randomseed='.$rndseed.';';
1.82      ng        887:   &Apache::run::run($safeinit,$safeeval);
1.17      albertel  888: }
                    889: 
1.55      albertel  890: sub startredirection {
                    891:   $Apache::lonxml::redirection++;
                    892:   push (@Apache::lonxml::outputstack, '');
                    893: }
                    894: 
                    895: sub endredirection {
                    896:   if (!$Apache::lonxml::redirection) {
1.72      albertel  897:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55      albertel  898:     return '';
                    899:   }
                    900:   $Apache::lonxml::redirection--;
                    901:   pop @Apache::lonxml::outputstack;
1.97      albertel  902: }
                    903: 
                    904: sub end_tag {
                    905:   my ($tagstack,$parstack,$token)=@_;
                    906:   pop(@$tagstack);
                    907:   pop(@$parstack);
                    908:   &decreasedepth($token);
1.55      albertel  909: }
                    910: 
1.17      albertel  911: sub initdepth {
                    912:   @Apache::lonxml::depthcounter=();
                    913:   $Apache::lonxml::depth=-1;
                    914:   $Apache::lonxml::olddepth=-1;
                    915: }
                    916: 
                    917: sub increasedepth {
1.19      albertel  918:   my ($token) = @_;
1.17      albertel  919:   $Apache::lonxml::depth++;
                    920:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    921:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    922:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    923:   }
1.42      albertel  924:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  925:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54      albertel  926: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  927: }
                    928: 
                    929: sub decreasedepth {
1.19      albertel  930:   my ($token) = @_;
1.17      albertel  931:   $Apache::lonxml::depth--;
1.36      albertel  932:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    933:     $#Apache::lonxml::depthcounter--;
                    934:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    935:   }
1.43      albertel  936:   if (  $Apache::lonxml::depth < -1) {
1.140     albertel  937:     &Apache::lonxml::warning("Missing tags, unable to properly run file.");
1.43      albertel  938:     $Apache::lonxml::depth='-1';
                    939:   }
1.42      albertel  940:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  941:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54      albertel  942: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  943: }
1.19      albertel  944: 
                    945: sub get_all_text {
                    946: 
                    947:  my($tag,$pars)= @_;
                    948:  my $depth=0;
                    949:  my $token;
                    950:  my $result='';
1.57      albertel  951:  if ( $tag =~ m:^/: ) { 
                    952:    my $tag=substr($tag,1); 
                    953: #   &Apache::lonxml::debug("have:$tag:");
                    954:    while (($depth >=0) && ($token = $pars->get_token)) {
                    955: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
                    956:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    957:        $result.=$token->[1];
                    958:      } elsif ($token->[0] eq 'PI') {
                    959:        $result.=$token->[2];
                    960:      } elsif ($token->[0] eq 'S') {
1.151     albertel  961:        if ($token->[1] =~ /^$tag$/i) { $depth++; }
1.57      albertel  962:        $result.=$token->[4];
                    963:      } elsif ($token->[0] eq 'E')  {
1.151     albertel  964:        if ( $token->[1] =~ /^$tag$/i) { $depth--; }
1.57      albertel  965:        #skip sending back the last end tag
                    966:        if ($depth > -1) { $result.=$token->[2]; } else {
                    967: 	 $pars->unget_token($token);
                    968:        }
                    969:      }
                    970:    }
                    971:  } else {
                    972:    while ($token = $pars->get_token) {
                    973: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                    974:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    975:        $result.=$token->[1];
                    976:      } elsif ($token->[0] eq 'PI') {
                    977:        $result.=$token->[2];
                    978:      } elsif ($token->[0] eq 'S') {
1.151     albertel  979:        if ( $token->[1] =~ /^$tag$/i) {
1.57      albertel  980: 	 $pars->unget_token($token); last;
                    981:        } else {
                    982: 	 $result.=$token->[4];
                    983:        }
                    984:      } elsif ($token->[0] eq 'E')  {
                    985:        $result.=$token->[2];
1.36      albertel  986:      }
1.19      albertel  987:    }
                    988:  }
1.49      albertel  989: # &Apache::lonxml::debug("Exit:$result:");
1.19      albertel  990:  return $result
                    991: }
                    992: 
1.23      albertel  993: sub newparser {
                    994:   my ($parser,$contentref,$dir) = @_;
                    995:   push (@$parser,HTML::TokeParser->new($contentref));
1.56      albertel  996:   $$parser['-1']->xml_mode('1');
1.163   ! albertel  997: #  $$parser['-1']->attr_encoded('1');
1.23      albertel  998:   if ( $dir eq '' ) {
                    999:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                   1000:   } else {
                   1001:     push (@Apache::lonxml::pwd, $dir);
                   1002:   } 
                   1003: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                   1004: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                   1005: }
1.1       sakharuk 1006: 
1.8       albertel 1007: sub parstring {
                   1008:   my ($token) = @_;
                   1009:   my $temp='';
1.142     albertel 1010:   foreach (@{$token->[3]}) {
1.35      www      1011:     unless ($_=~/\W/) {
1.42      albertel 1012:       my $val=$token->[2]->{$_};
1.150     albertel 1013:       $val =~ s/([\%\@\\\"])/\\$1/g;
1.51      albertel 1014:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42      albertel 1015:       $temp .= "my \$$_=\"$val\";"
1.20      albertel 1016:     }
1.142     albertel 1017:   }
1.8       albertel 1018:   return $temp;
                   1019: }
1.22      albertel 1020: 
1.34      www      1021: sub writeallows {
1.126     www      1022:     unless ($#extlinks>=0) { return; }
1.34      www      1023:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1.111     www      1024:     if ($ENV{'httpref.'.$thisurl}) {
                   1025: 	$thisurl=$ENV{'httpref.'.$thisurl};
                   1026:     }
1.34      www      1027:     my $thisdir=$thisurl;
                   1028:     $thisdir=~s/\/[^\/]+$//;
                   1029:     my %httpref=();
1.142     albertel 1030:     foreach (@extlinks) {
1.34      www      1031:        $httpref{'httpref.'.
1.125     www      1032:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142     albertel 1033:     }
1.126     www      1034:     @extlinks=();
1.34      www      1035:     &Apache::lonnet::appenv(%httpref);
                   1036: }
                   1037: 
1.66      www      1038: #
                   1039: # Afterburner handles anchors, highlights and links
                   1040: #
                   1041: sub afterburn {
                   1042:     my $result=shift;
1.154     albertel 1043:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1044: 					    ['highlight','anchor','link']);
1.66      www      1045:     if ($ENV{'form.highlight'}) {
1.142     albertel 1046:        foreach (split(/\,/,$ENV{'form.highlight'})) {
1.66      www      1047:            my $anchorname=$_;
                   1048: 	   my $matchthis=$anchorname;
                   1049:            $matchthis=~s/\_+/\\s\+/g;
                   1050:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142     albertel 1051:        }
1.66      www      1052:     }
                   1053:     if ($ENV{'form.link'}) {
1.142     albertel 1054:        foreach (split(/\,/,$ENV{'form.link'})) {
1.66      www      1055:            my ($anchorname,$linkurl)=split(/\>/,$_);
                   1056: 	   my $matchthis=$anchorname;
                   1057:            $matchthis=~s/\_+/\\s\+/g;
                   1058:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142     albertel 1059:        }
1.66      www      1060:     }
                   1061:     if ($ENV{'form.anchor'}) {
                   1062:         my $anchorname=$ENV{'form.anchor'};
                   1063: 	my $matchthis=$anchorname;
                   1064:         $matchthis=~s/\_+/\\s\+/g;
                   1065:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
                   1066:         $result.=(<<"ENDSCRIPT");
                   1067: <script>
                   1068:     document.location.hash='$anchorname';
                   1069: </script>
                   1070: ENDSCRIPT
                   1071:     }
                   1072:     return $result;
                   1073: }
                   1074: 
1.79      www      1075: sub storefile {
                   1076:     my ($file,$contents)=@_;
                   1077:     if (my $fh=Apache::File->new('>'.$file)) {
                   1078: 	print $fh $contents;
                   1079:         $fh->close();
1.147     albertel 1080:     } else {
                   1081:       &warning("Unable to save file $file");
1.79      www      1082:     }
                   1083: }
                   1084: 
1.151     albertel 1085: sub createnewhtml {
                   1086:   my $filecontents=(<<SIMPLECONTENT);
1.78      www      1087: <html>
                   1088: <head>
                   1089: <title>
                   1090:                            Title of Document Goes Here
                   1091: </title>
                   1092: </head>
                   1093: <body bgcolor="#FFFFFF">
                   1094: 
                   1095:                            Body of Document Goes Here
                   1096: 
                   1097: </body>
                   1098: </html>
                   1099: SIMPLECONTENT
1.151     albertel 1100:   return $filecontents;
                   1101: }
                   1102: 
1.147     albertel 1103: 
1.151     albertel 1104: sub inserteditinfo {
                   1105:       my ($result,$filecontents)=@_;
1.157     albertel 1106:       $filecontents = &HTML::Entities::encode($filecontents);
1.147     albertel 1107: #      my $editheader='<a href="#editsection">Edit below</a><hr />';
1.161     albertel 1108:       my $buttons=(<<BUTTONS);
                   1109: <input type="submit" name="attemptclean" 
                   1110:        value="Save and then attempt to clean HTML" />
                   1111: <input type="submit" name="savethisfile" value="Save this" />
                   1112: <input type="submit" name="viewmode" value="View" />
                   1113: BUTTONS
1.78      www      1114:       my $editfooter=(<<ENDFOOTER);
                   1115: <hr />
                   1116: <a name="editsection" />
                   1117: <form method="post">
1.161     albertel 1118: <input type="hidden" name="editmode" value="Edit" />
                   1119: $buttons
1.78      www      1120: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1.161     albertel 1121: $buttons
1.78      www      1122: <br />
                   1123: </form>
                   1124: ENDFOOTER
1.147     albertel 1125: #      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78      www      1126:       $result=~s/(\<\/body\>)/$editfooter/is;
                   1127:       return $result;
                   1128: }
                   1129: 
1.152     albertel 1130: sub get_target {
                   1131:   my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                   1132:   if ( $ENV{'request.state'} eq 'published') {
                   1133:     if ( defined($ENV{'form.grade_target'})
                   1134: 	 && ($viewgrades == 'F' )) {
                   1135:       return ($ENV{'form.grade_target'});
1.153     albertel 1136:     } elsif (defined($ENV{'form.grade_target'})) {
                   1137:       if (($ENV{'form.grade_target'} eq 'web') ||
                   1138: 	  ($ENV{'form.grade_target'} eq 'tex') ) {
                   1139: 	return $ENV{'form.grade_target'}
                   1140:       } else {
                   1141: 	return 'web';
                   1142:       }
1.152     albertel 1143:     } else {
                   1144:       return 'web';
                   1145:     }
                   1146:   } elsif ($ENV{'request.state'} eq 'construct') {
                   1147:     if ( defined($ENV{'form.grade_target'})) {
                   1148:       return ($ENV{'form.grade_target'});
                   1149:     } else {
                   1150:       return 'web';
                   1151:     }
                   1152:   } else {
                   1153:     return 'web';
                   1154:   }
                   1155: }
                   1156: 
1.24      sakharuk 1157: sub handler {
                   1158:   my $request=shift;
1.68      www      1159: 
1.152     albertel 1160:   my $target=&get_target();
1.68      www      1161: 
1.65      albertel 1162:   $Apache::lonxml::debug=0;
1.68      www      1163: 
1.25      sakharuk 1164:   if ($ENV{'browser.mathml'}) {
1.27      albertel 1165:     $request->content_type('text/xml');
                   1166:   } else {
                   1167:     $request->content_type('text/html');
1.25      sakharuk 1168:   }
1.141     albertel 1169:   &Apache::loncommon::no_cache($request);
1.27      albertel 1170:   $request->send_http_header;
1.141     albertel 1171: 
1.45      www      1172:   return OK if $request->header_only;
1.27      albertel 1173: 
1.79      www      1174: 
                   1175:   my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78      www      1176: #
                   1177: # Edit action? Save file.
                   1178: #
                   1179:   unless ($ENV{'request.state'} eq 'published') {
1.107     www      1180:       if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1.79      www      1181: 	  &storefile($file,$ENV{'form.filecont'});
1.78      www      1182:       }
                   1183:   }
1.24      sakharuk 1184:   my %mystyle;
1.147     albertel 1185:   my $result = '';
1.50      albertel 1186:   my $filecontents=&Apache::lonnet::getfile($file);
                   1187:   if ($filecontents == -1) {
1.78      www      1188:     $result=(<<ENDNOTFOUND);
                   1189: <html>
                   1190: <head>
                   1191: <title>File not found</title>
                   1192: </head>
                   1193: <body bgcolor="#FFFFFF">
                   1194: <b>File not found: $file</b>
                   1195: </body>
                   1196: </html>
                   1197: ENDNOTFOUND
1.50      albertel 1198:     $filecontents='';
1.151     albertel 1199:     if ($ENV{'request.state'} ne 'published') {
                   1200:       $filecontents=&createnewhtml();
1.161     albertel 1201:       $ENV{'form.editmode'}='Edit'; #force edit mode
1.151     albertel 1202:     }
1.50      albertel 1203:   } else {
1.147     albertel 1204:     unless ($ENV{'request.state'} eq 'published') {
                   1205:       if ($ENV{'form.attemptclean'}) {
                   1206: 	$filecontents=&htmlclean($filecontents,1);
1.107     www      1207:       }
1.147     albertel 1208:     }
1.161     albertel 1209:     if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1.147     albertel 1210:       $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
                   1211:     }
1.78      www      1212:   }
                   1213: 
                   1214: #
                   1215: # Edit action? Insert editing commands
                   1216: #
                   1217:   unless ($ENV{'request.state'} eq 'published') {
1.161     albertel 1218:     if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1.147     albertel 1219:       $result='<html><body bgcolor="#FFFFFF"></body></html>';
1.78      www      1220:       $result=&inserteditinfo($result,$filecontents);
1.147     albertel 1221:     }
1.66      www      1222:   }
1.147     albertel 1223: 
1.126     www      1224:   writeallows($request->uri);
1.50      albertel 1225: 
1.67      www      1226:   $request->print($result);
1.64      albertel 1227: 
1.45      www      1228:   return OK;
1.24      sakharuk 1229: }
1.147     albertel 1230: 
1.22      albertel 1231: sub debug {
                   1232:   if ($Apache::lonxml::debug eq 1) {
1.146     albertel 1233:     $|=1;
                   1234:     print("DEBUG:".join('<br />',@_)."<br />\n");
1.22      albertel 1235:   }
                   1236: }
1.49      albertel 1237: 
1.22      albertel 1238: sub error {
1.74      albertel 1239:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.146     albertel 1240:     print "<b>ERROR:</b>".join('<br />',@_)."<br />\n";
1.52      albertel 1241:   } else {
                   1242:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
                   1243:     #notify author
1.146     albertel 1244:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1.52      albertel 1245:     #notify course
                   1246:     if ( $ENV{'request.course.id'} ) {
                   1247:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1.143     www      1248:       my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1.52      albertel 1249:       foreach my $user (split /\,/, $users) {
                   1250: 	($user,my $domain) = split /:/, $user;
1.143     www      1251: 	&Apache::lonmsg::user_normal_msg($user,$domain,
1.146     albertel 1252:         "Error [$declutter]",join('<br />',@_));
1.52      albertel 1253:       }
                   1254:     }
1.74      albertel 1255: 
1.52      albertel 1256:     #FIXME probably shouldn't have me get everything forever.
1.146     albertel 1257:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1.74      albertel 1258:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel 1259:   }
1.22      albertel 1260: }
1.49      albertel 1261: 
1.22      albertel 1262: sub warning {
1.73      harris41 1263:   if ($ENV{'request.state'} eq 'construct') {
1.146     albertel 1264:     print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1.73      harris41 1265:   }
1.83      albertel 1266: }
                   1267: 
                   1268: sub get_param {
                   1269:   my ($param,$parstack,$safeeval,$context) = @_;
                   1270:   if ( ! $context ) { $context = -1; }
                   1271:   my $args ='';
                   1272:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.157     albertel 1273:   if ( ! $args ) { return undef; }
1.131     albertel 1274:   if ( $args =~ /my \$$param=\"/ ) {
                   1275:     return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
                   1276:   } else {
                   1277:     return undef;
                   1278:   }
1.22      albertel 1279: }
                   1280: 
1.132     albertel 1281: sub get_param_var {
                   1282:   my ($param,$parstack,$safeeval,$context) = @_;
                   1283:   if ( ! $context ) { $context = -1; }
                   1284:   my $args ='';
                   1285:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
                   1286:   if ( $args !~ /my \$$param=\"/ ) { return undef; }
                   1287:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
                   1288:   if ($value =~ /^[\$\@\%]/) {
                   1289:     return &Apache::run::run("return $value",$safeeval,1);
                   1290:   } else {
                   1291:     return $value;
                   1292:   }
                   1293: }
                   1294: 
1.74      albertel 1295: sub register_insert {
1.75      albertel 1296:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74      albertel 1297:   my $i;
1.76      albertel 1298:   my $tagnum=0;
1.74      albertel 1299:   my @order;
                   1300:   for ($i=0;$i < $#data; $i++) {
                   1301:     my $line = $data[$i];
                   1302:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
                   1303:     if ( $line =~ /TABLE/ ) { last; }
1.92      albertel 1304:     my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1.135     albertel 1305:     if ($tag) {
                   1306:       $insertlist{"$tagnum.tag"} = $tag;
                   1307:       $insertlist{"$tagnum.description"} = $descrip;
                   1308:       $insertlist{"$tagnum.color"} = $color;
                   1309:       $insertlist{"$tagnum.function"} = $function;
                   1310:       if (!defined($show)) { $show='yes'; }
                   1311:       $insertlist{"$tagnum.show"}= $show;
                   1312:       $insertlist{"$tag.num"}=$tagnum;
                   1313:       $tagnum++;
                   1314:     }
1.74      albertel 1315:   }
1.76      albertel 1316:   $i++; #skipping TABLE line
                   1317:   $tagnum = 0;
1.74      albertel 1318:   for (;$i < $#data;$i++) {
                   1319:     my $line = $data[$i];
1.76      albertel 1320:     my ($mnemonic,@which) = split(/ +/,$line);
                   1321:     my $tag = $insertlist{"$tagnum.tag"};
1.144     matthew  1322:     for (my $j=0;$j <=$#which;$j++) {
1.74      albertel 1323:       if ( $which[$j] eq 'Y' ) {
1.76      albertel 1324: 	if ($insertlist{"$j.show"} ne 'no') {
                   1325: 	  push(@{ $insertlist{"$tag.which"} },$j);
                   1326: 	}
1.74      albertel 1327:       }
                   1328:     }
1.76      albertel 1329:     $tagnum++;
1.74      albertel 1330:   }
                   1331: }
1.98      albertel 1332: 
                   1333: sub description {
                   1334:   my ($token)=@_;
1.138     albertel 1335:   my $tagnum;
                   1336:   my $tag=$token->[1];
                   1337:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
                   1338:     my $testtag=$namespace.'::'.$tag;
                   1339:     $tagnum=$insertlist{"$testtag.num"};
                   1340:     if (defined($tagnum)) { last; }
                   1341:   }
                   1342:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
                   1343:   return $insertlist{$tagnum.'.description'};
1.98      albertel 1344: }
1.123     albertel 1345: 
                   1346: # ----------------------------------------------------------------- whichuser
                   1347: # returns a list of $symb, $courseid, $domain, $name that is correct for
                   1348: # calls to lonnet functions for this setup.
                   1349: # - looks for form.grade_ parameters
                   1350: sub whichuser {
1.134     albertel 1351:   my ($symb,$courseid,$domain,$name);
1.123     albertel 1352:   if (defined($ENV{'form.grade_symb'})) {
                   1353:     my $tmp_courseid=$ENV{'form.grade_courseid'};
                   1354:     my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
                   1355:     if ($allowed) {
                   1356:       $symb=$ENV{'form.grade_symb'};
                   1357:       $courseid=$ENV{'form.grade_courseid'};
                   1358:       $domain=$ENV{'form.grade_domain'};
                   1359:       $name=$ENV{'form.grade_username'};
                   1360:     }
1.134     albertel 1361:   } else {
                   1362:     $symb=&Apache::lonnet::symbread();
                   1363:     $courseid=$ENV{'request.course.id'};
                   1364:     $domain=$ENV{'user.domain'};
                   1365:     $name=$ENV{'user.name'};
1.123     albertel 1366:   }
                   1367:   return ($symb,$courseid,$domain,$name);
                   1368: }
                   1369: 
1.1       sakharuk 1370: 1;
                   1371: __END__
1.68      www      1372: 
                   1373: 

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