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