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