--- loncom/xml/lonxml.pm 2004/10/05 18:51:50 1.339 +++ loncom/xml/lonxml.pm 2005/04/07 00:42:07 1.371 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # XML Parser Module # -# $Id: lonxml.pm,v 1.339 2004/10/05 18:51:50 albertel Exp $ +# $Id: lonxml.pm,v 1.371 2005/04/07 00:42:07 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -149,36 +149,49 @@ $Apache::lonxml::post_evaluate=1; $Apache::lonxml::warnings_error_header=''; sub xmlbegin { - my $output=''; - @htmlareafields=(); - if ($ENV{'browser.mathml'}) { - $output='' - .'' - .']>' + my ($style)=@_; + my $output=''; + @htmlareafields=(); + if ($ENV{'browser.mathml'}) { + $output='' + #.''."\n" +# .'] >' + .'' .''; - } else { - $output=''; - } - return $output; + .'xmlns="http://www.w3.org/1999/xhtml">'; + } else { + $output=''; + } + if ($style eq 'encode') { + $output=&HTML::Entities::encode($output,'<>&"'); + } + return $output; } sub xmlend { my ($target,$parser)=@_; my $mode='xml'; my $status='OPEN'; - if ($Apache::lonhomework::parsing_a_problem) { + if ($Apache::lonhomework::parsing_a_problem || + $Apache::lonhomework::parsing_a_task ) { $mode='problem'; $status=$Apache::inputtags::status[-1]; } - my $discussion=&Apache::lonfeedback::list_discussion($mode,$status); + my $discussion; + &Apache::loncommon::get_unprocessed_cgi + ($ENV{'query_string'},['LONCAPA_INTERNAL_no_discussion']); + if (! exists($ENV{'form.LONCAPA_INTERNAL_no_discussion'}) || + $ENV{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') { + $discussion=&Apache::lonfeedback::list_discussion($mode,$status); + } if ($target eq 'tex') { $discussion.='\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}'; &Apache::lonxml::newparser($parser,\$discussion,''); return ''; } else { - return $discussion.''; + return $discussion.&Apache::loncommon::endbodytag(); } } @@ -283,11 +296,11 @@ sub printtokenheader { } } -sub fontsettings() { +sub fontsettings { my $headerstring=''; if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { $headerstring.= - ''; + ''; } elsif (!$ENV{'browser.mathml'} && $ENV{'browser.unicode'}) { $headerstring.= ''; @@ -308,6 +321,7 @@ sub xmlparse { &setup_globals($request,$target); &Apache::inputtags::initialize_inputtags(); + &Apache::bridgetask::initialize_bridgetask(); &Apache::outputtags::initialize_outputtags(); &Apache::edit::initialize_edit(); &Apache::londefdef::initialize_londefdef(); @@ -352,8 +366,8 @@ sub xmlparse { my @stack = (); my @parstack = (); - &initdepth; - + &initdepth(); + &init_alarm(); my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars, $safeeval,\%style_for_target); @@ -362,28 +376,37 @@ sub xmlparse { } &do_registered_ssi(); if ($Apache::lonxml::counter_changed) { &store_counter() } + if ($ENV{'form.return_only_error_and_warning_counts'}) { + return "$errorcount:$warningcount"; + } return $finaloutput; } sub htmlclean { my ($raw,$full)=@_; +# Take care of CRLF etc - my $tree = HTML::TreeBuilder->new; - $tree->ignore_unknown(0); - - $tree->parse($raw); - - my $output= $tree->as_HTML(undef,' '); - - $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis; - $output=~s/\<\/(br|hr|img|meta|allow)\>//gis; + $raw=~s/\r\f/\n/gs; $raw=~s/\f\r/\n/gs; + $raw=~s/\r\n/\n/gs; $raw=~s/\n\r/\n/gs; + $raw=~s/\f/\n/gs; $raw=~s/\r/\n/gs; + $raw=~s/\&\#10\;/\n/gs; $raw=~s/\&\#13\;/\n/gs; + +# Generate empty tags, remove wrong end tags + $raw=~s/\<(br|hr|img|meta|allow|basefont)([^\>\/]*?)\>/\<$1$2 \/\>/gis; + $raw=~s/\<\/(br|hr|img|meta|allow|basefont)\>//gis; unless ($full) { - $output=~s/\<[\/]*(body|head|html)\>//gis; + $raw=~s/\<[\/]*(body|head|html)\>//gis; } - - $tree = $tree->delete; - - return $output; +# Make standard tags lowercase + foreach ('html','body','head','meta','h1','h2','h3','h4','b','i','m', + 'table','tr','td','th','p','br','hr','img','embed','font', + 'a','strong','center','title','basefont','li','ol','ul', + 'input','select','form','option','script','pre') { + $raw=~s/\<$_\s*\>/\<$_\>/gis; + $raw=~s/\<\/$_\s*\>/<\/$_\>/gis; + $raw=~s/\<$_\s([^\>]*)\>/<$_ $1\>/gis; + } + return $raw; } sub latex_special_symbols { @@ -398,18 +421,17 @@ sub latex_special_symbols { $string=~s/([^\\])\#/$1\\\#/g; } else { $string=~s/\\/\\ensuremath{\\backslash}/g; - $string=~s/([^\\]|^)\%/$1\\\%/g; - $string=~s/([^\\]|^)\$/$1\\\$/g; - $string=~s/([^\\])\_/$1\\_/g; - $string=~s/\$\$/\$\\\$/g; - $string=~s/\_\_/\_\\\_/g; - $string=~s/\#\#/\#\\\#/g; + $string=~s/\\\%|\%/\\\%/g; + $string=~s/\\{|{/\\{/g; + $string=~s/\\}|}/\\}/g; + $string=~s/\\\$|\$/\\\$/g; + $string=~s/\\\_|\_/\\\_/g; $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g; $string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less $string=&Apache::lonprintout::character_chart($string); # any & or # leftover should be safe to just escape - $string=~s/([^\\]|^)\&/$1\\\&/g; - $string=~s/([^\\]|^)\#/$1\\\#/g; + $string=~s/\\\&|\&/\\\&/g; + $string=~s/\\\#|\#/\\\#/g; $string=~s/\|/\$\\mid\$/g; #single { or } How to escape? } @@ -673,12 +695,15 @@ sub init_safespace { $safeeval->permit(":base_math"); $safeeval->permit("sort"); $safeeval->permit("time"); + $safeeval->deny("rand"); + $safeeval->deny("srand"); $safeeval->deny(":base_io"); $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse'); $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart'); $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT'); $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval, '&chem_standard_order'); + $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status'); $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin'); $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos'); @@ -780,6 +805,28 @@ sub default_homework_load { } } +{ + my $alarm_depth; + sub init_alarm { + alarm(0); + $alarm_depth=0; + } + + sub start_alarm { + if ($alarm_depth<1) { + my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'}); + if ($old) { + &Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur."); + } + } + $alarm_depth++; + } + + sub end_alarm { + $alarm_depth--; + if ($alarm_depth<1) { alarm(0); } + } +} my $metamode_was; sub startredirection { if (!$Apache::lonxml::redirection) { @@ -824,9 +871,12 @@ sub increasedepth { if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) { $Apache::lonxml::olddepth=$Apache::lonxml::depth; } - push(@timers,[&gettimeofday()]); - my $time=&tv_interval($lasttime); - $lasttime=[&gettimeofday()]; + my $time; + if ($Apache::lonxml::debug eq "1") { + push(@timers,[&gettimeofday()]); + $time=&tv_interval($lasttime); + $lasttime=[&gettimeofday()]; + } my $spacing=' 'x($Apache::lonxml::depth-1); my $curdepth=join('_',@Apache::lonxml::depthcounter); &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : \n"); @@ -844,9 +894,12 @@ sub decreasedepth { &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file.")); $Apache::lonxml::depth='-1'; } - my $timer=pop(@timers); - my $time=&tv_interval($lasttime); - $lasttime=[&gettimeofday()]; + my ($timer,$time); + if ($Apache::lonxml::debug eq "1") { + $timer=pop(@timers); + $time=&tv_interval($lasttime); + $lasttime=[&gettimeofday()]; + } my $spacing=' 'x$Apache::lonxml::depth; my $curdepth=join('_',@Apache::lonxml::depthcounter); &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : ".&tv_interval($timer)."\n"); @@ -1007,7 +1060,8 @@ sub get_all_text { sub newparser { my ($parser,$contentref,$dir) = @_; push (@$parser,HTML::LCParser->new($contentref)); - $$parser['-1']->xml_mode('1'); + $$parser[-1]->xml_mode(1); + $$parser[-1]->marked_sections(1); if ( $dir eq '' ) { push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]); } else { @@ -1022,6 +1076,8 @@ sub parstring { unless ($_=~/\W/) { my $val=$token->[2]->{$_}; $val =~ s/([\%\@\\\"\'])/\\$1/g; + $val =~ s/(\$[^{a-zA-Z_])/\\$1/g; + $val =~ s/(\$)$/\\$1/; #if ($val =~ m/^[\%\@]/) { $val="\\".$val; } $temp .= "my \$$_=\"$val\";"; } @@ -1148,7 +1204,10 @@ sub inserteditinfo { if ($filetype eq 'html') { my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons(); $initialize=&Apache::lonhtmlcommon::htmlareaheaders(). - &Apache::lonhtmlcommon::spellheader().(< $addbuttons @@ -1161,6 +1220,15 @@ $addbuttons } FULLPAGE + } else { + $initialize.=(< +$addbuttons + function initDocument() { + } + +FULLPAGE + } $result=~s/\]*)\>/\/i; $xml_help=&Apache::loncommon::helpLatexCheatsheet(); } @@ -1188,7 +1256,7 @@ $initialize $xml_help $buttons
- +
$buttons
@@ -1234,12 +1302,12 @@ sub handler { $Apache::lonxml::debug=$ENV{'user.debug'}; - if ($ENV{'browser.mathml'}) { - &Apache::loncommon::content_type($request,'text/xml'); - } else { - &Apache::loncommon::content_type($request,'text/html'); - } + &Apache::loncommon::content_type($request,'text/html'); &Apache::loncommon::no_cache($request); + if ($ENV{'request.state'} eq 'published') { + $request->set_last_modified(&Apache::lonnet::metadata($request->uri, + 'lastrevisiondate')); + } $request->send_http_header; return OK if $request->header_only; @@ -1281,7 +1349,7 @@ $bodytag ENDNOTFOUND - $filecontents=''; + $filecontents=''; if ($ENV{'request.state'} ne 'published') { if ($filetype eq 'sty') { $filecontents=&createnewsty(); @@ -1292,6 +1360,10 @@ ENDNOTFOUND } } else { unless ($ENV{'request.state'} eq 'published') { + if ($filecontents=~/BEGIN LON-CAPA Internal/) { + &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.')); + } + if ($ENV{'form.attemptclean'}) { $filecontents=&htmlclean($filecontents,1); } @@ -1303,6 +1375,7 @@ ENDNOTFOUND if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) { $result = &Apache::lonxml::xmlparse($request,$target,$filecontents, '',%mystyle); + undef($Apache::lonhomework::parsing_a_task); } } @@ -1313,7 +1386,11 @@ ENDNOTFOUND if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) { my $displayfile=$request->uri; $displayfile=~s/^\/[^\/]*//; - $result=''. + my $bodytag=''; + if ($ENV{'environment.remote'} eq 'off') { + $bodytag=&Apache::loncommon::bodytag(); + } + $result=''.$bodytag. &Apache::lonxml::message_location().'

'. $displayfile. '

'; @@ -1348,13 +1425,25 @@ sub debug { my $request=$Apache::lonxml::request; if (!$request) { $request=Apache->request; } $request->print('
DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."
\n"); -# &Apache::lonnet::logthis($_[0]); + #&Apache::lonnet::logthis($_[0]); } } +sub show_error_warn_msg { + if ($ENV{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' && + &Apache::lonnet::allowed('mdc',$ENV{'request.course.id'})) { + return 1; + } + return (($Apache::lonxml::debug eq 1) || + ($ENV{'request.state'} eq 'construct') || + ($Apache::lonhomework::browse eq 'F' + && + $ENV{'form.show_errors'} eq 'on')); +} + sub error { $errorcount++; - if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) { + if ( &show_error_warn_msg() ) { # If printing in construction space, put the error inside

 	push(@Apache::lonxml::error_messages,
 	     $Apache::lonxml::warnings_error_header.
@@ -1394,7 +1483,7 @@ sub warning {
     $warningcount++;
   
     if ($ENV{'form.grade_target'} ne 'tex') {
-	if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) {
+	if ( &show_error_warn_msg() ) {
 	    my $request=$Apache::lonxml::request;
 	    if (!$request) { $request=Apache->request; }
 	    push(@Apache::lonxml::warning_messages,
@@ -1471,7 +1560,7 @@ sub get_param_var {
   } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
   &Apache::lonxml::debug("first run is $value");
-  if ($value =~ /^[\$\@\%]\w+$/) {
+  if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
       &Apache::lonxml::debug("doing second");
       my @result=&Apache::run::run("return $value",$safeeval,1);
       if (!defined($result[0])) {
@@ -1559,29 +1648,36 @@ sub whichuser {
   my ($passedsymb)=@_;
   my ($symb,$courseid,$domain,$name,$publicuser);
   if (defined($ENV{'form.grade_symb'})) {
-    my $tmp_courseid=$ENV{'form.grade_courseid'};
-    my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
-    if ($allowed) {
-      $symb=$ENV{'form.grade_symb'};
-      $courseid=$ENV{'form.grade_courseid'};
-      $domain=$ENV{'form.grade_domain'};
-      $name=$ENV{'form.grade_username'};
-    }
-  } else {
-      if (!$passedsymb) {
-          $symb=&Apache::lonnet::symbread();
-      } else {
-          $symb=$passedsymb;
+      my ($tmp_courseid)=
+	  &Apache::loncommon::get_env_multiple('form.grade_courseid');
+      my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
+      if (!$allowed && 
+	  exists($ENV{'request.course.sec'}) && 
+	  $ENV{'request.course.sec'} !~ /^\s*$/) {
+	  $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid.
+					    '/'.$ENV{'request.course.sec'});
       }
-      $courseid=$ENV{'request.course.id'};
-      $domain=$ENV{'user.domain'};
-      $name=$ENV{'user.name'};
-      if ($name eq 'public' && $domain eq 'public') {
-	  if (!defined($ENV{'form.username'})) {
-	      $ENV{'form.username'}.=time.rand(10000000);
-	  }
-	  $name.=$ENV{'form.username'};
+      if ($allowed) {
+	  ($symb)=&Apache::loncommon::get_env_multiple('form.grade_symb');
+	  $courseid=$tmp_courseid;
+	  ($domain)=&Apache::loncommon::get_env_multiple('form.grade_domain');
+	  ($name)=&Apache::loncommon::get_env_multiple('form.grade_username');
+	  return ($symb,$courseid,$domain,$name,$publicuser);
+      }
+  }
+  if (!$passedsymb) {
+      $symb=&Apache::lonnet::symbread();
+  } else {
+      $symb=$passedsymb;
+  }
+  $courseid=$ENV{'request.course.id'};
+  $domain=$ENV{'user.domain'};
+  $name=$ENV{'user.name'};
+  if ($name eq 'public' && $domain eq 'public') {
+      if (!defined($ENV{'form.username'})) {
+	  $ENV{'form.username'}.=time.rand(10000000);
       }
+      $name.=$ENV{'form.username'};
   }
   return ($symb,$courseid,$domain,$name,$publicuser);
 }