MDL-52333 qtype: PHP7-compatibility in calculate_raw()

Code also modified by Tim Hunt
This commit is contained in:
Tony Levi
2015-12-10 16:40:08 +08:00
committed by David Monllao
parent 6ace0f59ae
commit bc91b91ab1
3 changed files with 33 additions and 12 deletions
+11 -4
View File
@@ -248,11 +248,18 @@ class qtype_calculated_qe2_attempt_updater extends question_qtype_attempt_update
* @return float the computed result.
*/
protected function calculate_raw($expression) {
// This validation trick from http://php.net/manual/en/function.eval.php.
if (!@eval('return true; $result = ' . $expression . ';')) {
return '[Invalid expression ' . $expression . ']';
try {
// In older PHP versions this this is a way to validate code passed to eval.
// The trick came from http://php.net/manual/en/function.eval.php.
if (@eval('return true; $result = ' . $expression . ';')) {
return eval('return ' . $expression . ';');
}
} catch (Throwable $e) {
// PHP7 and later now throws ParseException and friends from eval(),
// which is much better.
}
return eval('return ' . $expression . ';');
// In either case of an invalid $expression, we end here.
return '[Invalid expression ' . $expression . ']';
}
/**
+11 -4
View File
@@ -433,11 +433,18 @@ class qtype_calculated_variable_substituter {
* @return float the computed result.
*/
protected function calculate_raw($expression) {
// This validation trick from http://php.net/manual/en/function.eval.php .
if (!@eval('return true; $result = ' . $expression . ';')) {
throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '', $expression);
try {
// In older PHP versions this this is a way to validate code passed to eval.
// The trick came from http://php.net/manual/en/function.eval.php.
if (@eval('return true; $result = ' . $expression . ';')) {
return eval('return ' . $expression . ';');
}
} catch (Throwable $e) {
// PHP7 and later now throws ParseException and friends from eval(),
// which is much better.
}
return eval('return ' . $expression . ';');
// In either case of an invalid $expression, we end here.
throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '', $expression);
}
/**
@@ -272,11 +272,18 @@ class qtype_calculatedmulti_qe2_attempt_updater extends question_qtype_attempt_u
* @return float the computed result.
*/
protected function calculate_raw($expression) {
// This validation trick from http://php.net/manual/en/function.eval.php.
if (!@eval('return true; $result = ' . $expression . ';')) {
return '[Invalid expression ' . $expression . ']';
try {
// In older PHP versions this this is a way to validate code passed to eval.
// The trick came from http://php.net/manual/en/function.eval.php.
if (@eval('return true; $result = ' . $expression . ';')) {
return eval('return ' . $expression . ';');
}
} catch (Throwable $e) {
// PHP7 and later now throws ParseException and friends from eval(),
// which is much better.
}
return eval('return ' . $expression . ';');
// In either case of an invalid $expression, we end here.
return '[Invalid expression ' . $expression . ']';
}
/**