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