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