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