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