1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # $Id: lonxml.pm,v 1.197 2002/10/03 13:34:10 sakharuk 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:
367: if ($target eq 'edit') {
368: $result .="<script>\n".
369: "if (typeof menu != 'undefined') {menu.currentURL=null;}\n".
370: &Apache::loncommon::browser_and_searcher_javascript().
371: "\n</script>\n";
372: }
373: if ((($ENV{'request.publicaccess'}) ||
374: (!&Apache::lonnet::is_on_map($ENV{'REQUEST_URI'}))) &&
375: (!$forcereg)) {
376: return $result.
377: '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
378: }
379: if ($Apache::lonxml::registered && !$forcereg) { return ''; }
380: $Apache::lonxml::registered=1;
381: my $nothing='';
382: if ($ENV{'browser.type'} eq 'explorer') { $nothing='javascript:void(0);'; }
383: my $timesync='menu.syncclock(1000*'.time.');';
384: if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
385: my $hwkadd='';
386: if ($ENV{'request.filename'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
387: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
388: $hwkadd.=(<<ENDSUBM);
389: menu.switchbutton(7,1,'subm.gif','view sub','missions','gocmd("/adm/grades","submission")');
390: ENDSUBM
391: }
392: if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
393: $hwkadd.=(<<ENDGRDS);
394: menu.switchbutton(7,2,'pgrd.gif','problem','grades','gocmd("/adm/grades","gradingmenu")');
395: ENDGRDS
396: }
397: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
398: $hwkadd.=(<<ENDPARM);
399: menu.switchbutton(7,3,'pparm.gif','problem','parms','gocmd("/adm/parmset","set")');
400: ENDPARM
401: }
402: }
403: $result = (<<ENDREGTHIS);
404:
405: <script language="JavaScript">
406: // BEGIN LON-CAPA Internal
407:
408: function LONCAPAreg() {
409: menu=window.open("$nothing","LONCAPAmenu","",false);
410: menu.clearTimeout(menu.menucltim);
411: $timesync
412: menu.currentURL=window.location.pathname;
413: menu.reloadURL=window.location.pathname;
414: menu.currentStale=0;
415: menu.clearbut(3,1);
416: menu.switchbutton
417: (6,3,'catalog.gif','catalog','info','catalog_info()');
418: menu.switchbutton
419: (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
420: menu.switchbutton
421: (8,2,'fdbk.gif','feedback','discuss','gopost("/adm/feedback",currentURL)');
422: menu.switchbutton
423: (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
424: menu.switchbutton
425: (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
426: menu.switchbutton
427: (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
428: menu.switchbutton
429: (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
430: menu.switchbutton
431: (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
432: menu.switchbutton
433: (9,3,'anot.gif','anno-','tations','annotate()');
434: $hwkadd
435: }
436:
437: function LONCAPAstale() {
438: menu=window.open("$nothing","LONCAPAmenu","",false);
439: menu.currentStale=1;
440: if (menu.reloadURL!='' && menu.reloadURL!= null) {
441: menu.switchbutton
442: (3,1,'reload.gif','return','location','go(reloadURL)');
443: }
444: menu.clearbut(7,1);
445: menu.clearbut(7,2);
446: menu.clearbut(7,3);
447: menu.menucltim=menu.setTimeout(
448: 'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
449: 'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
450: 2000);
451:
452: }
453:
454: // END LON-CAPA Internal
455: </script>
456: ENDREGTHIS
457:
458: } else {
459: $result = (<<ENDDONOTREGTHIS);
460:
461: <script language="JavaScript">
462: // BEGIN LON-CAPA Internal
463:
464: function LONCAPAreg() {
465: menu=window.open("$nothing","LONCAPAmenu","",false);
466: $timesync
467: menu.currentStale=1;
468: menu.clearbut(2,1);
469: menu.clearbut(2,3);
470: menu.clearbut(8,1);
471: menu.clearbut(8,2);
472: menu.clearbut(8,3);
473: if (menu.currentURL) {
474: menu.switchbutton
475: (3,1,'reload.gif','return','location','go(currentURL)');
476: } else {
477: menu.clearbut(3,1);
478: }
479: }
480:
481: function LONCAPAstale() {
482: }
483:
484: // END LON-CAPA Internal
485: </script>
486: ENDDONOTREGTHIS
487: }
488: return $result;
489: }
490:
491: sub loadevents() {
492: return 'LONCAPAreg();';
493: }
494:
495: sub unloadevents() {
496: return 'LONCAPAstale();';
497: }
498:
499: sub printalltags {
500: my $temp;
501: foreach $temp (sort keys %Apache::lonxml::alltags) {
502: &Apache::lonxml::debug("$temp -- ".
503: join(',',@{ $Apache::lonxml::alltags{$temp} }));
504: }
505: }
506:
507: sub xmlparse {
508: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
509:
510: &setup_globals($request,$target);
511: #
512: # do we have a course style file?
513: #
514:
515: if ($ENV{'request.course.id'}) {
516: my $bodytext=
517: $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
518: if ($bodytext) {
519: my $location=&Apache::lonnet::filelocation('',$bodytext);
520: my $styletext=&Apache::lonnet::getfile($location);
521: if ($styletext ne '-1') {
522: %style_for_target = (%style_for_target,
523: &Apache::style::styleparser($target,$styletext));
524: }
525: }
526: }
527:
528: #&printalltags();
529: my @pars = ();
530: my $pwd=$ENV{'request.filename'};
531: $pwd =~ s:/[^/]*$::;
532: &newparser(\@pars,\$content_file_string,$pwd);
533:
534: my $safeeval = new Safe;
535: my $safehole = new Safe::Hole;
536: &init_safespace($target,$safeeval,$safehole,$safeinit);
537: #-------------------- Redefinition of the target in the case of compound target
538:
539: ($target, my @tenta) = split('&&',$target);
540:
541: my @stack = ();
542: my @parstack = ();
543: &initdepth;
544:
545: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
546: $safeeval,\%style_for_target);
547: if ($ENV{'request.uri'}) {
548: &writeallows($ENV{'request.uri'});
549: }
550: return $finaloutput;
551: }
552:
553: sub htmlclean {
554: my ($raw,$full)=@_;
555:
556: my $tree = HTML::TreeBuilder->new;
557: $tree->ignore_unknown(0);
558:
559: $tree->parse($raw);
560:
561: my $output= $tree->as_HTML(undef,' ');
562:
563: $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
564: $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
565: unless ($full) {
566: $output=~s/\<[\/]*(body|head|html)\>//gis;
567: }
568:
569: $tree = $tree->delete;
570:
571: return $output;
572: }
573:
574: sub latex_special_symbols {
575: my ($current_token,$stack,$parstack)=@_;
576: $current_token=~s/\\/\\char92 /g;
577: $current_token=~s/\^/\\char94 /g;
578: $current_token=~s/\~/\\char126 /g;
579: $current_token=~s/(&[^a-z\#])/\\$1/g;
580: if ($current_token=~/ \#\w/) {
581: $current_token=~s/ \#(\w)/ \\#$1/;
582: } else {
583: $current_token=~s/([^&]\#)/\\$1/g;
584: }
585: $current_token=~s/(\$|_|{|})/\\$1/g;
586: $current_token=~s/\\char92 /\\texttt{\\char92}/g;
587: $current_token=~s/>/\$>\$/g; #more
588: $current_token=~s/</\$<\$/g; #less
589: if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit
590: if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space
591: return $current_token;
592: }
593:
594: sub inner_xmlparse {
595: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
596: my $finaloutput = '';
597: my $result;
598: my $token;
599: while ( $#$pars > -1 ) {
600: while ($token = $$pars['-1']->get_token) {
601: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
602: if ($metamode<1) {
603: my $text=$token->[1];
604: if ($token->[0] eq 'C' && $target eq 'tex') {
605: $text = '%'.$text;
606: $text =~ s/[\n\r]//g;
607: }
608: $result.=$text;
609: }
610: } elsif ($token->[0] eq 'PI') {
611: if ($metamode<1) {
612: $result=$token->[2];
613: }
614: } elsif ($token->[0] eq 'S') {
615: # add tag to stack
616: push (@$stack,$token->[1]);
617: # add parameters list to another stack
618: push (@$parstack,&parstring($token));
619: &increasedepth($token);
620: if (exists $$style_for_target{$token->[1]}) {
621: if ($Apache::lonxml::redirection) {
622: $Apache::lonxml::outputstack['-1'] .=
623: &recurse($$style_for_target{$token->[1]},$target,$safeeval,
624: $style_for_target,@$parstack);
625: } else {
626: $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
627: $safeeval,$style_for_target,@$parstack);
628: }
629: } else {
630: $result = &callsub("start_$token->[1]", $target, $token, $stack,
631: $parstack, $pars, $safeeval, $style_for_target);
632: }
633: } elsif ($token->[0] eq 'E') {
634: #clear out any tags that didn't end
635: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
636: my $lasttag=$$stack[-1];
637: if ($token->[1] =~ /^$lasttag$/i) {
638: &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
639: last;
640: } else {
641: &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> in file');
642: &end_tag($stack,$parstack,$token);
643: }
644: }
645:
646: if (exists($$style_for_target{'/'."$token->[1]"})) {
647: if ($Apache::lonxml::redirection) {
648: $Apache::lonxml::outputstack['-1'] .=
649: &recurse($$style_for_target{'/'."$token->[1]"},
650: $target,$safeeval,$style_for_target,@$parstack);
651: } else {
652: $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
653: $target,$safeeval,$style_for_target,
654: @$parstack);
655: }
656: } else {
657: $result = &callsub("end_$token->[1]", $target, $token, $stack,
658: $parstack, $pars,$safeeval, $style_for_target);
659: }
660: } else {
661: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
662: }
663: #evaluate variable refs in result
664: if ($result ne "") {
665: if ( $#$parstack > -1 ) {
666: $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]);
667: } else {
668: $result= &Apache::run::evaluate($result,$safeeval,'');
669: }
670: }
671: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
672: if ($target eq 'tex') {
673: $result=&latex_special_symbols($result,$stack,$parstack);
674: }
675: }
676:
677: # Encode any high ASCII characters
678: if (!$Apache::lonxml::prevent_entity_encode) {
679: $result=&HTML::Entities::encode($result,"\200-\377");
680: }
681: if ($Apache::lonxml::redirection) {
682: $Apache::lonxml::outputstack['-1'] .= $result;
683: } else {
684: $finaloutput.=$result;
685: }
686: $result = '';
687:
688: if ($token->[0] eq 'E') {
689: &end_tag($stack,$parstack,$token);
690: }
691: }
692: pop @$pars;
693: pop @Apache::lonxml::pwd;
694: }
695:
696: # if ($target eq 'meta') {
697: # $finaloutput.=&endredirection;
698: # }
699:
700:
701: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
702: $finaloutput=&afterburn($finaloutput);
703: }
704: return $finaloutput;
705: }
706:
707: sub recurse {
708: my @innerstack = ();
709: my @innerparstack = ();
710: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
711: my @pat = ();
712: &newparser(\@pat,\$newarg);
713: my $tokenpat;
714: my $partstring = '';
715: my $output='';
716: my $decls='';
717: &Apache::lonxml::debug("Recursing");
718: while ( $#pat > -1 ) {
719: while ($tokenpat = $pat[$#pat]->get_token) {
720: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
721: if ($metamode<1) { $partstring=$tokenpat->[1]; }
722: } elsif ($tokenpat->[0] eq 'PI') {
723: if ($metamode<1) { $partstring=$tokenpat->[2]; }
724: } elsif ($tokenpat->[0] eq 'S') {
725: push (@innerstack,$tokenpat->[1]);
726: push (@innerparstack,&parstring($tokenpat));
727: &increasedepth($tokenpat);
728: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
729: \@innerstack, \@innerparstack, \@pat,
730: $safeeval, $style_for_target);
731: } elsif ($tokenpat->[0] eq 'E') {
732: #clear out any tags that didn't end
733: while ($tokenpat->[1] ne $innerstack[$#innerstack]
734: && ($#innerstack > -1)) {
735: my $lasttag=$innerstack[-1];
736: if ($tokenpat->[1] =~ /^$lasttag$/i) {
737: &Apache::lonxml::warning('Using tag </'.$tokenpat->[1].'> as end tag to <'.$innerstack[-1].'>');
738: last;
739: } else {
740: &Apache::lonxml::warning('Found tag </'.$tokenpat->[1].'> when looking for </'.$innerstack[-1].'> in file');
741: &end_tag(\@innerstack,\@innerparstack,$tokenpat);
742: }
743: }
744: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
745: \@innerstack, \@innerparstack, \@pat,
746: $safeeval, $style_for_target);
747: } else {
748: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
749: }
750: #pass both the variable to the style tag, and the tag we
751: #are processing inside the <definedtag>
752: if ( $partstring ne "" ) {
753: if ( $#parstack > -1 ) {
754: if ( $#innerparstack > -1 ) {
755: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
756: } else {
757: $decls= $parstack[$#parstack];
758: }
759: } else {
760: if ( $#innerparstack > -1 ) {
761: $decls=$innerparstack[$#innerparstack];
762: } else {
763: $decls='';
764: }
765: }
766: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
767: $partstring = '';
768: }
769: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
770: &decreasedepth($tokenpat);}
771: }
772: pop @pat;
773: pop @Apache::lonxml::pwd;
774: }
775: &Apache::lonxml::debug("Exiting Recursing");
776: return $output;
777: }
778:
779: sub callsub {
780: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
781: my $currentstring='';
782: my $nodefault;
783: {
784: my $sub1;
785: no strict 'refs';
786: my $tag=$token->[1];
787: my $space=$Apache::lonxml::alltags{$tag}[-1];
788: if (!$space) {
789: $tag=~tr/A-Z/a-z/;
790: $sub=~tr/A-Z/a-z/;
791: $space=$Apache::lonxml::alltags{$tag}[-1]
792: }
793:
794: my $deleted=0;
795: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
796: if (($token->[0] eq 'S') && ($target eq 'modified')) {
797: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
798: $parstack,$parser,$safeeval,
799: $style);
800: }
801: if (!$deleted) {
802: if ($space) {
803: &Apache::lonxml::debug("Calling sub $sub in $space $metamode");
804: $sub1="$space\:\:$sub";
805: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
806: $parstack,$parser,$safeeval,
807: $style);
808: } else {
809: &Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
810: if ($metamode <1) {
811: if (defined($token->[4]) && ($metamode < 1)) {
812: $currentstring = $token->[4];
813: } else {
814: $currentstring = $token->[2];
815: }
816: }
817: }
818: # &Apache::lonxml::debug("nodefalt:$nodefault:");
819: if ($currentstring eq '' && $nodefault eq '') {
820: if ($target eq 'edit') {
821: &Apache::lonxml::debug("doing default edit for $token->[1]");
822: if ($token->[0] eq 'S') {
823: $currentstring = &Apache::edit::tag_start($target,$token);
824: } elsif ($token->[0] eq 'E') {
825: $currentstring = &Apache::edit::tag_end($target,$token);
826: }
827: } elsif ($target eq 'modified') {
828: if ($token->[0] eq 'S') {
829: $currentstring = $token->[4];
830: $currentstring.=&Apache::edit::handle_insert();
831: } else {
832: $currentstring = $token->[2];
833: }
834: }
835: }
836: }
837: use strict 'refs';
838: }
839: return $currentstring;
840: }
841:
842: sub setup_globals {
843: my ($request,$target)=@_;
844: $Apache::lonxml::request=$request;
845: $Apache::lonxml::registered = 0;
846: @Apache::lonxml::pwd=();
847: @Apache::lonxml::extlinks=();
848: if ($target eq 'meta') {
849: $Apache::lonxml::redirection = 0;
850: $Apache::lonxml::metamode = 1;
851: $Apache::lonxml::evaluate = 1;
852: $Apache::lonxml::import = 0;
853: } elsif ($target eq 'answer') {
854: $Apache::lonxml::redirection = 0;
855: $Apache::lonxml::metamode = 1;
856: $Apache::lonxml::evaluate = 1;
857: $Apache::lonxml::import = 1;
858: } elsif ($target eq 'grade') {
859: &startredirection;
860: $Apache::lonxml::metamode = 0;
861: $Apache::lonxml::evaluate = 1;
862: $Apache::lonxml::import = 1;
863: } elsif ($target eq 'modified') {
864: $Apache::lonxml::redirection = 0;
865: $Apache::lonxml::metamode = 0;
866: $Apache::lonxml::evaluate = 0;
867: $Apache::lonxml::import = 0;
868: } elsif ($target eq 'edit') {
869: $Apache::lonxml::redirection = 0;
870: $Apache::lonxml::metamode = 0;
871: $Apache::lonxml::evaluate = 0;
872: $Apache::lonxml::import = 0;
873: } elsif ($target eq 'analyze') {
874: $Apache::lonxml::redirection = 0;
875: $Apache::lonxml::metamode = 0;
876: $Apache::lonxml::evaluate = 1;
877: $Apache::lonxml::import = 1;
878: } else {
879: $Apache::lonxml::redirection = 0;
880: $Apache::lonxml::metamode = 0;
881: $Apache::lonxml::evaluate = 1;
882: $Apache::lonxml::import = 1;
883: }
884: }
885:
886: sub init_safespace {
887: my ($target,$safeeval,$safehole,$safeinit) = @_;
888: $safeeval->permit("entereval");
889: $safeeval->permit(":base_math");
890: $safeeval->permit("sort");
891: $safeeval->deny(":base_io");
892: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
893: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
894:
895: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
896: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
897: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
898: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
899: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
900: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
901: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
902: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
903: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
904: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
905: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
906: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
907: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
908: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
909: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
910: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
911: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
912: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
913: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
914: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
915: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
916: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
917: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
918: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
919: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
920: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
921: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
922: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
923: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
924: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
925: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
926: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
927: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
928: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
929: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
930: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
931: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
932: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
933: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
934: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
935:
936: #need to inspect this class of ops
937: # $safeeval->deny(":base_orig");
938: $safeinit .= ';$external::target="'.$target.'";';
939: my $rndseed;
940: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
941: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
942: $safeinit .= ';$external::randomseed='.$rndseed.';';
943: &Apache::run::run($safeinit,$safeeval);
944: }
945:
946: sub startredirection {
947: $Apache::lonxml::redirection++;
948: push (@Apache::lonxml::outputstack, '');
949: }
950:
951: sub endredirection {
952: if (!$Apache::lonxml::redirection) {
953: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
954: return '';
955: }
956: $Apache::lonxml::redirection--;
957: pop @Apache::lonxml::outputstack;
958: }
959:
960: sub end_tag {
961: my ($tagstack,$parstack,$token)=@_;
962: pop(@$tagstack);
963: pop(@$parstack);
964: &decreasedepth($token);
965: }
966:
967: sub initdepth {
968: @Apache::lonxml::depthcounter=();
969: $Apache::lonxml::depth=-1;
970: $Apache::lonxml::olddepth=-1;
971: }
972:
973: sub increasedepth {
974: my ($token) = @_;
975: $Apache::lonxml::depth++;
976: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
977: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
978: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
979: }
980: my $curdepth=join('_',@Apache::lonxml::depthcounter);
981: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
982: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
983: }
984:
985: sub decreasedepth {
986: my ($token) = @_;
987: $Apache::lonxml::depth--;
988: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
989: $#Apache::lonxml::depthcounter--;
990: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
991: }
992: if ( $Apache::lonxml::depth < -1) {
993: &Apache::lonxml::warning("Missing tags, unable to properly run file.");
994: $Apache::lonxml::depth='-1';
995: }
996: my $curdepth=join('_',@Apache::lonxml::depthcounter);
997: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
998: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
999: }
1000:
1001: sub get_all_text_unbalanced {
1002: #there is a copy of this in lonpublisher.pm
1003: my($tag,$pars)= @_;
1004: my $token;
1005: my $result='';
1006: $tag='<'.$tag.'>';
1007: while ($token = $$pars[-1]->get_token) {
1008: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1009: $result.=$token->[1];
1010: } elsif ($token->[0] eq 'PI') {
1011: $result.=$token->[2];
1012: } elsif ($token->[0] eq 'S') {
1013: $result.=$token->[4];
1014: } elsif ($token->[0] eq 'E') {
1015: $result.=$token->[2];
1016: }
1017: if ($result =~ /(.*)$tag(.*)/) {
1018: &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
1019: &Apache::lonxml::debug('Result is :'.$1);
1020: $result=$1;
1021: my $redo=$tag.$2;
1022: &Apache::lonxml::newparser($pars,\$redo);
1023: last;
1024: }
1025: }
1026: return $result
1027: }
1028:
1029: sub get_all_text {
1030: my($tag,$pars)= @_;
1031: my $depth=0;
1032: my $token;
1033: my $result='';
1034: if ( $tag =~ m:^/: ) {
1035: my $tag=substr($tag,1);
1036: # &Apache::lonxml::debug("have:$tag:");
1037: while (($depth >=0) && ($token = $pars->get_token)) {
1038: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
1039: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1040: $result.=$token->[1];
1041: } elsif ($token->[0] eq 'PI') {
1042: $result.=$token->[2];
1043: } elsif ($token->[0] eq 'S') {
1044: if ($token->[1] =~ /^$tag$/i) { $depth++; }
1045: $result.=$token->[4];
1046: } elsif ($token->[0] eq 'E') {
1047: if ( $token->[1] =~ /^$tag$/i) { $depth--; }
1048: #skip sending back the last end tag
1049: if ($depth > -1) { $result.=$token->[2]; } else {
1050: $pars->unget_token($token);
1051: }
1052: }
1053: }
1054: } else {
1055: while ($token = $pars->get_token) {
1056: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1057: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1058: $result.=$token->[1];
1059: } elsif ($token->[0] eq 'PI') {
1060: $result.=$token->[2];
1061: } elsif ($token->[0] eq 'S') {
1062: if ( $token->[1] =~ /^$tag$/i) {
1063: $pars->unget_token($token); last;
1064: } else {
1065: $result.=$token->[4];
1066: }
1067: } elsif ($token->[0] eq 'E') {
1068: $result.=$token->[2];
1069: }
1070: }
1071: }
1072: # &Apache::lonxml::debug("Exit:$result:");
1073: return $result
1074: }
1075:
1076: sub newparser {
1077: my ($parser,$contentref,$dir) = @_;
1078: push (@$parser,HTML::LCParser->new($contentref));
1079: $$parser['-1']->xml_mode('1');
1080: if ( $dir eq '' ) {
1081: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1082: } else {
1083: push (@Apache::lonxml::pwd, $dir);
1084: }
1085: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
1086: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
1087: }
1088:
1089: sub parstring {
1090: my ($token) = @_;
1091: my $temp='';
1092: foreach (@{$token->[3]}) {
1093: unless ($_=~/\W/) {
1094: my $val=$token->[2]->{$_};
1095: $val =~ s/([\%\@\\\"])/\\$1/g;
1096: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1097: $temp .= "my \$$_=\"$val\";"
1098: }
1099: }
1100: return $temp;
1101: }
1102:
1103: sub writeallows {
1104: unless ($#extlinks>=0) { return; }
1105: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1106: if ($ENV{'httpref.'.$thisurl}) {
1107: $thisurl=$ENV{'httpref.'.$thisurl};
1108: }
1109: my $thisdir=$thisurl;
1110: $thisdir=~s/\/[^\/]+$//;
1111: my %httpref=();
1112: foreach (@extlinks) {
1113: $httpref{'httpref.'.
1114: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1115: }
1116: @extlinks=();
1117: &Apache::lonnet::appenv(%httpref);
1118: }
1119:
1120: #
1121: # Afterburner handles anchors, highlights and links
1122: #
1123: sub afterburn {
1124: my $result=shift;
1125: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1126: ['highlight','anchor','link']);
1127: if ($ENV{'form.highlight'}) {
1128: foreach (split(/\,/,$ENV{'form.highlight'})) {
1129: my $anchorname=$_;
1130: my $matchthis=$anchorname;
1131: $matchthis=~s/\_+/\\s\+/g;
1132: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1133: }
1134: }
1135: if ($ENV{'form.link'}) {
1136: foreach (split(/\,/,$ENV{'form.link'})) {
1137: my ($anchorname,$linkurl)=split(/\>/,$_);
1138: my $matchthis=$anchorname;
1139: $matchthis=~s/\_+/\\s\+/g;
1140: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1141: }
1142: }
1143: if ($ENV{'form.anchor'}) {
1144: my $anchorname=$ENV{'form.anchor'};
1145: my $matchthis=$anchorname;
1146: $matchthis=~s/\_+/\\s\+/g;
1147: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1148: $result.=(<<"ENDSCRIPT");
1149: <script>
1150: document.location.hash='$anchorname';
1151: </script>
1152: ENDSCRIPT
1153: }
1154: return $result;
1155: }
1156:
1157: sub storefile {
1158: my ($file,$contents)=@_;
1159: if (my $fh=Apache::File->new('>'.$file)) {
1160: print $fh $contents;
1161: $fh->close();
1162: } else {
1163: &warning("Unable to save file $file");
1164: }
1165: }
1166:
1167: sub createnewhtml {
1168: my $filecontents=(<<SIMPLECONTENT);
1169: <html>
1170: <head>
1171: <title>
1172: Title of Document Goes Here
1173: </title>
1174: </head>
1175: <body bgcolor="#FFFFFF">
1176:
1177: Body of Document Goes Here
1178:
1179: </body>
1180: </html>
1181: SIMPLECONTENT
1182: return $filecontents;
1183: }
1184:
1185:
1186: sub inserteditinfo {
1187: my ($result,$filecontents)=@_;
1188: $filecontents = &HTML::Entities::encode($filecontents);
1189: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1190: my $buttons=(<<BUTTONS);
1191: <input type="submit" name="attemptclean"
1192: value="Save and then attempt to clean HTML" />
1193: <input type="submit" name="savethisfile" value="Save this" />
1194: <input type="submit" name="viewmode" value="View" />
1195: BUTTONS
1196: my $editfooter=(<<ENDFOOTER);
1197: <hr />
1198: <a name="editsection" />
1199: <form method="post">
1200: <input type="hidden" name="editmode" value="Edit" />
1201: $buttons<br />
1202: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1203: <br />$buttons
1204: <br />
1205: </form>
1206: ENDFOOTER
1207: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1208: $result=~s/(\<\/body\>)/$editfooter/is;
1209: return $result;
1210: }
1211:
1212: sub get_target {
1213: my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1214: if ( $ENV{'request.state'} eq 'published') {
1215: if ( defined($ENV{'form.grade_target'})
1216: && ($viewgrades == 'F' )) {
1217: return ($ENV{'form.grade_target'});
1218: } elsif (defined($ENV{'form.grade_target'})) {
1219: if (($ENV{'form.grade_target'} eq 'web') ||
1220: ($ENV{'form.grade_target'} eq 'tex') ) {
1221: return $ENV{'form.grade_target'}
1222: } else {
1223: return 'web';
1224: }
1225: } else {
1226: return 'web';
1227: }
1228: } elsif ($ENV{'request.state'} eq 'construct') {
1229: if ( defined($ENV{'form.grade_target'})) {
1230: return ($ENV{'form.grade_target'});
1231: } else {
1232: return 'web';
1233: }
1234: } else {
1235: return 'web';
1236: }
1237: }
1238:
1239: sub handler {
1240: my $request=shift;
1241:
1242: my $target=&get_target();
1243:
1244: $Apache::lonxml::debug=0;
1245:
1246: if ($ENV{'browser.mathml'}) {
1247: $request->content_type('text/xml');
1248: } else {
1249: $request->content_type('text/html');
1250: }
1251: &Apache::loncommon::no_cache($request);
1252: $request->send_http_header;
1253:
1254: return OK if $request->header_only;
1255:
1256:
1257: my $file=&Apache::lonnet::filelocation("",$request->uri);
1258: #
1259: # Edit action? Save file.
1260: #
1261: unless ($ENV{'request.state'} eq 'published') {
1262: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1263: &storefile($file,$ENV{'form.filecont'});
1264: }
1265: }
1266: my %mystyle;
1267: my $result = '';
1268: my $filecontents=&Apache::lonnet::getfile($file);
1269: if ($filecontents == -1) {
1270: $result=(<<ENDNOTFOUND);
1271: <html>
1272: <head>
1273: <title>File not found</title>
1274: </head>
1275: <body bgcolor="#FFFFFF">
1276: <b>File not found: $file</b>
1277: </body>
1278: </html>
1279: ENDNOTFOUND
1280: $filecontents='';
1281: if ($ENV{'request.state'} ne 'published') {
1282: $filecontents=&createnewhtml();
1283: $ENV{'form.editmode'}='Edit'; #force edit mode
1284: }
1285: } else {
1286: unless ($ENV{'request.state'} eq 'published') {
1287: if ($ENV{'form.attemptclean'}) {
1288: $filecontents=&htmlclean($filecontents,1);
1289: }
1290: }
1291: if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1292: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1293: '',%mystyle);
1294: }
1295: }
1296:
1297: #
1298: # Edit action? Insert editing commands
1299: #
1300: unless ($ENV{'request.state'} eq 'published') {
1301: if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1302: my $displayfile=$request->uri;
1303: $displayfile=~s/^\/[^\/]*//;
1304: $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
1305: '</h3></body></html>';
1306: $result=&inserteditinfo($result,$filecontents);
1307: }
1308: }
1309:
1310: writeallows($request->uri);
1311:
1312: $request->print($result);
1313:
1314: return OK;
1315: }
1316:
1317: sub debug {
1318: if ($Apache::lonxml::debug eq 1) {
1319: $|=1;
1320: print("DEBUG:".&HTML::Entities::encode($_[0])."<br />\n");
1321: }
1322: }
1323:
1324: sub error {
1325: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1326: # If printing in construction space, put the error inside <pre></pre>
1327: print "<b>ERROR:</b>".join("\n",@_)."\n";
1328: } else {
1329: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1330: #notify author
1331: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1332: #notify course
1333: if ( $ENV{'request.course.id'} ) {
1334: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1335: my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1336: foreach my $user (split /\,/, $users) {
1337: ($user,my $domain) = split /:/, $user;
1338: &Apache::lonmsg::user_normal_msg($user,$domain,
1339: "Error [$declutter]",join('<br />',@_));
1340: }
1341: }
1342:
1343: #FIXME probably shouldn't have me get everything forever.
1344: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1345: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1346: }
1347: }
1348:
1349: sub warning {
1350: if ($ENV{'request.state'} eq 'construct') {
1351: print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1352: }
1353: }
1354:
1355: sub get_param {
1356: my ($param,$parstack,$safeeval,$context) = @_;
1357: if ( ! $context ) { $context = -1; }
1358: my $args ='';
1359: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1360: if ( ! $args ) { return undef; }
1361: if ( $args =~ /my \$$param=\"/ ) {
1362: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1363: } else {
1364: return undef;
1365: }
1366: }
1367:
1368: sub get_param_var {
1369: my ($param,$parstack,$safeeval,$context) = @_;
1370: if ( ! $context ) { $context = -1; }
1371: my $args ='';
1372: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1373: if ( $args !~ /my \$$param=\"/ ) { return undef; }
1374: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1375: if ($value =~ /^[\$\@\%]/) {
1376: return &Apache::run::run("return $value",$safeeval,1);
1377: } else {
1378: return $value;
1379: }
1380: }
1381:
1382: sub register_insert {
1383: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1384: my $i;
1385: my $tagnum=0;
1386: my @order;
1387: for ($i=0;$i < $#data; $i++) {
1388: my $line = $data[$i];
1389: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1390: if ( $line =~ /TABLE/ ) { last; }
1391: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1392: if ($tag) {
1393: $insertlist{"$tagnum.tag"} = $tag;
1394: $insertlist{"$tagnum.description"} = $descrip;
1395: $insertlist{"$tagnum.color"} = $color;
1396: $insertlist{"$tagnum.function"} = $function;
1397: if (!defined($show)) { $show='yes'; }
1398: $insertlist{"$tagnum.show"}= $show;
1399: $insertlist{"$tag.num"}=$tagnum;
1400: $tagnum++;
1401: }
1402: }
1403: $i++; #skipping TABLE line
1404: $tagnum = 0;
1405: for (;$i < $#data;$i++) {
1406: my $line = $data[$i];
1407: my ($mnemonic,@which) = split(/ +/,$line);
1408: my $tag = $insertlist{"$tagnum.tag"};
1409: for (my $j=0;$j <=$#which;$j++) {
1410: if ( $which[$j] eq 'Y' ) {
1411: if ($insertlist{"$j.show"} ne 'no') {
1412: push(@{ $insertlist{"$tag.which"} },$j);
1413: }
1414: }
1415: }
1416: $tagnum++;
1417: }
1418: }
1419:
1420: sub description {
1421: my ($token)=@_;
1422: my $tagnum;
1423: my $tag=$token->[1];
1424: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1425: my $testtag=$namespace.'::'.$tag;
1426: $tagnum=$insertlist{"$testtag.num"};
1427: if (defined($tagnum)) { last; }
1428: }
1429: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1430: return $insertlist{$tagnum.'.description'};
1431: }
1432:
1433: # ----------------------------------------------------------------- whichuser
1434: # returns a list of $symb, $courseid, $domain, $name that is correct for
1435: # calls to lonnet functions for this setup.
1436: # - looks for form.grade_ parameters
1437: sub whichuser {
1438: my ($symb,$courseid,$domain,$name);
1439: if (defined($ENV{'form.grade_symb'})) {
1440: my $tmp_courseid=$ENV{'form.grade_courseid'};
1441: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1442: if ($allowed) {
1443: $symb=$ENV{'form.grade_symb'};
1444: $courseid=$ENV{'form.grade_courseid'};
1445: $domain=$ENV{'form.grade_domain'};
1446: $name=$ENV{'form.grade_username'};
1447: }
1448: } else {
1449: $symb=&Apache::lonnet::symbread();
1450: $courseid=$ENV{'request.course.id'};
1451: $domain=$ENV{'user.domain'};
1452: $name=$ENV{'user.name'};
1453: }
1454: return ($symb,$courseid,$domain,$name);
1455: }
1456:
1457: 1;
1458: __END__
1459:
1460:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>