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