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