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

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

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