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: return (<<ENDINPUTFIELD)
158: <form method="post">
159: <table border="2" bgcolor="#FFFFBB">
160: <tr><th>DocID Checkin</th></tr>
161: <tr><td>
162: <table>
163: <tr>
164: <td>Scan in Barcode</td>
165: <td><input type="text" size="22" name="barcode" /></td>
166: </tr>
167: <tr><td><i>or</i> Type in DocID</td>
168: <td>
169: <input type="text" size="5" name="codeone" />
170: <b><font size+"+2"*</font></b>
171: <input type="text" size="5" name="codetwo" />
172: <b><font size+"+2"*</font></b>
173: <input type="text" size="10" name="codethree" value="$defhost" />
174: </td></tr>
175: </table>
176: </td></tr>
177: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
178: </table>
179: </form>
180: ENDINPUTFIELD
181: }
182:
183: sub maketoken {
184: my ($symb,$tuname,$tudom,$tcrsid)=@_;
185: unless ($symb) {
186: $symb=&Apache::lonnet::symbread();
187: }
188: unless ($tuname) {
189: $tuname=$ENV{'user.name'};
190: $tudom=$ENV{'user.domain'};
191: $tcrsid=$ENV{'request.course.id'};
192: }
193:
194: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
195: }
196:
197: sub printtokenheader {
198: my ($target,$token,$symb,$tuname,$tudom,$tcrsid)=@_;
199: unless ($token) { return ''; }
200:
201: unless ($symb) {
202: $symb=&Apache::lonnet::symbread();
203: }
204: unless ($tuname) {
205: $tuname=$ENV{'user.name'};
206: $tudom=$ENV{'user.domain'};
207: $tcrsid=$ENV{'request.course.id'};
208: }
209:
210: my %reply=&Apache::lonnet::get('environment',
211: ['firstname','middlename','lastname','generation'],
212: $tudom,$tuname);
213: my $plainname=$reply{'firstname'}.' '.
214: $reply{'middlename'}.' '.
215: $reply{'lastname'}.' '.
216: $reply{'generation'};
217:
218: if ($target eq 'web') {
219: return
220: '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
221: 'Checked out for '.$plainname.
222: '<br />User: '.$tuname.' at '.$tudom.
223: '<br />CourseID: '.$tcrsid.
224: '<br />DocID: '.$token.
225: '<br />Time: '.localtime().'<hr />';
226: } else {
227: return $token;
228: }
229: }
230:
231: sub fontsettings() {
232: my $headerstring='';
233: if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) {
234: $headerstring.=
235: '<meta Content-Type="text/html; charset=x-mac-roman">';
236: }
237: return $headerstring;
238: }
239:
240: sub registerurl {
241: my $forcereg=shift;
242: if ($Apache::lonxml::registered) { return ''; }
243: $Apache::lonxml::registered=1;
244: if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
245: my $hwkadd='';
246: if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
247: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
248: $hwkadd.=(<<ENDSUBM);
249: menu.switchbutton
250: (7,1,'subm.gif','view sub','missions',
251: 'gocmd("/adm/grades","submission")');
252: ENDSUBM
253: }
254: if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
255: $hwkadd.=(<<ENDGRDS);
256: menu.switchbutton
257: (7,2,'pgrd.gif','problem','grades',
258: 'gocmd("/adm/grades","viewgrades")');
259: ENDGRDS
260: }
261: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
262: $hwkadd.=(<<ENDPARM);
263: menu.switchbutton
264: (7,3,'pparm.gif','problem','parms',
265: 'gocmd("/adm/parmset","set")');
266: ENDPARM
267: }
268: }
269: return (<<ENDREGTHIS);
270:
271: <script language="JavaScript">
272: // BEGIN LON-CAPA Internal
273:
274: function LONCAPAreg() {
275: menu=window.open("","LONCAPAmenu");
276: menu.clearTimeout(menu.menucltim);
277: menu.currentURL=window.location.pathname;
278: menu.currentStale=0;
279: menu.clearbut(3,1);
280: menu.switchbutton
281: (6,3,'catalog.gif','catalog','info','catalog_info()');
282: menu.switchbutton
283: (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
284: menu.switchbutton
285: (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
286: menu.switchbutton
287: (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
288: menu.switchbutton
289: (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
290: menu.switchbutton
291: (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
292: menu.switchbutton
293: (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
294: menu.switchbutton
295: (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
296: menu.switchbutton
297: (9,3,'anot.gif','anno-','tations','annotate()');
298: $hwkadd
299: }
300:
301: function LONCAPAstale() {
302: menu=window.open("","LONCAPAmenu");
303: menu.currentStale=1;
304: menu.switchbutton
305: (3,1,'reload.gif','return','location','go(currentURL)');
306: menu.clearbut(7,1);
307: menu.clearbut(7,2);
308: menu.clearbut(7,3);
309: menu.menucltim=menu.setTimeout(
310: 'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
311: 'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
312: 2000);
313:
314: }
315:
316: // END LON-CAPA Internal
317: </script>
318: ENDREGTHIS
319:
320: } else {
321: return (<<ENDDONOTREGTHIS);
322:
323: <script language="JavaScript">
324: // BEGIN LON-CAPA Internal
325:
326: function LONCAPAreg() {
327: menu=window.open("","LONCAPAmenu");
328: menu.currentStale=1;
329: menu.clearbut(2,1);
330: menu.clearbut(2,3);
331: menu.clearbut(8,1);
332: menu.clearbut(8,2);
333: menu.clearbut(8,3);
334: if (menu.currentURL) {
335: menu.switchbutton
336: (3,1,'reload.gif','return','location','go(currentURL)');
337: } else {
338: menu.clearbut(3,1);
339: }
340: }
341:
342: function LONCAPAstale() {
343: }
344:
345: // END LON-CAPA Internal
346: </script>
347: ENDDONOTREGTHIS
348:
349: }
350: }
351:
352: sub loadevents() {
353: return 'LONCAPAreg();';
354: }
355:
356: sub unloadevents() {
357: return 'LONCAPAstale();';
358: }
359:
360: sub printalltags {
361: my $temp;
362: foreach $temp (sort keys %Apache::lonxml::alltags) {
363: &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
364: }
365: }
366:
367: sub xmlparse {
368: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
369:
370: &setup_globals($target);
371: #&printalltags();
372: my @pars = ();
373: my $pwd=$ENV{'request.filename'};
374: $pwd =~ s:/[^/]*$::;
375: &newparser(\@pars,\$content_file_string,$pwd);
376:
377: my $safeeval = new Safe;
378: my $safehole = new Safe::Hole;
379: &init_safespace($target,$safeeval,$safehole,$safeinit);
380: #-------------------- Redefinition of the target in the case of compound target
381:
382: ($target, my @tenta) = split('&&',$target);
383:
384: my @stack = ();
385: my @parstack = ();
386: &initdepth;
387:
388: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
389: $safeeval,\%style_for_target);
390:
391: return $finaloutput;
392: }
393:
394: sub htmlclean {
395: my ($raw,$full)=@_;
396:
397: my $tree = HTML::TreeBuilder->new;
398: $tree->ignore_unknown(0);
399:
400: $tree->parse($raw);
401:
402: my $output= $tree->as_HTML(undef,' ');
403:
404: $output=~s/\<(br|hr|img|meta|allow)([^\>\/]*)\>/\<$1$2 \/\>/gis;
405: $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
406: unless ($full) {
407: $output=~s/\<[\/]*(body|head|html)\>//gis;
408: }
409:
410: $tree = $tree->delete;
411:
412: return $output;
413: }
414:
415: sub inner_xmlparse {
416: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
417: &Apache::lonxml::debug('Reentrant parser starting, again?');
418: my $finaloutput = '';
419: my $result;
420: my $token;
421: while ( $#$pars > -1 ) {
422: while ($token = $$pars['-1']->get_token) {
423: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
424: if ($metamode<1) {
425: $result=$token->[1];
426: }
427: } elsif ($token->[0] eq 'PI') {
428: if ($metamode<1) {
429: $result=$token->[2];
430: }
431: } elsif ($token->[0] eq 'S') {
432: # add tag to stack
433: push (@$stack,$token->[1]);
434: # add parameters list to another stack
435: push (@$parstack,&parstring($token));
436: &increasedepth($token);
437: if (exists $$style_for_target{$token->[1]}) {
438: if ($Apache::lonxml::redirection) {
439: $Apache::lonxml::outputstack['-1'] .=
440: &recurse($$style_for_target{$token->[1]},$target,$safeeval,
441: $style_for_target,@$parstack);
442: } else {
443: $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
444: $safeeval,$style_for_target,@$parstack);
445: }
446: } else {
447: $result = &callsub("start_$token->[1]", $target, $token, $stack,
448: $parstack, $pars, $safeeval, $style_for_target);
449: }
450: } elsif ($token->[0] eq 'E') {
451: #clear out any tags that didn't end
452: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
453: &Apache::lonxml::warning("Unbalanced tags in resource $$stack['-1']");
454: &end_tag($stack,$parstack,$token);
455: }
456:
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]"},
461: $target,$safeeval,$style_for_target,@$parstack);
462: } else {
463: $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
464: $target,$safeeval,$style_for_target,
465: @$parstack);
466: }
467:
468: } else {
469: $result = &callsub("end_$token->[1]", $target, $token, $stack,
470: $parstack, $pars,$safeeval, $style_for_target);
471: }
472: } else {
473: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
474: }
475: #evaluate variable refs in result
476: if ($result ne "") {
477: if ( $#$parstack > -1 ) {
478: if ($Apache::lonxml::redirection) {
479: $Apache::lonxml::outputstack['-1'] .=
480: &Apache::run::evaluate($result,$safeeval,$$parstack['-1']);
481: } else {
482: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
483: $$parstack['-1']);
484: }
485: } else {
486: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
487: }
488: $result = '';
489: }
490: if ($token->[0] eq 'E') {
491: &end_tag($stack,$parstack,$token);
492: }
493: }
494: pop @$pars;
495: pop @Apache::lonxml::pwd;
496: }
497:
498: # if ($target eq 'meta') {
499: # $finaloutput.=&endredirection;
500: # }
501:
502: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
503: $finaloutput=&afterburn($finaloutput);
504: }
505: return $finaloutput;
506: }
507:
508: sub recurse {
509: my @innerstack = ();
510: my @innerparstack = ();
511: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
512: my @pat = ();
513: &newparser(\@pat,\$newarg);
514: my $tokenpat;
515: my $partstring = '';
516: my $output='';
517: my $decls='';
518: while ( $#pat > -1 ) {
519: while ($tokenpat = $pat[$#pat]->get_token) {
520: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
521: if ($metamode<1) { $partstring=$tokenpat->[1]; }
522: } elsif ($tokenpat->[0] eq 'PI') {
523: if ($metamode<1) { $partstring=$tokenpat->[2]; }
524: } elsif ($tokenpat->[0] eq 'S') {
525: push (@innerstack,$tokenpat->[1]);
526: push (@innerparstack,&parstring($tokenpat));
527: &increasedepth($tokenpat);
528: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
529: \@innerstack, \@innerparstack, \@pat,
530: $safeeval, $style_for_target);
531: } elsif ($tokenpat->[0] eq 'E') {
532: #clear out any tags that didn't end
533: while ($tokenpat->[1] ne $innerstack[$#innerstack]
534: && ($#innerstack > -1)) {
535: &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
536: &end_tag(\@innerstack,\@innerparstack,$tokenpat);
537: }
538: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
539: \@innerstack, \@innerparstack, \@pat,
540: $safeeval, $style_for_target);
541: } else {
542: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
543: }
544: #pass both the variable to the style tag, and the tag we
545: #are processing inside the <definedtag>
546: if ( $partstring ne "" ) {
547: if ( $#parstack > -1 ) {
548: if ( $#innerparstack > -1 ) {
549: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
550: } else {
551: $decls= $parstack[$#parstack];
552: }
553: } else {
554: if ( $#innerparstack > -1 ) {
555: $decls=$innerparstack[$#innerparstack];
556: } else {
557: $decls='';
558: }
559: }
560: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
561: $partstring = '';
562: }
563: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
564: &decreasedepth($tokenpat);}
565: }
566: pop @pat;
567: pop @Apache::lonxml::pwd;
568: }
569: return $output;
570: }
571:
572: sub callsub {
573: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
574: my $currentstring='';
575: my $nodefault;
576: {
577: my $sub1;
578: no strict 'refs';
579: my $tag=$token->[1];
580: my $space=$Apache::lonxml::alltags{$tag};
581: if (!$space) {
582: $tag=~tr/A-Z/a-z/;
583: $sub=~tr/A-Z/a-z/;
584: $space=$Apache::lonxml::alltags{$tag}
585: }
586:
587: my $deleted=0;
588: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
589: if (($token->[0] eq 'S') && ($target eq 'modified')) {
590: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
591: $parstack,$parser,$safeeval,
592: $style);
593: }
594: if (!$deleted) {
595: if ($space) {
596: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
597: $sub1="$space\:\:$sub";
598: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
599: $parstack,$parser,$safeeval,
600: $style);
601: } else {
602: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
603: if ($metamode <1) {
604: if (defined($token->[4]) && ($metamode < 1)) {
605: $currentstring = $token->[4];
606: } else {
607: $currentstring = $token->[2];
608: }
609: }
610: }
611: # &Apache::lonxml::debug("nodefalt:$nodefault:");
612: if ($currentstring eq '' && $nodefault eq '') {
613: if ($target eq 'edit') {
614: &Apache::lonxml::debug("doing default edit for $token->[1]");
615: if ($token->[0] eq 'S') {
616: $currentstring = &Apache::edit::tag_start($target,$token);
617: } elsif ($token->[0] eq 'E') {
618: $currentstring = &Apache::edit::tag_end($target,$token);
619: }
620: } elsif ($target eq 'modified') {
621: if ($token->[0] eq 'S') {
622: $currentstring = $token->[4];
623: $currentstring.=&Apache::edit::handle_insert();
624: } else {
625: $currentstring = $token->[2];
626: }
627: }
628: }
629: }
630: use strict 'refs';
631: }
632: return $currentstring;
633: }
634:
635: sub setup_globals {
636: my ($target)=@_;
637: $Apache::lonxml::registered = 0;
638: @Apache::lonxml::pwd=();
639: if ($target eq 'meta') {
640: $Apache::lonxml::redirection = 0;
641: $Apache::lonxml::metamode = 1;
642: $Apache::lonxml::evaluate = 1;
643: $Apache::lonxml::import = 0;
644: } elsif ($target eq 'grade') {
645: &startredirection;
646: $Apache::lonxml::metamode = 0;
647: $Apache::lonxml::evaluate = 1;
648: $Apache::lonxml::import = 1;
649: } elsif ($target eq 'modified') {
650: $Apache::lonxml::redirection = 0;
651: $Apache::lonxml::metamode = 0;
652: $Apache::lonxml::evaluate = 0;
653: $Apache::lonxml::import = 0;
654: } elsif ($target eq 'edit') {
655: $Apache::lonxml::redirection = 0;
656: $Apache::lonxml::metamode = 0;
657: $Apache::lonxml::evaluate = 0;
658: $Apache::lonxml::import = 0;
659: } else {
660: $Apache::lonxml::redirection = 0;
661: $Apache::lonxml::metamode = 0;
662: $Apache::lonxml::evaluate = 1;
663: $Apache::lonxml::import = 1;
664: }
665: }
666:
667: sub init_safespace {
668: my ($target,$safeeval,$safehole,$safeinit) = @_;
669: $safeeval->permit("entereval");
670: $safeeval->permit(":base_math");
671: $safeeval->permit("sort");
672: $safeeval->deny(":base_io");
673: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
674: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
675:
676: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
677: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
678: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
679: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
680: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
681: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
682: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
683: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
684: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
685: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
686: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
687: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
688: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
689: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
690: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
691: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
692: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
693: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
694: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
695: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
696: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
697: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
698: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
699: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
700: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
701: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
702: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
703: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
704: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
705: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
706: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
707: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
708: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
709: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
710: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
711: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
712: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
713: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
714: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
715: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
716:
717: #need to inspect this class of ops
718: # $safeeval->deny(":base_orig");
719: $safeinit .= ';$external::target="'.$target.'";';
720: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
721: &Apache::run::run($safeinit,$safeeval);
722: }
723:
724: sub startredirection {
725: $Apache::lonxml::redirection++;
726: push (@Apache::lonxml::outputstack, '');
727: }
728:
729: sub endredirection {
730: if (!$Apache::lonxml::redirection) {
731: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
732: return '';
733: }
734: $Apache::lonxml::redirection--;
735: pop @Apache::lonxml::outputstack;
736: }
737:
738: sub end_tag {
739: my ($tagstack,$parstack,$token)=@_;
740: pop(@$tagstack);
741: pop(@$parstack);
742: &decreasedepth($token);
743: }
744:
745: sub initdepth {
746: @Apache::lonxml::depthcounter=();
747: $Apache::lonxml::depth=-1;
748: $Apache::lonxml::olddepth=-1;
749: }
750:
751: sub increasedepth {
752: my ($token) = @_;
753: $Apache::lonxml::depth++;
754: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
755: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
756: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
757: }
758: my $curdepth=join('_',@Apache::lonxml::depthcounter);
759: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
760: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
761: }
762:
763: sub decreasedepth {
764: my ($token) = @_;
765: $Apache::lonxml::depth--;
766: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
767: $#Apache::lonxml::depthcounter--;
768: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
769: }
770: if ( $Apache::lonxml::depth < -1) {
771: &Apache::lonxml::warning("Unbalanced tags in resource");
772: $Apache::lonxml::depth='-1';
773: }
774: my $curdepth=join('_',@Apache::lonxml::depthcounter);
775: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
776: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
777: }
778:
779: sub get_all_text {
780:
781: my($tag,$pars)= @_;
782: my $depth=0;
783: my $token;
784: my $result='';
785: if ( $tag =~ m:^/: ) {
786: my $tag=substr($tag,1);
787: # &Apache::lonxml::debug("have:$tag:");
788: while (($depth >=0) && ($token = $pars->get_token)) {
789: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
790: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
791: $result.=$token->[1];
792: } elsif ($token->[0] eq 'PI') {
793: $result.=$token->[2];
794: } elsif ($token->[0] eq 'S') {
795: if ($token->[1] eq $tag) { $depth++; }
796: $result.=$token->[4];
797: } elsif ($token->[0] eq 'E') {
798: if ( $token->[1] eq $tag) { $depth--; }
799: #skip sending back the last end tag
800: if ($depth > -1) { $result.=$token->[2]; } else {
801: $pars->unget_token($token);
802: }
803: }
804: }
805: } else {
806: while ($token = $pars->get_token) {
807: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
808: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
809: $result.=$token->[1];
810: } elsif ($token->[0] eq 'PI') {
811: $result.=$token->[2];
812: } elsif ($token->[0] eq 'S') {
813: if ( $token->[1] eq $tag) {
814: $pars->unget_token($token); last;
815: } else {
816: $result.=$token->[4];
817: }
818: } elsif ($token->[0] eq 'E') {
819: $result.=$token->[2];
820: }
821: }
822: }
823: # &Apache::lonxml::debug("Exit:$result:");
824: return $result
825: }
826:
827: sub newparser {
828: my ($parser,$contentref,$dir) = @_;
829: push (@$parser,HTML::TokeParser->new($contentref));
830: $$parser['-1']->xml_mode('1');
831: if ( $dir eq '' ) {
832: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
833: } else {
834: push (@Apache::lonxml::pwd, $dir);
835: }
836: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
837: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
838: }
839:
840: sub parstring {
841: my ($token) = @_;
842: my $temp='';
843: map {
844: unless ($_=~/\W/) {
845: my $val=$token->[2]->{$_};
846: $val =~ s/([\%\@\\])/\\$1/g;
847: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
848: $temp .= "my \$$_=\"$val\";"
849: }
850: } @{$token->[3]};
851: return $temp;
852: }
853:
854: sub writeallows {
855: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
856: if ($ENV{'httpref.'.$thisurl}) {
857: $thisurl=$ENV{'httpref.'.$thisurl};
858: }
859: my $thisdir=$thisurl;
860: $thisdir=~s/\/[^\/]+$//;
861: my %httpref=();
862: map {
863: $httpref{'httpref.'.
864: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
865: &Apache::lonnet::appenv(%httpref);
866: }
867:
868: #
869: # Afterburner handles anchors, highlights and links
870: #
871: sub afterburn {
872: my $result=shift;
873: map {
874: my ($name, $value) = split(/=/,$_);
875: $value =~ tr/+/ /;
876: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
877: if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
878: unless ($ENV{'form.'.$name}) {
879: $ENV{'form.'.$name}=$value;
880: }
881: }
882: } (split(/&/,$ENV{'QUERY_STRING'}));
883: if ($ENV{'form.highlight'}) {
884: map {
885: my $anchorname=$_;
886: my $matchthis=$anchorname;
887: $matchthis=~s/\_+/\\s\+/g;
888: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
889: } split(/\,/,$ENV{'form.highlight'});
890: }
891: if ($ENV{'form.link'}) {
892: map {
893: my ($anchorname,$linkurl)=split(/\>/,$_);
894: my $matchthis=$anchorname;
895: $matchthis=~s/\_+/\\s\+/g;
896: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
897: } split(/\,/,$ENV{'form.link'});
898: }
899: if ($ENV{'form.anchor'}) {
900: my $anchorname=$ENV{'form.anchor'};
901: my $matchthis=$anchorname;
902: $matchthis=~s/\_+/\\s\+/g;
903: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
904: $result.=(<<"ENDSCRIPT");
905: <script>
906: document.location.hash='$anchorname';
907: </script>
908: ENDSCRIPT
909: }
910: return $result;
911: }
912:
913: sub storefile {
914: my ($file,$contents)=@_;
915: if (my $fh=Apache::File->new('>'.$file)) {
916: print $fh $contents;
917: $fh->close();
918: }
919: }
920:
921: sub inserteditinfo {
922: my ($result,$filecontents)=@_;
923: unless ($filecontents) {
924: $filecontents=(<<SIMPLECONTENT);
925: <html>
926: <head>
927: <title>
928: Title of Document Goes Here
929: </title>
930: </head>
931: <body bgcolor="#FFFFFF">
932:
933: Body of Document Goes Here
934:
935: </body>
936: </html>
937: SIMPLECONTENT
938: }
939: my $editheader='<a href="#editsection">Edit below</a><hr />';
940: my $editfooter=(<<ENDFOOTER);
941: <hr />
942: <a name="editsection" />
943: <form method="post">
944: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
945: <br />
946: <input type="submit" name="attemptclean"
947: value="Save and then attempt to clean HTML" />
948: <input type="submit" name="savethisfile" value="Save this" />
949: </form>
950: ENDFOOTER
951: $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
952: $result=~s/(\<\/body\>)/$editfooter/is;
953: return $result;
954: }
955:
956: sub handler {
957: my $request=shift;
958:
959: my $target='web';
960:
961: $Apache::lonxml::debug=0;
962:
963: if ($ENV{'browser.mathml'}) {
964: $request->content_type('text/xml');
965: } else {
966: $request->content_type('text/html');
967: }
968:
969: $request->send_http_header;
970:
971: return OK if $request->header_only;
972:
973:
974: my $file=&Apache::lonnet::filelocation("",$request->uri);
975: #
976: # Edit action? Save file.
977: #
978: unless ($ENV{'request.state'} eq 'published') {
979: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
980: &storefile($file,$ENV{'form.filecont'});
981: }
982: }
983: my %mystyle;
984: my $result = '';
985: my $filecontents=&Apache::lonnet::getfile($file);
986: if ($filecontents == -1) {
987: $result=(<<ENDNOTFOUND);
988: <html>
989: <head>
990: <title>File not found</title>
991: </head>
992: <body bgcolor="#FFFFFF">
993: <b>File not found: $file</b>
994: </body>
995: </html>
996: ENDNOTFOUND
997: $filecontents='';
998: } else {
999: unless ($ENV{'request.state'} eq 'published') {
1000: if ($ENV{'form.attemptclean'}) {
1001: $filecontents=&htmlclean($filecontents,1);
1002: }
1003: }
1004: $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1005: }
1006:
1007: #
1008: # Edit action? Insert editing commands
1009: #
1010: unless ($ENV{'request.state'} eq 'published') {
1011: $result=&inserteditinfo($result,$filecontents);
1012: }
1013:
1014: $request->print($result);
1015:
1016: writeallows($request->uri);
1017: return OK;
1018: }
1019:
1020: sub debug {
1021: if ($Apache::lonxml::debug eq 1) {
1022: print("DEBUG:".$_[0]."<br />\n");
1023: }
1024: }
1025:
1026: sub error {
1027: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1028: print "<b>ERROR:</b>".$_[0]."<br />\n";
1029: } else {
1030: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1031: #notify author
1032: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
1033: #notify course
1034: if ( $ENV{'request.course.id'} ) {
1035: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1036: foreach my $user (split /\,/, $users) {
1037: ($user,my $domain) = split /:/, $user;
1038: &Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1039: }
1040: }
1041:
1042: #FIXME probably shouldn't have me get everything forever.
1043: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
1044: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1045: }
1046: }
1047:
1048: sub warning {
1049: if ($ENV{'request.state'} eq 'construct') {
1050: print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1051: }
1052: }
1053:
1054: sub get_param {
1055: my ($param,$parstack,$safeeval,$context) = @_;
1056: if ( ! $context ) { $context = -1; }
1057: my $args ='';
1058: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1059: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1060: }
1061:
1062: sub register_insert {
1063: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1064: my $i;
1065: my $tagnum=0;
1066: my @order;
1067: for ($i=0;$i < $#data; $i++) {
1068: my $line = $data[$i];
1069: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1070: if ( $line =~ /TABLE/ ) { last; }
1071: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1072: $insertlist{"$tagnum.tag"} = $tag;
1073: $insertlist{"$tagnum.description"} = $descrip;
1074: $insertlist{"$tagnum.color"} = $color;
1075: $insertlist{"$tagnum.function"} = $function;
1076: $insertlist{"$tagnum.show"}= $show;
1077: $insertlist{"$tag.num"}=$tagnum;
1078: $tagnum++;
1079: }
1080: $i++; #skipping TABLE line
1081: $tagnum = 0;
1082: for (;$i < $#data;$i++) {
1083: my $line = $data[$i];
1084: my ($mnemonic,@which) = split(/ +/,$line);
1085: my $tag = $insertlist{"$tagnum.tag"};
1086: for (my $j=0;$j <$#which;$j++) {
1087: if ( $which[$j] eq 'Y' ) {
1088: if ($insertlist{"$j.show"} ne 'no') {
1089: push(@{ $insertlist{"$tag.which"} },$j);
1090: }
1091: }
1092: }
1093: $tagnum++;
1094: }
1095: }
1096:
1097: sub description {
1098: my ($token)=@_;
1099: return $insertlist{$insertlist{"$token->[1].num"}.'.description'};
1100: }
1101: 1;
1102: __END__
1103:
1104:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>