MDL-52333 qtype: PHP7-compatibility in calculate_raw()
Code also modified by Tim Hunt
This commit is contained in:
@@ -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 . ']';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 . ']';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user