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