| 1 | <?php | <?php | 
| 2 |  | /** | 
|  | /* |  | 
| 3 | * Smarty plugin | * Smarty plugin | 
| 4 | * ------------------------------------------------------------- | * @package Smarty | 
| 5 | * Type:     function | * @subpackage plugins | 
| 6 | * Name:     math | */ | 
| 7 | * Purpose:  handle math computations in template |  | 
| 8 | * ------------------------------------------------------------- |  | 
| 9 |  | /** | 
| 10 |  | * Smarty {math} function plugin | 
| 11 |  | * | 
| 12 |  | * Type:     function<br> | 
| 13 |  | * Name:     math<br> | 
| 14 |  | * Purpose:  handle math computations in template<br> | 
| 15 |  | * @link http://smarty.php.net/manual/en/language.function.math.php {math} | 
| 16 |  | *          (Smarty online manual) | 
| 17 |  | * @param array | 
| 18 |  | * @param Smarty | 
| 19 |  | * @return string | 
| 20 | */ | */ | 
| 21 | function smarty_function_math($params, &$smarty) | function smarty_function_math($params, &$smarty) | 
| 22 | { | { | 
| 35 | } | } | 
| 36 |  |  | 
| 37 | // match all vars in equation, make sure all are passed | // match all vars in equation, make sure all are passed | 
| 38 | preg_match_all("![a-zA-Z][a-zA-Z0-9]*!",$equation, $match); | preg_match_all("!\!(0x)([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match); | 
| 39 | $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10', | $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10', | 
| 40 | 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan'); | 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan'); | 
| 41 |  | foreach($match[2] as $curr_var) { | 
|  | foreach($match[0] as $curr_var) { |  | 
| 42 | if (!in_array($curr_var,array_keys($params)) && !in_array($curr_var, $allowed_funcs)) { | if (!in_array($curr_var,array_keys($params)) && !in_array($curr_var, $allowed_funcs)) { | 
| 43 | $smarty->trigger_error("math: parameter $curr_var not passed as argument"); | $smarty->trigger_error("math: parameter $curr_var not passed as argument"); | 
| 44 | return; | return; | 
| 64 |  |  | 
| 65 | if (empty($params['format'])) { | if (empty($params['format'])) { | 
| 66 | if (empty($params['assign'])) { | if (empty($params['assign'])) { | 
| 67 | echo $smarty_math_result; | return $smarty_math_result; | 
| 68 | } else { | } else { | 
| 69 | $smarty->assign($params['assign'],$smarty_math_result); | $smarty->assign($params['assign'],$smarty_math_result); | 
| 70 | } | } |