1: package Apache::run;
2: #
3: # $Id: run.pm,v 1.46 2004/03/31 05:24:00 albertel Exp $
4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28: use HTML::Entities;
29:
30: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';
31: my %_LONCAPA_INTERNAL_oldexpressions=();
32: while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) {
33: $_LONCAPA_INTERNAL_oldexpressions{$_}=1;
34: $_ =~s/ # $1 will be the variable reference or subroutine name
35: ((?:\$|\&) #look for a starting $ or &
36: (?:[\#|\$]* #support $$ or $#$ etc.
37: [A-Za-z][\w]*| # get variable name
38: \{[A-Za-z][\w]*\})) # for ${a}
39: # $2 is 0 or more array dereferences []
40: # or hash dereferences {}
41: # the ^$ and ^& is because we do this iteratively
42: # $a[$c] becomes $a[3] which then evaluates
43: ([\[\{][^\$\&\]\}]+[\]\}])*?
44: # $3 is the list of arguments
45: (\([^\$\&\)]+\))*?
46: # only match the above if there is not { [ ( coming up
47: # Why? (I.e. this fails &a(1)[2]
48: (?=[^\[\{\(]|$)/
49: &__LC_INTERNAL_EVALUATE__($1,$2,$3)/sexg;
50: if (scalar(values(%_LONCAPA_INTERNAL_oldexpressions))>10) {last;}
51: }
52: ENDEVALUATE
53:
54: sub evaluate {
55: my ($expression,$safeeval,$decls) = @_;
56: unless (defined($expression)) { return ''; }
57: if ($Apache::lonxml::evaluate < 1) { return $expression; }
58: my $result = '';
59: $@='';
60: $Apache::run::timeout=0;
61: $main::SIG{'ALRM'} = sub {
62: $Apache::run::timeout=1;
63: die("timeout");
64: };
65: my $innererror;
66: eval {
67: alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
68: $safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
69: "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
70: $innererror=$@;
71: alarm(0);
72: };
73: my $error=$@;
74: if ($error eq '' && $innererror eq '' && !$Apache::run::timeout) {
75: $result = $safeeval->reval('return $_;');
76: chomp $result;
77: } else {
78: if ($Apache::run::timeout) {
79: $error = 'Code ran too long. It ran for more than '.
80: $Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
81: }
82: &Apache::lonxml::error('substitution on <pre>'.
83: &HTML::Entities::encode($expression,'<>&"').
84: '</pre> with <pre>'.
85: &HTML::Entities::encode($decls,'<>&"').
86: '</pre> caused <pre>'.
87: &HTML::Entities::encode($error,'<>&"').' '.
88: &HTML::Entities::encode($innererror,'<>&"').
89: '</pre>');
90: }
91: return $result
92: }
93:
94: sub run {
95: my ($code,$safeeval,$hideerrors) = @_;
96: my @result;
97: $@='';
98: $Apache::run::timeout=0;
99: $main::SIG{'ALRM'} = sub {
100: $Apache::run::timeout=1;
101: die("timeout");
102: };
103: my $innererror;
104: eval {
105: alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
106: @result=$safeeval->reval($code);
107: $innererror=$@;
108: alarm(0);
109: };
110: my $error=$@;
111: if (($Apache::run::timeout || $error ne '' || $innererror ne '') && !$hideerrors) {
112: if ($Apache::run::timeout) {
113: $error = 'Code ran too long. It ran for more than '.
114: $Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds';
115: }
116: my $errormsg='<pre>'.&HTML::Entities::encode($error,'<>&"').' '.
117: &HTML::Entities::encode($innererror,'<>&"').
118: '</pre> occured while running <pre>';
119: $code=&HTML::Entities::encode($code,'<>&"');
120: if ($innererror=~/line (\d+)/) {
121: my $linenumber=$1;
122: my @code=split("\n",$code);
123: $code[$linenumber-1]='<b><font color="red">'.
124: $code[$linenumber-1].'</font></b>';
125: $code=join("\n",@code);
126: }
127: &Apache::lonxml::error($errormsg.$code.'</pre>');
128: }
129: if ( $#result < '1') {
130: return $result[0];
131: } else {
132: &Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
133: return (@result);
134: }
135: }
136:
137: sub dump {
138: my ($target,$safeeval)=@_;
139: my $dump='';
140: foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
141: if (($symname!~/^\_/) && ($symname!~/\:$/)) {
142: my $line;
143: if ($safeeval->reval('defined($'.$symname.')')) {
144: $line='$'.$symname.'='.$safeeval->reval('$'.$symname);
145: }
146: if ($safeeval->reval('defined(@'.$symname.')')) {
147: $line='@'.$symname.'=('.
148: $safeeval->reval('join(",",@'.$symname.')').")";
149: }
150: if ($safeeval->reval('defined(%'.$symname.')')) {
151: $line='%'.$symname.'=(';
152: $line.=$safeeval->reval('join(",",map { $_."=>".$'.
153: $symname.'{$_} } sort keys %'.
154: $symname.')').")"
155: }
156: if ($line ne '') {$dump.=&HTML::Entities::encode($line,'<>&"')."<br />";}
157: }
158: }
159: $dump.='';
160: return $dump;
161: }
162:
163: 1;
164: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>