Code

Updated smarty to 3.0 rc4
[gosa.git] / gosa-core / include / smarty / plugins / function.math.php
1 <?php
2 /**
3  * Smarty plugin
4  *
5  * This plugin is only for Smarty2 BC
6  * @package Smarty
7  * @subpackage PluginsFunction
8  */
10 /**
11  * Smarty {math} function plugin
12  *
13  * Type:     function<br>
14  * Name:     math<br>
15  * Purpose:  handle math computations in template<br>
16  * @link http://smarty.php.net/manual/en/language.function.math.php {math}
17  *          (Smarty online manual)
18  * @author   Monte Ohrt <monte at ohrt dot com>
19  * @param array $params parameters
20  * @param object $smarty Smarty object
21  * @param object $template template object
22  * @return string|null
23  */
24 function smarty_function_math($params, $smarty, $template)
25 {
26     // be sure equation parameter is present
27     if (empty($params['equation'])) {
28         trigger_error("math: missing equation parameter",E_USER_WARNING);
29         return;
30     }
32     $equation = $params['equation'];
34     // make sure parenthesis are balanced
35     if (substr_count($equation,"(") != substr_count($equation,")")) {
36         trigger_error("math: unbalanced parenthesis",E_USER_WARNING);
37         return;
38     }
40     // match all vars in equation, make sure all are passed
41     preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match);
42     $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10',
43                            'max','min','pi','pow','rand','round','sin','sqrt','srand','tan');
44     
45     foreach($match[1] as $curr_var) {
46         if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
47             trigger_error("math: function call $curr_var not allowed",E_USER_WARNING);
48             return;
49         }
50     }
52     foreach($params as $key => $val) {
53         if ($key != "equation" && $key != "format" && $key != "assign") {
54             // make sure value is not empty
55             if (strlen($val)==0) {
56                 trigger_error("math: parameter $key is empty",E_USER_WARNING);
57                 return;
58             }
59             if (!is_numeric($val)) {
60                 trigger_error("math: parameter $key: is not numeric",E_USER_WARNING);
61                 return;
62             }
63             $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
64         }
65     }
66     $smarty_math_result = null;
67     eval("\$smarty_math_result = ".$equation.";");
69     if (empty($params['format'])) {
70         if (empty($params['assign'])) {
71             return $smarty_math_result;
72         } else {
73             $template->assign($params['assign'],$smarty_math_result);
74         }
75     } else {
76         if (empty($params['assign'])){
77             printf($params['format'],$smarty_math_result);
78         } else {
79             $template->assign($params['assign'],sprintf($params['format'],$smarty_math_result));
80         }
81     }
82 }
84 ?>