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