1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # last modified 06/26/00 by Alexander Sakharuk
5: # 11/6 Gerd Kortemeyer
6: # 6/1/1 Gerd Kortemeyer
7: # 2/21,3/13 Guy
8: # 3/29,5/4 Gerd Kortemeyer
9: # 5/10 Scott Harrison
10: # 5/26 Gerd Kortemeyer
11: # 5/27 H. K. Ng
12: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
13: # 6/12,6/13 H. K. Ng
14: # 6/16 Gerd Kortemeyer
15: # 7/27 H. K. Ng
16: # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20 Gerd Kortemeyer
17:
18: package Apache::lonxml;
19: use vars
20: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
21: use strict;
22: use HTML::TokeParser;
23: use HTML::TreeBuilder;
24: use Safe;
25: use Safe::Hole;
26: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
27: use Math::Random qw(:all);
28: use Opcode;
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:
40: use Apache::Constants qw(:common);
41: use Apache::lontexconvert;
42: use Apache::style;
43: use Apache::run;
44: use Apache::londefdef;
45: use Apache::scripttag;
46: use Apache::edit;
47: use Apache::lonnet;
48: use Apache::File;
49:
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;
72:
73: # data structure for eidt mode, determines what tags can go into what other tags
74: %insertlist=();
75:
76: # stores the list of active tag namespaces
77: @namespace=();
78:
79: # has the dynamic menu been updated to know about this resource
80: $Apache::lonxml::registered=0;
81:
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 {
98: my $discussion='';
99: if ($ENV{'request.course.id'}) {
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);
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++) {
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: }
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)';
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: }
139: }
140: }
141: $discussion.='<p><b>'.$sender.'</b> ('.
142: localtime($contrib{$idx.':timestamp'}).
143: '):<blockquote>'.$message.
144: '</blockquote></p>';
145: }
146: }
147: }
148: $discussion.='</address>';
149: }
150: }
151: }
152: return $discussion.'</html>';
153: }
154:
155: sub tokeninputfield {
156: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
157: $defhost=~tr/a-z/A-Z/;
158: return (<<ENDINPUTFIELD)
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">
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>
184: <td><input type="text" size="22" name="barcode"
185: onChange="updatetoken()"/></td>
186: </tr>
187: <tr><td><i>or</i> Type in DocID</td>
188: <td>
189: <input type="text" size="5" name="codeone" />
190: <b><font size="+2">*</font></b>
191: <input type="text" size="5" name="codetwo" />
192: <b><font size="+2">*</font></b>
193: <input type="text" size="10" name="codethree" value="$defhost"
194: onChange="this.value=this.value.toUpperCase()" />
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
202: }
203:
204: sub maketoken {
205: my ($symb,$tuname,$tudom,$tcrsid)=@_;
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: }
214:
215: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
216: }
217:
218: sub printtokenheader {
219: my ($target,$token,$symb,$tuname,$tudom,$tcrsid)=@_;
220: unless ($token) { return ''; }
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: }
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:
239: if ($target eq 'web') {
240: return
241: '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
242: 'Checked out for '.$plainname.
243: '<br />User: '.$tuname.' at '.$tudom.
244: '<br />CourseID: '.$tcrsid.
245: '<br />DocID: '.$token.
246: '<br />Time: '.localtime().'<hr />';
247: } else {
248: return $token;
249: }
250: }
251:
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:
261: sub registerurl {
262: my $forcereg=shift;
263: if ($Apache::lonxml::registered) { return ''; }
264: $Apache::lonxml::registered=1;
265: if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
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',
272: 'gocmd("/adm/grades","submission")');
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',
279: 'gocmd("/adm/grades","viewgrades")');
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',
286: 'gocmd("/adm/parmset","set")');
287: ENDPARM
288: }
289: }
290: return (<<ENDREGTHIS);
291:
292: <script language="JavaScript">
293: // BEGIN LON-CAPA Internal
294:
295: function LONCAPAreg() {
296: menu=window.open("","LONCAPAmenu");
297: menu.clearTimeout(menu.menucltim);
298: menu.currentURL=window.location.pathname;
299: menu.currentStale=0;
300: menu.clearbut(3,1);
301: menu.switchbutton
302: (6,3,'catalog.gif','catalog','info','catalog_info()');
303: menu.switchbutton
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)');
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
318: (9,3,'anot.gif','anno-','tations','annotate()');
319: $hwkadd
320: }
321:
322: function LONCAPAstale() {
323: menu=window.open("","LONCAPAmenu");
324: menu.currentStale=1;
325: //menu.clearbut(7,1);
326: //menu.clearbut(7,2);
327: //menu.clearbut(7,3);
328: menu.menucltim=menu.setTimeout(
329: 'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
330: 'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
331: 2000);
332:
333: }
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() {
346: menu=window.open("","LONCAPAmenu");
347: menu.currentStale=1;
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);
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() {
362: }
363:
364: // END LON-CAPA Internal
365: </script>
366: ENDDONOTREGTHIS
367:
368: }
369: }
370:
371: sub loadevents() {
372: return 'LONCAPAreg();';
373: }
374:
375: sub unloadevents() {
376: return 'LONCAPAstale();';
377: }
378:
379: sub printalltags {
380: my $temp;
381: foreach $temp (sort keys %Apache::lonxml::alltags) {
382: &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
383: }
384: }
385:
386: sub xmlparse {
387: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
388:
389: &setup_globals($target);
390: #&printalltags();
391: my @pars = ();
392: my $pwd=$ENV{'request.filename'};
393: $pwd =~ s:/[^/]*$::;
394: &newparser(\@pars,\$content_file_string,$pwd);
395:
396: my $safeeval = new Safe;
397: my $safehole = new Safe::Hole;
398: &init_safespace($target,$safeeval,$safehole,$safeinit);
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 = ();
405: &initdepth;
406:
407: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
408: $safeeval,\%style_for_target);
409:
410: return $finaloutput;
411: }
412:
413: sub htmlclean {
414: my ($raw,$full)=@_;
415:
416: my $tree = HTML::TreeBuilder->new;
417: $tree->ignore_unknown(0);
418:
419: $tree->parse($raw);
420:
421: my $output= $tree->as_HTML(undef,' ');
422:
423: $output=~s/\<(br|hr|img|meta|allow)([^\>\/]*)\>/\<$1$2 \/\>/gis;
424: $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
425: unless ($full) {
426: $output=~s/\<[\/]*(body|head|html)\>//gis;
427: }
428:
429: $tree = $tree->delete;
430:
431: return $output;
432: }
433:
434: sub inner_xmlparse {
435: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
436: my $finaloutput = '';
437: my $result;
438: my $token;
439: while ( $#$pars > -1 ) {
440: while ($token = $$pars['-1']->get_token) {
441: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
442: if ($metamode<1) {
443: $result=$token->[1];
444: }
445: } elsif ($token->[0] eq 'PI') {
446: if ($metamode<1) {
447: $result=$token->[2];
448: }
449: } elsif ($token->[0] eq 'S') {
450: # add tag to stack
451: push (@$stack,$token->[1]);
452: # add parameters list to another stack
453: push (@$parstack,&parstring($token));
454: &increasedepth($token);
455: if (exists $$style_for_target{$token->[1]}) {
456: if ($Apache::lonxml::redirection) {
457: $Apache::lonxml::outputstack['-1'] .=
458: &recurse($$style_for_target{$token->[1]},$target,$safeeval,
459: $style_for_target,@$parstack);
460: } else {
461: $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
462: $safeeval,$style_for_target,@$parstack);
463: }
464: } else {
465: $result = &callsub("start_$token->[1]", $target, $token, $stack,
466: $parstack, $pars, $safeeval, $style_for_target);
467: }
468: } elsif ($token->[0] eq 'E') {
469: #clear out any tags that didn't end
470: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
471: &Apache::lonxml::warning("Unbalanced tags in resource $$stack['-1']");
472: &end_tag($stack,$parstack,$token);
473: }
474:
475: if (exists $$style_for_target{'/'."$token->[1]"}) {
476: if ($Apache::lonxml::redirection) {
477: $Apache::lonxml::outputstack['-1'] .=
478: &recurse($$style_for_target{'/'."$token->[1]"},
479: $target,$safeeval,$style_for_target,@$parstack);
480: } else {
481: $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
482: $target,$safeeval,$style_for_target,
483: @$parstack);
484: }
485:
486: } else {
487: $result = &callsub("end_$token->[1]", $target, $token, $stack,
488: $parstack, $pars,$safeeval, $style_for_target);
489: }
490: } else {
491: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
492: }
493: #evaluate variable refs in result
494: if ($result ne "") {
495: if ( $#$parstack > -1 ) {
496: if ($Apache::lonxml::redirection) {
497: $Apache::lonxml::outputstack['-1'] .=
498: &Apache::run::evaluate($result,$safeeval,$$parstack['-1']);
499: } else {
500: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
501: $$parstack['-1']);
502: }
503: } else {
504: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
505: }
506: $result = '';
507: }
508: if ($token->[0] eq 'E') {
509: &end_tag($stack,$parstack,$token);
510: }
511: }
512: pop @$pars;
513: pop @Apache::lonxml::pwd;
514: }
515:
516: # if ($target eq 'meta') {
517: # $finaloutput.=&endredirection;
518: # }
519:
520: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
521: $finaloutput=&afterburn($finaloutput);
522: }
523: return $finaloutput;
524: }
525:
526: sub recurse {
527: my @innerstack = ();
528: my @innerparstack = ();
529: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
530: my @pat = ();
531: &newparser(\@pat,\$newarg);
532: my $tokenpat;
533: my $partstring = '';
534: my $output='';
535: my $decls='';
536: while ( $#pat > -1 ) {
537: while ($tokenpat = $pat[$#pat]->get_token) {
538: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
539: if ($metamode<1) { $partstring=$tokenpat->[1]; }
540: } elsif ($tokenpat->[0] eq 'PI') {
541: if ($metamode<1) { $partstring=$tokenpat->[2]; }
542: } elsif ($tokenpat->[0] eq 'S') {
543: push (@innerstack,$tokenpat->[1]);
544: push (@innerparstack,&parstring($tokenpat));
545: &increasedepth($tokenpat);
546: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
547: \@innerstack, \@innerparstack, \@pat,
548: $safeeval, $style_for_target);
549: } elsif ($tokenpat->[0] eq 'E') {
550: #clear out any tags that didn't end
551: while ($tokenpat->[1] ne $innerstack[$#innerstack]
552: && ($#innerstack > -1)) {
553: &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
554: &end_tag(\@innerstack,\@innerparstack,$tokenpat);
555: }
556: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
557: \@innerstack, \@innerparstack, \@pat,
558: $safeeval, $style_for_target);
559: } else {
560: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
561: }
562: #pass both the variable to the style tag, and the tag we
563: #are processing inside the <definedtag>
564: if ( $partstring ne "" ) {
565: if ( $#parstack > -1 ) {
566: if ( $#innerparstack > -1 ) {
567: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
568: } else {
569: $decls= $parstack[$#parstack];
570: }
571: } else {
572: if ( $#innerparstack > -1 ) {
573: $decls=$innerparstack[$#innerparstack];
574: } else {
575: $decls='';
576: }
577: }
578: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
579: $partstring = '';
580: }
581: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
582: &decreasedepth($tokenpat);}
583: }
584: pop @pat;
585: pop @Apache::lonxml::pwd;
586: }
587: return $output;
588: }
589:
590: sub callsub {
591: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
592: my $currentstring='';
593: my $nodefault;
594: {
595: my $sub1;
596: no strict 'refs';
597: my $tag=$token->[1];
598: my $space=$Apache::lonxml::alltags{$tag};
599: if (!$space) {
600: $tag=~tr/A-Z/a-z/;
601: $sub=~tr/A-Z/a-z/;
602: $space=$Apache::lonxml::alltags{$tag}
603: }
604:
605: my $deleted=0;
606: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
607: if (($token->[0] eq 'S') && ($target eq 'modified')) {
608: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
609: $parstack,$parser,$safeeval,
610: $style);
611: }
612: if (!$deleted) {
613: if ($space) {
614: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
615: $sub1="$space\:\:$sub";
616: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
617: $parstack,$parser,$safeeval,
618: $style);
619: } else {
620: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
621: if ($metamode <1) {
622: if (defined($token->[4]) && ($metamode < 1)) {
623: $currentstring = $token->[4];
624: } else {
625: $currentstring = $token->[2];
626: }
627: }
628: }
629: # &Apache::lonxml::debug("nodefalt:$nodefault:");
630: if ($currentstring eq '' && $nodefault eq '') {
631: if ($target eq 'edit') {
632: &Apache::lonxml::debug("doing default edit for $token->[1]");
633: if ($token->[0] eq 'S') {
634: $currentstring = &Apache::edit::tag_start($target,$token);
635: } elsif ($token->[0] eq 'E') {
636: $currentstring = &Apache::edit::tag_end($target,$token);
637: }
638: } elsif ($target eq 'modified') {
639: if ($token->[0] eq 'S') {
640: $currentstring = $token->[4];
641: $currentstring.=&Apache::edit::handle_insert();
642: } else {
643: $currentstring = $token->[2];
644: }
645: }
646: }
647: }
648: use strict 'refs';
649: }
650: return $currentstring;
651: }
652:
653: sub setup_globals {
654: my ($target)=@_;
655: $Apache::lonxml::registered = 0;
656: @Apache::lonxml::pwd=();
657: @Apache::lonxml::extlinks=();
658: if ($target eq 'meta') {
659: $Apache::lonxml::redirection = 0;
660: $Apache::lonxml::metamode = 1;
661: $Apache::lonxml::evaluate = 1;
662: $Apache::lonxml::import = 0;
663: } elsif ($target eq 'grade') {
664: &startredirection;
665: $Apache::lonxml::metamode = 0;
666: $Apache::lonxml::evaluate = 1;
667: $Apache::lonxml::import = 1;
668: } elsif ($target eq 'modified') {
669: $Apache::lonxml::redirection = 0;
670: $Apache::lonxml::metamode = 0;
671: $Apache::lonxml::evaluate = 0;
672: $Apache::lonxml::import = 0;
673: } elsif ($target eq 'edit') {
674: $Apache::lonxml::redirection = 0;
675: $Apache::lonxml::metamode = 0;
676: $Apache::lonxml::evaluate = 0;
677: $Apache::lonxml::import = 0;
678: } else {
679: $Apache::lonxml::redirection = 0;
680: $Apache::lonxml::metamode = 0;
681: $Apache::lonxml::evaluate = 1;
682: $Apache::lonxml::import = 1;
683: }
684: }
685:
686: sub init_safespace {
687: my ($target,$safeeval,$safehole,$safeinit) = @_;
688: $safeeval->permit("entereval");
689: $safeeval->permit(":base_math");
690: $safeeval->permit("sort");
691: $safeeval->deny(":base_io");
692: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
693: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
694:
695: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
696: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
697: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
698: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
699: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
700: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
701: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
702: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
703: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
704: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
705: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
706: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
707: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
708: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
709: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
710: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
711: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
712: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
713: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
714: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
715: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
716: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
717: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
718: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
719: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
720: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
721: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
722: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
723: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
724: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
725: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
726: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
727: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
728: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
729: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
730: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
731: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
732: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
733: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
734: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
735:
736: #need to inspect this class of ops
737: # $safeeval->deny(":base_orig");
738: $safeinit .= ';$external::target="'.$target.'";';
739: my $rndseed;
740: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
741: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
742: $safeinit .= ';$external::randomseed='.$rndseed.';';
743: &Apache::run::run($safeinit,$safeeval);
744: }
745:
746: sub startredirection {
747: $Apache::lonxml::redirection++;
748: push (@Apache::lonxml::outputstack, '');
749: }
750:
751: sub endredirection {
752: if (!$Apache::lonxml::redirection) {
753: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
754: return '';
755: }
756: $Apache::lonxml::redirection--;
757: pop @Apache::lonxml::outputstack;
758: }
759:
760: sub end_tag {
761: my ($tagstack,$parstack,$token)=@_;
762: pop(@$tagstack);
763: pop(@$parstack);
764: &decreasedepth($token);
765: }
766:
767: sub initdepth {
768: @Apache::lonxml::depthcounter=();
769: $Apache::lonxml::depth=-1;
770: $Apache::lonxml::olddepth=-1;
771: }
772:
773: sub increasedepth {
774: my ($token) = @_;
775: $Apache::lonxml::depth++;
776: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
777: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
778: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
779: }
780: my $curdepth=join('_',@Apache::lonxml::depthcounter);
781: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
782: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
783: }
784:
785: sub decreasedepth {
786: my ($token) = @_;
787: $Apache::lonxml::depth--;
788: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
789: $#Apache::lonxml::depthcounter--;
790: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
791: }
792: if ( $Apache::lonxml::depth < -1) {
793: &Apache::lonxml::warning("Unbalanced tags in resource");
794: $Apache::lonxml::depth='-1';
795: }
796: my $curdepth=join('_',@Apache::lonxml::depthcounter);
797: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
798: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
799: }
800:
801: sub get_all_text {
802:
803: my($tag,$pars)= @_;
804: my $depth=0;
805: my $token;
806: my $result='';
807: if ( $tag =~ m:^/: ) {
808: my $tag=substr($tag,1);
809: # &Apache::lonxml::debug("have:$tag:");
810: while (($depth >=0) && ($token = $pars->get_token)) {
811: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
812: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
813: $result.=$token->[1];
814: } elsif ($token->[0] eq 'PI') {
815: $result.=$token->[2];
816: } elsif ($token->[0] eq 'S') {
817: if ($token->[1] eq $tag) { $depth++; }
818: $result.=$token->[4];
819: } elsif ($token->[0] eq 'E') {
820: if ( $token->[1] eq $tag) { $depth--; }
821: #skip sending back the last end tag
822: if ($depth > -1) { $result.=$token->[2]; } else {
823: $pars->unget_token($token);
824: }
825: }
826: }
827: } else {
828: while ($token = $pars->get_token) {
829: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
830: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
831: $result.=$token->[1];
832: } elsif ($token->[0] eq 'PI') {
833: $result.=$token->[2];
834: } elsif ($token->[0] eq 'S') {
835: if ( $token->[1] eq $tag) {
836: $pars->unget_token($token); last;
837: } else {
838: $result.=$token->[4];
839: }
840: } elsif ($token->[0] eq 'E') {
841: $result.=$token->[2];
842: }
843: }
844: }
845: # &Apache::lonxml::debug("Exit:$result:");
846: return $result
847: }
848:
849: sub newparser {
850: my ($parser,$contentref,$dir) = @_;
851: push (@$parser,HTML::TokeParser->new($contentref));
852: $$parser['-1']->xml_mode('1');
853: if ( $dir eq '' ) {
854: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
855: } else {
856: push (@Apache::lonxml::pwd, $dir);
857: }
858: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
859: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
860: }
861:
862: sub parstring {
863: my ($token) = @_;
864: my $temp='';
865: map {
866: unless ($_=~/\W/) {
867: my $val=$token->[2]->{$_};
868: $val =~ s/([\%\@\\])/\\$1/g;
869: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
870: $temp .= "my \$$_=\"$val\";"
871: }
872: } @{$token->[3]};
873: return $temp;
874: }
875:
876: sub writeallows {
877: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
878: if ($ENV{'httpref.'.$thisurl}) {
879: $thisurl=$ENV{'httpref.'.$thisurl};
880: }
881: my $thisdir=$thisurl;
882: $thisdir=~s/\/[^\/]+$//;
883: my %httpref=();
884: map {
885: $httpref{'httpref.'.
886: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
887: &Apache::lonnet::appenv(%httpref);
888: }
889:
890: #
891: # Afterburner handles anchors, highlights and links
892: #
893: sub afterburn {
894: my $result=shift;
895: map {
896: my ($name, $value) = split(/=/,$_);
897: $value =~ tr/+/ /;
898: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
899: if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
900: unless ($ENV{'form.'.$name}) {
901: $ENV{'form.'.$name}=$value;
902: }
903: }
904: } (split(/&/,$ENV{'QUERY_STRING'}));
905: if ($ENV{'form.highlight'}) {
906: map {
907: my $anchorname=$_;
908: my $matchthis=$anchorname;
909: $matchthis=~s/\_+/\\s\+/g;
910: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
911: } split(/\,/,$ENV{'form.highlight'});
912: }
913: if ($ENV{'form.link'}) {
914: map {
915: my ($anchorname,$linkurl)=split(/\>/,$_);
916: my $matchthis=$anchorname;
917: $matchthis=~s/\_+/\\s\+/g;
918: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
919: } split(/\,/,$ENV{'form.link'});
920: }
921: if ($ENV{'form.anchor'}) {
922: my $anchorname=$ENV{'form.anchor'};
923: my $matchthis=$anchorname;
924: $matchthis=~s/\_+/\\s\+/g;
925: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
926: $result.=(<<"ENDSCRIPT");
927: <script>
928: document.location.hash='$anchorname';
929: </script>
930: ENDSCRIPT
931: }
932: return $result;
933: }
934:
935: sub storefile {
936: my ($file,$contents)=@_;
937: if (my $fh=Apache::File->new('>'.$file)) {
938: print $fh $contents;
939: $fh->close();
940: }
941: }
942:
943: sub inserteditinfo {
944: my ($result,$filecontents)=@_;
945: unless ($filecontents) {
946: $filecontents=(<<SIMPLECONTENT);
947: <html>
948: <head>
949: <title>
950: Title of Document Goes Here
951: </title>
952: </head>
953: <body bgcolor="#FFFFFF">
954:
955: Body of Document Goes Here
956:
957: </body>
958: </html>
959: SIMPLECONTENT
960: }
961: my $editheader='<a href="#editsection">Edit below</a><hr />';
962: my $editfooter=(<<ENDFOOTER);
963: <hr />
964: <a name="editsection" />
965: <form method="post">
966: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
967: <br />
968: <input type="submit" name="attemptclean"
969: value="Save and then attempt to clean HTML" />
970: <input type="submit" name="savethisfile" value="Save this" />
971: </form>
972: ENDFOOTER
973: $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
974: $result=~s/(\<\/body\>)/$editfooter/is;
975: return $result;
976: }
977:
978: sub handler {
979: my $request=shift;
980:
981: my $target='web';
982:
983: $Apache::lonxml::debug=0;
984:
985: if ($ENV{'browser.mathml'}) {
986: $request->content_type('text/xml');
987: } else {
988: $request->content_type('text/html');
989: }
990:
991: $request->send_http_header;
992:
993: return OK if $request->header_only;
994:
995:
996: my $file=&Apache::lonnet::filelocation("",$request->uri);
997: #
998: # Edit action? Save file.
999: #
1000: unless ($ENV{'request.state'} eq 'published') {
1001: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1002: &storefile($file,$ENV{'form.filecont'});
1003: }
1004: }
1005: my %mystyle;
1006: my $result = '';
1007: my $filecontents=&Apache::lonnet::getfile($file);
1008: if ($filecontents == -1) {
1009: $result=(<<ENDNOTFOUND);
1010: <html>
1011: <head>
1012: <title>File not found</title>
1013: </head>
1014: <body bgcolor="#FFFFFF">
1015: <b>File not found: $file</b>
1016: </body>
1017: </html>
1018: ENDNOTFOUND
1019: $filecontents='';
1020: } else {
1021: unless ($ENV{'request.state'} eq 'published') {
1022: if ($ENV{'form.attemptclean'}) {
1023: $filecontents=&htmlclean($filecontents,1);
1024: }
1025: }
1026: $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1027: }
1028:
1029: #
1030: # Edit action? Insert editing commands
1031: #
1032: unless ($ENV{'request.state'} eq 'published') {
1033: $result=&inserteditinfo($result,$filecontents);
1034: }
1035:
1036: $request->print($result);
1037:
1038: writeallows($request->uri);
1039: return OK;
1040: }
1041:
1042: sub debug {
1043: if ($Apache::lonxml::debug eq 1) {
1044: print("DEBUG:".$_[0]."<br />\n");
1045: }
1046: }
1047:
1048: sub error {
1049: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1050: print "<b>ERROR:</b>".$_[0]."<br />\n";
1051: } else {
1052: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1053: #notify author
1054: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
1055: #notify course
1056: if ( $ENV{'request.course.id'} ) {
1057: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1058: foreach my $user (split /\,/, $users) {
1059: ($user,my $domain) = split /:/, $user;
1060: &Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1061: }
1062: }
1063:
1064: #FIXME probably shouldn't have me get everything forever.
1065: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
1066: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1067: }
1068: }
1069:
1070: sub warning {
1071: if ($ENV{'request.state'} eq 'construct') {
1072: print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1073: }
1074: }
1075:
1076: sub get_param {
1077: my ($param,$parstack,$safeeval,$context) = @_;
1078: if ( ! $context ) { $context = -1; }
1079: my $args ='';
1080: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1081: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1082: }
1083:
1084: sub register_insert {
1085: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1086: my $i;
1087: my $tagnum=0;
1088: my @order;
1089: for ($i=0;$i < $#data; $i++) {
1090: my $line = $data[$i];
1091: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1092: if ( $line =~ /TABLE/ ) { last; }
1093: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1094: $insertlist{"$tagnum.tag"} = $tag;
1095: $insertlist{"$tagnum.description"} = $descrip;
1096: $insertlist{"$tagnum.color"} = $color;
1097: $insertlist{"$tagnum.function"} = $function;
1098: $insertlist{"$tagnum.show"}= $show;
1099: $insertlist{"$tag.num"}=$tagnum;
1100: $tagnum++;
1101: }
1102: $i++; #skipping TABLE line
1103: $tagnum = 0;
1104: for (;$i < $#data;$i++) {
1105: my $line = $data[$i];
1106: my ($mnemonic,@which) = split(/ +/,$line);
1107: my $tag = $insertlist{"$tagnum.tag"};
1108: for (my $j=0;$j <$#which;$j++) {
1109: if ( $which[$j] eq 'Y' ) {
1110: if ($insertlist{"$j.show"} ne 'no') {
1111: push(@{ $insertlist{"$tag.which"} },$j);
1112: }
1113: }
1114: }
1115: $tagnum++;
1116: }
1117: }
1118:
1119: sub description {
1120: my ($token)=@_;
1121: return $insertlist{$insertlist{"$token->[1].num"}.'.description'};
1122: }
1123:
1124: # ----------------------------------------------------------------- whichuser
1125: # returns a list of $symb, $courseid, $domain, $name that is correct for
1126: # calls to lonnet functions for this setup.
1127: # - looks for form.grade_ parameters
1128: sub whichuser {
1129: my $symb=&Apache::lonnet::symbread();
1130: my $courseid=$ENV{'request.course.id'};
1131: my $domain=$ENV{'user.domain'};
1132: my $name=$ENV{'user.name'};
1133: if (defined($ENV{'form.grade_symb'})) {
1134: my $tmp_courseid=$ENV{'form.grade_courseid'};
1135: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1136: if ($allowed) {
1137: $symb=$ENV{'form.grade_symb'};
1138: $courseid=$ENV{'form.grade_courseid'};
1139: $domain=$ENV{'form.grade_domain'};
1140: $name=$ENV{'form.grade_username'};
1141: }
1142: }
1143: return ($symb,$courseid,$domain,$name);
1144: }
1145:
1146: 1;
1147: __END__
1148:
1149:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>