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