updated smarty
[openemr.git] / vendor / smarty / smarty / libs / plugins / function.math.php
blobd0ce1e6711cb6ce6a6b12fcfae4b5072e7089965
1 <?php
2 /**
3 * Smarty plugin
4 * This plugin is only for Smarty2 BC
6 * @package Smarty
7 * @subpackage PluginsFunction
8 */
10 /**
11 * Smarty {math} function plugin
12 * Type: function<br>
13 * Name: math<br>
14 * Purpose: handle math computations in template
16 * @link http://www.smarty.net/manual/en/language.function.math.php {math}
17 * (Smarty online manual)
18 * @author Monte Ohrt <monte at ohrt dot com>
20 * @param array $params parameters
21 * @param Smarty
23 * @return string|null
25 function smarty_function_math($params, &$smarty)
27 static $_allowed_funcs =
28 array('int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
29 'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true, 'rand' => true,
30 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true);
31 // be sure equation parameter is present
32 if (empty($params[ 'equation' ])) {
33 trigger_error("math: missing equation parameter", E_USER_WARNING);
35 return;
38 $equation = $params[ 'equation' ];
40 // make sure parenthesis are balanced
41 if (substr_count($equation, "(") != substr_count($equation, ")")) {
42 trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
44 return;
47 // disallow backticks
48 if (strpos($equation, '`') !== false) {
49 trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
51 return;
54 // also disallow dollar signs
55 if (strpos($equation, '$') !== false) {
56 trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
58 return;
61 foreach ($params as $key => $val) {
62 if ($key != "equation" && $key != "format" && $key != "assign") {
63 // make sure value is not empty
64 if (strlen($val) == 0) {
65 trigger_error("math: parameter '{$key}' is empty", E_USER_WARNING);
67 return;
69 if (!is_numeric($val)) {
70 trigger_error("math: parameter '{$key}' is not numeric", E_USER_WARNING);
72 return;
77 // match all vars in equation, make sure all are passed
78 preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
80 foreach ($match[ 1 ] as $curr_var) {
81 if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
82 trigger_error("math: function call '{$curr_var}' not allowed, or missing parameter '{$curr_var}'", E_USER_WARNING);
84 return;
88 foreach ($params as $key => $val) {
89 if ($key != "equation" && $key != "format" && $key != "assign") {
90 $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
93 $smarty_math_result = null;
94 eval("\$smarty_math_result = " . $equation . ";");
96 if (empty($params[ 'format' ])) {
97 if (empty($params[ 'assign' ])) {
98 return $smarty_math_result;
99 } else {
100 $smarty->assign($params[ 'assign' ], $smarty_math_result);
102 } else {
103 if (empty($params[ 'assign' ])) {
104 printf($params[ 'format' ], $smarty_math_result);
105 } else {
106 $smarty->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));