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