* @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 . ']';
}
/**
* @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);
}
/**
* @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 . ']';
}
/**