upgraded smarty version
[openemr.git] / vendor / smarty / smarty / libs / plugins / function.math.php
blob655fe728d538a94c33222fc15a45212ed3e3c923
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_Internal_Template $template template object
23 * @return string|null
25 function smarty_function_math($params, $template)
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 // match all vars in equation, make sure all are passed
62 preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
64 foreach ($match[ 1 ] as $curr_var) {
65 if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
66 trigger_error("math: function call $curr_var not allowed", E_USER_WARNING);
68 return;
72 foreach ($params as $key => $val) {
73 if ($key != "equation" && $key != "format" && $key != "assign") {
74 // make sure value is not empty
75 if (strlen($val) == 0) {
76 trigger_error("math: parameter $key is empty", E_USER_WARNING);
78 return;
80 if (!is_numeric($val)) {
81 trigger_error("math: parameter $key: is not numeric", E_USER_WARNING);
83 return;
85 $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
88 $smarty_math_result = null;
89 eval("\$smarty_math_result = " . $equation . ";");
91 if (empty($params[ 'format' ])) {
92 if (empty($params[ 'assign' ])) {
93 return $smarty_math_result;
94 } else {
95 $template->assign($params[ 'assign' ], $smarty_math_result);
97 } else {
98 if (empty($params[ 'assign' ])) {
99 printf($params[ 'format' ], $smarty_math_result);
100 } else {
101 $template->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));