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