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