]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.math.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.math.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r10971 - 2006-01-12 14:58:30 -0800 (Thu, 12 Jan 2006) - chris - Bug 4128: updating Smarty templates to 2.6.11, a version supposedly that plays better with PHP 5.1
22
23 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
24
25
26 */
27
28
29 /**
30  * Smarty plugin
31  * @package Smarty
32  * @subpackage plugins
33  */
34
35
36 /**
37  * Smarty {math} function plugin
38  *
39  * Type:     function<br>
40  * Name:     math<br>
41  * Purpose:  handle math computations in template<br>
42  * @link http://smarty.php.net/manual/en/language.function.math.php {math}
43  *          (Smarty online manual)
44  * @author   Monte Ohrt <monte at ohrt dot com>
45  * @param array
46  * @param Smarty
47  * @return string
48  */
49 function smarty_function_math($params, &$smarty)
50 {
51     // be sure equation parameter is present
52     if (empty($params['equation'])) {
53         $smarty->trigger_error("math: missing equation parameter");
54         return;
55     }
56
57     $equation = $params['equation'];
58
59     // make sure parenthesis are balanced
60     if (substr_count($equation,"(") != substr_count($equation,")")) {
61         $smarty->trigger_error("math: unbalanced parenthesis");
62         return;
63     }
64
65     // match all vars in equation, make sure all are passed
66     preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]+)!",$equation, $match);
67     $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10',
68                            'max','min','pi','pow','rand','round','sin','sqrt','srand','tan');
69     
70     foreach($match[1] as $curr_var) {
71         if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) {
72             $smarty->trigger_error("math: function call $curr_var not allowed");
73             return;
74         }
75     }
76
77     foreach($params as $key => $val) {
78         if ($key != "equation" && $key != "format" && $key != "assign") {
79             // make sure value is not empty
80             if (strlen($val)==0) {
81                 $smarty->trigger_error("math: parameter $key is empty");
82                 return;
83             }
84             if (!is_numeric($val)) {
85                 $smarty->trigger_error("math: parameter $key: is not numeric");
86                 return;
87             }
88             $equation = preg_replace("/\b$key\b/",$val, $equation);
89         }
90     }
91
92     eval("\$smarty_math_result = ".$equation.";");
93
94     if (empty($params['format'])) {
95         if (empty($params['assign'])) {
96             return $smarty_math_result;
97         } else {
98             $smarty->assign($params['assign'],$smarty_math_result);
99         }
100     } else {
101         if (empty($params['assign'])){
102             printf($params['format'],$smarty_math_result);
103         } else {
104             $smarty->assign($params['assign'],sprintf($params['format'],$smarty_math_result));
105         }
106     }
107 }
108
109 /* vim: set expandtab: */
110
111 ?>