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