Хранилища Subversion ant

Редакция

Редакция 2 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

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