Annotation of loncom/xml/lonxml.pm, revision 1.408
1.2 sakharuk 1: # The LearningOnline Network with CAPA
1.3 sakharuk 2: # XML Parser Module
1.2 sakharuk 3: #
1.408 ! albertel 4: # $Id: lonxml.pm,v 1.407 2006/04/18 20:43:47 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.316 albertel 39:
1.2 sakharuk 40:
1.4 albertel 41: package Apache::lonxml;
1.33 www 42: use vars
1.320 www 43: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount @htmlareafields);
1.1 sakharuk 44: use strict;
1.167 albertel 45: use HTML::LCParser();
1.161 albertel 46: use HTML::TreeBuilder();
47: use HTML::Entities();
48: use Safe();
49: use Safe::Hole();
50: use Math::Cephes();
51: use Math::Random();
52: use Opcode();
1.271 www 53: use POSIX qw(strftime);
1.339 albertel 54: use Time::HiRes qw( gettimeofday tv_interval );
1.393 albertel 55: use Symbol();
1.266 bowersj2 56:
1.72 albertel 57: sub register {
1.141 albertel 58: my ($space,@taglist) = @_;
59: foreach my $temptag (@taglist) {
60: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72 albertel 61: }
62: }
63:
1.141 albertel 64: sub deregister {
65: my ($space,@taglist) = @_;
66: foreach my $temptag (@taglist) {
67: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
68: if ($tempspace eq $space) {
69: pop(@{ $Apache::lonxml::alltags{$temptag} });
70: }
71: }
1.142 albertel 72: #&printalltags();
1.141 albertel 73: }
74:
1.46 www 75: use Apache::Constants qw(:common);
1.161 albertel 76: use Apache::lontexconvert();
77: use Apache::style();
78: use Apache::run();
79: use Apache::londefdef();
80: use Apache::scripttag();
1.285 www 81: use Apache::languagetags();
1.161 albertel 82: use Apache::edit();
1.266 bowersj2 83: use Apache::inputtags();
84: use Apache::outputtags();
1.372 albertel 85: use Apache::lonnet;
1.161 albertel 86: use Apache::File();
87: use Apache::loncommon();
1.198 www 88: use Apache::lonfeedback();
1.200 www 89: use Apache::lonmsg();
1.217 matthew 90: use Apache::loncacc();
1.280 www 91: use Apache::lonlocal;
1.79 www 92:
1.72 albertel 93: #================================================== Main subroutine: xmlparse
94: #debugging control, to turn on debugging modify the correct handler
95: $Apache::lonxml::debug=0;
1.206 albertel 96:
97: # keeps count of the number of warnings and errors generated in a parse
98: $warningcount=0;
99: $errorcount=0;
1.72 albertel 100:
101: #path to the directory containing the file currently being processed
102: @pwd=();
103:
104: #these two are used for capturing a subset of the output for later processing,
105: #don't touch them directly use &startredirection and &endredirection
106: @outputstack = ();
107: $redirection = 0;
108:
109: #controls wheter the <import> tag actually does
110: $import = 1;
111: @extlinks=();
112:
113: # meta mode is a bit weird only some output is to be turned off
114: #<output> tag turns metamode off (defined in londefdef.pm)
115: $metamode = 0;
116:
117: # turns on and of run::evaluate actually derefencing var refs
118: $evaluate = 1;
1.7 albertel 119:
1.74 albertel 120: # data structure for eidt mode, determines what tags can go into what other tags
121: %insertlist=();
1.68 www 122:
1.99 albertel 123: # stores the list of active tag namespaces
1.76 albertel 124: @namespace=();
125:
1.99 albertel 126: # has the dynamic menu been updated to know about this resource
127: $Apache::lonxml::registered=0;
128:
1.172 albertel 129: # a pointer the the Apache request object
130: $Apache::lonxml::request='';
131:
1.216 sakharuk 132: # a problem number counter, and check on ether it is used
1.237 sakharuk 133: $Apache::lonxml::counter=1;
1.204 albertel 134: $Apache::lonxml::counter_changed=0;
135:
1.212 albertel 136: #internal check on whether to look at style defs
137: $Apache::lonxml::usestyle=1;
1.260 albertel 138:
139: #locations used to store the parameter string for style substitutions
140: $Apache::lonxml::style_values='';
141: $Apache::lonxml::style_end_values='';
1.212 albertel 142:
1.281 albertel 143: #array of ssi calls that need to occur after we are done parsing
144: @Apache::lonxml::ssi_info=();
145:
1.282 albertel 146: #should we do the postag variable interpolation
147: $Apache::lonxml::post_evaluate=1;
148:
1.295 albertel 149: #a header message to emit in the case of any generated warning or errors
150: $Apache::lonxml::warnings_error_header='';
151:
1.396 foxr 152: # Control whether or not LaTeX symbols should be substituted for their
153: # \ style equivalents...this may be turned off e.g. in an verbatim
154: # environment.
155:
156: $Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on.
157:
158: sub enable_LaTeX_substitutions {
159: $Apache::lonxml::substitute_LaTeX_symbols = 1;
160: }
161: sub disable_LaTeX_substitutions {
162: $Apache::lonxml::substitute_LaTeX_symbols = 0;
163: }
164:
1.68 www 165: sub xmlbegin {
1.356 albertel 166: my ($style)=@_;
167: my $output='';
168: @htmlareafields=();
1.372 albertel 169: if ($env{'browser.mathml'}) {
1.356 albertel 170: $output='<?xml version="1.0"?>'
1.357 albertel 171: #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
172: # .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
173:
174: # .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">] >'
175: .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">'
1.68 www 176: .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" '
1.357 albertel 177: .'xmlns="http://www.w3.org/1999/xhtml">';
1.356 albertel 178: } else {
179: $output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
180: }
181: if ($style eq 'encode') {
182: $output=&HTML::Entities::encode($output,'<>&"');
183: }
184: return $output;
1.68 www 185: }
186:
187: sub xmlend {
1.335 sakharuk 188: my ($target,$parser)=@_;
1.278 www 189: my $mode='xml';
190: my $status='OPEN';
1.368 albertel 191: if ($Apache::lonhomework::parsing_a_problem ||
192: $Apache::lonhomework::parsing_a_task ) {
1.278 www 193: $mode='problem';
194: $status=$Apache::inputtags::status[-1];
195: }
1.362 matthew 196: my $discussion;
1.379 albertel 197: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
198: ['LONCAPA_INTERNAL_no_discussion']);
1.372 albertel 199: if (! exists($env{'form.LONCAPA_INTERNAL_no_discussion'}) ||
200: $env{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
1.362 matthew 201: $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
202: }
1.334 sakharuk 203: if ($target eq 'tex') {
1.335 sakharuk 204: $discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
1.334 sakharuk 205: &Apache::lonxml::newparser($parser,\$discussion,'');
206: return '';
207: }
1.405 albertel 208:
1.407 albertel 209: return $discussion;
1.119 www 210: }
211:
212: sub tokeninputfield {
1.120 www 213: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
214: $defhost=~tr/a-z/A-Z/;
1.119 www 215: return (<<ENDINPUTFIELD)
1.226 albertel 216: <script type="text/javascript">
1.120 www 217: function updatetoken() {
218: var comp=new Array;
219: var barcode=unescape(document.tokeninput.barcode.value);
220: comp=barcode.split('*');
221: if (typeof(comp[0])!="undefined") {
222: document.tokeninput.codeone.value=comp[0];
223: }
224: if (typeof(comp[1])!="undefined") {
225: document.tokeninput.codetwo.value=comp[1];
226: }
227: if (typeof(comp[2])!="undefined") {
228: comp[2]=comp[2].toUpperCase();
229: document.tokeninput.codethree.value=comp[2];
230: }
231: document.tokeninput.barcode.value='';
232: }
233: </script>
234: <form method="post" name="tokeninput">
1.119 www 235: <table border="2" bgcolor="#FFFFBB">
236: <tr><th>DocID Checkin</th></tr>
237: <tr><td>
238: <table>
239: <tr>
240: <td>Scan in Barcode</td>
1.120 www 241: <td><input type="text" size="22" name="barcode"
242: onChange="updatetoken()"/></td>
1.119 www 243: </tr>
244: <tr><td><i>or</i> Type in DocID</td>
245: <td>
246: <input type="text" size="5" name="codeone" />
1.120 www 247: <b><font size="+2">*</font></b>
1.119 www 248: <input type="text" size="5" name="codetwo" />
1.120 www 249: <b><font size="+2">*</font></b>
250: <input type="text" size="10" name="codethree" value="$defhost"
251: onChange="this.value=this.value.toUpperCase()" />
1.119 www 252: </td></tr>
253: </table>
254: </td></tr>
255: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
256: </table>
257: </form>
258: ENDINPUTFIELD
1.112 www 259: }
260:
1.116 www 261: sub maketoken {
1.118 www 262: my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112 www 263: unless ($symb) {
264: $symb=&Apache::lonnet::symbread();
265: }
266: unless ($tuname) {
1.372 albertel 267: $tuname=$env{'user.name'};
268: $tudom=$env{'user.domain'};
269: $tcrsid=$env{'request.course.id'};
1.112 www 270: }
1.116 www 271:
1.118 www 272: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
273: }
274:
275: sub printtokenheader {
1.133 albertel 276: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116 www 277: unless ($token) { return ''; }
1.118 www 278:
1.133 albertel 279: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
280: unless ($tsymb) {
281: $tsymb=$symb;
1.118 www 282: }
283: unless ($tuname) {
1.133 albertel 284: $tuname=$name;
285: $tudom=$domain;
286: $tcrsid=$courseid;
1.118 www 287: }
1.114 www 288:
1.390 albertel 289: my $plainname=&Apache::loncommon::plainname($tuname,$tudom);
1.114 www 290:
1.112 www 291: if ($target eq 'web') {
1.145 www 292: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115 www 293: return
1.221 albertel 294: '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
1.284 www 295: &mt('Checked out for').' '.$plainname.
296: '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
297: '<br />'.&mt('ID').': '.$idhash{$tuname}.
298: '<br />'.&mt('CourseID').': '.$tcrsid.
1.372 albertel 299: '<br />'.&mt('Course').': '.$env{'course.'.$tcrsid.'.description'}.
1.284 www 300: '<br />'.&mt('DocID').': '.$token.
301: '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
1.112 www 302: } else {
1.121 albertel 303: return $token;
1.112 www 304: }
1.68 www 305: }
306:
1.48 albertel 307: sub printalltags {
308: my $temp;
309: foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141 albertel 310: &Apache::lonxml::debug("$temp -- ".
311: join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48 albertel 312: }
313: }
1.31 sakharuk 314:
1.3 sakharuk 315: sub xmlparse {
1.172 albertel 316: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96 albertel 317:
1.172 albertel 318: &setup_globals($request,$target);
1.232 albertel 319: &Apache::inputtags::initialize_inputtags();
1.370 albertel 320: &Apache::bridgetask::initialize_bridgetask();
1.232 albertel 321: &Apache::outputtags::initialize_outputtags();
322: &Apache::edit::initialize_edit();
1.287 albertel 323: &Apache::londefdef::initialize_londefdef();
1.244 albertel 324:
1.178 www 325: #
326: # do we have a course style file?
327: #
328:
1.372 albertel 329: if ($env{'request.course.id'} && $env{'request.state'} ne 'construct') {
1.178 www 330: my $bodytext=
1.372 albertel 331: $env{'course.'.$env{'request.course.id'}.'.default_xml_style'};
1.178 www 332: if ($bodytext) {
1.337 albertel 333: foreach my $file (split(',',$bodytext)) {
334: my $location=&Apache::lonnet::filelocation('',$file);
335: my $styletext=&Apache::lonnet::getfile($location);
336: if ($styletext ne '-1') {
337: %style_for_target = (%style_for_target,
338: &Apache::style::styleparser($target,$styletext));
339: }
340: }
341: }
1.372 albertel 342: } elsif ($env{'construct.style'} && ($env{'request.state'} eq 'construct')) {
343: my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
1.291 sakharuk 344: my $styletext=&Apache::lonnet::getfile($location);
345: if ($styletext ne '-1') {
346: %style_for_target = (%style_for_target,
347: &Apache::style::styleparser($target,$styletext));
348: }
1.178 www 349: }
1.255 sakharuk 350: #&printalltags();
1.16 albertel 351: my @pars = ();
1.372 albertel 352: my $pwd=$env{'request.filename'};
1.23 albertel 353: $pwd =~ s:/[^/]*$::;
354: &newparser(\@pars,\$content_file_string,$pwd);
1.24 sakharuk 355:
1.3 sakharuk 356: my $safeeval = new Safe;
1.40 albertel 357: my $safehole = new Safe::Hole;
1.82 ng 358: &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3 sakharuk 359: #-------------------- Redefinition of the target in the case of compound target
360:
361: ($target, my @tenta) = split('&&',$target);
362:
1.150 albertel 363: my @stack = ();
1.3 sakharuk 364: my @parstack = ();
1.358 albertel 365: &initdepth();
366: &init_alarm();
1.101 albertel 367: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
1.394 albertel 368: $safeeval,\%style_for_target,1);
1.255 sakharuk 369:
1.372 albertel 370: if ($env{'request.uri'}) {
371: &writeallows($env{'request.uri'});
1.125 www 372: }
1.281 albertel 373: &do_registered_ssi();
1.204 albertel 374: if ($Apache::lonxml::counter_changed) { &store_counter() }
1.393 albertel 375:
376: &clean_safespace($safeeval);
377:
1.372 albertel 378: if ($env{'form.return_only_error_and_warning_counts'}) {
1.361 www 379: return "$errorcount:$warningcount";
380: }
1.3 sakharuk 381: return $finaloutput;
1.106 www 382: }
383:
1.191 albertel 384: sub latex_special_symbols {
1.272 albertel 385: my ($string,$where)=@_;
1.396 foxr 386: #
387: # If e.g. in verbatim mode, then don't substitute.
388: # but return original string.
389: #
390: if (!($Apache::lonxml::substitute_LaTeX_symbols)) {
391: return $string;
392: }
1.235 sakharuk 393: if ($where eq 'header') {
1.272 albertel 394: $string =~ s/(\\|_|\^)/ /g;
1.311 albertel 395: $string =~ s/(\$|%|\{|\})/\\$1/g;
1.273 sakharuk 396: $string =~ s/_/ /g;
1.311 albertel 397: $string=&Apache::lonprintout::character_chart($string);
398: # any & or # leftover should be safe to just escape
399: $string=~s/([^\\])\&/$1\\\&/g;
400: $string=~s/([^\\])\#/$1\\\#/g;
1.229 sakharuk 401: } else {
1.312 albertel 402: $string=~s/\\/\\ensuremath{\\backslash}/g;
1.367 albertel 403: $string=~s/\\\%|\%/\\\%/g;
404: $string=~s/\\{|{/\\{/g;
405: $string=~s/\\}|}/\\}/g;
1.378 albertel 406: $string=~s/\\ensuremath\\{\\backslash\\}/\\ensuremath{\\backslash}/g;
1.367 albertel 407: $string=~s/\\\$|\$/\\\$/g;
408: $string=~s/\\\_|\_/\\\_/g;
1.313 albertel 409: $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
1.310 sakharuk 410: $string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
1.311 albertel 411: $string=&Apache::lonprintout::character_chart($string);
412: # any & or # leftover should be safe to just escape
1.367 albertel 413: $string=~s/\\\&|\&/\\\&/g;
414: $string=~s/\\\#|\#/\\\#/g;
1.332 sakharuk 415: $string=~s/\|/\$\\mid\$/g;
1.310 sakharuk 416: #single { or } How to escape?
1.229 sakharuk 417: }
1.272 albertel 418: return $string;
1.188 sakharuk 419: }
420:
1.101 albertel 421: sub inner_xmlparse {
1.394 albertel 422: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_;
1.101 albertel 423: my $finaloutput = '';
424: my $result;
425: my $token;
1.258 albertel 426: my $dontpop=0;
1.389 albertel 427: my $startredirection = $Apache::lonxml::redirection;
1.101 albertel 428: while ( $#$pars > -1 ) {
429: while ($token = $$pars['-1']->get_token) {
1.261 albertel 430: if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
1.101 albertel 431: if ($metamode<1) {
1.190 albertel 432: my $text=$token->[1];
1.193 albertel 433: if ($token->[0] eq 'C' && $target eq 'tex') {
1.239 sakharuk 434: $text = '';
435: # $text = '%'.$text."\n";
1.182 sakharuk 436: }
1.190 albertel 437: $result.=$text;
1.101 albertel 438: }
1.261 albertel 439: } elsif (($token->[0] eq 'D')) {
440: if ($metamode<1 && $target eq 'web') {
441: my $text=$token->[1];
442: $result.=$text;
443: }
1.101 albertel 444: } elsif ($token->[0] eq 'PI') {
1.261 albertel 445: if ($metamode<1 && $target eq 'web') {
1.101 albertel 446: $result=$token->[2];
447: }
448: } elsif ($token->[0] eq 'S') {
1.140 albertel 449: # add tag to stack
1.101 albertel 450: push (@$stack,$token->[1]);
451: # add parameters list to another stack
452: push (@$parstack,&parstring($token));
1.140 albertel 453: &increasedepth($token);
1.212 albertel 454: if ($Apache::lonxml::usestyle &&
455: exists($$style_for_target{$token->[1]})) {
456: $Apache::lonxml::usestyle=0;
457: my $string=$$style_for_target{$token->[1]}.
458: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
459: &Apache::lonxml::newparser($pars,\$string);
1.257 albertel 460: $Apache::lonxml::style_values=$$parstack[-1];
1.259 albertel 461: $Apache::lonxml::style_end_values=$$parstack[-1];
1.101 albertel 462: } else {
463: $result = &callsub("start_$token->[1]", $target, $token, $stack,
464: $parstack, $pars, $safeeval, $style_for_target);
1.140 albertel 465: }
1.101 albertel 466: } elsif ($token->[0] eq 'E') {
1.212 albertel 467: if ($Apache::lonxml::usestyle &&
468: exists($$style_for_target{'/'."$token->[1]"})) {
469: $Apache::lonxml::usestyle=0;
470: my $string=$$style_for_target{'/'.$token->[1]}.
1.258 albertel 471: '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
1.212 albertel 472: &Apache::lonxml::newparser($pars,\$string);
1.259 albertel 473: $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
474: $Apache::lonxml::style_end_values='';
1.258 albertel 475: $dontpop=1;
1.101 albertel 476: } else {
1.258 albertel 477: #clear out any tags that didn't end
478: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
479: my $lasttag=$$stack[-1];
1.317 albertel 480: if ($token->[1] =~ /^\Q$lasttag\E$/i) {
1.258 albertel 481: &Apache::lonxml::warning('Using tag </'.$token->[1].'> on line '.$token->[3].' as end tag to <'.$$stack[-1].'>');
482: last;
483: } else {
484: &Apache::lonxml::warning('Found tag </'.$token->[1].'> on line '.$token->[3].' when looking for </'.$$stack[-1].'> in file');
485: &end_tag($stack,$parstack,$token);
486: }
487: }
488: $result = &callsub("end_$token->[1]", $target, $token, $stack,
489: $parstack, $pars,$safeeval, $style_for_target);
1.101 albertel 490: }
491: } else {
492: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
493: }
494: #evaluate variable refs in result
1.282 albertel 495: if ($Apache::lonxml::post_evaluate &&$result ne "") {
1.257 albertel 496: my $extras;
497: if (!$Apache::lonxml::usestyle) {
498: $extras=$Apache::lonxml::style_values;
499: }
1.101 albertel 500: if ( $#$parstack > -1 ) {
1.257 albertel 501: $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
1.101 albertel 502: } else {
1.257 albertel 503: $result= &Apache::run::evaluate($result,$safeeval,$extras);
1.101 albertel 504: }
1.163 albertel 505: }
1.282 albertel 506: $Apache::lonxml::post_evaluate=1;
507:
1.190 albertel 508: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.249 albertel 509: #Style file definitions should be correct
1.250 albertel 510: if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
1.311 albertel 511: $result=&latex_special_symbols($result);
1.249 albertel 512: }
1.190 albertel 513: }
514:
1.169 albertel 515: if ($Apache::lonxml::redirection) {
516: $Apache::lonxml::outputstack['-1'] .= $result;
517: } else {
518: $finaloutput.=$result;
519: }
520: $result = '';
521:
1.258 albertel 522: if ($token->[0] eq 'E' && !$dontpop) {
1.101 albertel 523: &end_tag($stack,$parstack,$token);
524: }
1.258 albertel 525: $dontpop=0;
1.224 albertel 526: }
1.212 albertel 527: if ($#$pars > -1) {
528: pop @$pars;
529: pop @Apache::lonxml::pwd;
530: }
1.101 albertel 531: }
532:
533: # if ($target eq 'meta') {
534: # $finaloutput.=&endredirection;
535: # }
536:
1.394 albertel 537: if ( $start && $target eq 'grade') { &endredirection(); }
1.389 albertel 538: if ( $Apache::lonxml::redirection > $startredirection) {
539: while ($Apache::lonxml::redirection > $startredirection) {
540: $finaloutput .= &endredirection();
1.387 albertel 541: }
542: }
1.101 albertel 543: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
544: $finaloutput=&afterburn($finaloutput);
1.216 sakharuk 545: }
1.101 albertel 546: return $finaloutput;
547: }
1.67 www 548:
1.318 matthew 549: ##
550: ## Looks to see if there is a subroutine defined for this tag. If so, call it,
551: ## otherwise do not call it as we do not know what it is.
552: ##
1.7 albertel 553: sub callsub {
1.84 albertel 554: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 555: my $currentstring='';
1.72 albertel 556: my $nodefault;
1.7 albertel 557: {
1.59 albertel 558: my $sub1;
1.7 albertel 559: no strict 'refs';
1.68 www 560: my $tag=$token->[1];
1.236 www 561: # get utterly rid of extended html tags
562: if ($tag=~/^x\-/i) { return ''; }
1.141 albertel 563: my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68 www 564: if (!$space) {
1.141 albertel 565: $tag=~tr/A-Z/a-z/;
1.68 www 566: $sub=~tr/A-Z/a-z/;
1.141 albertel 567: $space=$Apache::lonxml::alltags{$tag}[-1]
1.68 www 568: }
1.97 albertel 569:
570: my $deleted=0;
571: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
572: if (($token->[0] eq 'S') && ($target eq 'modified')) {
573: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
574: $parstack,$parser,$safeeval,
575: $style);
576: }
577: if (!$deleted) {
578: if ($space) {
1.220 albertel 579: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97 albertel 580: $sub1="$space\:\:$sub";
581: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
582: $parstack,$parser,$safeeval,
583: $style);
584: } else {
1.318 matthew 585: if ($target eq 'tex') {
586: # throw away tag name
587: return '';
588: }
1.220 albertel 589: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97 albertel 590: if ($metamode <1) {
591: if (defined($token->[4]) && ($metamode < 1)) {
592: $currentstring = $token->[4];
593: } else {
594: $currentstring = $token->[2];
595: }
1.62 sakharuk 596: }
1.7 albertel 597: }
1.97 albertel 598: # &Apache::lonxml::debug("nodefalt:$nodefault:");
599: if ($currentstring eq '' && $nodefault eq '') {
600: if ($target eq 'edit') {
1.220 albertel 601: #&Apache::lonxml::debug("doing default edit for $token->[1]");
1.97 albertel 602: if ($token->[0] eq 'S') {
603: $currentstring = &Apache::edit::tag_start($target,$token);
604: } elsif ($token->[0] eq 'E') {
605: $currentstring = &Apache::edit::tag_end($target,$token);
606: }
607: } elsif ($target eq 'modified') {
608: if ($token->[0] eq 'S') {
609: $currentstring = $token->[4];
610: $currentstring.=&Apache::edit::handle_insert();
1.210 www 611: } elsif ($token->[0] eq 'E') {
612: $currentstring = $token->[2];
613: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
1.97 albertel 614: } else {
615: $currentstring = $token->[2];
616: }
1.72 albertel 617: }
618: }
1.7 albertel 619: }
620: use strict 'refs';
621: }
622: return $currentstring;
1.82 ng 623: }
624:
1.96 albertel 625: sub setup_globals {
1.172 albertel 626: my ($request,$target)=@_;
627: $Apache::lonxml::request=$request;
1.99 albertel 628: $Apache::lonxml::registered = 0;
1.325 www 629: @Apache::lonxml::htmlareafields=();
1.205 www 630: $errorcount=0;
631: $warningcount=0;
1.207 albertel 632: $Apache::lonxml::default_homework_loaded=0;
1.212 albertel 633: $Apache::lonxml::usestyle=1;
1.204 albertel 634: &init_counter();
1.101 albertel 635: @Apache::lonxml::pwd=();
1.124 albertel 636: @Apache::lonxml::extlinks=();
1.281 albertel 637: @Apache::lonxml::ssi_info=();
1.282 albertel 638: $Apache::lonxml::post_evaluate=1;
1.295 albertel 639: $Apache::lonxml::warnings_error_header='';
1.397 albertel 640: $Apache::lonxml::substitute_LaTeX_symbols = 1;
1.96 albertel 641: if ($target eq 'meta') {
642: $Apache::lonxml::redirection = 0;
643: $Apache::lonxml::metamode = 1;
644: $Apache::lonxml::evaluate = 1;
645: $Apache::lonxml::import = 0;
1.129 albertel 646: } elsif ($target eq 'answer') {
647: $Apache::lonxml::redirection = 0;
648: $Apache::lonxml::metamode = 1;
649: $Apache::lonxml::evaluate = 1;
650: $Apache::lonxml::import = 1;
1.96 albertel 651: } elsif ($target eq 'grade') {
1.387 albertel 652: &startredirection(); #ended in inner_xmlparse on exit
1.96 albertel 653: $Apache::lonxml::metamode = 0;
654: $Apache::lonxml::evaluate = 1;
655: $Apache::lonxml::import = 1;
656: } elsif ($target eq 'modified') {
657: $Apache::lonxml::redirection = 0;
658: $Apache::lonxml::metamode = 0;
659: $Apache::lonxml::evaluate = 0;
660: $Apache::lonxml::import = 0;
661: } elsif ($target eq 'edit') {
662: $Apache::lonxml::redirection = 0;
663: $Apache::lonxml::metamode = 0;
664: $Apache::lonxml::evaluate = 0;
665: $Apache::lonxml::import = 0;
1.163 albertel 666: } elsif ($target eq 'analyze') {
667: $Apache::lonxml::redirection = 0;
668: $Apache::lonxml::metamode = 0;
669: $Apache::lonxml::evaluate = 1;
670: $Apache::lonxml::import = 1;
1.96 albertel 671: } else {
672: $Apache::lonxml::redirection = 0;
673: $Apache::lonxml::metamode = 0;
674: $Apache::lonxml::evaluate = 1;
675: $Apache::lonxml::import = 1;
676: }
677: }
678:
1.82 ng 679: sub init_safespace {
680: my ($target,$safeeval,$safehole,$safeinit) = @_;
1.393 albertel 681: $safeeval->deny_only(':dangerous');
682: $safeeval->reval('use Math::Complex;');
1.383 albertel 683: $safeeval->permit_only(":default");
1.82 ng 684: $safeeval->permit("entereval");
685: $safeeval->permit(":base_math");
686: $safeeval->permit("sort");
1.286 albertel 687: $safeeval->permit("time");
1.371 albertel 688: $safeeval->deny("rand");
689: $safeeval->deny("srand");
1.82 ng 690: $safeeval->deny(":base_io");
1.102 albertel 691: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.251 albertel 692: $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
1.82 ng 693: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.324 albertel 694: $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
695: '&chem_standard_order');
1.369 albertel 696: $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
1.324 albertel 697:
1.82 ng 698: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
699: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
700: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
701: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
702: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
703: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
704: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
705: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
706: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
707: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
708: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
709: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
710: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
711: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
712: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
713: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
714: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
715: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
716: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.215 albertel 717:
718: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
719: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
720: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
721: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
722: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
723: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
724: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
725: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
726: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
727: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
728: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
729: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
730: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
731: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
732: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
733: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
734: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
735: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
736: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
737: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
738: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
739: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
740:
1.383 albertel 741: $safehole->wrap(\&Math::Cephes::Matrix::mat,$safeeval,'&mat');
742: $safehole->wrap(\&Math::Cephes::Matrix::new,$safeeval,
743: '&Math::Cephes::Matrix::new');
744: $safehole->wrap(\&Math::Cephes::Matrix::coef,$safeeval,
745: '&Math::Cephes::Matrix::coef');
746: $safehole->wrap(\&Math::Cephes::Matrix::clr,$safeeval,
747: '&Math::Cephes::Matrix::clr');
748: $safehole->wrap(\&Math::Cephes::Matrix::add,$safeeval,
749: '&Math::Cephes::Matrix::add');
750: $safehole->wrap(\&Math::Cephes::Matrix::sub,$safeeval,
751: '&Math::Cephes::Matrix::sub');
752: $safehole->wrap(\&Math::Cephes::Matrix::mul,$safeeval,
753: '&Math::Cephes::Matrix::mul');
754: $safehole->wrap(\&Math::Cephes::Matrix::div,$safeeval,
755: '&Math::Cephes::Matrix::div');
756: $safehole->wrap(\&Math::Cephes::Matrix::inv,$safeeval,
757: '&Math::Cephes::Matrix::inv');
758: $safehole->wrap(\&Math::Cephes::Matrix::transp,$safeeval,
759: '&Math::Cephes::Matrix::transp');
760: $safehole->wrap(\&Math::Cephes::Matrix::simq,$safeeval,
761: '&Math::Cephes::Matrix::simq');
762: $safehole->wrap(\&Math::Cephes::Matrix::mat_to_vec,$safeeval,
763: '&Math::Cephes::Matrix::mat_to_vec');
764: $safehole->wrap(\&Math::Cephes::Matrix::vec_to_mat,$safeeval,
765: '&Math::Cephes::Matrix::vec_to_mat');
766: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
767: '&Math::Cephes::Matrix::check');
768: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
769: '&Math::Cephes::Matrix::check');
770:
1.215 albertel 771: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
772: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
773: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
774: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
775: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
776: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
777:
1.91 ng 778: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
779: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
780: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
781: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
782: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
783: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
784: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
785: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
786: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
787: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
788: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93 ng 789: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91 ng 790: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
791: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
792: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
793: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
794: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
795: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
796: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
797: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
798: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
1.305 albertel 799: $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
1.311 albertel 800: $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
1.322 albertel 801: $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
1.91 ng 802:
1.82 ng 803: #need to inspect this class of ops
804: # $safeeval->deny(":base_orig");
1.331 albertel 805: $safeeval->permit("require");
1.91 ng 806: $safeinit .= ';$external::target="'.$target.'";';
1.82 ng 807: &Apache::run::run($safeinit,$safeeval);
1.373 albertel 808: &initialize_rndseed($safeeval);
809: }
1.303 albertel 810:
1.393 albertel 811: sub clean_safespace {
812: my ($safeeval) = @_;
813: delete_package_recurse($safeeval->{Root});
814: }
815:
816: sub delete_package_recurse {
817: my ($package) = @_;
818: my @subp;
819: {
820: no strict 'refs';
821: while (my ($key,$val) = each(%{*{"$package\::"}})) {
822: if (!defined($val)) { next; }
823: local (*ENTRY) = $val;
824: if (defined *ENTRY{HASH} && $key =~ /::$/ &&
825: $key ne "main::" && $key ne "<none>::")
826: {
827: my ($p) = $package ne "main" ? "$package\::" : "";
828: ($p .= $key) =~ s/::$//;
829: push(@subp,$p);
830: }
831: }
832: }
833: foreach my $p (@subp) {
834: delete_package_recurse($p);
835: }
836: Symbol::delete_package($package);
837: }
838:
1.373 albertel 839: sub initialize_rndseed {
840: my ($safeeval)=@_;
841: my $rndseed;
842: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
843: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
844: my $safeinit = '$external::randomseed="'.$rndseed.'";';
845: &Apache::lonxml::debug("Setting rndseed to $rndseed");
846: &Apache::run::run($safeinit,$safeeval);
1.207 albertel 847: }
848:
849: sub default_homework_load {
850: my ($safeeval)=@_;
851: &Apache::lonxml::debug('Loading default_homework');
852: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.241 albertel 853: if ($default eq -1) {
1.207 albertel 854: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
855: } else {
856: &Apache::run::run($default,$safeeval);
857: $Apache::lonxml::default_homework_loaded=1;
858: }
1.17 albertel 859: }
860:
1.358 albertel 861: {
862: my $alarm_depth;
863: sub init_alarm {
864: alarm(0);
865: $alarm_depth=0;
866: }
867:
868: sub start_alarm {
869: if ($alarm_depth<1) {
870: my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
871: if ($old) {
872: &Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
873: }
874: }
875: $alarm_depth++;
876: }
877:
878: sub end_alarm {
879: $alarm_depth--;
880: if ($alarm_depth<1) { alarm(0); }
881: }
882: }
1.328 albertel 883: my $metamode_was;
1.55 albertel 884: sub startredirection {
1.328 albertel 885: if (!$Apache::lonxml::redirection) {
886: $metamode_was=$Apache::lonxml::metamode;
887: }
888: $Apache::lonxml::metamode=0;
889: $Apache::lonxml::redirection++;
890: push (@Apache::lonxml::outputstack, '');
1.55 albertel 891: }
892:
893: sub endredirection {
1.328 albertel 894: if (!$Apache::lonxml::redirection) {
1.380 www 895: &Apache::lonxml::error("Endredirection was called before a startredirection, perhaps you have unbalanced tags. Some debugging information:".join ":",caller);
1.328 albertel 896: return '';
897: }
898: $Apache::lonxml::redirection--;
899: if (!$Apache::lonxml::redirection) {
900: $Apache::lonxml::metamode=$metamode_was;
901: }
902: pop @Apache::lonxml::outputstack;
1.97 albertel 903: }
904:
905: sub end_tag {
906: my ($tagstack,$parstack,$token)=@_;
907: pop(@$tagstack);
908: pop(@$parstack);
909: &decreasedepth($token);
1.55 albertel 910: }
911:
1.17 albertel 912: sub initdepth {
913: @Apache::lonxml::depthcounter=();
914: $Apache::lonxml::depth=-1;
915: $Apache::lonxml::olddepth=-1;
916: }
917:
1.339 albertel 918: my @timers;
919: my $lasttime;
1.17 albertel 920: sub increasedepth {
1.19 albertel 921: my ($token) = @_;
1.17 albertel 922: $Apache::lonxml::depth++;
923: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
924: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
925: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
926: }
1.340 albertel 927: my $time;
928: if ($Apache::lonxml::debug eq "1") {
929: push(@timers,[&gettimeofday()]);
930: $time=&tv_interval($lasttime);
931: $lasttime=[&gettimeofday()];
932: }
1.339 albertel 933: my $spacing=' 'x($Apache::lonxml::depth-1);
1.42 albertel 934: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.339 albertel 935: &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : \n");
1.54 albertel 936: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 937: }
938:
939: sub decreasedepth {
1.19 albertel 940: my ($token) = @_;
1.17 albertel 941: $Apache::lonxml::depth--;
1.36 albertel 942: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
943: $#Apache::lonxml::depthcounter--;
944: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
945: }
1.43 albertel 946: if ( $Apache::lonxml::depth < -1) {
1.280 www 947: &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
1.43 albertel 948: $Apache::lonxml::depth='-1';
949: }
1.340 albertel 950: my ($timer,$time);
951: if ($Apache::lonxml::debug eq "1") {
952: $timer=pop(@timers);
953: $time=&tv_interval($lasttime);
954: $lasttime=[&gettimeofday()];
955: }
1.339 albertel 956: my $spacing=' 'x$Apache::lonxml::depth;
1.42 albertel 957: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.339 albertel 958: &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : ".&tv_interval($timer)."\n");
1.54 albertel 959: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 960: }
1.19 albertel 961:
1.399 albertel 962: sub get_id {
963: my ($parstack,$safeeval)=@_;
964: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
965: if ($env{'request.state'} eq 'construct' && $id =~ /(\.|_)/) {
966: &error(&mt("IDs are not allowed to contain "<tt>_</tt>" or "<tt>.</tt>""));
967: }
968: if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
969: return $id;
970: }
971:
1.180 albertel 972: sub get_all_text_unbalanced {
1.190 albertel 973: #there is a copy of this in lonpublisher.pm
1.326 albertel 974: my($tag,$pars)= @_;
975: my $token;
976: my $result='';
977: $tag='<'.$tag.'>';
978: while ($token = $$pars[-1]->get_token) {
979: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.386 albertel 980: if ($token->[0] eq 'T' && $token->[2]) {
1.382 albertel 981: $result.='<![CDATA['.$token->[1].']]>';
982: } else {
983: $result.=$token->[1];
984: }
1.326 albertel 985: } elsif ($token->[0] eq 'PI') {
986: $result.=$token->[2];
987: } elsif ($token->[0] eq 'S') {
988: $result.=$token->[4];
989: } elsif ($token->[0] eq 'E') {
990: $result.=$token->[2];
991: }
992: if ($result =~ /\Q$tag\E/is) {
993: ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
994: #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
995: #&Apache::lonxml::debug('Result is :'.$1);
996: $redo=$tag.$redo;
997: &Apache::lonxml::newparser($pars,\$redo);
998: last;
999: }
1000: }
1001: return $result
1.204 albertel 1002: }
1003:
1004: sub increment_counter {
1.247 albertel 1005: my ($increment) = @_;
1.289 sakharuk 1006: if (defined($increment) && $increment gt 0) {
1007: $Apache::lonxml::counter+=$increment;
1008: } else {
1009: $Apache::lonxml::counter++;
1.247 albertel 1010: }
1.289 sakharuk 1011: $Apache::lonxml::counter_changed=1;
1.204 albertel 1012: }
1013:
1014: sub init_counter {
1.391 albertel 1015: if ($env{'request.state'} eq 'construct') {
1016: $Apache::lonxml::counter=1;
1017: $Apache::lonxml::counter_changed=1;
1018: } elsif (defined($env{'form.counter'})) {
1.372 albertel 1019: $Apache::lonxml::counter=$env{'form.counter'};
1.247 albertel 1020: $Apache::lonxml::counter_changed=0;
1.237 sakharuk 1021: } else {
1.204 albertel 1022: $Apache::lonxml::counter=1;
1.247 albertel 1023: $Apache::lonxml::counter_changed=1;
1.204 albertel 1024: }
1025: }
1026:
1027: sub store_counter {
1028: &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
1.401 albertel 1029: $Apache::lonxml::counter_changed=0;
1.204 albertel 1030: return '';
1.180 albertel 1031: }
1032:
1.398 albertel 1033: {
1034: my $state;
1035: sub clear_problem_counter {
1036: undef($state);
1037: &Apache::lonnet::delenv('form.counter');
1038: &Apache::lonxml::init_counter();
1039: &Apache::lonxml::store_counter();
1040: }
1041:
1042: sub remember_problem_counter {
1043: &Apache::lonnet::transfer_profile_to_env();
1044: $state = $env{'form.counter'};
1045: }
1046:
1047: sub restore_problem_counter {
1048: if (defined($state)) {
1049: &Apache::lonnet::appenv(('form.counter' => $state));
1050: }
1051: }
1.401 albertel 1052: sub get_problem_counter {
1053: if ($Apache::lonxml::counter_changed) { &store_counter() }
1054: &Apache::lonnet::transfer_profile_to_env();
1055: return $env{'form.counter'};
1056: }
1.398 albertel 1057: }
1058:
1.19 albertel 1059: sub get_all_text {
1.270 albertel 1060: my($tag,$pars,$style)= @_;
1061: my $gotfullstack=1;
1062: if (ref($pars) ne 'ARRAY') {
1063: $gotfullstack=0;
1064: $pars=[$pars];
1065: }
1066: if (ref($style) ne 'HASH') {
1067: $style={};
1068: }
1069: my $depth=0;
1070: my $token;
1071: my $result='';
1072: if ( $tag =~ m:^/: ) {
1073: my $tag=substr($tag,1);
1074: #&Apache::lonxml::debug("have:$tag:");
1075: my $top_empty=0;
1076: while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
1077: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
1078: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
1079: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.382 albertel 1080: if ($token->[2]) {
1081: $result.='<![CDATA['.$token->[1].']]>';
1082: } else {
1083: $result.=$token->[1];
1084: }
1.270 albertel 1085: } elsif ($token->[0] eq 'PI') {
1086: $result.=$token->[2];
1087: } elsif ($token->[0] eq 'S') {
1.316 albertel 1088: if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
1089: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1090: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270 albertel 1091: $result.=$token->[4];
1092: } elsif ($token->[0] eq 'E') {
1.316 albertel 1093: if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1.270 albertel 1094: #skip sending back the last end tag
1.283 albertel 1095: if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1.270 albertel 1096: my $string=
1097: '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
1098: $$style{'/'.$token->[1]}.
1099: $token->[2].
1100: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
1101: &Apache::lonxml::newparser($pars,\$string);
1102: #&Apache::lonxml::debug("reParsing $string");
1103: next;
1104: }
1105: if ($depth > -1) {
1106: $result.=$token->[2];
1107: } else {
1108: $$pars[-1]->unget_token($token);
1109: }
1110: }
1111: }
1112: if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
1113: if (($depth >=0) && ($#$pars > 0) ) {
1114: pop(@$pars);
1115: pop(@Apache::lonxml::pwd);
1116: }
1117: }
1118: if ($top_empty && $depth >= 0) {
1119: #never found the end tag ran out of text, throw error send back blank
1120: &error('Never found end tag for <'.$tag.
1121: '> current string <pre>'.
1.314 albertel 1122: &HTML::Entities::encode($result,'<>&"').
1.270 albertel 1123: '</pre>');
1124: if ($gotfullstack) {
1125: my $newstring='</'.$tag.'>'.$result;
1126: &Apache::lonxml::newparser($pars,\$newstring);
1127: }
1128: $result='';
1129: }
1130: } else {
1131: while ($#$pars > -1) {
1132: while ($token = $$pars[-1]->get_token) {
1133: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1134: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
1135: ($token->[0] eq 'D')) {
1.382 albertel 1136: if ($token->[2]) {
1137: $result.='<![CDATA['.$token->[1].']]>';
1138: } else {
1139: $result.=$token->[1];
1140: }
1.270 albertel 1141: } elsif ($token->[0] eq 'PI') {
1142: $result.=$token->[2];
1143: } elsif ($token->[0] eq 'S') {
1.316 albertel 1144: if ( $token->[1] =~ /^\Q$tag\E$/i) {
1.270 albertel 1145: $$pars[-1]->unget_token($token); last;
1146: } else {
1147: $result.=$token->[4];
1148: }
1.316 albertel 1149: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1150: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270 albertel 1151: } elsif ($token->[0] eq 'E') {
1152: $result.=$token->[2];
1153: }
1154: }
1155: if (($#$pars > 0) ) {
1156: pop(@$pars);
1157: pop(@Apache::lonxml::pwd);
1158: } else { last; }
1159: }
1160: }
1161: #&Apache::lonxml::debug("Exit:$result:");
1162: return $result
1.19 albertel 1163: }
1164:
1.23 albertel 1165: sub newparser {
1166: my ($parser,$contentref,$dir) = @_;
1.167 albertel 1167: push (@$parser,HTML::LCParser->new($contentref));
1.365 albertel 1168: $$parser[-1]->xml_mode(1);
1169: $$parser[-1]->marked_sections(1);
1.23 albertel 1170: if ( $dir eq '' ) {
1171: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1172: } else {
1173: push (@Apache::lonxml::pwd, $dir);
1174: }
1175: }
1.1 sakharuk 1176:
1.8 albertel 1177: sub parstring {
1178: my ($token) = @_;
1179: my $temp='';
1.142 albertel 1180: foreach (@{$token->[3]}) {
1.35 www 1181: unless ($_=~/\W/) {
1.42 albertel 1182: my $val=$token->[2]->{$_};
1.231 albertel 1183: $val =~ s/([\%\@\\\"\'])/\\$1/g;
1.342 albertel 1184: $val =~ s/(\$[^{a-zA-Z_])/\\$1/g;
1.346 albertel 1185: $val =~ s/(\$)$/\\$1/;
1.51 albertel 1186: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.267 sakharuk 1187: $temp .= "my \$$_=\"$val\";";
1.20 albertel 1188: }
1.142 albertel 1189: }
1.8 albertel 1190: return $temp;
1191: }
1.22 albertel 1192:
1.384 albertel 1193: sub extlink {
1194: my ($res,$exact)=@_;
1195: if (!$exact) {
1196: $res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
1197: }
1198: push(@Apache::lonxml::extlinks,$res)
1199: }
1200:
1.34 www 1201: sub writeallows {
1.126 www 1202: unless ($#extlinks>=0) { return; }
1.377 albertel 1203: my $thisurl = &Apache::lonnet::clutter(shift);
1.372 albertel 1204: if ($env{'httpref.'.$thisurl}) {
1205: $thisurl=$env{'httpref.'.$thisurl};
1.111 www 1206: }
1.34 www 1207: my $thisdir=$thisurl;
1208: $thisdir=~s/\/[^\/]+$//;
1209: my %httpref=();
1.142 albertel 1210: foreach (@extlinks) {
1.34 www 1211: $httpref{'httpref.'.
1.125 www 1212: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142 albertel 1213: }
1.126 www 1214: @extlinks=();
1.34 www 1215: &Apache::lonnet::appenv(%httpref);
1216: }
1217:
1.281 albertel 1218: sub register_ssi {
1219: my ($url,%form)=@_;
1220: push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
1221: return '';
1222: }
1223:
1224: sub do_registered_ssi {
1225: foreach my $info (@Apache::lonxml::ssi_info) {
1226: my %form=%{ $info->{'form'}};
1227: my $url=$info->{'url'};
1228: &Apache::lonnet::ssi($url,%form);
1229: }
1230: }
1.66 www 1231: #
1232: # Afterburner handles anchors, highlights and links
1233: #
1234: sub afterburn {
1235: my $result=shift;
1.154 albertel 1236: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1237: ['highlight','anchor','link']);
1.372 albertel 1238: if ($env{'form.highlight'}) {
1239: foreach (split(/\,/,$env{'form.highlight'})) {
1.66 www 1240: my $anchorname=$_;
1241: my $matchthis=$anchorname;
1242: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1243: $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142 albertel 1244: }
1.66 www 1245: }
1.372 albertel 1246: if ($env{'form.link'}) {
1247: foreach (split(/\,/,$env{'form.link'})) {
1.66 www 1248: my ($anchorname,$linkurl)=split(/\>/,$_);
1249: my $matchthis=$anchorname;
1250: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1251: $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142 albertel 1252: }
1.66 www 1253: }
1.372 albertel 1254: if ($env{'form.anchor'}) {
1255: my $anchorname=$env{'form.anchor'};
1.66 www 1256: my $matchthis=$anchorname;
1257: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1258: $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1.66 www 1259: $result.=(<<"ENDSCRIPT");
1.226 albertel 1260: <script type="text/javascript">
1.66 www 1261: document.location.hash='$anchorname';
1262: </script>
1263: ENDSCRIPT
1264: }
1265: return $result;
1266: }
1267:
1.79 www 1268: sub storefile {
1269: my ($file,$contents)=@_;
1.290 albertel 1270: &Apache::lonnet::correct_line_ends(\$contents);
1.79 www 1271: if (my $fh=Apache::File->new('>'.$file)) {
1272: print $fh $contents;
1273: $fh->close();
1.271 www 1274: return 1;
1.147 albertel 1275: } else {
1.271 www 1276: &warning("Unable to save file $file");
1277: return 0;
1.79 www 1278: }
1279: }
1280:
1.151 albertel 1281: sub createnewhtml {
1.321 www 1282: my $title=&mt('Title of document goes here');
1283: my $body=&mt('Body of document goes here');
1284: my $filecontents=(<<SIMPLECONTENT);
1.78 www 1285: <html>
1286: <head>
1.321 www 1287: <title>$title</title>
1.78 www 1288: </head>
1289: <body bgcolor="#FFFFFF">
1.321 www 1290: $body
1.78 www 1291: </body>
1292: </html>
1293: SIMPLECONTENT
1.321 www 1294: return $filecontents;
1.151 albertel 1295: }
1296:
1.274 albertel 1297: sub createnewsty {
1298: my $filecontents=(<<SIMPLECONTENT);
1299: <definetag name="">
1300: <render>
1301: <web></web>
1302: <tex></tex>
1303: </render>
1304: </definetag>
1305: SIMPLECONTENT
1306: return $filecontents;
1307: }
1308:
1.147 albertel 1309:
1.151 albertel 1310: sub inserteditinfo {
1.274 albertel 1311: my ($result,$filecontents,$filetype)=@_;
1.314 albertel 1312: $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1.147 albertel 1313: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1.274 albertel 1314: my $xml_help = '';
1.321 www 1315: my $initialize='';
1.274 albertel 1316: if ($filetype eq 'html') {
1.323 www 1317: my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
1.333 www 1318: $initialize=&Apache::lonhtmlcommon::htmlareaheaders().
1.347 albertel 1319: &Apache::lonhtmlcommon::spellheader();
1320: if (!&Apache::lonhtmlcommon::htmlareablocked() &&
1321: &Apache::lonhtmlcommon::htmlareabrowser()) {
1322: $initialize.=(<<FULLPAGE);
1.321 www 1323: <script type="text/javascript">
1.323 www 1324: $addbuttons
1325:
1.321 www 1326: HTMLArea.loadPlugin("FullPage");
1327:
1328: function initDocument() {
1.323 www 1329: var editor=new HTMLArea("filecont",config);
1.321 www 1330: editor.registerPlugin(FullPage);
1331: editor.generate();
1332: }
1333: </script>
1334: FULLPAGE
1.347 albertel 1335: } else {
1336: $initialize.=(<<FULLPAGE);
1337: <script type="text/javascript">
1338: $addbuttons
1339: function initDocument() {
1340: }
1341: </script>
1342: FULLPAGE
1343: }
1.321 www 1344: $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
1345: $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1.274 albertel 1346: }
1347: my $cleanbut = '';
1.374 www 1348:
1.254 albertel 1349: my $titledisplay=&display_title();
1.280 www 1350: my %lt=&Apache::lonlocal::texthash('st' => 'Save this',
1351: 'vi' => 'View',
1352: 'ed' => 'Edit');
1.161 albertel 1353: my $buttons=(<<BUTTONS);
1.274 albertel 1354: $cleanbut
1.304 matthew 1355: <input type="submit" name="savethisfile" accesskey="s" value="$lt{'st'}" />
1356: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
1.161 albertel 1357: BUTTONS
1.333 www 1358: $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1.338 albertel 1359: $buttons.=&Apache::lonhtmlcommon::htmlareaselectactive('filecont');
1.78 www 1360: my $editfooter=(<<ENDFOOTER);
1.321 www 1361: $initialize
1.78 www 1362: <hr />
1363: <a name="editsection" />
1.333 www 1364: <form method="post" name="xmledit">
1.240 albertel 1365: $xml_help
1.280 www 1366: <input type="hidden" name="editmode" value="$lt{'ed'}" />
1.170 www 1367: $buttons<br />
1.366 albertel 1368: <textarea style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
1.170 www 1369: <br />$buttons
1.78 www 1370: <br />
1371: </form>
1.254 albertel 1372: $titledisplay
1.321 www 1373: </body>
1.78 www 1374: ENDFOOTER
1.147 albertel 1375: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78 www 1376: $result=~s/(\<\/body\>)/$editfooter/is;
1377: return $result;
1378: }
1379:
1.152 albertel 1380: sub get_target {
1.372 albertel 1381: my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1382: if ( $env{'request.state'} eq 'published') {
1383: if ( defined($env{'form.grade_target'})
1.152 albertel 1384: && ($viewgrades == 'F' )) {
1.372 albertel 1385: return ($env{'form.grade_target'});
1386: } elsif (defined($env{'form.grade_target'})) {
1387: if (($env{'form.grade_target'} eq 'web') ||
1388: ($env{'form.grade_target'} eq 'tex') ) {
1389: return $env{'form.grade_target'}
1.153 albertel 1390: } else {
1391: return 'web';
1392: }
1.152 albertel 1393: } else {
1394: return 'web';
1395: }
1.372 albertel 1396: } elsif ($env{'request.state'} eq 'construct') {
1397: if ( defined($env{'form.grade_target'})) {
1398: return ($env{'form.grade_target'});
1.152 albertel 1399: } else {
1400: return 'web';
1401: }
1402: } else {
1403: return 'web';
1404: }
1405: }
1406:
1.24 sakharuk 1407: sub handler {
1.255 sakharuk 1408: my $request=shift;
1409:
1410: my $target=&get_target();
1411:
1.372 albertel 1412: $Apache::lonxml::debug=$env{'user.debug'};
1.255 sakharuk 1413:
1.364 albertel 1414: &Apache::loncommon::content_type($request,'text/html');
1.255 sakharuk 1415: &Apache::loncommon::no_cache($request);
1.372 albertel 1416: if ($env{'request.state'} eq 'published') {
1.363 albertel 1417: $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
1418: 'lastrevisiondate'));
1419: }
1.255 sakharuk 1420: $request->send_http_header;
1421:
1422: return OK if $request->header_only;
1.68 www 1423:
1424:
1.255 sakharuk 1425: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.274 albertel 1426: my $filetype;
1427: if ($file =~ /\.sty$/) {
1428: $filetype='sty';
1429: } else {
1430: $filetype='html';
1431: }
1.78 www 1432: #
1433: # Edit action? Save file.
1434: #
1.372 albertel 1435: unless ($env{'request.state'} eq 'published') {
1.374 www 1436: if ($env{'form.savethisfile'}) {
1.372 albertel 1437: if (&storefile($file,$env{'form.filecont'})) {
1.309 albertel 1438: &Apache::lonxml::info("<font COLOR=\"#0000FF\">".
1439: &mt('Updated').": ".
1440: &Apache::lonlocal::locallocaltime(time).
1441: " </font>");
1.271 www 1442: }
1.255 sakharuk 1443: }
1444: }
1445: my %mystyle;
1446: my $result = '';
1447: my $filecontents=&Apache::lonnet::getfile($file);
1448: if ($filecontents eq -1) {
1.402 albertel 1449: my $start_page=&Apache::loncommon::start_page('File Error');
1450: my $end_page=&Apache::loncommon::end_page('File Error');
1.284 www 1451: my $fnf=&mt('File not found');
1.255 sakharuk 1452: $result=(<<ENDNOTFOUND);
1.402 albertel 1453: $start_page
1.284 www 1454: <b>$fnf: $file</b>
1.402 albertel 1455: $end_page
1.78 www 1456: ENDNOTFOUND
1.343 albertel 1457: $filecontents='';
1.372 albertel 1458: if ($env{'request.state'} ne 'published') {
1.274 albertel 1459: if ($filetype eq 'sty') {
1460: $filecontents=&createnewsty();
1461: } else {
1462: $filecontents=&createnewhtml();
1463: }
1.372 albertel 1464: $env{'form.editmode'}='Edit'; #force edit mode
1.255 sakharuk 1465: }
1466: } else {
1.372 albertel 1467: unless ($env{'request.state'} eq 'published') {
1.343 albertel 1468: if ($filecontents=~/BEGIN LON-CAPA Internal/) {
1.381 www 1469: &Apache::lonxml::error(&mt('This file appears to be a rendering of a LON-CAPA resource. If this is correct, this resource will act very oddly and incorrectly.'));
1.343 albertel 1470: }
1.264 www 1471: #
1472: # we are in construction space, see if edit mode forced
1.385 albertel 1473: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1474: ['editmode']);
1.255 sakharuk 1475: }
1.372 albertel 1476: if (!$env{'form.editmode'} || $env{'form.viewmode'}) {
1.255 sakharuk 1477: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1478: '',%mystyle);
1.368 albertel 1479: undef($Apache::lonhomework::parsing_a_task);
1.385 albertel 1480: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1481: ['rawmode']);
1.395 albertel 1482: if ($env{'form.rawmode'}) { $result = $filecontents; }
1.255 sakharuk 1483: }
1.147 albertel 1484: }
1.255 sakharuk 1485:
1.78 www 1486: #
1487: # Edit action? Insert editing commands
1488: #
1.372 albertel 1489: unless ($env{'request.state'} eq 'published') {
1490: if ($env{'form.editmode'} && (!($env{'form.viewmode'}))) {
1.255 sakharuk 1491: my $displayfile=$request->uri;
1492: $displayfile=~s/^\/[^\/]*//;
1.402 albertel 1493: my %options = ();
1494: if ($env{'environment.remote'} ne 'off') {
1495: $options{'bgcolor'} = '#FFFFFF';
1.349 albertel 1496: }
1.402 albertel 1497: my $start_page = &Apache::loncommon::start_page(undef,undef,
1498: \%options);
1499: $result=$start_page.
1.309 albertel 1500: &Apache::lonxml::message_location().'<h3>'.
1501: $displayfile.
1.402 albertel 1502: '</h3>'.&Apache::loncommon::end_page();
1.274 albertel 1503: $result=&inserteditinfo($result,$filecontents,$filetype);
1.255 sakharuk 1504: }
1.147 albertel 1505: }
1.402 albertel 1506: if ($filetype eq 'html') { &writeallows($request->uri); }
1.274 albertel 1507:
1.255 sakharuk 1508:
1.309 albertel 1509: &Apache::lonxml::add_messages(\$result);
1.255 sakharuk 1510: $request->print($result);
1511:
1512: return OK;
1.253 albertel 1513: }
1514:
1515: sub display_title {
1516: my $result;
1.372 albertel 1517: if ($env{'request.state'} eq 'construct') {
1.253 albertel 1518: my $title=&Apache::lonnet::gettitle();
1519: if (!defined($title) || $title eq '') {
1.372 albertel 1520: $title = $env{'request.filename'};
1.253 albertel 1521: $title = substr($title, rindex($title, '/') + 1);
1522: }
1523: $result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
1524: }
1525: return $result;
1.24 sakharuk 1526: }
1.147 albertel 1527:
1.22 albertel 1528: sub debug {
1.298 albertel 1529: if ($Apache::lonxml::debug eq "1") {
1530: $|=1;
1.300 albertel 1531: my $request=$Apache::lonxml::request;
1.388 albertel 1532: if (!$request) {
1533: eval { $request=Apache->request; };
1534: }
1535: if (!$request) {
1536: eval { $request=Apache2::RequestUtil->request; };
1537: }
1.314 albertel 1538: $request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1.346 albertel 1539: #&Apache::lonnet::logthis($_[0]);
1.298 albertel 1540: }
1.22 albertel 1541: }
1.49 albertel 1542:
1.348 albertel 1543: sub show_error_warn_msg {
1.372 albertel 1544: if ($env{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' &&
1545: &Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1.351 albertel 1546: return 1;
1547: }
1.348 albertel 1548: return (($Apache::lonxml::debug eq 1) ||
1.372 albertel 1549: ($env{'request.state'} eq 'construct') ||
1.348 albertel 1550: ($Apache::lonhomework::browse eq 'F'
1551: &&
1.372 albertel 1552: $env{'form.show_errors'} eq 'on'));
1.348 albertel 1553: }
1554:
1.22 albertel 1555: sub error {
1.336 albertel 1556: $errorcount++;
1.348 albertel 1557: if ( &show_error_warn_msg() ) {
1.336 albertel 1558: # If printing in construction space, put the error inside <pre></pre>
1559: push(@Apache::lonxml::error_messages,
1560: $Apache::lonxml::warnings_error_header.
1561: "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
1562: $Apache::lonxml::warnings_error_header='';
1563: } else {
1564: my $errormsg;
1565: my ($symb)=&Apache::lonnet::symbread();
1566: if ( !$symb ) {
1567: #public or browsers
1568: $errormsg=&mt("An error occured while processing this resource. The author has been notified.");
1.403 albertel 1569: }
1.404 albertel 1570: my $msg = join('<br />',@_);
1.336 albertel 1571: #notify author
1.403 albertel 1572: &Apache::lonmsg::author_res_msg($env{'request.filename'},$msg);
1.336 albertel 1573: #notify course
1.372 albertel 1574: if ( $symb && $env{'request.course.id'} ) {
1.380 www 1575: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1576: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.336 albertel 1577: my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
1.372 albertel 1578: my $declutter=&Apache::lonnet::declutter($env{'request.filename'});
1.336 albertel 1579: my @userlist;
1580: foreach (keys %users) {
1581: my ($user,$domain) = split(/:/, $_);
1582: push(@userlist,"$user\@$domain");
1.380 www 1583: my $key=$declutter.'_'.$user.'_'.$domain;
1584: my %lastnotified=&Apache::lonnet::get('nohist_xmlerrornotifications',
1585: [$key],
1586: $cdom,$cnum);
1587: my $now=time;
1588: if ($now-$lastnotified{$key}>86400) {
1589: &Apache::lonmsg::user_normal_msg($user,$domain,
1.403 albertel 1590: "Error [$declutter]",$msg);
1.380 www 1591: &Apache::lonnet::put('nohist_xmlerrornotifications',
1592: {$key => $now},
1593: $cdom,$cnum);
1594: }
1.336 albertel 1595: }
1.372 albertel 1596: if ($env{'request.role.adv'}) {
1.336 albertel 1597: $errormsg=&mt("An error occured while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
1598: } else {
1599: $errormsg=&mt("An error occured while processing this resource. The instructor has been notified.");
1600: }
1601: }
1602: push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1.52 albertel 1603: }
1.22 albertel 1604: }
1.49 albertel 1605:
1.22 albertel 1606: sub warning {
1.295 albertel 1607: $warningcount++;
1.261 albertel 1608:
1.372 albertel 1609: if ($env{'form.grade_target'} ne 'tex') {
1.348 albertel 1610: if ( &show_error_warn_msg() ) {
1.309 albertel 1611: push(@Apache::lonxml::warning_messages,
1612: $Apache::lonxml::warnings_error_header.
1613: "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
1.295 albertel 1614: $Apache::lonxml::warnings_error_header='';
1615: }
1616: }
1.309 albertel 1617: }
1618:
1619: sub info {
1.372 albertel 1620: if ($env{'form.grade_target'} ne 'tex'
1621: && $env{'request.state'} eq 'construct') {
1.309 albertel 1622: push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
1623: }
1624: }
1625:
1626: sub message_location {
1627: return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
1628: }
1629:
1630: sub add_messages {
1631: my ($msg)=@_;
1632: my $result=join(' ',
1633: @Apache::lonxml::info_messages,
1634: @Apache::lonxml::error_messages,
1635: @Apache::lonxml::warning_messages);
1636: undef(@Apache::lonxml::info_messages);
1637: undef(@Apache::lonxml::error_messages);
1638: undef(@Apache::lonxml::warning_messages);
1639: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
1640: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1.83 albertel 1641: }
1642:
1643: sub get_param {
1.213 albertel 1644: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1645: if ( ! $context ) { $context = -1; }
1646: my $args ='';
1647: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297 sakharuk 1648: if ( ! $Apache::lonxml::usestyle ) {
1649: $args=$Apache::lonxml::style_values.$args;
1650: }
1.213 albertel 1651: if ( ! $args ) { return undef; }
1652: if ( $case_insensitive ) {
1653: if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
1654: return &Apache::run::run("{$args;".'return $'.$param.'}',
1655: $safeeval); #'
1656: } else {
1657: return undef;
1658: }
1659: } else {
1660: if ( $args =~ /my \$\Q$param\E=\"/ ) {
1661: return &Apache::run::run("{$args;".'return $'.$param.'}',
1662: $safeeval); #'
1663: } else {
1664: return undef;
1665: }
1666: }
1.22 albertel 1667: }
1668:
1.132 albertel 1669: sub get_param_var {
1.213 albertel 1670: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1.132 albertel 1671: if ( ! $context ) { $context = -1; }
1672: my $args ='';
1673: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297 sakharuk 1674: if ( ! $Apache::lonxml::usestyle ) {
1675: $args=$Apache::lonxml::style_values.$args;
1676: }
1.230 albertel 1677: &Apache::lonxml::debug("Args are $args param is $param");
1.213 albertel 1678: if ($case_insensitive) {
1679: if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
1680: return undef;
1681: }
1682: } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
1.132 albertel 1683: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.230 albertel 1684: &Apache::lonxml::debug("first run is $value");
1.341 albertel 1685: if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1.230 albertel 1686: &Apache::lonxml::debug("doing second");
1687: my @result=&Apache::run::run("return $value",$safeeval,1);
1688: if (!defined($result[0])) {
1689: return $value
1690: } else {
1691: if (wantarray) { return @result; } else { return $result[0]; }
1692: }
1.132 albertel 1693: } else {
1694: return $value;
1695: }
1696: }
1697:
1.74 albertel 1698: sub register_insert {
1.75 albertel 1699: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74 albertel 1700: my $i;
1.76 albertel 1701: my $tagnum=0;
1.74 albertel 1702: my @order;
1703: for ($i=0;$i < $#data; $i++) {
1704: my $line = $data[$i];
1705: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1706: if ( $line =~ /TABLE/ ) { last; }
1.268 bowersj2 1707: my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
1.135 albertel 1708: if ($tag) {
1709: $insertlist{"$tagnum.tag"} = $tag;
1710: $insertlist{"$tagnum.description"} = $descrip;
1711: $insertlist{"$tagnum.color"} = $color;
1712: $insertlist{"$tagnum.function"} = $function;
1713: if (!defined($show)) { $show='yes'; }
1714: $insertlist{"$tagnum.show"}= $show;
1.268 bowersj2 1715: $insertlist{"$tagnum.helpfile"} = $helpfile;
1716: $insertlist{"$tagnum.helpdesc"} = $helpdesc;
1.135 albertel 1717: $insertlist{"$tag.num"}=$tagnum;
1718: $tagnum++;
1719: }
1.74 albertel 1720: }
1.76 albertel 1721: $i++; #skipping TABLE line
1722: $tagnum = 0;
1.74 albertel 1723: for (;$i < $#data;$i++) {
1724: my $line = $data[$i];
1.76 albertel 1725: my ($mnemonic,@which) = split(/ +/,$line);
1726: my $tag = $insertlist{"$tagnum.tag"};
1.144 matthew 1727: for (my $j=0;$j <=$#which;$j++) {
1.74 albertel 1728: if ( $which[$j] eq 'Y' ) {
1.76 albertel 1729: if ($insertlist{"$j.show"} ne 'no') {
1730: push(@{ $insertlist{"$tag.which"} },$j);
1731: }
1.74 albertel 1732: }
1733: }
1.76 albertel 1734: $tagnum++;
1.74 albertel 1735: }
1736: }
1.98 albertel 1737:
1738: sub description {
1739: my ($token)=@_;
1.138 albertel 1740: my $tagnum;
1741: my $tag=$token->[1];
1742: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1743: my $testtag=$namespace.'::'.$tag;
1744: $tagnum=$insertlist{"$testtag.num"};
1745: if (defined($tagnum)) { last; }
1746: }
1747: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1748: return $insertlist{$tagnum.'.description'};
1.268 bowersj2 1749: }
1750:
1751: # Returns a list containing the help file, and the description
1752: sub helpinfo {
1753: my ($token)=@_;
1754: my $tagnum;
1755: my $tag=$token->[1];
1756: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1757: my $testtag=$namespace.'::'.$tag;
1758: $tagnum=$insertlist{"$testtag.num"};
1759: if (defined($tagnum)) { last; }
1760: }
1761: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1762: return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'});
1.98 albertel 1763: }
1.123 albertel 1764:
1765: # ----------------------------------------------------------------- whichuser
1766: # returns a list of $symb, $courseid, $domain, $name that is correct for
1767: # calls to lonnet functions for this setup.
1768: # - looks for form.grade_ parameters
1769: sub whichuser {
1.262 matthew 1770: my ($passedsymb)=@_;
1.245 albertel 1771: my ($symb,$courseid,$domain,$name,$publicuser);
1.372 albertel 1772: if (defined($env{'form.grade_symb'})) {
1.350 albertel 1773: my ($tmp_courseid)=
1774: &Apache::loncommon::get_env_multiple('form.grade_courseid');
1775: my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
1776: if (!$allowed &&
1.372 albertel 1777: exists($env{'request.course.sec'}) &&
1778: $env{'request.course.sec'} !~ /^\s*$/) {
1.350 albertel 1779: $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid.
1.372 albertel 1780: '/'.$env{'request.course.sec'});
1.350 albertel 1781: }
1782: if ($allowed) {
1783: ($symb)=&Apache::loncommon::get_env_multiple('form.grade_symb');
1784: $courseid=$tmp_courseid;
1785: ($domain)=&Apache::loncommon::get_env_multiple('form.grade_domain');
1786: ($name)=&Apache::loncommon::get_env_multiple('form.grade_username');
1787: return ($symb,$courseid,$domain,$name,$publicuser);
1788: }
1789: }
1790: if (!$passedsymb) {
1791: $symb=&Apache::lonnet::symbread();
1.134 albertel 1792: } else {
1.350 albertel 1793: $symb=$passedsymb;
1794: }
1.372 albertel 1795: $courseid=$env{'request.course.id'};
1796: $domain=$env{'user.domain'};
1797: $name=$env{'user.name'};
1.350 albertel 1798: if ($name eq 'public' && $domain eq 'public') {
1.372 albertel 1799: if (!defined($env{'form.username'})) {
1800: $env{'form.username'}.=time.rand(10000000);
1.244 albertel 1801: }
1.372 albertel 1802: $name.=$env{'form.username'};
1.123 albertel 1803: }
1.245 albertel 1804: return ($symb,$courseid,$domain,$name,$publicuser);
1.123 albertel 1805: }
1806:
1.1 sakharuk 1807: 1;
1808: __END__
1.68 www 1809:
1810:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>