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

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

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