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