1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # $Id: lonxml.pm,v 1.196 2002/09/13 20:46:09 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
39: # last modified 06/26/00 by Alexander Sakharuk
40: # 11/6 Gerd Kortemeyer
41: # 6/1/1 Gerd Kortemeyer
42: # 2/21,3/13 Guy
43: # 3/29,5/4 Gerd Kortemeyer
44: # 5/10 Scott Harrison
45: # 5/26 Gerd Kortemeyer
46: # 5/27 H. K. Ng
47: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
48: # 6/12,6/13 H. K. Ng
49: # 6/16 Gerd Kortemeyer
50: # 7/27 H. K. Ng
51: # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20,8/23,8/24 Gerd Kortemeyer
52: # Guy Albertelli
53: # 9/26 Gerd Kortemeyer
54: # Dec Guy Albertelli
55: # YEAR=2002
56: # 1/1 Gerd Kortemeyer
57: # 1/2 Matthew Hall
58: # 1/3 Gerd Kortemeyer
59: #
60:
61: package Apache::lonxml;
62: use vars
63: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $prevent_entity_encode);
64: use strict;
65: use HTML::LCParser();
66: use HTML::TreeBuilder();
67: use HTML::Entities();
68: use Safe();
69: use Safe::Hole();
70: use Math::Cephes();
71: use Math::Random();
72: use Opcode();
73:
74: sub register {
75: my ($space,@taglist) = @_;
76: foreach my $temptag (@taglist) {
77: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
78: }
79: }
80:
81: sub deregister {
82: my ($space,@taglist) = @_;
83: foreach my $temptag (@taglist) {
84: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
85: if ($tempspace eq $space) {
86: pop(@{ $Apache::lonxml::alltags{$temptag} });
87: }
88: }
89: #&printalltags();
90: }
91:
92: use Apache::Constants qw(:common);
93: use Apache::lontexconvert();
94: use Apache::style();
95: use Apache::run();
96: use Apache::londefdef();
97: use Apache::scripttag();
98: use Apache::edit();
99: use Apache::lonnet();
100: use Apache::File();
101: use Apache::loncommon();
102:
103: #================================================== Main subroutine: xmlparse
104: #debugging control, to turn on debugging modify the correct handler
105: $Apache::lonxml::debug=0;
106:
107: #path to the directory containing the file currently being processed
108: @pwd=();
109:
110: #these two are used for capturing a subset of the output for later processing,
111: #don't touch them directly use &startredirection and &endredirection
112: @outputstack = ();
113: $redirection = 0;
114:
115: #controls wheter the <import> tag actually does
116: $import = 1;
117: @extlinks=();
118:
119: # meta mode is a bit weird only some output is to be turned off
120: #<output> tag turns metamode off (defined in londefdef.pm)
121: $metamode = 0;
122:
123: # turns on and of run::evaluate actually derefencing var refs
124: $evaluate = 1;
125:
126: # data structure for eidt mode, determines what tags can go into what other tags
127: %insertlist=();
128:
129: # stores the list of active tag namespaces
130: @namespace=();
131:
132: # if 0 all high ASCII characters will be encoded into HTML Entities
133: $prevent_entity_encode=0;
134:
135: # has the dynamic menu been updated to know about this resource
136: $Apache::lonxml::registered=0;
137:
138: # a pointer the the Apache request object
139: $Apache::lonxml::request='';
140:
141: sub xmlbegin {
142: my $output='';
143: if ($ENV{'browser.mathml'}) {
144: $output='<?xml version="1.0"?>'
145: .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
146: .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
147: .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
148: .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" '
149: .'xmlns="http://www.w3.org/TR/REC-html40">';
150: } else {
151: $output='<html>';
152: }
153: return $output;
154: }
155:
156: sub xmlend {
157: my ($discussiononly,$symb)=@_;
158: my $discussion='';
159: if ($ENV{'request.course.id'}) {
160: my $crs='/'.$ENV{'request.course.id'};
161: if ($ENV{'request.course.sec'}) {
162: $crs.='_'.$ENV{'request.course.sec'};
163: }
164: $crs=~s/\_/\//g;
165: my $seeid=&Apache::lonnet::allowed('rin',$crs);
166: unless ($symb) {
167: $symb=&Apache::lonnet::symbread();
168: }
169: if ($symb) {
170: my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
171: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
172: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
173: if ($contrib{'version'}) {
174: unless ($discussiononly) {
175: $discussion.=
176: '<address><hr />';
177: }
178: my $idx;
179: for ($idx=1;$idx<=$contrib{'version'};$idx++) {
180: my $hidden=($contrib{'hidden'}=~/\.$idx\./);
181: unless (($hidden) && (!$seeid)) {
182: my $message=$contrib{$idx.':message'};
183: $message=~s/\n/\<br \/\>/g;
184: $message=&Apache::lontexconvert::msgtexconverted($message);
185: if ($message) {
186: if ($hidden) {
187: $message='<font color="#888888">'.$message.'</font>';
188: }
189: my $screenname=&Apache::loncommon::screenname(
190: $contrib{$idx.':sendername'},
191: $contrib{$idx.':senderdomain'});
192: my $plainname=&Apache::loncommon::nickname(
193: $contrib{$idx.':sendername'},
194: $contrib{$idx.':senderdomain'});
195:
196: my $sender='Anonymous';
197: if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
198: $sender=&Apache::loncommon::aboutmewrapper(
199: $plainname,
200: $contrib{$idx.':sendername'},
201: $contrib{$idx.':senderdomain'}).' ('.
202: $contrib{$idx.':sendername'}.' at '.
203: $contrib{$idx.':senderdomain'}.')';
204: if ($contrib{$idx.':anonymous'}) {
205: $sender.=' [anonymous] '.
206: $screenname;
207: }
208: if ($seeid) {
209: if ($hidden) {
210: $sender.=' <a href="/adm/feedback?unhide='.
211: $symb.':::'.$idx.'">Make Visible</a>';
212: } else {
213: $sender.=' <a href="/adm/feedback?hide='.
214: $symb.':::'.$idx.'">Hide</a>';
215: }
216: }
217: } else {
218: if ($screenname) {
219: $sender='<i>'.$screenname.'</i>';
220: }
221: }
222: $discussion.='<p><b>'.$sender.'</b> ('.
223: localtime($contrib{$idx.':timestamp'}).
224: '):<blockquote>'.$message.
225: '</blockquote></p>';
226: }
227: }
228: }
229: unless ($discussiononly) {
230: $discussion.='</address>';
231: }
232: }
233: if ($discussiononly) {
234: $discussion.=(<<ENDDISCUSS);
235: <form action="/adm/feedback" method="post">
236: <input type="submit" name="discuss" value="Post Discussion" />
237: <input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
238: <input type="hidden" name="symb" value="$symb" />
239: <input type="hidden" name="sendit" value="true" />
240: <br />
241: <font size="1">Note: in anonymous discussion, your name is visible only to
242: course faculty</font><br />
243: <textarea name=comment cols=60 rows=10 wrap=hard></textarea>
244: </form>
245: ENDDISCUSS
246: }
247: }
248: }
249: return $discussion.($discussiononly?'':'</html>');
250: }
251:
252: sub tokeninputfield {
253: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
254: $defhost=~tr/a-z/A-Z/;
255: return (<<ENDINPUTFIELD)
256: <script>
257: function updatetoken() {
258: var comp=new Array;
259: var barcode=unescape(document.tokeninput.barcode.value);
260: comp=barcode.split('*');
261: if (typeof(comp[0])!="undefined") {
262: document.tokeninput.codeone.value=comp[0];
263: }
264: if (typeof(comp[1])!="undefined") {
265: document.tokeninput.codetwo.value=comp[1];
266: }
267: if (typeof(comp[2])!="undefined") {
268: comp[2]=comp[2].toUpperCase();
269: document.tokeninput.codethree.value=comp[2];
270: }
271: document.tokeninput.barcode.value='';
272: }
273: </script>
274: <form method="post" name="tokeninput">
275: <table border="2" bgcolor="#FFFFBB">
276: <tr><th>DocID Checkin</th></tr>
277: <tr><td>
278: <table>
279: <tr>
280: <td>Scan in Barcode</td>
281: <td><input type="text" size="22" name="barcode"
282: onChange="updatetoken()"/></td>
283: </tr>
284: <tr><td><i>or</i> Type in DocID</td>
285: <td>
286: <input type="text" size="5" name="codeone" />
287: <b><font size="+2">*</font></b>
288: <input type="text" size="5" name="codetwo" />
289: <b><font size="+2">*</font></b>
290: <input type="text" size="10" name="codethree" value="$defhost"
291: onChange="this.value=this.value.toUpperCase()" />
292: </td></tr>
293: </table>
294: </td></tr>
295: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
296: </table>
297: </form>
298: ENDINPUTFIELD
299: }
300:
301: sub maketoken {
302: my ($symb,$tuname,$tudom,$tcrsid)=@_;
303: unless ($symb) {
304: $symb=&Apache::lonnet::symbread();
305: }
306: unless ($tuname) {
307: $tuname=$ENV{'user.name'};
308: $tudom=$ENV{'user.domain'};
309: $tcrsid=$ENV{'request.course.id'};
310: }
311:
312: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
313: }
314:
315: sub printtokenheader {
316: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
317: unless ($token) { return ''; }
318:
319: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
320: unless ($tsymb) {
321: $tsymb=$symb;
322: }
323: unless ($tuname) {
324: $tuname=$name;
325: $tudom=$domain;
326: $tcrsid=$courseid;
327: }
328:
329: my %reply=&Apache::lonnet::get('environment',
330: ['firstname','middlename','lastname','generation'],
331: $tudom,$tuname);
332: my $plainname=$reply{'firstname'}.' '.
333: $reply{'middlename'}.' '.
334: $reply{'lastname'}.' '.
335: $reply{'generation'};
336:
337: if ($target eq 'web') {
338: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
339: return
340: '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
341: 'Checked out for '.$plainname.
342: '<br />User: '.$tuname.' at '.$tudom.
343: '<br />ID: '.$idhash{$tuname}.
344: '<br />CourseID: '.$tcrsid.
345: '<br />Course: '.$ENV{'course.'.$tcrsid.'.description'}.
346: '<br />DocID: '.$token.
347: '<br />Time: '.localtime().'<hr />';
348: } else {
349: return $token;
350: }
351: }
352:
353: sub fontsettings() {
354: my $headerstring='';
355: if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) {
356: $headerstring.=
357: '<meta Content-Type="text/html; charset=x-mac-roman">';
358: }
359: return $headerstring;
360: }
361:
362: sub registerurl {
363: my $forcereg=shift;
364: my $target = shift;
365: my $result = '';
366: if ($target eq 'edit') {
367: $result .="<script>\n".
368: "if (typeof menu != 'undefined') {menu.currentURL=null;}\n".
369: &Apache::loncommon::browser_and_searcher_javascript().
370: "\n</script>\n";
371: }
372: if ((($ENV{'request.publicaccess'}) ||
373: (!&Apache::lonnet::is_on_map($ENV{'REQUEST_URI'}))) &&
374: (!$forcereg)) {
375: return $result.
376: '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
377: }
378: if ($Apache::lonxml::registered && !$forcereg) { return ''; }
379: $Apache::lonxml::registered=1;
380: my $nothing='';
381: if ($ENV{'browser.type'} eq 'explorer') { $nothing='javascript:void(0);'; }
382: my $timesync='menu.syncclock(1000*'.time.');';
383: if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
384: my $hwkadd='';
385: if ($ENV{'request.filename'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
386: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
387: $hwkadd.=(<<ENDSUBM);
388: menu.switchbutton(7,1,'subm.gif','view sub','missions','gocmd("/adm/grades","submission")');
389: ENDSUBM
390: }
391: if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
392: $hwkadd.=(<<ENDGRDS);
393: menu.switchbutton(7,2,'pgrd.gif','problem','grades','gocmd("/adm/grades","gradingmenu")');
394: ENDGRDS
395: }
396: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
397: $hwkadd.=(<<ENDPARM);
398: menu.switchbutton(7,3,'pparm.gif','problem','parms','gocmd("/adm/parmset","set")');
399: ENDPARM
400: }
401: }
402: $result = (<<ENDREGTHIS);
403:
404: <script language="JavaScript">
405: // BEGIN LON-CAPA Internal
406:
407: function LONCAPAreg() {
408: menu=window.open("$nothing","LONCAPAmenu","",false);
409: menu.clearTimeout(menu.menucltim);
410: $timesync
411: menu.currentURL=window.location.pathname;
412: menu.reloadURL=window.location.pathname;
413: menu.currentStale=0;
414: menu.clearbut(3,1);
415: menu.switchbutton
416: (6,3,'catalog.gif','catalog','info','catalog_info()');
417: menu.switchbutton
418: (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
419: menu.switchbutton
420: (8,2,'fdbk.gif','feedback','discuss','gopost("/adm/feedback",currentURL)');
421: menu.switchbutton
422: (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
423: menu.switchbutton
424: (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
425: menu.switchbutton
426: (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
427: menu.switchbutton
428: (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
429: menu.switchbutton
430: (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
431: menu.switchbutton
432: (9,3,'anot.gif','anno-','tations','annotate()');
433: $hwkadd
434: }
435:
436: function LONCAPAstale() {
437: menu=window.open("$nothing","LONCAPAmenu","",false);
438: menu.currentStale=1;
439: if (menu.reloadURL!='' && menu.reloadURL!= null) {
440: menu.switchbutton
441: (3,1,'reload.gif','return','location','go(reloadURL)');
442: }
443: menu.clearbut(7,1);
444: menu.clearbut(7,2);
445: menu.clearbut(7,3);
446: menu.menucltim=menu.setTimeout(
447: 'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
448: 'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
449: 2000);
450:
451: }
452:
453: // END LON-CAPA Internal
454: </script>
455: ENDREGTHIS
456:
457: } else {
458: $result = (<<ENDDONOTREGTHIS);
459:
460: <script language="JavaScript">
461: // BEGIN LON-CAPA Internal
462:
463: function LONCAPAreg() {
464: menu=window.open("$nothing","LONCAPAmenu","",false);
465: $timesync
466: menu.currentStale=1;
467: menu.clearbut(2,1);
468: menu.clearbut(2,3);
469: menu.clearbut(8,1);
470: menu.clearbut(8,2);
471: menu.clearbut(8,3);
472: if (menu.currentURL) {
473: menu.switchbutton
474: (3,1,'reload.gif','return','location','go(currentURL)');
475: } else {
476: menu.clearbut(3,1);
477: }
478: }
479:
480: function LONCAPAstale() {
481: }
482:
483: // END LON-CAPA Internal
484: </script>
485: ENDDONOTREGTHIS
486: }
487: return $result;
488: }
489:
490: sub loadevents() {
491: return 'LONCAPAreg();';
492: }
493:
494: sub unloadevents() {
495: return 'LONCAPAstale();';
496: }
497:
498: sub printalltags {
499: my $temp;
500: foreach $temp (sort keys %Apache::lonxml::alltags) {
501: &Apache::lonxml::debug("$temp -- ".
502: join(',',@{ $Apache::lonxml::alltags{$temp} }));
503: }
504: }
505:
506: sub xmlparse {
507: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
508:
509: &setup_globals($request,$target);
510: #
511: # do we have a course style file?
512: #
513:
514: if ($ENV{'request.course.id'}) {
515: my $bodytext=
516: $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
517: if ($bodytext) {
518: my $location=&Apache::lonnet::filelocation('',$bodytext);
519: my $styletext=&Apache::lonnet::getfile($location);
520: if ($styletext ne '-1') {
521: %style_for_target = (%style_for_target,
522: &Apache::style::styleparser($target,$styletext));
523: }
524: }
525: }
526:
527: #&printalltags();
528: my @pars = ();
529: my $pwd=$ENV{'request.filename'};
530: $pwd =~ s:/[^/]*$::;
531: &newparser(\@pars,\$content_file_string,$pwd);
532:
533: my $safeeval = new Safe;
534: my $safehole = new Safe::Hole;
535: &init_safespace($target,$safeeval,$safehole,$safeinit);
536: #-------------------- Redefinition of the target in the case of compound target
537:
538: ($target, my @tenta) = split('&&',$target);
539:
540: my @stack = ();
541: my @parstack = ();
542: &initdepth;
543:
544: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
545: $safeeval,\%style_for_target);
546: if ($ENV{'request.uri'}) {
547: &writeallows($ENV{'request.uri'});
548: }
549: return $finaloutput;
550: }
551:
552: sub htmlclean {
553: my ($raw,$full)=@_;
554:
555: my $tree = HTML::TreeBuilder->new;
556: $tree->ignore_unknown(0);
557:
558: $tree->parse($raw);
559:
560: my $output= $tree->as_HTML(undef,' ');
561:
562: $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
563: $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
564: unless ($full) {
565: $output=~s/\<[\/]*(body|head|html)\>//gis;
566: }
567:
568: $tree = $tree->delete;
569:
570: return $output;
571: }
572:
573: sub latex_special_symbols {
574: my ($current_token,$stack,$parstack)=@_;
575: $current_token=~s/\\/\\char92 /g;
576: $current_token=~s/\^/\\char94 /g;
577: $current_token=~s/\~/\\char126 /g;
578: $current_token=~s/(&[^a-z#])/\\$1/g;
579: $current_token=~s/([^&]\#)/\\$1/g;
580: $current_token=~s/(\$|_|{|})/\\$1/g;
581: $current_token=~s/\\char92 /\\texttt{\\char92}/g;
582: $current_token=~s/>/\$>\$/g; #more
583: $current_token=~s/</\$<\$/g; #less
584: if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit
585: if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space
586: return $current_token;
587: }
588:
589: sub inner_xmlparse {
590: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
591: my $finaloutput = '';
592: my $result;
593: my $token;
594: while ( $#$pars > -1 ) {
595: while ($token = $$pars['-1']->get_token) {
596: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
597: if ($metamode<1) {
598: my $text=$token->[1];
599: if ($token->[0] eq 'C' && $target eq 'tex') {
600: $text = '%'.$text;
601: $text =~ s/[\n\r]//g;
602: }
603: $result.=$text;
604: }
605: } elsif ($token->[0] eq 'PI') {
606: if ($metamode<1) {
607: $result=$token->[2];
608: }
609: } elsif ($token->[0] eq 'S') {
610: # add tag to stack
611: push (@$stack,$token->[1]);
612: # add parameters list to another stack
613: push (@$parstack,&parstring($token));
614: &increasedepth($token);
615: if (exists $$style_for_target{$token->[1]}) {
616: if ($Apache::lonxml::redirection) {
617: $Apache::lonxml::outputstack['-1'] .=
618: &recurse($$style_for_target{$token->[1]},$target,$safeeval,
619: $style_for_target,@$parstack);
620: } else {
621: $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
622: $safeeval,$style_for_target,@$parstack);
623: }
624: } else {
625: $result = &callsub("start_$token->[1]", $target, $token, $stack,
626: $parstack, $pars, $safeeval, $style_for_target);
627: }
628: } elsif ($token->[0] eq 'E') {
629: #clear out any tags that didn't end
630: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
631: my $lasttag=$$stack[-1];
632: if ($token->[1] =~ /^$lasttag$/i) {
633: &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
634: last;
635: } else {
636: &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> in file');
637: &end_tag($stack,$parstack,$token);
638: }
639: }
640:
641: if (exists($$style_for_target{'/'."$token->[1]"})) {
642: if ($Apache::lonxml::redirection) {
643: $Apache::lonxml::outputstack['-1'] .=
644: &recurse($$style_for_target{'/'."$token->[1]"},
645: $target,$safeeval,$style_for_target,@$parstack);
646: } else {
647: $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
648: $target,$safeeval,$style_for_target,
649: @$parstack);
650: }
651: } else {
652: $result = &callsub("end_$token->[1]", $target, $token, $stack,
653: $parstack, $pars,$safeeval, $style_for_target);
654: }
655: } else {
656: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
657: }
658: #evaluate variable refs in result
659: if ($result ne "") {
660: if ( $#$parstack > -1 ) {
661: $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]);
662: } else {
663: $result= &Apache::run::evaluate($result,$safeeval,'');
664: }
665: }
666: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
667: if ($target eq 'tex') {
668: $result=&latex_special_symbols($result,$stack,$parstack);
669: }
670: }
671:
672: # Encode any high ASCII characters
673: if (!$Apache::lonxml::prevent_entity_encode) {
674: $result=&HTML::Entities::encode($result,"\200-\377");
675: }
676: if ($Apache::lonxml::redirection) {
677: $Apache::lonxml::outputstack['-1'] .= $result;
678: } else {
679: $finaloutput.=$result;
680: }
681: $result = '';
682:
683: if ($token->[0] eq 'E') {
684: &end_tag($stack,$parstack,$token);
685: }
686: }
687: pop @$pars;
688: pop @Apache::lonxml::pwd;
689: }
690:
691: # if ($target eq 'meta') {
692: # $finaloutput.=&endredirection;
693: # }
694:
695:
696: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
697: $finaloutput=&afterburn($finaloutput);
698: }
699: return $finaloutput;
700: }
701:
702: sub recurse {
703: my @innerstack = ();
704: my @innerparstack = ();
705: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
706: my @pat = ();
707: &newparser(\@pat,\$newarg);
708: my $tokenpat;
709: my $partstring = '';
710: my $output='';
711: my $decls='';
712: &Apache::lonxml::debug("Recursing");
713: while ( $#pat > -1 ) {
714: while ($tokenpat = $pat[$#pat]->get_token) {
715: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
716: if ($metamode<1) { $partstring=$tokenpat->[1]; }
717: } elsif ($tokenpat->[0] eq 'PI') {
718: if ($metamode<1) { $partstring=$tokenpat->[2]; }
719: } elsif ($tokenpat->[0] eq 'S') {
720: push (@innerstack,$tokenpat->[1]);
721: push (@innerparstack,&parstring($tokenpat));
722: &increasedepth($tokenpat);
723: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
724: \@innerstack, \@innerparstack, \@pat,
725: $safeeval, $style_for_target);
726: } elsif ($tokenpat->[0] eq 'E') {
727: #clear out any tags that didn't end
728: while ($tokenpat->[1] ne $innerstack[$#innerstack]
729: && ($#innerstack > -1)) {
730: my $lasttag=$innerstack[-1];
731: if ($tokenpat->[1] =~ /^$lasttag$/i) {
732: &Apache::lonxml::warning('Using tag </'.$tokenpat->[1].'> as end tag to <'.$innerstack[-1].'>');
733: last;
734: } else {
735: &Apache::lonxml::warning('Found tag </'.$tokenpat->[1].'> when looking for </'.$innerstack[-1].'> in file');
736: &end_tag(\@innerstack,\@innerparstack,$tokenpat);
737: }
738: }
739: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
740: \@innerstack, \@innerparstack, \@pat,
741: $safeeval, $style_for_target);
742: } else {
743: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
744: }
745: #pass both the variable to the style tag, and the tag we
746: #are processing inside the <definedtag>
747: if ( $partstring ne "" ) {
748: if ( $#parstack > -1 ) {
749: if ( $#innerparstack > -1 ) {
750: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
751: } else {
752: $decls= $parstack[$#parstack];
753: }
754: } else {
755: if ( $#innerparstack > -1 ) {
756: $decls=$innerparstack[$#innerparstack];
757: } else {
758: $decls='';
759: }
760: }
761: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
762: $partstring = '';
763: }
764: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
765: &decreasedepth($tokenpat);}
766: }
767: pop @pat;
768: pop @Apache::lonxml::pwd;
769: }
770: &Apache::lonxml::debug("Exiting Recursing");
771: return $output;
772: }
773:
774: sub callsub {
775: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
776: my $currentstring='';
777: my $nodefault;
778: {
779: my $sub1;
780: no strict 'refs';
781: my $tag=$token->[1];
782: my $space=$Apache::lonxml::alltags{$tag}[-1];
783: if (!$space) {
784: $tag=~tr/A-Z/a-z/;
785: $sub=~tr/A-Z/a-z/;
786: $space=$Apache::lonxml::alltags{$tag}[-1]
787: }
788:
789: my $deleted=0;
790: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
791: if (($token->[0] eq 'S') && ($target eq 'modified')) {
792: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
793: $parstack,$parser,$safeeval,
794: $style);
795: }
796: if (!$deleted) {
797: if ($space) {
798: &Apache::lonxml::debug("Calling sub $sub in $space $metamode");
799: $sub1="$space\:\:$sub";
800: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
801: $parstack,$parser,$safeeval,
802: $style);
803: } else {
804: &Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
805: if ($metamode <1) {
806: if (defined($token->[4]) && ($metamode < 1)) {
807: $currentstring = $token->[4];
808: } else {
809: $currentstring = $token->[2];
810: }
811: }
812: }
813: # &Apache::lonxml::debug("nodefalt:$nodefault:");
814: if ($currentstring eq '' && $nodefault eq '') {
815: if ($target eq 'edit') {
816: &Apache::lonxml::debug("doing default edit for $token->[1]");
817: if ($token->[0] eq 'S') {
818: $currentstring = &Apache::edit::tag_start($target,$token);
819: } elsif ($token->[0] eq 'E') {
820: $currentstring = &Apache::edit::tag_end($target,$token);
821: }
822: } elsif ($target eq 'modified') {
823: if ($token->[0] eq 'S') {
824: $currentstring = $token->[4];
825: $currentstring.=&Apache::edit::handle_insert();
826: } else {
827: $currentstring = $token->[2];
828: }
829: }
830: }
831: }
832: use strict 'refs';
833: }
834: return $currentstring;
835: }
836:
837: sub setup_globals {
838: my ($request,$target)=@_;
839: $Apache::lonxml::request=$request;
840: $Apache::lonxml::registered = 0;
841: @Apache::lonxml::pwd=();
842: @Apache::lonxml::extlinks=();
843: if ($target eq 'meta') {
844: $Apache::lonxml::redirection = 0;
845: $Apache::lonxml::metamode = 1;
846: $Apache::lonxml::evaluate = 1;
847: $Apache::lonxml::import = 0;
848: } elsif ($target eq 'answer') {
849: $Apache::lonxml::redirection = 0;
850: $Apache::lonxml::metamode = 1;
851: $Apache::lonxml::evaluate = 1;
852: $Apache::lonxml::import = 1;
853: } elsif ($target eq 'grade') {
854: &startredirection;
855: $Apache::lonxml::metamode = 0;
856: $Apache::lonxml::evaluate = 1;
857: $Apache::lonxml::import = 1;
858: } elsif ($target eq 'modified') {
859: $Apache::lonxml::redirection = 0;
860: $Apache::lonxml::metamode = 0;
861: $Apache::lonxml::evaluate = 0;
862: $Apache::lonxml::import = 0;
863: } elsif ($target eq 'edit') {
864: $Apache::lonxml::redirection = 0;
865: $Apache::lonxml::metamode = 0;
866: $Apache::lonxml::evaluate = 0;
867: $Apache::lonxml::import = 0;
868: } elsif ($target eq 'analyze') {
869: $Apache::lonxml::redirection = 0;
870: $Apache::lonxml::metamode = 0;
871: $Apache::lonxml::evaluate = 1;
872: $Apache::lonxml::import = 1;
873: } else {
874: $Apache::lonxml::redirection = 0;
875: $Apache::lonxml::metamode = 0;
876: $Apache::lonxml::evaluate = 1;
877: $Apache::lonxml::import = 1;
878: }
879: }
880:
881: sub init_safespace {
882: my ($target,$safeeval,$safehole,$safeinit) = @_;
883: $safeeval->permit("entereval");
884: $safeeval->permit(":base_math");
885: $safeeval->permit("sort");
886: $safeeval->deny(":base_io");
887: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
888: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
889:
890: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
891: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
892: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
893: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
894: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
895: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
896: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
897: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
898: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
899: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
900: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
901: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
902: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
903: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
904: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
905: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
906: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
907: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
908: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
909: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
910: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
911: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
912: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
913: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
914: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
915: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
916: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
917: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
918: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
919: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
920: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
921: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
922: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
923: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
924: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
925: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
926: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
927: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
928: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
929: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
930:
931: #need to inspect this class of ops
932: # $safeeval->deny(":base_orig");
933: $safeinit .= ';$external::target="'.$target.'";';
934: my $rndseed;
935: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
936: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
937: $safeinit .= ';$external::randomseed='.$rndseed.';';
938: &Apache::run::run($safeinit,$safeeval);
939: }
940:
941: sub startredirection {
942: $Apache::lonxml::redirection++;
943: push (@Apache::lonxml::outputstack, '');
944: }
945:
946: sub endredirection {
947: if (!$Apache::lonxml::redirection) {
948: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
949: return '';
950: }
951: $Apache::lonxml::redirection--;
952: pop @Apache::lonxml::outputstack;
953: }
954:
955: sub end_tag {
956: my ($tagstack,$parstack,$token)=@_;
957: pop(@$tagstack);
958: pop(@$parstack);
959: &decreasedepth($token);
960: }
961:
962: sub initdepth {
963: @Apache::lonxml::depthcounter=();
964: $Apache::lonxml::depth=-1;
965: $Apache::lonxml::olddepth=-1;
966: }
967:
968: sub increasedepth {
969: my ($token) = @_;
970: $Apache::lonxml::depth++;
971: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
972: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
973: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
974: }
975: my $curdepth=join('_',@Apache::lonxml::depthcounter);
976: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
977: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
978: }
979:
980: sub decreasedepth {
981: my ($token) = @_;
982: $Apache::lonxml::depth--;
983: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
984: $#Apache::lonxml::depthcounter--;
985: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
986: }
987: if ( $Apache::lonxml::depth < -1) {
988: &Apache::lonxml::warning("Missing tags, unable to properly run file.");
989: $Apache::lonxml::depth='-1';
990: }
991: my $curdepth=join('_',@Apache::lonxml::depthcounter);
992: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
993: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
994: }
995:
996: sub get_all_text_unbalanced {
997: #there is a copy of this in lonpublisher.pm
998: my($tag,$pars)= @_;
999: my $token;
1000: my $result='';
1001: $tag='<'.$tag.'>';
1002: while ($token = $$pars[-1]->get_token) {
1003: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1004: $result.=$token->[1];
1005: } elsif ($token->[0] eq 'PI') {
1006: $result.=$token->[2];
1007: } elsif ($token->[0] eq 'S') {
1008: $result.=$token->[4];
1009: } elsif ($token->[0] eq 'E') {
1010: $result.=$token->[2];
1011: }
1012: if ($result =~ /(.*)$tag(.*)/) {
1013: &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
1014: &Apache::lonxml::debug('Result is :'.$1);
1015: $result=$1;
1016: my $redo=$tag.$2;
1017: &Apache::lonxml::newparser($pars,\$redo);
1018: last;
1019: }
1020: }
1021: return $result
1022: }
1023:
1024: sub get_all_text {
1025: my($tag,$pars)= @_;
1026: my $depth=0;
1027: my $token;
1028: my $result='';
1029: if ( $tag =~ m:^/: ) {
1030: my $tag=substr($tag,1);
1031: # &Apache::lonxml::debug("have:$tag:");
1032: while (($depth >=0) && ($token = $pars->get_token)) {
1033: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
1034: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1035: $result.=$token->[1];
1036: } elsif ($token->[0] eq 'PI') {
1037: $result.=$token->[2];
1038: } elsif ($token->[0] eq 'S') {
1039: if ($token->[1] =~ /^$tag$/i) { $depth++; }
1040: $result.=$token->[4];
1041: } elsif ($token->[0] eq 'E') {
1042: if ( $token->[1] =~ /^$tag$/i) { $depth--; }
1043: #skip sending back the last end tag
1044: if ($depth > -1) { $result.=$token->[2]; } else {
1045: $pars->unget_token($token);
1046: }
1047: }
1048: }
1049: } else {
1050: while ($token = $pars->get_token) {
1051: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1052: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1053: $result.=$token->[1];
1054: } elsif ($token->[0] eq 'PI') {
1055: $result.=$token->[2];
1056: } elsif ($token->[0] eq 'S') {
1057: if ( $token->[1] =~ /^$tag$/i) {
1058: $pars->unget_token($token); last;
1059: } else {
1060: $result.=$token->[4];
1061: }
1062: } elsif ($token->[0] eq 'E') {
1063: $result.=$token->[2];
1064: }
1065: }
1066: }
1067: # &Apache::lonxml::debug("Exit:$result:");
1068: return $result
1069: }
1070:
1071: sub newparser {
1072: my ($parser,$contentref,$dir) = @_;
1073: push (@$parser,HTML::LCParser->new($contentref));
1074: $$parser['-1']->xml_mode('1');
1075: if ( $dir eq '' ) {
1076: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1077: } else {
1078: push (@Apache::lonxml::pwd, $dir);
1079: }
1080: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
1081: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
1082: }
1083:
1084: sub parstring {
1085: my ($token) = @_;
1086: my $temp='';
1087: foreach (@{$token->[3]}) {
1088: unless ($_=~/\W/) {
1089: my $val=$token->[2]->{$_};
1090: $val =~ s/([\%\@\\\"])/\\$1/g;
1091: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1092: $temp .= "my \$$_=\"$val\";"
1093: }
1094: }
1095: return $temp;
1096: }
1097:
1098: sub writeallows {
1099: unless ($#extlinks>=0) { return; }
1100: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1101: if ($ENV{'httpref.'.$thisurl}) {
1102: $thisurl=$ENV{'httpref.'.$thisurl};
1103: }
1104: my $thisdir=$thisurl;
1105: $thisdir=~s/\/[^\/]+$//;
1106: my %httpref=();
1107: foreach (@extlinks) {
1108: $httpref{'httpref.'.
1109: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1110: }
1111: @extlinks=();
1112: &Apache::lonnet::appenv(%httpref);
1113: }
1114:
1115: #
1116: # Afterburner handles anchors, highlights and links
1117: #
1118: sub afterburn {
1119: my $result=shift;
1120: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1121: ['highlight','anchor','link']);
1122: if ($ENV{'form.highlight'}) {
1123: foreach (split(/\,/,$ENV{'form.highlight'})) {
1124: my $anchorname=$_;
1125: my $matchthis=$anchorname;
1126: $matchthis=~s/\_+/\\s\+/g;
1127: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1128: }
1129: }
1130: if ($ENV{'form.link'}) {
1131: foreach (split(/\,/,$ENV{'form.link'})) {
1132: my ($anchorname,$linkurl)=split(/\>/,$_);
1133: my $matchthis=$anchorname;
1134: $matchthis=~s/\_+/\\s\+/g;
1135: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1136: }
1137: }
1138: if ($ENV{'form.anchor'}) {
1139: my $anchorname=$ENV{'form.anchor'};
1140: my $matchthis=$anchorname;
1141: $matchthis=~s/\_+/\\s\+/g;
1142: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1143: $result.=(<<"ENDSCRIPT");
1144: <script>
1145: document.location.hash='$anchorname';
1146: </script>
1147: ENDSCRIPT
1148: }
1149: return $result;
1150: }
1151:
1152: sub storefile {
1153: my ($file,$contents)=@_;
1154: if (my $fh=Apache::File->new('>'.$file)) {
1155: print $fh $contents;
1156: $fh->close();
1157: } else {
1158: &warning("Unable to save file $file");
1159: }
1160: }
1161:
1162: sub createnewhtml {
1163: my $filecontents=(<<SIMPLECONTENT);
1164: <html>
1165: <head>
1166: <title>
1167: Title of Document Goes Here
1168: </title>
1169: </head>
1170: <body bgcolor="#FFFFFF">
1171:
1172: Body of Document Goes Here
1173:
1174: </body>
1175: </html>
1176: SIMPLECONTENT
1177: return $filecontents;
1178: }
1179:
1180:
1181: sub inserteditinfo {
1182: my ($result,$filecontents)=@_;
1183: $filecontents = &HTML::Entities::encode($filecontents);
1184: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1185: my $buttons=(<<BUTTONS);
1186: <input type="submit" name="attemptclean"
1187: value="Save and then attempt to clean HTML" />
1188: <input type="submit" name="savethisfile" value="Save this" />
1189: <input type="submit" name="viewmode" value="View" />
1190: BUTTONS
1191: my $editfooter=(<<ENDFOOTER);
1192: <hr />
1193: <a name="editsection" />
1194: <form method="post">
1195: <input type="hidden" name="editmode" value="Edit" />
1196: $buttons<br />
1197: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1198: <br />$buttons
1199: <br />
1200: </form>
1201: ENDFOOTER
1202: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1203: $result=~s/(\<\/body\>)/$editfooter/is;
1204: return $result;
1205: }
1206:
1207: sub get_target {
1208: my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1209: if ( $ENV{'request.state'} eq 'published') {
1210: if ( defined($ENV{'form.grade_target'})
1211: && ($viewgrades == 'F' )) {
1212: return ($ENV{'form.grade_target'});
1213: } elsif (defined($ENV{'form.grade_target'})) {
1214: if (($ENV{'form.grade_target'} eq 'web') ||
1215: ($ENV{'form.grade_target'} eq 'tex') ) {
1216: return $ENV{'form.grade_target'}
1217: } else {
1218: return 'web';
1219: }
1220: } else {
1221: return 'web';
1222: }
1223: } elsif ($ENV{'request.state'} eq 'construct') {
1224: if ( defined($ENV{'form.grade_target'})) {
1225: return ($ENV{'form.grade_target'});
1226: } else {
1227: return 'web';
1228: }
1229: } else {
1230: return 'web';
1231: }
1232: }
1233:
1234: sub handler {
1235: my $request=shift;
1236:
1237: my $target=&get_target();
1238:
1239: $Apache::lonxml::debug=0;
1240:
1241: if ($ENV{'browser.mathml'}) {
1242: $request->content_type('text/xml');
1243: } else {
1244: $request->content_type('text/html');
1245: }
1246: &Apache::loncommon::no_cache($request);
1247: $request->send_http_header;
1248:
1249: return OK if $request->header_only;
1250:
1251:
1252: my $file=&Apache::lonnet::filelocation("",$request->uri);
1253: #
1254: # Edit action? Save file.
1255: #
1256: unless ($ENV{'request.state'} eq 'published') {
1257: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1258: &storefile($file,$ENV{'form.filecont'});
1259: }
1260: }
1261: my %mystyle;
1262: my $result = '';
1263: my $filecontents=&Apache::lonnet::getfile($file);
1264: if ($filecontents == -1) {
1265: $result=(<<ENDNOTFOUND);
1266: <html>
1267: <head>
1268: <title>File not found</title>
1269: </head>
1270: <body bgcolor="#FFFFFF">
1271: <b>File not found: $file</b>
1272: </body>
1273: </html>
1274: ENDNOTFOUND
1275: $filecontents='';
1276: if ($ENV{'request.state'} ne 'published') {
1277: $filecontents=&createnewhtml();
1278: $ENV{'form.editmode'}='Edit'; #force edit mode
1279: }
1280: } else {
1281: unless ($ENV{'request.state'} eq 'published') {
1282: if ($ENV{'form.attemptclean'}) {
1283: $filecontents=&htmlclean($filecontents,1);
1284: }
1285: }
1286: if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1287: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1288: '',%mystyle);
1289: }
1290: }
1291:
1292: #
1293: # Edit action? Insert editing commands
1294: #
1295: unless ($ENV{'request.state'} eq 'published') {
1296: if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1297: my $displayfile=$request->uri;
1298: $displayfile=~s/^\/[^\/]*//;
1299: $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
1300: '</h3></body></html>';
1301: $result=&inserteditinfo($result,$filecontents);
1302: }
1303: }
1304:
1305: writeallows($request->uri);
1306:
1307: $request->print($result);
1308:
1309: return OK;
1310: }
1311:
1312: sub debug {
1313: if ($Apache::lonxml::debug eq 1) {
1314: $|=1;
1315: print("DEBUG:".&HTML::Entities::encode($_[0])."<br />\n");
1316: }
1317: }
1318:
1319: sub error {
1320: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1321: # If printing in construction space, put the error inside <pre></pre>
1322: print "<b>ERROR:</b>".join("\n",@_)."\n";
1323: } else {
1324: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1325: #notify author
1326: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1327: #notify course
1328: if ( $ENV{'request.course.id'} ) {
1329: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1330: my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1331: foreach my $user (split /\,/, $users) {
1332: ($user,my $domain) = split /:/, $user;
1333: &Apache::lonmsg::user_normal_msg($user,$domain,
1334: "Error [$declutter]",join('<br />',@_));
1335: }
1336: }
1337:
1338: #FIXME probably shouldn't have me get everything forever.
1339: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1340: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1341: }
1342: }
1343:
1344: sub warning {
1345: if ($ENV{'request.state'} eq 'construct') {
1346: print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1347: }
1348: }
1349:
1350: sub get_param {
1351: my ($param,$parstack,$safeeval,$context) = @_;
1352: if ( ! $context ) { $context = -1; }
1353: my $args ='';
1354: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1355: if ( ! $args ) { return undef; }
1356: if ( $args =~ /my \$$param=\"/ ) {
1357: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1358: } else {
1359: return undef;
1360: }
1361: }
1362:
1363: sub get_param_var {
1364: my ($param,$parstack,$safeeval,$context) = @_;
1365: if ( ! $context ) { $context = -1; }
1366: my $args ='';
1367: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1368: if ( $args !~ /my \$$param=\"/ ) { return undef; }
1369: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1370: if ($value =~ /^[\$\@\%]/) {
1371: return &Apache::run::run("return $value",$safeeval,1);
1372: } else {
1373: return $value;
1374: }
1375: }
1376:
1377: sub register_insert {
1378: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1379: my $i;
1380: my $tagnum=0;
1381: my @order;
1382: for ($i=0;$i < $#data; $i++) {
1383: my $line = $data[$i];
1384: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1385: if ( $line =~ /TABLE/ ) { last; }
1386: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1387: if ($tag) {
1388: $insertlist{"$tagnum.tag"} = $tag;
1389: $insertlist{"$tagnum.description"} = $descrip;
1390: $insertlist{"$tagnum.color"} = $color;
1391: $insertlist{"$tagnum.function"} = $function;
1392: if (!defined($show)) { $show='yes'; }
1393: $insertlist{"$tagnum.show"}= $show;
1394: $insertlist{"$tag.num"}=$tagnum;
1395: $tagnum++;
1396: }
1397: }
1398: $i++; #skipping TABLE line
1399: $tagnum = 0;
1400: for (;$i < $#data;$i++) {
1401: my $line = $data[$i];
1402: my ($mnemonic,@which) = split(/ +/,$line);
1403: my $tag = $insertlist{"$tagnum.tag"};
1404: for (my $j=0;$j <=$#which;$j++) {
1405: if ( $which[$j] eq 'Y' ) {
1406: if ($insertlist{"$j.show"} ne 'no') {
1407: push(@{ $insertlist{"$tag.which"} },$j);
1408: }
1409: }
1410: }
1411: $tagnum++;
1412: }
1413: }
1414:
1415: sub description {
1416: my ($token)=@_;
1417: my $tagnum;
1418: my $tag=$token->[1];
1419: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1420: my $testtag=$namespace.'::'.$tag;
1421: $tagnum=$insertlist{"$testtag.num"};
1422: if (defined($tagnum)) { last; }
1423: }
1424: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1425: return $insertlist{$tagnum.'.description'};
1426: }
1427:
1428: # ----------------------------------------------------------------- whichuser
1429: # returns a list of $symb, $courseid, $domain, $name that is correct for
1430: # calls to lonnet functions for this setup.
1431: # - looks for form.grade_ parameters
1432: sub whichuser {
1433: my ($symb,$courseid,$domain,$name);
1434: if (defined($ENV{'form.grade_symb'})) {
1435: my $tmp_courseid=$ENV{'form.grade_courseid'};
1436: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1437: if ($allowed) {
1438: $symb=$ENV{'form.grade_symb'};
1439: $courseid=$ENV{'form.grade_courseid'};
1440: $domain=$ENV{'form.grade_domain'};
1441: $name=$ENV{'form.grade_username'};
1442: }
1443: } else {
1444: $symb=&Apache::lonnet::symbread();
1445: $courseid=$ENV{'request.course.id'};
1446: $domain=$ENV{'user.domain'};
1447: $name=$ENV{'user.name'};
1448: }
1449: return ($symb,$courseid,$domain,$name);
1450: }
1451:
1452: 1;
1453: __END__
1454:
1455:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>