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