15
function dsMathParseExpression($expression, $args) {
16
static $function_map = array(
35
'deg2rad' => 'deg2rad',
36
'rad2deg' => 'rad2deg',
41
$args = explode(",", $args);
42
foreach ($args as $arg) {
43
if (substr($arg,0,1) == '$')
44
$function_map[substr($arg, 1)] = $arg;
46
$function_map[$arg] = '$' . $arg;
50
$expression = preg_replace_callback('~\b[a-z]\w*\b~', function($match) use($function_map) {
51
$function = $match[0];
52
if (!isset($function_map[$function])) {
53
throw new ADEIException(translate("Illegal function '{$match[0]}'"));
55
return $function_map[$function];
58
// Remove any whitespace
59
$expression = strtolower(preg_replace('~\s+~', '', $expression));
62
if ($expression === '') {
63
throw new ADEIException(translate('Empty expression'));
67
// Invalid function calls
68
if (preg_match('~(?![$\w])[a-z]\w*(?![\(\w])~', $expression, $match) > 0) {
69
throw new ADEIException(translate("Invalid function call '{$match[0]}'"));
73
if (preg_match('~[^-+/%*&|<>!=.()0-9a-z,$]~', $expression, $match) > 0) {
74
throw new ADEIException(translate("Illegal character '{$match[0]}'"));
78
return "return({$expression});";
81
function dsMathCreateFunction($args, $expr) {
82
$expr = dsMathParseExpression($expr, $args);
83
return create_function($args, $expr);
15
86
function dsMathPreciseSubstract($a, $b) {
16
87
if ($a < $b) return -dsMathPreciseSubstract($b, $a);