1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # $Id: lonxml.pm,v 1.215 2002/11/13 23:21:07 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
39: # last modified 06/26/00 by Alexander Sakharuk
40: # 11/6 Gerd Kortemeyer
41: # 6/1/1 Gerd Kortemeyer
42: # 2/21,3/13 Guy
43: # 3/29,5/4 Gerd Kortemeyer
44: # 5/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 hether it is used
148: $Apache::lonxml::counter=0;
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;
627: $text =~ s/[\n\r]//g;
628: }
629: $result.=$text;
630: }
631: } elsif ($token->[0] eq 'PI') {
632: if ($metamode<1) {
633: $result=$token->[2];
634: }
635: } elsif ($token->[0] eq 'S') {
636: # add tag to stack
637: push (@$stack,$token->[1]);
638: # add parameters list to another stack
639: push (@$parstack,&parstring($token));
640: &increasedepth($token);
641: if ($Apache::lonxml::usestyle &&
642: exists($$style_for_target{$token->[1]})) {
643: $Apache::lonxml::usestyle=0;
644: my $string=$$style_for_target{$token->[1]}.
645: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
646: &Apache::lonxml::newparser($pars,\$string);
647: } else {
648: $result = &callsub("start_$token->[1]", $target, $token, $stack,
649: $parstack, $pars, $safeeval, $style_for_target);
650: }
651: } elsif ($token->[0] eq 'E') {
652: #clear out any tags that didn't end
653: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
654: my $lasttag=$$stack[-1];
655: if ($token->[1] =~ /^$lasttag$/i) {
656: &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
657: last;
658: } else {
659: &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> in file');
660: &end_tag($stack,$parstack,$token);
661: }
662: }
663:
664: if ($Apache::lonxml::usestyle &&
665: exists($$style_for_target{'/'."$token->[1]"})) {
666: $Apache::lonxml::usestyle=0;
667: my $string=$$style_for_target{'/'.$token->[1]}.
668: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
669: &Apache::lonxml::newparser($pars,\$string);
670: } else {
671: $result = &callsub("end_$token->[1]", $target, $token, $stack,
672: $parstack, $pars,$safeeval, $style_for_target);
673: }
674: } else {
675: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
676: }
677: #evaluate variable refs in result
678: if ($result ne "") {
679: if ( $#$parstack > -1 ) {
680: $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]);
681: } else {
682: $result= &Apache::run::evaluate($result,$safeeval,'');
683: }
684: }
685: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
686: if ($target eq 'tex') {
687: $result=&latex_special_symbols($result,$stack,$parstack);
688: }
689: }
690:
691: # Encode any high ASCII characters
692: if (!$Apache::lonxml::prevent_entity_encode) {
693: $result=&HTML::Entities::encode($result,"\200-\377");
694: }
695: if ($Apache::lonxml::redirection) {
696: $Apache::lonxml::outputstack['-1'] .= $result;
697: } else {
698: $finaloutput.=$result;
699: }
700: $result = '';
701:
702: if ($token->[0] eq 'E') {
703: &end_tag($stack,$parstack,$token);
704: }
705: }
706: if ($#$pars > -1) {
707: pop @$pars;
708: pop @Apache::lonxml::pwd;
709: }
710: }
711:
712: # if ($target eq 'meta') {
713: # $finaloutput.=&endredirection;
714: # }
715:
716:
717: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
718: $finaloutput=&afterburn($finaloutput);
719: }
720: return $finaloutput;
721: }
722:
723: sub callsub {
724: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
725: my $currentstring='';
726: my $nodefault;
727: {
728: my $sub1;
729: no strict 'refs';
730: my $tag=$token->[1];
731: my $space=$Apache::lonxml::alltags{$tag}[-1];
732: if (!$space) {
733: $tag=~tr/A-Z/a-z/;
734: $sub=~tr/A-Z/a-z/;
735: $space=$Apache::lonxml::alltags{$tag}[-1]
736: }
737:
738: my $deleted=0;
739: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
740: if (($token->[0] eq 'S') && ($target eq 'modified')) {
741: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
742: $parstack,$parser,$safeeval,
743: $style);
744: }
745: if (!$deleted) {
746: if ($space) {
747: &Apache::lonxml::debug("Calling sub $sub in $space $metamode");
748: $sub1="$space\:\:$sub";
749: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
750: $parstack,$parser,$safeeval,
751: $style);
752: } else {
753: &Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
754: if ($metamode <1) {
755: if (defined($token->[4]) && ($metamode < 1)) {
756: $currentstring = $token->[4];
757: } else {
758: $currentstring = $token->[2];
759: }
760: }
761: }
762: # &Apache::lonxml::debug("nodefalt:$nodefault:");
763: if ($currentstring eq '' && $nodefault eq '') {
764: if ($target eq 'edit') {
765: &Apache::lonxml::debug("doing default edit for $token->[1]");
766: if ($token->[0] eq 'S') {
767: $currentstring = &Apache::edit::tag_start($target,$token);
768: } elsif ($token->[0] eq 'E') {
769: $currentstring = &Apache::edit::tag_end($target,$token);
770: }
771: } elsif ($target eq 'modified') {
772: if ($token->[0] eq 'S') {
773: $currentstring = $token->[4];
774: $currentstring.=&Apache::edit::handle_insert();
775: } elsif ($token->[0] eq 'E') {
776: $currentstring = $token->[2];
777: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
778: } else {
779: $currentstring = $token->[2];
780: }
781: }
782: }
783: }
784: use strict 'refs';
785: }
786: return $currentstring;
787: }
788:
789: sub setup_globals {
790: my ($request,$target)=@_;
791: $Apache::lonxml::request=$request;
792: $Apache::lonxml::registered = 0;
793: $errorcount=0;
794: $warningcount=0;
795: $Apache::lonxml::default_homework_loaded=0;
796: $Apache::lonxml::usestyle=1;
797: &init_counter();
798: @Apache::lonxml::pwd=();
799: @Apache::lonxml::extlinks=();
800: if ($target eq 'meta') {
801: $Apache::lonxml::redirection = 0;
802: $Apache::lonxml::metamode = 1;
803: $Apache::lonxml::evaluate = 1;
804: $Apache::lonxml::import = 0;
805: } elsif ($target eq 'answer') {
806: $Apache::lonxml::redirection = 0;
807: $Apache::lonxml::metamode = 1;
808: $Apache::lonxml::evaluate = 1;
809: $Apache::lonxml::import = 1;
810: } elsif ($target eq 'grade') {
811: &startredirection;
812: $Apache::lonxml::metamode = 0;
813: $Apache::lonxml::evaluate = 1;
814: $Apache::lonxml::import = 1;
815: } elsif ($target eq 'modified') {
816: $Apache::lonxml::redirection = 0;
817: $Apache::lonxml::metamode = 0;
818: $Apache::lonxml::evaluate = 0;
819: $Apache::lonxml::import = 0;
820: } elsif ($target eq 'edit') {
821: $Apache::lonxml::redirection = 0;
822: $Apache::lonxml::metamode = 0;
823: $Apache::lonxml::evaluate = 0;
824: $Apache::lonxml::import = 0;
825: } elsif ($target eq 'analyze') {
826: $Apache::lonxml::redirection = 0;
827: $Apache::lonxml::metamode = 0;
828: $Apache::lonxml::evaluate = 1;
829: $Apache::lonxml::import = 1;
830: } else {
831: $Apache::lonxml::redirection = 0;
832: $Apache::lonxml::metamode = 0;
833: $Apache::lonxml::evaluate = 1;
834: $Apache::lonxml::import = 1;
835: }
836: }
837:
838: sub init_safespace {
839: my ($target,$safeeval,$safehole,$safeinit) = @_;
840: $safeeval->permit("entereval");
841: $safeeval->permit(":base_math");
842: $safeeval->permit("sort");
843: $safeeval->deny(":base_io");
844: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
845: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
846:
847: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
848: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
849: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
850: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
851: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
852: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
853: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
854: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
855: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
856: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
857: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
858: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
859: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
860: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
861: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
862: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
863: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
864: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
865: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
866:
867: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
868: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
869: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
870: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
871: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
872: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
873: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
874: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
875: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
876: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
877: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
878: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
879: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
880: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
881: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
882: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
883: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
884: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
885: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
886: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
887: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
888: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
889:
890: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
891: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
892: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
893: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
894: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
895: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
896:
897: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
898: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
899: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
900: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
901: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
902: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
903: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
904: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
905: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
906: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
907: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
908: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
909: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
910: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
911: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
912: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
913: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
914: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
915: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
916: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
917: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
918:
919: #need to inspect this class of ops
920: # $safeeval->deny(":base_orig");
921: $safeinit .= ';$external::target="'.$target.'";';
922: my $rndseed;
923: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
924: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
925: $safeinit .= ';$external::randomseed='.$rndseed.';';
926: &Apache::run::run($safeinit,$safeeval);
927: }
928:
929: sub default_homework_load {
930: my ($safeeval)=@_;
931: &Apache::lonxml::debug('Loading default_homework');
932: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
933: if ($default == -1) {
934: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
935: } else {
936: &Apache::run::run($default,$safeeval);
937: $Apache::lonxml::default_homework_loaded=1;
938: }
939: }
940:
941: sub startredirection {
942: $Apache::lonxml::redirection++;
943: push (@Apache::lonxml::outputstack, '');
944: }
945:
946: sub endredirection {
947: if (!$Apache::lonxml::redirection) {
948: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
949: return '';
950: }
951: $Apache::lonxml::redirection--;
952: pop @Apache::lonxml::outputstack;
953: }
954:
955: sub end_tag {
956: my ($tagstack,$parstack,$token)=@_;
957: pop(@$tagstack);
958: pop(@$parstack);
959: &decreasedepth($token);
960: }
961:
962: sub initdepth {
963: @Apache::lonxml::depthcounter=();
964: $Apache::lonxml::depth=-1;
965: $Apache::lonxml::olddepth=-1;
966: }
967:
968: sub increasedepth {
969: my ($token) = @_;
970: $Apache::lonxml::depth++;
971: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
972: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
973: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
974: }
975: my $curdepth=join('_',@Apache::lonxml::depthcounter);
976: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
977: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
978: }
979:
980: sub decreasedepth {
981: my ($token) = @_;
982: $Apache::lonxml::depth--;
983: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
984: $#Apache::lonxml::depthcounter--;
985: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
986: }
987: if ( $Apache::lonxml::depth < -1) {
988: &Apache::lonxml::warning("Missing tags, unable to properly run file.");
989: $Apache::lonxml::depth='-1';
990: }
991: my $curdepth=join('_',@Apache::lonxml::depthcounter);
992: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
993: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
994: }
995:
996: sub get_all_text_unbalanced {
997: #there is a copy of this in lonpublisher.pm
998: my($tag,$pars)= @_;
999: my $token;
1000: my $result='';
1001: $tag='<'.$tag.'>';
1002: while ($token = $$pars[-1]->get_token) {
1003: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1004: $result.=$token->[1];
1005: } elsif ($token->[0] eq 'PI') {
1006: $result.=$token->[2];
1007: } elsif ($token->[0] eq 'S') {
1008: $result.=$token->[4];
1009: } elsif ($token->[0] eq 'E') {
1010: $result.=$token->[2];
1011: }
1012: if ($result =~ /(.*)\Q$tag\E(.*)/s) {
1013: &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
1014: &Apache::lonxml::debug('Result is :'.$1);
1015: $result=$1;
1016: my $redo=$tag.$2;
1017: &Apache::lonxml::newparser($pars,\$redo);
1018: last;
1019: }
1020: }
1021: return $result
1022: }
1023:
1024: sub increment_counter {
1025: $Apache::lonxml::counter++;
1026: $Apache::lonxml::counter_changed=1;
1027: }
1028:
1029: sub init_counter {
1030: if (defined($ENV{'form.counter'})) {
1031: $Apache::lonxml::counter=$ENV{'form.counter'};
1032: } elsif (not defined($Apache::lonxml::counter)) {
1033: $Apache::lonxml::counter=1;
1034: &store_counter();
1035: }
1036: $Apache::lonxml::counter_changed=0;
1037: }
1038:
1039: sub store_counter {
1040: &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
1041: return '';
1042: }
1043:
1044: sub get_all_text {
1045: my($tag,$pars)= @_;
1046: &Apache::lonxml::debug("Got a ".ref($pars));
1047: if (ref($pars) ne 'ARRAY') {
1048: $pars=[$pars];
1049: }
1050: my $depth=0;
1051: my $token;
1052: my $result='';
1053: if ( $tag =~ m:^/: ) {
1054: my $tag=substr($tag,1);
1055: #&Apache::lonxml::debug("have:$tag:");
1056: while (($depth >=0) && ($#$pars > -1)) {
1057: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
1058: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
1059: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1060: $result.=$token->[1];
1061: } elsif ($token->[0] eq 'PI') {
1062: $result.=$token->[2];
1063: } elsif ($token->[0] eq 'S') {
1064: if ($token->[1] =~ /^$tag$/i) { $depth++; }
1065: $result.=$token->[4];
1066: } elsif ($token->[0] eq 'E') {
1067: if ( $token->[1] =~ /^$tag$/i) { $depth--; }
1068: #skip sending back the last end tag
1069: if ($depth > -1) { $result.=$token->[2]; } else {
1070: $$pars[-1]->unget_token($token);
1071: }
1072: }
1073: }
1074: if (($depth >=0) && ($#$pars > 0) ) {
1075: pop(@$pars);
1076: pop(@Apache::lonxml::pwd);
1077: }
1078: }
1079: } else {
1080: while ($#$pars > -1) {
1081: while ($token = $$pars[-1]->get_token) {
1082: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1083: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
1084: ($token->[0] eq 'D')) {
1085: $result.=$token->[1];
1086: } elsif ($token->[0] eq 'PI') {
1087: $result.=$token->[2];
1088: } elsif ($token->[0] eq 'S') {
1089: if ( $token->[1] =~ /^$tag$/i) {
1090: $$pars[-1]->unget_token($token); last;
1091: } else {
1092: $result.=$token->[4];
1093: }
1094: } elsif ($token->[0] eq 'E') {
1095: $result.=$token->[2];
1096: }
1097: }
1098: if (($#$pars > 0) ) {
1099: pop(@$pars);
1100: pop(@Apache::lonxml::pwd);
1101: } else { last; }
1102: }
1103: }
1104: if ($result =~ m|<LONCAPA_INTERNAL_TURN_STYLE_ON />|) {
1105: $Apache::lonxml::usestyle=1;
1106: }
1107: #&Apache::lonxml::debug("Exit:$result:");
1108: return $result
1109: }
1110:
1111: sub newparser {
1112: my ($parser,$contentref,$dir) = @_;
1113: push (@$parser,HTML::LCParser->new($contentref));
1114: $$parser['-1']->xml_mode('1');
1115: if ( $dir eq '' ) {
1116: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1117: } else {
1118: push (@Apache::lonxml::pwd, $dir);
1119: }
1120: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
1121: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
1122: }
1123:
1124: sub parstring {
1125: my ($token) = @_;
1126: my $temp='';
1127: foreach (@{$token->[3]}) {
1128: unless ($_=~/\W/) {
1129: my $val=$token->[2]->{$_};
1130: $val =~ s/([\%\@\\\"])/\\$1/g;
1131: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1132: $temp .= "my \$$_=\"$val\";"
1133: }
1134: }
1135: return $temp;
1136: }
1137:
1138: sub writeallows {
1139: unless ($#extlinks>=0) { return; }
1140: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1141: if ($ENV{'httpref.'.$thisurl}) {
1142: $thisurl=$ENV{'httpref.'.$thisurl};
1143: }
1144: my $thisdir=$thisurl;
1145: $thisdir=~s/\/[^\/]+$//;
1146: my %httpref=();
1147: foreach (@extlinks) {
1148: $httpref{'httpref.'.
1149: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1150: }
1151: @extlinks=();
1152: &Apache::lonnet::appenv(%httpref);
1153: }
1154:
1155: #
1156: # Afterburner handles anchors, highlights and links
1157: #
1158: sub afterburn {
1159: my $result=shift;
1160: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1161: ['highlight','anchor','link']);
1162: if ($ENV{'form.highlight'}) {
1163: foreach (split(/\,/,$ENV{'form.highlight'})) {
1164: my $anchorname=$_;
1165: my $matchthis=$anchorname;
1166: $matchthis=~s/\_+/\\s\+/g;
1167: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1168: }
1169: }
1170: if ($ENV{'form.link'}) {
1171: foreach (split(/\,/,$ENV{'form.link'})) {
1172: my ($anchorname,$linkurl)=split(/\>/,$_);
1173: my $matchthis=$anchorname;
1174: $matchthis=~s/\_+/\\s\+/g;
1175: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1176: }
1177: }
1178: if ($ENV{'form.anchor'}) {
1179: my $anchorname=$ENV{'form.anchor'};
1180: my $matchthis=$anchorname;
1181: $matchthis=~s/\_+/\\s\+/g;
1182: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1183: $result.=(<<"ENDSCRIPT");
1184: <script>
1185: document.location.hash='$anchorname';
1186: </script>
1187: ENDSCRIPT
1188: }
1189: return $result;
1190: }
1191:
1192: sub storefile {
1193: my ($file,$contents)=@_;
1194: if (my $fh=Apache::File->new('>'.$file)) {
1195: print $fh $contents;
1196: $fh->close();
1197: } else {
1198: &warning("Unable to save file $file");
1199: }
1200: }
1201:
1202: sub createnewhtml {
1203: my $filecontents=(<<SIMPLECONTENT);
1204: <html>
1205: <head>
1206: <title>
1207: Title of Document Goes Here
1208: </title>
1209: </head>
1210: <body bgcolor="#FFFFFF">
1211:
1212: Body of Document Goes Here
1213:
1214: </body>
1215: </html>
1216: SIMPLECONTENT
1217: return $filecontents;
1218: }
1219:
1220:
1221: sub inserteditinfo {
1222: my ($result,$filecontents)=@_;
1223: $filecontents = &HTML::Entities::encode($filecontents);
1224: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1225: my $buttons=(<<BUTTONS);
1226: <input type="submit" name="attemptclean"
1227: value="Save and then attempt to clean HTML" />
1228: <input type="submit" name="savethisfile" value="Save this" />
1229: <input type="submit" name="viewmode" value="View" />
1230: BUTTONS
1231: my $editfooter=(<<ENDFOOTER);
1232: <hr />
1233: <a name="editsection" />
1234: <form method="post">
1235: <input type="hidden" name="editmode" value="Edit" />
1236: $buttons<br />
1237: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1238: <br />$buttons
1239: <br />
1240: </form>
1241: ENDFOOTER
1242: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1243: $result=~s/(\<\/body\>)/$editfooter/is;
1244: return $result;
1245: }
1246:
1247: sub get_target {
1248: my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1249: if ( $ENV{'request.state'} eq 'published') {
1250: if ( defined($ENV{'form.grade_target'})
1251: && ($viewgrades == 'F' )) {
1252: return ($ENV{'form.grade_target'});
1253: } elsif (defined($ENV{'form.grade_target'})) {
1254: if (($ENV{'form.grade_target'} eq 'web') ||
1255: ($ENV{'form.grade_target'} eq 'tex') ) {
1256: return $ENV{'form.grade_target'}
1257: } else {
1258: return 'web';
1259: }
1260: } else {
1261: return 'web';
1262: }
1263: } elsif ($ENV{'request.state'} eq 'construct') {
1264: if ( defined($ENV{'form.grade_target'})) {
1265: return ($ENV{'form.grade_target'});
1266: } else {
1267: return 'web';
1268: }
1269: } else {
1270: return 'web';
1271: }
1272: }
1273:
1274: sub handler {
1275: my $request=shift;
1276:
1277: my $target=&get_target();
1278:
1279: $Apache::lonxml::debug=0;
1280:
1281: if ($ENV{'browser.mathml'}) {
1282: $request->content_type('text/xml');
1283: } else {
1284: $request->content_type('text/html');
1285: }
1286: &Apache::loncommon::no_cache($request);
1287: $request->send_http_header;
1288:
1289: return OK if $request->header_only;
1290:
1291:
1292: my $file=&Apache::lonnet::filelocation("",$request->uri);
1293: #
1294: # Edit action? Save file.
1295: #
1296: unless ($ENV{'request.state'} eq 'published') {
1297: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1298: &storefile($file,$ENV{'form.filecont'});
1299: }
1300: }
1301: my %mystyle;
1302: my $result = '';
1303: my $filecontents=&Apache::lonnet::getfile($file);
1304: if ($filecontents == -1) {
1305: $result=(<<ENDNOTFOUND);
1306: <html>
1307: <head>
1308: <title>File not found</title>
1309: </head>
1310: <body bgcolor="#FFFFFF">
1311: <b>File not found: $file</b>
1312: </body>
1313: </html>
1314: ENDNOTFOUND
1315: $filecontents='';
1316: if ($ENV{'request.state'} ne 'published') {
1317: $filecontents=&createnewhtml();
1318: $ENV{'form.editmode'}='Edit'; #force edit mode
1319: }
1320: } else {
1321: unless ($ENV{'request.state'} eq 'published') {
1322: if ($ENV{'form.attemptclean'}) {
1323: $filecontents=&htmlclean($filecontents,1);
1324: }
1325: }
1326: if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1327: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1328: '',%mystyle);
1329: }
1330: }
1331:
1332: #
1333: # Edit action? Insert editing commands
1334: #
1335: unless ($ENV{'request.state'} eq 'published') {
1336: if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1337: my $displayfile=$request->uri;
1338: $displayfile=~s/^\/[^\/]*//;
1339: $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
1340: '</h3></body></html>';
1341: $result=&inserteditinfo($result,$filecontents);
1342: }
1343: }
1344:
1345: writeallows($request->uri);
1346:
1347: $request->print($result);
1348:
1349: return OK;
1350: }
1351:
1352: sub debug {
1353: if ($Apache::lonxml::debug eq 1) {
1354: $|=1;
1355: print('<font size="-2"<pre>DEBUG:'.&HTML::Entities::encode($_[0])."</pre></font>\n");
1356: }
1357: }
1358:
1359: sub error {
1360: $errorcount++;
1361: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1362: # If printing in construction space, put the error inside <pre></pre>
1363: print "<b>ERROR:</b>".join("\n",@_)."\n";
1364: } else {
1365: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1366: #notify author
1367: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1368: #notify course
1369: if ( $ENV{'request.course.id'} ) {
1370: my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
1371: my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1372: foreach (keys %users) {
1373: my ($user,$domain) = split(/:/, $_);
1374: &Apache::lonmsg::user_normal_msg($user,$domain,
1375: "Error [$declutter]",join('<br />',@_));
1376: }
1377: }
1378:
1379: #FIXME probably shouldn't have me get everything forever.
1380: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1381: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1382: }
1383: }
1384:
1385: sub warning {
1386: $warningcount++;
1387: if ($ENV{'request.state'} eq 'construct') {
1388: print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1389: }
1390: }
1391:
1392: sub get_param {
1393: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1394: if ( ! $context ) { $context = -1; }
1395: my $args ='';
1396: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1397: if ( ! $args ) { return undef; }
1398: if ( $case_insensitive ) {
1399: if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
1400: return &Apache::run::run("{$args;".'return $'.$param.'}',
1401: $safeeval); #'
1402: } else {
1403: return undef;
1404: }
1405: } else {
1406: if ( $args =~ /my \$\Q$param\E=\"/ ) {
1407: return &Apache::run::run("{$args;".'return $'.$param.'}',
1408: $safeeval); #'
1409: } else {
1410: return undef;
1411: }
1412: }
1413: }
1414:
1415: sub get_param_var {
1416: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1417: if ( ! $context ) { $context = -1; }
1418: my $args ='';
1419: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1420: if ($case_insensitive) {
1421: if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
1422: return undef;
1423: }
1424: } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
1425: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1426: if ($value =~ /^[\$\@\%]/) {
1427: return &Apache::run::run("return $value",$safeeval,1);
1428: } else {
1429: return $value;
1430: }
1431: }
1432:
1433: sub register_insert {
1434: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1435: my $i;
1436: my $tagnum=0;
1437: my @order;
1438: for ($i=0;$i < $#data; $i++) {
1439: my $line = $data[$i];
1440: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1441: if ( $line =~ /TABLE/ ) { last; }
1442: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1443: if ($tag) {
1444: $insertlist{"$tagnum.tag"} = $tag;
1445: $insertlist{"$tagnum.description"} = $descrip;
1446: $insertlist{"$tagnum.color"} = $color;
1447: $insertlist{"$tagnum.function"} = $function;
1448: if (!defined($show)) { $show='yes'; }
1449: $insertlist{"$tagnum.show"}= $show;
1450: $insertlist{"$tag.num"}=$tagnum;
1451: $tagnum++;
1452: }
1453: }
1454: $i++; #skipping TABLE line
1455: $tagnum = 0;
1456: for (;$i < $#data;$i++) {
1457: my $line = $data[$i];
1458: my ($mnemonic,@which) = split(/ +/,$line);
1459: my $tag = $insertlist{"$tagnum.tag"};
1460: for (my $j=0;$j <=$#which;$j++) {
1461: if ( $which[$j] eq 'Y' ) {
1462: if ($insertlist{"$j.show"} ne 'no') {
1463: push(@{ $insertlist{"$tag.which"} },$j);
1464: }
1465: }
1466: }
1467: $tagnum++;
1468: }
1469: }
1470:
1471: sub description {
1472: my ($token)=@_;
1473: my $tagnum;
1474: my $tag=$token->[1];
1475: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1476: my $testtag=$namespace.'::'.$tag;
1477: $tagnum=$insertlist{"$testtag.num"};
1478: if (defined($tagnum)) { last; }
1479: }
1480: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1481: return $insertlist{$tagnum.'.description'};
1482: }
1483:
1484: # ----------------------------------------------------------------- whichuser
1485: # returns a list of $symb, $courseid, $domain, $name that is correct for
1486: # calls to lonnet functions for this setup.
1487: # - looks for form.grade_ parameters
1488: sub whichuser {
1489: my ($symb,$courseid,$domain,$name);
1490: if (defined($ENV{'form.grade_symb'})) {
1491: my $tmp_courseid=$ENV{'form.grade_courseid'};
1492: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1493: if ($allowed) {
1494: $symb=$ENV{'form.grade_symb'};
1495: $courseid=$ENV{'form.grade_courseid'};
1496: $domain=$ENV{'form.grade_domain'};
1497: $name=$ENV{'form.grade_username'};
1498: }
1499: } else {
1500: $symb=&Apache::lonnet::symbread();
1501: $courseid=$ENV{'request.course.id'};
1502: $domain=$ENV{'user.domain'};
1503: $name=$ENV{'user.name'};
1504: }
1505: return ($symb,$courseid,$domain,$name);
1506: }
1507:
1508: 1;
1509: __END__
1510:
1511:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>