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