Diff for /loncom/xml/lonxml.pm between versions 1.336 and 1.359

version 1.336, 2004/08/19 20:53:48 version 1.359, 2005/02/28 22:21:40
Line 51  use Math::Cephes(); Line 51  use Math::Cephes();
 use Math::Random();  use Math::Random();
 use Opcode();  use Opcode();
 use POSIX qw(strftime);  use POSIX qw(strftime);
   use Time::HiRes qw( gettimeofday tv_interval );
   
 sub register {  sub register {
   my ($space,@taglist) = @_;    my ($space,@taglist) = @_;
Line 149  $Apache::lonxml::post_evaluate=1; Line 149  $Apache::lonxml::post_evaluate=1;
 $Apache::lonxml::warnings_error_header='';  $Apache::lonxml::warnings_error_header='';
   
 sub xmlbegin {  sub xmlbegin {
   my $output='';      my ($style)=@_;
   @htmlareafields=();      my $output='';
   if ($ENV{'browser.mathml'}) {      @htmlareafields=();
       $output='<?xml version="1.0"?>'      if ($ENV{'browser.mathml'}) {
             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'   $output='<?xml version="1.0"?>'
             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '              #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'  #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
               
   #    .'<!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">] >'
       .'<!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">'
             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" '               .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
  .'xmlns="http://www.w3.org/TR/REC-html40">';      .'xmlns="http://www.w3.org/1999/xhtml">';
   } else {      } else {
       $output='<html>';   $output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
   }      }
   return $output;      if ($style eq 'encode') {
    $output=&HTML::Entities::encode($output,'<>&"');
       }
       return $output;
 }  }
   
 sub xmlend {  sub xmlend {
Line 283  sub printtokenheader { Line 289  sub printtokenheader {
     }      }
 }  }
   
 sub fontsettings() {  sub fontsettings {
     my $headerstring='';      my $headerstring='';
     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) {       if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
  $headerstring.=   $headerstring.=
     '<meta Content-Type="text/html; charset=x-mac-roman">';      '<meta Content-Type="text/html; charset=x-mac-roman" />';
     } elsif (!$ENV{'browser.mathml'} && $ENV{'browser.unicode'}) {      } elsif (!$ENV{'browser.mathml'} && $ENV{'browser.unicode'}) {
  $headerstring.=   $headerstring.=
     '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';      '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
Line 320  sub xmlparse { Line 326  sub xmlparse {
      my $bodytext=       my $bodytext=
  $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};   $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
      if ($bodytext) {       if ($bodytext) {
        my $location=&Apache::lonnet::filelocation('',$bodytext);   foreach my $file (split(',',$bodytext)) {
        my $styletext=&Apache::lonnet::getfile($location);       my $location=&Apache::lonnet::filelocation('',$file);
        if ($styletext ne '-1') {       my $styletext=&Apache::lonnet::getfile($location);
           %style_for_target = (%style_for_target,       if ($styletext ne '-1') {
                           &Apache::style::styleparser($target,$styletext));   %style_for_target = (%style_for_target,
        }        &Apache::style::styleparser($target,$styletext));
     }       }
    }
        }
  } elsif ($ENV{'construct.style'} && ($ENV{'request.state'} eq 'construct')) {   } elsif ($ENV{'construct.style'} && ($ENV{'request.state'} eq 'construct')) {
      my $location=&Apache::lonnet::filelocation('',$ENV{'construct.style'});       my $location=&Apache::lonnet::filelocation('',$ENV{'construct.style'});
      my $styletext=&Apache::lonnet::getfile($location);       my $styletext=&Apache::lonnet::getfile($location);
Line 350  sub xmlparse { Line 358  sub xmlparse {
   
  my @stack = ();   my @stack = ();
  my @parstack = ();   my @parstack = ();
  &initdepth;   &initdepth();
    &init_alarm();
  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,   my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
    $safeeval,\%style_for_target);     $safeeval,\%style_for_target);
   
Line 365  sub xmlparse { Line 373  sub xmlparse {
   
 sub htmlclean {  sub htmlclean {
     my ($raw,$full)=@_;      my ($raw,$full)=@_;
   # Take care of CRLF etc
   
     my $tree = HTML::TreeBuilder->new;      $raw=~s/\r\f/\n/gs; $raw=~s/\f\r/\n/gs;
     $tree->ignore_unknown(0);      $raw=~s/\r\n/\n/gs; $raw=~s/\n\r/\n/gs;
       $raw=~s/\f/\n/gs; $raw=~s/\r/\n/gs;
     $tree->parse($raw);      $raw=~s/\&\#10\;/\n/gs; $raw=~s/\&\#13\;/\n/gs;
   
     my $output= $tree->as_HTML(undef,' ');  # Generate empty tags, remove wrong end tags
       $raw=~s/\<(br|hr|img|meta|allow|basefont)([^\>\/]*?)\>/\<$1$2 \/\>/gis;
     $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;      $raw=~s/\<\/(br|hr|img|meta|allow|basefont)\>//gis;
     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;  
     unless ($full) {      unless ($full) {
        $output=~s/\<[\/]*(body|head|html)\>//gis;         $raw=~s/\<[\/]*(body|head|html)\>//gis;
     }      }
   # Make standard tags lowercase
     $tree = $tree->delete;      foreach ('html','body','head','meta','h1','h2','h3','h4','b','i','m',
                'table','tr','td','th','p','br','hr','img','embed','font',
     return $output;               '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 {  sub latex_special_symbols {
Line 778  sub default_homework_load { Line 792  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;  my $metamode_was;
 sub startredirection {  sub startredirection {
     if (!$Apache::lonxml::redirection) {      if (!$Apache::lonxml::redirection) {
Line 813  sub initdepth { Line 849  sub initdepth {
   $Apache::lonxml::olddepth=-1;    $Apache::lonxml::olddepth=-1;
 }  }
   
   my @timers;
   my $lasttime;
 sub increasedepth {  sub increasedepth {
   my ($token) = @_;    my ($token) = @_;
   $Apache::lonxml::depth++;    $Apache::lonxml::depth++;
Line 820  sub increasedepth { Line 858  sub increasedepth {
   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {    if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
     $Apache::lonxml::olddepth=$Apache::lonxml::depth;      $Apache::lonxml::olddepth=$Apache::lonxml::depth;
   }    }
     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);    my $curdepth=join('_',@Apache::lonxml::depthcounter);
   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");    &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : \n");
 #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";  #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
 }  }
   
Line 836  sub decreasedepth { Line 881  sub decreasedepth {
     &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));      &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
     $Apache::lonxml::depth='-1';      $Apache::lonxml::depth='-1';
   }    }
     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);    my $curdepth=join('_',@Apache::lonxml::depthcounter);
   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");    &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : ".&tv_interval($timer)."\n");
 #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";  #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
 }  }
   
Line 1010  sub parstring { Line 1062  sub parstring {
     unless ($_=~/\W/) {      unless ($_=~/\W/) {
       my $val=$token->[2]->{$_};        my $val=$token->[2]->{$_};
       $val =~ s/([\%\@\\\"\'])/\\$1/g;        $val =~ s/([\%\@\\\"\'])/\\$1/g;
         $val =~ s/(\$[^{a-zA-Z_])/\\$1/g;
         $val =~ s/(\$)$/\\$1/;
       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }        #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
       $temp .= "my \$$_=\"$val\";";        $temp .= "my \$$_=\"$val\";";
     }      }
Line 1136  sub inserteditinfo { Line 1190  sub inserteditinfo {
       if ($filetype eq 'html') {        if ($filetype eq 'html') {
   my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();    my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
   $initialize=&Apache::lonhtmlcommon::htmlareaheaders().    $initialize=&Apache::lonhtmlcommon::htmlareaheaders().
       &Apache::lonhtmlcommon::spellheader().(<<FULLPAGE);        &Apache::lonhtmlcommon::spellheader();
     if (!&Apache::lonhtmlcommon::htmlareablocked() &&
         &Apache::lonhtmlcommon::htmlareabrowser()) {
         $initialize.=(<<FULLPAGE);
 <script type="text/javascript">  <script type="text/javascript">
 $addbuttons  $addbuttons
   
Line 1149  $addbuttons Line 1206  $addbuttons
     }      }
 </script>  </script>
 FULLPAGE  FULLPAGE
             } else {
         $initialize.=(<<FULLPAGE);
   <script type="text/javascript">
   $addbuttons
       function initDocument() {
       }
   </script>
   FULLPAGE
     }
           $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;            $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
   $xml_help=&Apache::loncommon::helpLatexCheatsheet();    $xml_help=&Apache::loncommon::helpLatexCheatsheet();
       }        }
Line 1167  $cleanbut Line 1233  $cleanbut
 <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />  <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
 BUTTONS  BUTTONS
       $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');        $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
         $buttons.=&Apache::lonhtmlcommon::htmlareaselectactive('filecont');
       my $editfooter=(<<ENDFOOTER);        my $editfooter=(<<ENDFOOTER);
 $initialize  $initialize
 <hr />  <hr />
Line 1227  sub handler { Line 1294  sub handler {
  &Apache::loncommon::content_type($request,'text/html');   &Apache::loncommon::content_type($request,'text/html');
     }      }
     &Apache::loncommon::no_cache($request);      &Apache::loncommon::no_cache($request);
       $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
     'lastrevisiondate'));
     $request->send_http_header;      $request->send_http_header;
           
     return OK if $request->header_only;      return OK if $request->header_only;
Line 1268  $bodytag Line 1337  $bodytag
 </body>  </body>
 </html>  </html>
 ENDNOTFOUND  ENDNOTFOUND
     $filecontents='';          $filecontents='';
  if ($ENV{'request.state'} ne 'published') {   if ($ENV{'request.state'} ne 'published') {
     if ($filetype eq 'sty') {      if ($filetype eq 'sty') {
  $filecontents=&createnewsty();   $filecontents=&createnewsty();
Line 1279  ENDNOTFOUND Line 1348  ENDNOTFOUND
  }   }
     } else {      } else {
  unless ($ENV{'request.state'} eq 'published') {   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'}) {      if ($ENV{'form.attemptclean'}) {
  $filecontents=&htmlclean($filecontents,1);   $filecontents=&htmlclean($filecontents,1);
     }      }
Line 1300  ENDNOTFOUND Line 1373  ENDNOTFOUND
  if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {   if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
     my $displayfile=$request->uri;      my $displayfile=$request->uri;
     $displayfile=~s/^\/[^\/]*//;      $displayfile=~s/^\/[^\/]*//;
     $result='<html><body bgcolor="#FFFFFF">'.      my $bodytag='<body bgcolor="#FFFFFF">';
       if ($ENV{'environment.remote'} eq 'off') {
    $bodytag=&Apache::loncommon::bodytag();
       }
       $result='<html>'.$bodytag.
  &Apache::lonxml::message_location().'<h3>'.   &Apache::lonxml::message_location().'<h3>'.
  $displayfile.   $displayfile.
  '</h3></body></html>';   '</h3></body></html>';
Line 1335  sub debug { Line 1412  sub debug {
  my $request=$Apache::lonxml::request;   my $request=$Apache::lonxml::request;
  if (!$request) { $request=Apache->request; }   if (!$request) { $request=Apache->request; }
  $request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");   $request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\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 {  sub error {
     $errorcount++;      $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 <pre></pre>   # If printing in construction space, put the error inside <pre></pre>
  push(@Apache::lonxml::error_messages,   push(@Apache::lonxml::error_messages,
      $Apache::lonxml::warnings_error_header.       $Apache::lonxml::warnings_error_header.
Line 1381  sub warning { Line 1470  sub warning {
     $warningcount++;      $warningcount++;
       
     if ($ENV{'form.grade_target'} ne 'tex') {      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;      my $request=$Apache::lonxml::request;
     if (!$request) { $request=Apache->request; }      if (!$request) { $request=Apache->request; }
     push(@Apache::lonxml::warning_messages,      push(@Apache::lonxml::warning_messages,
Line 1458  sub get_param_var { Line 1547  sub get_param_var {
   } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }    } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'    my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
   &Apache::lonxml::debug("first run is $value");    &Apache::lonxml::debug("first run is $value");
   if ($value =~ /^[\$\@\%]\w+$/) {    if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
       &Apache::lonxml::debug("doing second");        &Apache::lonxml::debug("doing second");
       my @result=&Apache::run::run("return $value",$safeeval,1);        my @result=&Apache::run::run("return $value",$safeeval,1);
       if (!defined($result[0])) {        if (!defined($result[0])) {
Line 1546  sub whichuser { Line 1635  sub whichuser {
   my ($passedsymb)=@_;    my ($passedsymb)=@_;
   my ($symb,$courseid,$domain,$name,$publicuser);    my ($symb,$courseid,$domain,$name,$publicuser);
   if (defined($ENV{'form.grade_symb'})) {    if (defined($ENV{'form.grade_symb'})) {
     my $tmp_courseid=$ENV{'form.grade_courseid'};        my ($tmp_courseid)=
     my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);    &Apache::loncommon::get_env_multiple('form.grade_courseid');
     if ($allowed) {        my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
       $symb=$ENV{'form.grade_symb'};        if (!$allowed && 
       $courseid=$ENV{'form.grade_courseid'};    exists($ENV{'request.course.sec'}) && 
       $domain=$ENV{'form.grade_domain'};    $ENV{'request.course.sec'} !~ /^\s*$/) {
       $name=$ENV{'form.grade_username'};    $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid.
     }      '/'.$ENV{'request.course.sec'});
   } else {  
       if (!$passedsymb) {  
           $symb=&Apache::lonnet::symbread();  
       } else {  
           $symb=$passedsymb;  
       }        }
       $courseid=$ENV{'request.course.id'};        if ($allowed) {
       $domain=$ENV{'user.domain'};    ($symb)=&Apache::loncommon::get_env_multiple('form.grade_symb');
       $name=$ENV{'user.name'};    $courseid=$tmp_courseid;
       if ($name eq 'public' && $domain eq 'public') {    ($domain)=&Apache::loncommon::get_env_multiple('form.grade_domain');
   if (!defined($ENV{'form.username'})) {    ($name)=&Apache::loncommon::get_env_multiple('form.grade_username');
       $ENV{'form.username'}.=time.rand(10000000);    return ($symb,$courseid,$domain,$name,$publicuser);
   }        }
   $name.=$ENV{'form.username'};    }
     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);    return ($symb,$courseid,$domain,$name,$publicuser);
 }  }

Removed from v.1.336  
changed lines
  Added in v.1.359


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>