version 1.29, 2002/05/23 21:15:34
|
version 1.37, 2003/06/09 21:37:54
|
Line 29 use HTML::Entities;
|
Line 29 use HTML::Entities;
|
|
|
$Apache::run::EVALUATE_STRING=<<'ENDEVALUATE'; |
$Apache::run::EVALUATE_STRING=<<'ENDEVALUATE'; |
my %_LONCAPA_INTERNAL_oldexpressions=(); |
my %_LONCAPA_INTERNAL_oldexpressions=(); |
my $i=0; |
|
while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) { |
while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) { |
$_LONCAPA_INTERNAL_oldexpressions{$_}=1; |
$_LONCAPA_INTERNAL_oldexpressions{$_}=1; |
$_ =~s/((?:\$|\&)(?:[\#|\$]*[A-Za-z][\w]*|\{[A-Za-z][\w]*\}))([\[\{][^\$\&\]\}]+[\]\}])*?(\([^\$\&\)]+\))*?(?=[^\[\{\(]|$)/eval(defined(eval($1.$2))?eval('$1.$2.$3'):'$1.$2.$3')/seg; |
$_ =~s/((?:\$|\&)(?:[\#|\$]*[A-Za-z][\w]*|\{[A-Za-z][\w]*\}))([\[\{][^\$\&\]\}]+[\]\}])*?(\([^\$\&\)]+\))*?(?=[^\[\{\(]|$)/eval(defined(eval($1.$2))?eval('$1.$2.$3'):'$1.$2.$3')/seg; |
if ($i++ > 10 ) { last; } |
if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;} |
} |
} |
ENDEVALUATE |
ENDEVALUATE |
|
|
sub evaluate { |
sub evaluate { |
my ($expression,$safeeval,$decls) = @_; |
my ($expression,$safeeval,$decls) = @_; |
unless (defined($expression)) { return ''; } |
&Apache::lonxml::debug("got $expression and decls $decls"); |
if (!$Apache::lonxml::evaluate) { return $expression; } |
unless (defined($expression)) { return ''; } |
my $result = ''; |
if ($Apache::lonxml::evaluate < 1) { return $expression; } |
$@=''; |
my $result = ''; |
if ($Apache::lonxml::request) { |
$@=''; |
$Apache::lonxml::request->hard_timeout("Apache::run::run, evaluation $code"); |
$Apache::run::timeout=0; |
} |
$main::SIG{'ALRM'} = sub { |
$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression. |
$Apache::run::timeout=1; |
"\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}'); |
die("timeout"); |
if ($Apache::lonxml::request) { $Apache::lonxml::request->kill_timeout; } |
}; |
# $safeeval->reval('{'.$decls.';<< &evaluate(q|'.$expression.'|);}'); |
my $innererror; |
my $error=$@; |
eval { |
if ($@ eq '') { |
alarm($Apache::lonnet::perlvar{'lonScriptTimeout'}); |
$result = $safeeval->reval('return $_;'); |
$safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression. |
chomp $result; |
"\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}'); |
} else { |
$innererror=$@; |
&Apache::lonxml::error('substitution on <pre>'.$expression. |
alarm(0); |
'</pre> with <pre>'.$decls. |
}; |
'</pre> caused <pre>'.$error); |
my $error=$@; |
} |
if ($error eq '' && $innererror eq '' && !$Apache::run::timeout) { |
return $result |
$result = $safeeval->reval('return $_;'); |
|
chomp $result; |
|
} else { |
|
if ($Apache::run::timeout) { |
|
$error = 'Code ran too long. It ran for more than '. |
|
Apache->request->server->timeout.' seconds'; |
|
} |
|
&Apache::lonxml::error('substitution on <pre>'. |
|
&HTML::Entities::encode($expression). |
|
'</pre> with <pre>'. |
|
&HTML::Entities::encode($decls). |
|
'</pre> caused <pre>'. |
|
&HTML::Entities::encode($error).' '. |
|
&HTML::Entities::encode($innererror). |
|
'</pre>'); |
|
} |
|
return $result |
} |
} |
|
|
sub run { |
sub run { |
my ($code,$safeeval,$hideerrors) = @_; |
my ($code,$safeeval,$hideerrors) = @_; |
# print "inside run\n"; |
my @result; |
$@=''; |
$@=''; |
if ($Apache::lonxml::request) { |
$Apache::run::timeout=0; |
$Apache::lonxml::request->hard_timeout("Apache::run::run, evaluation $code"); |
$main::SIG{'ALRM'} = sub { |
} |
$Apache::run::timeout=1; |
my (@result)=$safeeval->reval($code); |
die("timeout"); |
if ($Apache::lonxml::request) { $Apache::lonxml::request->kill_timeout; } |
}; |
my $error=$@; |
my $innererror; |
if ($error ne '' && !$hideerrors) { |
eval { |
&Apache::lonxml::error('<pre>'.&HTML::Entities::encode($error). |
alarm($Apache::lonnet::perlvar{'lonScriptTimeout'}); |
'</pre> occured while running <pre>'. |
@result=$safeeval->reval($code); |
&HTML::Entities::encode($code).'</pre>'); |
$innererror=$@; |
} |
alarm(0); |
if ( $#result < '1') { |
}; |
return $result[0]; |
my $error=$@; |
} else { |
if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) { |
&Apache::lonxml::debug("<b>Got lots results</b>:$#result:"); |
if ($Apache::run::timeout) { |
return (@result); |
$error = 'Code ran too long. It ran for more than '. |
} |
Apache->request->server->timeout.' seconds'; |
|
} |
|
&Apache::lonxml::error('<pre>'.&HTML::Entities::encode($error).' '. |
|
&HTML::Entities::encode($innererror). |
|
'</pre> occured while running <pre>'. |
|
&HTML::Entities::encode($code).'</pre>'); |
|
} |
|
if ( $#result < '1') { |
|
return $result[0]; |
|
} else { |
|
&Apache::lonxml::debug("<b>Got lots results</b>:$#result:"); |
|
return (@result); |
|
} |
} |
} |
|
|
sub dump { |
sub dump { |