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