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