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