library/smarty/libs/plugins/function.math.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  * This plugin is only for Smarty2 BC
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage PluginsFunction
       
     8  */
       
     9 
       
    10 /**
       
    11  * Smarty {math} function plugin
       
    12  * Type:     function<br>
       
    13  * Name:     math<br>
       
    14  * Purpose:  handle math computations in template
       
    15  *
       
    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>
       
    19  *
       
    20  * @param array $params parameters
       
    21  * @param Smarty_Internal_Template $template template object
       
    22  *
       
    23  * @return string|null
       
    24  */
       
    25 function smarty_function_math($params, $template) {
       
    26     static $_allowed_funcs = array(
       
    27         'int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
       
    28         'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true,
       
    29         'rand' => true, 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true
       
    30     );
       
    31     // be sure equation parameter is present
       
    32     if (empty($params['equation'])) {
       
    33         trigger_error("math: missing equation parameter", E_USER_WARNING);
       
    34 
       
    35         return;
       
    36     }
       
    37 
       
    38     $equation = $params['equation'];
       
    39 
       
    40     // make sure parenthesis are balanced
       
    41     if (substr_count($equation, "(") != substr_count($equation, ")")) {
       
    42         trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
       
    43 
       
    44         return;
       
    45     }
       
    46 
       
    47     // match all vars in equation, make sure all are passed
       
    48     preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!", $equation, $match);
       
    49 
       
    50     foreach ($match[1] as $curr_var) {
       
    51         if ($curr_var && !isset($params[$curr_var]) && !isset($_allowed_funcs[$curr_var])) {
       
    52             trigger_error("math: function call $curr_var not allowed", E_USER_WARNING);
       
    53 
       
    54             return;
       
    55         }
       
    56     }
       
    57 
       
    58     foreach ($params as $key => $val) {
       
    59         if ($key != "equation" && $key != "format" && $key != "assign") {
       
    60             // make sure value is not empty
       
    61             if (strlen($val) == 0) {
       
    62                 trigger_error("math: parameter $key is empty", E_USER_WARNING);
       
    63 
       
    64                 return;
       
    65             }
       
    66             if (!is_numeric($val)) {
       
    67                 trigger_error("math: parameter $key: is not numeric", E_USER_WARNING);
       
    68 
       
    69                 return;
       
    70             }
       
    71             $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
       
    72         }
       
    73     }
       
    74     $smarty_math_result = null;
       
    75     eval("\$smarty_math_result = " . $equation . ";");
       
    76 
       
    77     if (empty($params['format'])) {
       
    78         if (empty($params['assign'])) {
       
    79             return $smarty_math_result;
       
    80         } else {
       
    81             $template->assign($params['assign'], $smarty_math_result);
       
    82         }
       
    83     } else {
       
    84         if (empty($params['assign'])) {
       
    85             printf($params['format'], $smarty_math_result);
       
    86         } else {
       
    87             $template->assign($params['assign'], sprintf($params['format'], $smarty_math_result));
       
    88         }
       
    89     }
       
    90 }