1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # $Id: lonxml.pm,v 1.243 2003/03/30 21:58:17 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
39: # last modified 06/26/00 by Alexander Sakharuk
40: # 11/6 Gerd Kortemeyer
41: # 6/1/1 Gerd Kortemeyer
42: # 2/21,3/13 Guy
43: # 3/29,5/4 Gerd Kortemeyer
44: # 5/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: # do we have a course style file?
406: #
407:
408: if ($ENV{'request.course.id'} && $ENV{'request.state'} ne 'construct') {
409: my $bodytext=
410: $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
411: if ($bodytext) {
412: my $location=&Apache::lonnet::filelocation('',$bodytext);
413: my $styletext=&Apache::lonnet::getfile($location);
414: if ($styletext ne '-1') {
415: %style_for_target = (%style_for_target,
416: &Apache::style::styleparser($target,$styletext));
417: }
418: }
419: }
420:
421: #&printalltags();
422: my @pars = ();
423: my $pwd=$ENV{'request.filename'};
424: $pwd =~ s:/[^/]*$::;
425: &newparser(\@pars,\$content_file_string,$pwd);
426:
427: my $safeeval = new Safe;
428: my $safehole = new Safe::Hole;
429: &init_safespace($target,$safeeval,$safehole,$safeinit);
430: #-------------------- Redefinition of the target in the case of compound target
431:
432: ($target, my @tenta) = split('&&',$target);
433:
434: my @stack = ();
435: my @parstack = ();
436: &initdepth;
437:
438: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
439: $safeeval,\%style_for_target);
440: if ($ENV{'request.uri'}) {
441: &writeallows($ENV{'request.uri'});
442: }
443: if ($Apache::lonxml::counter_changed) { &store_counter() }
444: return $finaloutput;
445: }
446:
447: sub htmlclean {
448: my ($raw,$full)=@_;
449:
450: my $tree = HTML::TreeBuilder->new;
451: $tree->ignore_unknown(0);
452:
453: $tree->parse($raw);
454:
455: my $output= $tree->as_HTML(undef,' ');
456:
457: $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
458: $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
459: unless ($full) {
460: $output=~s/\<[\/]*(body|head|html)\>//gis;
461: }
462:
463: $tree = $tree->delete;
464:
465: return $output;
466: }
467:
468: sub latex_special_symbols {
469: my ($current_token,$stack,$parstack,$where)=@_;
470: if ($where eq 'header') {
471: $current_token =~ s/(\\|_|\^)/ /g;
472: $current_token =~ s/(\$|%|\#|&|\{|\})/\\$1/g;
473: } else {
474: $current_token=~s/\\ /\\char92 /g;
475: $current_token=~s/\^/\\char94 /g;
476: $current_token=~s/\~/\\char126 /g;
477: $current_token=~s/(&[^a-z\#])/\\$1/g;
478: $current_token=~s/([^&])\#/$1\\#/g;
479: $current_token=~s/(\$|_|{|})/\\$1/g;
480: $current_token=~s/\\char92 /\\texttt{\\char92}/g;
481: $current_token=~s/(>|<)/\$$1\$/g; #more or less
482: if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit
483: if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space
484: }
485: return $current_token;
486: }
487:
488: sub inner_xmlparse {
489: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
490: my $finaloutput = '';
491: my $result;
492: my $token;
493: while ( $#$pars > -1 ) {
494: while ($token = $$pars['-1']->get_token) {
495: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
496: if ($metamode<1) {
497: my $text=$token->[1];
498: if ($token->[0] eq 'C' && $target eq 'tex') {
499: $text = '';
500: # $text = '%'.$text."\n";
501: }
502: $result.=$text;
503: }
504: } elsif ($token->[0] eq 'PI') {
505: if ($metamode<1) {
506: $result=$token->[2];
507: }
508: } elsif ($token->[0] eq 'S') {
509: # add tag to stack
510: push (@$stack,$token->[1]);
511: # add parameters list to another stack
512: push (@$parstack,&parstring($token));
513: &increasedepth($token);
514: if ($Apache::lonxml::usestyle &&
515: exists($$style_for_target{$token->[1]})) {
516: $Apache::lonxml::usestyle=0;
517: my $string=$$style_for_target{$token->[1]}.
518: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
519: &Apache::lonxml::newparser($pars,\$string);
520: } else {
521: $result = &callsub("start_$token->[1]", $target, $token, $stack,
522: $parstack, $pars, $safeeval, $style_for_target);
523: }
524: } elsif ($token->[0] eq 'E') {
525: #clear out any tags that didn't end
526: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
527: my $lasttag=$$stack[-1];
528: if ($token->[1] =~ /^$lasttag$/i) {
529: &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
530: last;
531: } else {
532: &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> in file');
533: &end_tag($stack,$parstack,$token);
534: }
535: }
536:
537: if ($Apache::lonxml::usestyle &&
538: exists($$style_for_target{'/'."$token->[1]"})) {
539: $Apache::lonxml::usestyle=0;
540: my $string=$$style_for_target{'/'.$token->[1]}.
541: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
542: &Apache::lonxml::newparser($pars,\$string);
543: } else {
544: $result = &callsub("end_$token->[1]", $target, $token, $stack,
545: $parstack, $pars,$safeeval, $style_for_target);
546: }
547: } else {
548: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
549: }
550: #evaluate variable refs in result
551: if ($result ne "") {
552: if ( $#$parstack > -1 ) {
553: $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]);
554: } else {
555: $result= &Apache::run::evaluate($result,$safeeval,'');
556: }
557: }
558: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
559: if ($target eq 'tex') {
560: $result=&latex_special_symbols($result,$stack,$parstack);
561: }
562: }
563:
564: # Encode any high ASCII characters
565: if (!$Apache::lonxml::prevent_entity_encode) {
566: $result=&HTML::Entities::encode($result,"\200-\377");
567: }
568: if ($Apache::lonxml::redirection) {
569: $Apache::lonxml::outputstack['-1'] .= $result;
570: } else {
571: $finaloutput.=$result;
572: }
573: $result = '';
574:
575: if ($token->[0] eq 'E') {
576: &end_tag($stack,$parstack,$token);
577: }
578: }
579: if ($#$pars > -1) {
580: pop @$pars;
581: pop @Apache::lonxml::pwd;
582: }
583: }
584:
585: # if ($target eq 'meta') {
586: # $finaloutput.=&endredirection;
587: # }
588:
589:
590: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
591: $finaloutput=&afterburn($finaloutput);
592: }
593: return $finaloutput;
594: }
595:
596: sub callsub {
597: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
598: my $currentstring='';
599: my $nodefault;
600: {
601: my $sub1;
602: no strict 'refs';
603: my $tag=$token->[1];
604: # get utterly rid of extended html tags
605: if ($tag=~/^x\-/i) { return ''; }
606: my $space=$Apache::lonxml::alltags{$tag}[-1];
607: if (!$space) {
608: $tag=~tr/A-Z/a-z/;
609: $sub=~tr/A-Z/a-z/;
610: $space=$Apache::lonxml::alltags{$tag}[-1]
611: }
612:
613: my $deleted=0;
614: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
615: if (($token->[0] eq 'S') && ($target eq 'modified')) {
616: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
617: $parstack,$parser,$safeeval,
618: $style);
619: }
620: if (!$deleted) {
621: if ($space) {
622: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
623: $sub1="$space\:\:$sub";
624: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
625: $parstack,$parser,$safeeval,
626: $style);
627: } else {
628: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
629: if ($metamode <1) {
630: if (defined($token->[4]) && ($metamode < 1)) {
631: $currentstring = $token->[4];
632: } else {
633: $currentstring = $token->[2];
634: }
635: }
636: }
637: # &Apache::lonxml::debug("nodefalt:$nodefault:");
638: if ($currentstring eq '' && $nodefault eq '') {
639: if ($target eq 'edit') {
640: #&Apache::lonxml::debug("doing default edit for $token->[1]");
641: if ($token->[0] eq 'S') {
642: $currentstring = &Apache::edit::tag_start($target,$token);
643: } elsif ($token->[0] eq 'E') {
644: $currentstring = &Apache::edit::tag_end($target,$token);
645: }
646: } elsif ($target eq 'modified') {
647: if ($token->[0] eq 'S') {
648: $currentstring = $token->[4];
649: $currentstring.=&Apache::edit::handle_insert();
650: } elsif ($token->[0] eq 'E') {
651: $currentstring = $token->[2];
652: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
653: } else {
654: $currentstring = $token->[2];
655: }
656: }
657: }
658: }
659: use strict 'refs';
660: }
661: return $currentstring;
662: }
663:
664: sub setup_globals {
665: my ($request,$target)=@_;
666: $Apache::lonxml::request=$request;
667: $Apache::lonxml::registered = 0;
668: $errorcount=0;
669: $warningcount=0;
670: $Apache::lonxml::default_homework_loaded=0;
671: $Apache::lonxml::usestyle=1;
672: &init_counter();
673: @Apache::lonxml::pwd=();
674: @Apache::lonxml::extlinks=();
675: if ($target eq 'meta') {
676: $Apache::lonxml::redirection = 0;
677: $Apache::lonxml::metamode = 1;
678: $Apache::lonxml::evaluate = 1;
679: $Apache::lonxml::import = 0;
680: } elsif ($target eq 'answer') {
681: $Apache::lonxml::redirection = 0;
682: $Apache::lonxml::metamode = 1;
683: $Apache::lonxml::evaluate = 1;
684: $Apache::lonxml::import = 1;
685: } elsif ($target eq 'grade') {
686: &startredirection;
687: $Apache::lonxml::metamode = 0;
688: $Apache::lonxml::evaluate = 1;
689: $Apache::lonxml::import = 1;
690: } elsif ($target eq 'modified') {
691: $Apache::lonxml::redirection = 0;
692: $Apache::lonxml::metamode = 0;
693: $Apache::lonxml::evaluate = 0;
694: $Apache::lonxml::import = 0;
695: } elsif ($target eq 'edit') {
696: $Apache::lonxml::redirection = 0;
697: $Apache::lonxml::metamode = 0;
698: $Apache::lonxml::evaluate = 0;
699: $Apache::lonxml::import = 0;
700: } elsif ($target eq 'analyze') {
701: $Apache::lonxml::redirection = 0;
702: $Apache::lonxml::metamode = 0;
703: $Apache::lonxml::evaluate = 1;
704: $Apache::lonxml::import = 1;
705: } else {
706: $Apache::lonxml::redirection = 0;
707: $Apache::lonxml::metamode = 0;
708: $Apache::lonxml::evaluate = 1;
709: $Apache::lonxml::import = 1;
710: }
711: }
712:
713: sub init_safespace {
714: my ($target,$safeeval,$safehole,$safeinit) = @_;
715: $safeeval->permit("entereval");
716: $safeeval->permit(":base_math");
717: $safeeval->permit("sort");
718: $safeeval->deny(":base_io");
719: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
720: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
721:
722: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
723: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
724: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
725: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
726: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
727: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
728: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
729: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
730: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
731: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
732: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
733: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
734: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
735: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
736: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
737: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
738: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
739: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
740: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
741:
742: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
743: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
744: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
745: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
746: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
747: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
748: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
749: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
750: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
751: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
752: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
753: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
754: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
755: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
756: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
757: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
758: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
759: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
760: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
761: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
762: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
763: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
764:
765: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
766: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
767: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
768: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
769: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
770: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
771:
772: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
773: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
774: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
775: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
776: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
777: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
778: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
779: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
780: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
781: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
782: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
783: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
784: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
785: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
786: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
787: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
788: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
789: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
790: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
791: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
792: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
793:
794: #need to inspect this class of ops
795: # $safeeval->deny(":base_orig");
796: $safeinit .= ';$external::target="'.$target.'";';
797: my $rndseed;
798: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
799: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
800: $safeinit .= ';$external::randomseed='.$rndseed.';';
801: &Apache::run::run($safeinit,$safeeval);
802: }
803:
804: sub default_homework_load {
805: my ($safeeval)=@_;
806: &Apache::lonxml::debug('Loading default_homework');
807: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
808: if ($default eq -1) {
809: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
810: } else {
811: &Apache::run::run($default,$safeeval);
812: $Apache::lonxml::default_homework_loaded=1;
813: }
814: }
815:
816: sub startredirection {
817: $Apache::lonxml::redirection++;
818: push (@Apache::lonxml::outputstack, '');
819: }
820:
821: sub endredirection {
822: if (!$Apache::lonxml::redirection) {
823: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
824: return '';
825: }
826: $Apache::lonxml::redirection--;
827: pop @Apache::lonxml::outputstack;
828: }
829:
830: sub end_tag {
831: my ($tagstack,$parstack,$token)=@_;
832: pop(@$tagstack);
833: pop(@$parstack);
834: &decreasedepth($token);
835: }
836:
837: sub initdepth {
838: @Apache::lonxml::depthcounter=();
839: $Apache::lonxml::depth=-1;
840: $Apache::lonxml::olddepth=-1;
841: }
842:
843: sub increasedepth {
844: my ($token) = @_;
845: $Apache::lonxml::depth++;
846: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
847: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
848: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
849: }
850: my $curdepth=join('_',@Apache::lonxml::depthcounter);
851: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
852: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
853: }
854:
855: sub decreasedepth {
856: my ($token) = @_;
857: $Apache::lonxml::depth--;
858: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
859: $#Apache::lonxml::depthcounter--;
860: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
861: }
862: if ( $Apache::lonxml::depth < -1) {
863: &Apache::lonxml::warning("Missing tags, unable to properly run file.");
864: $Apache::lonxml::depth='-1';
865: }
866: my $curdepth=join('_',@Apache::lonxml::depthcounter);
867: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
868: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
869: }
870:
871: sub get_all_text_unbalanced {
872: #there is a copy of this in lonpublisher.pm
873: my($tag,$pars)= @_;
874: my $token;
875: my $result='';
876: $tag='<'.$tag.'>';
877: while ($token = $$pars[-1]->get_token) {
878: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
879: $result.=$token->[1];
880: } elsif ($token->[0] eq 'PI') {
881: $result.=$token->[2];
882: } elsif ($token->[0] eq 'S') {
883: $result.=$token->[4];
884: } elsif ($token->[0] eq 'E') {
885: $result.=$token->[2];
886: }
887: if ($result =~ /(.*)\Q$tag\E(.*)/s) {
888: &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
889: &Apache::lonxml::debug('Result is :'.$1);
890: $result=$1;
891: my $redo=$tag.$2;
892: &Apache::lonxml::newparser($pars,\$redo);
893: last;
894: }
895: }
896: return $result
897: }
898:
899: sub increment_counter {
900: $Apache::lonxml::counter++;
901: $Apache::lonxml::counter_changed=1;
902: }
903:
904: sub init_counter {
905: if (defined($ENV{'form.counter'})) {
906: $Apache::lonxml::counter=$ENV{'form.counter'};
907: } else {
908: $Apache::lonxml::counter=1;
909: &store_counter();
910: }
911: $Apache::lonxml::counter_changed=0;
912: }
913:
914: sub store_counter {
915: &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
916: return '';
917: }
918:
919: sub get_all_text {
920: my($tag,$pars)= @_;
921: &Apache::lonxml::debug("Got a ".ref($pars));
922: my $gotfullstack=1;
923: if (ref($pars) ne 'ARRAY') {
924: $gotfullstack=0;
925: $pars=[$pars];
926: }
927: my $depth=0;
928: my $token;
929: my $result='';
930: if ( $tag =~ m:^/: ) {
931: my $tag=substr($tag,1);
932: #&Apache::lonxml::debug("have:$tag:");
933: my $top_empty=0;
934: while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
935: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
936: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
937: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
938: $result.=$token->[1];
939: } elsif ($token->[0] eq 'PI') {
940: $result.=$token->[2];
941: } elsif ($token->[0] eq 'S') {
942: if ($token->[1] =~ /^$tag$/i) { $depth++; }
943: $result.=$token->[4];
944: } elsif ($token->[0] eq 'E') {
945: if ( $token->[1] =~ /^$tag$/i) { $depth--; }
946: #skip sending back the last end tag
947: if ($depth > -1) { $result.=$token->[2]; } else {
948: $$pars[-1]->unget_token($token);
949: }
950: }
951: }
952: if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
953: if (($depth >=0) && ($#$pars > 0) ) {
954: pop(@$pars);
955: pop(@Apache::lonxml::pwd);
956: }
957: }
958: if ($top_empty && $depth >= 0) {
959: #never found the end tag ran out of text, throw error send back blank
960: &error('Never found end tag for <'.$tag.'>');
961: if ($gotfullstack) {
962: my $newstring='</'.$tag.'>'.$result;
963: &Apache::lonxml::newparser($pars,\$newstring);
964: }
965: $result='';
966: }
967: } else {
968: while ($#$pars > -1) {
969: while ($token = $$pars[-1]->get_token) {
970: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
971: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
972: ($token->[0] eq 'D')) {
973: $result.=$token->[1];
974: } elsif ($token->[0] eq 'PI') {
975: $result.=$token->[2];
976: } elsif ($token->[0] eq 'S') {
977: if ( $token->[1] =~ /^$tag$/i) {
978: $$pars[-1]->unget_token($token); last;
979: } else {
980: $result.=$token->[4];
981: }
982: } elsif ($token->[0] eq 'E') {
983: $result.=$token->[2];
984: }
985: }
986: if (($#$pars > 0) ) {
987: pop(@$pars);
988: pop(@Apache::lonxml::pwd);
989: } else { last; }
990: }
991: }
992: if ($result =~ m|<LONCAPA_INTERNAL_TURN_STYLE_ON />|) {
993: $Apache::lonxml::usestyle=1;
994: }
995: #&Apache::lonxml::debug("Exit:$result:");
996: return $result
997: }
998:
999: sub newparser {
1000: my ($parser,$contentref,$dir) = @_;
1001: push (@$parser,HTML::LCParser->new($contentref));
1002: $$parser['-1']->xml_mode('1');
1003: if ( $dir eq '' ) {
1004: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1005: } else {
1006: push (@Apache::lonxml::pwd, $dir);
1007: }
1008: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
1009: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
1010: }
1011:
1012: sub parstring {
1013: my ($token) = @_;
1014: my $temp='';
1015: foreach (@{$token->[3]}) {
1016: unless ($_=~/\W/) {
1017: my $val=$token->[2]->{$_};
1018: $val =~ s/([\%\@\\\"\'])/\\$1/g;
1019: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1020: $temp .= "my \$$_=\"$val\";"
1021: }
1022: }
1023: return $temp;
1024: }
1025:
1026: sub writeallows {
1027: unless ($#extlinks>=0) { return; }
1028: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1029: if ($ENV{'httpref.'.$thisurl}) {
1030: $thisurl=$ENV{'httpref.'.$thisurl};
1031: }
1032: my $thisdir=$thisurl;
1033: $thisdir=~s/\/[^\/]+$//;
1034: my %httpref=();
1035: foreach (@extlinks) {
1036: $httpref{'httpref.'.
1037: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1038: }
1039: @extlinks=();
1040: &Apache::lonnet::appenv(%httpref);
1041: }
1042:
1043: #
1044: # Afterburner handles anchors, highlights and links
1045: #
1046: sub afterburn {
1047: my $result=shift;
1048: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1049: ['highlight','anchor','link']);
1050: if ($ENV{'form.highlight'}) {
1051: foreach (split(/\,/,$ENV{'form.highlight'})) {
1052: my $anchorname=$_;
1053: my $matchthis=$anchorname;
1054: $matchthis=~s/\_+/\\s\+/g;
1055: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1056: }
1057: }
1058: if ($ENV{'form.link'}) {
1059: foreach (split(/\,/,$ENV{'form.link'})) {
1060: my ($anchorname,$linkurl)=split(/\>/,$_);
1061: my $matchthis=$anchorname;
1062: $matchthis=~s/\_+/\\s\+/g;
1063: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1064: }
1065: }
1066: if ($ENV{'form.anchor'}) {
1067: my $anchorname=$ENV{'form.anchor'};
1068: my $matchthis=$anchorname;
1069: $matchthis=~s/\_+/\\s\+/g;
1070: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1071: $result.=(<<"ENDSCRIPT");
1072: <script type="text/javascript">
1073: document.location.hash='$anchorname';
1074: </script>
1075: ENDSCRIPT
1076: }
1077: return $result;
1078: }
1079:
1080: sub storefile {
1081: my ($file,$contents)=@_;
1082: if (my $fh=Apache::File->new('>'.$file)) {
1083: print $fh $contents;
1084: $fh->close();
1085: } else {
1086: &warning("Unable to save file $file");
1087: }
1088: }
1089:
1090: sub createnewhtml {
1091: my $filecontents=(<<SIMPLECONTENT);
1092: <html>
1093: <head>
1094: <title>
1095: Title of Document Goes Here
1096: </title>
1097: </head>
1098: <body bgcolor="#FFFFFF">
1099:
1100: Body of Document Goes Here
1101:
1102: </body>
1103: </html>
1104: SIMPLECONTENT
1105: return $filecontents;
1106: }
1107:
1108:
1109: sub inserteditinfo {
1110: my ($result,$filecontents)=@_;
1111: $filecontents = &HTML::Entities::encode($filecontents);
1112: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1113: my $xml_help = '<table><tr><td>'.
1114: &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
1115: undef,undef,600)
1116: .'</td><td>'.
1117: &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
1118: undef,undef,600)
1119: .'</td></tr></table>';
1120: my $buttons=(<<BUTTONS);
1121: <input type="submit" name="attemptclean"
1122: value="Save and then attempt to clean HTML" />
1123: <input type="submit" name="savethisfile" value="Save this" />
1124: <input type="submit" name="viewmode" value="View" />
1125: BUTTONS
1126: my $editfooter=(<<ENDFOOTER);
1127: <hr />
1128: <a name="editsection" />
1129: <form method="post">
1130: $xml_help
1131: <input type="hidden" name="editmode" value="Edit" />
1132: $buttons<br />
1133: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1134: <br />$buttons
1135: <br />
1136: </form>
1137: ENDFOOTER
1138: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1139: $result=~s/(\<\/body\>)/$editfooter/is;
1140: return $result;
1141: }
1142:
1143: sub get_target {
1144: my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1145: if ( $ENV{'request.state'} eq 'published') {
1146: if ( defined($ENV{'form.grade_target'})
1147: && ($viewgrades == 'F' )) {
1148: return ($ENV{'form.grade_target'});
1149: } elsif (defined($ENV{'form.grade_target'})) {
1150: if (($ENV{'form.grade_target'} eq 'web') ||
1151: ($ENV{'form.grade_target'} eq 'tex') ) {
1152: return $ENV{'form.grade_target'}
1153: } else {
1154: return 'web';
1155: }
1156: } else {
1157: return 'web';
1158: }
1159: } elsif ($ENV{'request.state'} eq 'construct') {
1160: if ( defined($ENV{'form.grade_target'})) {
1161: return ($ENV{'form.grade_target'});
1162: } else {
1163: return 'web';
1164: }
1165: } else {
1166: return 'web';
1167: }
1168: }
1169:
1170: sub handler {
1171: my $request=shift;
1172:
1173: my $target=&get_target();
1174:
1175: $Apache::lonxml::debug=0;
1176:
1177: if ($ENV{'browser.mathml'}) {
1178: $request->content_type('text/xml');
1179: } else {
1180: $request->content_type('text/html');
1181: }
1182: &Apache::loncommon::no_cache($request);
1183: $request->send_http_header;
1184:
1185: return OK if $request->header_only;
1186:
1187:
1188: my $file=&Apache::lonnet::filelocation("",$request->uri);
1189: #
1190: # Edit action? Save file.
1191: #
1192: unless ($ENV{'request.state'} eq 'published') {
1193: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1194: &storefile($file,$ENV{'form.filecont'});
1195: }
1196: }
1197: my %mystyle;
1198: my $result = '';
1199: my $filecontents=&Apache::lonnet::getfile($file);
1200: if ($filecontents eq -1) {
1201: $result=(<<ENDNOTFOUND);
1202: <html>
1203: <head>
1204: <title>File not found</title>
1205: </head>
1206: <body bgcolor="#FFFFFF">
1207: <b>File not found: $file</b>
1208: </body>
1209: </html>
1210: ENDNOTFOUND
1211: $filecontents='';
1212: if ($ENV{'request.state'} ne 'published') {
1213: $filecontents=&createnewhtml();
1214: $ENV{'form.editmode'}='Edit'; #force edit mode
1215: }
1216: } else {
1217: unless ($ENV{'request.state'} eq 'published') {
1218: if ($ENV{'form.attemptclean'}) {
1219: $filecontents=&htmlclean($filecontents,1);
1220: }
1221: }
1222: if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1223: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1224: '',%mystyle);
1225: }
1226: }
1227:
1228: #
1229: # Edit action? Insert editing commands
1230: #
1231: unless ($ENV{'request.state'} eq 'published') {
1232: if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1233: my $displayfile=$request->uri;
1234: $displayfile=~s/^\/[^\/]*//;
1235: $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
1236: '</h3></body></html>';
1237: $result=&inserteditinfo($result,$filecontents);
1238: }
1239: }
1240:
1241: writeallows($request->uri);
1242:
1243: $request->print($result);
1244:
1245: return OK;
1246: }
1247:
1248: sub debug {
1249: if ($Apache::lonxml::debug eq 1) {
1250: $|=1;
1251: print('<font size="-2"<pre>DEBUG:'.&HTML::Entities::encode($_[0])."</pre></font>\n");
1252: }
1253: }
1254:
1255: sub error {
1256: $errorcount++;
1257: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1258: # If printing in construction space, put the error inside <pre></pre>
1259: print "<b>ERROR:</b>".join("\n",@_)."\n";
1260: } else {
1261: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1262: #notify author
1263: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1264: #notify course
1265: if ( $ENV{'request.course.id'} ) {
1266: my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
1267: my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1268: foreach (keys %users) {
1269: my ($user,$domain) = split(/:/, $_);
1270: &Apache::lonmsg::user_normal_msg($user,$domain,
1271: "Error [$declutter]",join('<br />',@_));
1272: }
1273: }
1274:
1275: #FIXME probably shouldn't have me get everything forever.
1276: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1277: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1278: }
1279: }
1280:
1281: sub warning {
1282: $warningcount++;
1283: if ($ENV{'request.state'} eq 'construct') {
1284: print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1285: }
1286: }
1287:
1288: sub get_param {
1289: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1290: if ( ! $context ) { $context = -1; }
1291: my $args ='';
1292: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1293: if ( ! $args ) { return undef; }
1294: if ( $case_insensitive ) {
1295: if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
1296: return &Apache::run::run("{$args;".'return $'.$param.'}',
1297: $safeeval); #'
1298: } else {
1299: return undef;
1300: }
1301: } else {
1302: if ( $args =~ /my \$\Q$param\E=\"/ ) {
1303: return &Apache::run::run("{$args;".'return $'.$param.'}',
1304: $safeeval); #'
1305: } else {
1306: return undef;
1307: }
1308: }
1309: }
1310:
1311: sub get_param_var {
1312: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1313: if ( ! $context ) { $context = -1; }
1314: my $args ='';
1315: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1316: &Apache::lonxml::debug("Args are $args param is $param");
1317: if ($case_insensitive) {
1318: if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
1319: return undef;
1320: }
1321: } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
1322: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1323: &Apache::lonxml::debug("first run is $value");
1324: if ($value =~ /^[\$\@\%]\w+$/) {
1325: &Apache::lonxml::debug("doing second");
1326: my @result=&Apache::run::run("return $value",$safeeval,1);
1327: if (!defined($result[0])) {
1328: return $value
1329: } else {
1330: if (wantarray) { return @result; } else { return $result[0]; }
1331: }
1332: } else {
1333: return $value;
1334: }
1335: }
1336:
1337: sub register_insert {
1338: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1339: my $i;
1340: my $tagnum=0;
1341: my @order;
1342: for ($i=0;$i < $#data; $i++) {
1343: my $line = $data[$i];
1344: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1345: if ( $line =~ /TABLE/ ) { last; }
1346: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1347: if ($tag) {
1348: $insertlist{"$tagnum.tag"} = $tag;
1349: $insertlist{"$tagnum.description"} = $descrip;
1350: $insertlist{"$tagnum.color"} = $color;
1351: $insertlist{"$tagnum.function"} = $function;
1352: if (!defined($show)) { $show='yes'; }
1353: $insertlist{"$tagnum.show"}= $show;
1354: $insertlist{"$tag.num"}=$tagnum;
1355: $tagnum++;
1356: }
1357: }
1358: $i++; #skipping TABLE line
1359: $tagnum = 0;
1360: for (;$i < $#data;$i++) {
1361: my $line = $data[$i];
1362: my ($mnemonic,@which) = split(/ +/,$line);
1363: my $tag = $insertlist{"$tagnum.tag"};
1364: for (my $j=0;$j <=$#which;$j++) {
1365: if ( $which[$j] eq 'Y' ) {
1366: if ($insertlist{"$j.show"} ne 'no') {
1367: push(@{ $insertlist{"$tag.which"} },$j);
1368: }
1369: }
1370: }
1371: $tagnum++;
1372: }
1373: }
1374:
1375: sub description {
1376: my ($token)=@_;
1377: my $tagnum;
1378: my $tag=$token->[1];
1379: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1380: my $testtag=$namespace.'::'.$tag;
1381: $tagnum=$insertlist{"$testtag.num"};
1382: if (defined($tagnum)) { last; }
1383: }
1384: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1385: return $insertlist{$tagnum.'.description'};
1386: }
1387:
1388: # ----------------------------------------------------------------- whichuser
1389: # returns a list of $symb, $courseid, $domain, $name that is correct for
1390: # calls to lonnet functions for this setup.
1391: # - looks for form.grade_ parameters
1392: sub whichuser {
1393: my ($symb,$courseid,$domain,$name);
1394: if (defined($ENV{'form.grade_symb'})) {
1395: my $tmp_courseid=$ENV{'form.grade_courseid'};
1396: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1397: if ($allowed) {
1398: $symb=$ENV{'form.grade_symb'};
1399: $courseid=$ENV{'form.grade_courseid'};
1400: $domain=$ENV{'form.grade_domain'};
1401: $name=$ENV{'form.grade_username'};
1402: }
1403: } else {
1404: $symb=&Apache::lonnet::symbread();
1405: $courseid=$ENV{'request.course.id'};
1406: $domain=$ENV{'user.domain'};
1407: $name=$ENV{'user.name'};
1408: }
1409: return ($symb,$courseid,$domain,$name);
1410: }
1411:
1412: 1;
1413: __END__
1414:
1415:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>