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