25 if (empty($params[
'equation'])) {
26 $smarty->trigger_error(
"math: missing equation parameter");
31 $equation = str_replace(
'`',
'',$params[
'equation']);
34 if (substr_count($equation,
"(") != substr_count($equation,
")")) {
35 $smarty->trigger_error(
"math: unbalanced parenthesis");
40 preg_match_all(
"!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match);
41 $allowed_funcs = array(
'int',
'abs',
'ceil',
'cos',
'exp',
'floor',
'log',
'log10',
42 'max',
'min',
'pi',
'pow',
'rand',
'round',
'sin',
'sqrt',
'srand',
'tan');
44 foreach($match[1] as $curr_var) {
45 if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
46 $smarty->trigger_error(
"math: function call $curr_var not allowed");
51 foreach($params as $key => $val) {
52 if ($key !=
"equation" && $key !=
"format" && $key !=
"assign") {
54 if (strlen($val)==0) {
55 $smarty->trigger_error(
"math: parameter $key is empty");
58 if (!is_numeric($val)) {
59 $smarty->trigger_error(
"math: parameter $key: is not numeric");
62 $equation = preg_replace(
"/\b$key\b/",
" \$params['$key'] ", $equation);
66 eval(
"\$smarty_math_result = ".$equation.
";");
68 if (empty($params[
'format'])) {
69 if (empty($params[
'assign'])) {
70 return $smarty_math_result;
72 $smarty->assign($params[
'assign'],$smarty_math_result);
75 if (empty($params[
'assign'])){
76 printf($params[
'format'],$smarty_math_result);
78 $smarty->assign($params[
'assign'],sprintf($params[
'format'],$smarty_math_result));